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