mas01mj@732
|
1 package asunit.framework {
|
mas01mj@732
|
2 import asunit.errors.AssertionFailedError;
|
mas01mj@732
|
3 import flash.errors.IllegalOperationError;
|
mas01mj@732
|
4 import flash.events.Event;
|
mas01mj@732
|
5 import flash.events.TimerEvent;
|
mas01mj@732
|
6 import flash.utils.Timer;
|
mas01mj@732
|
7
|
mas01mj@732
|
8 public class AsyncOperation{
|
mas01mj@732
|
9
|
mas01mj@732
|
10 private var timeout:Timer;
|
mas01mj@732
|
11 private var testCase:TestCase;
|
mas01mj@732
|
12 private var callback:Function;
|
mas01mj@732
|
13 private var duration:Number;
|
mas01mj@732
|
14 private var failureHandler:Function;
|
mas01mj@732
|
15
|
mas01mj@732
|
16 public function AsyncOperation(testCase:TestCase, handler:Function, duration:Number, failureHandler:Function=null){
|
mas01mj@732
|
17 this.testCase = testCase;
|
mas01mj@732
|
18 this.duration = duration;
|
mas01mj@732
|
19 timeout = new Timer(duration, 1);
|
mas01mj@732
|
20 timeout.addEventListener(TimerEvent.TIMER_COMPLETE, onTimeoutComplete);
|
mas01mj@732
|
21 timeout.start();
|
mas01mj@732
|
22 if(handler == null) {
|
mas01mj@732
|
23 handler = function(args:*):* {return;};
|
mas01mj@732
|
24 }
|
mas01mj@732
|
25 this.failureHandler = failureHandler;
|
mas01mj@732
|
26 var context:AsyncOperation = this;
|
mas01mj@732
|
27 callback = function(args:*):* {
|
mas01mj@732
|
28 timeout.stop();
|
mas01mj@732
|
29 try {
|
mas01mj@732
|
30 handler.apply(testCase, arguments);
|
mas01mj@732
|
31 }
|
mas01mj@732
|
32 catch(e:AssertionFailedError) {
|
mas01mj@732
|
33 testCase.getResult().addFailure(testCase, e);
|
mas01mj@732
|
34 }
|
mas01mj@732
|
35 catch(ioe:IllegalOperationError) {
|
mas01mj@732
|
36 testCase.getResult().addError(testCase, ioe);
|
mas01mj@732
|
37 }
|
mas01mj@732
|
38 catch(unknownError:Error) {
|
mas01mj@732
|
39 testCase.getResult().addError(testCase, unknownError);
|
mas01mj@732
|
40 }
|
mas01mj@732
|
41 finally {
|
mas01mj@732
|
42 testCase.asyncOperationComplete(context);
|
mas01mj@732
|
43 }
|
mas01mj@732
|
44 return;
|
mas01mj@732
|
45 };
|
mas01mj@732
|
46 }
|
mas01mj@732
|
47
|
mas01mj@732
|
48 public function getCallback():Function{
|
mas01mj@732
|
49 return callback;
|
mas01mj@732
|
50 }
|
mas01mj@732
|
51
|
mas01mj@732
|
52 private function onTimeoutComplete(event:TimerEvent):void {
|
mas01mj@732
|
53 if(null != failureHandler) {
|
mas01mj@732
|
54 failureHandler(new Event('async timeout'));
|
mas01mj@732
|
55 }
|
mas01mj@732
|
56 testCase.asyncOperationTimeout(this, duration, null==failureHandler);
|
mas01mj@732
|
57 }
|
mas01mj@732
|
58 }
|
mas01mj@732
|
59
|
mas01mj@732
|
60 }
|