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