comparison bindings/as3/ext/asunit/textui/TestRunner.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.textui {
2 import asunit.framework.Test;
3 import asunit.framework.TestResult;
4
5 import flash.display.MovieClip;
6 import flash.display.StageAlign;
7 import flash.display.StageScaleMode;
8 import flash.events.Event;
9 import flash.system.fscommand;
10 import flash.utils.clearInterval;
11 import flash.utils.describeType;
12 import flash.utils.getTimer;
13 import flash.utils.setInterval;
14 import flash.utils.Timer;
15 import flash.events.TimerEvent;
16 import flash.display.DisplayObject;
17
18 /**
19 * The base class for ActionScript 3.0 test harness.
20 *
21 * The <code>TestRunner</code> should be extended by your
22 * concrete runner for your project.
23 *
24 * If you're building a Flex application, you will need to
25 * extend the <code>FlexRunner</code>
26 *
27 * Your concrete runner will usually look like the following:
28 * <listing>
29 * package {
30 * import asunit.textui.TestRunner;
31 *
32 * public class MyProjectRunner extends TestRunner {
33 *
34 * public function MyProjectRunner() {
35 * // start(clazz:Class, methodName:String, showTrace:Boolean)
36 * // NOTE: sending a particular class and method name will
37 * // execute setUp(), the method and NOT tearDown.
38 * // This allows you to get visual confirmation while developing
39 * // visual entities
40 * start(AllTests, null, TestRunner.SHOW_TRACE);
41 * }
42 * }
43 * }
44 * </listing>
45 *
46 * @includeExample TestRunnerExample.as
47 *
48 * @see asunit.textui.FlexRunner
49 * @see asunit.textui.AirRunner
50 * @see asunit.textui.XMLResultPrinter
51 **/
52 public class TestRunner extends MovieClip {
53 public static const SUCCESS_EXIT:int = 0;
54 public static const FAILURE_EXIT:int = 1;
55 public static const EXCEPTION_EXIT:int = 2;
56 public static const SHOW_TRACE:Boolean = true;
57 protected var fPrinter:ResultPrinter;
58 protected var startTime:Number;
59 protected var result:TestResult;
60
61 public function TestRunner() {
62 configureListeners();
63 }
64
65 private function configureListeners():void {
66 addEventListener(Event.ADDED_TO_STAGE, addedHandler);
67 addEventListener(Event.ADDED, addedHandler);
68 }
69
70 protected function addedHandler(event:Event):void {
71 if (!stage)
72 {
73 return;
74 }
75 if(event.target === fPrinter) {
76 stage.align = StageAlign.TOP_LEFT;
77 stage.scaleMode = StageScaleMode.NO_SCALE;
78 stage.addEventListener(Event.RESIZE, resizeHandler);
79 resizeHandler(new Event("resize"));
80 }
81 }
82
83 private function resizeHandler(event:Event):void {
84 fPrinter.width = stage.stageWidth;
85 fPrinter.height = stage.stageHeight;
86 }
87
88 /**
89 * Starts a test run based on the <code>TestCase</code> or <code>TestSuite</code> provided.
90 *
91 * If a concrete <code>TestCase</code> is provided to the <code>start</code> method,
92 * you can also provide the string name of a single test method to execute.
93 *
94 * This will run the <code>TestCase</code> <code>setUp</code> method, then
95 * the test method name that was provided, and will <em>not</em> run <code>tearDown</code>.
96 *
97 * This is a great way to build visual components in isolation and verify that they
98 * behave as expected.
99 *
100 * @example The start method can accept a concrete test case and test method name:
101 * <listing>
102 * start(MyTestCase, 'myTestMethod');
103 * </listing>
104 *
105 * @example The start method usually accepts a test suite that includes all of your
106 * test methods.
107 * <listing>
108 * start(AllTests, null, TestRunner.SHOW_TRACE);
109 * </listing>
110 *
111 * @see TestSuite
112 */
113 public function start(testCase:Class, testMethod:String = null, showTrace:Boolean = false):TestResult {
114 // fscommand("showmenu", "false");
115 try {
116 var instance:Test;
117 if(testMethod != null) {
118 instance = new testCase(testMethod);
119 }
120 else {
121 instance = new testCase();
122 }
123 return doRun(instance, showTrace);
124 }
125 catch(e:Error) {
126 throw new Error("Could not create and run test suite: " + e.getStackTrace());
127 }
128 return null;
129 }
130
131 public function doRun(test:Test, showTrace:Boolean = false):TestResult {
132
133 result = new TestResult();
134
135 if (test.getIsComplete())
136 return result;
137
138 if(fPrinter == null) {
139 setPrinter(new ResultPrinter(showTrace));
140 }
141 else {
142 fPrinter.setShowTrace(showTrace);
143 }
144 result.addListener(getPrinter());
145 startTime = getTimer();
146 test.setResult(result);
147 test.setContext(this);
148 test.addEventListener(Event.COMPLETE, testCompleteHandler);
149 test.run();
150 return result;
151 }
152
153 private function testCompleteHandler(event:Event):void {
154 var endTime:Number = getTimer();
155 var runTime:Number = endTime - startTime;
156 getPrinter().printResult(result, runTime);
157 }
158
159 public function setPrinter(printer:ResultPrinter):void {
160 if(fPrinter is DisplayObject && getChildIndex(fPrinter)) {
161 removeChild(fPrinter);
162 }
163
164 fPrinter = printer;
165 if(fPrinter is DisplayObject) {
166 addChild(fPrinter);
167 }
168 }
169
170 public function getPrinter():ResultPrinter {
171 return fPrinter;
172 }
173 }
174 }