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('NetInternalsPrerenderView', |
| 13 function (failureUrl, successUrl) { |
| 14 // IDs for special HTML elements in prerender_view.html |
| 15 const HISTORY_DIV_ID = 'prerender-view-history-div'; |
| 16 const 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 */ |
| 37 function PrerenderTestObserver(failureUrl, successUrl) { |
| 38 // True if we've started prerendering |successUrl|. |
| 39 this.startedSuccessfulPrerender_ = false; |
| 40 this.failureUrl_ = failureUrl; |
| 41 this.successUrl_ = successUrl; |
| 42 this.state_ = STATE.START; |
| 43 } |
| 44 |
| 45 PrerenderTestObserver.prototype = { |
| 46 /** |
| 47 * Main function of the observer. Tracks state transitions, checks the |
| 48 * table sizes, and does some sanity checking on received data. |
| 49 */ |
| 50 onPrerenderInfoChanged: function(prerenderInfo) { |
| 51 console.log('State: ' + this.state_); |
| 52 |
| 53 // Verify that prerendering is enabled. |
| 54 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); |
| 55 |
| 56 // Check number of rows in both tables. |
| 57 checkStyledTableRows(HISTORY_DIV_ID, prerenderInfo.history.length); |
| 58 checkStyledTableRows(ACTIVE_DIV_ID, prerenderInfo.active.length); |
| 59 |
| 60 if (this.state_ == STATE.START) { |
| 61 this.start_(prerenderInfo); |
| 62 } else if (this.state_ == STATE.FAILURE_URL_LINKED) { |
| 63 this.failureUrlLinked_(prerenderInfo); |
| 64 } else if (this.state_ == STATE.SUCCESS_URL_LINKED) { |
| 65 this.successUrlLinked_(prerenderInfo); |
| 66 } |
| 67 }, |
| 68 |
| 69 /** |
| 70 * Start by triggering a prerender of |failureUrl_|. |
| 71 * At this point, we expect no active or historical prerender entries. |
| 72 */ |
| 73 start_: function(prerenderInfo) { |
| 74 expectEquals(0, prerenderInfo.active.length); |
| 75 expectEquals(0, prerenderInfo.history.length); |
| 76 |
| 77 // Adding the url we expect to fail. |
| 78 addPrerenderLink(this.failureUrl_); |
| 79 this.state_ = STATE.FAILURE_URL_LINKED; |
| 80 }, |
| 81 |
| 82 /** |
| 83 * We expect to either see the failure url as an active entry, or see it |
| 84 * move straight to the history. In the latter case, we skip a state. |
| 85 */ |
| 86 failureUrlLinked_: function(prerenderInfo) { |
| 87 // May see the failure url as active, or may see it move straight to the |
| 88 // history. If not, skip to the next state. |
| 89 if (prerenderInfo.active.length == 1) { |
| 90 expectEquals(this.failureUrl_, prerenderInfo.active[0].url); |
| 91 expectEquals(0, prerenderInfo.history.length); |
| 92 return; |
| 93 } |
| 94 |
| 95 // The prerender of |failureUrl_| has been cancelled, and is now in the |
| 96 // history. Go ahead and prerender |successUrl_|. |
| 97 this.prerenderSuccessUrl_(prerenderInfo); |
| 98 }, |
| 99 |
| 100 /** |
| 101 * Prerender |successUrl_|. The prerender of |failureUrl_| should have |
| 102 * failed, and it should now be in the history. |
| 103 */ |
| 104 prerenderSuccessUrl_: function(prerenderInfo) { |
| 105 // We may see the duration of the active prerender increase. If so, |
| 106 // do nothing. |
| 107 if (prerenderInfo.active.length == 1) |
| 108 return; |
| 109 |
| 110 assertEquals(1, prerenderInfo.history.length); |
| 111 expectEquals(this.failureUrl_, prerenderInfo.history[0].url); |
| 112 expectEquals(0, prerenderInfo.active.length); |
| 113 |
| 114 addPrerenderLink(this.successUrl_); |
| 115 this.state_ = STATE.SUCCESS_URL_LINKED; |
| 116 }, |
| 117 |
| 118 /** |
| 119 * At this point, we expect to see the failure url in the history, and the |
| 120 * successUrl in the active entry list, and the test is done. |
| 121 */ |
| 122 successUrlLinked_: function(prerenderInfo) { |
| 123 assertEquals(1, prerenderInfo.history.length); |
| 124 expectEquals(this.failureUrl_, prerenderInfo.history[0].url); |
| 125 assertEquals(1, prerenderInfo.active.length); |
| 126 expectEquals(this.successUrl_, prerenderInfo.active[0].url); |
| 127 testDone(); |
| 128 }, |
| 129 }; |
| 130 |
| 131 /** |
| 132 * Adds a <link rel="prerender" href="url"> to the document. |
| 133 */ |
| 134 function addPrerenderLink(url) { |
| 135 var link = document.createElement('link'); |
| 136 link.setAttribute('rel', 'prerender'); |
| 137 link.setAttribute('href', url); |
| 138 document.body.appendChild(link); |
| 139 } |
| 140 |
| 141 switchToView('prerender'); |
| 142 |
| 143 // Create the test observer, which will start the test once we see the initial |
| 144 // onPrerenderInfoChanged event from changing the active tab. |
| 145 var prerenderObserver = new PrerenderTestObserver(failureUrl, successUrl); |
| 146 g_browser.addPrerenderInfoObserver(prerenderObserver); |
| 147 }); |
OLD | NEW |