Chromium Code Reviews| Index: chrome/test/data/webui/net_internals/framework.js |
| =================================================================== |
| --- chrome/test/data/webui/net_internals/framework.js (revision 0) |
| +++ chrome/test/data/webui/net_internals/framework.js (revision 0) |
| @@ -0,0 +1,263 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * 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.
|
| + * C++ in net_internals_ui_browsertest.cc does any necessary setup, and then |
| + * calls the entry point for a test with RunJavascriptTest. The called |
| + * function can then use the assert/expect functions defined in test_api.js. |
| + * All callbacks from the browser are wrapped in such a way that they can |
| + * also use the assert/expect functions. |
| + * |
| + * A test ends when an assert/expect test fails, an exception is thrown, or |
| + * 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.
|
| + * updated to 'Test Failed' if an assert/expect test fails, or there was an |
| + * 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.
|
| + * an assert/expect test fails or an assertion is thrown only after testDone |
| + * is called is undefined. |
| + */ |
| + |
| +// Exported functions. |
| +var testDone; |
| +var checkStyledTableRows; |
| +var switchToView; |
| +var netInternalsTest; |
| +var runNetInternalsTest; |
| + |
| +// Start of anonymous namespace. |
| +(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
|
| + // Use a shorter poll interval for tests, since a few tests wait for polled |
| + // values to change. |
| + const TESTING_POLL_INTERVAL_MS = 50; |
| + |
| + // Indicates the test is complete. |
| + var done = false; |
| + |
| + /** |
| + * Updates the title of the page to report success or failure. Must be |
| + * called at most once for each test. |
| + */ |
| + function updateTitle(success) { |
| + if (success) { |
| + document.title = 'Test Passed'; |
| + } else { |
| + document.title = 'Test Failed'; |
| + } |
| + done = true; |
| + } |
| + |
| + /** |
| + * Called to indicate a test is complete. |
| + */ |
| + testDone = function() { |
| + done = true; |
| + }; |
| + |
| + /** |
| + * 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.
|
| + * functions in test_api.js. On failure, will set title to 'Test Failed', and |
| + * when a test is done and there was no failure, will set title to |
| + * 'Test Passed'. Calling expect/assert functions after testDone has been |
| + * called has undefined behavior. Returned test functions can safely call |
| + * each other directly. |
| + * |
| + * The resulting function has no return value. |
| + */ |
| + function createTestFunction(testName, testFunction) { |
| + return function() { |
| + // Convert arguments to an array, as their map method may be called on |
| + // failure by runTestFunction. |
| + var testArguments = Array.prototype.slice.call(arguments, 0); |
| + |
| + // If the test is already complete, do nothing. |
| + if (done) |
| + return; |
| + |
| + var result = runTestFunction(testName, testFunction, testArguments); |
| + |
| + // If the first value is false, the test failed. |
| + if (!result[0]) { |
| + // Print any error messages. |
| + console.log(result[1]); |
| + // Update title to indicate failure. |
| + updateTitle(false); |
| + } else if (done) { |
| + // If the first result is true, and |done| is also true, the test |
| + // passed. Update title to indicate success. |
| + updateTitle(true); |
| + } |
| + }; |
| + }; |
|
eroman
2011/08/03 00:32:24
nit: this semicolon is redundant.
mmenke
2011/08/03 15:44:17
Done.
|
| + |
| + /** |
| + * Finds the first styled table that's a child of |parentId|, and returns the |
| + * number of rows it has. Returns -1 if there's no such table. |
| + */ |
| + function getStyledTableNumRows(parentId) { |
| + var parent = document.getElementById(parentId); |
| + for (var i = 0; i < parent.children.length; ++i) { |
| + 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.
|
| + assertEquals('tbody', parent.children[i].children[1].localName, |
| + 'tbody expected as second child of styled table ' + |
| + parentId); |
| + return parent.children[i].children[1].children.length; |
| + } |
| + } |
| + return -1; |
| + }; |
|
eroman
2011/08/03 00:32:24
nit: redundant semi-colon.
mmenke
2011/08/03 15:44:17
Removed.
|
| + |
| + /** |
| + * Finds the first styled table that's a child of the element with the given |
| + * id, and checks if it has exactly |expectedRows| rows, not including the |
| + * header row. |
| + */ |
| + checkStyledTableRows = function(parentId, expectedRows) { |
| + expectEquals(expectedRows, getStyledTableNumRows(parentId), |
| + 'Incorrect number of rows in ' + parentId); |
| + }; |
| + |
| + /** |
| + * Switches to the specified tab. |
| + * TODO(mmenke): check that the tab visibility changes as expected. |
| + */ |
| + switchToView = function(tabId) { |
| + document.location.hash = '#' + tabId; |
| + }; |
| + |
| + // Dictionary of tests. |
| + var tests = {}; |
| + |
| + /** |
| + * Used to declare a test function called by the NetInternals browser test. |
| + * Takes in a name and a function, and adds it to the list of tests. |
| + */ |
| + netInternalsTest = function(testName, testFunction) { |
| + tests[testName] = testFunction; |
| + }; |
| + |
| + /** |
| + * Called by the browser to start the tests. If constants haven't been |
| + * received from the browser yet, waits until they have been. |
| + * Experimentally, this never seems to happen, but may theoretically be |
| + * possible. |
| + */ |
| + runNetInternalsTest = function(testName, testArguments) { |
| + // If we've already received the constants, start the tests. |
| + if (typeof(LogEventType) != 'undefined') { |
| + startNetInternalsTest(testName, testArguments); |
| + return; |
| + } |
| + |
| + // Otherwise, wait until we do. |
| + console.log('Received constants late.'); |
| + |
| + /** |
| + * Observer that starts the tests once we've received the constants. |
| + */ |
| + function ConstantsObserver() { |
| + this.testStarted_ = false; |
| + } |
| + |
| + ConstantsObserver.prototype.onConstantsReceived = function() { |
| + if (!this.testStarted_) { |
| + this.testStarted_ = true; |
| + startNetInternalsTest(testFunction, testArguments); |
| + } |
| + }; |
| + |
| + 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
|
| + }; |
| + |
| + /** |
| + * Starts running the test. A test is run until an assert/expect statement |
| + * fails or testDone is called. Those functions can only be called in the |
| + * test function body, or in response to a message dispatched by |
| + * |g_browser.receive|. |
| + */ |
| + function startNetInternalsTest(testName, testArguments) { |
| + // Wrap g_browser.receive around a test function so that assert and expect |
| + // functions can be called from observers. |
| + g_browser.receive = createTestFunction(testName, function() { |
| + BrowserBridge.prototype.receive.apply(g_browser, arguments); |
| + }); |
| + |
| + createTestFunction(testName, tests[testName]).apply(null, testArguments); |
| + } |
| + |
| +// End of anonymous namespace |
| +})(); |
| + |
| +netInternalsTest('NetInternalsDone', function() { |
| + expectTrue(true); |
| + assertFalse(false); |
| + testDone(); |
| +}); |
| + |
| +netInternalsTest('NetInternalsExpectFail', function() { |
| + expectTrue(false); |
| +}); |
| + |
| +netInternalsTest('NetInternalsAssertFail', function() { |
| + assertFalse(true); |
| +}); |
| + |
| +netInternalsTest('NetInternalsObserverDone', function() { |
| + /** |
| + * A HostResolverInfo observer that calls testDone() in response to the |
| + * first seen event. |
| + */ |
| + function HostResolverInfoObserver() { |
| + }; |
|
eroman
2011/08/03 00:32:24
nit: redundant semicolon
mmenke
2011/08/03 15:44:17
Done.
|
| + |
| + HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
| + expectTrue(true); |
| + 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.
|
| + testDone(); |
| + }; |
| + |
| + // Create the observer and add it to |g_browser|. |
| + 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
|
| + |
| + // Needed to trigger an update. |
| + switchToView('dns'); |
| +}); |
| + |
| +netInternalsTest('NetInternalsObserverExpectFail', function() { |
| + /** |
| + * A HostResolverInfo observer that triggers an exception in response to the |
| + * first seen event. |
| + */ |
| + function HostResolverInfoObserver() { |
| + }; |
|
eroman
2011/08/03 00:32:24
nit: redundant semicolon.
mmenke
2011/08/03 15:44:17
Done.
|
| + |
| + HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
| + expectFalse(true); |
| + testDone(); |
| + }; |
| + |
| + // Create the observer and add it to |g_browser|. |
| + g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
|
eroman
2011/08/03 00:32:24
Same comment as above, we don't remove the observe
|
| + |
| + // Needed to trigger an update. |
| + switchToView('dns'); |
| +}); |
| + |
| +netInternalsTest('NetInternalsObserverAssertFail', function() { |
| + /** |
| + * A HostResolverInfo observer that triggers an assertion in response to the |
| + * first seen event. |
| + */ |
| + function HostResolverInfoObserver() { |
| + }; |
|
eroman
2011/08/03 00:32:24
...
mmenke
2011/08/03 15:44:17
Done.
|
| + |
| + HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
| + assertTrue(false); |
| + }; |
| + |
| + // Create the observer and add it to |g_browser|. |
| + g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
| + |
| + // Needed to trigger an update. |
| + switchToView('dns'); |
| +}); |
| Property changes on: chrome\test\data\webui\net_internals\framework.js |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |