OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 * @fileoverview The way these tests work is as follows: | 6 * @fileoverview The way these tests work is as follows: |
7 * C++ in net_internals_ui_browsertest.cc does any necessary setup, and then | 7 * C++ in net_internals_ui_browsertest.cc does any necessary setup, and then |
8 * calls the entry point for a test with RunJavascriptTest. The called | 8 * calls the entry point for a test with RunJavascriptTest. The called |
9 * function can then use the assert/expect functions defined in test_api.js. | 9 * function can then use the assert/expect functions defined in test_api.js. |
10 * All callbacks from the browser are wrapped in such a way that they can | 10 * All callbacks from the browser are wrapped in such a way that they can |
11 * also use the assert/expect functions. | 11 * also use the assert/expect functions. |
12 * | 12 * |
13 * A test ends when an assert/expect test fails, an exception is thrown, or | 13 * A test ends when asyncTestDone, which is done when the end of a test |
14 * |netInternalsTest.testDone| is called. At that point, or soon afterwards, | 14 * is reached runTestFunction reports a failure. |
15 * the title is updated to 'Test Failed' if an assert/expect test fails, or | |
16 * there was an exception. Otherwise, it's set to 'Test Passed'. The | |
17 * behavior when an assert/expect test fails or an assertion is thrown only | |
18 * after |netInternalsTest.testDone| is called is undefined. | |
19 */ | 15 */ |
20 | 16 |
21 // Start of namespace. | 17 // Start of namespace. |
22 var netInternalsTest = (function() { | 18 var netInternalsTest = (function() { |
23 /** | 19 /** |
24 * Use a shorter poll interval for tests, since a few tests wait for polled | 20 * Use a shorter poll interval for tests, since a few tests wait for polled |
25 * values to change. | 21 * values to change. |
26 * @type {number} | 22 * @type {number} |
27 * @const | 23 * @const |
28 */ | 24 */ |
29 var TESTING_POLL_INTERVAL_MS = 50; | 25 var TESTING_POLL_INTERVAL_MS = 50; |
30 | 26 |
31 /** | 27 /** |
32 * Indicates if the test is complete. | |
33 * @type {boolean} | |
34 */ | |
35 var done = false; | |
36 | |
37 /** | |
38 * Updates the title of the page to report success or failure. Must be | |
39 * called at most once for each test. | |
40 * @param {boolean} success Description of success param. | |
41 */ | |
42 function updateTitle(success) { | |
43 if (success) { | |
44 document.title = 'Test Passed'; | |
45 } else { | |
46 document.title = 'Test Failed'; | |
47 } | |
48 done = true; | |
49 } | |
50 | |
51 /** | |
52 * Called to indicate a test is complete. | |
53 */ | |
54 function testDone() { | |
55 done = true; | |
56 } | |
57 | |
58 /** | |
59 * Creates a test function that can use the expect and assert functions | 28 * Creates a test function that can use the expect and assert functions |
60 * in test_api.js. On failure, will set title to 'Test Failed', and when | 29 * in test_api.js. Calls asyncTestDone when runTestFunction reports an |
61 * a test is done and there was no failure, will set title to 'Test Passed'. | 30 * error. Tests also call asyncTestDone when complete. |
62 * Calling expect/assert functions after done has been called has undefined | |
63 * behavior. Returned test functions can safely call each other directly. | |
64 * | 31 * |
65 * The resulting function has no return value. | 32 * The resulting function has no return value. |
66 * @param {string} testName The name of the function, reported on error. | 33 * @param {string} testName The name of the function, reported on error. |
67 * @param {Function} testFunction The function to run. | 34 * @param {Function} testFunction The function to run. |
68 * @return {function():void} Function that passes its parameters to | 35 * @return {function():void} Function that passes its parameters to |
69 * testFunction, and passes the test result, if any, to the browser | 36 * testFunction, and passes the test result, if any, to the browser |
70 * process by setting the window title. | 37 * process by setting the window title. |
71 */ | 38 */ |
72 function createTestFunction(testName, testFunction) { | 39 function createTestFunction(testName, testFunction) { |
73 return function() { | 40 return function() { |
74 // Convert arguments to an array, as their map method may be called on | 41 // Convert arguments to an array, as their map method may be called on |
75 // failure by runTestFunction. | 42 // failure by runTestFunction. |
76 var testArguments = Array.prototype.slice.call(arguments, 0); | 43 var testArguments = Array.prototype.slice.call(arguments, 0); |
77 | 44 |
78 // If the test is already complete, do nothing. | |
79 if (done) | |
80 return; | |
81 | |
82 var result = runTestFunction(testName, testFunction, testArguments); | 45 var result = runTestFunction(testName, testFunction, testArguments); |
83 | 46 |
84 // If the first value is false, the test failed. | 47 // If the first value is false, the test failed. |
85 if (!result[0]) { | 48 if (!result[0]) |
86 // Print any error messages. | 49 asyncTestDone(); |
87 console.log(result[1]); | |
88 // Update title to indicate failure. | |
89 updateTitle(false); | |
90 } else if (done) { | |
91 // If the first result is true, and |done| is also true, the test | |
92 // passed. Update title to indicate success. | |
93 updateTitle(true); | |
94 } | |
95 }; | 50 }; |
96 } | 51 } |
97 | 52 |
98 /** | 53 /** |
99 * Dictionary of tests. | 54 * Dictionary of tests. |
100 * @type {Object.<string, Function>} | 55 * @type {Object.<string, Function>} |
101 */ | 56 */ |
102 var tests = {}; | 57 var tests = {}; |
103 | 58 |
104 /** | 59 /** |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 this.testStarted_ = true; | 96 this.testStarted_ = true; |
142 startNetInternalsTest(testFunction, testArguments); | 97 startNetInternalsTest(testFunction, testArguments); |
143 } | 98 } |
144 }; | 99 }; |
145 | 100 |
146 g_browser.addConstantsObserver(new ConstantsObserver()); | 101 g_browser.addConstantsObserver(new ConstantsObserver()); |
147 } | 102 } |
148 | 103 |
149 /** | 104 /** |
150 * Starts running the test. A test is run until an assert/expect statement | 105 * Starts running the test. A test is run until an assert/expect statement |
151 * fails or testDone is called. Those functions can only be called in the | 106 * fails or testResult is called. Those functions can only be called in the |
152 * test function body, or in response to a message dispatched by | 107 * test function body, or in response to a message dispatched by |
153 * |g_browser.receive|. | 108 * |g_browser.receive|. |
154 * @param {string} testName The of the test to run. | 109 * @param {string} testName The of the test to run. |
155 * @param {Function} testArguments The test arguments. | 110 * @param {Function} testArguments The test arguments. |
156 */ | 111 */ |
157 function startNetInternalsTest(testName, testArguments) { | 112 function startNetInternalsTest(testName, testArguments) { |
158 // Wrap g_browser.receive around a test function so that assert and expect | 113 // Wrap g_browser.receive around a test function so that assert and expect |
159 // functions can be called from observers. | 114 // functions can be called from observers. |
160 g_browser.receive = createTestFunction('g_browser.receive', function() { | 115 g_browser.receive = createTestFunction('g_browser.receive', function() { |
161 BrowserBridge.prototype.receive.apply(g_browser, arguments); | 116 BrowserBridge.prototype.receive.apply(g_browser, arguments); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 * @param {string}: viewId Id of the view to switch to. | 151 * @param {string}: viewId Id of the view to switch to. |
197 */ | 152 */ |
198 function switchToView(viewId) { | 153 function switchToView(viewId) { |
199 document.location.hash = '#' + viewId; | 154 document.location.hash = '#' + viewId; |
200 } | 155 } |
201 | 156 |
202 // Exported functions. | 157 // Exported functions. |
203 return { | 158 return { |
204 test: test, | 159 test: test, |
205 runTest: runTest, | 160 runTest: runTest, |
206 testDone:testDone, | |
207 checkStyledTableRows: checkStyledTableRows, | 161 checkStyledTableRows: checkStyledTableRows, |
208 switchToView: switchToView | 162 switchToView: switchToView |
209 }; | 163 }; |
210 })(); | 164 })(); |
211 | 165 |
212 netInternalsTest.test('NetInternalsDone', function() { | 166 netInternalsTest.test('NetInternalsDone', function() { |
213 netInternalsTest.testDone(); | 167 asyncTestDone(); |
214 }); | 168 }); |
215 | 169 |
216 netInternalsTest.test('NetInternalsExpectFail', function() { | 170 netInternalsTest.test('NetInternalsExpectFail', function() { |
217 expectNotReached(); | 171 expectNotReached(); |
| 172 asyncTestDone(); |
218 }); | 173 }); |
219 | 174 |
220 netInternalsTest.test('NetInternalsAssertFail', function() { | 175 netInternalsTest.test('NetInternalsAssertFail', function() { |
221 assertNotReached(); | 176 assertNotReached(); |
| 177 asyncTestDone(); |
222 }); | 178 }); |
223 | 179 |
224 netInternalsTest.test('NetInternalsObserverDone', function() { | 180 netInternalsTest.test('NetInternalsObserverDone', function() { |
225 /** | 181 /** |
226 * A HostResolverInfo observer that calls testDone() in response to the | 182 * A HostResolverInfo observer that calls asyncTestDone() in response to the |
227 * first seen event. | 183 * first seen event. |
228 */ | 184 */ |
229 function HostResolverInfoObserver() { | 185 function HostResolverInfoObserver() { |
230 } | 186 } |
231 | 187 |
232 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { | 188 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
233 netInternalsTest.testDone(); | 189 asyncTestDone(); |
234 }; | 190 }; |
235 | 191 |
236 // Create the observer and add it to |g_browser|. | 192 // Create the observer and add it to |g_browser|. |
237 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); | 193 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
238 | 194 |
239 // Needed to trigger an update. | 195 // Needed to trigger an update. |
240 netInternalsTest.switchToView('dns'); | 196 netInternalsTest.switchToView('dns'); |
241 }); | 197 }); |
242 | 198 |
243 netInternalsTest.test('NetInternalsObserverExpectFail', function() { | 199 netInternalsTest.test('NetInternalsObserverExpectFail', function() { |
244 /** | 200 /** |
245 * A HostResolverInfo observer that triggers an exception in response to the | 201 * A HostResolverInfo observer that triggers an exception in response to the |
246 * first seen event. | 202 * first seen event. |
247 */ | 203 */ |
248 function HostResolverInfoObserver() { | 204 function HostResolverInfoObserver() { |
249 } | 205 } |
250 | 206 |
251 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { | 207 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
252 expectNotReached(); | 208 expectNotReached(); |
253 netInternalsTest.testDone(); | 209 asyncTestDone(); |
254 }; | 210 }; |
255 | 211 |
256 // Create the observer and add it to |g_browser|. | 212 // Create the observer and add it to |g_browser|. |
257 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); | 213 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
258 | 214 |
259 // Needed to trigger an update. | 215 // Needed to trigger an update. |
260 netInternalsTest.switchToView('dns'); | 216 netInternalsTest.switchToView('dns'); |
261 }); | 217 }); |
262 | 218 |
263 netInternalsTest.test('NetInternalsObserverAssertFail', function() { | 219 netInternalsTest.test('NetInternalsObserverAssertFail', function() { |
264 /** | 220 /** |
265 * A HostResolverInfo observer that triggers an assertion in response to the | 221 * A HostResolverInfo observer that triggers an assertion in response to the |
266 * first seen event. | 222 * first seen event. |
267 */ | 223 */ |
268 function HostResolverInfoObserver() { | 224 function HostResolverInfoObserver() { |
269 } | 225 } |
270 | 226 |
271 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { | 227 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
272 assertNotReached(); | 228 assertNotReached(); |
273 }; | 229 }; |
274 | 230 |
275 // Create the observer and add it to |g_browser|. | 231 // Create the observer and add it to |g_browser|. |
276 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); | 232 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
277 | 233 |
278 // Needed to trigger an update. | 234 // Needed to trigger an update. |
279 netInternalsTest.switchToView('dns'); | 235 netInternalsTest.switchToView('dns'); |
280 }); | 236 }); |
OLD | NEW |