/* * Copyright(c) 2006 the Spark project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.libspark.as3unit.tests { import flash.events.Event; import flash.events.EventDispatcher; import org.libspark.as3unit.inter.runners.AsyncError; import org.libspark.as3unit.inter.runners.TimeoutError; import org.libspark.as3unit.test; import org.libspark.as3unit.test_expected; import org.libspark.as3unit.assert.*; import org.libspark.as3unit.runner.AS3UnitCore; import org.libspark.as3unit.runner.Result; import org.libspark.as3unit.runner.notification.Failure; import org.libspark.as3unit.runner.Description; use namespace test; use namespace test_expected; public class AsyncTest { test function passAsync():void { Delay.call(async(function():void { }, 1000), 10); } test_expected static const failAsync:Class = AssertionFailedError; test function failAsync():void { Delay.call(async(function():void { fail(); }, 1000), 10); } test_expected static const failIfTimeout:Class = TimeoutError; test function failIfTimeout():void { async(function():void { }, 10); } test function passIfCallAsyncImmedialtely():void { var a:Function = async(function():void { }, 10); a(); } test_expected static const failIfCallAsyncImmediately:Class = AssertionFailedError; test function failIfCallAsyncImmediately():void { var a:Function = async(function():void { fail(); }, 10); a(); } test_expected static const workingWithEventDispatcher:Class = AssertionFailedError; test function workingWithEventDispatcher():void { var d:EventDispatcher = new EventDispatcher(); d.addEventListener('event', async(function(e:Event):void { fail(); }, 1000)); d.dispatchEvent(new Event('event')); } test function disposeAsync():void { DisposeAsyncTest.count = 0; var runner:AS3UnitCore = new AS3UnitCore(); runner.addListener(new FinishedListener(async(function():void { assertEquals(1, result.runCount); assertEquals(0, result.failureCount); assertEquals(1, DisposeAsyncTest.count); }, 1000))); var result:Result = runner.runClasses(DisposeAsyncTest); } test function disposeAsyncWhenTimeout():void { DisposeAsyncWhenTimeoutTest.count = 0; DisposeAsyncWhenTimeoutTest.f = null; var runner:AS3UnitCore = new AS3UnitCore(); runner.addListener(new FinishedListener(async(function():void { assertEquals(1, result.runCount); assertEquals(1, result.failureCount); assertEquals(0, DisposeAsyncWhenTimeoutTest.count); assertNotNull(DisposeAsyncWhenTimeoutTest.f); DisposeAsyncWhenTimeoutTest.f(); assertEquals(0, DisposeAsyncWhenTimeoutTest.count); }, 1000))); var result:Result = runner.runClasses(DisposeAsyncWhenTimeoutTest); } test function failIfNoExceptionThrown():void { var runner:AS3UnitCore = new AS3UnitCore(); runner.addListener(new FinishedListener(async(function():void { assertEquals(1, result.runCount); assertEquals(1, result.failureCount); assertTrue(Failure(result.failures[0]).message.indexOf('Expected exception:') >= 0); }, 1000))); var result:Result = runner.runClasses(NoExceptionThrownTest); } test_expected static const failIfCallAsyncOverTwoTimes:Class = AsyncError; test function failIfCallAsyncOverTwoTimes():void { async(function():void { }, 10); async(function():void { }, 10); } test function nestedAsync():void { NestedAsyncTest.run = false; var runner:AS3UnitCore = new AS3UnitCore(); runner.addListener(new FinishedListener(async(function():void { assertEquals(1, result.runCount); assertEquals(0, result.failureCount); assertTrue(NestedAsyncTest.run); }, 1000))); var result:Result = runner.runClasses(NestedAsyncTest); } test function sequence():void { SeqTest.f = null; var runner:AS3UnitCore = new AS3UnitCore(); var result:Result = runner.runClasses(SeqTest); assertEquals(1, result.runCount); assertEquals(0, result.failureCount); assertNotNull(SeqTest.f); SeqTest.f(); assertEquals(2, result.runCount); assertEquals(1, result.failureCount); assertNotNull(SeqTest.f); SeqTest.f(); assertEquals(3, result.runCount); assertEquals(2, result.failureCount); assertNotNull(SeqTest.f); SeqTest.f(); assertEquals(3, result.runCount); assertEquals(3, result.failureCount); } test function passArguments():void { var obj1:Object = new Object(); var obj2:Object = new Object(); var a:Function = async(function(o1:Object, o2:Object):void { assertSame(obj1, o1); assertSame(obj2, o2); }, 1000); a(obj1, obj2); } } } import flash.events.TimerEvent; import flash.utils.Timer; import org.libspark.as3unit.runner.notification.RunListener; import org.libspark.as3unit.runner.Result; import org.libspark.as3unit.test; import org.libspark.as3unit.test_expected; import org.libspark.as3unit.assert.fail; import org.libspark.as3unit.assert.async; use namespace test; use namespace test_expected; class Delay { public static function call(f:Function, delay:uint = 500):void { var t:Timer = new Timer(delay, 1); var listener:Function = function(e:TimerEvent):void { t.removeEventListener(TimerEvent.TIMER, listener); f(); }; t.addEventListener(TimerEvent.TIMER, listener); t.start(); } } class FinishedListener extends RunListener { public function FinishedListener(f:Function) { _f = f; } private var _f:Function; public override function testRunFinished(result:Result):void { super.testRunFinished(result); _f(); } } class DisposeAsyncTest { public static var count:uint; test function test():void { var a:Function = async(function():void { count++; a(); }, 1000); Delay.call(a, 10); } } class DisposeAsyncWhenTimeoutTest { public static var count:uint; public static var f:Function; test function timeout():void { f = async(function():void { count++; }, 10); } } class NoExceptionThrownTest { test_expected static const test:Class = Error; test function test():void { Delay.call(async(function():void { }, 1000), 10); } } class NestedAsyncTest { public static var run:Boolean; test function nest():void { Delay.call(async(function():void { Delay.call(async(function():void { run = true; }, 1000), 10); }, 1000), 10); } } class SeqTest { public static var f:Function; test function a():void { f = async(function():void { fail(); }, 1000); } test function b():void { f = async(function():void { fail(); }, 1000); } test function c():void { f = async(function():void { fail(); }, 1000); } }