|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview The way these tests work is as follows: | |
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 | |
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 | |
11 * also use the assert/expect functions. | |
12 * | |
13 * A test ends when an assert/expect test fails, an exception is thrown, or | |
14 * |netInternalsTest.testDone| is called. At that point, or soon afterwards, | |
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 */ | |
20 | |
21 // Start of namespace. | |
22 var netInternalsTest = (function() { | |
23 /** | |
24 * Use a shorter poll interval for tests, since a few tests wait for polled | |
25 * values to change. | |
26 * @type {number} | |
27 * @const | |
28 */ | |
29 var TESTING_POLL_INTERVAL_MS = 50; | |
30 | |
31 // Indicates the test is complete. | |
Sheridan Rawlins
2011/08/03 22:21:16
@type {boolean}
mmenke
2011/08/04 00:21:15
Done.
| |
32 var done = false; | |
33 | |
34 /** | |
35 * Updates the title of the page to report success or failure. Must be | |
36 * called at most once for each test. | |
37 * @param {boolean} success Description of success param. | |
38 */ | |
39 function updateTitle(success) { | |
40 if (success) { | |
41 document.title = 'Test Passed'; | |
42 } else { | |
43 document.title = 'Test Failed'; | |
44 } | |
45 done = true; | |
46 } | |
47 | |
48 /** | |
49 * Called to indicate a test is complete. | |
50 */ | |
51 function testDone() { | |
52 done = true; | |
53 } | |
54 | |
55 /** | |
56 * Creates a test function that can use the expect and assert functions | |
57 * in test_api.js. On failure, will set title to 'Test Failed', and when | |
58 * a test is done and there was no failure, will set title to 'Test Passed'. | |
59 * Calling expect/assert functions after done has been called has undefined | |
60 * behavior. Returned test functions can safely call each other directly. | |
61 * | |
62 * The resulting function has no return value. | |
63 * @param {string} testName The name of the function, reported on error. | |
64 * @param {Function} testFunction The function to run. | |
65 */ | |
66 function createTestFunction(testName, testFunction) { | |
67 return function() { | |
68 // Convert arguments to an array, as their map method may be called on | |
69 // failure by runTestFunction. | |
70 var testArguments = Array.prototype.slice.call(arguments, 0); | |
71 | |
72 // If the test is already complete, do nothing. | |
73 if (done) | |
74 return; | |
75 | |
76 var result = runTestFunction(testName, testFunction, testArguments); | |
77 | |
78 // If the first value is false, the test failed. | |
79 if (!result[0]) { | |
80 // Print any error messages. | |
81 console.log(result[1]); | |
82 // Update title to indicate failure. | |
83 updateTitle(false); | |
84 } else if (done) { | |
85 // If the first result is true, and |done| is also true, the test | |
86 // passed. Update title to indicate success. | |
87 updateTitle(true); | |
88 } | |
89 }; | |
90 } | |
91 | |
92 /** | |
93 * Dictionary of tests. | |
94 * @type {Object.<string, Function>} | |
95 */ | |
96 var tests = {}; | |
97 | |
98 /** | |
99 * Used to declare a test function called by the NetInternals browser test. | |
100 * Takes in a name and a function, and adds it to the list of tests. | |
101 * @param {string} testName The of the test. | |
102 * @param {Function} testFunction The test function. | |
103 */ | |
104 function test(testName, testFunction) { | |
105 tests[testName] = testFunction; | |
106 } | |
107 | |
108 /** | |
109 * Called by the browser to start a test. If constants haven't been | |
110 * received from the browser yet, waits until they have been. | |
111 * Experimentally, this never seems to happen, but may theoretically be | |
112 * possible. | |
113 * @param {string} testName The of the test to run. | |
114 * @param {Function} testArguments The test arguments. | |
115 */ | |
116 function runTest(testName, testArguments) { | |
117 // If we've already received the constants, start the tests. | |
118 if (typeof(LogEventType) != 'undefined') { | |
119 startNetInternalsTest(testName, testArguments); | |
120 return; | |
121 } | |
122 | |
123 // Otherwise, wait until we do. | |
124 console.log('Received constants late.'); | |
125 | |
126 /** | |
127 * Observer that starts the tests once we've received the constants. | |
128 */ | |
129 function ConstantsObserver() { | |
130 this.testStarted_ = false; | |
131 } | |
132 | |
133 ConstantsObserver.prototype.onConstantsReceived = function() { | |
134 if (!this.testStarted_) { | |
135 this.testStarted_ = true; | |
136 startNetInternalsTest(testFunction, testArguments); | |
137 } | |
138 }; | |
139 | |
140 g_browser.addConstantsObserver(new ConstantsObserver()); | |
141 } | |
142 | |
143 /** | |
144 * Starts running the test. A test is run until an assert/expect statement | |
145 * fails or testDone is called. Those functions can only be called in the | |
146 * test function body, or in response to a message dispatched by | |
147 * |g_browser.receive|. | |
148 * @param {string} testName The of the test to run. | |
149 * @param {Function} testArguments The test arguments. | |
150 */ | |
151 function startNetInternalsTest(testName, testArguments) { | |
152 // Wrap g_browser.receive around a test function so that assert and expect | |
153 // functions can be called from observers. | |
154 g_browser.receive = createTestFunction('g_browser.receive', function() { | |
155 BrowserBridge.prototype.receive.apply(g_browser, arguments); | |
156 }); | |
157 | |
158 createTestFunction(testName, tests[testName]).apply(null, testArguments); | |
159 } | |
160 | |
161 /** | |
162 * Finds the first styled table that's a child of |parentId|, and returns the | |
163 * number of rows it has. Returns -1 if there's no such table. | |
164 * @param {string} parentId HTML element id containing a styled table. | |
165 */ | |
166 function getStyledTableNumRows(parentId) { | |
167 // The tbody element of the first styled table in |parentId|. | |
168 var tbody = document.querySelector('#' + parentId + ' .styledTable tbody'); | |
169 if (!tbody) | |
170 return -1; | |
171 return tbody.children.length; | |
172 } | |
173 | |
174 /** | |
175 * Finds the first styled table that's a child of the element with the given | |
176 * id, and checks if it has exactly |expectedRows| rows, not including the | |
177 * header row. | |
178 * @param {string} parentId HTML element id containing a styled table. | |
179 * @param {expectedRows} expectedRows Expected number of rows in the table. | |
Sheridan Rawlins
2011/08/03 22:21:16
s/{expectedRows}/{number}/
mmenke
2011/08/04 00:21:15
Done.
| |
180 */ | |
181 function checkStyledTableRows(parentId, expectedRows) { | |
182 expectEquals(expectedRows, getStyledTableNumRows(parentId), | |
183 'Incorrect number of rows in ' + parentId); | |
184 } | |
185 | |
186 /** | |
187 * Switches to the specified tab. | |
188 * TODO(mmenke): check that the tab visibility changes as expected. | |
Sheridan Rawlins
2011/08/03 22:21:16
@param
mmenke
2011/08/04 00:21:15
Done.
| |
189 */ | |
190 function switchToView(tabId) { | |
191 document.location.hash = '#' + tabId; | |
192 } | |
193 | |
194 // Exported functions. | |
195 return { | |
196 test: test, | |
197 runTest: runTest, | |
198 testDone:testDone, | |
199 checkStyledTableRows: checkStyledTableRows, | |
200 switchToView: switchToView | |
201 }; | |
202 })(); | |
203 | |
204 netInternalsTest.test('NetInternalsDone', function() { | |
205 netInternalsTest.testDone(); | |
206 }); | |
207 | |
208 netInternalsTest.test('NetInternalsExpectFail', function() { | |
209 expectNotReached(); | |
210 }); | |
211 | |
212 netInternalsTest.test('NetInternalsAssertFail', function() { | |
213 assertNotReached(); | |
214 }); | |
215 | |
216 netInternalsTest.test('NetInternalsObserverDone', function() { | |
217 /** | |
218 * A HostResolverInfo observer that calls testDone() in response to the | |
219 * first seen event. | |
220 */ | |
221 function HostResolverInfoObserver() { | |
222 } | |
223 | |
224 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { | |
225 netInternalsTest.testDone(); | |
226 }; | |
227 | |
228 // Create the observer and add it to |g_browser|. | |
229 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); | |
230 | |
231 // Needed to trigger an update. | |
232 netInternalsTest.switchToView('dns'); | |
233 }); | |
234 | |
235 netInternalsTest.test('NetInternalsObserverExpectFail', function() { | |
236 /** | |
237 * A HostResolverInfo observer that triggers an exception in response to the | |
238 * first seen event. | |
239 */ | |
240 function HostResolverInfoObserver() { | |
241 } | |
242 | |
243 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { | |
244 expectNotReached(); | |
245 netInternalsTest.testDone(); | |
246 }; | |
247 | |
248 // Create the observer and add it to |g_browser|. | |
249 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); | |
250 | |
251 // Needed to trigger an update. | |
252 netInternalsTest.switchToView('dns'); | |
253 }); | |
254 | |
255 netInternalsTest.test('NetInternalsObserverAssertFail', function() { | |
256 /** | |
257 * A HostResolverInfo observer that triggers an assertion in response to the | |
258 * first seen event. | |
259 */ | |
260 function HostResolverInfoObserver() { | |
261 } | |
262 | |
263 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { | |
264 assertNotReached(); | |
265 }; | |
266 | |
267 // Create the observer and add it to |g_browser|. | |
268 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); | |
269 | |
270 // Needed to trigger an update. | |
271 netInternalsTest.switchToView('dns'); | |
272 }); | |
OLD | NEW |