comparison bindings/as3/ext/asunit/framework/AsynchronousTestCase.as @ 732:3a0b9700b3d2

* Initial AS3 commit
author mas01mj
date Tue, 14 Sep 2010 16:47:10 +0000
parents
children
comparison
equal deleted inserted replaced
731:65134dd772fc 732:3a0b9700b3d2
1 package asunit.framework {
2
3 import asunit.errors.AbstractError;
4
5 import flash.errors.IllegalOperationError;
6 import flash.events.*;
7 import flash.net.URLLoader;
8 import flash.utils.getTimer;
9
10 /**
11 * Extend this class if you have a TestCase that requires the
12 * asynchronous load of external data.
13 */
14 public class AsynchronousTestCase extends TestCase implements Test {
15
16 protected static const DEFAULT_REMOTE_TIMEOUT:int = 30000;
17 private static const INVALID_TIME:int = -1;
18
19 private var _ioErrorExpected:Boolean;
20 private var _remoteDuration:int;
21 private var _remoteStartTime:int;
22 private var _remoteTimeout:int;
23 private var _securityErrorExpected:Boolean;
24
25 public function AsynchronousTestCase(testMethod:String = null) {
26 super(testMethod);
27 _remoteStartTime = INVALID_TIME;
28
29 // set defaults for user-configurable properties:
30 _remoteTimeout = DEFAULT_REMOTE_TIMEOUT;
31 _ioErrorExpected = false;
32 _securityErrorExpected = false;
33 }
34
35 public function get remoteDuration():int {
36 return _remoteDuration;
37 }
38
39 public function remoteDurationIsValid():Boolean {
40 return _remoteDuration != INVALID_TIME;
41 }
42
43 // see testRemoteDuration() below
44 public function set remoteTimeout(ms:int):void {
45 _remoteTimeout = ms;
46 }
47
48 public function set ioErrorExpected(yn:Boolean):void {
49 _ioErrorExpected = yn;
50 }
51
52 public function set securityErrorExpected(yn:Boolean):void {
53 _securityErrorExpected = yn;
54 }
55
56 // use this method in overriding run() if you are using a URLLoader:
57 protected function configureListeners(loader:URLLoader):void {
58 loader.addEventListener(Event.COMPLETE, completeHandler);
59 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
60 loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
61 loader.addEventListener(Event.OPEN, openHandler);
62 loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
63 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
64 }
65
66 // in a subclass, you should override this method and call super.run() at the end
67 public override function run():void {
68 if ((this as Object).constructor == AsynchronousTestCase)
69 {
70 throw new AbstractError("run() method must be overridden in class derived from AsynchronousTestCase");
71 }
72
73 startRemoteDuration();
74 }
75
76 private final function startRemoteDuration():void {
77 _remoteStartTime = getTimer();
78 }
79
80 private final function setRemoteDuration():void {
81 if (_remoteStartTime == INVALID_TIME) {
82 // I guess you overrode run() in a subclass without calling super.run()
83 _remoteDuration = INVALID_TIME;
84 }
85 else {
86 _remoteDuration = getTimer() - _remoteStartTime;
87 }
88 }
89
90 protected final function completeHandler(event:Event):void {
91 setRemoteDuration();
92 setDataSource(event);
93 // call super.run() to execute test methods:
94 runTests();
95 }
96
97 // override this method to put a copy of the data into a member reference
98 protected function setDataSource(event:Event):void {
99 throw new AbstractError("setDataSource must be overridden in class derived from AsynchronousTestCase");
100 }
101
102 // this method gives derived classes access to TestCase.run()
103 protected final function runTests():void {
104 super.run();
105 }
106
107 // TODO: add support for failing status events...
108 protected function httpStatusHandler(event:HTTPStatusEvent):void {
109 // I believe this is useless except in AIR.
110 }
111
112 protected final function ioErrorHandler(event:IOErrorEvent):void {
113 result.startTest(this);
114 if (_ioErrorExpected == false)
115 {
116 // access is authorized and we didn't get in: log the error
117 result.addError(this, new IllegalOperationError(event.text));
118 }
119 setRemoteDuration();
120 testRemoteDuration();
121 dispatchEvent(new Event(Event.COMPLETE));
122 }
123
124 protected function openHandler(event:Event):void {
125 }
126
127 protected function progressHandler(event:ProgressEvent):void {
128 }
129
130 protected final function securityErrorHandler(event:SecurityErrorEvent):void {
131 result.startTest(this);
132 if (_securityErrorExpected == false)
133 {
134 // access is authorized and we didn't get in: log the error
135 result.addError(this, new IllegalOperationError(event.text));
136 }
137 setRemoteDuration();
138 testRemoteDuration();
139 dispatchEvent(new Event(Event.COMPLETE));
140 }
141
142 public function testRemoteDuration():void {
143 if (!remoteDurationIsValid())
144 {
145 return;
146 }
147 if (_remoteDuration > _remoteTimeout)
148 {
149 result.addError(this, new IllegalOperationError("remote communication took too long: " + _remoteDuration/1000 + " seconds.\n" + this.toString()));
150 }
151 }
152
153 public function testUnauthorizedAccess():void {
154 if (_securityErrorExpected || _ioErrorExpected)
155 {
156 fail("unauthorized access permitted (expected no access)\n" + this.toString());
157 }
158 }
159
160 }
161 }