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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 } | 89 } |
90 } | 90 } |
91 if (!equal) { | 91 if (!equal) { |
92 var message = | 92 var message = |
93 ['Assertion Failed', 'Observed: ' + v2, 'Expected: ' + v1].join('\n '); | 93 ['Assertion Failed', 'Observed: ' + v2, 'Expected: ' + v1].join('\n '); |
94 throw new Error(message); | 94 throw new Error(message); |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 /** | 98 /** |
99 * Runs all functions starting with test and reports success or | 99 * Defines runTests. |
100 * failure of the test suite. | |
101 */ | 100 */ |
102 function runTests() { | 101 (function(exports) { |
103 var tests = []; | 102 /** |
104 var success = true; | 103 * List of test cases. |
105 for (var name in window) { | 104 * @type {Array.<string>} List of function names for tests to run. |
106 if (typeof window[name] == 'function' && /^test/.test(name)) | 105 */ |
107 tests.push(name); | 106 var testCases = []; |
| 107 |
| 108 /** |
| 109 * Indicates if all tests have run successfully. |
| 110 * @type {boolean} |
| 111 */ |
| 112 var cleanTestRun = true; |
| 113 |
| 114 /** |
| 115 * Armed during setup of a test to call the matching tear down code. |
| 116 * @type {Function} |
| 117 */ |
| 118 var pendingTearDown = null; |
| 119 |
| 120 /** |
| 121 * Runs all functions starting with test and reports success or |
| 122 * failure of the test suite. |
| 123 */ |
| 124 function runTests() { |
| 125 for (var name in window) { |
| 126 if (typeof window[name] == 'function' && /^test/.test(name)) |
| 127 testCases.push(name); |
| 128 } |
| 129 if (!testCases.length) { |
| 130 console.error('Failed to find test cases.'); |
| 131 cleanTestRun = false; |
| 132 } |
| 133 continueTesting(); |
108 } | 134 } |
109 if (!tests.length) { | 135 |
110 console.error('\nFailed to find test cases.'); | 136 /** |
111 success = false; | 137 * Runs the next test in the queue. Reports the test results if the queue is |
112 } | 138 * empty. |
113 for (var i = 0; i < tests.length; i++) { | 139 */ |
114 try { | 140 function continueTesting() { |
115 if (window.setUp) | 141 if (pendingTearDown) { |
116 window.setUp(); | 142 pendingTearDown(); |
117 window[tests[i]](); | 143 pendingTearDown = null; |
118 } catch (err) { | |
119 console.error('Failure in test ' + tests[i] + '\n' + err); | |
120 console.log(err.stack); | |
121 success = false; | |
122 } | 144 } |
123 if (window.tearDown) | 145 if (testCases.length > 0) { |
124 window.tearDown(); | 146 var fn = testCases.pop(); |
125 } | 147 var isAsyncTest = window[fn].length; |
126 endTests(success); | 148 try { |
127 } | 149 if (window.setUp) |
| 150 window.setUp(); |
| 151 pendingTearDown = window.tearDown; |
| 152 window[fn](continueTesting); |
| 153 } catch(err) { |
| 154 console.error('Failure in test ' + fn + '\n' + err); |
| 155 console.log(err.stack); |
| 156 cleanTestRun = false; |
| 157 } |
| 158 // Asynchronous tests must manually call continueTesting when complete. |
| 159 if (!isAsyncTest) |
| 160 continueTesting(); |
| 161 } else { |
| 162 endTests(cleanTestRun); |
| 163 } |
| 164 if (testCases.length) { |
| 165 domAutomationController.setAutomationId(1); |
| 166 domAutomationController.send('PENDING'); |
| 167 } |
| 168 }; |
| 169 |
| 170 exports.runTests = runTests; |
| 171 })(this); |
128 | 172 |
129 /** | 173 /** |
130 * Signals completion of a test. | 174 * Signals completion of a test. |
131 * @param {boolean} success Indicates if the test completed successfully. | 175 * @param {boolean} success Indicates if the test completed successfully. |
132 */ | 176 */ |
133 function endTests(success) { | 177 function endTests(success) { |
134 domAutomationController.setAutomationId(1); | 178 domAutomationController.setAutomationId(1); |
135 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); | 179 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); |
136 } | 180 } |
137 | 181 |
138 window.onerror = function() { | 182 window.onerror = function() { |
139 endTests(false); | 183 endTests(false); |
140 }; | 184 }; |
OLD | NEW |