Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: chrome/test/data/webui/net_internals/net_internals_test.js

Issue 7553009: Add some browser tests for net-internals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Response to Sheridan's comments Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 /**
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
60 * in test_api.js. On failure, will set title to 'Test Failed', and when
61 * a test is done and there was no failure, will set title to 'Test Passed'.
62 * Calling expect/assert functions after done has been called has undefined
63 * behavior. Returned test functions can safely call each other directly.
64 *
65 * The resulting function has no return value.
66 * @param {string} testName The name of the function, reported on error.
67 * @param {Function} testFunction The function to run.
Sheridan Rawlins 2011/08/04 00:25:21 @return {function()} Description.
mmenke 2011/08/04 00:54:59 Done (And there was one other, too, also done),
68 */
69 function createTestFunction(testName, testFunction) {
70 return function() {
71 // Convert arguments to an array, as their map method may be called on
72 // failure by runTestFunction.
73 var testArguments = Array.prototype.slice.call(arguments, 0);
74
75 // If the test is already complete, do nothing.
76 if (done)
77 return;
78
79 var result = runTestFunction(testName, testFunction, testArguments);
80
81 // If the first value is false, the test failed.
82 if (!result[0]) {
83 // Print any error messages.
84 console.log(result[1]);
85 // Update title to indicate failure.
86 updateTitle(false);
87 } else if (done) {
88 // If the first result is true, and |done| is also true, the test
89 // passed. Update title to indicate success.
90 updateTitle(true);
91 }
92 };
93 }
94
95 /**
96 * Dictionary of tests.
97 * @type {Object.<string, Function>}
98 */
99 var tests = {};
100
101 /**
102 * Used to declare a test function called by the NetInternals browser test.
103 * Takes in a name and a function, and adds it to the list of tests.
104 * @param {string} testName The of the test.
105 * @param {Function} testFunction The test function.
106 */
107 function test(testName, testFunction) {
108 tests[testName] = testFunction;
109 }
110
111 /**
112 * Called by the browser to start a test. If constants haven't been
113 * received from the browser yet, waits until they have been.
114 * Experimentally, this never seems to happen, but may theoretically be
115 * possible.
116 * @param {string} testName The of the test to run.
117 * @param {Function} testArguments The test arguments.
118 */
119 function runTest(testName, testArguments) {
120 // If we've already received the constants, start the tests.
121 if (typeof(LogEventType) != 'undefined') {
122 startNetInternalsTest(testName, testArguments);
123 return;
124 }
125
126 // Otherwise, wait until we do.
127 console.log('Received constants late.');
128
129 /**
130 * Observer that starts the tests once we've received the constants.
131 */
132 function ConstantsObserver() {
133 this.testStarted_ = false;
134 }
135
136 ConstantsObserver.prototype.onConstantsReceived = function() {
137 if (!this.testStarted_) {
138 this.testStarted_ = true;
139 startNetInternalsTest(testFunction, testArguments);
140 }
141 };
142
143 g_browser.addConstantsObserver(new ConstantsObserver());
144 }
145
146 /**
147 * Starts running the test. A test is run until an assert/expect statement
148 * fails or testDone is called. Those functions can only be called in the
149 * test function body, or in response to a message dispatched by
150 * |g_browser.receive|.
151 * @param {string} testName The of the test to run.
152 * @param {Function} testArguments The test arguments.
153 */
154 function startNetInternalsTest(testName, testArguments) {
155 // Wrap g_browser.receive around a test function so that assert and expect
156 // functions can be called from observers.
157 g_browser.receive = createTestFunction('g_browser.receive', function() {
158 BrowserBridge.prototype.receive.apply(g_browser, arguments);
159 });
160
161 createTestFunction(testName, tests[testName]).apply(null, testArguments);
162 }
163
164 /**
165 * Finds the first styled table that's a child of |parentId|, and returns the
166 * number of rows it has. Returns -1 if there's no such table.
167 * @param {string} parentId HTML element id containing a styled table.
168 */
169 function getStyledTableNumRows(parentId) {
170 // The tbody element of the first styled table in |parentId|.
171 var tbody = document.querySelector('#' + parentId + ' .styledTable tbody');
172 if (!tbody)
173 return -1;
174 return tbody.children.length;
175 }
176
177 /**
178 * Finds the first styled table that's a child of the element with the given
179 * id, and checks if it has exactly |expectedRows| rows, not including the
180 * header row.
181 * @param {string} parentId HTML element id containing a styled table.
182 * @param {number} expectedRows Expected number of rows in the table.
183 */
184 function checkStyledTableRows(parentId, expectedRows) {
185 expectEquals(expectedRows, getStyledTableNumRows(parentId),
186 'Incorrect number of rows in ' + parentId);
187 }
188
189 /**
190 * Switches to the specified tab.
191 * TODO(mmenke): check that the tab visibility changes as expected.
192 * @param {string}: viewId Id of the view to switch to.
193 */
194 function switchToView(viewId) {
195 document.location.hash = '#' + viewId;
196 }
197
198 // Exported functions.
199 return {
200 test: test,
201 runTest: runTest,
202 testDone:testDone,
203 checkStyledTableRows: checkStyledTableRows,
204 switchToView: switchToView
205 };
206 })();
207
208 netInternalsTest.test('NetInternalsDone', function() {
209 netInternalsTest.testDone();
210 });
211
212 netInternalsTest.test('NetInternalsExpectFail', function() {
213 expectNotReached();
214 });
215
216 netInternalsTest.test('NetInternalsAssertFail', function() {
217 assertNotReached();
218 });
219
220 netInternalsTest.test('NetInternalsObserverDone', function() {
221 /**
222 * A HostResolverInfo observer that calls testDone() in response to the
223 * first seen event.
224 */
225 function HostResolverInfoObserver() {
226 }
227
228 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() {
229 netInternalsTest.testDone();
230 };
231
232 // Create the observer and add it to |g_browser|.
233 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver());
234
235 // Needed to trigger an update.
236 netInternalsTest.switchToView('dns');
237 });
238
239 netInternalsTest.test('NetInternalsObserverExpectFail', function() {
240 /**
241 * A HostResolverInfo observer that triggers an exception in response to the
242 * first seen event.
243 */
244 function HostResolverInfoObserver() {
245 }
246
247 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() {
248 expectNotReached();
249 netInternalsTest.testDone();
250 };
251
252 // Create the observer and add it to |g_browser|.
253 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver());
254
255 // Needed to trigger an update.
256 netInternalsTest.switchToView('dns');
257 });
258
259 netInternalsTest.test('NetInternalsObserverAssertFail', function() {
260 /**
261 * A HostResolverInfo observer that triggers an assertion in response to the
262 * first seen event.
263 */
264 function HostResolverInfoObserver() {
265 }
266
267 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() {
268 assertNotReached();
269 };
270
271 // Create the observer and add it to |g_browser|.
272 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver());
273
274 // Needed to trigger an update.
275 netInternalsTest.switchToView('dns');
276 });
OLDNEW
« no previous file with comments | « chrome/test/data/webui/net_internals/log_view_painter.js ('k') | chrome/test/data/webui/net_internals/prerender_view.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698