OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * List of test cases. | |
7 * @type {Array.<strring>} List of function names for tests to run. | |
Dan Beam
2013/06/20 18:31:46
!Array.<string>
| |
8 */ | |
9 var testCases = []; | |
10 | |
11 /** | |
12 * Indicates if all tests have run successfully. | |
13 * @type {boolean} | |
14 */ | |
15 var cleanTestRun = true; | |
16 | |
17 /** | |
18 * Armed during setup of a test to call the matching tear down code. | |
19 * @type {?Function} | |
20 */ | |
21 var pendingTearDown = null; | |
22 | |
23 /** | |
24 * Indicates if running an asynchronous test. | |
25 * @type {boolean} | |
26 */ | |
27 var runningAsynchronousTest = false; | |
Dan Beam
2013/06/20 18:31:46
nit: move right above continueTesting() IMO
| |
28 | |
29 /** | |
6 * Tests that an observation matches the expected value. | 30 * Tests that an observation matches the expected value. |
7 * @param {Object} expected The expected value. | 31 * @param {Object} expected The expected value. |
8 * @param {Object} observed The actual value. | 32 * @param {Object} observed The actual value. |
9 * @param {string=} opt_message Optional message to include with a test | 33 * @param {string=} opt_message Optional message to include with a test |
10 * failure. | 34 * failure. |
11 */ | 35 */ |
12 function assertEquals(expected, observed, opt_message) { | 36 function assertEquals(expected, observed, opt_message) { |
13 if (observed !== expected) { | 37 if (observed !== expected) { |
14 var message = 'Assertion Failed\n Observed: ' + observed + | 38 var message = 'Assertion Failed\n Observed: ' + observed + |
15 '\n Expected: ' + expected; | 39 '\n Expected: ' + expected; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 if (observed === reference) { | 74 if (observed === reference) { |
51 var message = 'Assertion Failed\n Observed: ' + observed + | 75 var message = 'Assertion Failed\n Observed: ' + observed + |
52 '\n Reference: ' + reference; | 76 '\n Reference: ' + reference; |
53 if (opt_message) | 77 if (opt_message) |
54 message = message + '\n ' + opt_message; | 78 message = message + '\n ' + opt_message; |
55 throw new Error(message); | 79 throw new Error(message); |
56 } | 80 } |
57 } | 81 } |
58 | 82 |
59 /** | 83 /** |
60 * Verifies that a test evaluation results in an assertion failure. | 84 * Verifies that a test evaluation results in an exception. |
61 * @param {!Function} f The test function. | 85 * @param {!Function} f The test function. |
62 */ | 86 */ |
63 function assertThrows(f) { | 87 function assertThrows(f) { |
64 var triggeredError = false; | 88 var triggeredError = false; |
65 try { | 89 try { |
66 f(); | 90 f(); |
67 } catch(err) { | 91 } catch(err) { |
68 triggeredError = true; | 92 triggeredError = true; |
69 } | 93 } |
70 if (!triggeredError) | 94 if (!triggeredError) |
71 throw new Error('Assertion Failed: throw expected.'); | 95 throw new Error('Assertion Failed: throw expected.'); |
72 } | 96 } |
73 | 97 |
74 /** | 98 /** |
99 * Verifies that the contents of the expected and observed arrays match. | |
100 * @param {Array} expected The expected result. | |
101 * @param {Array} observed The actual result. | |
102 */ | |
103 function assertArrayEquals(expected, observed) { | |
104 var v1 = Array.prototype.slice.call(expected); | |
105 var v2 = Array.prototype.slice.call(observed); | |
106 var equal = v1.length == v2.length; | |
107 if (equal) { | |
108 for (var i = 0; i < v1.length; i++) { | |
109 if (v1[i] !== v2[i]) { | |
110 equal = false; | |
111 break; | |
112 } | |
113 } | |
114 } | |
115 if (!equal) { | |
116 var message = 'Assertion Failed\n Observed: ' + v1 + | |
117 '\n Expected: ' + v2; | |
118 throw new Error(message); | |
119 } | |
120 } | |
121 | |
122 /** | |
75 * Runs all functions starting with test and reports success or | 123 * Runs all functions starting with test and reports success or |
76 * failure of the test suite. | 124 * failure of the test suite. |
77 */ | 125 */ |
78 function runTests() { | 126 function runTests() { |
79 var tests = []; | |
80 | |
81 if (window.setUp) | |
82 window.setUp(); | |
83 | |
84 var success = true; | |
85 for (var name in window) { | 127 for (var name in window) { |
86 if (typeof window[name] == 'function' && /^test/.test(name)) | 128 if (typeof window[name] == 'function' && /^test/.test(name)) |
arv (Not doing code reviews)
2013/06/20 15:49:38
var isAsyncTest = window[name].length;
kevers
2013/06/20 17:34:18
Added isAyncTest in the continueTesting function b
| |
87 tests.push(name); | 129 testCases.push(name); |
88 } | 130 } |
89 if (!tests.length) { | 131 if (!testCases.length) { |
90 console.error('\nFailed to find test cases.'); | 132 console.error('\nFailed to find test cases.'); |
91 success = false; | 133 cleanTestRun = false; |
92 } | 134 } |
93 for (var i = 0; i < tests.length; i++) { | 135 continueTesting(); |
94 try { | |
95 window[tests[i]](); | |
96 } catch (err) { | |
97 console.error('Failure in test ' + tests[i] + '\n' + err); | |
98 console.log(err.stack); | |
99 success = false; | |
100 } | |
101 } | |
102 | |
103 if (window.tearDown) | |
104 window.tearDown(); | |
105 | |
106 endTests(success); | |
107 } | 136 } |
108 | 137 |
109 /** | 138 /** |
139 * Runs the next test in the queue. Reports the test results if the queue is | |
140 * empty. | |
141 */ | |
142 function continueTesting() { | |
143 runningAsynchronousTest = false; | |
Dan Beam
2013/06/20 18:31:46
nit: \n
| |
144 if (pendingTearDown) { | |
145 pendingTearDown(); | |
146 pendingTearDown = null; | |
147 } | |
Dan Beam
2013/06/20 18:31:46
nit: \n
| |
148 if(testCases.length > 0) { | |
Dan Beam
2013/06/20 18:31:46
if (
| |
149 var fn = testCases.pop(); | |
150 try { | |
151 if (window.setUp) | |
152 window.setUp(); | |
Dan Beam
2013/06/20 18:31:46
nit: \n
| |
153 pendingTearDown = window.tearDown; | |
154 window[fn](); | |
155 } catch(err) { | |
156 console.error('Failure in test ' + fn + '\n' + err); | |
157 console.log(err.stack); | |
158 cleanTestRun = false; | |
159 } | |
160 // Asynchronous tests must manually call continueTesting when complete. | |
161 if (!runningAsynchronousTest) | |
162 continueTesting(); | |
163 } else { | |
164 endTests(cleanTestRun); | |
165 } | |
Dan Beam
2013/06/20 18:31:46
nit: \n
| |
166 if (testCases.length) { | |
167 domAutomationController.setAutomationId(1); | |
168 domAutomationController.send('PENDING'); | |
169 } | |
170 } | |
171 | |
172 /** | |
110 * Signals completion of a test. | 173 * Signals completion of a test. |
111 * @param {boolean} success Indicates if the test completed successfully. | 174 * @param {boolean} success Indicates if the test completed successfully. |
112 */ | 175 */ |
113 function endTests(success) { | 176 function endTests(success) { |
114 domAutomationController.setAutomationId(1); | 177 domAutomationController.setAutomationId(1); |
115 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); | 178 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); |
116 } | 179 } |
117 | 180 |
118 window.onerror = function() { | 181 window.onerror = function() { |
119 endTests(false); | 182 endTests(false); |
120 }; | 183 }; |
OLD | NEW |