mas01mj@732: package asunit.framework { mas01mj@732: import asunit.framework.TestCase; mas01mj@732: import flash.display.Sprite; mas01mj@732: import flash.events.Event; mas01mj@732: import flash.events.IEventDispatcher; mas01mj@732: import flash.events.EventDispatcher; mas01mj@732: import flash.utils.setTimeout; mas01mj@732: mas01mj@732: // TestCase subclasses should always end with 'Test', the example mas01mj@732: // doesn't because we don't want TestSuites in this directory. mas01mj@732: public class TestCaseExample extends TestCase { mas01mj@732: private var date:Date; mas01mj@732: private var sprite:Sprite; mas01mj@732: mas01mj@732: // TestCase constructors must be implemented as follows mas01mj@732: // so that we can execute a single method on them from mas01mj@732: // the TestRunner mas01mj@732: public function TestCaseExample(testMethod:String = null) { mas01mj@732: super(testMethod); mas01mj@732: } mas01mj@732: mas01mj@732: // This method will be called before every test method mas01mj@732: override protected function setUp():void { mas01mj@732: date = new Date(); mas01mj@732: // sprite = new Sprite(); mas01mj@732: // addChild(sprite); mas01mj@732: } mas01mj@732: mas01mj@732: // This method will be called after every test method mas01mj@732: // but only if we're executing the entire TestCase, mas01mj@732: // the tearDown method won't be called if we're mas01mj@732: // calling start(MyTestCase, "someMethod"); mas01mj@732: override protected function tearDown():void { mas01mj@732: // removeChild(sprite); mas01mj@732: // sprite = null; mas01mj@732: date = null; mas01mj@732: } mas01mj@732: mas01mj@732: // This is auto-created by the XULUI and ensures that mas01mj@732: // our objects are actually created as we expect. mas01mj@732: public function testInstantiated():void { mas01mj@732: assertTrue("Date instantiated", date is Date); mas01mj@732: // assertTrue("Sprite instantiated", sprite is Sprite); mas01mj@732: } mas01mj@732: mas01mj@732: // This is an example of a typical test method mas01mj@732: public function testMonthGetterSetter():void { mas01mj@732: date.month = 1; mas01mj@732: assertEquals(1, date.month); mas01mj@732: } mas01mj@732: mas01mj@732: // This is an asynchronous test method mas01mj@732: public function testAsyncFeature():void { mas01mj@732: // create a new object that dispatches events... mas01mj@732: var dispatcher:IEventDispatcher = new EventDispatcher(); mas01mj@732: // get a TestCase async event handler reference mas01mj@732: // the 2nd arg is an optional timeout in ms. (default=1000ms ) mas01mj@732: var handler:Function = addAsync(changeHandler, 2000); mas01mj@732: // subscribe to your event dispatcher using the returned handler mas01mj@732: dispatcher.addEventListener(Event.CHANGE, handler); mas01mj@732: // cause the event to be dispatched. mas01mj@732: // either immediately: mas01mj@732: //dispatcher.dispatchEvent(new Event(Event.CHANGE)); mas01mj@732: // or in the future < your assigned timeout mas01mj@732: setTimeout( dispatcher.dispatchEvent, 200, new Event(Event.CHANGE)); mas01mj@732: } mas01mj@732: mas01mj@732: protected function changeHandler(event:Event):void { mas01mj@732: // perform assertions in your handler mas01mj@732: assertEquals(Event.CHANGE, event.type); mas01mj@732: } mas01mj@732: } mas01mj@732: }