PageRenderTime 47ms CodeModel.GetById 33ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/org/osflash/signals/natives/sets/EventDispatcherSignalSetTest.as

http://github.com/robertpenner/as3-signals
ActionScript | 217 lines | 149 code | 51 blank | 17 comment | 10 complexity | f2c547d731986515b7f546691b8a16d8 MD5 | raw file
  1package org.osflash.signals.natives.sets
  2{
  3	import org.osflash.signals.ISlot;
  4	import asunit.asserts.fail;
  5	import asunit.asserts.assertNotNull;
  6	import asunit.asserts.assertSame;
  7	import asunit.asserts.assertTrue;
  8	import asunit.framework.IAsync;
  9
 10	import flash.display.Sprite;
 11	import flash.events.Event;
 12	/**
 13	 * @author Simon Richardson - simon@ustwo.co.uk
 14	 */
 15	public class EventDispatcherSignalSetTest
 16	{
 17		
 18		[Inject]
 19		public var async:IAsync;
 20		
 21		private var sprite:Sprite;
 22		
 23		private var signalSet:EventDispatcherSignalSet;
 24
 25		[Before]
 26		public function setUp():void 
 27		{
 28			sprite = new Sprite();
 29			signalSet = new EventDispatcherSignalSet(sprite);
 30		}
 31
 32		[After]
 33		public function tearDown():void 
 34		{
 35			signalSet.removeAll();
 36			signalSet = null;
 37			sprite = null;
 38		}
 39		
 40		
 41		protected function newEmptyHandler():Function
 42		{
 43			return function(e:*):void {};
 44		}
 45		
 46		private function handleEvent(event:Event):void
 47		{
 48			assertSame(sprite, event.target);
 49		}
 50		
 51		protected function failIfCalled(e:Event):void
 52		{
 53			fail('This event handler should not have been called.');
 54		}
 55		
 56		//////
 57		
 58		[Test]
 59		public function numListeners_should_be_zero():void
 60		{
 61			assertTrue('Number of listeners should be 0.', signalSet.numListeners == 0);
 62		}
 63		
 64		//////
 65		
 66		[Test]
 67		public function signals_should_be_not_null():void
 68		{
 69			assertNotNull('Signals should not be null.', signalSet.signals);
 70		}
 71		
 72		//////
 73		
 74		[Test]
 75		public function signals_length_should_be_empty():void
 76		{
 77			assertTrue('Signals length should be 0.', signalSet.signals.length == 0);
 78		}
 79		
 80		//////
 81		
 82		[Test]
 83		public function signals_length_should_be_zero_after_removeAll():void
 84		{
 85			signalSet.removeAll();
 86			
 87			assertTrue('Signals length should be 0.', signalSet.signals.length == 0);
 88		}
 89		
 90		//////
 91		
 92		[Test]
 93		public function numListeners_should_be_zero_after_removeAll():void
 94		{
 95			signalSet.removeAll();
 96			
 97			assertTrue('Number of listeners should be 0.', signalSet.numListeners == 0);
 98		}
 99		
100		//////
101		
102		[Test]
103		public function activate_signal_should_not_be_null():void
104		{
105			assertNotNull('Activate NativeSignal should not be null', signalSet.activate);
106		}
107		
108		//////
109		
110		[Test]
111		public function deactivate_signal_should_not_be_null():void
112		{
113			assertNotNull('Deactivate NativeSignal should not be null', signalSet.deactivate);
114		}
115		
116		//////
117		
118		[Test]
119		public function add_activate_then_EventDispatcher_dispatch_should_call_signal_listener():void
120		{
121			signalSet.activate.add(async.add(handleEvent));
122			sprite.dispatchEvent(new Event(Event.ACTIVATE));
123		}
124
125		//////
126		
127		[Test]
128		public function add_deactivate_then_EventDispatcher_dispatch_should_call_signal_listener():void
129		{
130			signalSet.deactivate.add(async.add(handleEvent));
131			sprite.dispatchEvent(new Event(Event.DEACTIVATE));
132		}
133		
134		//////
135		
136		[Test]
137		public function add_activate_then_pause_and_dispatch_should_not_call_signal_listener():void
138		{
139			const slot:ISlot = signalSet.activate.add(failIfCalled);
140			slot.enabled = false;
141			
142			sprite.dispatchEvent(new Event(Event.ACTIVATE));
143		}
144		
145		//////
146		
147		[Test]
148		public function add_deactivate_then_pause_and_dispatch_should_not_call_signal_listener():void
149		{
150			const slot:ISlot = signalSet.deactivate.add(failIfCalled);
151			slot.enabled = false;
152			
153			sprite.dispatchEvent(new Event(Event.DEACTIVATE));
154		}
155		
156		//////
157		
158		[Test]
159		public function add_activate_then_numListeners_should_be_one():void
160		{
161			signalSet.activate.add(handleEvent);
162			assertTrue('Number of listeners should be 1', signalSet.numListeners == 1);
163		}
164		
165		//////
166		
167		[Test]
168		public function add_deactivate_then_numListeners_should_be_one():void
169		{
170			signalSet.deactivate.add(handleEvent);
171			assertTrue('Number of listeners should be 1', signalSet.numListeners == 1);
172		}
173		
174		//////
175		
176		[Test]
177		public function add_too_both_signals_then_numListeners_should_be_two():void
178		{
179			signalSet.activate.add(handleEvent);
180			signalSet.deactivate.add(handleEvent);
181			assertTrue('Number of listeners should be 2', signalSet.numListeners == 2);
182		}	
183		
184		[Test]
185		public function add_too_both_signals_then_remove_activate_numListeners_should_be_one():void
186		{
187			signalSet.activate.add(handleEvent);
188			signalSet.deactivate.add(handleEvent);
189			
190			signalSet.activate.removeAll();
191			
192			assertTrue('Number of listeners should be 1', signalSet.numListeners == 1);
193		}		
194		
195		[Test]
196		public function add_too_both_signals_then_remove_deactivate_numListeners_should_be_one():void
197		{
198			signalSet.activate.add(handleEvent);
199			signalSet.deactivate.add(handleEvent);
200			
201			signalSet.deactivate.removeAll();
202			
203			assertTrue('Number of listeners should be 1', signalSet.numListeners == 1);
204		}
205		
206		[Test]
207		public function add_too_both_signals_then_removeAll_numListeners_should_be_zero():void
208		{
209			signalSet.activate.add(handleEvent);
210			signalSet.deactivate.add(handleEvent);
211			
212			signalSet.removeAll();
213			
214			assertTrue('Number of listeners should be 0', signalSet.numListeners == 0);
215		}
216	}
217}