This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git


The following commit(s) were added to refs/heads/develop by this push:
     new a22ca2f  RoyaleUnit: added Async.requireEvent() and Async.failOnEvent()
a22ca2f is described below

commit a22ca2f40c537b7a85720322b4b09bf4cf07e434
Author: Josh Tynjala <[email protected]>
AuthorDate: Wed Sep 25 13:35:56 2019 -0700

    RoyaleUnit: added Async.requireEvent() and Async.failOnEvent()
---
 .../royale/org/apache/royale/test/async/Async.as   |  74 +++++++++++
 .../RoyaleUnit/src/test/royale/tests/AsyncTests.as | 136 +++++++++++++++++++++
 2 files changed, 210 insertions(+)

diff --git 
a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/async/Async.as
 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/async/Async.as
index 206d330..fe97961 100644
--- 
a/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/async/Async.as
+++ 
b/frameworks/projects/RoyaleUnit/src/main/royale/org/apache/royale/test/async/Async.as
@@ -18,11 +18,18 @@
 
////////////////////////////////////////////////////////////////////////////////
 package org.apache.royale.test.async
 {
+       import org.apache.royale.events.Event;
+       import org.apache.royale.events.EventDispatcher;
+       import org.apache.royale.test.Assert;
+
        /**
         * Helper functions for tests marked with <code>[Test(async)]</code> 
metadata.
         */
        public class Async
        {
+               /**
+                * Calls a function after a delay, measured in milliseconds.
+                */
                public static function delayCall(testCase:Object, 
delayedFunction:Function, delayMS:int):void
                {
                        var handler:IAsyncHandler = 
AsyncLocator.getAsyncHandlerForTest(testCase);
@@ -31,5 +38,72 @@ package org.apache.royale.test.async
                                delayedFunction();
                        }, delayMS);
                }
+               
+               /**
+                * If an event is not dispatched within the specified delay 
(measured in
+                * milliseconds), the test will fail.
+                */
+               public static function requireEvent(testCase:Object, 
dispatcher:EventDispatcher, eventName:String, timeout:int):void
+               {
+                       handleEvent(testCase, dispatcher, eventName, null, 
timeout);
+               }
+               
+               /**
+                * Similar to <code>requireEvent()</code>, requires an event to 
be
+                * dispatched for the test to pass, but the arguments are also 
passed to
+                * a custom event handler that may perform additional checks.
+                */
+               public static function handleEvent(testCase:Object, 
dispatcher:EventDispatcher, eventName:String, eventHandler:Function, 
timeout:int):void
+               {
+                       var eventDispatched:Boolean = false;
+                       function handleDispatch(...rest:Array):void
+                       {
+                               eventDispatched = true;
+                               if(eventHandler != null)
+                               {
+                                       eventHandler.apply(null, rest);
+                               }
+                       }
+                       dispatcher.addEventListener(eventName, handleDispatch);
+                       var handler:IAsyncHandler = 
AsyncLocator.getAsyncHandlerForTest(testCase);
+                       handler.asyncHandler(function():void
+                       {
+                               dispatcher.removeEventListener(eventName, 
handleDispatch);
+                               if(!eventDispatched)
+                               {
+                                       Assert.fail("Timeout occurred before 
expected event: " + eventName);
+                               }
+                       }, timeout);
+               }
+               
+               /**
+                * If an event is dispatched within the specified delay, the 
test case
+                * will fail.
+                */
+               public static function failOnEvent(testCase:Object, 
dispatcher:EventDispatcher, eventName:String, timeout:int):void
+               {
+                       var savedEvent:Event = null;
+                       var eventDispatched:Boolean = false;
+                       function handleDispatch(event:Event, ...rest:Array):void
+                       {
+                               savedEvent = event;
+                               eventDispatched = true;
+                       }
+                       dispatcher.addEventListener(eventName, handleDispatch);
+                       var handler:IAsyncHandler = 
AsyncLocator.getAsyncHandlerForTest(testCase);
+                       handler.asyncHandler(function():void
+                       {
+                               dispatcher.removeEventListener(eventName, 
handleDispatch);
+                               if(eventDispatched)
+                               {
+                                       var message:String = "Unexpected event 
" + eventName + "dispatched";
+                                       if(savedEvent != null)
+                                       {
+                                               message += " " + 
String(savedEvent);
+                                       }
+                                       Assert.fail(message);
+                               }
+                       }, timeout);
+               }
        }
 }
