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