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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
// 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 "base/string_util.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/task_manager/task_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.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 "grit/generated_resources.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "ui/base/l10n/l10n_util.h"
#include <queue>
// 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()) <<
" when testing URL " << prerender_url().path();
// 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) {
PushExpectedFinalStatus(expected_final_status);
}
void PushExpectedFinalStatus(FinalStatus expected_final_status) {
expected_final_status_queue_.push(expected_final_status);
}
virtual PrerenderContents* CreatePrerenderContents(
PrerenderManager* prerender_manager, Profile* profile, const GURL& url,
const std::vector<GURL>& alias_urls, const GURL& referrer) {
CHECK(!expected_final_status_queue_.empty()) <<
"Creating prerender contents for " << url.path() <<
"with no expected final status";
FinalStatus expected_final_status = expected_final_status_queue_.front();
expected_final_status_queue_.pop();
return new TestPrerenderContents(prerender_manager, profile, url,
alias_urls, referrer,
expected_final_status);
}
private:
std::queue<FinalStatus> expected_final_status_queue_;
};
} // namespace
class PrerenderBrowserTest : public InProcessBrowserTest {
public:
PrerenderBrowserTest()
: prc_factory_(NULL),
use_https_src_server_(false),
on_iteration_succeeded_(true) {
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());
dest_url_ = UrlForHtmlFile(html_file);
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);
// This is needed to exit the event loop once the prerendered page has
// stopped loading or was cancelled.
ASSERT_TRUE(prerender_manager());
prerender_manager()->rate_limit_enabled_ = false;
ASSERT_TRUE(prc_factory_ == NULL);
prc_factory_ =
new WaitForLoadPrerenderContentsFactory(expected_final_status);
prerender_manager()->SetPrerenderContentsFactory(prc_factory_);
// 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;
EXPECT_TRUE(on_iteration_succeeded_);
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);
MessageLoopForUI::current()->PostTask(
FROM_HERE,
NewRunnableMethod(this,
&PrerenderBrowserTest::CallOnIteration,
prerender_contents->render_view_host()));
}
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_);
// 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);
}
bool UrlIsInPrerenderManager(const std::string& html_file) {
GURL dest_url = UrlForHtmlFile(html_file);
return (prerender_manager()->FindEntry(dest_url) != NULL);
}
bool UrlIsPendingInPrerenderManager(const std::string& html_file) {
GURL dest_url = UrlForHtmlFile(html_file);
return (prerender_manager()->FindPendingEntry(dest_url) != NULL);
}
void set_use_https_src(bool use_https_src_server) {
use_https_src_server_ = use_https_src_server;
}
void PushExpectedFinalStatus(FinalStatus expected_final_status) {
DCHECK(prerender_manager()->prerender_contents_factory_.get() ==
prc_factory_);
prc_factory_->PushExpectedFinalStatus(expected_final_status);
}
TaskManagerModel* model() const {
return TaskManager::GetInstance()->model();
}
private:
PrerenderManager* prerender_manager() const {
Profile* profile = browser()->GetSelectedTabContents()->profile();
PrerenderManager* prerender_manager = profile->GetPrerenderManager();
return prerender_manager;
}
// Non-const as test_server()->GetURL() is not const
GURL UrlForHtmlFile(const std::string& html_file) {
std::string dest_path = "files/prerender/";
dest_path.append(html_file);
return test_server()->GetURL(dest_path);
}
void CallOnIteration(RenderViewHost* rvh) {
on_iteration_succeeded_ = ui_test_utils::ExecuteJavaScript(
rvh,
L"",
L"if (typeof(OnIteration) != 'undefined') {OnIteration();}");
}
WaitForLoadPrerenderContentsFactory* prc_factory_;
GURL dest_url_;
bool use_https_src_server_;
bool on_iteration_succeeded_;
};
// 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) {
std::string redirect_path;
ASSERT_TRUE(CreateRedirect("../download-test1.lib", &redirect_path));
PrerenderTestURL(redirect_path, 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.
// Disabled, http://crbug.com/73580
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
DISABLED_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.
// Disabled, http://crbug.com/77870.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
DISABLED_PrerenderExcessiveMemory) {
PrerenderTestURL("prerender_excessive_memory.html",
FINAL_STATUS_MEMORY_LIMIT_EXCEEDED, 1);
}
// Checks that we don't prerender in an infinite loop.
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderInfiniteLoop) {
const char* const kHtmlFileA = "prerender_infinite_a.html";
const char* const kHtmlFileB = "prerender_infinite_b.html";
PrerenderTestURL(kHtmlFileA, FINAL_STATUS_USED, 1);
// Next url should be in pending list but not an active entry.
EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
EXPECT_TRUE(UrlIsPendingInPrerenderManager(kHtmlFileB));
// We are not going to navigate back to kHtmlFileA but we will start the
// preload so we need to set the final status to expect here before
// navigating.
PushExpectedFinalStatus(FINAL_STATUS_APP_TERMINATING);
NavigateToDestURL();
// Make sure the PrerenderContents for the next url is now in the manager
// and not pending.
EXPECT_TRUE(UrlIsInPrerenderManager(kHtmlFileB));
EXPECT_FALSE(UrlIsPendingInPrerenderManager(kHtmlFileB));
}
// Checks that we don't prerender in an infinite loop and multiple links are
// handled correctly.
// Flaky, http://crbug.com/77323
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, FLAKY_PrerenderInfiniteLoopMultiple) {
const char* const kHtmlFileA = "prerender_infinite_a_multiple.html";
const char* const kHtmlFileB = "prerender_infinite_b_multiple.html";
const char* const kHtmlFileC = "prerender_infinite_c_multiple.html";
PrerenderTestURL(kHtmlFileA, FINAL_STATUS_USED, 1);
// Next url should be in pending list but not an active entry.
EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileC));
EXPECT_TRUE(UrlIsPendingInPrerenderManager(kHtmlFileB));
EXPECT_TRUE(UrlIsPendingInPrerenderManager(kHtmlFileC));
// We are not going to navigate back to kHtmlFileA but we will start the
// preload so we need to set the final status to expect here before
// navigating. We set them on a queue so whichever we see first is expected to
// be evicted, and the second should stick around until we exit.
PushExpectedFinalStatus(FINAL_STATUS_EVICTED);
PushExpectedFinalStatus(FINAL_STATUS_APP_TERMINATING);
NavigateToDestURL();
// Make sure the PrerenderContents for the next urls are now in the manager
// and not pending. One and only one of the URLs (the last seen) should be the
// active entry.
bool url_b_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileB);
bool url_c_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileC);
EXPECT_TRUE((url_b_is_active_prerender || url_c_is_active_prerender) &&
!(url_b_is_active_prerender && url_c_is_active_prerender));
EXPECT_FALSE(UrlIsPendingInPrerenderManager(kHtmlFileB));
EXPECT_FALSE(UrlIsPendingInPrerenderManager(kHtmlFileC));
}
IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, TaskManager) {
// Show the task manager. This populates the model.
browser()->window()->ShowTaskManager();
// Start with two resources.
EXPECT_EQ(2, model()->ResourceCount());
PrerenderTestURL("prerender_page.html", FINAL_STATUS_USED, 1);
// The prerender makes three.
EXPECT_EQ(3, model()->ResourceCount());
// It shouldn't have a TabContents associated with it.
ASSERT_TRUE(model()->GetResourceTabContents(1) == NULL);
// The prefix should be "Prerender:"
string16 prefix =
l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
string16());
ASSERT_TRUE(StartsWith(model()->GetResourceTitle(1), prefix, true));
NavigateToDestURL();
// Prerender task should be killed and removed from the Task Manager.
EXPECT_EQ(2, model()->ResourceCount());
}
} // namespace prerender
|