mas01mj@732
|
1 package asunit.framework {
|
mas01mj@732
|
2 import asunit.framework.TestCase;
|
mas01mj@732
|
3 import flash.display.Sprite;
|
mas01mj@732
|
4 import flash.events.Event;
|
mas01mj@732
|
5 import flash.events.IEventDispatcher;
|
mas01mj@732
|
6 import flash.events.EventDispatcher;
|
mas01mj@732
|
7 import flash.utils.setTimeout;
|
mas01mj@732
|
8
|
mas01mj@732
|
9 // TestCase subclasses should always end with 'Test', the example
|
mas01mj@732
|
10 // doesn't because we don't want TestSuites in this directory.
|
mas01mj@732
|
11 public class TestCaseExample extends TestCase {
|
mas01mj@732
|
12 private var date:Date;
|
mas01mj@732
|
13 private var sprite:Sprite;
|
mas01mj@732
|
14
|
mas01mj@732
|
15 // TestCase constructors must be implemented as follows
|
mas01mj@732
|
16 // so that we can execute a single method on them from
|
mas01mj@732
|
17 // the TestRunner
|
mas01mj@732
|
18 public function TestCaseExample(testMethod:String = null) {
|
mas01mj@732
|
19 super(testMethod);
|
mas01mj@732
|
20 }
|
mas01mj@732
|
21
|
mas01mj@732
|
22 // This method will be called before every test method
|
mas01mj@732
|
23 override protected function setUp():void {
|
mas01mj@732
|
24 date = new Date();
|
mas01mj@732
|
25 // sprite = new Sprite();
|
mas01mj@732
|
26 // addChild(sprite);
|
mas01mj@732
|
27 }
|
mas01mj@732
|
28
|
mas01mj@732
|
29 // This method will be called after every test method
|
mas01mj@732
|
30 // but only if we're executing the entire TestCase,
|
mas01mj@732
|
31 // the tearDown method won't be called if we're
|
mas01mj@732
|
32 // calling start(MyTestCase, "someMethod");
|
mas01mj@732
|
33 override protected function tearDown():void {
|
mas01mj@732
|
34 // removeChild(sprite);
|
mas01mj@732
|
35 // sprite = null;
|
mas01mj@732
|
36 date = null;
|
mas01mj@732
|
37 }
|
mas01mj@732
|
38
|
mas01mj@732
|
39 // This is auto-created by the XULUI and ensures that
|
mas01mj@732
|
40 // our objects are actually created as we expect.
|
mas01mj@732
|
41 public function testInstantiated():void {
|
mas01mj@732
|
42 assertTrue("Date instantiated", date is Date);
|
mas01mj@732
|
43 // assertTrue("Sprite instantiated", sprite is Sprite);
|
mas01mj@732
|
44 }
|
mas01mj@732
|
45
|
mas01mj@732
|
46 // This is an example of a typical test method
|
mas01mj@732
|
47 public function testMonthGetterSetter():void {
|
mas01mj@732
|
48 date.month = 1;
|
mas01mj@732
|
49 assertEquals(1, date.month);
|
mas01mj@732
|
50 }
|
mas01mj@732
|
51
|
mas01mj@732
|
52 // This is an asynchronous test method
|
mas01mj@732
|
53 public function testAsyncFeature():void {
|
mas01mj@732
|
54 // create a new object that dispatches events...
|
mas01mj@732
|
55 var dispatcher:IEventDispatcher = new EventDispatcher();
|
mas01mj@732
|
56 // get a TestCase async event handler reference
|
mas01mj@732
|
57 // the 2nd arg is an optional timeout in ms. (default=1000ms )
|
mas01mj@732
|
58 var handler:Function = addAsync(changeHandler, 2000);
|
mas01mj@732
|
59 // subscribe to your event dispatcher using the returned handler
|
mas01mj@732
|
60 dispatcher.addEventListener(Event.CHANGE, handler);
|
mas01mj@732
|
61 // cause the event to be dispatched.
|
mas01mj@732
|
62 // either immediately:
|
mas01mj@732
|
63 //dispatcher.dispatchEvent(new Event(Event.CHANGE));
|
mas01mj@732
|
64 // or in the future < your assigned timeout
|
mas01mj@732
|
65 setTimeout( dispatcher.dispatchEvent, 200, new Event(Event.CHANGE));
|
mas01mj@732
|
66 }
|
mas01mj@732
|
67
|
mas01mj@732
|
68 protected function changeHandler(event:Event):void {
|
mas01mj@732
|
69 // perform assertions in your handler
|
mas01mj@732
|
70 assertEquals(Event.CHANGE, event.type);
|
mas01mj@732
|
71 }
|
mas01mj@732
|
72 }
|
mas01mj@732
|
73 }
|