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|, opened a new tab if needed, |
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 be prerendered. |
37 * @param {string} successUrl URL that can be prerendered. | 45 * @param {string} shouldSucceed Whether or not the prerender should succeed. |
| 46 * @param {string} finalStatus The expected value of |final_status|. |
38 * @constructor | 47 * @constructor |
39 */ | 48 */ |
40 function PrerenderTestObserver(failureUrl, successUrl) { | 49 function PrerenderTestObserver(url, shouldSucceed, finalStatus) { |
41 // True if we've started prerendering |successUrl|. | 50 // True if we've started prerendering |successUrl|. |
42 this.startedSuccessfulPrerender_ = false; | 51 this.startedSuccessfulPrerender_ = false; |
43 this.failureUrl_ = failureUrl; | 52 this.url_ = url; |
44 this.successUrl_ = successUrl; | 53 this.shouldSucceed_ = shouldSucceed; |
| 54 this.finalStatus_ = finalStatus; |
45 this.state_ = STATE.START; | 55 this.state_ = STATE.START; |
46 } | 56 } |
47 | 57 |
48 PrerenderTestObserver.prototype = { | 58 PrerenderTestObserver.prototype = { |
49 /** | 59 /** |
50 * Main function of the observer. Tracks state transitions, checks the | 60 * Main function of the observer. Tracks state transitions, checks the |
51 * table sizes, and does some sanity checking on received data. | 61 * table sizes, and does some sanity checking on received data. |
52 * @param {Object} prerenderInfo State of prerendering pages. | 62 * @param {Object} prerenderInfo State of prerendering pages. |
53 */ | 63 */ |
54 onPrerenderInfoChanged: function(prerenderInfo) { | 64 onPrerenderInfoChanged: function(prerenderInfo) { |
55 console.log('State: ' + this.state_); | |
56 | |
57 // Verify that prerendering is enabled. | 65 // Verify that prerendering is enabled. |
58 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); | 66 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); |
59 | 67 |
60 // Check number of rows in both tables. | 68 // Check number of rows in both tables. |
61 netInternalsTest.checkStyledTableRows(HISTORY_DIV_ID, | 69 netInternalsTest.checkStyledTableRows(HISTORY_DIV_ID, |
62 prerenderInfo.history.length); | 70 prerenderInfo.history.length); |
63 netInternalsTest.checkStyledTableRows(ACTIVE_DIV_ID, | 71 netInternalsTest.checkStyledTableRows(ACTIVE_DIV_ID, |
64 prerenderInfo.active.length); | 72 prerenderInfo.active.length); |
65 | 73 |
66 if (this.state_ == STATE.START) { | 74 if (this.state_ == STATE.START) { |
67 this.start_(prerenderInfo); | 75 this.start_(prerenderInfo); |
68 } else if (this.state_ == STATE.FAILURE_URL_LINKED) { | 76 } else if (this.state_ == STATE.NEED_OPEN_IN_NEW_TAB) { |
69 this.failureUrlLinked_(prerenderInfo); | 77 this.openInNewTab_(prerenderInfo); |
70 } else if (this.state_ == STATE.SUCCESS_URL_LINKED) { | 78 } else if (this.state_ == STATE.HISTORY_WAIT) { |
71 this.successUrlLinked_(prerenderInfo); | 79 this.checkDone_(prerenderInfo); |
72 } | 80 } |
73 }, | 81 }, |
74 | 82 |
75 /** | 83 /** |
76 * Start by triggering a prerender of |failureUrl_|. | 84 * Start by triggering a prerender of |url_|. |
77 * At this point, we expect no active or historical prerender entries. | 85 * At this point, we expect no active or historical prerender entries. |
78 * @param {Object} prerenderInfo State of prerendering pages. | 86 * @param {Object} prerenderInfo State of prerendering pages. |
79 */ | 87 */ |
80 start_: function(prerenderInfo) { | 88 start_: function(prerenderInfo) { |
81 expectEquals(0, prerenderInfo.active.length); | 89 expectEquals(0, prerenderInfo.active.length); |
82 expectEquals(0, prerenderInfo.history.length); | 90 expectEquals(0, prerenderInfo.history.length); |
83 | 91 |
84 // Adding the url we expect to fail. | 92 // Adding the url we expect to fail. |
85 addPrerenderLink(this.failureUrl_); | 93 addPrerenderLink(this.url_); |
86 this.state_ = STATE.FAILURE_URL_LINKED; | 94 if (this.shouldSucceed_) { |
| 95 this.state_ = STATE.NEED_OPEN_IN_NEW_TAB; |
| 96 } else { |
| 97 this.state_ = STATE.HISTORY_WAIT; |
| 98 } |
| 99 }, |
| 100 |
| 101 /** |
| 102 * Starts opening |url_| in a new tab. |
| 103 * At this point, we expect the prerender to be active. |
| 104 * Only called if |shouldSucceed_| is true, and |urlOpenedInNewTab_| is |
| 105 * false. |
| 106 * @param {Object} prerenderInfo State of prerendering pages. |
| 107 */ |
| 108 openInNewTab_: function(prerenderInfo) { |
| 109 expectEquals(0, prerenderInfo.history.length); |
| 110 assertEquals(1, prerenderInfo.active.length); |
| 111 expectEquals(this.url_, prerenderInfo.active[0].url); |
| 112 expectTrue(this.shouldSucceed_); |
| 113 |
| 114 chrome.send('openNewTab', [this.url_]); |
| 115 this.state_ = STATE.HISTORY_WAIT; |
87 }, | 116 }, |
88 | 117 |
89 /** | 118 /** |
90 * We expect to either see the failure url as an active entry, or see it | 119 * 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. | 120 * in the history. In the latter case, the test completes. |
92 * @param {Object} prerenderInfo State of prerendering pages. | 121 * @param {Object} prerenderInfo State of prerendering pages. |
93 */ | 122 */ |
94 failureUrlLinked_: function(prerenderInfo) { | 123 checkDone_: function(prerenderInfo) { |
95 // May see the failure url as active, or may see it move straight to the | 124 // 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) { | 125 if (prerenderInfo.active.length == 1) { |
98 expectEquals(this.failureUrl_, prerenderInfo.active[0].url); | 126 expectEquals(this.url_, prerenderInfo.active[0].url); |
99 expectEquals(0, prerenderInfo.history.length); | 127 expectEquals(0, prerenderInfo.history.length); |
100 return; | 128 return; |
101 } | 129 } |
102 | 130 |
103 // The prerender of |failureUrl_| has been cancelled, and is now in the | 131 // The prerender of |url_| is now in the history. |
104 // history. Go ahead and prerender |successUrl_|. | 132 this.checkHistory_(prerenderInfo); |
105 this.prerenderSuccessUrl_(prerenderInfo); | |
106 }, | 133 }, |
107 | 134 |
108 /** | 135 /** |
109 * Prerender |successUrl_|. The prerender of |failureUrl_| should have | 136 * 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. | 137 * @param {Object} prerenderInfo State of prerendering pages. |
112 */ | 138 */ |
113 prerenderSuccessUrl_: function(prerenderInfo) { | 139 checkHistory_: function(prerenderInfo) { |
114 // We may see the duration of the active prerender increase. If so, | 140 expectEquals(0, prerenderInfo.active.length); |
115 // do nothing. | 141 assertEquals(1, prerenderInfo.history.length); |
116 if (prerenderInfo.active.length == 1) | 142 expectEquals(this.url_, prerenderInfo.history[0].url); |
117 return; | 143 expectEquals(this.finalStatus_, prerenderInfo.history[0].final_status); |
118 | 144 |
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(); | 145 netInternalsTest.testDone(); |
138 }, | 146 } |
139 }; | 147 }; |
140 | 148 |
141 /** | 149 /** |
142 * Adds a <link rel="prerender" href="url"> to the document. | 150 * Adds a <link rel="prerender" href="url"> to the document. |
143 * @param {string} url URL of the page to prerender. | 151 * @param {string} url URL of the page to prerender. |
144 */ | 152 */ |
145 function addPrerenderLink(url) { | 153 function addPrerenderLink(url) { |
146 var link = document.createElement('link'); | 154 var link = document.createElement('link'); |
147 link.setAttribute('rel', 'prerender'); | 155 link.setAttribute('rel', 'prerender'); |
148 link.setAttribute('href', url); | 156 link.setAttribute('href', url); |
149 document.body.appendChild(link); | 157 document.body.appendChild(link); |
150 } | 158 } |
151 | 159 |
152 netInternalsTest.switchToView('prerender'); | 160 netInternalsTest.switchToView('prerender'); |
153 | 161 |
154 // Create the test observer, which will start the test once we see the initial | 162 // Create the test observer, which will start the test once we see the initial |
155 // onPrerenderInfoChanged event from changing the active tab. | 163 // onPrerenderInfoChanged event from changing the active tab. |
156 var prerenderObserver = new PrerenderTestObserver(failureUrl, successUrl); | 164 var prerenderObserver = new PrerenderTestObserver(url, shouldSucceed, |
| 165 finalStatus); |
157 g_browser.addPrerenderInfoObserver(prerenderObserver); | 166 g_browser.addPrerenderInfoObserver(prerenderObserver); |
158 }); | 167 }); |
OLD | NEW |