/
SignalTest.as
195 lines (162 loc) · 4.53 KB
/
SignalTest.as
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package org.osflash.signals
{
import asunit.asserts.*;
import asunit4.async.addAsync;
import org.osflash.signals.events.GenericEvent;
import org.osflash.signals.events.IEvent;
import flash.display.Sprite;
public class SignalTest
{
public var completed:Signal;
[Before]
public function setUp():void
{
completed = new Signal();
}
[After]
public function tearDown():void
{
completed.removeAll();
completed = null;
}
[Test]
public function numListeners_is_0_after_creation():void
{
assertEquals(0, completed.numListeners);
}
//////
[Test(async)]
public function dispatch_should_pass_event_to_listener_but_not_set_signal_or_target_properties():void
{
completed.add(addAsync(checkGenericEvent, 10));
completed.dispatch(new GenericEvent());
}
protected function checkGenericEvent(e:GenericEvent):void
{
assertNull('event.signal is not set by Signal', e.signal);
assertNull('event.target is not set by Signal', e.target);
}
//////
[Test(async)]
public function add_two_listeners_and_dispatch_should_call_both():void
{
completed.add(addAsync(checkGenericEvent, 10));
completed.add(addAsync(checkGenericEvent, 10));
completed.dispatch(new GenericEvent());
}
//////
[Test]
public function addOnce_and_dispatch_should_remove_listener_automatically():void
{
completed.addOnce(newEmptyHandler());
completed.dispatch(new GenericEvent());
assertEquals('there should be no listeners', 0, completed.numListeners);
}
//////
[Test]
public function add_listener_then_remove_then_dispatch_should_not_call_listener():void
{
completed.add(failIfCalled);
completed.remove(failIfCalled);
completed.dispatch(new GenericEvent());
}
private function failIfCalled(e:IEvent):void
{
fail('This event handler should not have been called.');
}
//////
[Test]
public function add_listener_then_remove_function_not_in_listeners_should_do_nothing():void
{
completed.add(newEmptyHandler());
completed.remove(newEmptyHandler());
assertEquals(1, completed.numListeners);
}
//////
[Test(async)]
public function add_2_listeners_remove_2nd_then_dispatch_should_call_1st_not_2nd_listener():void
{
completed.add(addAsync(checkGenericEvent, 10));
completed.add(failIfCalled);
completed.remove(failIfCalled);
completed.dispatch(new GenericEvent());
}
//////
[Test]
public function add_2_listeners_should_yield_numListeners_of_2():void
{
completed.add(newEmptyHandler());
completed.add(newEmptyHandler());
assertEquals(2, completed.numListeners);
}
private function newEmptyHandler():Function
{
return function(e:*):void {};
}
//////
[Test]
public function add_2_listeners_then_remove_1_should_yield_numListeners_of_1():void
{
var firstFunc:Function = newEmptyHandler();
completed.add(firstFunc);
completed.add(newEmptyHandler());
completed.remove(firstFunc);
assertEquals(1, completed.numListeners);
}
[Test]
public function add_2_listeners_then_removeAll_should_yield_numListeners_of_0():void
{
completed.add(newEmptyHandler());
completed.add(newEmptyHandler());
completed.removeAll();
assertEquals(0, completed.numListeners);
}
[Test]
public function add_same_listener_twice_should_only_add_it_once():void
{
var func:Function = newEmptyHandler();
completed.add(func);
completed.add(func);
assertEquals(1, completed.numListeners);
}
//////
[Test]
public function dispatch_non_IEvent_without_error():void
{
completed.addOnce(checkSprite);
// Sprite doesn't have a target property,
// so if the signal tried to set .target,
// an error would be thrown and this test would fail.
completed.dispatch(new Sprite());
}
private function checkSprite(sprite:Sprite):void
{
assertTrue(sprite is Sprite);
}
//////
[Test]
public function dispatch_2_listeners_1st_listener_removes_itself_then_2nd_listener_is_still_called():void
{
completed.add(selfRemover);
// addAsync verifies the second listener is called
completed.add(addAsync(newEmptyHandler(), 10));
completed.dispatch();
}
private function selfRemover():void
{
completed.remove(selfRemover);
}
//////
[Test]
public function dispatch_2_listeners_1st_listener_removes_all_then_2nd_listener_is_still_called():void
{
completed.add(addAsync(allRemover, 10));
completed.add(addAsync(newEmptyHandler(), 10));
completed.dispatch();
}
private function allRemover():void
{
completed.removeAll();
}
}
}