PageRenderTime 21ms CodeModel.GetById 6ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/org/osflash/signals/MonoSignalTest.as

http://github.com/robertpenner/as3-signals
ActionScript | 308 lines | 229 code | 72 blank | 7 comment | 4 complexity | 7da83a52a456c8a6eeb7ccbdcd2dc221 MD5 | raw file
  1package org.osflash.signals
  2{
  3	import asunit.asserts.assertEquals;
  4	import asunit.asserts.assertNotNull;
  5	import asunit.asserts.assertNull;
  6	import asunit.asserts.assertTrue;
  7	import asunit.asserts.fail;
  8	import asunit.framework.IAsync;
  9
 10	import org.osflash.signals.events.GenericEvent;
 11	import org.osflash.signals.events.IEvent;
 12
 13	import flash.display.Sprite;
 14	import flash.errors.IllegalOperationError;
 15	
 16	/**
 17	 * @author Simon Richardson - me@simonrichardson.info
 18	 */
 19	public class MonoSignalTest
 20	{
 21		[Inject]
 22	    public var async:IAsync;
 23		
 24		public var signal:MonoSignal;
 25		
 26		[Before]
 27		public function setUp():void
 28		{
 29			signal = new MonoSignal();
 30		}
 31
 32		[After]
 33		public function tearDown():void
 34		{
 35			signal.removeAll();
 36			signal = null;
 37		}
 38		
 39		[Test]
 40		public function numListeners_is_0_after_creation():void
 41		{
 42			assertEquals(0, signal.numListeners);
 43		}
 44				
 45
 46		
 47		[Test]
 48		public function dispatch_should_pass_event_to_listener_but_not_set_signal_or_target_properties():void
 49		{
 50			signal.add(async.add(checkGenericEvent, 10));
 51			signal.dispatch(new GenericEvent());
 52		}
 53		
 54		protected function checkGenericEvent(e:GenericEvent):void
 55		{
 56			assertNull('event.signal is not set by Signal', e.signal);
 57			assertNull('event.target is not set by Signal', e.target);
 58		}
 59		
 60
 61		
 62		[Test(expects="flash.errors.IllegalOperationError")]
 63		public function add_two_listeners_should_throw_an_error():void
 64		{
 65			signal.add(checkGenericEvent);
 66			signal.add(checkGenericEvent);
 67		}
 68		
 69
 70		
 71		[Test(expects="flash.errors.IllegalOperationError")]
 72		public function add_one_hundred_listeners_should_throw_an_error():void
 73		{
 74			for(var i:int = 0; i<100; i++)
 75			{
 76				signal.add(checkGenericEvent);
 77			}
 78		}
 79		
 80
 81		
 82		[Test]
 83		public function add_one_listeners_then_remove_it_then_add_another_listener():void
 84		{
 85			signal.add(failIfCalled);
 86			signal.remove(failIfCalled);
 87			signal.add(checkGenericEvent);
 88			signal.dispatch(new GenericEvent());
 89		}	
 90		
 91
 92		
 93		[Test]
 94		public function addOnce_and_dispatch_should_remove_listener_automatically():void
 95		{
 96			signal.addOnce(newEmptyHandler());
 97			signal.dispatch(new GenericEvent());
 98			assertEquals('there should be no listeners', 0, signal.numListeners);
 99		}
100		
101
102		
103		[Test]
104		public function add_listener_then_remove_then_dispatch_should_not_call_listener():void
105		{
106			signal.add(failIfCalled);
107			signal.remove(failIfCalled);
108			signal.dispatch(new GenericEvent());
109		}
110		
111		private function failIfCalled(e:IEvent):void
112		{
113			fail('This event handler should not have been called.');
114		}
115		
116
117		
118		[Test]
119		public function add_listener_then_remove_function_not_in_listeners_should_do_nothing():void
120		{
121			signal.add(newEmptyHandler());
122			signal.remove(newEmptyHandler());
123			assertEquals(1, signal.numListeners);
124		}
125		
126		private function newEmptyHandler():Function
127		{
128			return function(e:*):void {};
129		}
130		
131
132		
133		[Test(expects="flash.errors.IllegalOperationError")]
134		public function addOnce_same_listener_twice_should_throw_error():void
135		{
136			var func:Function = newEmptyHandler();
137			signal.addOnce(func);
138			signal.addOnce(func);
139		}
140		
141
142		[Test]
143		public function dispatch_non_IEvent_without_error():void
144		{
145			signal.addOnce(checkSprite);
146			// Sprite doesn't have a target property,
147			// so if the signal tried to set .target,
148			// an error would be thrown and this test would fail.
149			signal.dispatch(new Sprite());
150		}
151		
152		private function checkSprite(sprite:Sprite):void
153		{
154			assertTrue(sprite is Sprite);
155		}
156		
157
158		[Test]
159		public function adding_a_listener_during_dispatch_should_not_call_it():void
160		{
161			signal.add(async.add(addListenerDuringDispatch, 10));
162			signal.dispatch(new GenericEvent());
163		}
164		
165		private function addListenerDuringDispatch():void
166		{
167			try
168			{
169				signal.add(failIfCalled);
170			}
171			catch(error:IllegalOperationError)
172			{
173				assertTrue('there should be 1 listener', signal.numListeners == 1);
174			}
175			catch(error:Error)
176			{
177				fail('This error should not have been thrown.');
178			}
179		}
180		
181
182		
183		[Test]
184		public function removed_listener_should_return_slot():void
185		{
186			var listener:Function = function():void{};
187			var slot:ISlot = signal.add(listener);
188			
189			assertTrue("Slot is returned", slot == signal.remove(listener));
190		}
191		
192
193		
194		[Test]
195		public function removed_listener_should_be_returned():void
196		{
197			var slot:ISlot = signal.add(function():void{});
198			var listener:Function = slot.listener;
199			
200			assertTrue("Slot is returned", slot == signal.remove(listener));
201		}
202		
203		
204		[Test]
205		public function slot_params_are_null_when_created():void
206		{
207			var listener:Function = newEmptyHandler();
208			var slot:ISlot = signal.add(listener);
209
210			assertNull('params should be null', slot.params); 
211		}
212
213		[Test]
214		public function slot_params_should_not_be_null_after_adding_array():void
215		{
216			var listener:Function = newEmptyHandler();
217			var slot:ISlot = signal.add(listener);
218			slot.params = [];
219
220			assertNotNull('params should not be null', slot.params); 
221		}
222
223		[Test]
224		public function slot_params_with_one_param_should_be_sent_through_to_listener():void
225		{
226			var listener:Function = function(...args):void
227									{ 
228										assertTrue(args[0] is int);
229										assertEquals(args[0], 1234);
230									};
231
232			var slot:ISlot = signal.add(listener);
233			slot.params = [1234];
234
235			signal.dispatch();
236		}	
237
238		[Test]
239		public function slot_params_with_multiple_params_should_be_sent_through_to_listener():void
240		{
241			var listener:Function = function(...args):void
242									{ 
243										assertTrue(args[0] is int);
244										assertEquals(args[0], 12345);
245										
246										assertTrue(args[1] is String);
247										assertEquals(args[1], 'text');
248										
249										assertTrue(args[2] is Sprite);
250										assertEquals(args[2], slot.params[2]);
251									};
252
253			var slot:ISlot = signal.add(listener);
254			slot.params = [12345, 'text', new Sprite()];
255
256			signal.dispatch();
257		}
258				
259		[Test]
260		public function verify_chaining_of_slot_params():void
261		{
262			var listener:Function = function(...args):void
263									{ 
264										assertEquals(args.length, 1);
265										assertEquals(args[0], 1234567);
266									};
267			
268			signal.add(listener).params = [1234567];
269						
270			signal.dispatch();
271		}
272		
273		[Test]
274		public function verify_chaining_and_concat_of_slot_params():void
275		{
276			var listener:Function = function(...args):void
277									{ 
278										assertEquals(args.length, 2);
279										assertEquals(args[0], 12345678);
280										assertEquals(args[1], 'text');
281									};
282			
283			signal.add(listener).params = [12345678].concat(['text']);
284						
285			signal.dispatch();
286		}
287		
288		
289		[Test]
290		public function verify_chaining_and_pushing_on_to_slot_params():void
291		{
292			var listener:Function = function(...args):void
293									{ 
294										assertEquals(args.length, 2);
295										assertEquals(args[0], 123456789);
296										assertEquals(args[1], 'text');
297									};
298			
299			// This is ugly, but I put money on somebody will attempt to do this!
300			
301			var slots:ISlot;
302			(slots = signal.add(listener)).params = [123456789];
303			slots.params.push('text');
304			
305			signal.dispatch();
306		}
307	}
308}