Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_browsertest.cc
blob8f68a2dc039c40e30e4cfabba1c170aea5e9e8f2
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <deque>
6 #include <vector>
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/path_service.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/test/histogram_tester.h"
21 #include "base/test/test_timeouts.h"
22 #include "base/values.h"
23 #include "chrome/browser/browsing_data/browsing_data_helper.h"
24 #include "chrome/browser/browsing_data/browsing_data_remover.h"
25 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
26 #include "chrome/browser/chrome_content_browser_client.h"
27 #include "chrome/browser/chrome_notification_types.h"
28 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
29 #include "chrome/browser/extensions/extension_apitest.h"
30 #include "chrome/browser/external_protocol/external_protocol_handler.h"
31 #include "chrome/browser/favicon/favicon_tab_helper.h"
32 #include "chrome/browser/net/prediction_options.h"
33 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
34 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
35 #include "chrome/browser/prerender/prerender_contents.h"
36 #include "chrome/browser/prerender/prerender_field_trial.h"
37 #include "chrome/browser/prerender/prerender_handle.h"
38 #include "chrome/browser/prerender/prerender_link_manager.h"
39 #include "chrome/browser/prerender/prerender_link_manager_factory.h"
40 #include "chrome/browser/prerender/prerender_manager.h"
41 #include "chrome/browser/prerender/prerender_manager_factory.h"
42 #include "chrome/browser/profiles/profile.h"
43 #include "chrome/browser/profiles/profile_io_data.h"
44 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
45 #include "chrome/browser/safe_browsing/database_manager.h"
46 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
47 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
48 #include "chrome/browser/task_manager/task_manager.h"
49 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
50 #include "chrome/browser/ui/browser.h"
51 #include "chrome/browser/ui/browser_commands.h"
52 #include "chrome/browser/ui/browser_finder.h"
53 #include "chrome/browser/ui/browser_navigator.h"
54 #include "chrome/browser/ui/browser_window.h"
55 #include "chrome/browser/ui/location_bar/location_bar.h"
56 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
57 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
58 #include "chrome/browser/ui/omnibox/omnibox_view.h"
59 #include "chrome/browser/ui/tabs/tab_strip_model.h"
60 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
61 #include "chrome/common/chrome_paths.h"
62 #include "chrome/common/chrome_switches.h"
63 #include "chrome/common/pref_names.h"
64 #include "chrome/grit/generated_resources.h"
65 #include "chrome/test/base/in_process_browser_test.h"
66 #include "chrome/test/base/test_switches.h"
67 #include "chrome/test/base/ui_test_utils.h"
68 #include "components/content_settings/core/browser/host_content_settings_map.h"
69 #include "components/variations/entropy_provider.h"
70 #include "components/variations/variations_associated_data.h"
71 #include "content/public/browser/browser_message_filter.h"
72 #include "content/public/browser/devtools_agent_host.h"
73 #include "content/public/browser/navigation_controller.h"
74 #include "content/public/browser/navigation_entry.h"
75 #include "content/public/browser/notification_service.h"
76 #include "content/public/browser/render_frame_host.h"
77 #include "content/public/browser/render_process_host.h"
78 #include "content/public/browser/render_view_host.h"
79 #include "content/public/browser/site_instance.h"
80 #include "content/public/browser/web_contents.h"
81 #include "content/public/browser/web_contents_observer.h"
82 #include "content/public/common/url_constants.h"
83 #include "content/public/test/browser_test_utils.h"
84 #include "content/public/test/test_navigation_observer.h"
85 #include "content/public/test/test_utils.h"
86 #include "extensions/common/constants.h"
87 #include "extensions/common/extension_urls.h"
88 #include "extensions/common/manifest_handlers/mime_types_handler.h"
89 #include "extensions/common/switches.h"
90 #include "extensions/test/result_catcher.h"
91 #include "net/base/escape.h"
92 #include "net/cert/x509_certificate.h"
93 #include "net/dns/mock_host_resolver.h"
94 #include "net/ssl/client_cert_store.h"
95 #include "net/ssl/ssl_cert_request_info.h"
96 #include "net/test/url_request/url_request_mock_http_job.h"
97 #include "net/url_request/url_request_context.h"
98 #include "net/url_request/url_request_context_getter.h"
99 #include "net/url_request/url_request_filter.h"
100 #include "net/url_request/url_request_interceptor.h"
101 #include "net/url_request/url_request_job.h"
102 #include "ui/base/l10n/l10n_util.h"
103 #include "url/gurl.h"
105 using chrome_browser_net::NetworkPredictionOptions;
106 using content::BrowserThread;
107 using content::DevToolsAgentHost;
108 using content::NavigationController;
109 using content::OpenURLParams;
110 using content::Referrer;
111 using content::RenderFrameHost;
112 using content::RenderViewHost;
113 using content::RenderWidgetHost;
114 using content::TestNavigationObserver;
115 using content::WebContents;
116 using content::WebContentsObserver;
117 using net::NetworkChangeNotifier;
118 using task_manager::browsertest_util::WaitForTaskManagerRows;
120 // Prerender tests work as follows:
122 // A page with a prefetch link to the test page is loaded. Once prerendered,
123 // its Javascript function DidPrerenderPass() is called, which returns true if
124 // the page behaves as expected when prerendered.
126 // The prerendered page is then displayed on a tab. The Javascript function
127 // DidDisplayPass() is called, and returns true if the page behaved as it
128 // should while being displayed.
130 namespace prerender {
132 namespace {
134 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
135 public:
136 ConnectionType GetCurrentConnectionType() const override {
137 return NetworkChangeNotifier::CONNECTION_WIFI;
141 class MockNetworkChangeNotifier4G : public NetworkChangeNotifier {
142 public:
143 ConnectionType GetCurrentConnectionType() const override {
144 return NetworkChangeNotifier::CONNECTION_4G;
148 // Constants used in the test HTML files.
149 const char* kReadyTitle = "READY";
150 const char* kPassTitle = "PASS";
152 std::string CreateClientRedirect(const std::string& dest_url) {
153 const char* const kClientRedirectBase = "client-redirect?";
154 return kClientRedirectBase + net::EscapeQueryParamValue(dest_url, false);
157 std::string CreateServerRedirect(const std::string& dest_url) {
158 const char* const kServerRedirectBase = "server-redirect?";
159 return kServerRedirectBase + net::EscapeQueryParamValue(dest_url, false);
162 // Clears the specified data using BrowsingDataRemover.
163 void ClearBrowsingData(Browser* browser, int remove_mask) {
164 BrowsingDataRemover* remover =
165 BrowsingDataRemover::CreateForUnboundedRange(browser->profile());
166 BrowsingDataRemoverCompletionObserver observer(remover);
167 remover->Remove(remove_mask, BrowsingDataHelper::UNPROTECTED_WEB);
168 observer.BlockUntilCompletion();
169 // BrowsingDataRemover deletes itself.
172 // Returns true if the prerender is expected to abort on its own, before
173 // attempting to swap it.
174 bool ShouldAbortPrerenderBeforeSwap(FinalStatus status) {
175 switch (status) {
176 case FINAL_STATUS_USED:
177 case FINAL_STATUS_WINDOW_OPENER:
178 case FINAL_STATUS_APP_TERMINATING:
179 case FINAL_STATUS_PROFILE_DESTROYED:
180 case FINAL_STATUS_CACHE_OR_HISTORY_CLEARED:
181 // We'll crash the renderer after it's loaded.
182 case FINAL_STATUS_RENDERER_CRASHED:
183 case FINAL_STATUS_CANCELLED:
184 case FINAL_STATUS_DEVTOOLS_ATTACHED:
185 case FINAL_STATUS_PAGE_BEING_CAPTURED:
186 case FINAL_STATUS_NAVIGATION_UNCOMMITTED:
187 case FINAL_STATUS_WOULD_HAVE_BEEN_USED:
188 case FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE:
189 return false;
190 default:
191 return true;
195 // Convenience function to wait for a title. Handles the case when the
196 // WebContents already has the expected title.
197 void WaitForASCIITitle(WebContents* web_contents,
198 const char* expected_title_ascii) {
199 base::string16 expected_title = base::ASCIIToUTF16(expected_title_ascii);
200 if (web_contents->GetTitle() == expected_title)
201 return;
202 content::TitleWatcher title_watcher(web_contents, expected_title);
203 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
206 // Waits for the destruction of a RenderProcessHost's IPC channel.
207 // Used to make sure the PrerenderLinkManager's OnChannelClosed function has
208 // been called, before checking its state.
209 class ChannelDestructionWatcher {
210 public:
211 ChannelDestructionWatcher() : channel_destroyed_(false) {
214 ~ChannelDestructionWatcher() {
217 void WatchChannel(content::RenderProcessHost* host) {
218 host->AddFilter(new DestructionMessageFilter(this));
221 void WaitForChannelClose() {
222 run_loop_.Run();
223 EXPECT_TRUE(channel_destroyed_);
226 private:
227 // When destroyed, calls ChannelDestructionWatcher::OnChannelDestroyed.
228 // Ignores all messages.
229 class DestructionMessageFilter : public content::BrowserMessageFilter {
230 public:
231 explicit DestructionMessageFilter(ChannelDestructionWatcher* watcher)
232 : BrowserMessageFilter(0),
233 watcher_(watcher) {
236 private:
237 ~DestructionMessageFilter() override {
238 content::BrowserThread::PostTask(
239 content::BrowserThread::UI, FROM_HERE,
240 base::Bind(&ChannelDestructionWatcher::OnChannelDestroyed,
241 base::Unretained(watcher_)));
244 bool OnMessageReceived(const IPC::Message& message) override {
245 return false;
248 ChannelDestructionWatcher* watcher_;
250 DISALLOW_COPY_AND_ASSIGN(DestructionMessageFilter);
253 void OnChannelDestroyed() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
256 EXPECT_FALSE(channel_destroyed_);
257 channel_destroyed_ = true;
258 run_loop_.Quit();
261 bool channel_destroyed_;
262 base::RunLoop run_loop_;
264 DISALLOW_COPY_AND_ASSIGN(ChannelDestructionWatcher);
267 // A navigation observer to wait on either a new load or a swap of a
268 // WebContents. On swap, if the new WebContents is still loading, wait for that
269 // load to complete as well. Note that the load must begin after the observer is
270 // attached.
271 class NavigationOrSwapObserver : public WebContentsObserver,
272 public TabStripModelObserver {
273 public:
274 // Waits for either a new load or a swap of |tab_strip_model|'s active
275 // WebContents.
276 NavigationOrSwapObserver(TabStripModel* tab_strip_model,
277 WebContents* web_contents)
278 : WebContentsObserver(web_contents),
279 tab_strip_model_(tab_strip_model),
280 did_start_loading_(false),
281 number_of_loads_(1) {
282 CHECK_NE(TabStripModel::kNoTab,
283 tab_strip_model->GetIndexOfWebContents(web_contents));
284 tab_strip_model_->AddObserver(this);
287 // Waits for either |number_of_loads| loads or a swap of |tab_strip_model|'s
288 // active WebContents.
289 NavigationOrSwapObserver(TabStripModel* tab_strip_model,
290 WebContents* web_contents,
291 int number_of_loads)
292 : WebContentsObserver(web_contents),
293 tab_strip_model_(tab_strip_model),
294 did_start_loading_(false),
295 number_of_loads_(number_of_loads) {
296 CHECK_NE(TabStripModel::kNoTab,
297 tab_strip_model->GetIndexOfWebContents(web_contents));
298 tab_strip_model_->AddObserver(this);
301 ~NavigationOrSwapObserver() override {
302 tab_strip_model_->RemoveObserver(this);
305 void set_did_start_loading() {
306 did_start_loading_ = true;
309 void Wait() {
310 loop_.Run();
313 // WebContentsObserver implementation:
314 void DidStartLoading() override { did_start_loading_ = true; }
315 void DidStopLoading() override {
316 if (!did_start_loading_)
317 return;
318 number_of_loads_--;
319 if (number_of_loads_ == 0)
320 loop_.Quit();
323 // TabStripModelObserver implementation:
324 void TabReplacedAt(TabStripModel* tab_strip_model,
325 WebContents* old_contents,
326 WebContents* new_contents,
327 int index) override {
328 if (old_contents != web_contents())
329 return;
330 // Switch to observing the new WebContents.
331 Observe(new_contents);
332 if (new_contents->IsLoading()) {
333 // If the new WebContents is still loading, wait for it to complete. Only
334 // one load post-swap is supported.
335 did_start_loading_ = true;
336 number_of_loads_ = 1;
337 } else {
338 loop_.Quit();
342 private:
343 TabStripModel* tab_strip_model_;
344 bool did_start_loading_;
345 int number_of_loads_;
346 base::RunLoop loop_;
349 // Waits for a new tab to open and a navigation or swap in it.
350 class NewTabNavigationOrSwapObserver {
351 public:
352 NewTabNavigationOrSwapObserver()
353 : new_tab_observer_(
354 chrome::NOTIFICATION_TAB_ADDED,
355 base::Bind(&NewTabNavigationOrSwapObserver::OnTabAdded,
356 base::Unretained(this))) {
357 // Watch for NOTIFICATION_TAB_ADDED. Add a callback so that the
358 // NavigationOrSwapObserver can be attached synchronously and no events are
359 // missed.
362 void Wait() {
363 new_tab_observer_.Wait();
364 swap_observer_->Wait();
367 bool OnTabAdded(const content::NotificationSource& source,
368 const content::NotificationDetails& details) {
369 if (swap_observer_)
370 return true;
371 WebContents* new_tab = content::Details<WebContents>(details).ptr();
372 // Get the TabStripModel. Assume this is attached to a Browser.
373 TabStripModel* tab_strip_model =
374 static_cast<Browser*>(new_tab->GetDelegate())->tab_strip_model();
375 swap_observer_.reset(new NavigationOrSwapObserver(tab_strip_model,
376 new_tab));
377 swap_observer_->set_did_start_loading();
378 return true;
381 private:
382 content::WindowedNotificationObserver new_tab_observer_;
383 scoped_ptr<NavigationOrSwapObserver> swap_observer_;
386 // PrerenderContents that stops the UI message loop on DidStopLoading().
387 class TestPrerenderContents : public PrerenderContents {
388 public:
389 TestPrerenderContents(
390 PrerenderManager* prerender_manager,
391 Profile* profile,
392 const GURL& url,
393 const content::Referrer& referrer,
394 Origin origin,
395 FinalStatus expected_final_status)
396 : PrerenderContents(prerender_manager, profile, url,
397 referrer, origin, PrerenderManager::kNoExperiment),
398 expected_final_status_(expected_final_status),
399 new_render_view_host_(NULL),
400 was_hidden_(false),
401 was_shown_(false),
402 should_be_shown_(expected_final_status == FINAL_STATUS_USED),
403 skip_final_checks_(false) {
406 ~TestPrerenderContents() override {
407 if (skip_final_checks_)
408 return;
410 if (expected_final_status_ == FINAL_STATUS_MAX) {
411 EXPECT_EQ(MATCH_COMPLETE_REPLACEMENT, match_complete_status());
412 } else {
413 EXPECT_EQ(expected_final_status_, final_status()) <<
414 " when testing URL " << prerender_url().path() <<
415 " (Expected: " << NameFromFinalStatus(expected_final_status_) <<
416 ", Actual: " << NameFromFinalStatus(final_status()) << ")";
418 // Prerendering RenderViewHosts should be hidden before the first
419 // navigation, so this should be happen for every PrerenderContents for
420 // which a RenderViewHost is created, regardless of whether or not it's
421 // used.
422 if (new_render_view_host_)
423 EXPECT_TRUE(was_hidden_);
425 // A used PrerenderContents will only be destroyed when we swap out
426 // WebContents, at the end of a navigation caused by a call to
427 // NavigateToURLImpl().
428 if (final_status() == FINAL_STATUS_USED)
429 EXPECT_TRUE(new_render_view_host_);
431 EXPECT_EQ(should_be_shown_, was_shown_);
434 void RenderProcessGone(base::TerminationStatus status) override {
435 // On quit, it's possible to end up here when render processes are closed
436 // before the PrerenderManager is destroyed. As a result, it's possible to
437 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
438 // on quit.
440 // It's also possible for this to be called after we've been notified of
441 // app termination, but before we've been deleted, which is why the second
442 // check is needed.
443 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING &&
444 final_status() != expected_final_status_) {
445 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED;
448 PrerenderContents::RenderProcessGone(status);
451 bool CheckURL(const GURL& url) override {
452 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
453 // the PrerenderRendererCrash test.
454 if (url.spec() != content::kChromeUICrashURL)
455 return PrerenderContents::CheckURL(url);
456 return true;
459 // For tests that open the prerender in a new background tab, the RenderView
460 // will not have been made visible when the PrerenderContents is destroyed
461 // even though it is used.
462 void set_should_be_shown(bool value) { should_be_shown_ = value; }
464 // For tests which do not know whether the prerender will be used.
465 void set_skip_final_checks(bool value) { skip_final_checks_ = value; }
467 FinalStatus expected_final_status() const { return expected_final_status_; }
469 private:
470 void OnRenderViewHostCreated(RenderViewHost* new_render_view_host) override {
471 // Used to make sure the RenderViewHost is hidden and, if used,
472 // subsequently shown.
473 notification_registrar().Add(
474 this,
475 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
476 content::Source<RenderWidgetHost>(new_render_view_host));
478 new_render_view_host_ = new_render_view_host;
480 PrerenderContents::OnRenderViewHostCreated(new_render_view_host);
483 void Observe(int type,
484 const content::NotificationSource& source,
485 const content::NotificationDetails& details) override {
486 if (type ==
487 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED) {
488 EXPECT_EQ(new_render_view_host_,
489 content::Source<RenderWidgetHost>(source).ptr());
490 bool is_visible = *content::Details<bool>(details).ptr();
492 if (!is_visible) {
493 was_hidden_ = true;
494 } else if (is_visible && was_hidden_) {
495 // Once hidden, a prerendered RenderViewHost should only be shown after
496 // being removed from the PrerenderContents for display.
497 EXPECT_FALSE(GetRenderViewHost());
498 was_shown_ = true;
500 return;
502 PrerenderContents::Observe(type, source, details);
505 FinalStatus expected_final_status_;
507 // The RenderViewHost created for the prerender, if any.
508 RenderViewHost* new_render_view_host_;
509 // Set to true when the prerendering RenderWidget is hidden.
510 bool was_hidden_;
511 // Set to true when the prerendering RenderWidget is shown, after having been
512 // hidden.
513 bool was_shown_;
514 // Expected final value of was_shown_. Defaults to true for
515 // FINAL_STATUS_USED, and false otherwise.
516 bool should_be_shown_;
517 // If true, |expected_final_status_| and other shutdown checks are skipped.
518 bool skip_final_checks_;
521 // A handle to a TestPrerenderContents whose lifetime is under the caller's
522 // control. A PrerenderContents may be destroyed at any point. This allows
523 // tracking the final status, etc.
524 class TestPrerender : public PrerenderContents::Observer,
525 public base::SupportsWeakPtr<TestPrerender> {
526 public:
527 TestPrerender()
528 : contents_(NULL),
529 number_of_loads_(0),
530 expected_number_of_loads_(0) {
532 ~TestPrerender() override {
533 if (contents_)
534 contents_->RemoveObserver(this);
537 TestPrerenderContents* contents() const { return contents_; }
538 int number_of_loads() const { return number_of_loads_; }
540 void WaitForCreate() { create_loop_.Run(); }
541 void WaitForStart() { start_loop_.Run(); }
542 void WaitForStop() { stop_loop_.Run(); }
544 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR
545 // for the prerender to stop running (just to avoid a timeout if the prerender
546 // dies). Note: this does not assert equality on the number of loads; the
547 // caller must do it instead.
548 void WaitForLoads(int expected_number_of_loads) {
549 DCHECK(!load_waiter_);
550 DCHECK(!expected_number_of_loads_);
551 if (number_of_loads_ < expected_number_of_loads) {
552 load_waiter_.reset(new base::RunLoop);
553 expected_number_of_loads_ = expected_number_of_loads;
554 load_waiter_->Run();
555 load_waiter_.reset();
556 expected_number_of_loads_ = 0;
558 EXPECT_LE(expected_number_of_loads, number_of_loads_);
561 void OnPrerenderCreated(TestPrerenderContents* contents) {
562 DCHECK(!contents_);
563 contents_ = contents;
564 contents_->AddObserver(this);
565 create_loop_.Quit();
568 // PrerenderContents::Observer implementation:
569 void OnPrerenderStart(PrerenderContents* contents) override {
570 start_loop_.Quit();
573 void OnPrerenderStopLoading(PrerenderContents* contents) override {
574 number_of_loads_++;
575 if (load_waiter_ && number_of_loads_ >= expected_number_of_loads_)
576 load_waiter_->Quit();
579 void OnPrerenderStop(PrerenderContents* contents) override {
580 DCHECK(contents_);
581 contents_ = NULL;
582 stop_loop_.Quit();
583 // If there is a WaitForLoads call and it has yet to see the expected number
584 // of loads, stop the loop so the test fails instead of timing out.
585 if (load_waiter_)
586 load_waiter_->Quit();
589 void OnPrerenderCreatedMatchCompleteReplacement(
590 PrerenderContents* contents,
591 PrerenderContents* replacement) override {}
593 private:
594 TestPrerenderContents* contents_;
595 int number_of_loads_;
597 int expected_number_of_loads_;
598 scoped_ptr<base::RunLoop> load_waiter_;
600 base::RunLoop create_loop_;
601 base::RunLoop start_loop_;
602 base::RunLoop stop_loop_;
604 DISALLOW_COPY_AND_ASSIGN(TestPrerender);
607 // PrerenderManager that uses TestPrerenderContents.
608 class TestPrerenderContentsFactory : public PrerenderContents::Factory {
609 public:
610 TestPrerenderContentsFactory() {}
612 ~TestPrerenderContentsFactory() override {
613 EXPECT_TRUE(expected_contents_queue_.empty());
616 scoped_ptr<TestPrerender> ExpectPrerenderContents(FinalStatus final_status) {
617 scoped_ptr<TestPrerender> handle(new TestPrerender());
618 expected_contents_queue_.push_back(
619 ExpectedContents(final_status, handle->AsWeakPtr()));
620 return handle.Pass();
623 PrerenderContents* CreatePrerenderContents(
624 PrerenderManager* prerender_manager,
625 Profile* profile,
626 const GURL& url,
627 const content::Referrer& referrer,
628 Origin origin,
629 uint8 experiment_id) override {
630 ExpectedContents expected;
631 if (!expected_contents_queue_.empty()) {
632 expected = expected_contents_queue_.front();
633 expected_contents_queue_.pop_front();
635 VLOG(1) << "Creating prerender contents for " << url.path() <<
636 " with expected final status " << expected.final_status;
637 VLOG(1) << expected_contents_queue_.size() << " left in the queue.";
638 TestPrerenderContents* contents =
639 new TestPrerenderContents(prerender_manager,
640 profile, url, referrer, origin,
641 expected.final_status);
642 if (expected.handle)
643 expected.handle->OnPrerenderCreated(contents);
644 return contents;
647 private:
648 struct ExpectedContents {
649 ExpectedContents() : final_status(FINAL_STATUS_MAX) { }
650 ExpectedContents(FinalStatus final_status,
651 const base::WeakPtr<TestPrerender>& handle)
652 : final_status(final_status),
653 handle(handle) {
656 FinalStatus final_status;
657 base::WeakPtr<TestPrerender> handle;
660 std::deque<ExpectedContents> expected_contents_queue_;
663 #if defined(FULL_SAFE_BROWSING)
664 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
665 // a given URL.
666 class FakeSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager {
667 public:
668 explicit FakeSafeBrowsingDatabaseManager(SafeBrowsingService* service)
669 : SafeBrowsingDatabaseManager(service),
670 threat_type_(SB_THREAT_TYPE_SAFE) { }
672 // Called on the IO thread to check if the given url is safe or not. If we
673 // can synchronously determine that the url is safe, CheckUrl returns true.
674 // Otherwise it returns false, and "client" is called asynchronously with the
675 // result when it is ready.
676 // Returns true, indicating a SAFE result, unless the URL is the fixed URL
677 // specified by the user, and the user-specified result is not SAFE
678 // (in which that result will be communicated back via a call into the
679 // client, and false will be returned).
680 // Overrides SafeBrowsingService::CheckBrowseUrl.
681 bool CheckBrowseUrl(const GURL& gurl, Client* client) override {
682 if (gurl != url_ || threat_type_ == SB_THREAT_TYPE_SAFE)
683 return true;
685 BrowserThread::PostTask(
686 BrowserThread::IO, FROM_HERE,
687 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone,
688 this, gurl, client));
689 return false;
692 void SetThreatTypeForUrl(const GURL& url, SBThreatType threat_type) {
693 url_ = url;
694 threat_type_ = threat_type;
697 private:
698 ~FakeSafeBrowsingDatabaseManager() override {}
700 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) {
701 std::vector<SBThreatType> expected_threats;
702 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
703 expected_threats.push_back(SB_THREAT_TYPE_URL_PHISHING);
704 SafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check(
705 std::vector<GURL>(1, gurl),
706 std::vector<SBFullHash>(),
707 client,
708 safe_browsing_util::MALWARE,
709 expected_threats);
710 sb_check.url_results[0] = threat_type_;
711 client->OnSafeBrowsingResult(sb_check);
714 GURL url_;
715 SBThreatType threat_type_;
716 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
719 class FakeSafeBrowsingService : public SafeBrowsingService {
720 public:
721 FakeSafeBrowsingService() { }
723 // Returned pointer has the same lifespan as the database_manager_ refcounted
724 // object.
725 FakeSafeBrowsingDatabaseManager* fake_database_manager() {
726 return fake_database_manager_;
729 protected:
730 ~FakeSafeBrowsingService() override {}
732 SafeBrowsingDatabaseManager* CreateDatabaseManager() override {
733 fake_database_manager_ = new FakeSafeBrowsingDatabaseManager(this);
734 return fake_database_manager_;
737 private:
738 FakeSafeBrowsingDatabaseManager* fake_database_manager_;
740 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService);
743 // Factory that creates FakeSafeBrowsingService instances.
744 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
745 public:
746 TestSafeBrowsingServiceFactory() :
747 most_recent_service_(NULL) { }
748 ~TestSafeBrowsingServiceFactory() override {}
750 SafeBrowsingService* CreateSafeBrowsingService() override {
751 most_recent_service_ = new FakeSafeBrowsingService();
752 return most_recent_service_;
755 FakeSafeBrowsingService* most_recent_service() const {
756 return most_recent_service_;
759 private:
760 FakeSafeBrowsingService* most_recent_service_;
762 #endif
764 class FakeDevToolsClient : public content::DevToolsAgentHostClient {
765 public:
766 FakeDevToolsClient() {}
767 ~FakeDevToolsClient() override {}
768 void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
769 const std::string& message) override {}
770 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {}
773 class RestorePrerenderMode {
774 public:
775 RestorePrerenderMode() : prev_mode_(PrerenderManager::GetMode()) {
778 ~RestorePrerenderMode() { PrerenderManager::SetMode(prev_mode_); }
779 private:
780 PrerenderManager::PrerenderManagerMode prev_mode_;
783 // URLRequestJob (and associated handler) which hangs.
784 class HangingURLRequestJob : public net::URLRequestJob {
785 public:
786 HangingURLRequestJob(net::URLRequest* request,
787 net::NetworkDelegate* network_delegate)
788 : net::URLRequestJob(request, network_delegate) {
791 void Start() override {}
793 private:
794 ~HangingURLRequestJob() override {}
797 class HangingFirstRequestInterceptor : public net::URLRequestInterceptor {
798 public:
799 HangingFirstRequestInterceptor(const base::FilePath& file,
800 base::Closure callback)
801 : file_(file),
802 callback_(callback),
803 first_run_(true) {
805 ~HangingFirstRequestInterceptor() override {}
807 net::URLRequestJob* MaybeInterceptRequest(
808 net::URLRequest* request,
809 net::NetworkDelegate* network_delegate) const override {
810 if (first_run_) {
811 first_run_ = false;
812 if (!callback_.is_null()) {
813 BrowserThread::PostTask(
814 BrowserThread::UI, FROM_HERE, callback_);
816 return new HangingURLRequestJob(request, network_delegate);
818 return new net::URLRequestMockHTTPJob(
819 request,
820 network_delegate,
821 file_,
822 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
823 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
826 private:
827 base::FilePath file_;
828 base::Closure callback_;
829 mutable bool first_run_;
832 // Makes |url| never respond on the first load, and then with the contents of
833 // |file| afterwards. When the first load has been scheduled, runs |callback| on
834 // the UI thread.
835 void CreateHangingFirstRequestInterceptorOnIO(
836 const GURL& url, const base::FilePath& file, base::Closure callback) {
837 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
838 scoped_ptr<net::URLRequestInterceptor> never_respond_handler(
839 new HangingFirstRequestInterceptor(file, callback));
840 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
841 url, never_respond_handler.Pass());
844 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks.
845 class MockHTTPJob : public net::URLRequestMockHTTPJob {
846 public:
847 MockHTTPJob(net::URLRequest* request,
848 net::NetworkDelegate* delegate,
849 const base::FilePath& file)
850 : net::URLRequestMockHTTPJob(
851 request,
852 delegate,
853 file,
854 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
855 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
857 void set_start_callback(const base::Closure& start_callback) {
858 start_callback_ = start_callback;
861 void Start() override {
862 if (!start_callback_.is_null())
863 start_callback_.Run();
864 net::URLRequestMockHTTPJob::Start();
867 private:
868 ~MockHTTPJob() override {}
870 base::Closure start_callback_;
873 // Dummy counter class to live on the UI thread for counting requests.
874 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> {
875 public:
876 RequestCounter() : count_(0), expected_count_(-1) {}
877 int count() const { return count_; }
879 void RequestStarted() {
880 count_++;
881 if (loop_ && count_ == expected_count_)
882 loop_->Quit();
885 void WaitForCount(int expected_count) {
886 ASSERT_TRUE(!loop_);
887 ASSERT_EQ(-1, expected_count_);
888 if (count_ < expected_count) {
889 expected_count_ = expected_count;
890 loop_.reset(new base::RunLoop);
891 loop_->Run();
892 expected_count_ = -1;
893 loop_.reset();
896 EXPECT_EQ(expected_count, count_);
898 private:
899 int count_;
900 int expected_count_;
901 scoped_ptr<base::RunLoop> loop_;
904 // Protocol handler which counts the number of requests that start.
905 class CountingInterceptor : public net::URLRequestInterceptor {
906 public:
907 CountingInterceptor(const base::FilePath& file,
908 const base::WeakPtr<RequestCounter>& counter)
909 : file_(file),
910 counter_(counter),
911 weak_factory_(this) {
913 ~CountingInterceptor() override {}
915 net::URLRequestJob* MaybeInterceptRequest(
916 net::URLRequest* request,
917 net::NetworkDelegate* network_delegate) const override {
918 MockHTTPJob* job = new MockHTTPJob(request, network_delegate, file_);
919 job->set_start_callback(base::Bind(&CountingInterceptor::RequestStarted,
920 weak_factory_.GetWeakPtr()));
921 return job;
924 void RequestStarted() {
925 BrowserThread::PostTask(
926 BrowserThread::UI, FROM_HERE,
927 base::Bind(&RequestCounter::RequestStarted, counter_));
930 private:
931 base::FilePath file_;
932 base::WeakPtr<RequestCounter> counter_;
933 mutable base::WeakPtrFactory<CountingInterceptor> weak_factory_;
936 // Makes |url| respond to requests with the contents of |file|, counting the
937 // number that start in |counter|.
938 void CreateCountingInterceptorOnIO(
939 const GURL& url,
940 const base::FilePath& file,
941 const base::WeakPtr<RequestCounter>& counter) {
942 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
943 scoped_ptr<net::URLRequestInterceptor> request_interceptor(
944 new CountingInterceptor(file, counter));
945 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
946 url, request_interceptor.Pass());
949 // Makes |url| respond to requests with the contents of |file|.
950 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
951 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
952 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
953 url,
954 net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
955 file, BrowserThread::GetBlockingPool()));
958 // A ContentBrowserClient that cancels all prerenderers on OpenURL.
959 class TestContentBrowserClient : public chrome::ChromeContentBrowserClient {
960 public:
961 TestContentBrowserClient() {}
962 ~TestContentBrowserClient() override {}
964 // chrome::ChromeContentBrowserClient implementation.
965 bool ShouldAllowOpenURL(content::SiteInstance* site_instance,
966 const GURL& url) override {
967 PrerenderManagerFactory::GetForProfile(
968 Profile::FromBrowserContext(site_instance->GetBrowserContext()))
969 ->CancelAllPrerenders();
970 return chrome::ChromeContentBrowserClient::ShouldAllowOpenURL(site_instance,
971 url);
974 private:
975 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient);
978 // A ContentBrowserClient that forces cross-process navigations.
979 class SwapProcessesContentBrowserClient
980 : public chrome::ChromeContentBrowserClient {
981 public:
982 SwapProcessesContentBrowserClient() {}
983 ~SwapProcessesContentBrowserClient() override {}
985 // chrome::ChromeContentBrowserClient implementation.
986 bool ShouldSwapProcessesForRedirect(
987 content::ResourceContext* resource_context,
988 const GURL& current_url,
989 const GURL& new_url) override {
990 return true;
993 private:
994 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient);
997 // An ExternalProtocolHandler that blocks everything and asserts it never is
998 // called.
999 class NeverRunsExternalProtocolHandlerDelegate
1000 : public ExternalProtocolHandler::Delegate {
1001 public:
1002 // ExternalProtocolHandler::Delegate implementation.
1003 ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
1004 ShellIntegration::DefaultWebClientObserver* observer,
1005 const std::string& protocol) override {
1006 NOTREACHED();
1007 // This will crash, but it shouldn't get this far with BlockState::BLOCK
1008 // anyway.
1009 return NULL;
1011 ExternalProtocolHandler::BlockState GetBlockState(
1012 const std::string& scheme) override {
1013 // Block everything and fail the test.
1014 ADD_FAILURE();
1015 return ExternalProtocolHandler::BLOCK;
1017 void BlockRequest() override {}
1018 void RunExternalProtocolDialog(const GURL& url,
1019 int render_process_host_id,
1020 int routing_id) override {
1021 NOTREACHED();
1023 void LaunchUrlWithoutSecurityCheck(const GURL& url) override { NOTREACHED(); }
1024 void FinishedProcessingCheck() override { NOTREACHED(); }
1027 base::FilePath GetTestPath(const std::string& file_name) {
1028 return ui_test_utils::GetTestFilePath(
1029 base::FilePath(FILE_PATH_LITERAL("prerender")),
1030 base::FilePath().AppendASCII(file_name));
1033 } // namespace
1035 // Many of these tests are flaky. See http://crbug.com/249179
1036 class PrerenderBrowserTest : virtual public InProcessBrowserTest {
1037 public:
1038 PrerenderBrowserTest()
1039 : autostart_test_server_(true),
1040 prerender_contents_factory_(NULL),
1041 #if defined(FULL_SAFE_BROWSING)
1042 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
1043 #endif
1044 call_javascript_(true),
1045 check_load_events_(true),
1046 loader_path_("files/prerender/prerender_loader.html"),
1047 explicitly_set_browser_(NULL) {}
1049 ~PrerenderBrowserTest() override {}
1051 content::SessionStorageNamespace* GetSessionStorageNamespace() const {
1052 WebContents* web_contents = GetActiveWebContents();
1053 if (!web_contents)
1054 return NULL;
1055 return web_contents->GetController().GetDefaultSessionStorageNamespace();
1058 void SetUpInProcessBrowserTestFixture() override {
1059 #if defined(FULL_SAFE_BROWSING)
1060 SafeBrowsingService::RegisterFactory(safe_browsing_factory_.get());
1061 #endif
1064 void TearDownInProcessBrowserTestFixture() override {
1065 #if defined(FULL_SAFE_BROWSING)
1066 SafeBrowsingService::RegisterFactory(NULL);
1067 #endif
1070 void SetUpCommandLine(base::CommandLine* command_line) override {
1071 command_line->AppendSwitchASCII(switches::kPrerenderMode,
1072 switches::kPrerenderModeSwitchValueEnabled);
1073 #if defined(OS_MACOSX)
1074 // The plugins directory isn't read by default on the Mac, so it needs to be
1075 // explicitly registered.
1076 base::FilePath app_dir;
1077 PathService::Get(chrome::DIR_APP, &app_dir);
1078 command_line->AppendSwitchPath(
1079 switches::kExtraPluginDir,
1080 app_dir.Append(FILE_PATH_LITERAL("plugins")));
1081 #endif
1082 command_line->AppendSwitch(switches::kAlwaysAuthorizePlugins);
1083 command_line->AppendSwitch(switches::kEnableNpapi);
1086 void SetPreference(NetworkPredictionOptions value) {
1087 browser()->profile()->GetPrefs()->SetInteger(
1088 prefs::kNetworkPredictionOptions, value);
1091 // Verifies whether ShouldDisableLocalPredictorDueToPreferencesAndNetwork
1092 // produces the desired output.
1093 void TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
1094 bool preference_wifi_network_wifi,
1095 bool preference_wifi_network_4g,
1096 bool preference_always_network_wifi,
1097 bool preference_always_network_4g,
1098 bool preference_never_network_wifi,
1099 bool preference_never_network_4g) {
1100 Profile* profile = browser()->profile();
1102 // Set real NetworkChangeNotifier singleton aside.
1103 scoped_ptr<NetworkChangeNotifier::DisableForTest> disable_for_test(
1104 new NetworkChangeNotifier::DisableForTest);
1106 // Set preference to WIFI_ONLY: prefetch when not on cellular.
1107 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY);
1109 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
1110 EXPECT_EQ(
1111 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
1112 preference_wifi_network_wifi);
1115 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
1116 EXPECT_EQ(
1117 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
1118 preference_wifi_network_4g);
1121 // Set preference to ALWAYS: always prefetch.
1122 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS);
1124 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
1125 EXPECT_EQ(
1126 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
1127 preference_always_network_wifi);
1130 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
1131 EXPECT_EQ(
1132 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
1133 preference_always_network_4g);
1136 // Set preference to NEVER: never prefetch.
1137 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER);
1139 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
1140 EXPECT_EQ(
1141 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
1142 preference_never_network_wifi);
1145 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
1146 EXPECT_EQ(
1147 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile),
1148 preference_never_network_4g);
1152 void SetUpOnMainThread() override {
1153 current_browser()->profile()->GetPrefs()->SetBoolean(
1154 prefs::kPromptForDownload, false);
1155 IncreasePrerenderMemory();
1156 if (autostart_test_server_)
1157 ASSERT_TRUE(test_server()->Start());
1158 ChromeResourceDispatcherHostDelegate::
1159 SetExternalProtocolHandlerDelegateForTesting(
1160 &external_protocol_handler_delegate_);
1162 PrerenderManager* prerender_manager = GetPrerenderManager();
1163 ASSERT_TRUE(prerender_manager);
1164 prerender_manager->mutable_config().rate_limit_enabled = false;
1165 ASSERT_TRUE(prerender_contents_factory_ == NULL);
1166 prerender_contents_factory_ = new TestPrerenderContentsFactory;
1167 prerender_manager->SetPrerenderContentsFactory(prerender_contents_factory_);
1170 // Convenience function to get the currently active WebContents in
1171 // current_browser().
1172 WebContents* GetActiveWebContents() const {
1173 return current_browser()->tab_strip_model()->GetActiveWebContents();
1176 // Overload for a single expected final status
1177 scoped_ptr<TestPrerender> PrerenderTestURL(
1178 const std::string& html_file,
1179 FinalStatus expected_final_status,
1180 int expected_number_of_loads) {
1181 GURL url = test_server()->GetURL(html_file);
1182 return PrerenderTestURL(url,
1183 expected_final_status,
1184 expected_number_of_loads);
1187 ScopedVector<TestPrerender> PrerenderTestURL(
1188 const std::string& html_file,
1189 const std::vector<FinalStatus>& expected_final_status_queue,
1190 int expected_number_of_loads) {
1191 GURL url = test_server()->GetURL(html_file);
1192 return PrerenderTestURLImpl(url,
1193 expected_final_status_queue,
1194 expected_number_of_loads);
1197 scoped_ptr<TestPrerender> PrerenderTestURL(
1198 const GURL& url,
1199 FinalStatus expected_final_status,
1200 int expected_number_of_loads) {
1201 std::vector<FinalStatus> expected_final_status_queue(
1202 1, expected_final_status);
1203 std::vector<TestPrerender*> prerenders;
1204 PrerenderTestURLImpl(url,
1205 expected_final_status_queue,
1206 expected_number_of_loads).release(&prerenders);
1207 CHECK_EQ(1u, prerenders.size());
1208 return scoped_ptr<TestPrerender>(prerenders[0]);
1211 void NavigateToDestURL() const {
1212 NavigateToDestURLWithDisposition(CURRENT_TAB, true);
1215 // Opens the url in a new tab, with no opener.
1216 void NavigateToDestURLWithDisposition(
1217 WindowOpenDisposition disposition,
1218 bool expect_swap_to_succeed) const {
1219 NavigateToURLWithParams(
1220 content::OpenURLParams(dest_url_, Referrer(), disposition,
1221 ui::PAGE_TRANSITION_TYPED, false),
1222 expect_swap_to_succeed);
1225 void NavigateToURL(const std::string& dest_html_file) const {
1226 NavigateToURLWithDisposition(dest_html_file, CURRENT_TAB, true);
1229 void NavigateToURLWithDisposition(const std::string& dest_html_file,
1230 WindowOpenDisposition disposition,
1231 bool expect_swap_to_succeed) const {
1232 GURL dest_url = test_server()->GetURL(dest_html_file);
1233 NavigateToURLWithDisposition(dest_url, disposition, expect_swap_to_succeed);
1236 void NavigateToURLWithDisposition(const GURL& dest_url,
1237 WindowOpenDisposition disposition,
1238 bool expect_swap_to_succeed) const {
1239 NavigateToURLWithParams(
1240 content::OpenURLParams(dest_url, Referrer(), disposition,
1241 ui::PAGE_TRANSITION_TYPED, false),
1242 expect_swap_to_succeed);
1245 void NavigateToURLWithParams(const content::OpenURLParams& params,
1246 bool expect_swap_to_succeed) const {
1247 NavigateToURLImpl(params, expect_swap_to_succeed);
1250 void OpenDestURLViaClick() const {
1251 OpenURLViaClick(dest_url_);
1254 void OpenURLViaClick(const GURL& url) const {
1255 OpenURLWithJSImpl("Click", url, GURL(), false);
1258 void OpenDestURLViaClickTarget() const {
1259 OpenURLWithJSImpl("ClickTarget", dest_url_, GURL(), true);
1262 void OpenDestURLViaClickPing(const GURL& ping_url) const {
1263 OpenURLWithJSImpl("ClickPing", dest_url_, ping_url, false);
1266 void OpenDestURLViaClickNewWindow() const {
1267 OpenURLWithJSImpl("ShiftClick", dest_url_, GURL(), true);
1270 void OpenDestURLViaClickNewForegroundTab() const {
1271 #if defined(OS_MACOSX)
1272 OpenURLWithJSImpl("MetaShiftClick", dest_url_, GURL(), true);
1273 #else
1274 OpenURLWithJSImpl("CtrlShiftClick", dest_url_, GURL(), true);
1275 #endif
1278 void OpenDestURLViaWindowOpen() const {
1279 OpenURLViaWindowOpen(dest_url_);
1282 void OpenURLViaWindowOpen(const GURL& url) const {
1283 OpenURLWithJSImpl("WindowOpen", url, GURL(), true);
1286 void RemoveLinkElement(int i) const {
1287 GetActiveWebContents()->GetMainFrame()->ExecuteJavaScript(
1288 base::ASCIIToUTF16(base::StringPrintf("RemoveLinkElement(%d)", i)));
1291 void ClickToNextPageAfterPrerender() {
1292 TestNavigationObserver nav_observer(GetActiveWebContents());
1293 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame();
1294 render_frame_host->ExecuteJavaScript(base::ASCIIToUTF16("ClickOpenLink()"));
1295 nav_observer.Wait();
1298 void NavigateToNextPageAfterPrerender() const {
1299 ui_test_utils::NavigateToURL(
1300 current_browser(),
1301 test_server()->GetURL("files/prerender/prerender_page.html"));
1304 // Called after the prerendered page has been navigated to and then away from.
1305 // Navigates back through the history to the prerendered page.
1306 void GoBackToPrerender() {
1307 TestNavigationObserver back_nav_observer(GetActiveWebContents());
1308 chrome::GoBack(current_browser(), CURRENT_TAB);
1309 back_nav_observer.Wait();
1310 bool original_prerender_page = false;
1311 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1312 GetActiveWebContents(),
1313 "window.domAutomationController.send(IsOriginalPrerenderPage())",
1314 &original_prerender_page));
1315 EXPECT_TRUE(original_prerender_page);
1318 // Goes back to the page that was active before the prerender was swapped
1319 // in. This must be called when the prerendered page is the current page
1320 // in the active tab.
1321 void GoBackToPageBeforePrerender() {
1322 WebContents* tab = GetActiveWebContents();
1323 ASSERT_TRUE(tab);
1324 EXPECT_FALSE(tab->IsLoading());
1325 TestNavigationObserver back_nav_observer(tab);
1326 chrome::GoBack(current_browser(), CURRENT_TAB);
1327 back_nav_observer.Wait();
1328 bool js_result;
1329 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1330 tab,
1331 "window.domAutomationController.send(DidBackToOriginalPagePass())",
1332 &js_result));
1333 EXPECT_TRUE(js_result);
1336 bool UrlIsInPrerenderManager(const std::string& html_file) const {
1337 return UrlIsInPrerenderManager(test_server()->GetURL(html_file));
1340 bool UrlIsInPrerenderManager(const GURL& url) const {
1341 return GetPrerenderManager()->FindPrerenderData(
1342 url, GetSessionStorageNamespace()) != NULL;
1345 void UseHttpsSrcServer() {
1346 if (https_src_server_)
1347 return;
1348 https_src_server_.reset(
1349 new net::SpawnedTestServer(
1350 net::SpawnedTestServer::TYPE_HTTPS,
1351 net::SpawnedTestServer::kLocalhost,
1352 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
1353 CHECK(https_src_server_->Start());
1356 void DisableJavascriptCalls() {
1357 call_javascript_ = false;
1360 void DisableLoadEventCheck() {
1361 check_load_events_ = false;
1364 TaskManagerModel* GetModel() const {
1365 return TaskManager::GetInstance()->model();
1368 PrerenderManager* GetPrerenderManager() const {
1369 PrerenderManager* prerender_manager =
1370 PrerenderManagerFactory::GetForProfile(current_browser()->profile());
1371 return prerender_manager;
1374 const PrerenderLinkManager* GetPrerenderLinkManager() const {
1375 PrerenderLinkManager* prerender_link_manager =
1376 PrerenderLinkManagerFactory::GetForProfile(
1377 current_browser()->profile());
1378 return prerender_link_manager;
1381 int GetPrerenderEventCount(int index, const std::string& type) const {
1382 int event_count;
1383 std::string expression = base::StringPrintf(
1384 "window.domAutomationController.send("
1385 " GetPrerenderEventCount(%d, '%s'))", index, type.c_str());
1387 CHECK(content::ExecuteScriptAndExtractInt(
1388 GetActiveWebContents(), expression, &event_count));
1389 return event_count;
1392 bool DidReceivePrerenderStartEventForLinkNumber(int index) const {
1393 return GetPrerenderEventCount(index, "webkitprerenderstart") > 0;
1396 int GetPrerenderLoadEventCountForLinkNumber(int index) const {
1397 return GetPrerenderEventCount(index, "webkitprerenderload");
1400 int GetPrerenderDomContentLoadedEventCountForLinkNumber(int index) const {
1401 return GetPrerenderEventCount(index, "webkitprerenderdomcontentloaded");
1404 bool DidReceivePrerenderStopEventForLinkNumber(int index) const {
1405 return GetPrerenderEventCount(index, "webkitprerenderstop") > 0;
1408 void WaitForPrerenderEventCount(int index,
1409 const std::string& type,
1410 int count) const {
1411 int dummy;
1412 std::string expression = base::StringPrintf(
1413 "WaitForPrerenderEventCount(%d, '%s', %d,"
1414 " window.domAutomationController.send.bind("
1415 " window.domAutomationController, 0))",
1416 index, type.c_str(), count);
1418 CHECK(content::ExecuteScriptAndExtractInt(
1419 GetActiveWebContents(), expression, &dummy));
1420 CHECK_EQ(0, dummy);
1423 bool HadPrerenderEventErrors() const {
1424 bool had_prerender_event_errors;
1425 CHECK(content::ExecuteScriptAndExtractBool(
1426 GetActiveWebContents(),
1427 "window.domAutomationController.send(Boolean("
1428 " hadPrerenderEventErrors))",
1429 &had_prerender_event_errors));
1430 return had_prerender_event_errors;
1433 // Asserting on this can result in flaky tests. PrerenderHandles are
1434 // removed from the PrerenderLinkManager when the prerender is canceled from
1435 // the browser, when the prerenders are cancelled from the renderer process,
1436 // or the channel for the renderer process is closed on the IO thread. In the
1437 // last case, the code must be careful to wait for the channel to close, as it
1438 // is done asynchronously after swapping out the old process. See
1439 // ChannelDestructionWatcher.
1440 bool IsEmptyPrerenderLinkManager() const {
1441 return GetPrerenderLinkManager()->IsEmpty();
1444 size_t GetLinkPrerenderCount() const {
1445 return GetPrerenderLinkManager()->prerenders_.size();
1448 size_t GetRunningLinkPrerenderCount() const {
1449 return GetPrerenderLinkManager()->CountRunningPrerenders();
1452 // Returns length of |prerender_manager_|'s history, or -1 on failure.
1453 int GetHistoryLength() const {
1454 scoped_ptr<base::DictionaryValue> prerender_dict(
1455 static_cast<base::DictionaryValue*>(
1456 GetPrerenderManager()->GetAsValue()));
1457 if (!prerender_dict.get())
1458 return -1;
1459 base::ListValue* history_list;
1460 if (!prerender_dict->GetList("history", &history_list))
1461 return -1;
1462 return static_cast<int>(history_list->GetSize());
1465 #if defined(FULL_SAFE_BROWSING)
1466 FakeSafeBrowsingDatabaseManager* GetFakeSafeBrowsingDatabaseManager() {
1467 return safe_browsing_factory_->most_recent_service()->
1468 fake_database_manager();
1470 #endif
1472 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const {
1473 PrerenderManager::PrerenderData* prerender_data =
1474 GetPrerenderManager()->FindPrerenderData(url, NULL);
1475 return static_cast<TestPrerenderContents*>(
1476 prerender_data ? prerender_data->contents() : NULL);
1479 void SetLoaderHostOverride(const std::string& host) {
1480 loader_host_override_ = host;
1481 host_resolver()->AddRule(host, "127.0.0.1");
1484 void set_loader_path(const std::string& path) {
1485 loader_path_ = path;
1488 void set_loader_query(const std::string& query) {
1489 loader_query_ = query;
1492 GURL GetCrossDomainTestUrl(const std::string& path) {
1493 static const std::string secondary_domain = "www.foo.com";
1494 host_resolver()->AddRule(secondary_domain, "127.0.0.1");
1495 std::string url_str(base::StringPrintf(
1496 "http://%s:%d/%s",
1497 secondary_domain.c_str(),
1498 test_server()->host_port_pair().port(),
1499 path.c_str()));
1500 return GURL(url_str);
1503 void set_browser(Browser* browser) {
1504 explicitly_set_browser_ = browser;
1507 Browser* current_browser() const {
1508 return explicitly_set_browser_ ? explicitly_set_browser_ : browser();
1511 const GURL& dest_url() const {
1512 return dest_url_;
1515 void IncreasePrerenderMemory() {
1516 // Increase the memory allowed in a prerendered page above normal settings.
1517 // Debug build bots occasionally run against the default limit, and tests
1518 // were failing because the prerender was canceled due to memory exhaustion.
1519 // http://crbug.com/93076
1520 GetPrerenderManager()->mutable_config().max_bytes = 1000 * 1024 * 1024;
1523 bool DidPrerenderPass(WebContents* web_contents) const {
1524 bool prerender_test_result = false;
1525 if (!content::ExecuteScriptAndExtractBool(
1526 web_contents,
1527 "window.domAutomationController.send(DidPrerenderPass())",
1528 &prerender_test_result))
1529 return false;
1530 return prerender_test_result;
1533 bool DidDisplayPass(WebContents* web_contents) const {
1534 bool display_test_result = false;
1535 if (!content::ExecuteScriptAndExtractBool(
1536 web_contents,
1537 "window.domAutomationController.send(DidDisplayPass())",
1538 &display_test_result))
1539 return false;
1540 return display_test_result;
1543 scoped_ptr<TestPrerender> ExpectPrerender(FinalStatus expected_final_status) {
1544 return prerender_contents_factory_->ExpectPrerenderContents(
1545 expected_final_status);
1548 void AddPrerender(const GURL& url, int index) {
1549 std::string javascript = base::StringPrintf(
1550 "AddPrerender('%s', %d)", url.spec().c_str(), index);
1551 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame();
1552 render_frame_host->ExecuteJavaScript(base::ASCIIToUTF16(javascript));
1555 // Returns a string for pattern-matching TaskManager tab entries.
1556 base::string16 MatchTaskManagerTab(const char* page_title) {
1557 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX,
1558 base::ASCIIToUTF16(page_title));
1561 // Returns a string for pattern-matching TaskManager prerender entries.
1562 base::string16 MatchTaskManagerPrerender(const char* page_title) {
1563 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
1564 base::ASCIIToUTF16(page_title));
1567 const base::HistogramTester& histogram_tester() { return histogram_tester_; }
1569 protected:
1570 bool autostart_test_server_;
1572 private:
1573 // TODO(davidben): Remove this altogether so the tests don't globally assume
1574 // only one prerender.
1575 TestPrerenderContents* GetPrerenderContents() const {
1576 return GetPrerenderContentsFor(dest_url_);
1579 ScopedVector<TestPrerender> PrerenderTestURLImpl(
1580 const GURL& prerender_url,
1581 const std::vector<FinalStatus>& expected_final_status_queue,
1582 int expected_number_of_loads) {
1583 dest_url_ = prerender_url;
1585 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
1586 replacement_text.push_back(
1587 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec()));
1588 std::string replacement_path;
1589 CHECK(net::SpawnedTestServer::GetFilePathWithReplacements(
1590 loader_path_,
1591 replacement_text,
1592 &replacement_path));
1594 const net::SpawnedTestServer* src_server = test_server();
1595 if (https_src_server_)
1596 src_server = https_src_server_.get();
1597 GURL loader_url = src_server->GetURL(
1598 replacement_path + "&" + loader_query_);
1600 GURL::Replacements loader_replacements;
1601 if (!loader_host_override_.empty())
1602 loader_replacements.SetHostStr(loader_host_override_);
1603 loader_url = loader_url.ReplaceComponents(loader_replacements);
1605 VLOG(1) << "Running test with queue length " <<
1606 expected_final_status_queue.size();
1607 CHECK(!expected_final_status_queue.empty());
1608 ScopedVector<TestPrerender> prerenders;
1609 for (size_t i = 0; i < expected_final_status_queue.size(); i++) {
1610 prerenders.push_back(
1611 prerender_contents_factory_->ExpectPrerenderContents(
1612 expected_final_status_queue[i]).release());
1615 FinalStatus expected_final_status = expected_final_status_queue.front();
1617 // Navigate to the loader URL and then wait for the first prerender to be
1618 // created.
1619 ui_test_utils::NavigateToURL(current_browser(), loader_url);
1620 prerenders[0]->WaitForCreate();
1621 prerenders[0]->WaitForLoads(expected_number_of_loads);
1623 if (ShouldAbortPrerenderBeforeSwap(expected_final_status)) {
1624 // The prerender will abort on its own. Assert it does so correctly.
1625 prerenders[0]->WaitForStop();
1626 EXPECT_FALSE(prerenders[0]->contents());
1627 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
1628 } else {
1629 // Otherwise, check that it prerendered correctly.
1630 TestPrerenderContents* prerender_contents = prerenders[0]->contents();
1632 CHECK_NE(static_cast<PrerenderContents*>(NULL), prerender_contents);
1633 EXPECT_EQ(FINAL_STATUS_MAX, prerender_contents->final_status());
1634 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1636 if (call_javascript_) {
1637 // Check if page behaves as expected while in prerendered state.
1638 EXPECT_TRUE(DidPrerenderPass(prerender_contents->prerender_contents()));
1642 // Test that the referring page received the right start and load events.
1643 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1644 if (check_load_events_) {
1645 EXPECT_EQ(expected_number_of_loads, prerenders[0]->number_of_loads());
1646 EXPECT_EQ(expected_number_of_loads,
1647 GetPrerenderLoadEventCountForLinkNumber(0));
1649 EXPECT_FALSE(HadPrerenderEventErrors());
1651 return prerenders.Pass();
1654 void NavigateToURLImpl(const content::OpenURLParams& params,
1655 bool expect_swap_to_succeed) const {
1656 ASSERT_NE(static_cast<PrerenderManager*>(NULL), GetPrerenderManager());
1657 // Make sure in navigating we have a URL to use in the PrerenderManager.
1658 ASSERT_NE(static_cast<PrerenderContents*>(NULL), GetPrerenderContents());
1660 WebContents* web_contents = GetPrerenderContents()->prerender_contents();
1662 // Navigate and wait for either the load to finish normally or for a swap to
1663 // occur.
1664 // TODO(davidben): The only handles CURRENT_TAB navigations, which is the
1665 // only case tested or prerendered right now.
1666 CHECK_EQ(CURRENT_TAB, params.disposition);
1667 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
1668 GetActiveWebContents());
1669 WebContents* target_web_contents = current_browser()->OpenURL(params);
1670 swap_observer.Wait();
1672 if (web_contents && expect_swap_to_succeed) {
1673 EXPECT_EQ(web_contents, target_web_contents);
1674 if (call_javascript_)
1675 EXPECT_TRUE(DidDisplayPass(web_contents));
1679 // Opens the prerendered page using javascript functions in the loader
1680 // page. |javascript_function_name| should be a 0 argument function which is
1681 // invoked. |new_web_contents| is true if the navigation is expected to
1682 // happen in a new WebContents via OpenURL.
1683 void OpenURLWithJSImpl(const std::string& javascript_function_name,
1684 const GURL& url,
1685 const GURL& ping_url,
1686 bool new_web_contents) const {
1687 WebContents* web_contents = GetActiveWebContents();
1688 RenderFrameHost* render_frame_host = web_contents->GetMainFrame();
1689 // Extra arguments in JS are ignored.
1690 std::string javascript = base::StringPrintf(
1691 "%s('%s', '%s')", javascript_function_name.c_str(),
1692 url.spec().c_str(), ping_url.spec().c_str());
1694 if (new_web_contents) {
1695 NewTabNavigationOrSwapObserver observer;
1696 render_frame_host->
1697 ExecuteJavaScriptForTests(base::ASCIIToUTF16(javascript));
1698 observer.Wait();
1699 } else {
1700 NavigationOrSwapObserver observer(current_browser()->tab_strip_model(),
1701 web_contents);
1702 render_frame_host->ExecuteJavaScript(base::ASCIIToUTF16(javascript));
1703 observer.Wait();
1707 TestPrerenderContentsFactory* prerender_contents_factory_;
1708 #if defined(FULL_SAFE_BROWSING)
1709 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_;
1710 #endif
1711 NeverRunsExternalProtocolHandlerDelegate external_protocol_handler_delegate_;
1712 GURL dest_url_;
1713 scoped_ptr<net::SpawnedTestServer> https_src_server_;
1714 bool call_javascript_;
1715 bool check_load_events_;
1716 std::string loader_host_override_;
1717 std::string loader_path_;
1718 std::string loader_query_;
1719 Browser* explicitly_set_browser_;
1720 base::HistogramTester histogram_tester_;
1723 // Checks that a page is correctly prerendered in the case of a
1724 // <link rel=prerender> tag and then loaded into a tab in response to a
1725 // navigation.
1726 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) {
1727 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
1728 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
1729 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1730 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1731 histogram_tester().ExpectTotalCount(
1732 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1733 histogram_tester().ExpectTotalCount(
1734 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
1736 ChannelDestructionWatcher channel_close_watcher;
1737 channel_close_watcher.WatchChannel(
1738 GetActiveWebContents()->GetRenderProcessHost());
1739 NavigateToDestURL();
1740 channel_close_watcher.WaitForChannelClose();
1742 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
1743 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
1745 histogram_tester().ExpectTotalCount(
1746 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
1748 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1751 // Checks that cross-domain prerenders emit the correct histograms.
1752 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageCrossDomain) {
1753 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
1754 FINAL_STATUS_USED, 1);
1755 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1756 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1757 histogram_tester().ExpectTotalCount(
1758 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1759 histogram_tester().ExpectTotalCount(
1760 "Prerender.webcross_PrerenderNotSwappedInPLT", 1);
1762 NavigateToDestURL();
1763 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLT", 1);
1764 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLTMatched",
1766 histogram_tester().ExpectTotalCount(
1767 "Prerender.webcross_PerceivedPLTMatchedComplete", 1);
1770 // Checks that pending prerenders launch and receive proper event treatment.
1771 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPagePending) {
1772 scoped_ptr<TestPrerender> prerender =
1773 PrerenderTestURL("files/prerender/prerender_page_pending.html",
1774 FINAL_STATUS_USED, 1);
1776 // Navigate to the prerender.
1777 scoped_ptr<TestPrerender> prerender2 = ExpectPrerender(FINAL_STATUS_USED);
1778 NavigateToDestURL();
1779 // Abort early if the original prerender didn't swap, so as not to hang.
1780 ASSERT_FALSE(prerender->contents());
1782 // Wait for the new prerender to be ready.
1783 prerender2->WaitForStart();
1784 prerender2->WaitForLoads(1);
1786 const GURL prerender_page_url =
1787 test_server()->GetURL("files/prerender/prerender_page.html");
1788 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1789 EXPECT_NE(static_cast<TestPrerenderContents*>(NULL),
1790 GetPrerenderContentsFor(prerender_page_url));
1792 // Now navigate to our target page.
1793 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
1794 GetActiveWebContents());
1795 ui_test_utils::NavigateToURLWithDisposition(
1796 current_browser(), prerender_page_url, CURRENT_TAB,
1797 ui_test_utils::BROWSER_TEST_NONE);
1798 swap_observer.Wait();
1800 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1803 // Checks that pending prerenders which are canceled before they are launched
1804 // never get started.
1805 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageRemovesPending) {
1806 PrerenderTestURL("files/prerender/prerender_page_removes_pending.html",
1807 FINAL_STATUS_USED, 1);
1809 ChannelDestructionWatcher channel_close_watcher;
1810 channel_close_watcher.WatchChannel(
1811 GetActiveWebContents()->GetRenderProcessHost());
1812 NavigateToDestURL();
1813 channel_close_watcher.WaitForChannelClose();
1815 EXPECT_FALSE(DidReceivePrerenderStartEventForLinkNumber(1));
1816 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1817 EXPECT_FALSE(HadPrerenderEventErrors());
1818 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1819 // calls did a thread/process hop to the renderer which insured pending
1820 // renderer events have arrived.
1821 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1824 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageRemovingLink) {
1825 scoped_ptr<TestPrerender> prerender =
1826 PrerenderTestURL("files/prerender/prerender_page.html",
1827 FINAL_STATUS_CANCELLED, 1);
1829 // No ChannelDestructionWatcher is needed here, since prerenders in the
1830 // PrerenderLinkManager should be deleted by removing the links, rather than
1831 // shutting down the renderer process.
1832 RemoveLinkElement(0);
1833 prerender->WaitForStop();
1835 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1836 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1837 EXPECT_FALSE(HadPrerenderEventErrors());
1838 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1839 // calls did a thread/process hop to the renderer which insured pending
1840 // renderer events have arrived.
1841 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1844 IN_PROC_BROWSER_TEST_F(
1845 PrerenderBrowserTest, PrerenderPageRemovingLinkWithTwoLinks) {
1846 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
1847 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
1849 set_loader_query("links_to_insert=2");
1850 scoped_ptr<TestPrerender> prerender =
1851 PrerenderTestURL("files/prerender/prerender_page.html",
1852 FINAL_STATUS_CANCELLED, 1);
1853 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1854 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1855 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1856 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1858 RemoveLinkElement(0);
1859 RemoveLinkElement(1);
1860 prerender->WaitForStop();
1862 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1863 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1864 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1865 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1866 EXPECT_FALSE(HadPrerenderEventErrors());
1867 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1868 // calls did a thread/process hop to the renderer which insured pending
1869 // renderer events have arrived.
1870 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1873 IN_PROC_BROWSER_TEST_F(
1874 PrerenderBrowserTest, PrerenderPageRemovingLinkWithTwoLinksOneLate) {
1875 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
1876 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
1878 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
1879 scoped_ptr<TestPrerender> prerender =
1880 PrerenderTestURL(url, FINAL_STATUS_CANCELLED, 1);
1882 // Add a second prerender for the same link. It reuses the prerender, so only
1883 // the start event fires here.
1884 AddPrerender(url, 1);
1885 WaitForPrerenderEventCount(1, "webkitprerenderstart", 1);
1886 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1887 EXPECT_EQ(0, GetPrerenderLoadEventCountForLinkNumber(1));
1888 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1890 RemoveLinkElement(0);
1891 RemoveLinkElement(1);
1892 prerender->WaitForStop();
1894 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1895 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1896 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1897 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1898 EXPECT_FALSE(HadPrerenderEventErrors());
1899 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1900 // calls did a thread/process hop to the renderer which insured pending
1901 // renderer events have arrived.
1902 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1905 IN_PROC_BROWSER_TEST_F(
1906 PrerenderBrowserTest,
1907 PrerenderPageRemovingLinkWithTwoLinksRemovingOne) {
1908 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
1909 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
1910 set_loader_query("links_to_insert=2");
1911 PrerenderTestURL("files/prerender/prerender_page.html",
1912 FINAL_STATUS_USED, 1);
1913 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1914 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1915 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1916 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1918 RemoveLinkElement(0);
1919 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1920 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1921 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1922 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1923 EXPECT_FALSE(HadPrerenderEventErrors());
1924 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1925 // calls did a thread/process hop to the renderer which insured pending
1926 // renderer events have arrived.
1927 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1929 ChannelDestructionWatcher channel_close_watcher;
1930 channel_close_watcher.WatchChannel(
1931 GetActiveWebContents()->GetRenderProcessHost());
1932 NavigateToDestURL();
1933 channel_close_watcher.WaitForChannelClose();
1935 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1938 // Checks that the visibility API works.
1939 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderVisibility) {
1940 PrerenderTestURL("files/prerender/prerender_visibility.html",
1941 FINAL_STATUS_USED,
1943 NavigateToDestURL();
1946 // Checks that the prerendering of a page is canceled correctly if we try to
1947 // swap it in before it commits.
1948 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap) {
1949 // Navigate to a page that triggers a prerender for a URL that never commits.
1950 const GURL kNoCommitUrl("http://never-respond.example.com");
1951 base::FilePath file(GetTestPath("prerender_page.html"));
1953 base::RunLoop prerender_start_loop;
1954 BrowserThread::PostTask(
1955 BrowserThread::IO, FROM_HERE,
1956 base::Bind(&CreateHangingFirstRequestInterceptorOnIO, kNoCommitUrl, file,
1957 prerender_start_loop.QuitClosure()));
1958 DisableJavascriptCalls();
1959 PrerenderTestURL(kNoCommitUrl,
1960 FINAL_STATUS_NAVIGATION_UNCOMMITTED,
1962 // Wait for the hanging request to be scheduled.
1963 prerender_start_loop.Run();
1965 // Navigate to the URL, but assume the contents won't be swapped in.
1966 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
1969 // Checks that client redirects don't add alias URLs until after they commit.
1970 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap2) {
1971 // Navigate to a page that then navigates to a URL that never commits.
1972 const GURL kNoCommitUrl("http://never-respond.example.com");
1973 base::FilePath file(GetTestPath("prerender_page.html"));
1975 base::RunLoop prerender_start_loop;
1976 BrowserThread::PostTask(
1977 BrowserThread::IO, FROM_HERE,
1978 base::Bind(&CreateHangingFirstRequestInterceptorOnIO, kNoCommitUrl, file,
1979 prerender_start_loop.QuitClosure()));
1980 DisableJavascriptCalls();
1981 PrerenderTestURL(CreateClientRedirect(kNoCommitUrl.spec()),
1982 FINAL_STATUS_APP_TERMINATING, 1);
1983 // Wait for the hanging request to be scheduled.
1984 prerender_start_loop.Run();
1986 // Navigating to the second URL should not swap.
1987 NavigateToURLWithDisposition(kNoCommitUrl, CURRENT_TAB, false);
1990 // Checks that the prerendering of a page is canceled correctly when a
1991 // Javascript alert is called.
1992 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertBeforeOnload) {
1993 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
1994 FINAL_STATUS_JAVASCRIPT_ALERT,
1998 // Checks that the prerendering of a page is canceled correctly when a
1999 // Javascript alert is called.
2000 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertAfterOnload) {
2001 PrerenderTestURL("files/prerender/prerender_alert_after_onload.html",
2002 FINAL_STATUS_JAVASCRIPT_ALERT,
2006 // Checks that plugins are not loaded while a page is being preloaded, but
2007 // are loaded when the page is displayed.
2008 #if defined(USE_AURA) && !defined(OS_WIN)
2009 // http://crbug.com/103496
2010 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2011 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2012 #elif defined(OS_MACOSX)
2013 // http://crbug.com/100514
2014 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2015 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2016 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
2017 // TODO(jschuh): Failing plugin tests. crbug.com/244653
2018 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2019 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2020 #elif defined(OS_LINUX)
2021 // http://crbug.com/306715
2022 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2023 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2024 #else
2025 #define MAYBE_PrerenderDelayLoadPlugin PrerenderDelayLoadPlugin
2026 #define MAYBE_PrerenderPluginPowerSaver PrerenderPluginPowerSaver
2027 #endif
2028 // http://crbug.com/306715
2029 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, MAYBE_PrerenderDelayLoadPlugin) {
2030 PrerenderTestURL("files/prerender/plugin_delay_load.html",
2031 FINAL_STATUS_USED,
2033 NavigateToDestURL();
2036 // For enabled Plugin Power Saver, checks that plugins are not loaded while
2037 // a page is being preloaded, but are loaded when the page is displayed.
2038 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, MAYBE_PrerenderPluginPowerSaver) {
2039 // Enable click-to-play.
2040 HostContentSettingsMap* content_settings_map =
2041 current_browser()->profile()->GetHostContentSettingsMap();
2042 content_settings_map->SetDefaultContentSetting(
2043 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_DETECT_IMPORTANT_CONTENT);
2045 PrerenderTestURL("files/prerender/prerender_plugin_power_saver.html",
2046 FINAL_STATUS_USED, 1);
2047 NavigateToDestURL();
2050 // Checks that we don't load a NaCl plugin when NaCl is disabled.
2051 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNaClPluginDisabled) {
2052 PrerenderTestURL("files/prerender/prerender_plugin_nacl_disabled.html",
2053 FINAL_STATUS_USED,
2055 NavigateToDestURL();
2058 // Run this check again. When we try to load aa ppapi plugin, the
2059 // "loadstart" event is asynchronously posted to a message loop.
2060 // It's possible that earlier call could have been run before the
2061 // the "loadstart" event was posted.
2062 // TODO(mmenke): While this should reliably fail on regressions, the
2063 // reliability depends on the specifics of ppapi plugin
2064 // loading. It would be great if we could avoid that.
2065 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2068 // Checks that plugins in an iframe are not loaded while a page is
2069 // being preloaded, but are loaded when the page is displayed.
2070 #if defined(USE_AURA) && !defined(OS_WIN)
2071 // http://crbug.com/103496
2072 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2073 DISABLED_PrerenderIframeDelayLoadPlugin
2074 #elif defined(OS_MACOSX)
2075 // http://crbug.com/100514
2076 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2077 DISABLED_PrerenderIframeDelayLoadPlugin
2078 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
2079 // TODO(jschuh): Failing plugin tests. crbug.com/244653
2080 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2081 DISABLED_PrerenderIframeDelayLoadPlugin
2082 #else
2083 #define MAYBE_PrerenderIframeDelayLoadPlugin PrerenderIframeDelayLoadPlugin
2084 #endif
2085 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2086 MAYBE_PrerenderIframeDelayLoadPlugin) {
2087 PrerenderTestURL("files/prerender/prerender_iframe_plugin_delay_load.html",
2088 FINAL_STATUS_USED,
2090 NavigateToDestURL();
2093 // Renders a page that contains a prerender link to a page that contains an
2094 // iframe with a source that requires http authentication. This should not
2095 // prerender successfully.
2096 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttpAuthentication) {
2097 PrerenderTestURL("files/prerender/prerender_http_auth_container.html",
2098 FINAL_STATUS_AUTH_NEEDED,
2102 // Checks that client-issued redirects work with prerendering.
2103 // This version navigates to the page which issues the redirection, rather
2104 // than the final destination page.
2105 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2106 PrerenderClientRedirectNavigateToFirst) {
2107 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2108 FINAL_STATUS_USED,
2110 NavigateToDestURL();
2113 // Checks that client-issued redirects work with prerendering.
2114 // This version navigates to the final destination page, rather than the
2115 // page which does the redirection.
2116 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2117 PrerenderClientRedirectNavigateToSecond) {
2118 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2119 FINAL_STATUS_USED,
2121 NavigateToURL("files/prerender/prerender_page.html");
2124 // Checks that redirects with location.replace do not cancel a prerender and
2125 // and swap when navigating to the first page.
2126 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2127 PrerenderLocationReplaceNavigateToFirst) {
2128 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2129 FINAL_STATUS_USED,
2131 NavigateToDestURL();
2134 // Checks that redirects with location.replace do not cancel a prerender and
2135 // and swap when navigating to the second.
2136 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2137 PrerenderLocationReplaceNavigateToSecond) {
2138 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2139 FINAL_STATUS_USED,
2141 NavigateToURL("files/prerender/prerender_page.html");
2144 // Checks that we get the right PPLT histograms for client redirect prerenders
2145 // and navigations when the referring page is Google.
2146 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2147 PrerenderLocationReplaceGWSHistograms) {
2148 DisableJavascriptCalls();
2150 // The loader page should look like Google.
2151 static const char kGoogleDotCom[] = "www.google.com";
2152 SetLoaderHostOverride(kGoogleDotCom);
2153 set_loader_path("files/prerender/prerender_loader_with_replace_state.html");
2155 GURL dest_url = GetCrossDomainTestUrl(
2156 "files/prerender/prerender_deferred_image.html");
2158 GURL prerender_url = test_server()->GetURL(
2159 "files/prerender/prerender_location_replace.html?" +
2160 net::EscapeQueryParamValue(dest_url.spec(), false) +
2161 "#prerender");
2162 GURL::Replacements replacements;
2163 replacements.SetHostStr(kGoogleDotCom);
2164 prerender_url = prerender_url.ReplaceComponents(replacements);
2166 // The prerender will not completely load until after the swap, so wait for a
2167 // title change before calling DidPrerenderPass.
2168 scoped_ptr<TestPrerender> prerender =
2169 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 1);
2170 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
2171 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
2172 EXPECT_EQ(1, prerender->number_of_loads());
2174 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
2175 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
2176 histogram_tester().ExpectTotalCount(
2177 "Prerender.none_PerceivedPLTMatchedComplete", 0);
2178 // Although there is a client redirect, it is dropped from histograms because
2179 // it is a Google URL. The target page itself does not load until after the
2180 // swap.
2181 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2184 GURL navigate_url = test_server()->GetURL(
2185 "files/prerender/prerender_location_replace.html?" +
2186 net::EscapeQueryParamValue(dest_url.spec(), false) +
2187 "#navigate");
2188 navigate_url = navigate_url.ReplaceComponents(replacements);
2190 NavigationOrSwapObserver swap_observer(
2191 current_browser()->tab_strip_model(),
2192 GetActiveWebContents(), 2);
2193 current_browser()->OpenURL(OpenURLParams(
2194 navigate_url, Referrer(), CURRENT_TAB,
2195 ui::PAGE_TRANSITION_TYPED, false));
2196 swap_observer.Wait();
2198 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2200 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2202 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLT", 1);
2203 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLTMatched", 1);
2204 histogram_tester().ExpectTotalCount(
2205 "Prerender.gws_PerceivedPLTMatchedComplete", 1);
2207 // The client redirect does /not/ count as a miss because it's a Google URL.
2208 histogram_tester().ExpectTotalCount("Prerender.PerceivedPLTFirstAfterMiss",
2212 // Checks that client-issued redirects work with prerendering.
2213 // This version navigates to the final destination page, rather than the
2214 // page which does the redirection via a mouse click.
2215 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2216 PrerenderClientRedirectNavigateToSecondViaClick) {
2217 GURL prerender_url = test_server()->GetURL(
2218 CreateClientRedirect("files/prerender/prerender_page.html"));
2219 GURL destination_url = test_server()->GetURL(
2220 "files/prerender/prerender_page.html");
2221 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 2);
2222 OpenURLViaClick(destination_url);
2225 // Checks that a page served over HTTPS is correctly prerendered.
2226 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttps) {
2227 net::SpawnedTestServer https_server(
2228 net::SpawnedTestServer::TYPE_HTTPS, net::SpawnedTestServer::kLocalhost,
2229 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2230 ASSERT_TRUE(https_server.Start());
2231 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2232 PrerenderTestURL(https_url,
2233 FINAL_STATUS_USED,
2235 NavigateToDestURL();
2238 // Checks that client-issued redirects within an iframe in a prerendered
2239 // page will not count as an "alias" for the prerendered page.
2240 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2241 PrerenderClientRedirectInIframe) {
2242 std::string redirect_path = CreateClientRedirect(
2243 "/files/prerender/prerender_embedded_content.html");
2244 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2245 replacement_text.push_back(
2246 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
2247 std::string replacement_path;
2248 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2249 "files/prerender/prerender_with_iframe.html",
2250 replacement_text,
2251 &replacement_path));
2252 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 2);
2253 EXPECT_FALSE(UrlIsInPrerenderManager(
2254 "files/prerender/prerender_embedded_content.html"));
2255 NavigateToDestURL();
2258 // Checks that server-issued redirects work with prerendering.
2259 // This version navigates to the page which issues the redirection, rather
2260 // than the final destination page.
2261 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2262 PrerenderServerRedirectNavigateToFirst) {
2263 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2264 FINAL_STATUS_USED,
2266 NavigateToDestURL();
2269 // Checks that server-issued redirects work with prerendering.
2270 // This version navigates to the final destination page, rather than the
2271 // page which does the redirection.
2272 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2273 PrerenderServerRedirectNavigateToSecond) {
2274 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2275 FINAL_STATUS_USED,
2277 NavigateToURL("files/prerender/prerender_page.html");
2280 // Checks that server-issued redirects work with prerendering.
2281 // This version navigates to the final destination page, rather than the
2282 // page which does the redirection via a mouse click.
2283 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2284 PrerenderServerRedirectNavigateToSecondViaClick) {
2285 GURL prerender_url = test_server()->GetURL(
2286 CreateServerRedirect("files/prerender/prerender_page.html"));
2287 GURL destination_url = test_server()->GetURL(
2288 "files/prerender/prerender_page.html");
2289 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 1);
2290 OpenURLViaClick(destination_url);
2293 // Checks that server-issued redirects within an iframe in a prerendered
2294 // page will not count as an "alias" for the prerendered page.
2295 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderServerRedirectInIframe) {
2296 std::string redirect_path = CreateServerRedirect(
2297 "/files/prerender/prerender_embedded_content.html");
2298 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2299 replacement_text.push_back(
2300 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
2301 std::string replacement_path;
2302 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2303 "files/prerender/prerender_with_iframe.html",
2304 replacement_text,
2305 &replacement_path));
2306 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2307 EXPECT_FALSE(UrlIsInPrerenderManager(
2308 "files/prerender/prerender_embedded_content.html"));
2309 NavigateToDestURL();
2312 // Prerenders a page that contains an automatic download triggered through an
2313 // iframe. This should not prerender successfully.
2314 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadIframe) {
2315 PrerenderTestURL("files/prerender/prerender_download_iframe.html",
2316 FINAL_STATUS_DOWNLOAD,
2320 // Prerenders a page that contains an automatic download triggered through
2321 // Javascript changing the window.location. This should not prerender
2322 // successfully
2323 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadLocation) {
2324 PrerenderTestURL(CreateClientRedirect("files/download-test1.lib"),
2325 FINAL_STATUS_DOWNLOAD,
2329 // Prerenders a page that contains an automatic download triggered through a
2330 // client-issued redirect. This should not prerender successfully.
2331 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadClientRedirect) {
2332 PrerenderTestURL("files/prerender/prerender_download_refresh.html",
2333 FINAL_STATUS_DOWNLOAD,
2337 // Checks that the referrer is set when prerendering.
2338 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrer) {
2339 PrerenderTestURL("files/prerender/prerender_referrer.html",
2340 FINAL_STATUS_USED,
2342 NavigateToDestURL();
2345 // Checks that the referrer is not set when prerendering and the source page is
2346 // HTTPS.
2347 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2348 PrerenderNoSSLReferrer) {
2349 UseHttpsSrcServer();
2350 PrerenderTestURL("files/prerender/prerender_no_referrer.html",
2351 FINAL_STATUS_USED,
2353 NavigateToDestURL();
2356 // Checks that the referrer is set when prerendering is cancelled.
2357 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelReferrer) {
2358 scoped_ptr<TestContentBrowserClient> test_content_browser_client(
2359 new TestContentBrowserClient);
2360 content::ContentBrowserClient* original_browser_client =
2361 content::SetBrowserClientForTesting(test_content_browser_client.get());
2363 PrerenderTestURL("files/prerender/prerender_referrer.html",
2364 FINAL_STATUS_CANCELLED,
2366 OpenDestURLViaClick();
2368 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2370 content::SetBrowserClientForTesting(original_browser_client);
2373 // Checks that popups on a prerendered page cause cancellation.
2374 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPopup) {
2375 PrerenderTestURL("files/prerender/prerender_popup.html",
2376 FINAL_STATUS_CREATE_NEW_WINDOW,
2380 // Checks that registering a protocol handler causes cancellation.
2381 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRegisterProtocolHandler) {
2382 PrerenderTestURL("files/prerender/prerender_register_protocol_handler.html",
2383 FINAL_STATUS_REGISTER_PROTOCOL_HANDLER,
2387 // Checks that renderers using excessive memory will be terminated.
2388 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExcessiveMemory) {
2389 ASSERT_TRUE(GetPrerenderManager());
2390 GetPrerenderManager()->mutable_config().max_bytes = 30 * 1024 * 1024;
2391 // The excessive memory kill may happen before or after the load event as it
2392 // happens asynchronously with IPC calls. Even if the test does not start
2393 // allocating until after load, the browser process might notice before the
2394 // message gets through. This happens on XP debug bots because they're so
2395 // slow. Instead, don't bother checking the load event count.
2396 DisableLoadEventCheck();
2397 PrerenderTestURL("files/prerender/prerender_excessive_memory.html",
2398 FINAL_STATUS_MEMORY_LIMIT_EXCEEDED, 0);
2401 // Checks shutdown code while a prerender is active.
2402 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderQuickQuit) {
2403 DisableJavascriptCalls();
2404 DisableLoadEventCheck();
2405 PrerenderTestURL("files/prerender/prerender_page.html",
2406 FINAL_STATUS_APP_TERMINATING,
2410 // Checks that we don't prerender in an infinite loop.
2411 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderInfiniteLoop) {
2412 const char* const kHtmlFileA = "files/prerender/prerender_infinite_a.html";
2413 const char* const kHtmlFileB = "files/prerender/prerender_infinite_b.html";
2415 std::vector<FinalStatus> expected_final_status_queue;
2416 expected_final_status_queue.push_back(FINAL_STATUS_USED);
2417 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2419 ScopedVector<TestPrerender> prerenders =
2420 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
2421 ASSERT_TRUE(prerenders[0]->contents());
2422 // Assert that the pending prerender is in there already. This relies on the
2423 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2424 // the page load one.
2425 EXPECT_EQ(2U, GetLinkPrerenderCount());
2426 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2428 // Next url should be in pending list but not an active entry.
2429 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2431 NavigateToDestURL();
2433 // Make sure the PrerenderContents for the next url is now in the manager and
2434 // not pending. This relies on pending prerenders being resolved in the same
2435 // event loop iteration as OnPrerenderStop.
2436 EXPECT_TRUE(UrlIsInPrerenderManager(kHtmlFileB));
2437 EXPECT_EQ(1U, GetLinkPrerenderCount());
2438 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2441 // Checks that we don't prerender in an infinite loop and multiple links are
2442 // handled correctly.
2443 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2444 PrerenderInfiniteLoopMultiple) {
2445 const char* const kHtmlFileA =
2446 "files/prerender/prerender_infinite_a_multiple.html";
2447 const char* const kHtmlFileB =
2448 "files/prerender/prerender_infinite_b_multiple.html";
2449 const char* const kHtmlFileC =
2450 "files/prerender/prerender_infinite_c_multiple.html";
2452 // This test is conceptually simplest if concurrency is at two, since we
2453 // don't have to worry about which of kHtmlFileB or kHtmlFileC gets evicted.
2454 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
2455 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
2457 std::vector<FinalStatus> expected_final_status_queue;
2458 expected_final_status_queue.push_back(FINAL_STATUS_USED);
2459 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2460 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2462 ScopedVector<TestPrerender> prerenders =
2463 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
2464 ASSERT_TRUE(prerenders[0]->contents());
2466 // Next url should be in pending list but not an active entry. This relies on
2467 // the fact that the renderer sends out the AddLinkRelPrerender IPC before
2468 // sending the page load one.
2469 EXPECT_EQ(3U, GetLinkPrerenderCount());
2470 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2471 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2472 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileC));
2474 NavigateToDestURL();
2476 // Make sure the PrerenderContents for the next urls are now in the manager
2477 // and not pending. One and only one of the URLs (the last seen) should be the
2478 // active entry. This relies on pending prerenders being resolved in the same
2479 // event loop iteration as OnPrerenderStop.
2480 bool url_b_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileB);
2481 bool url_c_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileC);
2482 EXPECT_TRUE(url_b_is_active_prerender && url_c_is_active_prerender);
2483 EXPECT_EQ(2U, GetLinkPrerenderCount());
2484 EXPECT_EQ(2U, GetRunningLinkPrerenderCount());
2487 // Checks that pending prerenders are aborted (and never launched) when launched
2488 // by a prerender that itself gets aborted.
2489 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAbortPendingOnCancel) {
2490 const char* const kHtmlFileA = "files/prerender/prerender_infinite_a.html";
2491 const char* const kHtmlFileB = "files/prerender/prerender_infinite_b.html";
2493 scoped_ptr<TestPrerender> prerender =
2494 PrerenderTestURL(kHtmlFileA, FINAL_STATUS_CANCELLED, 1);
2495 ASSERT_TRUE(prerender->contents());
2496 // Assert that the pending prerender is in there already. This relies on the
2497 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2498 // the page load one.
2499 EXPECT_EQ(2U, GetLinkPrerenderCount());
2500 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2502 // Next url should be in pending list but not an active entry.
2503 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2505 // Cancel the prerender.
2506 GetPrerenderManager()->CancelAllPrerenders();
2507 prerender->WaitForStop();
2509 // All prerenders are now gone.
2510 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
2513 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerBeforePrerender) {
2514 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2515 const base::string16 any_tab = MatchTaskManagerTab("*");
2516 const base::string16 original = MatchTaskManagerTab("Preloader");
2517 const base::string16 prerender = MatchTaskManagerPrerender("Prerender Page");
2518 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2520 // Show the task manager. This populates the model.
2521 chrome::OpenTaskManager(current_browser());
2522 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2523 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2525 // Prerender a page in addition to the original tab.
2526 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2528 // A TaskManager entry should appear like "Prerender: Prerender Page"
2529 // alongside the original tab entry. There should be just these two entries.
2530 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender));
2531 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original));
2532 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final));
2533 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender));
2534 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2536 // Swap in the prerendered content.
2537 NavigateToDestURL();
2539 // The "Prerender: " TaskManager entry should disappear, being replaced by a
2540 // "Tab: Prerender Page" entry, and nothing else.
2541 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender));
2542 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original));
2543 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2544 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2545 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2548 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerAfterPrerender) {
2549 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2550 const base::string16 any_tab = MatchTaskManagerTab("*");
2551 const base::string16 original = MatchTaskManagerTab("Preloader");
2552 const base::string16 prerender = MatchTaskManagerPrerender("Prerender Page");
2553 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2555 // Start with two resources.
2556 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2558 // Show the task manager. This populates the model. Importantly, we're doing
2559 // this after the prerender WebContents already exists - the task manager
2560 // needs to find it, it can't just listen for creation.
2561 chrome::OpenTaskManager(current_browser());
2563 // A TaskManager entry should appear like "Prerender: Prerender Page"
2564 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender));
2565 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original));
2566 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final));
2567 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender));
2568 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2570 // Swap in the tab.
2571 NavigateToDestURL();
2573 // The "Prerender: Prerender Page" TaskManager row should disappear, being
2574 // replaced by "Tab: Prerender Page"
2575 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender));
2576 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original));
2577 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2578 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2579 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2582 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerAfterSwapIn) {
2583 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2584 const base::string16 any_tab = MatchTaskManagerTab("*");
2585 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2587 // Prerender, and swap it in.
2588 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2589 NavigateToDestURL();
2591 // Show the task manager. This populates the model. Importantly, we're doing
2592 // this after the prerender has been swapped in.
2593 chrome::OpenTaskManager(current_browser());
2595 // We should not see a prerender resource in the task manager, just a normal
2596 // page.
2597 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2598 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2599 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2602 // Checks that audio loads are deferred on prerendering.
2603 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Audio) {
2604 PrerenderTestURL("files/prerender/prerender_html5_audio.html",
2605 FINAL_STATUS_USED,
2607 NavigateToDestURL();
2608 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2611 // Checks that audio loads are deferred on prerendering and played back when
2612 // the prerender is swapped in if autoplay is set.
2613 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioAutoplay) {
2614 PrerenderTestURL("files/prerender/prerender_html5_audio_autoplay.html",
2615 FINAL_STATUS_USED,
2617 NavigateToDestURL();
2618 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2621 // Checks that audio loads are deferred on prerendering and played back when
2622 // the prerender is swapped in if js starts playing.
2623 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioJsplay) {
2624 PrerenderTestURL("files/prerender/prerender_html5_audio_jsplay.html",
2625 FINAL_STATUS_USED,
2627 NavigateToDestURL();
2628 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2631 // Checks that video loads are deferred on prerendering.
2632 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Video) {
2633 PrerenderTestURL("files/prerender/prerender_html5_video.html",
2634 FINAL_STATUS_USED,
2636 NavigateToDestURL();
2637 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2640 // Checks that video tags inserted by javascript are deferred and played
2641 // correctly on swap in.
2642 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoJs) {
2643 PrerenderTestURL("files/prerender/prerender_html5_video_script.html",
2644 FINAL_STATUS_USED,
2646 NavigateToDestURL();
2647 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2650 // Checks for correct network events by using a busy sleep the javascript.
2651 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoNetwork) {
2652 DisableJavascriptCalls();
2653 scoped_ptr<TestPrerender> prerender =
2654 PrerenderTestURL("files/prerender/prerender_html5_video_network.html",
2655 FINAL_STATUS_USED,
2657 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
2658 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
2659 NavigateToDestURL();
2660 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2663 // Checks that scripts can retrieve the correct window size while prerendering.
2664 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWindowSize) {
2665 PrerenderTestURL("files/prerender/prerender_size.html",
2666 FINAL_STATUS_USED,
2668 NavigateToDestURL();
2671 // TODO(jam): http://crbug.com/350550
2672 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
2674 // Checks that prerenderers will terminate when the RenderView crashes.
2675 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRendererCrash) {
2676 scoped_ptr<TestPrerender> prerender =
2677 PrerenderTestURL("files/prerender/prerender_page.html",
2678 FINAL_STATUS_RENDERER_CRASHED,
2681 // Navigate to about:crash and then wait for the renderer to crash.
2682 ASSERT_TRUE(prerender->contents());
2683 ASSERT_TRUE(prerender->contents()->prerender_contents());
2684 prerender->contents()->prerender_contents()->GetController().
2685 LoadURL(
2686 GURL(content::kChromeUICrashURL),
2687 content::Referrer(),
2688 ui::PAGE_TRANSITION_TYPED,
2689 std::string());
2690 prerender->WaitForStop();
2692 #endif
2694 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2695 PrerenderPageWithFragment) {
2696 PrerenderTestURL("files/prerender/prerender_page.html#fragment",
2697 FINAL_STATUS_USED,
2700 ChannelDestructionWatcher channel_close_watcher;
2701 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
2702 GetActiveWebContents()->GetRenderProcessHost());
2703 NavigateToDestURL();
2704 channel_close_watcher.WaitForChannelClose();
2706 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2709 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2710 PrerenderPageWithRedirectedFragment) {
2711 PrerenderTestURL(
2712 CreateClientRedirect("files/prerender/prerender_page.html#fragment"),
2713 FINAL_STATUS_USED,
2716 ChannelDestructionWatcher channel_close_watcher;
2717 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
2718 GetActiveWebContents()->GetRenderProcessHost());
2719 NavigateToDestURL();
2720 channel_close_watcher.WaitForChannelClose();
2722 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2725 // Checks that we do not use a prerendered page when navigating from
2726 // the main page to a fragment.
2727 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2728 PrerenderPageNavigateFragment) {
2729 PrerenderTestURL("files/prerender/no_prerender_page.html",
2730 FINAL_STATUS_APP_TERMINATING,
2732 NavigateToURLWithDisposition(
2733 "files/prerender/no_prerender_page.html#fragment",
2734 CURRENT_TAB, false);
2737 // Checks that we do not use a prerendered page when we prerender a fragment
2738 // but navigate to the main page.
2739 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2740 PrerenderFragmentNavigatePage) {
2741 PrerenderTestURL("files/prerender/no_prerender_page.html#fragment",
2742 FINAL_STATUS_APP_TERMINATING,
2744 NavigateToURLWithDisposition(
2745 "files/prerender/no_prerender_page.html",
2746 CURRENT_TAB, false);
2749 // Checks that we do not use a prerendered page when we prerender a fragment
2750 // but navigate to a different fragment on the same page.
2751 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2752 PrerenderFragmentNavigateFragment) {
2753 PrerenderTestURL("files/prerender/no_prerender_page.html#other_fragment",
2754 FINAL_STATUS_APP_TERMINATING,
2756 NavigateToURLWithDisposition(
2757 "files/prerender/no_prerender_page.html#fragment",
2758 CURRENT_TAB, false);
2761 // Checks that we do not use a prerendered page when the page uses a client
2762 // redirect to refresh from a fragment on the same page.
2763 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2764 PrerenderClientRedirectFromFragment) {
2765 PrerenderTestURL(
2766 CreateClientRedirect("files/prerender/no_prerender_page.html#fragment"),
2767 FINAL_STATUS_APP_TERMINATING,
2769 NavigateToURLWithDisposition(
2770 "files/prerender/no_prerender_page.html",
2771 CURRENT_TAB, false);
2774 // Checks that we do not use a prerendered page when the page uses a client
2775 // redirect to refresh to a fragment on the same page.
2776 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2777 PrerenderClientRedirectToFragment) {
2778 PrerenderTestURL(
2779 CreateClientRedirect("files/prerender/no_prerender_page.html"),
2780 FINAL_STATUS_APP_TERMINATING,
2782 NavigateToURLWithDisposition(
2783 "files/prerender/no_prerender_page.html#fragment",
2784 CURRENT_TAB, false);
2787 // Checks that we correctly use a prerendered page when the page uses JS to set
2788 // the window.location.hash to a fragment on the same page.
2789 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2790 PrerenderPageChangeFragmentLocationHash) {
2791 PrerenderTestURL("files/prerender/prerender_fragment_location_hash.html",
2792 FINAL_STATUS_USED,
2794 NavigateToURL("files/prerender/prerender_fragment_location_hash.html");
2797 // Checks that prerendering a PNG works correctly.
2798 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImagePng) {
2799 DisableJavascriptCalls();
2800 PrerenderTestURL("files/prerender/image.png", FINAL_STATUS_USED, 1);
2801 NavigateToDestURL();
2804 // Checks that prerendering a JPG works correctly.
2805 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImageJpeg) {
2806 DisableJavascriptCalls();
2807 PrerenderTestURL("files/prerender/image.jpeg", FINAL_STATUS_USED, 1);
2808 NavigateToDestURL();
2811 // Checks that a prerender of a CRX will result in a cancellation due to
2812 // download.
2813 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCrx) {
2814 PrerenderTestURL("files/prerender/extension.crx", FINAL_STATUS_DOWNLOAD, 0);
2817 // Checks that xhr GET requests allow prerenders.
2818 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrGet) {
2819 PrerenderTestURL("files/prerender/prerender_xhr_get.html",
2820 FINAL_STATUS_USED,
2822 NavigateToDestURL();
2825 // Checks that xhr HEAD requests allow prerenders.
2826 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrHead) {
2827 PrerenderTestURL("files/prerender/prerender_xhr_head.html",
2828 FINAL_STATUS_USED,
2830 NavigateToDestURL();
2833 // Checks that xhr OPTIONS requests allow prerenders.
2834 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrOptions) {
2835 PrerenderTestURL("files/prerender/prerender_xhr_options.html",
2836 FINAL_STATUS_USED,
2838 NavigateToDestURL();
2841 // Checks that xhr TRACE requests allow prerenders.
2842 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrTrace) {
2843 PrerenderTestURL("files/prerender/prerender_xhr_trace.html",
2844 FINAL_STATUS_USED,
2846 NavigateToDestURL();
2849 // Checks that xhr POST requests allow prerenders.
2850 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPost) {
2851 PrerenderTestURL("files/prerender/prerender_xhr_post.html",
2852 FINAL_STATUS_USED,
2854 NavigateToDestURL();
2857 // Checks that xhr PUT cancels prerenders.
2858 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPut) {
2859 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
2860 FINAL_STATUS_INVALID_HTTP_METHOD,
2864 // Checks that xhr DELETE cancels prerenders.
2865 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrDelete) {
2866 PrerenderTestURL("files/prerender/prerender_xhr_delete.html",
2867 FINAL_STATUS_INVALID_HTTP_METHOD,
2871 // Checks that a top-level page which would trigger an SSL error is canceled.
2872 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorTopLevel) {
2873 net::SpawnedTestServer::SSLOptions ssl_options;
2874 ssl_options.server_certificate =
2875 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2876 net::SpawnedTestServer https_server(
2877 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2878 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2879 ASSERT_TRUE(https_server.Start());
2880 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2881 PrerenderTestURL(https_url,
2882 FINAL_STATUS_SSL_ERROR,
2886 // Checks that an SSL error that comes from a subresource does not cancel
2887 // the page. Non-main-frame requests are simply cancelled if they run into
2888 // an SSL problem.
2889 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorSubresource) {
2890 net::SpawnedTestServer::SSLOptions ssl_options;
2891 ssl_options.server_certificate =
2892 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2893 net::SpawnedTestServer https_server(
2894 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2895 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2896 ASSERT_TRUE(https_server.Start());
2897 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
2898 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2899 replacement_text.push_back(
2900 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
2901 std::string replacement_path;
2902 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2903 "files/prerender/prerender_with_image.html",
2904 replacement_text,
2905 &replacement_path));
2906 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2907 NavigateToDestURL();
2910 // Checks that an SSL error that comes from an iframe does not cancel
2911 // the page. Non-main-frame requests are simply cancelled if they run into
2912 // an SSL problem.
2913 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorIframe) {
2914 net::SpawnedTestServer::SSLOptions ssl_options;
2915 ssl_options.server_certificate =
2916 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2917 net::SpawnedTestServer https_server(
2918 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2919 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2920 ASSERT_TRUE(https_server.Start());
2921 GURL https_url = https_server.GetURL(
2922 "files/prerender/prerender_embedded_content.html");
2923 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2924 replacement_text.push_back(
2925 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
2926 std::string replacement_path;
2927 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2928 "files/prerender/prerender_with_iframe.html",
2929 replacement_text,
2930 &replacement_path));
2931 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2932 NavigateToDestURL();
2935 // Checks that we cancel correctly when window.print() is called.
2936 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPrint) {
2937 DisableLoadEventCheck();
2938 PrerenderTestURL("files/prerender/prerender_print.html",
2939 FINAL_STATUS_WINDOW_PRINT,
2943 // Checks that prerenders do not get swapped into target pages that have opened
2944 // popups; the BrowsingInstance is not empty.
2945 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderTargetHasPopup) {
2946 PrerenderTestURL("files/prerender/prerender_page.html",
2947 FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE,
2949 OpenURLViaWindowOpen(GURL(url::kAboutBlankURL));
2951 // Switch back to the current tab and attempt to swap it in.
2952 current_browser()->tab_strip_model()->ActivateTabAt(0, true);
2953 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
2956 class TestClientCertStore : public net::ClientCertStore {
2957 public:
2958 TestClientCertStore() {}
2959 ~TestClientCertStore() override {}
2961 // net::ClientCertStore:
2962 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info,
2963 net::CertificateList* selected_certs,
2964 const base::Closure& callback) override {
2965 *selected_certs = net::CertificateList(
2966 1, scoped_refptr<net::X509Certificate>(
2967 new net::X509Certificate("test", "test", base::Time(), base::Time())));
2968 callback.Run();
2972 scoped_ptr<net::ClientCertStore> CreateCertStore() {
2973 return scoped_ptr<net::ClientCertStore>(new TestClientCertStore);
2976 // Checks that a top-level page which would normally request an SSL client
2977 // certificate will never be seen since it's an https top-level resource.
2978 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2979 PrerenderSSLClientCertTopLevel) {
2980 ProfileIOData::FromResourceContext(
2981 current_browser()->profile()->GetResourceContext())->
2982 set_client_cert_store_factory_for_testing(
2983 base::Bind(&CreateCertStore));
2984 net::SpawnedTestServer::SSLOptions ssl_options;
2985 ssl_options.request_client_certificate = true;
2986 net::SpawnedTestServer https_server(
2987 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2988 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2989 ASSERT_TRUE(https_server.Start());
2990 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2991 PrerenderTestURL(https_url, FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED, 0);
2994 // Checks that an SSL Client Certificate request that originates from a
2995 // subresource will cancel the prerendered page.
2996 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2997 PrerenderSSLClientCertSubresource) {
2998 ProfileIOData::FromResourceContext(
2999 current_browser()->profile()->GetResourceContext())->
3000 set_client_cert_store_factory_for_testing(
3001 base::Bind(&CreateCertStore));
3002 net::SpawnedTestServer::SSLOptions ssl_options;
3003 ssl_options.request_client_certificate = true;
3004 net::SpawnedTestServer https_server(
3005 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
3006 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3007 ASSERT_TRUE(https_server.Start());
3008 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
3009 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3010 replacement_text.push_back(
3011 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
3012 std::string replacement_path;
3013 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3014 "files/prerender/prerender_with_image.html",
3015 replacement_text,
3016 &replacement_path));
3017 PrerenderTestURL(replacement_path,
3018 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
3022 // Checks that an SSL Client Certificate request that originates from an
3023 // iframe will cancel the prerendered page.
3024 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLClientCertIframe) {
3025 ProfileIOData::FromResourceContext(
3026 current_browser()->profile()->GetResourceContext())->
3027 set_client_cert_store_factory_for_testing(
3028 base::Bind(&CreateCertStore));
3029 net::SpawnedTestServer::SSLOptions ssl_options;
3030 ssl_options.request_client_certificate = true;
3031 net::SpawnedTestServer https_server(
3032 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
3033 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3034 ASSERT_TRUE(https_server.Start());
3035 GURL https_url = https_server.GetURL(
3036 "files/prerender/prerender_embedded_content.html");
3037 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3038 replacement_text.push_back(
3039 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
3040 std::string replacement_path;
3041 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3042 "files/prerender/prerender_with_iframe.html",
3043 replacement_text,
3044 &replacement_path));
3045 PrerenderTestURL(replacement_path,
3046 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
3050 #if defined(FULL_SAFE_BROWSING)
3051 // Ensures that we do not prerender pages with a safe browsing
3052 // interstitial.
3053 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingTopLevel) {
3054 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3055 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3056 url, SB_THREAT_TYPE_URL_MALWARE);
3057 PrerenderTestURL("files/prerender/prerender_page.html",
3058 FINAL_STATUS_SAFE_BROWSING, 0);
3061 // Ensures that server redirects to a malware page will cancel prerenders.
3062 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3063 PrerenderSafeBrowsingServerRedirect) {
3064 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3065 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3066 url, SB_THREAT_TYPE_URL_MALWARE);
3067 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
3068 FINAL_STATUS_SAFE_BROWSING,
3072 // Ensures that client redirects to a malware page will cancel prerenders.
3073 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3074 PrerenderSafeBrowsingClientRedirect) {
3075 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3076 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3077 url, SB_THREAT_TYPE_URL_MALWARE);
3078 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
3079 FINAL_STATUS_SAFE_BROWSING,
3083 // Ensures that we do not prerender pages which have a malware subresource.
3084 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingSubresource) {
3085 GURL image_url = test_server()->GetURL("files/prerender/image.jpeg");
3086 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3087 image_url, SB_THREAT_TYPE_URL_MALWARE);
3088 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3089 replacement_text.push_back(
3090 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3091 std::string replacement_path;
3092 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3093 "files/prerender/prerender_with_image.html",
3094 replacement_text,
3095 &replacement_path));
3096 PrerenderTestURL(replacement_path,
3097 FINAL_STATUS_SAFE_BROWSING,
3101 // Ensures that we do not prerender pages which have a malware iframe.
3102 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingIframe) {
3103 GURL iframe_url = test_server()->GetURL(
3104 "files/prerender/prerender_embedded_content.html");
3105 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3106 iframe_url, SB_THREAT_TYPE_URL_MALWARE);
3107 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3108 replacement_text.push_back(
3109 std::make_pair("REPLACE_WITH_URL", iframe_url.spec()));
3110 std::string replacement_path;
3111 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3112 "files/prerender/prerender_with_iframe.html",
3113 replacement_text,
3114 &replacement_path));
3115 PrerenderTestURL(replacement_path,
3116 FINAL_STATUS_SAFE_BROWSING,
3120 #endif
3122 // Checks that a local storage read will not cause prerender to fail.
3123 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageRead) {
3124 PrerenderTestURL("files/prerender/prerender_localstorage_read.html",
3125 FINAL_STATUS_USED,
3127 NavigateToDestURL();
3130 // Checks that a local storage write will not cause prerender to fail.
3131 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) {
3132 PrerenderTestURL("files/prerender/prerender_localstorage_write.html",
3133 FINAL_STATUS_USED,
3135 NavigateToDestURL();
3138 // Checks that the favicon is properly loaded on prerender.
3139 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderFavicon) {
3140 scoped_ptr<TestPrerender> prerender =
3141 PrerenderTestURL("files/prerender/prerender_favicon.html",
3142 FINAL_STATUS_USED,
3144 NavigateToDestURL();
3146 if (!FaviconTabHelper::FromWebContents(
3147 GetActiveWebContents())->FaviconIsValid()) {
3148 // If the favicon has not been set yet, wait for it to be.
3149 content::WindowedNotificationObserver favicon_update_watcher(
3150 chrome::NOTIFICATION_FAVICON_UPDATED,
3151 content::Source<WebContents>(GetActiveWebContents()));
3152 favicon_update_watcher.Wait();
3154 EXPECT_TRUE(FaviconTabHelper::FromWebContents(
3155 GetActiveWebContents())->FaviconIsValid());
3158 // Checks that when a prerendered page is swapped in to a referring page, the
3159 // unload handlers on the referring page are executed.
3160 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderUnload) {
3161 // Matches URL in prerender_loader_with_unload.html.
3162 const GURL unload_url("http://unload-url.test");
3163 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3164 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3165 RequestCounter unload_counter;
3166 BrowserThread::PostTask(
3167 BrowserThread::IO, FROM_HERE,
3168 base::Bind(&CreateCountingInterceptorOnIO,
3169 unload_url, empty_file, unload_counter.AsWeakPtr()));
3171 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3172 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3173 NavigateToDestURL();
3174 unload_counter.WaitForCount(1);
3177 // Checks that a hanging unload on the referring page of a prerender swap does
3178 // not crash the browser on exit.
3179 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHangingUnload) {
3180 // Matches URL in prerender_loader_with_unload.html.
3181 const GURL hang_url("http://unload-url.test");
3182 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3183 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3184 BrowserThread::PostTask(
3185 BrowserThread::IO, FROM_HERE,
3186 base::Bind(&CreateHangingFirstRequestInterceptorOnIO,
3187 hang_url, empty_file,
3188 base::Closure()));
3190 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3191 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3192 NavigateToDestURL();
3196 // Checks that when the history is cleared, prerendering is cancelled and
3197 // prerendering history is cleared.
3198 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearHistory) {
3199 scoped_ptr<TestPrerender> prerender =
3200 PrerenderTestURL("files/prerender/prerender_page.html",
3201 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
3204 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_HISTORY);
3205 prerender->WaitForStop();
3207 // Make sure prerender history was cleared.
3208 EXPECT_EQ(0, GetHistoryLength());
3211 // Checks that when the cache is cleared, prerenders are cancelled but
3212 // prerendering history is not cleared.
3213 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearCache) {
3214 scoped_ptr<TestPrerender> prerender =
3215 PrerenderTestURL("files/prerender/prerender_page.html",
3216 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
3219 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_CACHE);
3220 prerender->WaitForStop();
3222 // Make sure prerender history was not cleared. Not a vital behavior, but
3223 // used to compare with PrerenderClearHistory test.
3224 EXPECT_EQ(1, GetHistoryLength());
3227 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelAll) {
3228 scoped_ptr<TestPrerender> prerender =
3229 PrerenderTestURL("files/prerender/prerender_page.html",
3230 FINAL_STATUS_CANCELLED,
3233 GetPrerenderManager()->CancelAllPrerenders();
3234 prerender->WaitForStop();
3236 EXPECT_FALSE(prerender->contents());
3239 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderEvents) {
3240 scoped_ptr<TestPrerender> prerender =
3241 PrerenderTestURL("files/prerender/prerender_page.html",
3242 FINAL_STATUS_CANCELLED, 1);
3244 GetPrerenderManager()->CancelAllPrerenders();
3245 prerender->WaitForStop();
3247 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
3248 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
3249 EXPECT_FALSE(HadPrerenderEventErrors());
3252 // Cancels the prerender of a page with its own prerender. The second prerender
3253 // should never be started.
3254 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3255 PrerenderCancelPrerenderWithPrerender) {
3256 scoped_ptr<TestPrerender> prerender =
3257 PrerenderTestURL("files/prerender/prerender_infinite_a.html",
3258 FINAL_STATUS_CANCELLED,
3261 GetPrerenderManager()->CancelAllPrerenders();
3262 prerender->WaitForStop();
3264 EXPECT_FALSE(prerender->contents());
3267 // Prerendering and history tests.
3268 // The prerendered page is navigated to in several ways [navigate via
3269 // omnibox, click on link, key-modified click to open in background tab, etc],
3270 // followed by a navigation to another page from the prerendered page, followed
3271 // by a back navigation.
3273 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNavigateClickGoBack) {
3274 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3275 FINAL_STATUS_USED,
3277 NavigateToDestURL();
3278 ClickToNextPageAfterPrerender();
3279 GoBackToPrerender();
3282 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNavigateNavigateGoBack) {
3283 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3284 FINAL_STATUS_USED,
3286 NavigateToDestURL();
3287 NavigateToNextPageAfterPrerender();
3288 GoBackToPrerender();
3291 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickClickGoBack) {
3292 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3293 FINAL_STATUS_USED,
3295 OpenDestURLViaClick();
3296 ClickToNextPageAfterPrerender();
3297 GoBackToPrerender();
3300 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNavigateGoBack) {
3301 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3302 FINAL_STATUS_USED,
3304 OpenDestURLViaClick();
3305 NavigateToNextPageAfterPrerender();
3306 GoBackToPrerender();
3309 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewWindow) {
3310 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3311 FINAL_STATUS_APP_TERMINATING, 1);
3312 OpenDestURLViaClickNewWindow();
3315 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewForegroundTab) {
3316 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3317 FINAL_STATUS_APP_TERMINATING, 1);
3318 OpenDestURLViaClickNewForegroundTab();
3321 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3322 NavigateToPrerenderedPageWhenDevToolsAttached) {
3323 DisableJavascriptCalls();
3324 WebContents* web_contents =
3325 current_browser()->tab_strip_model()->GetActiveWebContents();
3326 scoped_refptr<DevToolsAgentHost> agent(
3327 DevToolsAgentHost::GetOrCreateFor(web_contents));
3328 FakeDevToolsClient client;
3329 agent->AttachClient(&client);
3330 const char* url = "files/prerender/prerender_page.html";
3331 PrerenderTestURL(url, FINAL_STATUS_DEVTOOLS_ATTACHED, 1);
3332 NavigateToURLWithDisposition(url, CURRENT_TAB, false);
3333 agent->DetachClient();
3336 // Validate that the sessionStorage namespace remains the same when swapping
3337 // in a prerendered page.
3338 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSessionStorage) {
3339 set_loader_path("files/prerender/prerender_loader_with_session_storage.html");
3340 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
3341 FINAL_STATUS_USED,
3343 NavigateToDestURL();
3344 GoBackToPageBeforePrerender();
3347 // Checks that the control group works. An XHR PUT cannot be detected in the
3348 // control group.
3349 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, ControlGroup) {
3350 RestorePrerenderMode restore_prerender_mode;
3351 PrerenderManager::SetMode(
3352 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
3353 DisableJavascriptCalls();
3354 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3355 FINAL_STATUS_WOULD_HAVE_BEEN_USED, 0);
3356 NavigateToDestURL();
3359 // Checks that the control group correctly hits WOULD_HAVE_BEEN_USED
3360 // renderer-initiated navigations. (This verifies that the ShouldFork logic
3361 // behaves correctly.)
3362 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, ControlGroupRendererInitiated) {
3363 RestorePrerenderMode restore_prerender_mode;
3364 PrerenderManager::SetMode(
3365 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
3366 DisableJavascriptCalls();
3367 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3368 FINAL_STATUS_WOULD_HAVE_BEEN_USED, 0);
3369 OpenDestURLViaClick();
3372 // Make sure that the MatchComplete dummy works in the normal case. Once
3373 // a prerender is cancelled because of a script, a dummy must be created to
3374 // account for the MatchComplete case, and it must have a final status of
3375 // FINAL_STATUS_WOULD_HAVE_BEEN_USED.
3376 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, MatchCompleteDummy) {
3377 RestorePrerenderMode restore_prerender_mode;
3378 PrerenderManager::SetMode(
3379 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3381 std::vector<FinalStatus> expected_final_status_queue;
3382 expected_final_status_queue.push_back(FINAL_STATUS_INVALID_HTTP_METHOD);
3383 expected_final_status_queue.push_back(FINAL_STATUS_WOULD_HAVE_BEEN_USED);
3384 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3385 expected_final_status_queue, 1);
3386 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3387 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3388 histogram_tester().ExpectTotalCount(
3389 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3390 histogram_tester().ExpectTotalCount(
3391 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
3393 NavigateToDestURL();
3394 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3395 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3397 histogram_tester().ExpectTotalCount(
3398 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3401 class PrerenderBrowserTestWithNaCl : public PrerenderBrowserTest {
3402 public:
3403 PrerenderBrowserTestWithNaCl() {}
3404 ~PrerenderBrowserTestWithNaCl() override {}
3406 void SetUpCommandLine(base::CommandLine* command_line) override {
3407 PrerenderBrowserTest::SetUpCommandLine(command_line);
3408 command_line->AppendSwitch(switches::kEnableNaCl);
3412 // Check that NaCl plugins work when enabled, with prerendering.
3413 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithNaCl,
3414 PrerenderNaClPluginEnabled) {
3415 #if defined(OS_WIN) && defined(USE_ASH)
3416 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
3417 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
3418 switches::kAshBrowserTests))
3419 return;
3420 #endif
3422 PrerenderTestURL("files/prerender/prerender_plugin_nacl_enabled.html",
3423 FINAL_STATUS_USED,
3425 NavigateToDestURL();
3427 // To avoid any chance of a race, we have to let the script send its response
3428 // asynchronously.
3429 WebContents* web_contents =
3430 browser()->tab_strip_model()->GetActiveWebContents();
3431 bool display_test_result = false;
3432 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents,
3433 "DidDisplayReallyPass()",
3434 &display_test_result));
3435 ASSERT_TRUE(display_test_result);
3438 // Checks that the referrer policy is used when prerendering.
3439 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrerPolicy) {
3440 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3441 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3442 FINAL_STATUS_USED,
3444 NavigateToDestURL();
3447 // Checks that the referrer policy is used when prerendering on HTTPS.
3448 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3449 PrerenderSSLReferrerPolicy) {
3450 UseHttpsSrcServer();
3451 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3452 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3453 FINAL_STATUS_USED,
3455 NavigateToDestURL();
3458 // Checks that the referrer policy is used when prerendering is cancelled.
3459 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelReferrerPolicy) {
3460 scoped_ptr<TestContentBrowserClient> test_content_browser_client(
3461 new TestContentBrowserClient);
3462 content::ContentBrowserClient* original_browser_client =
3463 content::SetBrowserClientForTesting(test_content_browser_client.get());
3465 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3466 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3467 FINAL_STATUS_CANCELLED,
3469 OpenDestURLViaClick();
3471 bool display_test_result = false;
3472 WebContents* web_contents =
3473 browser()->tab_strip_model()->GetActiveWebContents();
3474 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
3475 web_contents,
3476 "window.domAutomationController.send(DidDisplayPass())",
3477 &display_test_result));
3478 EXPECT_TRUE(display_test_result);
3480 content::SetBrowserClientForTesting(original_browser_client);
3483 // Test interaction of the webNavigation and tabs API with prerender.
3484 class PrerenderBrowserTestWithExtensions : public PrerenderBrowserTest,
3485 public ExtensionApiTest {
3486 public:
3487 PrerenderBrowserTestWithExtensions() {
3488 // The individual tests start the test server through ExtensionApiTest, so
3489 // the port number can be passed through to the extension.
3490 autostart_test_server_ = false;
3493 void SetUp() override { PrerenderBrowserTest::SetUp(); }
3495 void SetUpCommandLine(base::CommandLine* command_line) override {
3496 PrerenderBrowserTest::SetUpCommandLine(command_line);
3497 ExtensionApiTest::SetUpCommandLine(command_line);
3500 void SetUpInProcessBrowserTestFixture() override {
3501 PrerenderBrowserTest::SetUpInProcessBrowserTestFixture();
3502 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
3505 void TearDownInProcessBrowserTestFixture() override {
3506 PrerenderBrowserTest::TearDownInProcessBrowserTestFixture();
3507 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
3510 void SetUpOnMainThread() override {
3511 PrerenderBrowserTest::SetUpOnMainThread();
3515 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, WebNavigation) {
3516 ASSERT_TRUE(StartSpawnedTestServer());
3517 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3519 // Wait for the extension to set itself up and return control to us.
3520 ASSERT_TRUE(RunExtensionTest("webnavigation/prerender")) << message_;
3522 extensions::ResultCatcher catcher;
3524 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3526 ChannelDestructionWatcher channel_close_watcher;
3527 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
3528 GetActiveWebContents()->GetRenderProcessHost());
3529 NavigateToDestURL();
3530 channel_close_watcher.WaitForChannelClose();
3532 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3533 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
3536 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, TabsApi) {
3537 ASSERT_TRUE(StartSpawnedTestServer());
3538 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3540 // Wait for the extension to set itself up and return control to us.
3541 ASSERT_TRUE(RunExtensionTest("tabs/on_replaced")) << message_;
3543 extensions::ResultCatcher catcher;
3545 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3547 ChannelDestructionWatcher channel_close_watcher;
3548 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
3549 GetActiveWebContents()->GetRenderProcessHost());
3550 NavigateToDestURL();
3551 channel_close_watcher.WaitForChannelClose();
3553 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3554 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
3557 // Test that prerenders abort when navigating to a stream.
3558 // See chrome/browser/extensions/api/streams_private/streams_private_apitest.cc
3559 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, StreamsTest) {
3560 RestorePrerenderMode restore_prerender_mode;
3561 PrerenderManager::SetMode(
3562 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3564 ASSERT_TRUE(StartSpawnedTestServer());
3566 const extensions::Extension* extension = LoadExtension(
3567 test_data_dir_.AppendASCII("streams_private/handle_mime_type"));
3568 ASSERT_TRUE(extension);
3569 EXPECT_EQ(std::string(extension_misc::kMimeHandlerPrivateTestExtensionId),
3570 extension->id());
3571 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
3572 ASSERT_TRUE(handler);
3573 EXPECT_TRUE(handler->CanHandleMIMEType("application/msword"));
3575 PrerenderTestURL("files/prerender/document.doc", FINAL_STATUS_DOWNLOAD, 0);
3577 // Sanity-check that the extension would have picked up the stream in a normal
3578 // navigation had prerender not intercepted it.
3579 // streams_private/handle_mime_type reports success if it has handled the
3580 // application/msword type.
3581 extensions::ResultCatcher catcher;
3582 NavigateToDestURL();
3583 EXPECT_TRUE(catcher.GetNextResult());
3586 // Checks that non-http/https/chrome-extension subresource cancels the
3587 // prerender.
3588 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3589 PrerenderCancelSubresourceUnsupportedScheme) {
3590 GURL image_url = GURL("invalidscheme://www.google.com/test.jpg");
3591 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3592 replacement_text.push_back(
3593 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3594 std::string replacement_path;
3595 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3596 "files/prerender/prerender_with_image.html",
3597 replacement_text,
3598 &replacement_path));
3599 PrerenderTestURL(replacement_path, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3602 // Ensure that about:blank is permitted for any subresource.
3603 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3604 PrerenderAllowAboutBlankSubresource) {
3605 GURL image_url = GURL("about:blank");
3606 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3607 replacement_text.push_back(
3608 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3609 std::string replacement_path;
3610 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3611 "files/prerender/prerender_with_image.html",
3612 replacement_text,
3613 &replacement_path));
3614 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3615 NavigateToDestURL();
3618 // Checks that non-http/https/chrome-extension subresource cancels the prerender
3619 // on redirect.
3620 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3621 PrerenderCancelSubresourceRedirectUnsupportedScheme) {
3622 GURL image_url = test_server()->GetURL(
3623 CreateServerRedirect("invalidscheme://www.google.com/test.jpg"));
3624 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3625 replacement_text.push_back(
3626 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3627 std::string replacement_path;
3628 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3629 "files/prerender/prerender_with_image.html",
3630 replacement_text,
3631 &replacement_path));
3632 PrerenderTestURL(replacement_path, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3635 // Checks that chrome-extension subresource does not cancel the prerender.
3636 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3637 PrerenderKeepSubresourceExtensionScheme) {
3638 GURL image_url = GURL("chrome-extension://abcdefg/test.jpg");
3639 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3640 replacement_text.push_back(
3641 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3642 std::string replacement_path;
3643 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3644 "files/prerender/prerender_with_image.html",
3645 replacement_text,
3646 &replacement_path));
3647 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3648 NavigateToDestURL();
3651 // Checks that redirect to chrome-extension subresource does not cancel the
3652 // prerender.
3653 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3654 PrerenderKeepSubresourceRedirectExtensionScheme) {
3655 GURL image_url = test_server()->GetURL(
3656 CreateServerRedirect("chrome-extension://abcdefg/test.jpg"));
3657 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3658 replacement_text.push_back(
3659 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3660 std::string replacement_path;
3661 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3662 "files/prerender/prerender_with_image.html",
3663 replacement_text,
3664 &replacement_path));
3665 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3666 NavigateToDestURL();
3669 // Checks that non-http/https main page redirects cancel the prerender.
3670 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3671 PrerenderCancelMainFrameRedirectUnsupportedScheme) {
3672 GURL url = test_server()->GetURL(
3673 CreateServerRedirect("invalidscheme://www.google.com/test.html"));
3674 PrerenderTestURL(url, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3677 // Checks that media source video loads are deferred on prerendering.
3678 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5MediaSourceVideo) {
3679 PrerenderTestURL("files/prerender/prerender_html5_video_media_source.html",
3680 FINAL_STATUS_USED,
3682 NavigateToDestURL();
3683 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
3686 // Checks that a prerender that creates an audio stream (via a WebAudioDevice)
3687 // is cancelled.
3688 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWebAudioDevice) {
3689 DisableLoadEventCheck();
3690 PrerenderTestURL("files/prerender/prerender_web_audio_device.html",
3691 FINAL_STATUS_CREATING_AUDIO_STREAM, 0);
3694 // Checks that prerenders do not swap in to WebContents being captured.
3695 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCapturedWebContents) {
3696 PrerenderTestURL("files/prerender/prerender_page.html",
3697 FINAL_STATUS_PAGE_BEING_CAPTURED, 1);
3698 WebContents* web_contents = GetActiveWebContents();
3699 web_contents->IncrementCapturerCount(gfx::Size());
3700 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3701 web_contents->DecrementCapturerCount();
3704 // Checks that prerenders are aborted on cross-process navigation from
3705 // a server redirect.
3706 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3707 PrerenderCrossProcessServerRedirect) {
3708 // Force everything to be a process swap.
3709 SwapProcessesContentBrowserClient test_browser_client;
3710 content::ContentBrowserClient* original_browser_client =
3711 content::SetBrowserClientForTesting(&test_browser_client);
3713 PrerenderTestURL(
3714 CreateServerRedirect("files/prerender/prerender_page.html"),
3715 FINAL_STATUS_OPEN_URL, 0);
3717 content::SetBrowserClientForTesting(original_browser_client);
3720 // Checks that URLRequests for prerenders being aborted on cross-process
3721 // navigation from a server redirect are cleaned up, so they don't keep cache
3722 // entries locked.
3723 // See http://crbug.com/341134
3724 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3725 PrerenderCrossProcessServerRedirectNoHang) {
3726 const char kDestPath[] = "files/prerender/prerender_page.html";
3727 // Force everything to be a process swap.
3728 SwapProcessesContentBrowserClient test_browser_client;
3729 content::ContentBrowserClient* original_browser_client =
3730 content::SetBrowserClientForTesting(&test_browser_client);
3732 PrerenderTestURL(CreateServerRedirect(kDestPath), FINAL_STATUS_OPEN_URL, 0);
3734 ui_test_utils::NavigateToURL(
3735 browser(),
3736 test_server()->GetURL(kDestPath));
3738 content::SetBrowserClientForTesting(original_browser_client);
3741 // Checks that prerenders are aborted on cross-process navigation from
3742 // a client redirect.
3743 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3744 PrerenderCrossProcessClientRedirect) {
3745 // Cross-process navigation logic for renderer-initiated navigations
3746 // is partially controlled by the renderer, namely
3747 // ChromeContentRendererClient. This test instead relies on the Web
3748 // Store triggering such navigations.
3749 std::string webstore_url = extension_urls::GetWebstoreLaunchURL();
3751 // Mock out requests to the Web Store.
3752 base::FilePath file(GetTestPath("prerender_page.html"));
3753 BrowserThread::PostTask(
3754 BrowserThread::IO, FROM_HERE,
3755 base::Bind(&CreateMockInterceptorOnIO, GURL(webstore_url), file));
3757 PrerenderTestURL(CreateClientRedirect(webstore_url),
3758 FINAL_STATUS_OPEN_URL, 1);
3761 // Checks that canceling a MatchComplete dummy doesn't result in two
3762 // stop events.
3763 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, CancelMatchCompleteDummy) {
3764 RestorePrerenderMode restore_prerender_mode;
3765 PrerenderManager::SetMode(
3766 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3768 std::vector<FinalStatus> expected_final_status_queue;
3769 expected_final_status_queue.push_back(FINAL_STATUS_JAVASCRIPT_ALERT);
3770 expected_final_status_queue.push_back(FINAL_STATUS_CANCELLED);
3771 ScopedVector<TestPrerender> prerenders =
3772 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
3773 expected_final_status_queue, 0);
3775 // Cancel the MatchComplete dummy.
3776 GetPrerenderManager()->CancelAllPrerenders();
3777 prerenders[1]->WaitForStop();
3779 // Check the referring page only got one copy of the event.
3780 EXPECT_FALSE(HadPrerenderEventErrors());
3783 // Checks that a deferred redirect to an image is not loaded until the page is
3784 // visible. Also test the right histogram events are emitted in this case.
3785 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredImage) {
3786 DisableJavascriptCalls();
3788 // The prerender will not completely load until after the swap, so wait for a
3789 // title change before calling DidPrerenderPass.
3790 scoped_ptr<TestPrerender> prerender =
3791 PrerenderTestURL(
3792 "files/prerender/prerender_deferred_image.html",
3793 FINAL_STATUS_USED, 0);
3794 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
3795 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
3796 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
3797 EXPECT_EQ(0, prerender->number_of_loads());
3798 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3799 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3800 histogram_tester().ExpectTotalCount(
3801 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3802 histogram_tester().ExpectTotalCount(
3803 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3805 // Swap.
3806 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
3807 GetActiveWebContents());
3808 ui_test_utils::NavigateToURLWithDisposition(
3809 current_browser(), dest_url(), CURRENT_TAB,
3810 ui_test_utils::BROWSER_TEST_NONE);
3811 swap_observer.Wait();
3813 // The prerender never observes the final load.
3814 EXPECT_EQ(0, prerender->number_of_loads());
3816 // Now check DidDisplayPass.
3817 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3819 histogram_tester().ExpectTotalCount(
3820 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3821 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3822 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3824 histogram_tester().ExpectTotalCount(
3825 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3828 // Checks that a deferred redirect to an image is not loaded until the
3829 // page is visible, even after another redirect.
3830 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3831 PrerenderDeferredImageAfterRedirect) {
3832 DisableJavascriptCalls();
3834 // The prerender will not completely load until after the swap, so wait for a
3835 // title change before calling DidPrerenderPass.
3836 scoped_ptr<TestPrerender> prerender =
3837 PrerenderTestURL(
3838 "files/prerender/prerender_deferred_image.html",
3839 FINAL_STATUS_USED, 0);
3840 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
3841 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
3842 EXPECT_EQ(0, prerender->number_of_loads());
3844 // Swap.
3845 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
3846 GetActiveWebContents());
3847 ui_test_utils::NavigateToURLWithDisposition(
3848 current_browser(), dest_url(), CURRENT_TAB,
3849 ui_test_utils::BROWSER_TEST_NONE);
3850 swap_observer.Wait();
3852 // The prerender never observes the final load.
3853 EXPECT_EQ(0, prerender->number_of_loads());
3855 // Now check DidDisplayPass.
3856 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3859 // Checks that deferred redirects in the main frame are followed.
3860 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredMainFrame) {
3861 DisableJavascriptCalls();
3862 PrerenderTestURL(
3863 "files/prerender/image-deferred.png",
3864 FINAL_STATUS_USED, 1);
3865 NavigateToDestURL();
3868 // Checks that deferred redirects in the main frame are followed, even
3869 // with a double-redirect.
3870 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3871 PrerenderDeferredMainFrameAfterRedirect) {
3872 DisableJavascriptCalls();
3873 PrerenderTestURL(
3874 CreateServerRedirect("files/prerender/image-deferred.png"),
3875 FINAL_STATUS_USED, 1);
3876 NavigateToDestURL();
3879 // Checks that deferred redirects in a synchronous XHR abort the
3880 // prerender.
3881 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredSynchronousXHR) {
3882 RestorePrerenderMode restore_prerender_mode;
3883 PrerenderManager::SetMode(
3884 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3885 PrerenderTestURL("files/prerender/prerender_deferred_sync_xhr.html",
3886 FINAL_STATUS_BAD_DEFERRED_REDIRECT, 0);
3887 NavigateToDestURL();
3890 // Checks that prerenders are not swapped for navigations with extra headers.
3891 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExtraHeadersNoSwap) {
3892 PrerenderTestURL("files/prerender/prerender_page.html",
3893 FINAL_STATUS_APP_TERMINATING, 1);
3895 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3896 ui::PAGE_TRANSITION_TYPED, false);
3897 params.extra_headers = "X-Custom-Header: 42\r\n";
3898 NavigateToURLWithParams(params, false);
3901 // Checks that prerenders are not swapped for navigations with browser-initiated
3902 // POST data.
3903 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3904 PrerenderBrowserInitiatedPostNoSwap) {
3905 PrerenderTestURL("files/prerender/prerender_page.html",
3906 FINAL_STATUS_APP_TERMINATING, 1);
3908 std::string post_data = "DATA";
3909 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3910 ui::PAGE_TRANSITION_TYPED, false);
3911 params.uses_post = true;
3912 params.browser_initiated_post_data =
3913 base::RefCountedString::TakeString(&post_data);
3914 NavigateToURLWithParams(params, false);
3917 // Checks that the prerendering of a page is canceled correctly when the
3918 // prerendered page tries to make a second navigation entry.
3919 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNewNavigationEntry) {
3920 PrerenderTestURL("files/prerender/prerender_new_entry.html",
3921 FINAL_STATUS_NEW_NAVIGATION_ENTRY,
3925 // Attempt a swap-in in a new tab. The session storage doesn't match, so it
3926 // should not swap.
3927 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageNewTab) {
3928 PrerenderTestURL("files/prerender/prerender_page.html",
3929 FINAL_STATUS_APP_TERMINATING, 1);
3931 // Open a new tab to navigate in.
3932 ui_test_utils::NavigateToURLWithDisposition(
3933 current_browser(), GURL(url::kAboutBlankURL), NEW_FOREGROUND_TAB,
3934 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
3936 // Now navigate in the new tab.
3937 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3940 // Checks that prerenders honor |should_replace_current_entry|.
3941 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReplaceCurrentEntry) {
3942 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3944 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3945 ui::PAGE_TRANSITION_TYPED, false);
3946 params.should_replace_current_entry = true;
3947 NavigateToURLWithParams(params, false);
3949 const NavigationController& controller =
3950 GetActiveWebContents()->GetController();
3951 // First entry is about:blank, second is prerender_page.html.
3952 EXPECT_TRUE(controller.GetPendingEntry() == NULL);
3953 EXPECT_EQ(2, controller.GetEntryCount());
3954 EXPECT_EQ(GURL(url::kAboutBlankURL), controller.GetEntryAtIndex(0)->GetURL());
3955 EXPECT_EQ(dest_url(), controller.GetEntryAtIndex(1)->GetURL());
3958 // Checks that <a ping> requests are not dropped in prerender.
3959 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPing) {
3960 // Count hits to a certain URL.
3961 const GURL kPingURL("http://prerender.test/ping");
3962 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3963 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3964 RequestCounter ping_counter;
3965 BrowserThread::PostTask(
3966 BrowserThread::IO, FROM_HERE,
3967 base::Bind(&CreateCountingInterceptorOnIO,
3968 kPingURL, empty_file, ping_counter.AsWeakPtr()));
3970 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3971 OpenDestURLViaClickPing(kPingURL);
3973 ping_counter.WaitForCount(1);
3976 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPPLTNormalNavigation) {
3977 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3978 ui_test_utils::NavigateToURL(current_browser(), url);
3979 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3980 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3981 histogram_tester().ExpectTotalCount(
3982 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3985 // Checks that a prerender which calls window.close() on itself is aborted.
3986 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWindowClose) {
3987 DisableLoadEventCheck();
3988 PrerenderTestURL("files/prerender/prerender_window_close.html",
3989 FINAL_STATUS_CLOSED, 0);
3992 class PrerenderIncognitoBrowserTest : public PrerenderBrowserTest {
3993 public:
3994 void SetUpOnMainThread() override {
3995 Profile* normal_profile = current_browser()->profile();
3996 set_browser(ui_test_utils::OpenURLOffTheRecord(
3997 normal_profile, GURL("about:blank")));
3998 PrerenderBrowserTest::SetUpOnMainThread();
4002 // Checks that prerendering works in incognito mode.
4003 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest, PrerenderIncognito) {
4004 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
4005 NavigateToDestURL();
4008 // Checks that prerenders are aborted when an incognito profile is closed.
4009 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest,
4010 PrerenderIncognitoClosed) {
4011 scoped_ptr<TestPrerender> prerender =
4012 PrerenderTestURL("files/prerender/prerender_page.html",
4013 FINAL_STATUS_PROFILE_DESTROYED, 1);
4014 current_browser()->window()->Close();
4015 prerender->WaitForStop();
4018 class PrerenderOmniboxBrowserTest : public PrerenderBrowserTest {
4019 public:
4020 LocationBar* GetLocationBar() {
4021 return current_browser()->window()->GetLocationBar();
4024 OmniboxView* GetOmniboxView() {
4025 return GetLocationBar()->GetOmniboxView();
4028 void WaitForAutocompleteDone(OmniboxView* omnibox_view) {
4029 AutocompleteController* controller =
4030 omnibox_view->model()->popup_model()->autocomplete_controller();
4031 while (!controller->done()) {
4032 content::WindowedNotificationObserver ready_observer(
4033 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
4034 content::Source<AutocompleteController>(controller));
4035 ready_observer.Wait();
4039 predictors::AutocompleteActionPredictor* GetAutocompleteActionPredictor() {
4040 Profile* profile = current_browser()->profile();
4041 return predictors::AutocompleteActionPredictorFactory::GetForProfile(
4042 profile);
4045 scoped_ptr<TestPrerender> StartOmniboxPrerender(
4046 const GURL& url,
4047 FinalStatus expected_final_status) {
4048 scoped_ptr<TestPrerender> prerender =
4049 ExpectPrerender(expected_final_status);
4050 WebContents* web_contents = GetActiveWebContents();
4051 GetAutocompleteActionPredictor()->StartPrerendering(
4052 url,
4053 web_contents->GetController().GetDefaultSessionStorageNamespace(),
4054 gfx::Size(50, 50));
4055 prerender->WaitForStart();
4056 return prerender.Pass();
4060 // Checks that closing the omnibox popup cancels an omnibox prerender.
4061 // http://crbug.com/395152
4062 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest,
4063 DISABLED_PrerenderOmniboxCancel) {
4064 // Fake an omnibox prerender.
4065 scoped_ptr<TestPrerender> prerender = StartOmniboxPrerender(
4066 test_server()->GetURL("files/empty.html"),
4067 FINAL_STATUS_CANCELLED);
4069 // Revert the location bar. This should cancel the prerender.
4070 GetLocationBar()->Revert();
4071 prerender->WaitForStop();
4074 // Checks that accepting omnibox input abandons an omnibox prerender.
4075 // http://crbug.com/394592
4076 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest,
4077 DISABLED_PrerenderOmniboxAbandon) {
4078 // Set the abandon timeout to something high so it does not introduce
4079 // flakiness if the prerender times out before the test completes.
4080 GetPrerenderManager()->mutable_config().abandon_time_to_live =
4081 base::TimeDelta::FromDays(999);
4083 // Enter a URL into the Omnibox.
4084 OmniboxView* omnibox_view = GetOmniboxView();
4085 omnibox_view->OnBeforePossibleChange();
4086 omnibox_view->SetUserText(
4087 base::UTF8ToUTF16(test_server()->GetURL("files/empty.html?1").spec()));
4088 omnibox_view->OnAfterPossibleChange();
4089 WaitForAutocompleteDone(omnibox_view);
4091 // Fake an omnibox prerender for a different URL.
4092 scoped_ptr<TestPrerender> prerender = StartOmniboxPrerender(
4093 test_server()->GetURL("files/empty.html?2"),
4094 FINAL_STATUS_APP_TERMINATING);
4096 // The final status may be either FINAL_STATUS_APP_TERMINATING or
4097 // FINAL_STATUS_CANCELLED. Although closing the omnibox will not cancel an
4098 // abandoned prerender, the AutocompleteActionPredictor will cancel the
4099 // predictor on destruction.
4100 prerender->contents()->set_skip_final_checks(true);
4102 // Navigate to the URL entered.
4103 omnibox_view->model()->AcceptInput(CURRENT_TAB, false);
4105 // Prerender should be running, but abandoned.
4106 EXPECT_TRUE(
4107 GetAutocompleteActionPredictor()->IsPrerenderAbandonedForTesting());
4110 // Prefetch should be allowed depending on preference and network type.
4111 // This test is for the bsae case: no Finch overrides should never disable.
4112 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
4113 LocalPredictorDisableWorksBaseCase) {
4114 TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
4115 false /*preference_wifi_network_wifi*/,
4116 false /*preference_wifi_network_4g*/,
4117 false /*preference_always_network_wifi*/,
4118 false /*preference_always_network_4g*/,
4119 false /*preference_never_network_wifi*/,
4120 false /*preference_never_network_4g*/);
4123 } // namespace prerender