\ No newline at end of file
diff --git a/frameworks/projects/RoyaleUnit/src/test/royale/tests/AsyncTests.as 
b/frameworks/projects/RoyaleUnit/src/test/royale/tests/AsyncTests.as
index 3d817c7..68d2a34 100644
--- a/frameworks/projects/RoyaleUnit/src/test/royale/tests/AsyncTests.as
+++ b/frameworks/projects/RoyaleUnit/src/test/royale/tests/AsyncTests.as
@@ -73,6 +73,7 @@ package tests
                                
Assert.assertStrictlyEquals(listener.failureCount, 0);
                                
Assert.assertStrictlyEquals(listener.ignoreCount, 0);
                                Assert.assertNull(listener.active);
+                               Assert.assertTrue(listener.result.successful);
                        }, 400);
                }
 
@@ -90,6 +91,7 @@ package tests
                        Assert.assertStrictlyEquals(listener.failureCount, 1);
                        Assert.assertStrictlyEquals(listener.ignoreCount, 0);
                        Assert.assertNull(listener.active);
+                       Assert.assertFalse(listener.result.successful);
                }
 
                [Test(async)]
@@ -108,6 +110,87 @@ package tests
                                
Assert.assertStrictlyEquals(listener.failureCount, 1);
                                
Assert.assertStrictlyEquals(listener.ignoreCount, 0);
                                Assert.assertNull(listener.active);
+                               Assert.assertFalse(listener.result.successful);
+                       }, 400);
+               }
+
+               [Test(async)]
+               public function 
testAsyncTestFinishesWithFailOnEventAndNoDispatch():void
+               {
+                       var notifier:RunNotifier = new RunNotifier();
+                       var listener:AsyncListener = new AsyncListener();
+                       notifier.addListener(listener);
+                       _runner = new 
MetadataRunner(AsyncFailOnEventWithoutDispatchFixture);
+                       _runner.run(notifier);
+                       Async.delayCall(this, function():void
+                       {
+                               Assert.assertTrue(listener.runStarted);
+                               Assert.assertTrue(listener.runFinished);
+                               
Assert.assertStrictlyEquals(listener.finishedCount, 1);
+                               
Assert.assertStrictlyEquals(listener.failureCount, 0);
+                               
Assert.assertStrictlyEquals(listener.ignoreCount, 0);
+                               Assert.assertNull(listener.active);
+                               Assert.assertTrue(listener.result.successful);
+                       }, 400);
+               }
+
+               [Test(async)]
+               public function 
testAsyncTestFailsWithFailOnEventAndDispatch():void
+               {
+                       var notifier:RunNotifier = new RunNotifier();
+                       var listener:AsyncListener = new AsyncListener();
+                       notifier.addListener(listener);
+                       _runner = new 
MetadataRunner(AsyncFailOnEventWithDispatchFixture);
+                       _runner.run(notifier);
+                       Async.delayCall(this, function():void
+                       {
+                               Assert.assertTrue(listener.runStarted);
+                               Assert.assertTrue(listener.runFinished);
+                               
Assert.assertStrictlyEquals(listener.finishedCount, 1);
+                               
Assert.assertStrictlyEquals(listener.failureCount, 1);
+                               
Assert.assertStrictlyEquals(listener.ignoreCount, 0);
+                               Assert.assertNull(listener.active);
+                               Assert.assertFalse(listener.result.successful);
+                       }, 400);
+               }
+
+               [Test(async)]
+               public function 
testAsyncTestFinishesWithRequireEventAndDispatch():void
+               {
+                       var notifier:RunNotifier = new RunNotifier();
+                       var listener:AsyncListener = new AsyncListener();
+                       notifier.addListener(listener);
+                       _runner = new 
MetadataRunner(AsyncRequireEventWithDispatchFixture);
+                       _runner.run(notifier);
+                       Async.delayCall(this, function():void
+                       {
+                               Assert.assertTrue(listener.runStarted);
+                               Assert.assertTrue(listener.runFinished);
+                               
Assert.assertStrictlyEquals(listener.finishedCount, 1);
+                               
Assert.assertStrictlyEquals(listener.failureCount, 0);
+                               
Assert.assertStrictlyEquals(listener.ignoreCount, 0);
+                               Assert.assertNull(listener.active);
+                               Assert.assertTrue(listener.result.successful);
+                       }, 400);
+               }
+
+               [Test(async)]
+               public function 
testAsyncTestFailsWithRequireEventAndNoDispatch():void
+               {
+                       var notifier:RunNotifier = new RunNotifier();
+                       var listener:AsyncListener = new AsyncListener();
+                       notifier.addListener(listener);
+                       _runner = new 
MetadataRunner(AsyncRequireEventWithoutDispatchFixture);
+                       _runner.run(notifier);
+                       Async.delayCall(this, function():void
+                       {
+                               Assert.assertTrue(listener.runStarted);
+                               Assert.assertTrue(listener.runFinished);
+                               
Assert.assertStrictlyEquals(listener.finishedCount, 1);
+                               
Assert.assertStrictlyEquals(listener.failureCount, 1);
+                               
Assert.assertStrictlyEquals(listener.ignoreCount, 0);
+                               Assert.assertNull(listener.active);
+                               Assert.assertFalse(listener.result.successful);
                        }, 400);
                }
        }
