Index: chrome/test/data/webui/net_internals/net_internals_test.js |
=================================================================== |
--- chrome/test/data/webui/net_internals/net_internals_test.js (revision 0) |
+++ chrome/test/data/webui/net_internals/net_internals_test.js (revision 0) |
@@ -0,0 +1,253 @@ |
+// 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 as follows: |
Sheridan Rawlins
2011/08/03 18:29:57
nit: @fileoverview
mmenke
2011/08/03 19:30:12
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 |
+ * |netInternalsTest.testDone| is called. At that point, or soon afterwards, |
+ * the title is updated to 'Test Failed' if an assert/expect test fails, or |
+ * there was an exception. Otherwise, it's set to 'Test Passed'. The |
+ * behavior when an assert/expect test fails or an assertion is thrown only |
+ * after |netInternalsTest.testDone| is called is undefined. |
+ */ |
+ |
+// Start of namespace. |
+var netInternalsTest = (function() { |
+ // Use a shorter poll interval for tests, since a few tests wait for polled |
+ // values to change. |
+ 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.
|
+ |
+ // 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. |
Sheridan Rawlins
2011/08/03 18:29:57
@param {boolean} success Description of success pa
mmenke
2011/08/03 19:30:12
Done.
|
+ */ |
+ function updateTitle(success) { |
+ if (success) { |
+ document.title = 'Test Passed'; |
+ } else { |
+ document.title = 'Test Failed'; |
+ } |
+ done = true; |
+ } |
+ |
+ /** |
+ * Called to indicate a test is complete. |
+ */ |
+ function testDone() { |
+ done = true; |
+ } |
+ |
+ /** |
+ * Creates a test function that can use the expect and assert 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 done has been called has undefined |
+ * behavior. Returned test functions can safely call each other directly. |
+ * |
+ * 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.
|
+ */ |
+ 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); |
+ } |
+ }; |
+ } |
+ |
+ // Dictionary of tests. |
Sheridan Rawlins
2011/08/03 18:29:57
@type {Object.<string, Function>}
mmenke
2011/08/03 19:30:12
Done.
|
+ 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. |
+ */ |
+ function test(testName, testFunction) { |
+ tests[testName] = testFunction; |
+ } |
+ |
+ /** |
+ * Called by the browser to start a test. 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. |
Sheridan Rawlins
2011/08/03 18:29:57
use @param
mmenke
2011/08/03 19:30:12
Done.
|
+ */ |
+ function runTest(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()); |
+ } |
+ |
+ /** |
+ * 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|. |
Sheridan Rawlins
2011/08/03 18:29:57
@param
mmenke
2011/08/03 19:30:12
Done.
|
+ */ |
+ 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('g_browser.receive', function() { |
+ BrowserBridge.prototype.receive.apply(g_browser, arguments); |
+ }); |
+ |
+ createTestFunction(testName, tests[testName]).apply(null, testArguments); |
+ } |
+ |
+ /** |
+ * 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. |
+ */ |
Sheridan Rawlins
2011/08/03 18:29:57
@param
mmenke
2011/08/03 19:30:12
Done.
|
+ function getStyledTableNumRows(parentId) { |
+ // The tbody element of the first styled table in |parentId|. |
+ var tbody = document.querySelector('#' + parentId + ' .styledTable tbody'); |
+ if (!tbody) |
+ return -1; |
+ return tbody.children.length; |
+ } |
+ |
+ /** |
+ * 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. |
Sheridan Rawlins
2011/08/03 18:29:57
@param
mmenke
2011/08/03 19:30:12
Done.
|
+ */ |
+ function checkStyledTableRows(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. |
+ */ |
+ function switchToView(tabId) { |
+ document.location.hash = '#' + tabId; |
+ } |
+ |
+ // Exported functions. |
+ return { |
+ test: test, |
+ runTest: runTest, |
+ testDone:testDone, |
+ checkStyledTableRows: checkStyledTableRows, |
+ switchToView: switchToView |
+ }; |
+})(); |
+ |
+netInternalsTest.test('NetInternalsDone', function() { |
+ netInternalsTest.testDone(); |
+}); |
+ |
+netInternalsTest.test('NetInternalsExpectFail', function() { |
+ expectTrue(false); |
Sheridan Rawlins
2011/08/03 18:29:57
expectNotReached
mmenke
2011/08/03 19:30:12
Done
|
+}); |
+ |
+netInternalsTest.test('NetInternalsAssertFail', function() { |
+ assertFalse(true); |
Sheridan Rawlins
2011/08/03 18:29:57
assertNotReached
mmenke
2011/08/03 19:30:12
Done
|
+}); |
+ |
+netInternalsTest.test('NetInternalsObserverDone', function() { |
+ /** |
+ * A HostResolverInfo observer that calls testDone() in response to the |
+ * first seen event. |
+ */ |
+ function HostResolverInfoObserver() { |
+ } |
+ |
+ HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
+ netInternalsTest.testDone(); |
+ }; |
+ |
+ // Create the observer and add it to |g_browser|. |
+ g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
+ |
+ // Needed to trigger an update. |
+ netInternalsTest.switchToView('dns'); |
+}); |
+ |
+netInternalsTest.test('NetInternalsObserverExpectFail', function() { |
+ /** |
+ * A HostResolverInfo observer that triggers an exception in response to the |
+ * first seen event. |
+ */ |
+ function HostResolverInfoObserver() { |
+ } |
+ |
+ HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
+ expectFalse(true); |
+ netInternalsTest.testDone(); |
+ }; |
+ |
+ // Create the observer and add it to |g_browser|. |
+ g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
+ |
+ // Needed to trigger an update. |
+ netInternalsTest.switchToView('dns'); |
+}); |
+ |
+netInternalsTest.test('NetInternalsObserverAssertFail', function() { |
+ /** |
+ * A HostResolverInfo observer that triggers an assertion in response to the |
+ * first seen event. |
+ */ |
+ function HostResolverInfoObserver() { |
+ } |
+ |
+ HostResolverInfoObserver.prototype.onHostResolverInfoChanged = function() { |
+ assertTrue(false); |
Sheridan Rawlins
2011/08/03 18:29:57
assertNotReached
mmenke
2011/08/03 19:30:12
Done
|
+ }; |
+ |
+ // Create the observer and add it to |g_browser|. |
+ g_browser.addHostResolverInfoObserver(new HostResolverInfoObserver()); |
+ |
+ // Needed to trigger an update. |
+ netInternalsTest.switchToView('dns'); |
+}); |
Property changes on: chrome\test\data\webui\net_internals\net_internals_test.js |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |