-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_lifecycle_test.go
More file actions
65 lines (56 loc) · 2.04 KB
/
Copy pathnode_lifecycle_test.go
File metadata and controls
65 lines (56 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package willow
import (
"strings"
"testing"
)
// OnEnter fires when a node enters the live scene tree; OnReady fires once the
// first time; OnExit fires when it leaves. Re-adding fires OnEnter again but not
// OnReady.
func TestNodeLifecycleHooks(t *testing.T) {
scene := NewScene()
var ev []string
child := NewContainer("child")
child.OnEnter = func() { ev = append(ev, "enter") }
child.OnReady = func() { ev = append(ev, "ready") }
child.OnExit = func() { ev = append(ev, "exit") }
scene.Root.AddChild(child) // enter, ready
scene.Root.RemoveChild(child) // exit
scene.Root.AddChild(child) // enter (no second ready)
got := strings.Join(ev, ",")
want := "enter,ready,exit,enter"
if got != want {
t.Fatalf("hook sequence = %q, want %q", got, want)
}
}
// Adding/removing a subtree fires hooks for every node: enter top-down (parent
// before child), exit bottom-up (child before parent).
func TestNodeLifecycleSubtreeOrder(t *testing.T) {
scene := NewScene()
var ev []string
parent := NewContainer("parent")
child := NewContainer("child")
parent.AddChild(child) // parent not in scene yet -> no hooks fire
parent.OnEnter = func() { ev = append(ev, "parent-enter") }
child.OnEnter = func() { ev = append(ev, "child-enter") }
parent.OnExit = func() { ev = append(ev, "parent-exit") }
child.OnExit = func() { ev = append(ev, "child-exit") }
scene.Root.AddChild(parent) // enter: parent then child
scene.Root.RemoveChild(parent) // exit: child then parent
got := strings.Join(ev, ",")
want := "parent-enter,child-enter,child-exit,parent-exit"
if got != want {
t.Fatalf("subtree hook order = %q, want %q", got, want)
}
}
// A node built and attached under a parent that is not yet in the scene gets no
// hooks until the whole subtree is added to the live tree.
func TestNodeLifecycleNoFireBeforeScene(t *testing.T) {
var fired bool
parent := NewContainer("parent")
child := NewContainer("child")
child.OnEnter = func() { fired = true }
parent.AddChild(child)
if fired {
t.Fatal("OnEnter fired while detached from any scene")
}
}