@@ -118,6 +201,8 @@ import org.apache.royale.test.runners.notification.Failure;
 import org.apache.royale.test.runners.notification.Result;
 import org.apache.royale.test.Assert;
 import org.apache.royale.test.async.Async;
+import org.apache.royale.events.EventDispatcher;
+import org.apache.royale.events.Event;
 
 class AsyncFixture
 {
@@ -153,6 +238,55 @@ class AsyncFailWithDelayCallFixture
        }
 }
 
+class AsyncFailOnEventWithoutDispatchFixture
+{
+       [Test(async,timeout="200")]
+       public function test1():void
+       {
+               var dispatcher:EventDispatcher = new EventDispatcher();
+               Async.failOnEvent(this, dispatcher, Event.CHANGE, 100);
+       }
+}
+
+class AsyncFailOnEventWithDispatchFixture
+{
+       [Test(async,timeout="300")]
+       public function test1():void
+       {
+               var dispatcher:EventDispatcher = new EventDispatcher();
+               Async.failOnEvent(this, dispatcher, Event.CHANGE, 200);
+               Async.delayCall(this, function():void
+               {
+                       dispatcher.dispatchEvent(new Event(Event.CHANGE));
+                       Assert.fail();
+               }, 100);
+       }
+}
+
+class AsyncRequireEventWithDispatchFixture
+{
+       [Test(async,timeout="300")]
+       public function test1():void
+       {
+               var dispatcher:EventDispatcher = new EventDispatcher();
+               Async.requireEvent(this, dispatcher, Event.CHANGE, 200);
+               Async.delayCall(this, function():void
+               {
+                       dispatcher.dispatchEvent(new Event(Event.CHANGE));
+               }, 100);
+       }
+}
+
+class AsyncRequireEventWithoutDispatchFixture
+{
+       [Test(async,timeout="200")]
+       public function test1():void
+       {
+               var dispatcher:EventDispatcher = new EventDispatcher();
+               Async.requireEvent(this, dispatcher, Event.CHANGE, 100);
+       }
+}
+
 class AsyncListener implements IRunListener
 {
        public var runStarted:Boolean = false;
@@ -161,6 +295,7 @@ class AsyncListener implements IRunListener
        public var finishedCount:int = 0;
        public var failureCount:int = 0;
        public var ignoreCount:int = 0;
+       public var result:Result = null;
 
        public function testStarted(description:String):void
        {
@@ -190,6 +325,7 @@ class AsyncListener implements IRunListener
 
        public function testRunFinished(result:Result):void
        {
+               this.result = result;
                runFinished = true;
        }
 }
\ No newline at end of file

Reply via email to