Chromium Code Reviews| Index: chrome/test/data/webui/net_internals/prerender_view.js |
| =================================================================== |
| --- chrome/test/data/webui/net_internals/prerender_view.js (revision 0) |
| +++ chrome/test/data/webui/net_internals/prerender_view.js (revision 0) |
| @@ -0,0 +1,147 @@ |
| +// 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. |
| + |
| +/** |
| + * Tries to prerender two pages, one that will fail and one that will succeed. |
| + * Checks that we see all relevant events, and update the corresponding tables. |
| + * The prerender that will fail will briefly be active before it fails. Having |
| + * an active prerender will block another prerender from starting too soon, so |
| + * |failureUrl| must be prerendered first. |
| + */ |
| +netInternalsTest('NetInternalsPrerenderView', |
| + function (failureUrl, successUrl) { |
| + // IDs for special HTML elements in prerender_view.html |
| + const HISTORY_DIV_ID = 'prerender-view-history-div'; |
| + const ACTIVE_DIV_ID = 'prerender-view-active-div'; |
| + |
| + // Phases of the test. |
| + const STATE = { |
| + // We've switched to the prerender tab, but have yet to receive the |
| + // resulting onPrerenderInfoChanged event with no prerenders active or in |
| + // the history. |
| + START: 0, |
| + // We've added the prefetch link for |failureUrl|. We may receive more |
| + // than one event while in this state, as we may see it as active once |
| + // or more before it moves to the history. We will not receive any |
| + // event with both history and active prerenders empty in this state, |
| + // as we only send notifications when the values change. |
| + FAILURE_URL_LINKED: 1, |
| + // We've added the prefetch link for |successUrl|. |
| + SUCCESS_URL_LINKED: 2, |
|
eroman
2011/08/03 00:32:24
nit: remove the trailing comma. I don't remember w
mmenke
2011/08/03 15:44:17
Done.
|
| + }; |
| + |
| + /** |
| + * Observer responsible for running the test and checking results. |
| + */ |
| + function PrerenderTestObserver(failureUrl, successUrl) { |
| + // True if we've started prerendering |successUrl|. |
| + this.startedSuccessfulPrerender_ = false; |
| + this.failureUrl_ = failureUrl; |
| + this.successUrl_ = successUrl; |
| + this.state_ = STATE.START; |
| + } |
| + |
| + PrerenderTestObserver.prototype = { |
| + /** |
| + * Main function of the observer. Tracks state transitions, checks the |
| + * table sizes, and does some sanity checking on received data. |
| + */ |
| + onPrerenderInfoChanged: function(prerenderInfo) { |
| + console.log('State: ' + this.state_); |
| + |
| + // Verify that prerendering is enabled. |
| + assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); |
| + |
| + // Check number of rows in both tables. |
| + checkStyledTableRows(HISTORY_DIV_ID, prerenderInfo.history.length); |
| + checkStyledTableRows(ACTIVE_DIV_ID, prerenderInfo.active.length); |
| + |
| + if (this.state_ == STATE.START) { |
| + this.start_(prerenderInfo); |
| + } else if (this.state_ == STATE.FAILURE_URL_LINKED) { |
| + this.failureUrlLinked_(prerenderInfo); |
| + } else if (this.state_ == STATE.SUCCESS_URL_LINKED) { |
| + this.successUrlLinked_(prerenderInfo); |
| + } |
| + }, |
| + |
| + /** |
| + * Start by triggering a prerender of |failureUrl_|. |
| + * At this point, we expect no active or historical prerender entries. |
| + */ |
| + start_: function(prerenderInfo) { |
| + expectEquals(0, prerenderInfo.active.length); |
| + expectEquals(0, prerenderInfo.history.length); |
| + |
| + // Adding the url we expect to fail. |
| + addPrerenderLink(this.failureUrl_); |
| + this.state_ = STATE.FAILURE_URL_LINKED; |
| + }, |
| + |
| + /** |
| + * We expect to either see the failure url as an active entry, or see it |
| + * move straight to the history. In the latter case, we skip a state. |
| + */ |
| + failureUrlLinked_: function(prerenderInfo) { |
| + // May see the failure url as active, or may see it move straight to the |
| + // history. If not, skip to the next state. |
| + if (prerenderInfo.active.length == 1) { |
| + expectEquals(this.failureUrl_, prerenderInfo.active[0].url); |
| + expectEquals(0, prerenderInfo.history.length); |
| + return; |
| + } |
| + |
| + // The prerender of |failureUrl_| has been cancelled, and is now in the |
| + // history. Go ahead and prerender |successUrl_|. |
| + this.prerenderSuccessUrl_(prerenderInfo); |
| + }, |
| + |
| + /** |
| + * Prerender |successUrl_|. The prerender of |failureUrl_| should have |
| + * failed, and it should now be in the history. |
| + */ |
| + prerenderSuccessUrl_: function(prerenderInfo) { |
| + // We may see the duration of the active prerender increase. If so, |
| + // do nothing. |
| + if (prerenderInfo.active.length == 1) |
| + return; |
| + |
| + assertEquals(1, prerenderInfo.history.length); |
| + expectEquals(this.failureUrl_, prerenderInfo.history[0].url); |
| + expectEquals(0, prerenderInfo.active.length); |
| + |
| + addPrerenderLink(this.successUrl_); |
| + this.state_ = STATE.SUCCESS_URL_LINKED; |
| + }, |
| + |
| + /** |
| + * At this point, we expect to see the failure url in the history, and the |
| + * successUrl in the active entry list, and the test is done. |
| + */ |
| + successUrlLinked_: function(prerenderInfo) { |
| + assertEquals(1, prerenderInfo.history.length); |
| + expectEquals(this.failureUrl_, prerenderInfo.history[0].url); |
| + assertEquals(1, prerenderInfo.active.length); |
| + expectEquals(this.successUrl_, prerenderInfo.active[0].url); |
| + testDone(); |
| + }, |
| + }; |
| + |
| + /** |
| + * Adds a <link rel="prerender" href="url"> to the document. |
| + */ |
| + function addPrerenderLink(url) { |
| + var link = document.createElement('link'); |
| + link.setAttribute('rel', 'prerender'); |
| + link.setAttribute('href', url); |
| + document.body.appendChild(link); |
| + } |
| + |
| + switchToView('prerender'); |
| + |
| + // Create the test observer, which will start the test once we see the initial |
| + // onPrerenderInfoChanged event from changing the active tab. |
| + var prerenderObserver = new PrerenderTestObserver(failureUrl, successUrl); |
| + g_browser.addPrerenderInfoObserver(prerenderObserver); |
| +}); |
| Property changes on: chrome\test\data\webui\net_internals\prerender_view.js |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |