1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/path_service.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/net/url_request_context_getter.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "net/url_request/url_request_context.h"
// Prerender tests work as follows:
//
// A page with a prefetch link to the test page is loaded. Once prerendered,
// its Javascript function DidPrerenderPass() is called, which returns true if
// the page behaves as expected when prerendered.
//
// The prerendered page is then displayed on a tab. The Javascript function
// DidDisplayPass() is called, and returns true if the page behaved as it
// should while being displayed.
namespace prerender {
namespace {
bool CreateRedirect(const std::string& dest_url, std::string* redirect_path) {
std::vector<net::TestServer::StringPair> replacement_text;
replacement_text.push_back(make_pair("REPLACE_WITH_URL", dest_url));
return net::TestServer::GetFilePathWithReplacements(
"prerender_redirect.html",
replacement_text,
redirect_path);
}
// PrerenderContents that stops the UI message loop on DidStopLoading().
class TestPrerenderContents : public PrerenderContents {
public:
TestPrerenderContents(
PrerenderManager* prerender_manager, Profile* profile, const GURL& url,
const std::vector<GURL>& alias_urls,
const GURL& referrer,
FinalStatus expected_final_status)
: PrerenderContents(prerender_manager, profile, url, alias_urls,
referrer),
did_finish_loading_(false),
expected_final_status_(expected_final_status) {
}
virtual ~TestPrerenderContents() {
EXPECT_EQ(expected_final_status_, final_status());
// In the event we are destroyed, say if the prerender was canceled, quit
// the UI message loop.
if (!did_finish_loading_)
MessageLoopForUI::current()->Quit();
}
virtual void DidStopLoading() {
PrerenderContents::DidStopLoading();
did_finish_loading_ = true;
MessageLoopForUI::current()->Quit();
}
bool did_finish_loading() const { return did_finish_loading_; }
void set_did_finish_loading(bool did_finish_loading) {
did_finish_loading_ = did_finish_loading;
}
private:
bool did_finish_loading_;
FinalStatus expected_final_status_;
};
// PrerenderManager that uses TestPrerenderContents.
class WaitForLoadPrerenderContentsFactory : public PrerenderContents::Factory {
public:
explicit WaitForLoadPrerenderContentsFactory(
FinalStatus expected_final_status)
: expected_final_status_(expected_final_status) {
}
virtual PrerenderContents* CreatePrerenderContents(
PrerenderManager* prerender_manager, Profile* profile, const GURL& url,
const std::vector<GURL>& alias_urls, const GURL& referrer) {
return new TestPrerenderContents(prerender_manager, profile, url,
alias_urls, referrer,
expected_final_status_);
}
private:
FinalStatus expected_final_status_;
};
} // namespace
class PrerenderBrowserTest : public InProcessBrowserTest {
public:
PrerenderBrowserTest() : use_https_src_server_(false) {
EnableDOMAutomation();
}
virtual void SetUpCommandLine(CommandLine* command_line) {
command_line->AppendSwitchASCII(switches::kPrerender,
switches::kPrerenderSwitchValueEnabled);
#if defined(OS_MACOSX)
// The plugins directory isn't read by default on the Mac, so it needs to be
// explicitly registered.
FilePath app_dir;
PathService::Get(chrome::DIR_APP, &app_dir);
command_line->AppendSwitchPath(
switches::kExtraPluginDir,
app_dir.Append(FILE_PATH_LITERAL("plugins")));
#endif
}
void PrerenderTestURL(const std::string& html_file,
FinalStatus expected_final_status,
int total_navigations) {
ASSERT_TRUE(test_server()->Start());
std::string dest_path = "files/prerender/";
dest_path.append(html_file);
dest_url_ = test_server()->GetURL(dest_path);
std::vector<net::TestServer::StringPair> replacement_text;
replacement_text.push_back(
make_pair("REPLACE_WITH_PREFETCH_URL", dest_url_.spec()));
std::string replacement_path;
ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
"files/prerender/prerender_loader.html",
replacement_text,
&replacement_path));
net::TestServer* src_server = test_server();
scoped_ptr<net::TestServer> https_src_server;
if (use_https_src_server_) {
https_src_server.reset(
new net::TestServer(net::TestServer::TYPE_HTTPS,
FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
ASSERT_TRUE(https_src_server->Start());
src_server = https_src_server.get();
}
GURL src_url = src_server->GetURL(replacement_path);
Profile* profile = browser()->GetSelectedTabContents()->profile();
PrerenderManager* prerender_manager = profile->GetPrerenderManager();
ASSERT_TRUE(prerender_manager);
// This is needed to exit the event loop once the prerendered page has
// stopped loading or was cancelled.
prerender_manager->SetPrerenderContentsFactory(
new WaitForLoadPrerenderContentsFactory(expected_final_status));
// ui_test_utils::NavigateToURL uses its own observer and message loop.
// Since the test needs to wait until the prerendered page has stopped
// loading, rathather than the page directly navigated to, need to
// handle browser navigation directly.
browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED);
TestPrerenderContents* prerender_contents = NULL;
int navigations = 0;
while (true) {
ui_test_utils::RunMessageLoop();
++navigations;
prerender_contents =
static_cast<TestPrerenderContents*>(
prerender_manager->FindEntry(dest_url_));
if (prerender_contents == NULL ||
!prerender_contents->did_finish_loading() ||
navigations >= total_navigations) {
EXPECT_EQ(navigations, total_navigations);
break;
}
prerender_contents->set_did_finish_loading(false);
}
switch (expected_final_status) {
case FINAL_STATUS_USED: {
ASSERT_TRUE(prerender_contents != NULL);
ASSERT_TRUE(prerender_contents->did_finish_loading());
// Check if page behaves as expected while in prerendered state.
bool prerender_test_result = false;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
prerender_contents->render_view_host(), L"",
L"window.domAutomationController.send(DidPrerenderPass())",
&prerender_test_result));
EXPECT_TRUE(prerender_test_result);
break;
}
default:
// In the failure case, we should have removed dest_url_ from the
// prerender_manager.
EXPECT_TRUE(prerender_contents == NULL);
break;
}
}
void NavigateToDestURL() const {
ui_test_utils::NavigateToURL(browser(), dest_url_);
Profile* profile = browser()->GetSelectedTabContents()->profile();
PrerenderManager* prerender_manager = profile->GetPrerenderManager();
ASSERT_TRUE(prerender_manager);
// Make sure the PrerenderContents found earlier was used or removed
EXPECT_TRUE(prerender_manager->FindEntry(dest_url_) == NULL);
// Check if page behaved as expected when actually displayed.
bool display_test_result = false;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedTabContents()->render_view_host(), L"",
L"window.domAutomationController.send(DidDisplayPass())",
&display_test_result));
EXPECT_TRUE(display_test_result);
}
void set_use_https_src(bool use_https_src_server) {
use_https_src_server_ = use_https_src_server;
}
private:
GURL dest_url_;
bool use_https_src_server_;
};
// Checks that a page is correctly prerendered in the case of a
// <link rel=prefetch> tag and then loaded into a tab in response to a
// navigation.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) {
PrerenderTestURL("prerender_page.html",
FINAL_STATUS_USED, 1);
NavigateToDestURL();
}
// Checks that the prerendering of a page is canceled correctly when a
// Javascript alert is called.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertBeforeOnload) {
PrerenderTestURL(
"prerender_alert_before_onload.html",
FINAL_STATUS_JAVASCRIPT_ALERT, 1);
}
// Checks that the prerendering of a page is canceled correctly when a
// Javascript alert is called.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertAfterOnload) {
PrerenderTestURL(
"prerender_alert_after_onload.html",
FINAL_STATUS_JAVASCRIPT_ALERT, 1);
}
// Checks that plugins are not loaded while a page is being preloaded, but
// are loaded when the page is displayed.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDelayLoadPlugin) {
PrerenderTestURL("plugin_delay_load.html",
FINAL_STATUS_USED, 1);
NavigateToDestURL();
}
// Checks that plugins in an iframe are not loaded while a page is
// being preloaded, but are loaded when the page is displayed.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderIframeDelayLoadPlugin) {
PrerenderTestURL("prerender_iframe_plugin_delay_load.html",
FINAL_STATUS_USED, 1);
NavigateToDestURL();
}
// Renders a page that contains a prerender link to a page that contains an
// iframe with a source that requires http authentication. This should not
// prerender successfully.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttpAuthentication) {
PrerenderTestURL("prerender_http_auth_container.html",
FINAL_STATUS_AUTH_NEEDED, 1);
}
// Checks that HTML redirects work with prerendering - specifically, checks the
// page is used and plugins aren't loaded.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRedirect) {
std::string redirect_path;
ASSERT_TRUE(CreateRedirect("prerender_page.html", &redirect_path));
PrerenderTestURL(redirect_path,
FINAL_STATUS_USED, 2);
NavigateToDestURL();
}
// Prerenders a page that contains an automatic download triggered through an
// iframe. This should not prerender successfully.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadIFrame) {
PrerenderTestURL("prerender_download_iframe.html",
FINAL_STATUS_DOWNLOAD, 1);
}
// Prerenders a page that contains an automatic download triggered through
// Javascript changing the window.location. This should not prerender
// successfully.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadLocation) {
PrerenderTestURL("prerender_download_location.html",
FINAL_STATUS_DOWNLOAD, 2);
}
// Prerenders a page that contains an automatic download triggered through a
// <meta http-equiv="refresh"> tag. This should not prerender successfully.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadRefresh) {
PrerenderTestURL("prerender_download_refresh.html",
FINAL_STATUS_DOWNLOAD, 2);
}
// Checks that the referrer is set when prerendering.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrer) {
PrerenderTestURL("prerender_referrer.html",
FINAL_STATUS_USED, 1);
NavigateToDestURL();
}
// Checks that the referrer is not set when prerendering and the source page is
// HTTPS.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoSSLReferrer) {
set_use_https_src(true);
PrerenderTestURL("prerender_no_referrer.html",
FINAL_STATUS_USED, 1);
NavigateToDestURL();
}
// Checks that popups on a prerendered page cause cancellation.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPopup) {
PrerenderTestURL("prerender_popup.html",
FINAL_STATUS_CREATE_NEW_WINDOW, 1);
}
// Test that page-based redirects to https will cancel prerenders.
// Flaky, http://crbug.com/73580
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, FLAKY_PrerenderRedirectToHttps) {
net::TestServer https_server(net::TestServer::TYPE_HTTPS,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
std::string redirect_path;
ASSERT_TRUE(CreateRedirect(https_url.spec(), &redirect_path));
PrerenderTestURL(redirect_path,
FINAL_STATUS_HTTPS,
2);
}
// Checks that renderers using excessive memory will be terminated.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExcessiveMemory) {
PrerenderTestURL("prerender_excessive_memory.html",
FINAL_STATUS_MEMORY_LIMIT_EXCEEDED, 1);
}
} // namespace prerender
|