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 a page. |shouldSucceed| indicates whether the prerender | 6 * Tries to prerender a page. A loader page is opened in a background tab, |
7 * is expected to succeed or not. If it's false, we just wait for the page | 7 * which triggers the prerender as well as has a link to it. |shouldSucceed| |
8 * to fail, possibly seeing it as active first. If it's true, we open the | 8 * indicates whether the prerender is expected to succeed or not. |
9 * URL in another tab. This is done via a message to the test handler, rather | 9 * If it's false, we just wait for the page to fail, possibly seeing it as |
10 * than via Javascript, so we don't cancel the prerender since prerendering | 10 * active first. If it's true, we navigate to the URL in the background tab. |
11 * can't currently set window.opener properly. | |
12 * | 11 * |
13 * Checks that we see all relevant events, and update the corresponding tables. | 12 * Checks that we see all relevant events, and update the corresponding tables. |
14 * In both cases, we exit the test once we see the prerender in the history. | 13 * In both cases, we exit the test once we see the prerender in the history. |
15 * |finalStatus| is the expected status value when the page reaches the | 14 * |finalStatus| is the expected status value when the page reaches the |
16 * history. | 15 * history. |
17 */ | 16 */ |
18 netInternalsTest.test('netInternalsPrerenderView', | 17 netInternalsTest.test('netInternalsPrerenderView', |
19 function (url, shouldSucceed, finalStatus) { | 18 function (url, loaderUrl, shouldSucceed, finalStatus) { |
20 // Phases of the test. | 19 // Phases of the test. |
21 const STATE = { | 20 const STATE = { |
22 // We've switched to the prerender tab, but have yet to receive the | 21 // We've switched to the prerender tab, but have yet to receive the |
23 // resulting onPrerenderInfoChanged event with no prerenders active or in | 22 // resulting onPrerenderInfoChanged event with no prerenders active or in |
24 // the history. | 23 // the history. |
25 START: 0, | 24 START: 0, |
26 // We've added the prerender link, but still need to open the link in a new | 25 // We're waiting for the prerender loader to start in a background tab, |
27 // tab. Only visit this state if |shouldSucceed| is true. | 26 // as well as the prerendered view to be created. Only visit this state if |
28 NEED_OPEN_IN_NEW_TAB: 1, | 27 // |shouldSucceed| is true. |
29 // We've added the prefetch link for |url|, opened a new tab if needed, | 28 NEED_NAVIGATE: 1, |
30 // and are waiting for it to move to the history. We may see the prerender | 29 // The prerendered view has been created, and we've started the navigation |
31 // one or more times in the active list, or it may move straight to the | 30 // to it. We're waiting for it to move to the history. We may see the |
32 // history. We will not receive any event with both history and active | 31 // prerender one or more times in the active list, or it may move straight |
33 // prerenders empty while in this state, as we only send notifications | 32 // to the history. We will not receive any event with both history and |
34 // when the values change. | 33 // active prerenders empty while in this state, as we only send |
| 34 // notifications when the values change. |
35 HISTORY_WAIT: 2 | 35 HISTORY_WAIT: 2 |
36 }; | 36 }; |
37 | 37 |
38 /** | 38 /** |
39 * Observer responsible for running the test and checking results. | 39 * Observer responsible for running the test and checking results. |
40 * @param {string} url URL to be prerendered. | 40 * @param {string} url URL to be prerendered. |
| 41 * @param {string} loaderUrl URL to trigger the prerender. |
41 * @param {string} shouldSucceed Whether or not the prerender should succeed. | 42 * @param {string} shouldSucceed Whether or not the prerender should succeed. |
42 * @param {string} finalStatus The expected value of |final_status|. | 43 * @param {string} finalStatus The expected value of |final_status|. |
43 * @constructor | 44 * @constructor |
44 */ | 45 */ |
45 function PrerenderTestObserver(url, shouldSucceed, finalStatus) { | 46 function PrerenderTestObserver(url, loaderUrl, shouldSucceed, finalStatus) { |
46 // True if we've started prerendering |successUrl|. | 47 // True if we've started prerendering |successUrl|. |
47 this.startedSuccessfulPrerender_ = false; | 48 this.startedSuccessfulPrerender_ = false; |
48 this.url_ = url; | 49 this.url_ = url; |
| 50 this.loaderUrl_ = loaderUrl; |
49 this.shouldSucceed_ = shouldSucceed; | 51 this.shouldSucceed_ = shouldSucceed; |
50 this.finalStatus_ = finalStatus; | 52 this.finalStatus_ = finalStatus; |
51 this.state_ = STATE.START; | 53 this.state_ = STATE.START; |
52 } | 54 } |
53 | 55 |
54 PrerenderTestObserver.prototype = { | 56 PrerenderTestObserver.prototype = { |
55 /** | 57 /** |
56 * Main function of the observer. Tracks state transitions, checks the | 58 * Main function of the observer. Tracks state transitions, checks the |
57 * table sizes, and does some sanity checking on received data. | 59 * table sizes, and does some sanity checking on received data. |
58 * @param {Object} prerenderInfo State of prerendering pages. | 60 * @param {Object} prerenderInfo State of prerendering pages. |
59 */ | 61 */ |
60 onPrerenderInfoChanged: function(prerenderInfo) { | 62 onPrerenderInfoChanged: function(prerenderInfo) { |
61 // Verify that prerendering is enabled. | 63 // Verify that prerendering is enabled. |
62 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); | 64 assertTrue(prerenderInfo.enabled, 'Prerendering not enabled.'); |
63 | 65 |
64 // Check number of rows in both tables. | 66 // Check number of rows in both tables. |
65 netInternalsTest.checkStyledTableRows(PrerenderView.HISTORY_DIV_ID, | 67 netInternalsTest.checkStyledTableRows(PrerenderView.HISTORY_DIV_ID, |
66 prerenderInfo.history.length); | 68 prerenderInfo.history.length); |
67 netInternalsTest.checkStyledTableRows(PrerenderView.ACTIVE_DIV_ID, | 69 netInternalsTest.checkStyledTableRows(PrerenderView.ACTIVE_DIV_ID, |
68 prerenderInfo.active.length); | 70 prerenderInfo.active.length); |
69 | 71 |
70 if (this.state_ == STATE.START) { | 72 if (this.state_ == STATE.START) { |
71 this.start_(prerenderInfo); | 73 this.start_(prerenderInfo); |
72 } else if (this.state_ == STATE.NEED_OPEN_IN_NEW_TAB) { | 74 } else if (this.state_ == STATE.NEED_NAVIGATE) { |
73 this.openInNewTab_(prerenderInfo); | 75 this.navigate_(prerenderInfo); |
74 } else if (this.state_ == STATE.HISTORY_WAIT) { | 76 } else if (this.state_ == STATE.HISTORY_WAIT) { |
75 this.checkDone_(prerenderInfo); | 77 this.checkDone_(prerenderInfo); |
76 } | 78 } |
77 }, | 79 }, |
78 | 80 |
79 /** | 81 /** |
80 * Start by triggering a prerender of |url_|. | 82 * Start by triggering a prerender of |url_| in a background tab. |
81 * At this point, we expect no active or historical prerender entries. | 83 * At this point, we expect no active or historical prerender entries. |
82 * @param {Object} prerenderInfo State of prerendering pages. | 84 * @param {Object} prerenderInfo State of prerendering pages. |
83 */ | 85 */ |
84 start_: function(prerenderInfo) { | 86 start_: function(prerenderInfo) { |
85 expectEquals(0, prerenderInfo.active.length); | 87 expectEquals(0, prerenderInfo.active.length); |
86 expectEquals(0, prerenderInfo.history.length); | 88 expectEquals(0, prerenderInfo.history.length); |
87 | 89 chrome.send('openNewTab', [this.loaderUrl_]); |
88 // Adding the url we expect to fail. | |
89 addPrerenderLink(this.url_); | |
90 if (this.shouldSucceed_) { | 90 if (this.shouldSucceed_) { |
91 this.state_ = STATE.NEED_OPEN_IN_NEW_TAB; | 91 this.state_ = STATE.NEED_NAVIGATE; |
92 } else { | 92 } else { |
93 this.state_ = STATE.HISTORY_WAIT; | 93 this.state_ = STATE.HISTORY_WAIT; |
94 } | 94 } |
95 }, | 95 }, |
96 | 96 |
97 /** | 97 /** |
98 * Starts opening |url_| in a new tab. | 98 * Navigate to the prerendered page in the background tab. |
99 * At this point, we expect the prerender to be active. | |
100 * Only called if |shouldSucceed_| is true, and |urlOpenedInNewTab_| is | |
101 * false. | |
102 * @param {Object} prerenderInfo State of prerendering pages. | 99 * @param {Object} prerenderInfo State of prerendering pages. |
103 */ | 100 */ |
104 openInNewTab_: function(prerenderInfo) { | 101 navigate_: function(prerenderInfo) { |
105 expectEquals(0, prerenderInfo.history.length); | 102 expectEquals(0, prerenderInfo.history.length); |
106 assertEquals(1, prerenderInfo.active.length); | 103 assertEquals(1, prerenderInfo.active.length); |
107 expectEquals(this.url_, prerenderInfo.active[0].url); | 104 expectEquals(this.url_, prerenderInfo.active[0].url); |
108 expectTrue(this.shouldSucceed_); | 105 expectTrue(this.shouldSucceed_); |
109 | 106 chrome.send('navigateToPrerender'); |
110 chrome.send('openNewTab', [this.url_]); | |
111 this.state_ = STATE.HISTORY_WAIT; | 107 this.state_ = STATE.HISTORY_WAIT; |
112 }, | 108 }, |
113 | 109 |
114 /** | 110 /** |
115 * We expect to either see the failure url as an active entry, or see it | 111 * We expect to either see the failure url as an active entry, or see it |
116 * in the history. In the latter case, the test completes. | 112 * in the history. In the latter case, the test completes. |
117 * @param {Object} prerenderInfo State of prerendering pages. | 113 * @param {Object} prerenderInfo State of prerendering pages. |
118 */ | 114 */ |
119 checkDone_: function(prerenderInfo) { | 115 checkDone_: function(prerenderInfo) { |
120 // If we see the url as active, continue running the test. | 116 // If we see the url as active, continue running the test. |
(...skipping 14 matching lines...) Expand all Loading... |
135 checkHistory_: function(prerenderInfo) { | 131 checkHistory_: function(prerenderInfo) { |
136 expectEquals(0, prerenderInfo.active.length); | 132 expectEquals(0, prerenderInfo.active.length); |
137 assertEquals(1, prerenderInfo.history.length); | 133 assertEquals(1, prerenderInfo.history.length); |
138 expectEquals(this.url_, prerenderInfo.history[0].url); | 134 expectEquals(this.url_, prerenderInfo.history[0].url); |
139 expectEquals(this.finalStatus_, prerenderInfo.history[0].final_status); | 135 expectEquals(this.finalStatus_, prerenderInfo.history[0].final_status); |
140 | 136 |
141 testDone(); | 137 testDone(); |
142 } | 138 } |
143 }; | 139 }; |
144 | 140 |
145 /** | |
146 * Adds a <link rel="prerender" href="url"> to the document. | |
147 * @param {string} url URL of the page to prerender. | |
148 */ | |
149 function addPrerenderLink(url) { | |
150 var link = document.createElement('link'); | |
151 link.setAttribute('rel', 'prerender'); | |
152 link.setAttribute('href', url); | |
153 document.body.appendChild(link); | |
154 } | |
155 | |
156 netInternalsTest.switchToView('prerender'); | 141 netInternalsTest.switchToView('prerender'); |
157 | 142 |
158 // Create the test observer, which will start the test once we see the initial | 143 // Create the test observer, which will start the test once we see the initial |
159 // onPrerenderInfoChanged event from changing the active tab. | 144 // onPrerenderInfoChanged event from changing the active tab. |
160 var prerenderObserver = new PrerenderTestObserver(url, shouldSucceed, | 145 var prerenderObserver = new PrerenderTestObserver(url, loaderUrl, |
161 finalStatus); | 146 shouldSucceed, finalStatus); |
162 g_browser.addPrerenderInfoObserver(prerenderObserver, true); | 147 g_browser.addPrerenderInfoObserver(prerenderObserver, true); |
163 }); | 148 }); |
OLD | NEW |