-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcontroller_test.go
More file actions
151 lines (122 loc) · 3.71 KB
/
Copy pathcontroller_test.go
File metadata and controls
151 lines (122 loc) · 3.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package animation
import (
"math"
"testing"
"time"
)
func TestControllerTickReturnsFalseWhenEmpty(t *testing.T) {
ctrl := NewController()
active := ctrl.Tick(16 * time.Millisecond)
if active {
t.Error("empty controller should return false")
}
}
func TestControllerHasActive(t *testing.T) {
ctrl := NewController()
if ctrl.HasActive() {
t.Error("empty controller should have no active")
}
sig := newMockSignal(0)
To(sig, 1.0).Duration(100 * time.Millisecond).Start(ctrl)
if !ctrl.HasActive() {
t.Error("controller should have active after adding animation")
}
}
func TestControllerAutoCancel(t *testing.T) {
sig := newMockSignal(0)
ctrl := NewController()
// Start first animation.
To(sig, 1.0).
From(0.0).
Duration(100 * time.Millisecond).
Ease(Linear).
Start(ctrl)
ctrl.Tick(50 * time.Millisecond)
midVal := sig.Get()
if math.Abs(float64(midVal-0.5)) > 0.05 {
t.Errorf("first animation at 50ms: got %v, want ~0.5", midVal)
}
// Start second animation on same signal — should auto-cancel first.
To(sig, 0.0).
Duration(100 * time.Millisecond).
Ease(Linear).
Start(ctrl)
// Second animation starts from current value (~0.5) to 0.
ctrl.Tick(50 * time.Millisecond)
// Should be roughly midway between ~0.5 and 0 = ~0.25.
if math.Abs(float64(sig.Get()-0.25)) > 0.1 {
t.Errorf("second animation at 50ms: got %v, want ~0.25", sig.Get())
}
}
func TestControllerCancelAll(t *testing.T) {
ctrl := NewController()
sig1 := newMockSignal(0)
sig2 := newMockSignal(0)
To(sig1, 1.0).Duration(100 * time.Millisecond).Start(ctrl)
To(sig2, 1.0).Duration(100 * time.Millisecond).Start(ctrl)
if !ctrl.HasActive() {
t.Error("should have active animations")
}
ctrl.CancelAll()
if ctrl.HasActive() {
t.Error("should have no active after CancelAll")
}
}
func TestControllerCancelSpecific(t *testing.T) {
ctrl := NewController()
sig1 := newMockSignal(0)
sig2 := newMockSignal(0)
To(sig1, 1.0).Duration(100 * time.Millisecond).Start(ctrl)
To(sig2, 1.0).Duration(100 * time.Millisecond).Start(ctrl)
ctrl.Cancel(sig1)
// sig2 should still be active.
active := ctrl.Tick(50 * time.Millisecond)
if !active {
t.Error("sig2 animation should still be active")
}
if sig1.Get() != 0 {
t.Errorf("sig1 should not have been updated: got %v", sig1.Get())
}
}
func TestControllerMultipleSignals(t *testing.T) {
ctrl := NewController()
sig1 := newMockSignal(0)
sig2 := newMockSignal(0)
To(sig1, 1.0).From(0.0).Duration(100 * time.Millisecond).Ease(Linear).Start(ctrl)
To(sig2, 2.0).From(0.0).Duration(100 * time.Millisecond).Ease(Linear).Start(ctrl)
ctrl.Tick(50 * time.Millisecond)
if math.Abs(float64(sig1.Get()-0.5)) > 0.05 {
t.Errorf("sig1 at 50ms: got %v, want ~0.5", sig1.Get())
}
if math.Abs(float64(sig2.Get()-1.0)) > 0.05 {
t.Errorf("sig2 at 50ms: got %v, want ~1.0", sig2.Get())
}
}
func TestControllerSpringAutoCancel(t *testing.T) {
sig := newMockSignal(0)
ctrl := NewController()
// Start tween, then spring on same signal.
To(sig, 1.0).Duration(100 * time.Millisecond).Start(ctrl)
SpringTo(sig, 2.0).Stiffness(StiffnessHigh).DampingRatio(DampingNoBouncy).Start(ctrl)
// Should have only one active (the spring replaced the tween).
count := len(ctrl.active)
if count != 1 {
t.Errorf("expected 1 active animation, got %d", count)
}
}
func TestControllerCleanup(t *testing.T) {
ctrl := NewController()
sig := newMockSignal(0)
To(sig, 1.0).Duration(50 * time.Millisecond).Ease(Linear).Start(ctrl)
// Complete the animation.
ctrl.Tick(50 * time.Millisecond)
if ctrl.HasActive() {
t.Error("completed animation should be cleaned up")
}
}
func TestControllerNewReturnsNonNil(t *testing.T) {
ctrl := NewController()
if ctrl == nil {
t.Error("NewController should return non-nil")
}
}