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 * Tests that an observation matches the expected value. | 6 * Tests that an observation matches the expected value. |
7 * @param {Object} expected The expected value. | 7 * @param {Object} expected The expected value. |
8 * @param {Object} observed The actual value. | 8 * @param {Object} observed The actual value. |
9 * @param {string=} opt_message Optional message to include with a test | 9 * @param {string=} opt_message Optional message to include with a test |
10 * failure. | 10 * failure. |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 112 } |
113 } else { | 113 } else { |
114 assertEquals(expected, observed, opt_message); | 114 assertEquals(expected, observed, opt_message); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 /** | 118 /** |
119 * Defines runTests. | 119 * Defines runTests. |
120 */ | 120 */ |
121 (function(exports) { | 121 (function(exports) { |
122 /** | 122 /** |
123 * List of test cases. | 123 * List of test cases. |
124 * @type {Array<string>} List of function names for tests to run. | 124 * @type {Array<string>} List of function names for tests to run. |
125 */ | 125 */ |
126 var testCases = []; | 126 var testCases = []; |
127 | 127 |
128 /** | 128 /** |
129 * Indicates if all tests have run successfully. | 129 * Indicates if all tests have run successfully. |
130 * @type {boolean} | 130 * @type {boolean} |
131 */ | 131 */ |
132 var cleanTestRun = true; | 132 var cleanTestRun = true; |
133 | 133 |
134 /** | 134 /** |
135 * Armed during setup of a test to call the matching tear down code. | 135 * Armed during setup of a test to call the matching tear down code. |
136 * @type {Function} | 136 * @type {Function} |
137 */ | 137 */ |
138 var pendingTearDown = null; | 138 var pendingTearDown = null; |
139 | 139 |
140 /** | 140 /** |
141 * Runs all functions starting with test and reports success or | 141 * Runs all functions starting with test and reports success or |
142 * failure of the test suite. | 142 * failure of the test suite. |
143 */ | 143 */ |
144 function runTests() { | 144 function runTests() { |
145 for (var name in window) { | 145 for (var name in window) { |
146 // To avoid unnecessary getting properties, test name first. | 146 // To avoid unnecessary getting properties, test name first. |
147 if (/^test/.test(name) && typeof window[name] == 'function') | 147 if (/^test/.test(name) && typeof window[name] == 'function') |
148 testCases.push(name); | 148 testCases.push(name); |
149 } | 149 } |
150 if (!testCases.length) { | 150 if (!testCases.length) { |
151 console.error('Failed to find test cases.'); | 151 console.error('Failed to find test cases.'); |
| 152 cleanTestRun = false; |
| 153 } |
| 154 try { |
| 155 if (window.setUpPage) |
| 156 window.setUpPage(); |
| 157 } catch (err) { |
| 158 cleanTestRun = false; |
| 159 } |
| 160 continueTesting(); |
| 161 } |
| 162 |
| 163 /** |
| 164 * Runs the next test in the queue. Reports the test results if the queue is |
| 165 * empty. |
| 166 * @param {boolean=} opt_asyncTestFailure Optional parameter indicated if the |
| 167 * last asynchronous test failed. |
| 168 */ |
| 169 function continueTesting(opt_asyncTestFailure) { |
| 170 if (opt_asyncTestFailure) |
| 171 cleanTestRun = false; |
| 172 var done = false; |
| 173 if (pendingTearDown) { |
| 174 pendingTearDown(); |
| 175 pendingTearDown = null; |
| 176 } |
| 177 if (testCases.length > 0) { |
| 178 var fn = testCases.pop(); |
| 179 var isAsyncTest = window[fn].length; |
| 180 try { |
| 181 if (window.setUp) |
| 182 window.setUp(); |
| 183 pendingTearDown = window.tearDown; |
| 184 window[fn](continueTesting); |
| 185 } catch (err) { |
| 186 console.error('Failure in test ' + fn + '\n' + err); |
| 187 console.log(err.stack); |
152 cleanTestRun = false; | 188 cleanTestRun = false; |
153 } | 189 } |
154 try { | 190 // Asynchronous tests must manually call continueTesting when complete. |
155 if (window.setUpPage) | 191 if (!isAsyncTest) |
156 window.setUpPage(); | 192 continueTesting(); |
157 } catch (err) { | 193 } else { |
158 cleanTestRun = false; | 194 done = true; |
159 } | 195 endTests(cleanTestRun); |
160 continueTesting(); | |
161 } | 196 } |
| 197 if (!done) { |
| 198 domAutomationController.setAutomationId(1); |
| 199 domAutomationController.send('PENDING'); |
| 200 } |
| 201 } |
162 | 202 |
163 /** | 203 exports.runTests = runTests; |
164 * Runs the next test in the queue. Reports the test results if the queue is | |
165 * empty. | |
166 * @param {boolean=} opt_asyncTestFailure Optional parameter indicated if the | |
167 * last asynchronous test failed. | |
168 */ | |
169 function continueTesting(opt_asyncTestFailure) { | |
170 if (opt_asyncTestFailure) | |
171 cleanTestRun = false; | |
172 var done = false; | |
173 if (pendingTearDown) { | |
174 pendingTearDown(); | |
175 pendingTearDown = null; | |
176 } | |
177 if (testCases.length > 0) { | |
178 var fn = testCases.pop(); | |
179 var isAsyncTest = window[fn].length; | |
180 try { | |
181 if (window.setUp) | |
182 window.setUp(); | |
183 pendingTearDown = window.tearDown; | |
184 window[fn](continueTesting); | |
185 } catch (err) { | |
186 console.error('Failure in test ' + fn + '\n' + err); | |
187 console.log(err.stack); | |
188 cleanTestRun = false; | |
189 } | |
190 // Asynchronous tests must manually call continueTesting when complete. | |
191 if (!isAsyncTest) | |
192 continueTesting(); | |
193 } else { | |
194 done = true; | |
195 endTests(cleanTestRun); | |
196 } | |
197 if (!done) { | |
198 domAutomationController.setAutomationId(1); | |
199 domAutomationController.send('PENDING'); | |
200 } | |
201 } | |
202 | |
203 exports.runTests = runTests; | |
204 })(this); | 204 })(this); |
205 | 205 |
206 /** | 206 /** |
207 * Signals completion of a test. | 207 * Signals completion of a test. |
208 * @param {boolean} success Indicates if the test completed successfully. | 208 * @param {boolean} success Indicates if the test completed successfully. |
209 */ | 209 */ |
210 function endTests(success) { | 210 function endTests(success) { |
211 domAutomationController.setAutomationId(1); | 211 domAutomationController.setAutomationId(1); |
212 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); | 212 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); |
213 } | 213 } |
214 | 214 |
215 window.onerror = function() { | 215 window.onerror = function() { |
216 endTests(false); | 216 endTests(false); |
217 }; | 217 }; |
OLD | NEW |