├── .gitignore ├── libs ├── flexunit.swc └── as3-signals-v0.9-BETA.swc ├── src └── org │ └── osflash │ └── signals │ └── utils │ ├── registerFailureSignal.as │ ├── failOnSignal.as │ ├── proceedOnSignal.as │ ├── handleSignal.as │ ├── SignalAsync.as │ └── SignalAsyncEvent.as ├── tests ├── TestRunner.as └── org │ └── osflash │ └── signals │ └── utils │ └── SignalAsyncTest.as ├── MIT-LICENSE.txt ├── README.textile └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /.settings 2 | /bin 3 | /.as3_classpath 4 | /.project 5 | *.iml 6 | .idea/ 7 | target/ 8 | -------------------------------------------------------------------------------- /libs/flexunit.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evan-liu/as3-signals-utilities-async/HEAD/libs/flexunit.swc -------------------------------------------------------------------------------- /libs/as3-signals-v0.9-BETA.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evan-liu/as3-signals-utilities-async/HEAD/libs/as3-signals-v0.9-BETA.swc -------------------------------------------------------------------------------- /src/org/osflash/signals/utils/registerFailureSignal.as: -------------------------------------------------------------------------------- 1 | package org.osflash.signals.utils 2 | { 3 | import org.flexunit.async.Async; 4 | import org.osflash.signals.ISignal; 5 | /** 6 | * Delegate function for Async.registerFailureEvent(). 7 | * @author eidiot 8 | */ 9 | public function registerFailureSignal(testCase:Object, signal:ISignal):void 10 | { 11 | Async.registerFailureEvent(testCase, new SignalAsync(signal), SignalAsyncEvent.CALLED); 12 | } 13 | } -------------------------------------------------------------------------------- /tests/TestRunner.as: -------------------------------------------------------------------------------- 1 | package 2 | { 3 | import org.flexunit.internals.TraceListener; 4 | import org.flexunit.runner.FlexUnitCore; 5 | import org.osflash.signals.utils.SignalAsyncTest; 6 | 7 | import flash.display.Sprite; 8 | /** 9 | * @author eidiot 10 | */ 11 | public class TestRunner extends Sprite 12 | { 13 | public function TestRunner() 14 | { 15 | super(); 16 | var flexUnit:FlexUnitCore = new FlexUnitCore(); 17 | flexUnit.addListener(new TraceListener()); 18 | flexUnit.run(SignalAsyncTest); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/org/osflash/signals/utils/failOnSignal.as: -------------------------------------------------------------------------------- 1 | package org.osflash.signals.utils 2 | { 3 | import org.flexunit.async.Async; 4 | import org.osflash.signals.ISignal; 5 | /** 6 | * Delegate function for Async.failOnSignal(). 7 | * @author eidiot 8 | */ 9 | public function failOnSignal(testCase:Object, signal:ISignal, 10 | timeout:int = 500, 11 | timeoutHandler:Function = null):void 12 | { 13 | Async.failOnEvent(testCase, new SignalAsync(signal), 14 | SignalAsyncEvent.CALLED, timeout, timeoutHandler); 15 | } 16 | } -------------------------------------------------------------------------------- /src/org/osflash/signals/utils/proceedOnSignal.as: -------------------------------------------------------------------------------- 1 | package org.osflash.signals.utils 2 | { 3 | import org.flexunit.async.Async; 4 | import org.osflash.signals.ISignal; 5 | /** 6 | * Delegate function for Async.proceedOnSignal(). 7 | * @author eidiot 8 | */ 9 | public function proceedOnSignal(testCase:Object, signal:ISignal, 10 | timeout:int = 500, 11 | timeoutHandler:Function = null):void 12 | { 13 | Async.proceedOnEvent(testCase, new SignalAsync(signal), 14 | SignalAsyncEvent.CALLED, timeout, timeoutHandler); 15 | } 16 | } -------------------------------------------------------------------------------- /src/org/osflash/signals/utils/handleSignal.as: -------------------------------------------------------------------------------- 1 | package org.osflash.signals.utils 2 | { 3 | import org.flexunit.async.Async; 4 | import org.osflash.signals.ISignal; 5 | /** 6 | * Delegate function for Async.handleSignal(). 7 | * @author eidiot 8 | */ 9 | public function handleSignal(testCase:Object, signal:ISignal, 10 | signalHandler:Function, 11 | timeout:int = 500, 12 | passThroughData:Object = null, 13 | timeoutHandler:Function = null):void 14 | { 15 | Async.handleEvent(testCase, new SignalAsync(signal), 16 | SignalAsyncEvent.CALLED, signalHandler, timeout, 17 | passThroughData, timeoutHandler); 18 | } 19 | } -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 eidiot 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /src/org/osflash/signals/utils/SignalAsync.as: -------------------------------------------------------------------------------- 1 | package org.osflash.signals.utils 2 | { 3 | import org.osflash.signals.ISignal; 4 | 5 | import flash.events.EventDispatcher; 6 | /** 7 | * Utility call to test objects that has signals. 8 | * 9 | * @author eidiot 10 | */ 11 | public class SignalAsync extends EventDispatcher 12 | { 13 | //====================================================================== 14 | // Constructor 15 | //====================================================================== 16 | /** 17 | * Construct a SignalAsync. 18 | */ 19 | public function SignalAsync(signal:ISignal) 20 | { 21 | signal.addOnce(onCalled); 22 | } 23 | //====================================================================== 24 | // Callbacks 25 | //====================================================================== 26 | private function onCalled(a:* = null, b:* = null, c:* = null, d:* = null, 27 | e:* = null, f:* = null, g:* = null, h:* = null):void 28 | { 29 | dispatchEvent(new SignalAsyncEvent(SignalAsyncEvent.CALLED, arguments)); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/org/osflash/signals/utils/SignalAsyncEvent.as: -------------------------------------------------------------------------------- 1 | package org.osflash.signals.utils 2 | { 3 | import flash.events.Event; 4 | /** Signal Async event. 5 | * @author eidiot 6 | */ 7 | public class SignalAsyncEvent extends Event 8 | { 9 | //====================================================================== 10 | // Class constants 11 | //====================================================================== 12 | public static const CALLED:String = "called"; 13 | //====================================================================== 14 | // Constructor 15 | //====================================================================== 16 | public function SignalAsyncEvent(type:String, args:Array) 17 | { 18 | super(type); 19 | _args = args; 20 | } 21 | //---------------------------------- 22 | // args 23 | //---------------------------------- 24 | private var _args:Array; 25 | /** 26 | * Arguments of the called signal. 27 | */ 28 | public function get args():Array 29 | { 30 | return _args; 31 | } 32 | //====================================================================== 33 | // Overridden methods 34 | //====================================================================== 35 | override public function clone():Event 36 | { 37 | return new SignalAsyncEvent(type, _args); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /tests/org/osflash/signals/utils/SignalAsyncTest.as: -------------------------------------------------------------------------------- 1 | package org.osflash.signals.utils 2 | { 3 | import org.flexunit.asserts.assertEquals; 4 | import org.osflash.signals.Signal; 5 | /** 6 | * @author eidiot 7 | */ 8 | public class SignalAsyncTest 9 | { 10 | //====================================================================== 11 | // Test methods 12 | //====================================================================== 13 | [Test(async)] 14 | public function test_proceedOnSignal():void 15 | { 16 | var signal:Signal = new Signal(); 17 | proceedOnSignal(this, signal); 18 | signal.dispatch(); 19 | } 20 | [Test(async)] 21 | public function test_handleSignal():void 22 | { 23 | var signal:Signal = new Signal(); 24 | handleSignal(this, signal, verify_handleSignal, 500, {"name":"Tom", "age":20}); 25 | signal.dispatch("Tom", 20); 26 | } 27 | [Test(async)] 28 | public function test_failOnSignal():void 29 | { 30 | var signal:Signal = new Signal(); 31 | failOnSignal(this, signal); 32 | //signal.dispatch(); 33 | } 34 | [Test(async)] 35 | public function test_registerFailureSignal():void 36 | { 37 | var signal:Signal = new Signal(); 38 | registerFailureSignal(this, signal); 39 | //signal.dispatch(); 40 | } 41 | //====================================================================== 42 | // Verify methods 43 | //====================================================================== 44 | private function verify_handleSignal(event:SignalAsyncEvent, data:Object):void 45 | { 46 | assertEquals(event.args[0], data.name); 47 | assertEquals(event.args[1], data.age); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h2. Utility classes for unit test signals. 2 | 3 | This library is a utility for "Robert Penner":http://robertpenner.com/ 's "as3-signals":http://github.com/robertpenner/as3-signals with some delegate methods to test them with FlexUnit4: 4 | 5 | * proceedOnSignal 6 | * handleSignal 7 | * failOnSignal 8 | * registerFailureSignal 9 | 10 | h3. proceedOnSignal 11 | 12 | Use this method to ensure that some signal is dispatched during an asynchronous test. 13 | 14 |
15 | [Test(async)]
16 | public function test_proceedOnSignal():void 
17 | {
18 |     var model:IModel = new SomeModel();
19 |     proceedOnSignal(this, model.changedSignal);
20 |     model.doSomethingChange();
21 | }
22 | 
23 | 24 | h3. handleSignal 25 | 26 | Use this method to ensure that some signal is dispatched and do more assertions in the handler. The handler method must have two arguments. The first one is a SignalAsyncEvent, you can get all arguments passed by the signal's dispatch() method by event.args. The second Object typed argument is the data passed by the passThroughData argument in the handleSignal method. 27 | 28 |
29 | [Test(async)] 
30 | public function change_user():void 
31 | {
32 |     var model:IModel = new SomeModel();
33 |     handleSignal(this, model.changedSignal, verify_user, 500, {name:"Tom", age:20});
34 |     model.changeUser("Tom", 20);
35 | }
36 | private function verify_user(event:SignalAsyncEvent, data:Object):void 
37 | {
38 |     assertEquals(event.args[ 0 ], data.name);
39 |     assertEquals(event.args[ 1 ], data.age);
40 | }
41 | 
42 | 43 | h3. failOnSignal 44 | 45 | Use this method to ensure that some signal is not dispatched during an asynchronous test in a time period. 46 | 47 |
48 | [Test(async)] 
49 | public function not_changed():void 
50 | {
51 |     var model:IModel = new SomeModel();
52 |     failOnSignal(model.changedSignal);
53 |     model.doSomethingNotChange();
54 | }
55 | 
56 | 57 | h3. registerFailureSignal 58 | 59 | Use this method to fail a test when a signal is dispatched. Think you are waiting for a success signal of a service, you will want to fail the test when the service's error signal is dispatched instead of waiting until timeout. 60 | 61 |
62 | [Test(async)] 
63 | public function call_service():void 
64 | {
65 |     var service:IService = new SomeService();
66 |     registerFailureSignal(this, service.failedSignal);
67 |     proceedOnSignal(this, service.successSignal);
68 |     service.call();
69 | }
70 | 
-------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | org.osflash 8 | as3-signals-utilities-async 9 | 1.0-SNAPSHOT 10 | swc 11 | as3-signals-utilities-async 12 | 13 | 14 | 4.1.0.16076 15 | 3.7.1 16 | 17 | 18 | 19 | src 20 | tests 21 | 22 | 23 | org.sonatype.flexmojos 24 | flexmojos-maven-plugin 25 | ${flexmojos.version} 26 | true 27 | 28 | 29 | true 30 | true 31 | true 32 | 33 | 34 | *Test.as 35 | 36 | 37 | 38 | org.osflash.signals.utils.failOnSignal 39 | org.osflash.signals.utils.handleSignal 40 | org.osflash.signals.utils.proceedOnSignal 41 | org.osflash.signals.utils.registerFailureSignal 42 | org.osflash.signals.utils.SignalAsync 43 | org.osflash.signals.utils.SignalAsyncEvent 44 | 45 | 46 | 47 | 48 | 49 | com.adobe.flex 50 | compiler 51 | ${flex.sdk.version} 52 | pom 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | com.adobe.flex.framework 64 | flex-framework 65 | ${flex.sdk.version} 66 | pom 67 | 68 | 69 | 70 | org.osflash 71 | as3-signals 72 | 0.7.0 73 | swc 74 | 75 | 76 | 77 | com.adobe.flexunit 78 | flexunit 79 | 4.1.0-beta3 80 | swc 81 | 82 | 83 | 84 | 85 | 86 | 87 | --------------------------------------------------------------------------------