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