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: |
| 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 |
| 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 |
| 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() { |
| 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 |
| 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 }; |
| 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') { |
| 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 }; |
| 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()); |
| 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 // A HostResolverInfo observer that calls testDone() in response to the |
| 207 // first seen event. |
| 208 function HostResolverInfoObserver() { |
| 209 }; |
| 210 |
| 211 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
| 212 expectTrue(true); |
| 213 assertFalse(false); |
| 214 testDone(); |
| 215 }; |
| 216 |
| 217 // Create the observer and add it to |g_browser|. |
| 218 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
| 219 |
| 220 // Needed to trigger an update. |
| 221 switchToView('dns'); |
| 222 }); |
| 223 |
| 224 netInternalsTest('NetInternalsObserverExpectFail', function() { |
| 225 // A HostResolverInfo observer that triggers an exception in response to the |
| 226 // first seen event. |
| 227 function HostResolverInfoObserver() { |
| 228 }; |
| 229 |
| 230 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
| 231 expectFalse(true); |
| 232 testDone(); |
| 233 }; |
| 234 |
| 235 // Create the observer and add it to |g_browser|. |
| 236 g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
| 237 |
| 238 // Needed to trigger an update. |
| 239 switchToView('dns'); |
| 240 }); |
| 241 |
| 242 netInternalsTest('NetInternalsObserverAssertFail', function() { |
| 243 // A HostResolverInfo observer that triggers an assertion in response to the |
| 244 // first seen event. |
| 245 function HostResolverInfoObserver() { |
| 246 }; |
| 247 |
| 248 HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
| 249 assertTrue(false); |
| 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 switchToView('dns'); |
| 257 }); |
OLD | NEW |