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 * Tries to prerender two pages, one that will fail and one that will succeed. |
| 7 * Checks that we see all relevant events, and update the corresponding tables. |
| 8 * The prerender that will fail will briefly be active before it fails. Having |
| 9 * an active prerender will block another prerender from starting too soon, so |
| 10 * |failureUrl| must be prerendered first. |
| 11 */ |
| 12 netInternalsTest.test('NetInternalsPrerenderView', |
| 13 function (failureUrl, successUrl) { |
| 14 // IDs for special HTML elements in prerender_view.html |
| 15 var HISTORY_DIV_ID = 'prerender-view-history-div'; |
| 16 var ACTIVE_DIV_ID = 'prerender-view-active-div'; |
| 17 |
| 18 // Phases of the test. |
| 19 const STATE = { |
| 20 // We've switched to the prerender tab, but have yet to receive the |
| 21 // resulting onPrerenderInfoChanged event with no prerenders active or in |
| 22 // the history. |
| 23 START: 0, |
| 24 // We've added the prefetch link for |failureUrl|. We may receive more |
| 25 // than one event while in this state, as we may see it as active once |
| 26 // or more before it moves to the history. We will not receive any |
| 27 // event with both history and active prerenders empty in this state, |
| 28 // as we only send notifications when the values change. |
| 29 FAILURE_URL_LINKED: 1, |
| 30 // We've added the prefetch link for |successUrl|. |
| 31 SUCCESS_URL_LINKED: 2 |
| 32 }; |
| 33 |
| 34 /** |
| 35 * Observer responsible for running the test and checking results. |
| 36 * @param {string} failureUrl URL that can't be prerendered. |
| 37 * @param {string} successUrl URL that can be prerendered. |
| 38 * @constructor |
| 39 */ |
| 40 function PrerenderTestObserver(failureUrl, successUrl) { |
| 41 // True if we've started prerendering |successUrl|. |
| 42 this.startedSuccessfulPrerender_ = false; |
| 43 this.failureUrl_ = failureUrl; |
| 44 this.successUrl_ = successUrl; |
| 45 this.state_ = STATE.START; |
| 46 } |
| 47 |
| 48 PrerenderTestObserver.prototype = { |
| 49 /** |
| 50 * Main function of the observer. Tracks state transitions, checks the |
| 51 * table sizes, and does some sanity checking on received data. |
| 52 * @param {Object} prerenderInfo State of prerendering pages. |
| 53 */ |
| 54 onPrerenderInfoChanged: function(prerenderInfo) { |
| 55 console.log('State: ' + this.state_); |
| 56 |
| 57 // Verify that prerendering is enabled. |
| 58 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); |
| 59 |
| 60 // Check number of rows in both tables. |
| 61 netInternalsTest.checkStyledTableRows(HISTORY_DIV_ID, |
| 62 prerenderInfo.history.length); |
| 63 netInternalsTest.checkStyledTableRows(ACTIVE_DIV_ID, |
| 64 prerenderInfo.active.length); |
| 65 |
| 66 if (this.state_ == STATE.START) { |
| 67 this.start_(prerenderInfo); |
| 68 } else if (this.state_ == STATE.FAILURE_URL_LINKED) { |
| 69 this.failureUrlLinked_(prerenderInfo); |
| 70 } else if (this.state_ == STATE.SUCCESS_URL_LINKED) { |
| 71 this.successUrlLinked_(prerenderInfo); |
| 72 } |
| 73 }, |
| 74 |
| 75 /** |
| 76 * Start by triggering a prerender of |failureUrl_|. |
| 77 * At this point, we expect no active or historical prerender entries. |
| 78 * @param {Object} prerenderInfo State of prerendering pages. |
| 79 */ |
| 80 start_: function(prerenderInfo) { |
| 81 expectEquals(0, prerenderInfo.active.length); |
| 82 expectEquals(0, prerenderInfo.history.length); |
| 83 |
| 84 // Adding the url we expect to fail. |
| 85 addPrerenderLink(this.failureUrl_); |
| 86 this.state_ = STATE.FAILURE_URL_LINKED; |
| 87 }, |
| 88 |
| 89 /** |
| 90 * We expect to either see the failure url as an active entry, or see it |
| 91 * move straight to the history. In the latter case, we skip a state. |
| 92 * @param {Object} prerenderInfo State of prerendering pages. |
| 93 */ |
| 94 failureUrlLinked_: function(prerenderInfo) { |
| 95 // May see the failure url as active, or may see it move straight to the |
| 96 // history. If not, skip to the next state. |
| 97 if (prerenderInfo.active.length == 1) { |
| 98 expectEquals(this.failureUrl_, prerenderInfo.active[0].url); |
| 99 expectEquals(0, prerenderInfo.history.length); |
| 100 return; |
| 101 } |
| 102 |
| 103 // The prerender of |failureUrl_| has been cancelled, and is now in the |
| 104 // history. Go ahead and prerender |successUrl_|. |
| 105 this.prerenderSuccessUrl_(prerenderInfo); |
| 106 }, |
| 107 |
| 108 /** |
| 109 * Prerender |successUrl_|. The prerender of |failureUrl_| should have |
| 110 * failed, and it should now be in the history. |
| 111 * @param {Object} prerenderInfo State of prerendering pages. |
| 112 */ |
| 113 prerenderSuccessUrl_: function(prerenderInfo) { |
| 114 // We may see the duration of the active prerender increase. If so, |
| 115 // do nothing. |
| 116 if (prerenderInfo.active.length == 1) |
| 117 return; |
| 118 |
| 119 assertEquals(1, prerenderInfo.history.length); |
| 120 expectEquals(this.failureUrl_, prerenderInfo.history[0].url); |
| 121 expectEquals(0, prerenderInfo.active.length); |
| 122 |
| 123 addPrerenderLink(this.successUrl_); |
| 124 this.state_ = STATE.SUCCESS_URL_LINKED; |
| 125 }, |
| 126 |
| 127 /** |
| 128 * At this point, we expect to see the failure url in the history, and the |
| 129 * successUrl in the active entry list, and the test is done. |
| 130 * @param {Object} prerenderInfo State of prerendering pages. |
| 131 */ |
| 132 successUrlLinked_: function(prerenderInfo) { |
| 133 assertEquals(1, prerenderInfo.history.length); |
| 134 expectEquals(this.failureUrl_, prerenderInfo.history[0].url); |
| 135 assertEquals(1, prerenderInfo.active.length); |
| 136 expectEquals(this.successUrl_, prerenderInfo.active[0].url); |
| 137 netInternalsTest.testDone(); |
| 138 }, |
| 139 }; |
| 140 |
| 141 /** |
| 142 * Adds a <link rel="prerender" href="url"> to the document. |
| 143 * @param {string} url URL of the page to prerender. |
| 144 */ |
| 145 function addPrerenderLink(url) { |
| 146 var link = document.createElement('link'); |
| 147 link.setAttribute('rel', 'prerender'); |
| 148 link.setAttribute('href', url); |
| 149 document.body.appendChild(link); |
| 150 } |
| 151 |
| 152 netInternalsTest.switchToView('prerender'); |
| 153 |
| 154 // Create the test observer, which will start the test once we see the initial |
| 155 // onPrerenderInfoChanged event from changing the active tab. |
| 156 var prerenderObserver = new PrerenderTestObserver(failureUrl, successUrl); |
| 157 g_browser.addPrerenderInfoObserver(prerenderObserver); |
| 158 }); |
OLD | NEW |