Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Tries to prerender two pages, one that will fail and one that will succeed. | 6 * Tries to prerender a page. |shouldSucceed| indicates whether the prerender |
| 7 * is expected to succeed or not. If it's false, we just wait for the page | |
| 8 * to fail, possibly seeing it as active first. If it's true, we open the | |
| 9 * URL in another tab. This is done via a message to the test handler, rather | |
| 10 * than via Javascript, so we don't cancel the prerender since prerendering | |
| 11 * can't currently set window.opener properly. | |
| 12 * | |
| 7 * Checks that we see all relevant events, and update the corresponding tables. | 13 * 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 | 14 * In both cases, we exit the test once we see the prerender in the history. |
| 9 * an active prerender will block another prerender from starting too soon, so | 15 * |finalStatus| is the expected status value when the page reaches the |
| 10 * |failureUrl| must be prerendered first. | 16 * history. |
| 11 */ | 17 */ |
| 12 netInternalsTest.test('NetInternalsPrerenderView', | 18 netInternalsTest.test('NetInternalsPrerenderView', |
| 13 function (failureUrl, successUrl) { | 19 function (url, shouldSucceed, finalStatus) { |
| 14 // IDs for special HTML elements in prerender_view.html | 20 // IDs for special HTML elements in prerender_view.html |
| 15 var HISTORY_DIV_ID = 'prerender-view-history-div'; | 21 var HISTORY_DIV_ID = 'prerender-view-history-div'; |
| 16 var ACTIVE_DIV_ID = 'prerender-view-active-div'; | 22 var ACTIVE_DIV_ID = 'prerender-view-active-div'; |
| 17 | 23 |
| 18 // Phases of the test. | 24 // Phases of the test. |
| 19 const STATE = { | 25 const STATE = { |
| 20 // We've switched to the prerender tab, but have yet to receive the | 26 // We've switched to the prerender tab, but have yet to receive the |
| 21 // resulting onPrerenderInfoChanged event with no prerenders active or in | 27 // resulting onPrerenderInfoChanged event with no prerenders active or in |
| 22 // the history. | 28 // the history. |
| 23 START: 0, | 29 START: 0, |
| 24 // We've added the prefetch link for |failureUrl|. We may receive more | 30 // We've added the prerender link, but still need to open the link in a new |
| 25 // than one event while in this state, as we may see it as active once | 31 // tab. Only visit this state if |shouldSucceed| is true. |
| 26 // or more before it moves to the history. We will not receive any | 32 NEED_OPEN_IN_NEW_TAB: 1, |
| 27 // event with both history and active prerenders empty in this state, | 33 // We've added the prefetch link for |url|, openned a new tab if needed, |
|
eroman
2011/08/09 18:41:54
spelling: openned
mmenke
2011/08/09 18:49:39
Done.
| |
| 28 // as we only send notifications when the values change. | 34 // and are waiting for it to move to the history. We may see the prerender |
| 29 FAILURE_URL_LINKED: 1, | 35 // one or more times in the active list, or it may move straight to the |
| 30 // We've added the prefetch link for |successUrl|. | 36 // history. We will not receive any event with both history and active |
| 31 SUCCESS_URL_LINKED: 2 | 37 // prerenders empty while in this state, as we only send notifications |
| 38 // when the values change. | |
| 39 HISTORY_WAIT: 2 | |
| 32 }; | 40 }; |
| 33 | 41 |
| 34 /** | 42 /** |
| 35 * Observer responsible for running the test and checking results. | 43 * Observer responsible for running the test and checking results. |
| 36 * @param {string} failureUrl URL that can't be prerendered. | 44 * @param {string} url URL to prerendered. |
|
eroman
2011/08/09 18:41:54
"to be prerendered"
mmenke
2011/08/09 18:49:39
Done.
| |
| 37 * @param {string} successUrl URL that can be prerendered. | 45 * @param {string} shouldSucceed whether or not the prerender should succeed. |
| 38 * @constructor | 46 * @constructor |
| 39 */ | 47 */ |
| 40 function PrerenderTestObserver(failureUrl, successUrl) { | 48 function PrerenderTestObserver(url, shouldSucceed, finalStatus) { |
| 41 // True if we've started prerendering |successUrl|. | 49 // True if we've started prerendering |successUrl|. |
| 42 this.startedSuccessfulPrerender_ = false; | 50 this.startedSuccessfulPrerender_ = false; |
| 43 this.failureUrl_ = failureUrl; | 51 this.url_ = url; |
| 44 this.successUrl_ = successUrl; | 52 this.shouldSucceed_ = shouldSucceed; |
| 53 this.finalStatus_ = finalStatus; | |
| 45 this.state_ = STATE.START; | 54 this.state_ = STATE.START; |
| 46 } | 55 } |
| 47 | 56 |
| 48 PrerenderTestObserver.prototype = { | 57 PrerenderTestObserver.prototype = { |
| 49 /** | 58 /** |
| 50 * Main function of the observer. Tracks state transitions, checks the | 59 * Main function of the observer. Tracks state transitions, checks the |
| 51 * table sizes, and does some sanity checking on received data. | 60 * table sizes, and does some sanity checking on received data. |
| 52 * @param {Object} prerenderInfo State of prerendering pages. | 61 * @param {Object} prerenderInfo State of prerendering pages. |
| 53 */ | 62 */ |
| 54 onPrerenderInfoChanged: function(prerenderInfo) { | 63 onPrerenderInfoChanged: function(prerenderInfo) { |
| 55 console.log('State: ' + this.state_); | |
| 56 | |
| 57 // Verify that prerendering is enabled. | 64 // Verify that prerendering is enabled. |
| 58 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); | 65 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); |
| 59 | 66 |
| 60 // Check number of rows in both tables. | 67 // Check number of rows in both tables. |
| 61 netInternalsTest.checkStyledTableRows(HISTORY_DIV_ID, | 68 netInternalsTest.checkStyledTableRows(HISTORY_DIV_ID, |
| 62 prerenderInfo.history.length); | 69 prerenderInfo.history.length); |
| 63 netInternalsTest.checkStyledTableRows(ACTIVE_DIV_ID, | 70 netInternalsTest.checkStyledTableRows(ACTIVE_DIV_ID, |
| 64 prerenderInfo.active.length); | 71 prerenderInfo.active.length); |
| 65 | 72 |
| 66 if (this.state_ == STATE.START) { | 73 if (this.state_ == STATE.START) { |
| 67 this.start_(prerenderInfo); | 74 this.start_(prerenderInfo); |
| 68 } else if (this.state_ == STATE.FAILURE_URL_LINKED) { | 75 } else if (this.state_ == STATE.NEED_OPEN_IN_NEW_TAB) { |
| 69 this.failureUrlLinked_(prerenderInfo); | 76 this.openInNewTab_(prerenderInfo); |
| 70 } else if (this.state_ == STATE.SUCCESS_URL_LINKED) { | 77 } else if (this.state_ == STATE.HISTORY_WAIT) { |
| 71 this.successUrlLinked_(prerenderInfo); | 78 this.checkDone_(prerenderInfo); |
| 72 } | 79 } |
| 73 }, | 80 }, |
| 74 | 81 |
| 75 /** | 82 /** |
| 76 * Start by triggering a prerender of |failureUrl_|. | 83 * Start by triggering a prerender of |url_|. |
| 77 * At this point, we expect no active or historical prerender entries. | 84 * At this point, we expect no active or historical prerender entries. |
| 78 * @param {Object} prerenderInfo State of prerendering pages. | 85 * @param {Object} prerenderInfo State of prerendering pages. |
| 79 */ | 86 */ |
| 80 start_: function(prerenderInfo) { | 87 start_: function(prerenderInfo) { |
| 81 expectEquals(0, prerenderInfo.active.length); | 88 expectEquals(0, prerenderInfo.active.length); |
| 82 expectEquals(0, prerenderInfo.history.length); | 89 expectEquals(0, prerenderInfo.history.length); |
| 83 | 90 |
| 84 // Adding the url we expect to fail. | 91 // Adding the url we expect to fail. |
| 85 addPrerenderLink(this.failureUrl_); | 92 addPrerenderLink(this.url_); |
| 86 this.state_ = STATE.FAILURE_URL_LINKED; | 93 if (this.shouldSucceed_) { |
| 94 this.state_ = STATE.NEED_OPEN_IN_NEW_TAB; | |
| 95 } else { | |
| 96 this.state_ = STATE.HISTORY_WAIT; | |
| 97 } | |
| 98 }, | |
| 99 | |
| 100 /** | |
| 101 * Starts opening |url_| in a new tab. | |
| 102 * At this point, we expect the prerender to be active. | |
| 103 * Only called if |shouldSucceed_| is true, and |urlOpenedInNewTab_| is | |
| 104 * false. | |
| 105 * @param {Object} prerenderInfo State of prerendering pages. | |
| 106 */ | |
| 107 openInNewTab_: function(prerenderInfo) { | |
| 108 expectEquals(0, prerenderInfo.history.length); | |
| 109 assertEquals(1, prerenderInfo.active.length); | |
| 110 expectEquals(this.url_, prerenderInfo.active[0].url); | |
| 111 expectTrue(this.shouldSucceed_); | |
| 112 | |
| 113 chrome.send('openNewTab', [this.url_]); | |
| 114 this.state_ = STATE.HISTORY_WAIT; | |
| 87 }, | 115 }, |
| 88 | 116 |
| 89 /** | 117 /** |
| 90 * We expect to either see the failure url as an active entry, or see it | 118 * 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. | 119 * in the history. In the latter case, the test completes. |
| 92 * @param {Object} prerenderInfo State of prerendering pages. | 120 * @param {Object} prerenderInfo State of prerendering pages. |
| 93 */ | 121 */ |
| 94 failureUrlLinked_: function(prerenderInfo) { | 122 checkDone_: function(prerenderInfo) { |
| 95 // May see the failure url as active, or may see it move straight to the | 123 // If we see the url as active, continue running the test. |
| 96 // history. If not, skip to the next state. | |
| 97 if (prerenderInfo.active.length == 1) { | 124 if (prerenderInfo.active.length == 1) { |
| 98 expectEquals(this.failureUrl_, prerenderInfo.active[0].url); | 125 expectEquals(this.url_, prerenderInfo.active[0].url); |
| 99 expectEquals(0, prerenderInfo.history.length); | 126 expectEquals(0, prerenderInfo.history.length); |
| 100 return; | 127 return; |
| 101 } | 128 } |
| 102 | 129 |
| 103 // The prerender of |failureUrl_| has been cancelled, and is now in the | 130 // The prerender of |url_| is now in the history. |
| 104 // history. Go ahead and prerender |successUrl_|. | 131 this.checkHistory_(prerenderInfo); |
| 105 this.prerenderSuccessUrl_(prerenderInfo); | |
| 106 }, | 132 }, |
| 107 | 133 |
| 108 /** | 134 /** |
| 109 * Prerender |successUrl_|. The prerender of |failureUrl_| should have | 135 * Check if the history is consistent with expectations, and end the test. |
| 110 * failed, and it should now be in the history. | |
| 111 * @param {Object} prerenderInfo State of prerendering pages. | 136 * @param {Object} prerenderInfo State of prerendering pages. |
| 112 */ | 137 */ |
| 113 prerenderSuccessUrl_: function(prerenderInfo) { | 138 checkHistory_: function(prerenderInfo) { |
| 114 // We may see the duration of the active prerender increase. If so, | 139 expectEquals(0, prerenderInfo.active.length); |
| 115 // do nothing. | 140 assertEquals(1, prerenderInfo.history.length); |
| 116 if (prerenderInfo.active.length == 1) | 141 expectEquals(this.url_, prerenderInfo.history[0].url); |
| 117 return; | 142 expectEquals(this.finalStatus_, prerenderInfo.history[0].final_status); |
| 118 | 143 |
| 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(); | 144 netInternalsTest.testDone(); |
| 138 }, | 145 } |
| 139 }; | 146 }; |
| 140 | 147 |
| 141 /** | 148 /** |
| 142 * Adds a <link rel="prerender" href="url"> to the document. | 149 * Adds a <link rel="prerender" href="url"> to the document. |
| 143 * @param {string} url URL of the page to prerender. | 150 * @param {string} url URL of the page to prerender. |
| 144 */ | 151 */ |
| 145 function addPrerenderLink(url) { | 152 function addPrerenderLink(url) { |
| 146 var link = document.createElement('link'); | 153 var link = document.createElement('link'); |
| 147 link.setAttribute('rel', 'prerender'); | 154 link.setAttribute('rel', 'prerender'); |
| 148 link.setAttribute('href', url); | 155 link.setAttribute('href', url); |
| 149 document.body.appendChild(link); | 156 document.body.appendChild(link); |
| 150 } | 157 } |
| 151 | 158 |
| 152 netInternalsTest.switchToView('prerender'); | 159 netInternalsTest.switchToView('prerender'); |
| 153 | 160 |
| 154 // Create the test observer, which will start the test once we see the initial | 161 // Create the test observer, which will start the test once we see the initial |
| 155 // onPrerenderInfoChanged event from changing the active tab. | 162 // onPrerenderInfoChanged event from changing the active tab. |
| 156 var prerenderObserver = new PrerenderTestObserver(failureUrl, successUrl); | 163 var prerenderObserver = new PrerenderTestObserver(url, shouldSucceed, |
| 164 finalStatus); | |
| 157 g_browser.addPrerenderInfoObserver(prerenderObserver); | 165 g_browser.addPrerenderInfoObserver(prerenderObserver); |
| 158 }); | 166 }); |
| OLD | NEW |