Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_browsertest.cc
blob94c238160bd678e0750bfe876694b4bfc393e670
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/macros.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/run_loop.h"
18 #include "base/scoped_observer.h"
19 #include "base/stl_util.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/test/histogram_tester.h"
24 #include "base/test/test_timeouts.h"
25 #include "base/values.h"
26 #include "chrome/browser/browsing_data/browsing_data_helper.h"
27 #include "chrome/browser/browsing_data/browsing_data_remover.h"
28 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
29 #include "chrome/browser/chrome_content_browser_client.h"
30 #include "chrome/browser/chrome_notification_types.h"
31 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
32 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
33 #include "chrome/browser/extensions/extension_apitest.h"
34 #include "chrome/browser/external_protocol/external_protocol_handler.h"
35 #include "chrome/browser/net/prediction_options.h"
36 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
37 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
38 #include "chrome/browser/prerender/prerender_contents.h"
39 #include "chrome/browser/prerender/prerender_field_trial.h"
40 #include "chrome/browser/prerender/prerender_handle.h"
41 #include "chrome/browser/prerender/prerender_link_manager.h"
42 #include "chrome/browser/prerender/prerender_link_manager_factory.h"
43 #include "chrome/browser/prerender/prerender_manager.h"
44 #include "chrome/browser/prerender/prerender_manager_factory.h"
45 #include "chrome/browser/profiles/profile.h"
46 #include "chrome/browser/profiles/profile_io_data.h"
47 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
48 #include "chrome/browser/safe_browsing/database_manager.h"
49 #include "chrome/browser/safe_browsing/local_database_manager.h"
50 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
51 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
52 #include "chrome/browser/safe_browsing/test_database_manager.h"
53 #include "chrome/browser/task_management/providers/web_contents/web_contents_tags_manager.h"
54 #include "chrome/browser/task_management/task_management_browsertest_util.h"
55 #include "chrome/browser/task_manager/task_manager.h"
56 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
57 #include "chrome/browser/ui/browser.h"
58 #include "chrome/browser/ui/browser_commands.h"
59 #include "chrome/browser/ui/browser_finder.h"
60 #include "chrome/browser/ui/browser_navigator.h"
61 #include "chrome/browser/ui/browser_window.h"
62 #include "chrome/browser/ui/location_bar/location_bar.h"
63 #include "chrome/browser/ui/tabs/tab_strip_model.h"
64 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
65 #include "chrome/common/chrome_paths.h"
66 #include "chrome/common/chrome_switches.h"
67 #include "chrome/common/pref_names.h"
68 #include "chrome/grit/generated_resources.h"
69 #include "chrome/test/base/in_process_browser_test.h"
70 #include "chrome/test/base/test_switches.h"
71 #include "chrome/test/base/ui_test_utils.h"
72 #include "components/content_settings/core/browser/host_content_settings_map.h"
73 #include "components/favicon/content/content_favicon_driver.h"
74 #include "components/favicon/core/favicon_driver_observer.h"
75 #include "components/omnibox/browser/omnibox_edit_model.h"
76 #include "components/omnibox/browser/omnibox_popup_model.h"
77 #include "components/omnibox/browser/omnibox_view.h"
78 #include "components/variations/entropy_provider.h"
79 #include "components/variations/variations_associated_data.h"
80 #include "content/public/browser/browser_message_filter.h"
81 #include "content/public/browser/devtools_agent_host.h"
82 #include "content/public/browser/navigation_controller.h"
83 #include "content/public/browser/navigation_entry.h"
84 #include "content/public/browser/notification_service.h"
85 #include "content/public/browser/render_frame_host.h"
86 #include "content/public/browser/render_process_host.h"
87 #include "content/public/browser/render_view_host.h"
88 #include "content/public/browser/site_instance.h"
89 #include "content/public/browser/web_contents.h"
90 #include "content/public/browser/web_contents_observer.h"
91 #include "content/public/common/url_constants.h"
92 #include "content/public/test/browser_test_utils.h"
93 #include "content/public/test/ppapi_test_utils.h"
94 #include "content/public/test/test_navigation_observer.h"
95 #include "content/public/test/test_utils.h"
96 #include "extensions/common/constants.h"
97 #include "extensions/common/extension_urls.h"
98 #include "extensions/common/manifest_handlers/mime_types_handler.h"
99 #include "extensions/common/switches.h"
100 #include "extensions/test/result_catcher.h"
101 #include "net/base/escape.h"
102 #include "net/cert/x509_certificate.h"
103 #include "net/dns/mock_host_resolver.h"
104 #include "net/ssl/client_cert_store.h"
105 #include "net/ssl/ssl_cert_request_info.h"
106 #include "net/test/url_request/url_request_mock_http_job.h"
107 #include "net/url_request/url_request_context.h"
108 #include "net/url_request/url_request_context_getter.h"
109 #include "net/url_request/url_request_filter.h"
110 #include "net/url_request/url_request_interceptor.h"
111 #include "net/url_request/url_request_job.h"
112 #include "ppapi/shared_impl/ppapi_switches.h"
113 #include "ui/base/l10n/l10n_util.h"
114 #include "url/gurl.h"
116 using chrome_browser_net::NetworkPredictionOptions;
117 using content::BrowserThread;
118 using content::DevToolsAgentHost;
119 using content::NavigationController;
120 using content::OpenURLParams;
121 using content::Referrer;
122 using content::RenderFrameHost;
123 using content::RenderViewHost;
124 using content::RenderWidgetHost;
125 using content::TestNavigationObserver;
126 using content::WebContents;
127 using content::WebContentsObserver;
128 using net::NetworkChangeNotifier;
129 using task_manager::browsertest_util::WaitForTaskManagerRows;
131 // Prerender tests work as follows:
133 // A page with a prefetch link to the test page is loaded. Once prerendered,
134 // its Javascript function DidPrerenderPass() is called, which returns true if
135 // the page behaves as expected when prerendered.
137 // The prerendered page is then displayed on a tab. The Javascript function
138 // DidDisplayPass() is called, and returns true if the page behaved as it
139 // should while being displayed.
141 namespace prerender {
143 namespace {
145 class FaviconUpdateWatcher : public favicon::FaviconDriverObserver {
146 public:
147 explicit FaviconUpdateWatcher(content::WebContents* web_contents)
148 : seen_(false), running_(false), scoped_observer_(this) {
149 scoped_observer_.Add(
150 favicon::ContentFaviconDriver::FromWebContents(web_contents));
153 void Wait() {
154 if (seen_)
155 return;
157 running_ = true;
158 message_loop_runner_ = new content::MessageLoopRunner;
159 message_loop_runner_->Run();
162 private:
163 void OnFaviconAvailable(const gfx::Image& image) override {}
164 void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
165 bool icon_url_changed) override {
166 seen_ = true;
167 if (!running_)
168 return;
170 message_loop_runner_->Quit();
171 running_ = false;
174 bool seen_;
175 bool running_;
176 ScopedObserver<favicon::FaviconDriver, FaviconUpdateWatcher> scoped_observer_;
177 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
179 DISALLOW_COPY_AND_ASSIGN(FaviconUpdateWatcher);
182 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
183 public:
184 ConnectionType GetCurrentConnectionType() const override {
185 return NetworkChangeNotifier::CONNECTION_WIFI;
189 class MockNetworkChangeNotifier4G : public NetworkChangeNotifier {
190 public:
191 ConnectionType GetCurrentConnectionType() const override {
192 return NetworkChangeNotifier::CONNECTION_4G;
196 // Constants used in the test HTML files.
197 const char* kReadyTitle = "READY";
198 const char* kPassTitle = "PASS";
200 std::string CreateClientRedirect(const std::string& dest_url) {
201 const char* const kClientRedirectBase = "client-redirect?";
202 return kClientRedirectBase + net::EscapeQueryParamValue(dest_url, false);
205 std::string CreateServerRedirect(const std::string& dest_url) {
206 const char* const kServerRedirectBase = "server-redirect?";
207 return kServerRedirectBase + net::EscapeQueryParamValue(dest_url, false);
210 // Clears the specified data using BrowsingDataRemover.
211 void ClearBrowsingData(Browser* browser, int remove_mask) {
212 BrowsingDataRemover* remover =
213 BrowsingDataRemover::CreateForUnboundedRange(browser->profile());
214 BrowsingDataRemoverCompletionObserver observer(remover);
215 remover->Remove(remove_mask, BrowsingDataHelper::UNPROTECTED_WEB);
216 observer.BlockUntilCompletion();
217 // BrowsingDataRemover deletes itself.
220 // Returns true if the prerender is expected to abort on its own, before
221 // attempting to swap it.
222 bool ShouldAbortPrerenderBeforeSwap(FinalStatus status) {
223 switch (status) {
224 case FINAL_STATUS_USED:
225 case FINAL_STATUS_WINDOW_OPENER:
226 case FINAL_STATUS_APP_TERMINATING:
227 case FINAL_STATUS_PROFILE_DESTROYED:
228 case FINAL_STATUS_CACHE_OR_HISTORY_CLEARED:
229 // We'll crash the renderer after it's loaded.
230 case FINAL_STATUS_RENDERER_CRASHED:
231 case FINAL_STATUS_CANCELLED:
232 case FINAL_STATUS_DEVTOOLS_ATTACHED:
233 case FINAL_STATUS_PAGE_BEING_CAPTURED:
234 case FINAL_STATUS_NAVIGATION_UNCOMMITTED:
235 case FINAL_STATUS_WOULD_HAVE_BEEN_USED:
236 case FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE:
237 return false;
238 default:
239 return true;
243 // Convenience function to wait for a title. Handles the case when the
244 // WebContents already has the expected title.
245 void WaitForASCIITitle(WebContents* web_contents,
246 const char* expected_title_ascii) {
247 base::string16 expected_title = base::ASCIIToUTF16(expected_title_ascii);
248 if (web_contents->GetTitle() == expected_title)
249 return;
250 content::TitleWatcher title_watcher(web_contents, expected_title);
251 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
254 // Waits for the destruction of a RenderProcessHost's IPC channel.
255 // Used to make sure the PrerenderLinkManager's OnChannelClosed function has
256 // been called, before checking its state.
257 class ChannelDestructionWatcher {
258 public:
259 ChannelDestructionWatcher() : channel_destroyed_(false) {
262 ~ChannelDestructionWatcher() {
265 void WatchChannel(content::RenderProcessHost* host) {
266 host->AddFilter(new DestructionMessageFilter(this));
269 void WaitForChannelClose() {
270 run_loop_.Run();
271 EXPECT_TRUE(channel_destroyed_);
274 private:
275 // When destroyed, calls ChannelDestructionWatcher::OnChannelDestroyed.
276 // Ignores all messages.
277 class DestructionMessageFilter : public content::BrowserMessageFilter {
278 public:
279 explicit DestructionMessageFilter(ChannelDestructionWatcher* watcher)
280 : BrowserMessageFilter(0),
281 watcher_(watcher) {
284 private:
285 ~DestructionMessageFilter() override {
286 content::BrowserThread::PostTask(
287 content::BrowserThread::UI, FROM_HERE,
288 base::Bind(&ChannelDestructionWatcher::OnChannelDestroyed,
289 base::Unretained(watcher_)));
292 bool OnMessageReceived(const IPC::Message& message) override {
293 return false;
296 ChannelDestructionWatcher* watcher_;
298 DISALLOW_COPY_AND_ASSIGN(DestructionMessageFilter);
301 void OnChannelDestroyed() {
302 DCHECK_CURRENTLY_ON(BrowserThread::UI);
304 EXPECT_FALSE(channel_destroyed_);
305 channel_destroyed_ = true;
306 run_loop_.Quit();
309 bool channel_destroyed_;
310 base::RunLoop run_loop_;
312 DISALLOW_COPY_AND_ASSIGN(ChannelDestructionWatcher);
315 // A navigation observer to wait on either a new load or a swap of a
316 // WebContents. On swap, if the new WebContents is still loading, wait for that
317 // load to complete as well. Note that the load must begin after the observer is
318 // attached.
319 class NavigationOrSwapObserver : public WebContentsObserver,
320 public TabStripModelObserver {
321 public:
322 // Waits for either a new load or a swap of |tab_strip_model|'s active
323 // WebContents.
324 NavigationOrSwapObserver(TabStripModel* tab_strip_model,
325 WebContents* web_contents)
326 : WebContentsObserver(web_contents),
327 tab_strip_model_(tab_strip_model),
328 did_start_loading_(false),
329 number_of_loads_(1) {
330 CHECK_NE(TabStripModel::kNoTab,
331 tab_strip_model->GetIndexOfWebContents(web_contents));
332 tab_strip_model_->AddObserver(this);
335 // Waits for either |number_of_loads| loads or a swap of |tab_strip_model|'s
336 // active WebContents.
337 NavigationOrSwapObserver(TabStripModel* tab_strip_model,
338 WebContents* web_contents,
339 int number_of_loads)
340 : WebContentsObserver(web_contents),
341 tab_strip_model_(tab_strip_model),
342 did_start_loading_(false),
343 number_of_loads_(number_of_loads) {
344 CHECK_NE(TabStripModel::kNoTab,
345 tab_strip_model->GetIndexOfWebContents(web_contents));
346 tab_strip_model_->AddObserver(this);
349 ~NavigationOrSwapObserver() override {
350 tab_strip_model_->RemoveObserver(this);
353 void set_did_start_loading() {
354 did_start_loading_ = true;
357 void Wait() {
358 loop_.Run();
361 // WebContentsObserver implementation:
362 void DidStartLoading() override { did_start_loading_ = true; }
363 void DidStopLoading() override {
364 if (!did_start_loading_)
365 return;
366 number_of_loads_--;
367 if (number_of_loads_ == 0)
368 loop_.Quit();
371 // TabStripModelObserver implementation:
372 void TabReplacedAt(TabStripModel* tab_strip_model,
373 WebContents* old_contents,
374 WebContents* new_contents,
375 int index) override {
376 if (old_contents != web_contents())
377 return;
378 // Switch to observing the new WebContents.
379 Observe(new_contents);
380 if (new_contents->IsLoading()) {
381 // If the new WebContents is still loading, wait for it to complete. Only
382 // one load post-swap is supported.
383 did_start_loading_ = true;
384 number_of_loads_ = 1;
385 } else {
386 loop_.Quit();
390 private:
391 TabStripModel* tab_strip_model_;
392 bool did_start_loading_;
393 int number_of_loads_;
394 base::RunLoop loop_;
397 // Waits for a new tab to open and a navigation or swap in it.
398 class NewTabNavigationOrSwapObserver {
399 public:
400 NewTabNavigationOrSwapObserver()
401 : new_tab_observer_(
402 chrome::NOTIFICATION_TAB_ADDED,
403 base::Bind(&NewTabNavigationOrSwapObserver::OnTabAdded,
404 base::Unretained(this))) {
405 // Watch for NOTIFICATION_TAB_ADDED. Add a callback so that the
406 // NavigationOrSwapObserver can be attached synchronously and no events are
407 // missed.
410 void Wait() {
411 new_tab_observer_.Wait();
412 swap_observer_->Wait();
415 bool OnTabAdded(const content::NotificationSource& source,
416 const content::NotificationDetails& details) {
417 if (swap_observer_)
418 return true;
419 WebContents* new_tab = content::Details<WebContents>(details).ptr();
420 // Get the TabStripModel. Assume this is attached to a Browser.
421 TabStripModel* tab_strip_model =
422 static_cast<Browser*>(new_tab->GetDelegate())->tab_strip_model();
423 swap_observer_.reset(new NavigationOrSwapObserver(tab_strip_model,
424 new_tab));
425 swap_observer_->set_did_start_loading();
426 return true;
429 private:
430 content::WindowedNotificationObserver new_tab_observer_;
431 scoped_ptr<NavigationOrSwapObserver> swap_observer_;
434 // PrerenderContents that stops the UI message loop on DidStopLoading().
435 class TestPrerenderContents : public PrerenderContents {
436 public:
437 TestPrerenderContents(
438 PrerenderManager* prerender_manager,
439 Profile* profile,
440 const GURL& url,
441 const content::Referrer& referrer,
442 Origin origin,
443 FinalStatus expected_final_status)
444 : PrerenderContents(prerender_manager, profile, url, referrer, origin),
445 expected_final_status_(expected_final_status),
446 new_render_view_host_(NULL),
447 was_hidden_(false),
448 was_shown_(false),
449 should_be_shown_(expected_final_status == FINAL_STATUS_USED),
450 skip_final_checks_(false) {
453 ~TestPrerenderContents() override {
454 if (skip_final_checks_)
455 return;
457 if (expected_final_status_ == FINAL_STATUS_MAX) {
458 EXPECT_EQ(MATCH_COMPLETE_REPLACEMENT, match_complete_status());
459 } else {
460 EXPECT_EQ(expected_final_status_, final_status()) <<
461 " when testing URL " << prerender_url().path() <<
462 " (Expected: " << NameFromFinalStatus(expected_final_status_) <<
463 ", Actual: " << NameFromFinalStatus(final_status()) << ")";
465 // Prerendering RenderViewHosts should be hidden before the first
466 // navigation, so this should be happen for every PrerenderContents for
467 // which a RenderViewHost is created, regardless of whether or not it's
468 // used.
469 if (new_render_view_host_)
470 EXPECT_TRUE(was_hidden_);
472 // A used PrerenderContents will only be destroyed when we swap out
473 // WebContents, at the end of a navigation caused by a call to
474 // NavigateToURLImpl().
475 if (final_status() == FINAL_STATUS_USED)
476 EXPECT_TRUE(new_render_view_host_);
478 EXPECT_EQ(should_be_shown_, was_shown_);
481 void RenderProcessGone(base::TerminationStatus status) override {
482 // On quit, it's possible to end up here when render processes are closed
483 // before the PrerenderManager is destroyed. As a result, it's possible to
484 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
485 // on quit.
487 // It's also possible for this to be called after we've been notified of
488 // app termination, but before we've been deleted, which is why the second
489 // check is needed.
490 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING &&
491 final_status() != expected_final_status_) {
492 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED;
495 PrerenderContents::RenderProcessGone(status);
498 bool CheckURL(const GURL& url) override {
499 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
500 // the PrerenderRendererCrash test.
501 if (url.spec() != content::kChromeUICrashURL)
502 return PrerenderContents::CheckURL(url);
503 return true;
506 // For tests that open the prerender in a new background tab, the RenderView
507 // will not have been made visible when the PrerenderContents is destroyed
508 // even though it is used.
509 void set_should_be_shown(bool value) { should_be_shown_ = value; }
511 // For tests which do not know whether the prerender will be used.
512 void set_skip_final_checks(bool value) { skip_final_checks_ = value; }
514 FinalStatus expected_final_status() const { return expected_final_status_; }
516 private:
517 void OnRenderViewHostCreated(RenderViewHost* new_render_view_host) override {
518 // Used to make sure the RenderViewHost is hidden and, if used,
519 // subsequently shown.
520 notification_registrar().Add(
521 this,
522 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
523 content::Source<RenderWidgetHost>(new_render_view_host));
525 new_render_view_host_ = new_render_view_host;
527 PrerenderContents::OnRenderViewHostCreated(new_render_view_host);
530 void Observe(int type,
531 const content::NotificationSource& source,
532 const content::NotificationDetails& details) override {
533 if (type ==
534 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED) {
535 EXPECT_EQ(new_render_view_host_,
536 content::Source<RenderWidgetHost>(source).ptr());
537 bool is_visible = *content::Details<bool>(details).ptr();
539 if (!is_visible) {
540 was_hidden_ = true;
541 } else if (is_visible && was_hidden_) {
542 // Once hidden, a prerendered RenderViewHost should only be shown after
543 // being removed from the PrerenderContents for display.
544 EXPECT_FALSE(GetRenderViewHost());
545 was_shown_ = true;
547 return;
549 PrerenderContents::Observe(type, source, details);
552 FinalStatus expected_final_status_;
554 // The RenderViewHost created for the prerender, if any.
555 RenderViewHost* new_render_view_host_;
556 // Set to true when the prerendering RenderWidget is hidden.
557 bool was_hidden_;
558 // Set to true when the prerendering RenderWidget is shown, after having been
559 // hidden.
560 bool was_shown_;
561 // Expected final value of was_shown_. Defaults to true for
562 // FINAL_STATUS_USED, and false otherwise.
563 bool should_be_shown_;
564 // If true, |expected_final_status_| and other shutdown checks are skipped.
565 bool skip_final_checks_;
568 // A handle to a TestPrerenderContents whose lifetime is under the caller's
569 // control. A PrerenderContents may be destroyed at any point. This allows
570 // tracking the final status, etc.
571 class TestPrerender : public PrerenderContents::Observer,
572 public base::SupportsWeakPtr<TestPrerender> {
573 public:
574 TestPrerender()
575 : contents_(NULL),
576 number_of_loads_(0),
577 expected_number_of_loads_(0) {
579 ~TestPrerender() override {
580 if (contents_)
581 contents_->RemoveObserver(this);
584 TestPrerenderContents* contents() const { return contents_; }
585 int number_of_loads() const { return number_of_loads_; }
587 void WaitForCreate() { create_loop_.Run(); }
588 void WaitForStart() { start_loop_.Run(); }
589 void WaitForStop() { stop_loop_.Run(); }
591 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR
592 // for the prerender to stop running (just to avoid a timeout if the prerender
593 // dies). Note: this does not assert equality on the number of loads; the
594 // caller must do it instead.
595 void WaitForLoads(int expected_number_of_loads) {
596 DCHECK(!load_waiter_);
597 DCHECK(!expected_number_of_loads_);
598 if (number_of_loads_ < expected_number_of_loads) {
599 load_waiter_.reset(new base::RunLoop);
600 expected_number_of_loads_ = expected_number_of_loads;
601 load_waiter_->Run();
602 load_waiter_.reset();
603 expected_number_of_loads_ = 0;
605 EXPECT_LE(expected_number_of_loads, number_of_loads_);
608 void OnPrerenderCreated(TestPrerenderContents* contents) {
609 DCHECK(!contents_);
610 contents_ = contents;
611 contents_->AddObserver(this);
612 create_loop_.Quit();
615 // PrerenderContents::Observer implementation:
616 void OnPrerenderStart(PrerenderContents* contents) override {
617 start_loop_.Quit();
620 void OnPrerenderStopLoading(PrerenderContents* contents) override {
621 number_of_loads_++;
622 if (load_waiter_ && number_of_loads_ >= expected_number_of_loads_)
623 load_waiter_->Quit();
626 void OnPrerenderStop(PrerenderContents* contents) override {
627 DCHECK(contents_);
628 contents_ = NULL;
629 stop_loop_.Quit();
630 // If there is a WaitForLoads call and it has yet to see the expected number
631 // of loads, stop the loop so the test fails instead of timing out.
632 if (load_waiter_)
633 load_waiter_->Quit();
636 void OnPrerenderCreatedMatchCompleteReplacement(
637 PrerenderContents* contents,
638 PrerenderContents* replacement) override {}
640 private:
641 TestPrerenderContents* contents_;
642 int number_of_loads_;
644 int expected_number_of_loads_;
645 scoped_ptr<base::RunLoop> load_waiter_;
647 base::RunLoop create_loop_;
648 base::RunLoop start_loop_;
649 base::RunLoop stop_loop_;
651 DISALLOW_COPY_AND_ASSIGN(TestPrerender);
654 // PrerenderManager that uses TestPrerenderContents.
655 class TestPrerenderContentsFactory : public PrerenderContents::Factory {
656 public:
657 TestPrerenderContentsFactory() {}
659 ~TestPrerenderContentsFactory() override {
660 EXPECT_TRUE(expected_contents_queue_.empty());
663 scoped_ptr<TestPrerender> ExpectPrerenderContents(FinalStatus final_status) {
664 scoped_ptr<TestPrerender> handle(new TestPrerender());
665 expected_contents_queue_.push_back(
666 ExpectedContents(final_status, handle->AsWeakPtr()));
667 return handle.Pass();
670 PrerenderContents* CreatePrerenderContents(
671 PrerenderManager* prerender_manager,
672 Profile* profile,
673 const GURL& url,
674 const content::Referrer& referrer,
675 Origin origin) override {
676 ExpectedContents expected;
677 if (!expected_contents_queue_.empty()) {
678 expected = expected_contents_queue_.front();
679 expected_contents_queue_.pop_front();
681 VLOG(1) << "Creating prerender contents for " << url.path() <<
682 " with expected final status " << expected.final_status;
683 VLOG(1) << expected_contents_queue_.size() << " left in the queue.";
684 TestPrerenderContents* contents =
685 new TestPrerenderContents(prerender_manager,
686 profile, url, referrer, origin,
687 expected.final_status);
688 if (expected.handle)
689 expected.handle->OnPrerenderCreated(contents);
690 return contents;
693 private:
694 struct ExpectedContents {
695 ExpectedContents() : final_status(FINAL_STATUS_MAX) { }
696 ExpectedContents(FinalStatus final_status,
697 const base::WeakPtr<TestPrerender>& handle)
698 : final_status(final_status),
699 handle(handle) {
702 FinalStatus final_status;
703 base::WeakPtr<TestPrerender> handle;
706 std::deque<ExpectedContents> expected_contents_queue_;
709 // TODO(nparker): Switch this to use TestSafeBrowsingDatabaseManager and run
710 // with SAFE_BROWSING_DB_LOCAL || SAFE_BROWSING_DB_REMOTE.
711 #if defined(FULL_SAFE_BROWSING)
712 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
713 // a given URL.
714 class FakeSafeBrowsingDatabaseManager
715 : public LocalSafeBrowsingDatabaseManager {
716 public:
717 explicit FakeSafeBrowsingDatabaseManager(SafeBrowsingService* service)
718 : LocalSafeBrowsingDatabaseManager(service),
719 threat_type_(SB_THREAT_TYPE_SAFE) {}
721 // Called on the IO thread to check if the given url is safe or not. If we
722 // can synchronously determine that the url is safe, CheckUrl returns true.
723 // Otherwise it returns false, and "client" is called asynchronously with the
724 // result when it is ready.
725 // Returns true, indicating a SAFE result, unless the URL is the fixed URL
726 // specified by the user, and the user-specified result is not SAFE
727 // (in which that result will be communicated back via a call into the
728 // client, and false will be returned).
729 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl.
730 bool CheckBrowseUrl(const GURL& gurl, Client* client) override {
731 if (gurl != url_ || threat_type_ == SB_THREAT_TYPE_SAFE)
732 return true;
734 BrowserThread::PostTask(
735 BrowserThread::IO, FROM_HERE,
736 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone,
737 this, gurl, client));
738 return false;
741 void SetThreatTypeForUrl(const GURL& url, SBThreatType threat_type) {
742 url_ = url;
743 threat_type_ = threat_type;
746 private:
747 ~FakeSafeBrowsingDatabaseManager() override {}
749 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) {
750 std::vector<SBThreatType> expected_threats;
751 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
752 expected_threats.push_back(SB_THREAT_TYPE_URL_PHISHING);
753 // TODO(nparker): Replace SafeBrowsingCheck w/ a call to
754 // client->OnCheckBrowseUrlResult()
755 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check(
756 std::vector<GURL>(1, gurl),
757 std::vector<SBFullHash>(),
758 client,
759 safe_browsing_util::MALWARE,
760 expected_threats);
761 sb_check.url_results[0] = threat_type_;
762 sb_check.OnSafeBrowsingResult();
765 GURL url_;
766 SBThreatType threat_type_;
767 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
770 class FakeSafeBrowsingService : public SafeBrowsingService {
771 public:
772 FakeSafeBrowsingService() { }
774 // Returned pointer has the same lifespan as the database_manager_ refcounted
775 // object.
776 FakeSafeBrowsingDatabaseManager* fake_database_manager() {
777 return fake_database_manager_;
780 protected:
781 ~FakeSafeBrowsingService() override {}
783 SafeBrowsingDatabaseManager* CreateDatabaseManager() override {
784 fake_database_manager_ = new FakeSafeBrowsingDatabaseManager(this);
785 return fake_database_manager_;
788 private:
789 FakeSafeBrowsingDatabaseManager* fake_database_manager_;
791 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService);
794 // Factory that creates FakeSafeBrowsingService instances.
795 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
796 public:
797 TestSafeBrowsingServiceFactory() :
798 most_recent_service_(NULL) { }
799 ~TestSafeBrowsingServiceFactory() override {}
801 SafeBrowsingService* CreateSafeBrowsingService() override {
802 most_recent_service_ = new FakeSafeBrowsingService();
803 return most_recent_service_;
806 FakeSafeBrowsingService* most_recent_service() const {
807 return most_recent_service_;
810 private:
811 FakeSafeBrowsingService* most_recent_service_;
813 #endif
815 class FakeDevToolsClient : public content::DevToolsAgentHostClient {
816 public:
817 FakeDevToolsClient() {}
818 ~FakeDevToolsClient() override {}
819 void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
820 const std::string& message) override {}
821 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {}
824 class RestorePrerenderMode {
825 public:
826 RestorePrerenderMode() : prev_mode_(PrerenderManager::GetMode()) {
829 ~RestorePrerenderMode() { PrerenderManager::SetMode(prev_mode_); }
830 private:
831 PrerenderManager::PrerenderManagerMode prev_mode_;
834 // URLRequestJob (and associated handler) which hangs.
835 class HangingURLRequestJob : public net::URLRequestJob {
836 public:
837 HangingURLRequestJob(net::URLRequest* request,
838 net::NetworkDelegate* network_delegate)
839 : net::URLRequestJob(request, network_delegate) {
842 void Start() override {}
844 private:
845 ~HangingURLRequestJob() override {}
848 class HangingFirstRequestInterceptor : public net::URLRequestInterceptor {
849 public:
850 HangingFirstRequestInterceptor(const base::FilePath& file,
851 base::Closure callback)
852 : file_(file),
853 callback_(callback),
854 first_run_(true) {
856 ~HangingFirstRequestInterceptor() override {}
858 net::URLRequestJob* MaybeInterceptRequest(
859 net::URLRequest* request,
860 net::NetworkDelegate* network_delegate) const override {
861 if (first_run_) {
862 first_run_ = false;
863 if (!callback_.is_null()) {
864 BrowserThread::PostTask(
865 BrowserThread::UI, FROM_HERE, callback_);
867 return new HangingURLRequestJob(request, network_delegate);
869 return new net::URLRequestMockHTTPJob(
870 request,
871 network_delegate,
872 file_,
873 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
874 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
877 private:
878 base::FilePath file_;
879 base::Closure callback_;
880 mutable bool first_run_;
883 // Makes |url| never respond on the first load, and then with the contents of
884 // |file| afterwards. When the first load has been scheduled, runs |callback| on
885 // the UI thread.
886 void CreateHangingFirstRequestInterceptorOnIO(
887 const GURL& url, const base::FilePath& file, base::Closure callback) {
888 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
889 scoped_ptr<net::URLRequestInterceptor> never_respond_handler(
890 new HangingFirstRequestInterceptor(file, callback));
891 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
892 url, never_respond_handler.Pass());
895 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks.
896 class MockHTTPJob : public net::URLRequestMockHTTPJob {
897 public:
898 MockHTTPJob(net::URLRequest* request,
899 net::NetworkDelegate* delegate,
900 const base::FilePath& file)
901 : net::URLRequestMockHTTPJob(
902 request,
903 delegate,
904 file,
905 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
906 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
908 void set_start_callback(const base::Closure& start_callback) {
909 start_callback_ = start_callback;
912 void Start() override {
913 if (!start_callback_.is_null())
914 start_callback_.Run();
915 net::URLRequestMockHTTPJob::Start();
918 private:
919 ~MockHTTPJob() override {}
921 base::Closure start_callback_;
924 // Dummy counter class to live on the UI thread for counting requests.
925 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> {
926 public:
927 RequestCounter() : count_(0), expected_count_(-1) {}
928 int count() const { return count_; }
930 void RequestStarted() {
931 count_++;
932 if (loop_ && count_ == expected_count_)
933 loop_->Quit();
936 void WaitForCount(int expected_count) {
937 ASSERT_TRUE(!loop_);
938 ASSERT_EQ(-1, expected_count_);
939 if (count_ < expected_count) {
940 expected_count_ = expected_count;
941 loop_.reset(new base::RunLoop);
942 loop_->Run();
943 expected_count_ = -1;
944 loop_.reset();
947 EXPECT_EQ(expected_count, count_);
949 private:
950 int count_;
951 int expected_count_;
952 scoped_ptr<base::RunLoop> loop_;
955 // Protocol handler which counts the number of requests that start.
956 class CountingInterceptor : public net::URLRequestInterceptor {
957 public:
958 CountingInterceptor(const base::FilePath& file,
959 const base::WeakPtr<RequestCounter>& counter)
960 : file_(file),
961 counter_(counter),
962 weak_factory_(this) {
964 ~CountingInterceptor() override {}
966 net::URLRequestJob* MaybeInterceptRequest(
967 net::URLRequest* request,
968 net::NetworkDelegate* network_delegate) const override {
969 MockHTTPJob* job = new MockHTTPJob(request, network_delegate, file_);
970 job->set_start_callback(base::Bind(&CountingInterceptor::RequestStarted,
971 weak_factory_.GetWeakPtr()));
972 return job;
975 void RequestStarted() {
976 BrowserThread::PostTask(
977 BrowserThread::UI, FROM_HERE,
978 base::Bind(&RequestCounter::RequestStarted, counter_));
981 private:
982 base::FilePath file_;
983 base::WeakPtr<RequestCounter> counter_;
984 mutable base::WeakPtrFactory<CountingInterceptor> weak_factory_;
987 // Makes |url| respond to requests with the contents of |file|, counting the
988 // number that start in |counter|.
989 void CreateCountingInterceptorOnIO(
990 const GURL& url,
991 const base::FilePath& file,
992 const base::WeakPtr<RequestCounter>& counter) {
993 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
994 scoped_ptr<net::URLRequestInterceptor> request_interceptor(
995 new CountingInterceptor(file, counter));
996 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
997 url, request_interceptor.Pass());
1000 // Makes |url| respond to requests with the contents of |file|.
1001 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
1002 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1003 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
1004 url,
1005 net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
1006 file, BrowserThread::GetBlockingPool()));
1009 // A ContentBrowserClient that cancels all prerenderers on OpenURL.
1010 class TestContentBrowserClient : public ChromeContentBrowserClient {
1011 public:
1012 TestContentBrowserClient() {}
1013 ~TestContentBrowserClient() override {}
1015 // ChromeContentBrowserClient:
1016 bool ShouldAllowOpenURL(content::SiteInstance* site_instance,
1017 const GURL& url) override {
1018 PrerenderManagerFactory::GetForProfile(
1019 Profile::FromBrowserContext(site_instance->GetBrowserContext()))
1020 ->CancelAllPrerenders();
1021 return ChromeContentBrowserClient::ShouldAllowOpenURL(site_instance,
1022 url);
1025 private:
1026 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient);
1029 // A ContentBrowserClient that forces cross-process navigations.
1030 class SwapProcessesContentBrowserClient : public ChromeContentBrowserClient {
1031 public:
1032 SwapProcessesContentBrowserClient() {}
1033 ~SwapProcessesContentBrowserClient() override {}
1035 // ChromeContentBrowserClient:
1036 bool ShouldSwapProcessesForRedirect(
1037 content::ResourceContext* resource_context,
1038 const GURL& current_url,
1039 const GURL& new_url) override {
1040 return true;
1043 private:
1044 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient);
1047 // An ExternalProtocolHandler that blocks everything and asserts it never is
1048 // called.
1049 class NeverRunsExternalProtocolHandlerDelegate
1050 : public ExternalProtocolHandler::Delegate {
1051 public:
1052 // ExternalProtocolHandler::Delegate implementation.
1053 ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
1054 ShellIntegration::DefaultWebClientObserver* observer,
1055 const std::string& protocol) override {
1056 NOTREACHED();
1057 // This will crash, but it shouldn't get this far with BlockState::BLOCK
1058 // anyway.
1059 return NULL;
1061 ExternalProtocolHandler::BlockState GetBlockState(
1062 const std::string& scheme) override {
1063 // Block everything and fail the test.
1064 ADD_FAILURE();
1065 return ExternalProtocolHandler::BLOCK;
1067 void BlockRequest() override {}
1068 void RunExternalProtocolDialog(const GURL& url,
1069 int render_process_host_id,
1070 int routing_id,
1071 ui::PageTransition page_transition,
1072 bool has_user_gesture) override {
1073 NOTREACHED();
1075 void LaunchUrlWithoutSecurityCheck(const GURL& url) override { NOTREACHED(); }
1076 void FinishedProcessingCheck() override { NOTREACHED(); }
1079 base::FilePath GetTestPath(const std::string& file_name) {
1080 return ui_test_utils::GetTestFilePath(
1081 base::FilePath(FILE_PATH_LITERAL("prerender")),
1082 base::FilePath().AppendASCII(file_name));
1085 } // namespace
1087 class PrerenderBrowserTest : virtual public InProcessBrowserTest {
1088 public:
1089 PrerenderBrowserTest()
1090 : autostart_test_server_(true),
1091 prerender_contents_factory_(NULL),
1092 #if defined(FULL_SAFE_BROWSING)
1093 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
1094 #endif
1095 call_javascript_(true),
1096 check_load_events_(true),
1097 loader_path_("files/prerender/prerender_loader.html"),
1098 explicitly_set_browser_(NULL) {}
1100 ~PrerenderBrowserTest() override {}
1102 content::SessionStorageNamespace* GetSessionStorageNamespace() const {
1103 WebContents* web_contents = GetActiveWebContents();
1104 if (!web_contents)
1105 return NULL;
1106 return web_contents->GetController().GetDefaultSessionStorageNamespace();
1109 void SetUpInProcessBrowserTestFixture() override {
1110 #if defined(FULL_SAFE_BROWSING)
1111 SafeBrowsingService::RegisterFactory(safe_browsing_factory_.get());
1112 #endif
1115 void TearDownInProcessBrowserTestFixture() override {
1116 #if defined(FULL_SAFE_BROWSING)
1117 SafeBrowsingService::RegisterFactory(NULL);
1118 #endif
1121 void SetUpCommandLine(base::CommandLine* command_line) override {
1122 command_line->AppendSwitchASCII(switches::kPrerenderMode,
1123 switches::kPrerenderModeSwitchValueEnabled);
1124 command_line->AppendSwitch(switches::kEnablePepperTesting);
1125 command_line->AppendSwitchASCII(
1126 switches::kOverridePluginPowerSaverForTesting, "ignore-list");
1128 ASSERT_TRUE(ppapi::RegisterPowerSaverTestPlugin(command_line));
1131 void SetUpOnMainThread() override {
1132 current_browser()->profile()->GetPrefs()->SetBoolean(
1133 prefs::kPromptForDownload, false);
1134 IncreasePrerenderMemory();
1135 if (autostart_test_server_)
1136 ASSERT_TRUE(test_server()->Start());
1137 ChromeResourceDispatcherHostDelegate::
1138 SetExternalProtocolHandlerDelegateForTesting(
1139 &external_protocol_handler_delegate_);
1141 PrerenderManager* prerender_manager = GetPrerenderManager();
1142 ASSERT_TRUE(prerender_manager);
1143 prerender_manager->mutable_config().rate_limit_enabled = false;
1144 ASSERT_TRUE(prerender_contents_factory_ == NULL);
1145 prerender_contents_factory_ = new TestPrerenderContentsFactory;
1146 prerender_manager->SetPrerenderContentsFactory(prerender_contents_factory_);
1149 // Convenience function to get the currently active WebContents in
1150 // current_browser().
1151 WebContents* GetActiveWebContents() const {
1152 return current_browser()->tab_strip_model()->GetActiveWebContents();
1155 // Overload for a single expected final status
1156 scoped_ptr<TestPrerender> PrerenderTestURL(
1157 const std::string& html_file,
1158 FinalStatus expected_final_status,
1159 int expected_number_of_loads) {
1160 GURL url = test_server()->GetURL(html_file);
1161 return PrerenderTestURL(url,
1162 expected_final_status,
1163 expected_number_of_loads);
1166 ScopedVector<TestPrerender> PrerenderTestURL(
1167 const std::string& html_file,
1168 const std::vector<FinalStatus>& expected_final_status_queue,
1169 int expected_number_of_loads) {
1170 GURL url = test_server()->GetURL(html_file);
1171 return PrerenderTestURLImpl(url,
1172 expected_final_status_queue,
1173 expected_number_of_loads);
1176 scoped_ptr<TestPrerender> PrerenderTestURL(
1177 const GURL& url,
1178 FinalStatus expected_final_status,
1179 int expected_number_of_loads) {
1180 std::vector<FinalStatus> expected_final_status_queue(
1181 1, expected_final_status);
1182 std::vector<TestPrerender*> prerenders;
1183 PrerenderTestURLImpl(url,
1184 expected_final_status_queue,
1185 expected_number_of_loads).release(&prerenders);
1186 CHECK_EQ(1u, prerenders.size());
1187 return scoped_ptr<TestPrerender>(prerenders[0]);
1190 void NavigateToDestURL() const {
1191 NavigateToDestURLWithDisposition(CURRENT_TAB, true);
1194 // Opens the url in a new tab, with no opener.
1195 void NavigateToDestURLWithDisposition(
1196 WindowOpenDisposition disposition,
1197 bool expect_swap_to_succeed) const {
1198 NavigateToURLWithParams(
1199 content::OpenURLParams(dest_url_, Referrer(), disposition,
1200 ui::PAGE_TRANSITION_TYPED, false),
1201 expect_swap_to_succeed);
1204 void NavigateToURL(const std::string& dest_html_file) const {
1205 NavigateToURLWithDisposition(dest_html_file, CURRENT_TAB, true);
1208 void NavigateToURLWithDisposition(const std::string& dest_html_file,
1209 WindowOpenDisposition disposition,
1210 bool expect_swap_to_succeed) const {
1211 GURL dest_url = test_server()->GetURL(dest_html_file);
1212 NavigateToURLWithDisposition(dest_url, disposition, expect_swap_to_succeed);
1215 void NavigateToURLWithDisposition(const GURL& dest_url,
1216 WindowOpenDisposition disposition,
1217 bool expect_swap_to_succeed) const {
1218 NavigateToURLWithParams(
1219 content::OpenURLParams(dest_url, Referrer(), disposition,
1220 ui::PAGE_TRANSITION_TYPED, false),
1221 expect_swap_to_succeed);
1224 void NavigateToURLWithParams(const content::OpenURLParams& params,
1225 bool expect_swap_to_succeed) const {
1226 NavigateToURLImpl(params, expect_swap_to_succeed);
1229 void OpenDestURLViaClick() const {
1230 OpenURLViaClick(dest_url_);
1233 void OpenURLViaClick(const GURL& url) const {
1234 OpenURLWithJSImpl("Click", url, GURL(), false);
1237 void OpenDestURLViaClickTarget() const {
1238 OpenURLWithJSImpl("ClickTarget", dest_url_, GURL(), true);
1241 void OpenDestURLViaClickPing(const GURL& ping_url) const {
1242 OpenURLWithJSImpl("ClickPing", dest_url_, ping_url, false);
1245 void OpenDestURLViaClickNewWindow() const {
1246 OpenURLWithJSImpl("ShiftClick", dest_url_, GURL(), true);
1249 void OpenDestURLViaClickNewForegroundTab() const {
1250 #if defined(OS_MACOSX)
1251 OpenURLWithJSImpl("MetaShiftClick", dest_url_, GURL(), true);
1252 #else
1253 OpenURLWithJSImpl("CtrlShiftClick", dest_url_, GURL(), true);
1254 #endif
1257 void OpenDestURLViaWindowOpen() const {
1258 OpenURLViaWindowOpen(dest_url_);
1261 void OpenURLViaWindowOpen(const GURL& url) const {
1262 OpenURLWithJSImpl("WindowOpen", url, GURL(), true);
1265 void RemoveLinkElement(int i) const {
1266 GetActiveWebContents()->GetMainFrame()->ExecuteJavaScriptForTests(
1267 base::ASCIIToUTF16(base::StringPrintf("RemoveLinkElement(%d)", i)));
1270 void ClickToNextPageAfterPrerender() {
1271 TestNavigationObserver nav_observer(GetActiveWebContents());
1272 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame();
1273 render_frame_host->ExecuteJavaScriptForTests(
1274 base::ASCIIToUTF16("ClickOpenLink()"));
1275 nav_observer.Wait();
1278 void NavigateToNextPageAfterPrerender() const {
1279 ui_test_utils::NavigateToURL(
1280 current_browser(),
1281 test_server()->GetURL("files/prerender/prerender_page.html"));
1284 // Called after the prerendered page has been navigated to and then away from.
1285 // Navigates back through the history to the prerendered page.
1286 void GoBackToPrerender() {
1287 TestNavigationObserver back_nav_observer(GetActiveWebContents());
1288 chrome::GoBack(current_browser(), CURRENT_TAB);
1289 back_nav_observer.Wait();
1290 bool original_prerender_page = false;
1291 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1292 GetActiveWebContents(),
1293 "window.domAutomationController.send(IsOriginalPrerenderPage())",
1294 &original_prerender_page));
1295 EXPECT_TRUE(original_prerender_page);
1298 // Goes back to the page that was active before the prerender was swapped
1299 // in. This must be called when the prerendered page is the current page
1300 // in the active tab.
1301 void GoBackToPageBeforePrerender() {
1302 WebContents* tab = GetActiveWebContents();
1303 ASSERT_TRUE(tab);
1304 EXPECT_FALSE(tab->IsLoading());
1305 TestNavigationObserver back_nav_observer(tab);
1306 chrome::GoBack(current_browser(), CURRENT_TAB);
1307 back_nav_observer.Wait();
1308 bool js_result;
1309 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1310 tab,
1311 "window.domAutomationController.send(DidBackToOriginalPagePass())",
1312 &js_result));
1313 EXPECT_TRUE(js_result);
1316 bool UrlIsInPrerenderManager(const std::string& html_file) const {
1317 return UrlIsInPrerenderManager(test_server()->GetURL(html_file));
1320 bool UrlIsInPrerenderManager(const GURL& url) const {
1321 return GetPrerenderManager()->FindPrerenderData(
1322 url, GetSessionStorageNamespace()) != NULL;
1325 void UseHttpsSrcServer() {
1326 if (https_src_server_)
1327 return;
1328 https_src_server_.reset(
1329 new net::SpawnedTestServer(
1330 net::SpawnedTestServer::TYPE_HTTPS,
1331 net::SpawnedTestServer::kLocalhost,
1332 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
1333 CHECK(https_src_server_->Start());
1336 void DisableJavascriptCalls() {
1337 call_javascript_ = false;
1340 void DisableLoadEventCheck() {
1341 check_load_events_ = false;
1344 TaskManagerModel* GetModel() const {
1345 return TaskManager::GetInstance()->model();
1348 PrerenderManager* GetPrerenderManager() const {
1349 PrerenderManager* prerender_manager =
1350 PrerenderManagerFactory::GetForProfile(current_browser()->profile());
1351 return prerender_manager;
1354 const PrerenderLinkManager* GetPrerenderLinkManager() const {
1355 PrerenderLinkManager* prerender_link_manager =
1356 PrerenderLinkManagerFactory::GetForProfile(
1357 current_browser()->profile());
1358 return prerender_link_manager;
1361 int GetPrerenderEventCount(int index, const std::string& type) const {
1362 int event_count;
1363 std::string expression = base::StringPrintf(
1364 "window.domAutomationController.send("
1365 " GetPrerenderEventCount(%d, '%s'))", index, type.c_str());
1367 CHECK(content::ExecuteScriptAndExtractInt(
1368 GetActiveWebContents(), expression, &event_count));
1369 return event_count;
1372 bool DidReceivePrerenderStartEventForLinkNumber(int index) const {
1373 return GetPrerenderEventCount(index, "webkitprerenderstart") > 0;
1376 int GetPrerenderLoadEventCountForLinkNumber(int index) const {
1377 return GetPrerenderEventCount(index, "webkitprerenderload");
1380 int GetPrerenderDomContentLoadedEventCountForLinkNumber(int index) const {
1381 return GetPrerenderEventCount(index, "webkitprerenderdomcontentloaded");
1384 bool DidReceivePrerenderStopEventForLinkNumber(int index) const {
1385 return GetPrerenderEventCount(index, "webkitprerenderstop") > 0;
1388 void WaitForPrerenderEventCount(int index,
1389 const std::string& type,
1390 int count) const {
1391 int dummy;
1392 std::string expression = base::StringPrintf(
1393 "WaitForPrerenderEventCount(%d, '%s', %d,"
1394 " window.domAutomationController.send.bind("
1395 " window.domAutomationController, 0))",
1396 index, type.c_str(), count);
1398 CHECK(content::ExecuteScriptAndExtractInt(
1399 GetActiveWebContents(), expression, &dummy));
1400 CHECK_EQ(0, dummy);
1403 bool HadPrerenderEventErrors() const {
1404 bool had_prerender_event_errors;
1405 CHECK(content::ExecuteScriptAndExtractBool(
1406 GetActiveWebContents(),
1407 "window.domAutomationController.send(Boolean("
1408 " hadPrerenderEventErrors))",
1409 &had_prerender_event_errors));
1410 return had_prerender_event_errors;
1413 // Asserting on this can result in flaky tests. PrerenderHandles are
1414 // removed from the PrerenderLinkManager when the prerender is canceled from
1415 // the browser, when the prerenders are cancelled from the renderer process,
1416 // or the channel for the renderer process is closed on the IO thread. In the
1417 // last case, the code must be careful to wait for the channel to close, as it
1418 // is done asynchronously after swapping out the old process. See
1419 // ChannelDestructionWatcher.
1420 bool IsEmptyPrerenderLinkManager() const {
1421 return GetPrerenderLinkManager()->IsEmpty();
1424 size_t GetLinkPrerenderCount() const {
1425 return GetPrerenderLinkManager()->prerenders_.size();
1428 size_t GetRunningLinkPrerenderCount() const {
1429 return GetPrerenderLinkManager()->CountRunningPrerenders();
1432 // Returns length of |prerender_manager_|'s history, or -1 on failure.
1433 int GetHistoryLength() const {
1434 scoped_ptr<base::DictionaryValue> prerender_dict(
1435 static_cast<base::DictionaryValue*>(
1436 GetPrerenderManager()->GetAsValue()));
1437 if (!prerender_dict.get())
1438 return -1;
1439 base::ListValue* history_list;
1440 if (!prerender_dict->GetList("history", &history_list))
1441 return -1;
1442 return static_cast<int>(history_list->GetSize());
1445 #if defined(FULL_SAFE_BROWSING)
1446 FakeSafeBrowsingDatabaseManager* GetFakeSafeBrowsingDatabaseManager() {
1447 return safe_browsing_factory_->most_recent_service()->
1448 fake_database_manager();
1450 #endif
1452 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const {
1453 PrerenderManager::PrerenderData* prerender_data =
1454 GetPrerenderManager()->FindPrerenderData(url, NULL);
1455 return static_cast<TestPrerenderContents*>(
1456 prerender_data ? prerender_data->contents() : NULL);
1459 void SetLoaderHostOverride(const std::string& host) {
1460 loader_host_override_ = host;
1461 host_resolver()->AddRule(host, "127.0.0.1");
1464 void set_loader_path(const std::string& path) {
1465 loader_path_ = path;
1468 void set_loader_query(const std::string& query) {
1469 loader_query_ = query;
1472 GURL GetCrossDomainTestUrl(const std::string& path) {
1473 static const std::string secondary_domain = "www.foo.com";
1474 host_resolver()->AddRule(secondary_domain, "127.0.0.1");
1475 std::string url_str(base::StringPrintf(
1476 "http://%s:%d/%s",
1477 secondary_domain.c_str(),
1478 test_server()->host_port_pair().port(),
1479 path.c_str()));
1480 return GURL(url_str);
1483 void set_browser(Browser* browser) {
1484 explicitly_set_browser_ = browser;
1487 Browser* current_browser() const {
1488 return explicitly_set_browser_ ? explicitly_set_browser_ : browser();
1491 const GURL& dest_url() const {
1492 return dest_url_;
1495 void IncreasePrerenderMemory() {
1496 // Increase the memory allowed in a prerendered page above normal settings.
1497 // Debug build bots occasionally run against the default limit, and tests
1498 // were failing because the prerender was canceled due to memory exhaustion.
1499 // http://crbug.com/93076
1500 GetPrerenderManager()->mutable_config().max_bytes = 1000 * 1024 * 1024;
1503 bool DidPrerenderPass(WebContents* web_contents) const {
1504 bool prerender_test_result = false;
1505 if (!content::ExecuteScriptAndExtractBool(
1506 web_contents,
1507 "window.domAutomationController.send(DidPrerenderPass())",
1508 &prerender_test_result))
1509 return false;
1510 return prerender_test_result;
1513 bool DidDisplayPass(WebContents* web_contents) const {
1514 bool display_test_result = false;
1515 if (!content::ExecuteScriptAndExtractBool(
1516 web_contents,
1517 "window.domAutomationController.send(DidDisplayPass())",
1518 &display_test_result))
1519 return false;
1520 return display_test_result;
1523 scoped_ptr<TestPrerender> ExpectPrerender(FinalStatus expected_final_status) {
1524 return prerender_contents_factory_->ExpectPrerenderContents(
1525 expected_final_status);
1528 void AddPrerender(const GURL& url, int index) {
1529 std::string javascript = base::StringPrintf(
1530 "AddPrerender('%s', %d)", url.spec().c_str(), index);
1531 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame();
1532 render_frame_host->ExecuteJavaScriptForTests(
1533 base::ASCIIToUTF16(javascript));
1536 // Returns a string for pattern-matching TaskManager tab entries.
1537 base::string16 MatchTaskManagerTab(const char* page_title) {
1538 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX,
1539 base::ASCIIToUTF16(page_title));
1542 // Returns a string for pattern-matching TaskManager prerender entries.
1543 base::string16 MatchTaskManagerPrerender(const char* page_title) {
1544 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
1545 base::ASCIIToUTF16(page_title));
1548 const base::HistogramTester& histogram_tester() { return histogram_tester_; }
1550 protected:
1551 bool autostart_test_server_;
1553 private:
1554 // TODO(davidben): Remove this altogether so the tests don't globally assume
1555 // only one prerender.
1556 TestPrerenderContents* GetPrerenderContents() const {
1557 return GetPrerenderContentsFor(dest_url_);
1560 ScopedVector<TestPrerender> PrerenderTestURLImpl(
1561 const GURL& prerender_url,
1562 const std::vector<FinalStatus>& expected_final_status_queue,
1563 int expected_number_of_loads) {
1564 dest_url_ = prerender_url;
1566 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
1567 replacement_text.push_back(
1568 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec()));
1569 std::string replacement_path;
1570 CHECK(net::SpawnedTestServer::GetFilePathWithReplacements(
1571 loader_path_,
1572 replacement_text,
1573 &replacement_path));
1575 const net::SpawnedTestServer* src_server = test_server();
1576 if (https_src_server_)
1577 src_server = https_src_server_.get();
1578 GURL loader_url = src_server->GetURL(
1579 replacement_path + "&" + loader_query_);
1581 GURL::Replacements loader_replacements;
1582 if (!loader_host_override_.empty())
1583 loader_replacements.SetHostStr(loader_host_override_);
1584 loader_url = loader_url.ReplaceComponents(loader_replacements);
1586 VLOG(1) << "Running test with queue length " <<
1587 expected_final_status_queue.size();
1588 CHECK(!expected_final_status_queue.empty());
1589 ScopedVector<TestPrerender> prerenders;
1590 for (size_t i = 0; i < expected_final_status_queue.size(); i++) {
1591 prerenders.push_back(
1592 prerender_contents_factory_->ExpectPrerenderContents(
1593 expected_final_status_queue[i]).release());
1596 FinalStatus expected_final_status = expected_final_status_queue.front();
1598 // Navigate to the loader URL and then wait for the first prerender to be
1599 // created.
1600 ui_test_utils::NavigateToURL(current_browser(), loader_url);
1601 prerenders[0]->WaitForCreate();
1602 prerenders[0]->WaitForLoads(expected_number_of_loads);
1604 if (ShouldAbortPrerenderBeforeSwap(expected_final_status)) {
1605 // The prerender will abort on its own. Assert it does so correctly.
1606 prerenders[0]->WaitForStop();
1607 EXPECT_FALSE(prerenders[0]->contents());
1608 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
1609 } else {
1610 // Otherwise, check that it prerendered correctly.
1611 TestPrerenderContents* prerender_contents = prerenders[0]->contents();
1613 CHECK_NE(static_cast<PrerenderContents*>(NULL), prerender_contents);
1614 EXPECT_EQ(FINAL_STATUS_MAX, prerender_contents->final_status());
1615 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1617 if (call_javascript_) {
1618 // Check if page behaves as expected while in prerendered state.
1619 EXPECT_TRUE(DidPrerenderPass(prerender_contents->prerender_contents()));
1623 // Test that the referring page received the right start and load events.
1624 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1625 if (check_load_events_) {
1626 EXPECT_EQ(expected_number_of_loads, prerenders[0]->number_of_loads());
1627 EXPECT_EQ(expected_number_of_loads,
1628 GetPrerenderLoadEventCountForLinkNumber(0));
1630 EXPECT_FALSE(HadPrerenderEventErrors());
1632 return prerenders.Pass();
1635 void NavigateToURLImpl(const content::OpenURLParams& params,
1636 bool expect_swap_to_succeed) const {
1637 ASSERT_NE(static_cast<PrerenderManager*>(NULL), GetPrerenderManager());
1638 // Make sure in navigating we have a URL to use in the PrerenderManager.
1639 ASSERT_NE(static_cast<PrerenderContents*>(NULL), GetPrerenderContents());
1641 WebContents* web_contents = GetPrerenderContents()->prerender_contents();
1643 // Navigate and wait for either the load to finish normally or for a swap to
1644 // occur.
1645 // TODO(davidben): The only handles CURRENT_TAB navigations, which is the
1646 // only case tested or prerendered right now.
1647 CHECK_EQ(CURRENT_TAB, params.disposition);
1648 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
1649 GetActiveWebContents());
1650 WebContents* target_web_contents = current_browser()->OpenURL(params);
1651 swap_observer.Wait();
1653 if (web_contents && expect_swap_to_succeed) {
1654 EXPECT_EQ(web_contents, target_web_contents);
1655 if (call_javascript_)
1656 EXPECT_TRUE(DidDisplayPass(web_contents));
1660 // Opens the prerendered page using javascript functions in the loader
1661 // page. |javascript_function_name| should be a 0 argument function which is
1662 // invoked. |new_web_contents| is true if the navigation is expected to
1663 // happen in a new WebContents via OpenURL.
1664 void OpenURLWithJSImpl(const std::string& javascript_function_name,
1665 const GURL& url,
1666 const GURL& ping_url,
1667 bool new_web_contents) const {
1668 WebContents* web_contents = GetActiveWebContents();
1669 RenderFrameHost* render_frame_host = web_contents->GetMainFrame();
1670 // Extra arguments in JS are ignored.
1671 std::string javascript = base::StringPrintf(
1672 "%s('%s', '%s')", javascript_function_name.c_str(),
1673 url.spec().c_str(), ping_url.spec().c_str());
1675 if (new_web_contents) {
1676 NewTabNavigationOrSwapObserver observer;
1677 render_frame_host->ExecuteJavaScriptWithUserGestureForTests(
1678 base::ASCIIToUTF16(javascript));
1679 observer.Wait();
1680 } else {
1681 NavigationOrSwapObserver observer(current_browser()->tab_strip_model(),
1682 web_contents);
1683 render_frame_host->ExecuteJavaScriptForTests(
1684 base::ASCIIToUTF16(javascript));
1685 observer.Wait();
1689 TestPrerenderContentsFactory* prerender_contents_factory_;
1690 #if defined(FULL_SAFE_BROWSING)
1691 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_;
1692 #endif
1693 NeverRunsExternalProtocolHandlerDelegate external_protocol_handler_delegate_;
1694 GURL dest_url_;
1695 scoped_ptr<net::SpawnedTestServer> https_src_server_;
1696 bool call_javascript_;
1697 bool check_load_events_;
1698 std::string loader_host_override_;
1699 std::string loader_path_;
1700 std::string loader_query_;
1701 Browser* explicitly_set_browser_;
1702 base::HistogramTester histogram_tester_;
1705 // Checks that a page is correctly prerendered in the case of a
1706 // <link rel=prerender> tag and then loaded into a tab in response to a
1707 // navigation.
1708 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) {
1709 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
1710 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
1711 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1712 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1713 histogram_tester().ExpectTotalCount(
1714 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1715 histogram_tester().ExpectTotalCount(
1716 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
1718 ChannelDestructionWatcher channel_close_watcher;
1719 channel_close_watcher.WatchChannel(
1720 GetActiveWebContents()->GetRenderProcessHost());
1721 NavigateToDestURL();
1722 channel_close_watcher.WaitForChannelClose();
1724 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
1725 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
1727 histogram_tester().ExpectTotalCount(
1728 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
1730 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1733 // Checks that cross-domain prerenders emit the correct histograms.
1734 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageCrossDomain) {
1735 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
1736 FINAL_STATUS_USED, 1);
1737 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1738 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1739 histogram_tester().ExpectTotalCount(
1740 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1741 histogram_tester().ExpectTotalCount(
1742 "Prerender.webcross_PrerenderNotSwappedInPLT", 1);
1744 NavigateToDestURL();
1745 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLT", 1);
1746 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLTMatched",
1748 histogram_tester().ExpectTotalCount(
1749 "Prerender.webcross_PerceivedPLTMatchedComplete", 1);
1752 // Checks that pending prerenders launch and receive proper event treatment.
1753 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPagePending) {
1754 scoped_ptr<TestPrerender> prerender =
1755 PrerenderTestURL("files/prerender/prerender_page_pending.html",
1756 FINAL_STATUS_USED, 1);
1758 // Navigate to the prerender.
1759 scoped_ptr<TestPrerender> prerender2 = ExpectPrerender(FINAL_STATUS_USED);
1760 NavigateToDestURL();
1761 // Abort early if the original prerender didn't swap, so as not to hang.
1762 ASSERT_FALSE(prerender->contents());
1764 // Wait for the new prerender to be ready.
1765 prerender2->WaitForStart();
1766 prerender2->WaitForLoads(1);
1768 const GURL prerender_page_url =
1769 test_server()->GetURL("files/prerender/prerender_page.html");
1770 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1771 EXPECT_NE(static_cast<TestPrerenderContents*>(NULL),
1772 GetPrerenderContentsFor(prerender_page_url));
1774 // Now navigate to our target page.
1775 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
1776 GetActiveWebContents());
1777 ui_test_utils::NavigateToURLWithDisposition(
1778 current_browser(), prerender_page_url, CURRENT_TAB,
1779 ui_test_utils::BROWSER_TEST_NONE);
1780 swap_observer.Wait();
1782 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1785 // Checks that pending prerenders which are canceled before they are launched
1786 // never get started.
1787 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageRemovesPending) {
1788 PrerenderTestURL("files/prerender/prerender_page_removes_pending.html",
1789 FINAL_STATUS_USED, 1);
1791 ChannelDestructionWatcher channel_close_watcher;
1792 channel_close_watcher.WatchChannel(
1793 GetActiveWebContents()->GetRenderProcessHost());
1794 NavigateToDestURL();
1795 channel_close_watcher.WaitForChannelClose();
1797 EXPECT_FALSE(DidReceivePrerenderStartEventForLinkNumber(1));
1798 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1799 EXPECT_FALSE(HadPrerenderEventErrors());
1800 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1801 // calls did a thread/process hop to the renderer which insured pending
1802 // renderer events have arrived.
1803 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1806 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageRemovingLink) {
1807 scoped_ptr<TestPrerender> prerender =
1808 PrerenderTestURL("files/prerender/prerender_page.html",
1809 FINAL_STATUS_CANCELLED, 1);
1811 // No ChannelDestructionWatcher is needed here, since prerenders in the
1812 // PrerenderLinkManager should be deleted by removing the links, rather than
1813 // shutting down the renderer process.
1814 RemoveLinkElement(0);
1815 prerender->WaitForStop();
1817 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1818 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1819 EXPECT_FALSE(HadPrerenderEventErrors());
1820 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1821 // calls did a thread/process hop to the renderer which insured pending
1822 // renderer events have arrived.
1823 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1826 IN_PROC_BROWSER_TEST_F(
1827 PrerenderBrowserTest, PrerenderPageRemovingLinkWithTwoLinks) {
1828 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
1829 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
1831 set_loader_query("links_to_insert=2");
1832 scoped_ptr<TestPrerender> prerender =
1833 PrerenderTestURL("files/prerender/prerender_page.html",
1834 FINAL_STATUS_CANCELLED, 1);
1835 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1836 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1837 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1838 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1840 RemoveLinkElement(0);
1841 RemoveLinkElement(1);
1842 prerender->WaitForStop();
1844 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1845 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1846 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1847 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1848 EXPECT_FALSE(HadPrerenderEventErrors());
1849 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1850 // calls did a thread/process hop to the renderer which insured pending
1851 // renderer events have arrived.
1852 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1855 IN_PROC_BROWSER_TEST_F(
1856 PrerenderBrowserTest, PrerenderPageRemovingLinkWithTwoLinksOneLate) {
1857 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
1858 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
1860 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
1861 scoped_ptr<TestPrerender> prerender =
1862 PrerenderTestURL(url, FINAL_STATUS_CANCELLED, 1);
1864 // Add a second prerender for the same link. It reuses the prerender, so only
1865 // the start event fires here.
1866 AddPrerender(url, 1);
1867 WaitForPrerenderEventCount(1, "webkitprerenderstart", 1);
1868 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1869 EXPECT_EQ(0, GetPrerenderLoadEventCountForLinkNumber(1));
1870 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1872 RemoveLinkElement(0);
1873 RemoveLinkElement(1);
1874 prerender->WaitForStop();
1876 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1877 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1878 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1879 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1880 EXPECT_FALSE(HadPrerenderEventErrors());
1881 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1882 // calls did a thread/process hop to the renderer which insured pending
1883 // renderer events have arrived.
1884 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1887 IN_PROC_BROWSER_TEST_F(
1888 PrerenderBrowserTest,
1889 PrerenderPageRemovingLinkWithTwoLinksRemovingOne) {
1890 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
1891 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
1892 set_loader_query("links_to_insert=2");
1893 PrerenderTestURL("files/prerender/prerender_page.html",
1894 FINAL_STATUS_USED, 1);
1895 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1896 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1897 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1898 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1900 RemoveLinkElement(0);
1901 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1902 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1903 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1904 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1905 EXPECT_FALSE(HadPrerenderEventErrors());
1906 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1907 // calls did a thread/process hop to the renderer which insured pending
1908 // renderer events have arrived.
1909 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1911 ChannelDestructionWatcher channel_close_watcher;
1912 channel_close_watcher.WatchChannel(
1913 GetActiveWebContents()->GetRenderProcessHost());
1914 NavigateToDestURL();
1915 channel_close_watcher.WaitForChannelClose();
1917 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1920 // Checks that the visibility API works.
1921 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderVisibility) {
1922 PrerenderTestURL("files/prerender/prerender_visibility.html",
1923 FINAL_STATUS_USED,
1925 NavigateToDestURL();
1928 // Checks that the prerendering of a page is canceled correctly if we try to
1929 // swap it in before it commits.
1930 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap) {
1931 // Navigate to a page that triggers a prerender for a URL that never commits.
1932 const GURL kNoCommitUrl("http://never-respond.example.com");
1933 base::FilePath file(GetTestPath("prerender_page.html"));
1935 base::RunLoop prerender_start_loop;
1936 BrowserThread::PostTask(
1937 BrowserThread::IO, FROM_HERE,
1938 base::Bind(&CreateHangingFirstRequestInterceptorOnIO, kNoCommitUrl, file,
1939 prerender_start_loop.QuitClosure()));
1940 DisableJavascriptCalls();
1941 PrerenderTestURL(kNoCommitUrl,
1942 FINAL_STATUS_NAVIGATION_UNCOMMITTED,
1944 // Wait for the hanging request to be scheduled.
1945 prerender_start_loop.Run();
1947 // Navigate to the URL, but assume the contents won't be swapped in.
1948 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
1951 // Checks that client redirects don't add alias URLs until after they commit.
1952 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap2) {
1953 // Navigate to a page that then navigates to a URL that never commits.
1954 const GURL kNoCommitUrl("http://never-respond.example.com");
1955 base::FilePath file(GetTestPath("prerender_page.html"));
1957 base::RunLoop prerender_start_loop;
1958 BrowserThread::PostTask(
1959 BrowserThread::IO, FROM_HERE,
1960 base::Bind(&CreateHangingFirstRequestInterceptorOnIO, kNoCommitUrl, file,
1961 prerender_start_loop.QuitClosure()));
1962 DisableJavascriptCalls();
1963 PrerenderTestURL(CreateClientRedirect(kNoCommitUrl.spec()),
1964 FINAL_STATUS_APP_TERMINATING, 1);
1965 // Wait for the hanging request to be scheduled.
1966 prerender_start_loop.Run();
1968 // Navigating to the second URL should not swap.
1969 NavigateToURLWithDisposition(kNoCommitUrl, CURRENT_TAB, false);
1972 // Checks that the prerendering of a page is canceled correctly when a
1973 // Javascript alert is called.
1974 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertBeforeOnload) {
1975 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
1976 FINAL_STATUS_JAVASCRIPT_ALERT,
1980 // Checks that the prerendering of a page is canceled correctly when a
1981 // Javascript alert is called.
1982 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertAfterOnload) {
1983 PrerenderTestURL("files/prerender/prerender_alert_after_onload.html",
1984 FINAL_STATUS_JAVASCRIPT_ALERT,
1988 // Checks that plugins are not loaded while a page is being preloaded, but
1989 // are loaded when the page is displayed.
1990 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDelayLoadPlugin) {
1991 PrerenderTestURL("files/prerender/prerender_plugin_delay_load.html",
1992 FINAL_STATUS_USED, 1);
1993 NavigateToDestURL();
1996 // For Content Setting DETECT, checks that plugins are not loaded while
1997 // a page is being preloaded, but are loaded when the page is displayed.
1998 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderContentSettingDetect) {
1999 HostContentSettingsMap* content_settings_map =
2000 HostContentSettingsMapFactory::GetForProfile(
2001 current_browser()->profile());
2002 content_settings_map->SetDefaultContentSetting(
2003 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_DETECT_IMPORTANT_CONTENT);
2005 PrerenderTestURL("files/prerender/prerender_plugin_power_saver.html",
2006 FINAL_STATUS_USED, 1);
2008 DisableJavascriptCalls();
2009 NavigateToDestURL();
2010 bool second_placeholder_present = false;
2011 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
2012 GetActiveWebContents(), "AwaitPluginPrerollAndPlaceholder();",
2013 &second_placeholder_present));
2014 EXPECT_TRUE(second_placeholder_present);
2017 // For Content Setting BLOCK, checks that plugins are never loaded.
2018 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderContentSettingBlock) {
2019 HostContentSettingsMap* content_settings_map =
2020 HostContentSettingsMapFactory::GetForProfile(
2021 current_browser()->profile());
2022 content_settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS,
2023 CONTENT_SETTING_BLOCK);
2025 PrerenderTestURL("files/prerender/prerender_plugin_never_load.html",
2026 FINAL_STATUS_USED, 1);
2027 NavigateToDestURL();
2030 // Checks that we don't load a NaCl plugin when NaCl is disabled.
2031 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNaClPluginDisabled) {
2032 PrerenderTestURL("files/prerender/prerender_plugin_nacl_disabled.html",
2033 FINAL_STATUS_USED,
2035 NavigateToDestURL();
2038 // Run this check again. When we try to load aa ppapi plugin, the
2039 // "loadstart" event is asynchronously posted to a message loop.
2040 // It's possible that earlier call could have been run before the
2041 // the "loadstart" event was posted.
2042 // TODO(mmenke): While this should reliably fail on regressions, the
2043 // reliability depends on the specifics of ppapi plugin
2044 // loading. It would be great if we could avoid that.
2045 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2048 // Checks that plugins in an iframe are not loaded while a page is
2049 // being preloaded, but are loaded when the page is displayed.
2050 #if defined(USE_AURA) && !defined(OS_WIN)
2051 // http://crbug.com/103496
2052 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2053 DISABLED_PrerenderIframeDelayLoadPlugin
2054 #elif defined(OS_MACOSX)
2055 // http://crbug.com/100514
2056 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2057 DISABLED_PrerenderIframeDelayLoadPlugin
2058 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
2059 // TODO(jschuh): Failing plugin tests. crbug.com/244653
2060 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2061 DISABLED_PrerenderIframeDelayLoadPlugin
2062 #else
2063 #define MAYBE_PrerenderIframeDelayLoadPlugin PrerenderIframeDelayLoadPlugin
2064 #endif
2065 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2066 MAYBE_PrerenderIframeDelayLoadPlugin) {
2067 PrerenderTestURL("files/prerender/prerender_iframe_plugin_delay_load.html",
2068 FINAL_STATUS_USED,
2070 NavigateToDestURL();
2073 // Renders a page that contains a prerender link to a page that contains an
2074 // iframe with a source that requires http authentication. This should not
2075 // prerender successfully.
2076 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttpAuthentication) {
2077 PrerenderTestURL("files/prerender/prerender_http_auth_container.html",
2078 FINAL_STATUS_AUTH_NEEDED,
2082 // Checks that client-issued redirects work with prerendering.
2083 // This version navigates to the page which issues the redirection, rather
2084 // than the final destination page.
2085 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2086 PrerenderClientRedirectNavigateToFirst) {
2087 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2088 FINAL_STATUS_USED,
2090 NavigateToDestURL();
2093 // Checks that client-issued redirects work with prerendering.
2094 // This version navigates to the final destination page, rather than the
2095 // page which does the redirection.
2096 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2097 PrerenderClientRedirectNavigateToSecond) {
2098 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2099 FINAL_STATUS_USED,
2101 NavigateToURL("files/prerender/prerender_page.html");
2104 // Checks that redirects with location.replace do not cancel a prerender and
2105 // and swap when navigating to the first page.
2106 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2107 PrerenderLocationReplaceNavigateToFirst) {
2108 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2109 FINAL_STATUS_USED,
2111 NavigateToDestURL();
2114 // Checks that redirects with location.replace do not cancel a prerender and
2115 // and swap when navigating to the second.
2116 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2117 PrerenderLocationReplaceNavigateToSecond) {
2118 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2119 FINAL_STATUS_USED,
2121 NavigateToURL("files/prerender/prerender_page.html");
2124 // Checks that we get the right PPLT histograms for client redirect prerenders
2125 // and navigations when the referring page is Google.
2126 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2127 PrerenderLocationReplaceGWSHistograms) {
2128 DisableJavascriptCalls();
2130 // The loader page should look like Google.
2131 static const char kGoogleDotCom[] = "www.google.com";
2132 SetLoaderHostOverride(kGoogleDotCom);
2133 set_loader_path("files/prerender/prerender_loader_with_replace_state.html");
2135 GURL dest_url = GetCrossDomainTestUrl(
2136 "files/prerender/prerender_deferred_image.html");
2138 GURL prerender_url = test_server()->GetURL(
2139 "files/prerender/prerender_location_replace.html?" +
2140 net::EscapeQueryParamValue(dest_url.spec(), false) +
2141 "#prerender");
2142 GURL::Replacements replacements;
2143 replacements.SetHostStr(kGoogleDotCom);
2144 prerender_url = prerender_url.ReplaceComponents(replacements);
2146 // The prerender will not completely load until after the swap, so wait for a
2147 // title change before calling DidPrerenderPass.
2148 scoped_ptr<TestPrerender> prerender =
2149 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 1);
2150 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
2151 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
2152 EXPECT_EQ(1, prerender->number_of_loads());
2154 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
2155 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
2156 histogram_tester().ExpectTotalCount(
2157 "Prerender.none_PerceivedPLTMatchedComplete", 0);
2158 // Although there is a client redirect, it is dropped from histograms because
2159 // it is a Google URL. The target page itself does not load until after the
2160 // swap.
2161 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2164 GURL navigate_url = test_server()->GetURL(
2165 "files/prerender/prerender_location_replace.html?" +
2166 net::EscapeQueryParamValue(dest_url.spec(), false) +
2167 "#navigate");
2168 navigate_url = navigate_url.ReplaceComponents(replacements);
2170 NavigationOrSwapObserver swap_observer(
2171 current_browser()->tab_strip_model(),
2172 GetActiveWebContents(), 2);
2173 current_browser()->OpenURL(OpenURLParams(
2174 navigate_url, Referrer(), CURRENT_TAB,
2175 ui::PAGE_TRANSITION_TYPED, false));
2176 swap_observer.Wait();
2178 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2180 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2182 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLT", 1);
2183 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLTMatched", 1);
2184 histogram_tester().ExpectTotalCount(
2185 "Prerender.gws_PerceivedPLTMatchedComplete", 1);
2187 // The client redirect does /not/ count as a miss because it's a Google URL.
2188 histogram_tester().ExpectTotalCount("Prerender.PerceivedPLTFirstAfterMiss",
2192 // Checks that client-issued redirects work with prerendering.
2193 // This version navigates to the final destination page, rather than the
2194 // page which does the redirection via a mouse click.
2195 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2196 PrerenderClientRedirectNavigateToSecondViaClick) {
2197 GURL prerender_url = test_server()->GetURL(
2198 CreateClientRedirect("files/prerender/prerender_page.html"));
2199 GURL destination_url = test_server()->GetURL(
2200 "files/prerender/prerender_page.html");
2201 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 2);
2202 OpenURLViaClick(destination_url);
2205 // Checks that a page served over HTTPS is correctly prerendered.
2206 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttps) {
2207 net::SpawnedTestServer https_server(
2208 net::SpawnedTestServer::TYPE_HTTPS, net::SpawnedTestServer::kLocalhost,
2209 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2210 ASSERT_TRUE(https_server.Start());
2211 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2212 PrerenderTestURL(https_url,
2213 FINAL_STATUS_USED,
2215 NavigateToDestURL();
2218 // Checks that client-issued redirects within an iframe in a prerendered
2219 // page will not count as an "alias" for the prerendered page.
2220 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2221 PrerenderClientRedirectInIframe) {
2222 std::string redirect_path = CreateClientRedirect(
2223 "/files/prerender/prerender_embedded_content.html");
2224 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2225 replacement_text.push_back(
2226 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
2227 std::string replacement_path;
2228 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2229 "files/prerender/prerender_with_iframe.html",
2230 replacement_text,
2231 &replacement_path));
2232 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 2);
2233 EXPECT_FALSE(UrlIsInPrerenderManager(
2234 "files/prerender/prerender_embedded_content.html"));
2235 NavigateToDestURL();
2238 // Checks that server-issued redirects work with prerendering.
2239 // This version navigates to the page which issues the redirection, rather
2240 // than the final destination page.
2241 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2242 PrerenderServerRedirectNavigateToFirst) {
2243 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2244 FINAL_STATUS_USED,
2246 NavigateToDestURL();
2249 // Checks that server-issued redirects work with prerendering.
2250 // This version navigates to the final destination page, rather than the
2251 // page which does the redirection.
2252 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2253 PrerenderServerRedirectNavigateToSecond) {
2254 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2255 FINAL_STATUS_USED,
2257 NavigateToURL("files/prerender/prerender_page.html");
2260 // Checks that server-issued redirects work with prerendering.
2261 // This version navigates to the final destination page, rather than the
2262 // page which does the redirection via a mouse click.
2263 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2264 PrerenderServerRedirectNavigateToSecondViaClick) {
2265 GURL prerender_url = test_server()->GetURL(
2266 CreateServerRedirect("files/prerender/prerender_page.html"));
2267 GURL destination_url = test_server()->GetURL(
2268 "files/prerender/prerender_page.html");
2269 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 1);
2270 OpenURLViaClick(destination_url);
2273 // Checks that server-issued redirects within an iframe in a prerendered
2274 // page will not count as an "alias" for the prerendered page.
2275 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderServerRedirectInIframe) {
2276 std::string redirect_path = CreateServerRedirect(
2277 "/files/prerender/prerender_embedded_content.html");
2278 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2279 replacement_text.push_back(
2280 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
2281 std::string replacement_path;
2282 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2283 "files/prerender/prerender_with_iframe.html",
2284 replacement_text,
2285 &replacement_path));
2286 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2287 EXPECT_FALSE(UrlIsInPrerenderManager(
2288 "files/prerender/prerender_embedded_content.html"));
2289 NavigateToDestURL();
2292 // Prerenders a page that contains an automatic download triggered through an
2293 // iframe. This should not prerender successfully.
2294 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadIframe) {
2295 PrerenderTestURL("files/prerender/prerender_download_iframe.html",
2296 FINAL_STATUS_DOWNLOAD,
2300 // Prerenders a page that contains an automatic download triggered through
2301 // Javascript changing the window.location. This should not prerender
2302 // successfully
2303 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadLocation) {
2304 PrerenderTestURL(CreateClientRedirect("files/download-test1.lib"),
2305 FINAL_STATUS_DOWNLOAD,
2309 // Prerenders a page that contains an automatic download triggered through a
2310 // client-issued redirect. This should not prerender successfully.
2311 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadClientRedirect) {
2312 PrerenderTestURL("files/prerender/prerender_download_refresh.html",
2313 FINAL_STATUS_DOWNLOAD,
2317 // Checks that the referrer is set when prerendering.
2318 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrer) {
2319 PrerenderTestURL("files/prerender/prerender_referrer.html",
2320 FINAL_STATUS_USED,
2322 NavigateToDestURL();
2325 // Checks that the referrer is not set when prerendering and the source page is
2326 // HTTPS.
2327 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2328 PrerenderNoSSLReferrer) {
2329 UseHttpsSrcServer();
2330 PrerenderTestURL("files/prerender/prerender_no_referrer.html",
2331 FINAL_STATUS_USED,
2333 NavigateToDestURL();
2336 // Checks that the referrer is set when prerendering is cancelled.
2337 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelReferrer) {
2338 scoped_ptr<TestContentBrowserClient> test_content_browser_client(
2339 new TestContentBrowserClient);
2340 content::ContentBrowserClient* original_browser_client =
2341 content::SetBrowserClientForTesting(test_content_browser_client.get());
2343 PrerenderTestURL("files/prerender/prerender_referrer.html",
2344 FINAL_STATUS_CANCELLED,
2346 OpenDestURLViaClick();
2348 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2350 content::SetBrowserClientForTesting(original_browser_client);
2353 // Checks that popups on a prerendered page cause cancellation.
2354 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPopup) {
2355 PrerenderTestURL("files/prerender/prerender_popup.html",
2356 FINAL_STATUS_CREATE_NEW_WINDOW,
2360 // Checks that registering a protocol handler causes cancellation.
2361 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRegisterProtocolHandler) {
2362 PrerenderTestURL("files/prerender/prerender_register_protocol_handler.html",
2363 FINAL_STATUS_REGISTER_PROTOCOL_HANDLER,
2367 // Checks that renderers using excessive memory will be terminated.
2368 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExcessiveMemory) {
2369 ASSERT_TRUE(GetPrerenderManager());
2370 GetPrerenderManager()->mutable_config().max_bytes = 30 * 1024 * 1024;
2371 // The excessive memory kill may happen before or after the load event as it
2372 // happens asynchronously with IPC calls. Even if the test does not start
2373 // allocating until after load, the browser process might notice before the
2374 // message gets through. This happens on XP debug bots because they're so
2375 // slow. Instead, don't bother checking the load event count.
2376 DisableLoadEventCheck();
2377 PrerenderTestURL("files/prerender/prerender_excessive_memory.html",
2378 FINAL_STATUS_MEMORY_LIMIT_EXCEEDED, 0);
2381 // Checks shutdown code while a prerender is active.
2382 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderQuickQuit) {
2383 DisableJavascriptCalls();
2384 DisableLoadEventCheck();
2385 PrerenderTestURL("files/prerender/prerender_page.html",
2386 FINAL_STATUS_APP_TERMINATING,
2390 // Checks that we don't prerender in an infinite loop.
2391 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderInfiniteLoop) {
2392 const char* const kHtmlFileA = "files/prerender/prerender_infinite_a.html";
2393 const char* const kHtmlFileB = "files/prerender/prerender_infinite_b.html";
2395 std::vector<FinalStatus> expected_final_status_queue;
2396 expected_final_status_queue.push_back(FINAL_STATUS_USED);
2397 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2399 ScopedVector<TestPrerender> prerenders =
2400 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
2401 ASSERT_TRUE(prerenders[0]->contents());
2402 // Assert that the pending prerender is in there already. This relies on the
2403 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2404 // the page load one.
2405 EXPECT_EQ(2U, GetLinkPrerenderCount());
2406 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2408 // Next url should be in pending list but not an active entry.
2409 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2411 NavigateToDestURL();
2413 // Make sure the PrerenderContents for the next url is now in the manager and
2414 // not pending. This relies on pending prerenders being resolved in the same
2415 // event loop iteration as OnPrerenderStop.
2416 EXPECT_TRUE(UrlIsInPrerenderManager(kHtmlFileB));
2417 EXPECT_EQ(1U, GetLinkPrerenderCount());
2418 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2421 // Checks that we don't prerender in an infinite loop and multiple links are
2422 // handled correctly.
2423 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2424 PrerenderInfiniteLoopMultiple) {
2425 const char* const kHtmlFileA =
2426 "files/prerender/prerender_infinite_a_multiple.html";
2427 const char* const kHtmlFileB =
2428 "files/prerender/prerender_infinite_b_multiple.html";
2429 const char* const kHtmlFileC =
2430 "files/prerender/prerender_infinite_c_multiple.html";
2432 // This test is conceptually simplest if concurrency is at two, since we
2433 // don't have to worry about which of kHtmlFileB or kHtmlFileC gets evicted.
2434 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
2435 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
2437 std::vector<FinalStatus> expected_final_status_queue;
2438 expected_final_status_queue.push_back(FINAL_STATUS_USED);
2439 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2440 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2442 ScopedVector<TestPrerender> prerenders =
2443 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
2444 ASSERT_TRUE(prerenders[0]->contents());
2446 // Next url should be in pending list but not an active entry. This relies on
2447 // the fact that the renderer sends out the AddLinkRelPrerender IPC before
2448 // sending the page load one.
2449 EXPECT_EQ(3U, GetLinkPrerenderCount());
2450 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2451 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2452 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileC));
2454 NavigateToDestURL();
2456 // Make sure the PrerenderContents for the next urls are now in the manager
2457 // and not pending. One and only one of the URLs (the last seen) should be the
2458 // active entry. This relies on pending prerenders being resolved in the same
2459 // event loop iteration as OnPrerenderStop.
2460 bool url_b_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileB);
2461 bool url_c_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileC);
2462 EXPECT_TRUE(url_b_is_active_prerender && url_c_is_active_prerender);
2463 EXPECT_EQ(2U, GetLinkPrerenderCount());
2464 EXPECT_EQ(2U, GetRunningLinkPrerenderCount());
2467 // Checks that pending prerenders are aborted (and never launched) when launched
2468 // by a prerender that itself gets aborted.
2469 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAbortPendingOnCancel) {
2470 const char* const kHtmlFileA = "files/prerender/prerender_infinite_a.html";
2471 const char* const kHtmlFileB = "files/prerender/prerender_infinite_b.html";
2473 scoped_ptr<TestPrerender> prerender =
2474 PrerenderTestURL(kHtmlFileA, FINAL_STATUS_CANCELLED, 1);
2475 ASSERT_TRUE(prerender->contents());
2476 // Assert that the pending prerender is in there already. This relies on the
2477 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2478 // the page load one.
2479 EXPECT_EQ(2U, GetLinkPrerenderCount());
2480 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2482 // Next url should be in pending list but not an active entry.
2483 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2485 // Cancel the prerender.
2486 GetPrerenderManager()->CancelAllPrerenders();
2487 prerender->WaitForStop();
2489 // All prerenders are now gone.
2490 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
2493 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerBeforePrerender) {
2494 // This test is for the old implementation of the task manager. We must
2495 // explicitly disable the new one.
2496 task_manager::browsertest_util::EnableOldTaskManager();
2498 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2499 const base::string16 any_tab = MatchTaskManagerTab("*");
2500 const base::string16 original = MatchTaskManagerTab("Preloader");
2501 const base::string16 prerender = MatchTaskManagerPrerender("Prerender Page");
2502 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2504 // Show the task manager. This populates the model.
2505 chrome::OpenTaskManager(current_browser());
2506 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2507 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2509 // Prerender a page in addition to the original tab.
2510 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2512 // A TaskManager entry should appear like "Prerender: Prerender Page"
2513 // alongside the original tab entry. There should be just these two entries.
2514 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender));
2515 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original));
2516 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final));
2517 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender));
2518 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2520 // Swap in the prerendered content.
2521 NavigateToDestURL();
2523 // The "Prerender: " TaskManager entry should disappear, being replaced by a
2524 // "Tab: Prerender Page" entry, and nothing else.
2525 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender));
2526 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original));
2527 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2528 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2529 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2532 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerAfterPrerender) {
2533 // This test is for the old implementation of the task manager. We must
2534 // explicitly disable the new one.
2535 task_manager::browsertest_util::EnableOldTaskManager();
2537 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2538 const base::string16 any_tab = MatchTaskManagerTab("*");
2539 const base::string16 original = MatchTaskManagerTab("Preloader");
2540 const base::string16 prerender = MatchTaskManagerPrerender("Prerender Page");
2541 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2543 // Start with two resources.
2544 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2546 // Show the task manager. This populates the model. Importantly, we're doing
2547 // this after the prerender WebContents already exists - the task manager
2548 // needs to find it, it can't just listen for creation.
2549 chrome::OpenTaskManager(current_browser());
2551 // A TaskManager entry should appear like "Prerender: Prerender Page"
2552 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender));
2553 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original));
2554 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final));
2555 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender));
2556 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2558 // Swap in the tab.
2559 NavigateToDestURL();
2561 // The "Prerender: Prerender Page" TaskManager row should disappear, being
2562 // replaced by "Tab: Prerender Page"
2563 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender));
2564 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original));
2565 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2566 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2567 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2570 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerAfterSwapIn) {
2571 // This test is for the old implementation of the task manager. We must
2572 // explicitly disable the new one.
2573 task_manager::browsertest_util::EnableOldTaskManager();
2575 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2576 const base::string16 any_tab = MatchTaskManagerTab("*");
2577 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2579 // Prerender, and swap it in.
2580 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2581 NavigateToDestURL();
2583 // Show the task manager. This populates the model. Importantly, we're doing
2584 // this after the prerender has been swapped in.
2585 chrome::OpenTaskManager(current_browser());
2587 // We should not see a prerender resource in the task manager, just a normal
2588 // page.
2589 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2590 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2591 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2594 // Checks that audio loads are deferred on prerendering.
2595 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Audio) {
2596 PrerenderTestURL("files/prerender/prerender_html5_audio.html",
2597 FINAL_STATUS_USED,
2599 NavigateToDestURL();
2600 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2603 // Checks that audio loads are deferred on prerendering and played back when
2604 // the prerender is swapped in if autoplay is set.
2605 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioAutoplay) {
2606 PrerenderTestURL("files/prerender/prerender_html5_audio_autoplay.html",
2607 FINAL_STATUS_USED,
2609 NavigateToDestURL();
2610 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2613 // Checks that audio loads are deferred on prerendering and played back when
2614 // the prerender is swapped in if js starts playing.
2615 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioJsplay) {
2616 PrerenderTestURL("files/prerender/prerender_html5_audio_jsplay.html",
2617 FINAL_STATUS_USED,
2619 NavigateToDestURL();
2620 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2623 // Checks that video loads are deferred on prerendering.
2624 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Video) {
2625 PrerenderTestURL("files/prerender/prerender_html5_video.html",
2626 FINAL_STATUS_USED,
2628 NavigateToDestURL();
2629 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2632 // Checks that video tags inserted by javascript are deferred and played
2633 // correctly on swap in.
2634 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoJs) {
2635 PrerenderTestURL("files/prerender/prerender_html5_video_script.html",
2636 FINAL_STATUS_USED,
2638 NavigateToDestURL();
2639 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2642 // Checks for correct network events by using a busy sleep the javascript.
2643 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoNetwork) {
2644 DisableJavascriptCalls();
2645 scoped_ptr<TestPrerender> prerender =
2646 PrerenderTestURL("files/prerender/prerender_html5_video_network.html",
2647 FINAL_STATUS_USED,
2649 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
2650 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
2651 NavigateToDestURL();
2652 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2655 // Checks that scripts can retrieve the correct window size while prerendering.
2656 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWindowSize) {
2657 PrerenderTestURL("files/prerender/prerender_size.html",
2658 FINAL_STATUS_USED,
2660 NavigateToDestURL();
2663 // TODO(jam): http://crbug.com/350550
2664 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
2666 // Checks that prerenderers will terminate when the RenderView crashes.
2667 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRendererCrash) {
2668 scoped_ptr<TestPrerender> prerender =
2669 PrerenderTestURL("files/prerender/prerender_page.html",
2670 FINAL_STATUS_RENDERER_CRASHED,
2673 // Navigate to about:crash and then wait for the renderer to crash.
2674 ASSERT_TRUE(prerender->contents());
2675 ASSERT_TRUE(prerender->contents()->prerender_contents());
2676 prerender->contents()->prerender_contents()->GetController().
2677 LoadURL(
2678 GURL(content::kChromeUICrashURL),
2679 content::Referrer(),
2680 ui::PAGE_TRANSITION_TYPED,
2681 std::string());
2682 prerender->WaitForStop();
2684 #endif
2686 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2687 PrerenderPageWithFragment) {
2688 PrerenderTestURL("files/prerender/prerender_page.html#fragment",
2689 FINAL_STATUS_USED,
2692 ChannelDestructionWatcher channel_close_watcher;
2693 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
2694 GetActiveWebContents()->GetRenderProcessHost());
2695 NavigateToDestURL();
2696 channel_close_watcher.WaitForChannelClose();
2698 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2701 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2702 PrerenderPageWithRedirectedFragment) {
2703 PrerenderTestURL(
2704 CreateClientRedirect("files/prerender/prerender_page.html#fragment"),
2705 FINAL_STATUS_USED,
2708 ChannelDestructionWatcher channel_close_watcher;
2709 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
2710 GetActiveWebContents()->GetRenderProcessHost());
2711 NavigateToDestURL();
2712 channel_close_watcher.WaitForChannelClose();
2714 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2717 // Checks that we do not use a prerendered page when navigating from
2718 // the main page to a fragment.
2719 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2720 PrerenderPageNavigateFragment) {
2721 PrerenderTestURL("files/prerender/no_prerender_page.html",
2722 FINAL_STATUS_APP_TERMINATING,
2724 NavigateToURLWithDisposition(
2725 "files/prerender/no_prerender_page.html#fragment",
2726 CURRENT_TAB, false);
2729 // Checks that we do not use a prerendered page when we prerender a fragment
2730 // but navigate to the main page.
2731 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2732 PrerenderFragmentNavigatePage) {
2733 PrerenderTestURL("files/prerender/no_prerender_page.html#fragment",
2734 FINAL_STATUS_APP_TERMINATING,
2736 NavigateToURLWithDisposition(
2737 "files/prerender/no_prerender_page.html",
2738 CURRENT_TAB, false);
2741 // Checks that we do not use a prerendered page when we prerender a fragment
2742 // but navigate to a different fragment on the same page.
2743 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2744 PrerenderFragmentNavigateFragment) {
2745 PrerenderTestURL("files/prerender/no_prerender_page.html#other_fragment",
2746 FINAL_STATUS_APP_TERMINATING,
2748 NavigateToURLWithDisposition(
2749 "files/prerender/no_prerender_page.html#fragment",
2750 CURRENT_TAB, false);
2753 // Checks that we do not use a prerendered page when the page uses a client
2754 // redirect to refresh from a fragment on the same page.
2755 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2756 PrerenderClientRedirectFromFragment) {
2757 PrerenderTestURL(
2758 CreateClientRedirect("files/prerender/no_prerender_page.html#fragment"),
2759 FINAL_STATUS_APP_TERMINATING,
2761 NavigateToURLWithDisposition(
2762 "files/prerender/no_prerender_page.html",
2763 CURRENT_TAB, false);
2766 // Checks that we do not use a prerendered page when the page uses a client
2767 // redirect to refresh to a fragment on the same page.
2768 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2769 PrerenderClientRedirectToFragment) {
2770 PrerenderTestURL(
2771 CreateClientRedirect("files/prerender/no_prerender_page.html"),
2772 FINAL_STATUS_APP_TERMINATING,
2774 NavigateToURLWithDisposition(
2775 "files/prerender/no_prerender_page.html#fragment",
2776 CURRENT_TAB, false);
2779 // Checks that we correctly use a prerendered page when the page uses JS to set
2780 // the window.location.hash to a fragment on the same page.
2781 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2782 PrerenderPageChangeFragmentLocationHash) {
2783 PrerenderTestURL("files/prerender/prerender_fragment_location_hash.html",
2784 FINAL_STATUS_USED,
2786 NavigateToURL("files/prerender/prerender_fragment_location_hash.html");
2789 // Checks that prerendering a PNG works correctly.
2790 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImagePng) {
2791 DisableJavascriptCalls();
2792 PrerenderTestURL("files/prerender/image.png", FINAL_STATUS_USED, 1);
2793 NavigateToDestURL();
2796 // Checks that prerendering a JPG works correctly.
2797 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImageJpeg) {
2798 DisableJavascriptCalls();
2799 PrerenderTestURL("files/prerender/image.jpeg", FINAL_STATUS_USED, 1);
2800 NavigateToDestURL();
2803 // Checks that a prerender of a CRX will result in a cancellation due to
2804 // download.
2805 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCrx) {
2806 PrerenderTestURL("files/prerender/extension.crx", FINAL_STATUS_DOWNLOAD, 0);
2809 // Checks that xhr GET requests allow prerenders.
2810 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrGet) {
2811 PrerenderTestURL("files/prerender/prerender_xhr_get.html",
2812 FINAL_STATUS_USED,
2814 NavigateToDestURL();
2817 // Checks that xhr HEAD requests allow prerenders.
2818 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrHead) {
2819 PrerenderTestURL("files/prerender/prerender_xhr_head.html",
2820 FINAL_STATUS_USED,
2822 NavigateToDestURL();
2825 // Checks that xhr OPTIONS requests allow prerenders.
2826 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrOptions) {
2827 PrerenderTestURL("files/prerender/prerender_xhr_options.html",
2828 FINAL_STATUS_USED,
2830 NavigateToDestURL();
2833 // Checks that xhr TRACE requests allow prerenders.
2834 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrTrace) {
2835 PrerenderTestURL("files/prerender/prerender_xhr_trace.html",
2836 FINAL_STATUS_USED,
2838 NavigateToDestURL();
2841 // Checks that xhr POST requests allow prerenders.
2842 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPost) {
2843 PrerenderTestURL("files/prerender/prerender_xhr_post.html",
2844 FINAL_STATUS_USED,
2846 NavigateToDestURL();
2849 // Checks that xhr PUT cancels prerenders.
2850 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPut) {
2851 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
2852 FINAL_STATUS_INVALID_HTTP_METHOD,
2856 // Checks that xhr DELETE cancels prerenders.
2857 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrDelete) {
2858 PrerenderTestURL("files/prerender/prerender_xhr_delete.html",
2859 FINAL_STATUS_INVALID_HTTP_METHOD,
2863 // Checks that a top-level page which would trigger an SSL error is canceled.
2864 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorTopLevel) {
2865 net::SpawnedTestServer::SSLOptions ssl_options;
2866 ssl_options.server_certificate =
2867 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2868 net::SpawnedTestServer https_server(
2869 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2870 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2871 ASSERT_TRUE(https_server.Start());
2872 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2873 PrerenderTestURL(https_url,
2874 FINAL_STATUS_SSL_ERROR,
2878 // Checks that an SSL error that comes from a subresource does not cancel
2879 // the page. Non-main-frame requests are simply cancelled if they run into
2880 // an SSL problem.
2881 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorSubresource) {
2882 net::SpawnedTestServer::SSLOptions ssl_options;
2883 ssl_options.server_certificate =
2884 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2885 net::SpawnedTestServer https_server(
2886 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2887 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2888 ASSERT_TRUE(https_server.Start());
2889 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
2890 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2891 replacement_text.push_back(
2892 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
2893 std::string replacement_path;
2894 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2895 "files/prerender/prerender_with_image.html",
2896 replacement_text,
2897 &replacement_path));
2898 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2899 NavigateToDestURL();
2902 // Checks that an SSL error that comes from an iframe does not cancel
2903 // the page. Non-main-frame requests are simply cancelled if they run into
2904 // an SSL problem.
2905 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorIframe) {
2906 net::SpawnedTestServer::SSLOptions ssl_options;
2907 ssl_options.server_certificate =
2908 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2909 net::SpawnedTestServer https_server(
2910 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2911 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2912 ASSERT_TRUE(https_server.Start());
2913 GURL https_url = https_server.GetURL(
2914 "files/prerender/prerender_embedded_content.html");
2915 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2916 replacement_text.push_back(
2917 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
2918 std::string replacement_path;
2919 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2920 "files/prerender/prerender_with_iframe.html",
2921 replacement_text,
2922 &replacement_path));
2923 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2924 NavigateToDestURL();
2927 // Checks that we cancel correctly when window.print() is called.
2928 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPrint) {
2929 DisableLoadEventCheck();
2930 PrerenderTestURL("files/prerender/prerender_print.html",
2931 FINAL_STATUS_WINDOW_PRINT,
2935 // Checks that prerenders do not get swapped into target pages that have opened
2936 // popups; the BrowsingInstance is not empty.
2937 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderTargetHasPopup) {
2938 PrerenderTestURL("files/prerender/prerender_page.html",
2939 FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE,
2941 OpenURLViaWindowOpen(GURL(url::kAboutBlankURL));
2943 // Switch back to the current tab and attempt to swap it in.
2944 current_browser()->tab_strip_model()->ActivateTabAt(0, true);
2945 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
2948 class TestClientCertStore : public net::ClientCertStore {
2949 public:
2950 TestClientCertStore() {}
2951 ~TestClientCertStore() override {}
2953 // net::ClientCertStore:
2954 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info,
2955 net::CertificateList* selected_certs,
2956 const base::Closure& callback) override {
2957 *selected_certs = net::CertificateList(
2958 1, scoped_refptr<net::X509Certificate>(
2959 new net::X509Certificate("test", "test", base::Time(), base::Time())));
2960 callback.Run();
2964 scoped_ptr<net::ClientCertStore> CreateCertStore() {
2965 return scoped_ptr<net::ClientCertStore>(new TestClientCertStore);
2968 // Checks that a top-level page which would normally request an SSL client
2969 // certificate will never be seen since it's an https top-level resource.
2970 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2971 PrerenderSSLClientCertTopLevel) {
2972 ProfileIOData::FromResourceContext(
2973 current_browser()->profile()->GetResourceContext())->
2974 set_client_cert_store_factory_for_testing(
2975 base::Bind(&CreateCertStore));
2976 net::SpawnedTestServer::SSLOptions ssl_options;
2977 ssl_options.request_client_certificate = true;
2978 net::SpawnedTestServer https_server(
2979 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2980 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2981 ASSERT_TRUE(https_server.Start());
2982 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2983 PrerenderTestURL(https_url, FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED, 0);
2986 // Checks that an SSL Client Certificate request that originates from a
2987 // subresource will cancel the prerendered page.
2988 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2989 PrerenderSSLClientCertSubresource) {
2990 ProfileIOData::FromResourceContext(
2991 current_browser()->profile()->GetResourceContext())->
2992 set_client_cert_store_factory_for_testing(
2993 base::Bind(&CreateCertStore));
2994 net::SpawnedTestServer::SSLOptions ssl_options;
2995 ssl_options.request_client_certificate = true;
2996 net::SpawnedTestServer https_server(
2997 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2998 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2999 ASSERT_TRUE(https_server.Start());
3000 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
3001 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3002 replacement_text.push_back(
3003 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
3004 std::string replacement_path;
3005 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3006 "files/prerender/prerender_with_image.html",
3007 replacement_text,
3008 &replacement_path));
3009 PrerenderTestURL(replacement_path,
3010 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
3014 // Checks that an SSL Client Certificate request that originates from an
3015 // iframe will cancel the prerendered page.
3016 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLClientCertIframe) {
3017 ProfileIOData::FromResourceContext(
3018 current_browser()->profile()->GetResourceContext())->
3019 set_client_cert_store_factory_for_testing(
3020 base::Bind(&CreateCertStore));
3021 net::SpawnedTestServer::SSLOptions ssl_options;
3022 ssl_options.request_client_certificate = true;
3023 net::SpawnedTestServer https_server(
3024 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
3025 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3026 ASSERT_TRUE(https_server.Start());
3027 GURL https_url = https_server.GetURL(
3028 "files/prerender/prerender_embedded_content.html");
3029 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3030 replacement_text.push_back(
3031 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
3032 std::string replacement_path;
3033 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3034 "files/prerender/prerender_with_iframe.html",
3035 replacement_text,
3036 &replacement_path));
3037 PrerenderTestURL(replacement_path,
3038 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
3042 #if defined(FULL_SAFE_BROWSING)
3043 // Ensures that we do not prerender pages with a safe browsing
3044 // interstitial.
3045 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingTopLevel) {
3046 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3047 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3048 url, SB_THREAT_TYPE_URL_MALWARE);
3049 PrerenderTestURL("files/prerender/prerender_page.html",
3050 FINAL_STATUS_SAFE_BROWSING, 0);
3053 // Ensures that server redirects to a malware page will cancel prerenders.
3054 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3055 PrerenderSafeBrowsingServerRedirect) {
3056 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3057 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3058 url, SB_THREAT_TYPE_URL_MALWARE);
3059 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
3060 FINAL_STATUS_SAFE_BROWSING,
3064 // Ensures that client redirects to a malware page will cancel prerenders.
3065 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3066 PrerenderSafeBrowsingClientRedirect) {
3067 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3068 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3069 url, SB_THREAT_TYPE_URL_MALWARE);
3070 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
3071 FINAL_STATUS_SAFE_BROWSING,
3075 // Ensures that we do not prerender pages which have a malware subresource.
3076 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingSubresource) {
3077 GURL image_url = test_server()->GetURL("files/prerender/image.jpeg");
3078 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3079 image_url, SB_THREAT_TYPE_URL_MALWARE);
3080 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3081 replacement_text.push_back(
3082 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3083 std::string replacement_path;
3084 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3085 "files/prerender/prerender_with_image.html",
3086 replacement_text,
3087 &replacement_path));
3088 PrerenderTestURL(replacement_path,
3089 FINAL_STATUS_SAFE_BROWSING,
3093 // Ensures that we do not prerender pages which have a malware iframe.
3094 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingIframe) {
3095 GURL iframe_url = test_server()->GetURL(
3096 "files/prerender/prerender_embedded_content.html");
3097 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3098 iframe_url, SB_THREAT_TYPE_URL_MALWARE);
3099 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3100 replacement_text.push_back(
3101 std::make_pair("REPLACE_WITH_URL", iframe_url.spec()));
3102 std::string replacement_path;
3103 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3104 "files/prerender/prerender_with_iframe.html",
3105 replacement_text,
3106 &replacement_path));
3107 PrerenderTestURL(replacement_path,
3108 FINAL_STATUS_SAFE_BROWSING,
3112 #endif
3114 // Checks that a local storage read will not cause prerender to fail.
3115 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageRead) {
3116 PrerenderTestURL("files/prerender/prerender_localstorage_read.html",
3117 FINAL_STATUS_USED,
3119 NavigateToDestURL();
3122 // Checks that a local storage write will not cause prerender to fail.
3123 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) {
3124 PrerenderTestURL("files/prerender/prerender_localstorage_write.html",
3125 FINAL_STATUS_USED,
3127 NavigateToDestURL();
3130 // Checks that the favicon is properly loaded on prerender.
3131 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderFavicon) {
3132 scoped_ptr<TestPrerender> prerender =
3133 PrerenderTestURL("files/prerender/prerender_favicon.html",
3134 FINAL_STATUS_USED,
3136 NavigateToDestURL();
3138 favicon::FaviconDriver* favicon_driver =
3139 favicon::ContentFaviconDriver::FromWebContents(GetActiveWebContents());
3140 if (!favicon_driver->FaviconIsValid()) {
3141 // If the favicon has not been set yet, wait for it to be.
3142 FaviconUpdateWatcher favicon_update_watcher(GetActiveWebContents());
3143 favicon_update_watcher.Wait();
3145 EXPECT_TRUE(favicon_driver->FaviconIsValid());
3148 // Checks that when a prerendered page is swapped in to a referring page, the
3149 // unload handlers on the referring page are executed.
3150 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderUnload) {
3151 // Matches URL in prerender_loader_with_unload.html.
3152 const GURL unload_url("http://unload-url.test");
3153 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3154 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3155 RequestCounter unload_counter;
3156 BrowserThread::PostTask(
3157 BrowserThread::IO, FROM_HERE,
3158 base::Bind(&CreateCountingInterceptorOnIO,
3159 unload_url, empty_file, unload_counter.AsWeakPtr()));
3161 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3162 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3163 NavigateToDestURL();
3164 unload_counter.WaitForCount(1);
3167 // Checks that a hanging unload on the referring page of a prerender swap does
3168 // not crash the browser on exit.
3169 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHangingUnload) {
3170 // Matches URL in prerender_loader_with_unload.html.
3171 const GURL hang_url("http://unload-url.test");
3172 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3173 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3174 BrowserThread::PostTask(
3175 BrowserThread::IO, FROM_HERE,
3176 base::Bind(&CreateHangingFirstRequestInterceptorOnIO,
3177 hang_url, empty_file,
3178 base::Closure()));
3180 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3181 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3182 NavigateToDestURL();
3186 // Checks that when the history is cleared, prerendering is cancelled and
3187 // prerendering history is cleared.
3188 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearHistory) {
3189 scoped_ptr<TestPrerender> prerender =
3190 PrerenderTestURL("files/prerender/prerender_page.html",
3191 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
3194 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_HISTORY);
3195 prerender->WaitForStop();
3197 // Make sure prerender history was cleared.
3198 EXPECT_EQ(0, GetHistoryLength());
3201 // Checks that when the cache is cleared, prerenders are cancelled but
3202 // prerendering history is not cleared.
3203 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearCache) {
3204 scoped_ptr<TestPrerender> prerender =
3205 PrerenderTestURL("files/prerender/prerender_page.html",
3206 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
3209 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_CACHE);
3210 prerender->WaitForStop();
3212 // Make sure prerender history was not cleared. Not a vital behavior, but
3213 // used to compare with PrerenderClearHistory test.
3214 EXPECT_EQ(1, GetHistoryLength());
3217 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelAll) {
3218 scoped_ptr<TestPrerender> prerender =
3219 PrerenderTestURL("files/prerender/prerender_page.html",
3220 FINAL_STATUS_CANCELLED,
3223 GetPrerenderManager()->CancelAllPrerenders();
3224 prerender->WaitForStop();
3226 EXPECT_FALSE(prerender->contents());
3229 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderEvents) {
3230 scoped_ptr<TestPrerender> prerender =
3231 PrerenderTestURL("files/prerender/prerender_page.html",
3232 FINAL_STATUS_CANCELLED, 1);
3234 GetPrerenderManager()->CancelAllPrerenders();
3235 prerender->WaitForStop();
3237 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
3238 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
3239 EXPECT_FALSE(HadPrerenderEventErrors());
3242 // Cancels the prerender of a page with its own prerender. The second prerender
3243 // should never be started.
3244 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3245 PrerenderCancelPrerenderWithPrerender) {
3246 scoped_ptr<TestPrerender> prerender =
3247 PrerenderTestURL("files/prerender/prerender_infinite_a.html",
3248 FINAL_STATUS_CANCELLED,
3251 GetPrerenderManager()->CancelAllPrerenders();
3252 prerender->WaitForStop();
3254 EXPECT_FALSE(prerender->contents());
3257 // Prerendering and history tests.
3258 // The prerendered page is navigated to in several ways [navigate via
3259 // omnibox, click on link, key-modified click to open in background tab, etc],
3260 // followed by a navigation to another page from the prerendered page, followed
3261 // by a back navigation.
3263 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNavigateClickGoBack) {
3264 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3265 FINAL_STATUS_USED,
3267 NavigateToDestURL();
3268 ClickToNextPageAfterPrerender();
3269 GoBackToPrerender();
3272 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNavigateNavigateGoBack) {
3273 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3274 FINAL_STATUS_USED,
3276 NavigateToDestURL();
3277 NavigateToNextPageAfterPrerender();
3278 GoBackToPrerender();
3281 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickClickGoBack) {
3282 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3283 FINAL_STATUS_USED,
3285 OpenDestURLViaClick();
3286 ClickToNextPageAfterPrerender();
3287 GoBackToPrerender();
3290 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNavigateGoBack) {
3291 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3292 FINAL_STATUS_USED,
3294 OpenDestURLViaClick();
3295 NavigateToNextPageAfterPrerender();
3296 GoBackToPrerender();
3299 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewWindow) {
3300 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3301 FINAL_STATUS_APP_TERMINATING, 1);
3302 OpenDestURLViaClickNewWindow();
3305 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewForegroundTab) {
3306 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3307 FINAL_STATUS_APP_TERMINATING, 1);
3308 OpenDestURLViaClickNewForegroundTab();
3311 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3312 NavigateToPrerenderedPageWhenDevToolsAttached) {
3313 DisableJavascriptCalls();
3314 WebContents* web_contents =
3315 current_browser()->tab_strip_model()->GetActiveWebContents();
3316 scoped_refptr<DevToolsAgentHost> agent(
3317 DevToolsAgentHost::GetOrCreateFor(web_contents));
3318 FakeDevToolsClient client;
3319 agent->AttachClient(&client);
3320 const char* url = "files/prerender/prerender_page.html";
3321 PrerenderTestURL(url, FINAL_STATUS_DEVTOOLS_ATTACHED, 1);
3322 NavigateToURLWithDisposition(url, CURRENT_TAB, false);
3323 agent->DetachClient();
3326 // Validate that the sessionStorage namespace remains the same when swapping
3327 // in a prerendered page.
3328 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSessionStorage) {
3329 set_loader_path("files/prerender/prerender_loader_with_session_storage.html");
3330 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
3331 FINAL_STATUS_USED,
3333 NavigateToDestURL();
3334 GoBackToPageBeforePrerender();
3337 // Checks that the control group works. An XHR PUT cannot be detected in the
3338 // control group.
3339 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, ControlGroup) {
3340 RestorePrerenderMode restore_prerender_mode;
3341 PrerenderManager::SetMode(
3342 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
3343 DisableJavascriptCalls();
3344 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3345 FINAL_STATUS_WOULD_HAVE_BEEN_USED, 0);
3346 NavigateToDestURL();
3349 // Checks that the control group correctly hits WOULD_HAVE_BEEN_USED
3350 // renderer-initiated navigations. (This verifies that the ShouldFork logic
3351 // behaves correctly.)
3352 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, ControlGroupRendererInitiated) {
3353 RestorePrerenderMode restore_prerender_mode;
3354 PrerenderManager::SetMode(
3355 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
3356 DisableJavascriptCalls();
3357 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3358 FINAL_STATUS_WOULD_HAVE_BEEN_USED, 0);
3359 OpenDestURLViaClick();
3362 // Make sure that the MatchComplete dummy works in the normal case. Once
3363 // a prerender is cancelled because of a script, a dummy must be created to
3364 // account for the MatchComplete case, and it must have a final status of
3365 // FINAL_STATUS_WOULD_HAVE_BEEN_USED.
3366 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, MatchCompleteDummy) {
3367 RestorePrerenderMode restore_prerender_mode;
3368 PrerenderManager::SetMode(
3369 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3371 std::vector<FinalStatus> expected_final_status_queue;
3372 expected_final_status_queue.push_back(FINAL_STATUS_INVALID_HTTP_METHOD);
3373 expected_final_status_queue.push_back(FINAL_STATUS_WOULD_HAVE_BEEN_USED);
3374 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3375 expected_final_status_queue, 1);
3376 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3377 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3378 histogram_tester().ExpectTotalCount(
3379 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3380 histogram_tester().ExpectTotalCount(
3381 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
3383 NavigateToDestURL();
3384 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3385 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3387 histogram_tester().ExpectTotalCount(
3388 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3391 // Checks that the referrer policy is used when prerendering.
3392 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrerPolicy) {
3393 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3394 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3395 FINAL_STATUS_USED,
3397 NavigateToDestURL();
3400 // Checks that the referrer policy is used when prerendering on HTTPS.
3401 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3402 PrerenderSSLReferrerPolicy) {
3403 UseHttpsSrcServer();
3404 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3405 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3406 FINAL_STATUS_USED,
3408 NavigateToDestURL();
3411 // Checks that the referrer policy is used when prerendering is cancelled.
3412 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelReferrerPolicy) {
3413 scoped_ptr<TestContentBrowserClient> test_content_browser_client(
3414 new TestContentBrowserClient);
3415 content::ContentBrowserClient* original_browser_client =
3416 content::SetBrowserClientForTesting(test_content_browser_client.get());
3418 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3419 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3420 FINAL_STATUS_CANCELLED,
3422 OpenDestURLViaClick();
3424 bool display_test_result = false;
3425 WebContents* web_contents =
3426 browser()->tab_strip_model()->GetActiveWebContents();
3427 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
3428 web_contents,
3429 "window.domAutomationController.send(DidDisplayPass())",
3430 &display_test_result));
3431 EXPECT_TRUE(display_test_result);
3433 content::SetBrowserClientForTesting(original_browser_client);
3436 // Test interaction of the webNavigation and tabs API with prerender.
3437 class PrerenderBrowserTestWithExtensions : public PrerenderBrowserTest,
3438 public ExtensionApiTest {
3439 public:
3440 PrerenderBrowserTestWithExtensions() {
3441 // The individual tests start the test server through ExtensionApiTest, so
3442 // the port number can be passed through to the extension.
3443 autostart_test_server_ = false;
3446 void SetUp() override { PrerenderBrowserTest::SetUp(); }
3448 void SetUpCommandLine(base::CommandLine* command_line) override {
3449 PrerenderBrowserTest::SetUpCommandLine(command_line);
3450 ExtensionApiTest::SetUpCommandLine(command_line);
3453 void SetUpInProcessBrowserTestFixture() override {
3454 PrerenderBrowserTest::SetUpInProcessBrowserTestFixture();
3455 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
3458 void TearDownInProcessBrowserTestFixture() override {
3459 PrerenderBrowserTest::TearDownInProcessBrowserTestFixture();
3460 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
3463 void TearDownOnMainThread() override {
3464 PrerenderBrowserTest::TearDownOnMainThread();
3465 ExtensionApiTest::TearDownOnMainThread();
3468 void SetUpOnMainThread() override {
3469 PrerenderBrowserTest::SetUpOnMainThread();
3473 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, WebNavigation) {
3474 ASSERT_TRUE(StartSpawnedTestServer());
3475 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3477 // Wait for the extension to set itself up and return control to us.
3478 ASSERT_TRUE(RunExtensionTest("webnavigation/prerender")) << message_;
3480 extensions::ResultCatcher catcher;
3482 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3484 ChannelDestructionWatcher channel_close_watcher;
3485 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
3486 GetActiveWebContents()->GetRenderProcessHost());
3487 NavigateToDestURL();
3488 channel_close_watcher.WaitForChannelClose();
3490 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3491 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
3494 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, TabsApi) {
3495 ASSERT_TRUE(StartSpawnedTestServer());
3496 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3498 // Wait for the extension to set itself up and return control to us.
3499 ASSERT_TRUE(RunExtensionTest("tabs/on_replaced")) << message_;
3501 extensions::ResultCatcher catcher;
3503 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3505 ChannelDestructionWatcher channel_close_watcher;
3506 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
3507 GetActiveWebContents()->GetRenderProcessHost());
3508 NavigateToDestURL();
3509 channel_close_watcher.WaitForChannelClose();
3511 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3512 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
3515 // Test that prerenders abort when navigating to a stream.
3516 // See chrome/browser/extensions/api/streams_private/streams_private_apitest.cc
3517 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, StreamsTest) {
3518 RestorePrerenderMode restore_prerender_mode;
3519 PrerenderManager::SetMode(
3520 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3522 ASSERT_TRUE(StartSpawnedTestServer());
3524 const extensions::Extension* extension = LoadExtension(
3525 test_data_dir_.AppendASCII("streams_private/handle_mime_type"));
3526 ASSERT_TRUE(extension);
3527 EXPECT_EQ(std::string(extension_misc::kMimeHandlerPrivateTestExtensionId),
3528 extension->id());
3529 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
3530 ASSERT_TRUE(handler);
3531 EXPECT_TRUE(handler->CanHandleMIMEType("application/msword"));
3533 PrerenderTestURL("files/prerender/document.doc", FINAL_STATUS_DOWNLOAD, 0);
3535 // Sanity-check that the extension would have picked up the stream in a normal
3536 // navigation had prerender not intercepted it.
3537 // streams_private/handle_mime_type reports success if it has handled the
3538 // application/msword type.
3539 extensions::ResultCatcher catcher;
3540 NavigateToDestURL();
3541 EXPECT_TRUE(catcher.GetNextResult());
3544 // Checks that non-http/https/chrome-extension subresource cancels the
3545 // prerender.
3546 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3547 PrerenderCancelSubresourceUnsupportedScheme) {
3548 GURL image_url = GURL("invalidscheme://www.google.com/test.jpg");
3549 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3550 replacement_text.push_back(
3551 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3552 std::string replacement_path;
3553 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3554 "files/prerender/prerender_with_image.html",
3555 replacement_text,
3556 &replacement_path));
3557 PrerenderTestURL(replacement_path, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3560 // Ensure that about:blank is permitted for any subresource.
3561 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3562 PrerenderAllowAboutBlankSubresource) {
3563 GURL image_url = GURL("about:blank");
3564 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3565 replacement_text.push_back(
3566 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3567 std::string replacement_path;
3568 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3569 "files/prerender/prerender_with_image.html",
3570 replacement_text,
3571 &replacement_path));
3572 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3573 NavigateToDestURL();
3576 // Checks that non-http/https/chrome-extension subresource cancels the prerender
3577 // on redirect.
3578 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3579 PrerenderCancelSubresourceRedirectUnsupportedScheme) {
3580 GURL image_url = test_server()->GetURL(
3581 CreateServerRedirect("invalidscheme://www.google.com/test.jpg"));
3582 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3583 replacement_text.push_back(
3584 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3585 std::string replacement_path;
3586 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3587 "files/prerender/prerender_with_image.html",
3588 replacement_text,
3589 &replacement_path));
3590 PrerenderTestURL(replacement_path, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3593 // Checks that chrome-extension subresource does not cancel the prerender.
3594 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3595 PrerenderKeepSubresourceExtensionScheme) {
3596 GURL image_url = GURL("chrome-extension://abcdefg/test.jpg");
3597 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3598 replacement_text.push_back(
3599 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3600 std::string replacement_path;
3601 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3602 "files/prerender/prerender_with_image.html",
3603 replacement_text,
3604 &replacement_path));
3605 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3606 NavigateToDestURL();
3609 // Checks that redirect to chrome-extension subresource does not cancel the
3610 // prerender.
3611 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3612 PrerenderKeepSubresourceRedirectExtensionScheme) {
3613 GURL image_url = test_server()->GetURL(
3614 CreateServerRedirect("chrome-extension://abcdefg/test.jpg"));
3615 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3616 replacement_text.push_back(
3617 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3618 std::string replacement_path;
3619 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3620 "files/prerender/prerender_with_image.html",
3621 replacement_text,
3622 &replacement_path));
3623 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3624 NavigateToDestURL();
3627 // Checks that non-http/https main page redirects cancel the prerender.
3628 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3629 PrerenderCancelMainFrameRedirectUnsupportedScheme) {
3630 GURL url = test_server()->GetURL(
3631 CreateServerRedirect("invalidscheme://www.google.com/test.html"));
3632 PrerenderTestURL(url, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3635 // Checks that media source video loads are deferred on prerendering.
3636 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5MediaSourceVideo) {
3637 PrerenderTestURL("files/prerender/prerender_html5_video_media_source.html",
3638 FINAL_STATUS_USED,
3640 NavigateToDestURL();
3641 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
3644 // Checks that a prerender that creates an audio stream (via a WebAudioDevice)
3645 // is cancelled.
3646 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWebAudioDevice) {
3647 DisableLoadEventCheck();
3648 PrerenderTestURL("files/prerender/prerender_web_audio_device.html",
3649 FINAL_STATUS_CREATING_AUDIO_STREAM, 0);
3652 // Checks that prerenders do not swap in to WebContents being captured.
3653 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCapturedWebContents) {
3654 PrerenderTestURL("files/prerender/prerender_page.html",
3655 FINAL_STATUS_PAGE_BEING_CAPTURED, 1);
3656 WebContents* web_contents = GetActiveWebContents();
3657 web_contents->IncrementCapturerCount(gfx::Size());
3658 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3659 web_contents->DecrementCapturerCount();
3662 // Checks that prerenders are aborted on cross-process navigation from
3663 // a server redirect.
3664 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3665 PrerenderCrossProcessServerRedirect) {
3666 // Force everything to be a process swap.
3667 SwapProcessesContentBrowserClient test_browser_client;
3668 content::ContentBrowserClient* original_browser_client =
3669 content::SetBrowserClientForTesting(&test_browser_client);
3671 PrerenderTestURL(
3672 CreateServerRedirect("files/prerender/prerender_page.html"),
3673 FINAL_STATUS_OPEN_URL, 0);
3675 content::SetBrowserClientForTesting(original_browser_client);
3678 // Checks that URLRequests for prerenders being aborted on cross-process
3679 // navigation from a server redirect are cleaned up, so they don't keep cache
3680 // entries locked.
3681 // See http://crbug.com/341134
3682 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3683 PrerenderCrossProcessServerRedirectNoHang) {
3684 const char kDestPath[] = "files/prerender/prerender_page.html";
3685 // Force everything to be a process swap.
3686 SwapProcessesContentBrowserClient test_browser_client;
3687 content::ContentBrowserClient* original_browser_client =
3688 content::SetBrowserClientForTesting(&test_browser_client);
3690 PrerenderTestURL(CreateServerRedirect(kDestPath), FINAL_STATUS_OPEN_URL, 0);
3692 ui_test_utils::NavigateToURL(
3693 browser(),
3694 test_server()->GetURL(kDestPath));
3696 content::SetBrowserClientForTesting(original_browser_client);
3699 // Checks that prerenders are aborted on cross-process navigation from
3700 // a client redirect.
3701 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3702 PrerenderCrossProcessClientRedirect) {
3703 // Cross-process navigation logic for renderer-initiated navigations
3704 // is partially controlled by the renderer, namely
3705 // ChromeContentRendererClient. This test instead relies on the Web
3706 // Store triggering such navigations.
3707 std::string webstore_url = extension_urls::GetWebstoreLaunchURL();
3709 // Mock out requests to the Web Store.
3710 base::FilePath file(GetTestPath("prerender_page.html"));
3711 BrowserThread::PostTask(
3712 BrowserThread::IO, FROM_HERE,
3713 base::Bind(&CreateMockInterceptorOnIO, GURL(webstore_url), file));
3715 PrerenderTestURL(CreateClientRedirect(webstore_url),
3716 FINAL_STATUS_OPEN_URL, 1);
3719 // Checks that canceling a MatchComplete dummy doesn't result in two
3720 // stop events.
3721 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, CancelMatchCompleteDummy) {
3722 RestorePrerenderMode restore_prerender_mode;
3723 PrerenderManager::SetMode(
3724 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3726 std::vector<FinalStatus> expected_final_status_queue;
3727 expected_final_status_queue.push_back(FINAL_STATUS_JAVASCRIPT_ALERT);
3728 expected_final_status_queue.push_back(FINAL_STATUS_CANCELLED);
3729 ScopedVector<TestPrerender> prerenders =
3730 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
3731 expected_final_status_queue, 0);
3733 // Cancel the MatchComplete dummy.
3734 GetPrerenderManager()->CancelAllPrerenders();
3735 prerenders[1]->WaitForStop();
3737 // Check the referring page only got one copy of the event.
3738 EXPECT_FALSE(HadPrerenderEventErrors());
3741 // Checks that a deferred redirect to an image is not loaded until the page is
3742 // visible. Also test the right histogram events are emitted in this case.
3743 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredImage) {
3744 DisableJavascriptCalls();
3746 // The prerender will not completely load until after the swap, so wait for a
3747 // title change before calling DidPrerenderPass.
3748 scoped_ptr<TestPrerender> prerender =
3749 PrerenderTestURL(
3750 "files/prerender/prerender_deferred_image.html",
3751 FINAL_STATUS_USED, 0);
3752 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
3753 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
3754 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
3755 EXPECT_EQ(0, prerender->number_of_loads());
3756 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3757 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3758 histogram_tester().ExpectTotalCount(
3759 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3760 histogram_tester().ExpectTotalCount(
3761 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3763 // Swap.
3764 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
3765 GetActiveWebContents());
3766 ui_test_utils::NavigateToURLWithDisposition(
3767 current_browser(), dest_url(), CURRENT_TAB,
3768 ui_test_utils::BROWSER_TEST_NONE);
3769 swap_observer.Wait();
3771 // The prerender never observes the final load.
3772 EXPECT_EQ(0, prerender->number_of_loads());
3774 // Now check DidDisplayPass.
3775 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3777 histogram_tester().ExpectTotalCount(
3778 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3779 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3780 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3782 histogram_tester().ExpectTotalCount(
3783 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3786 // Checks that a deferred redirect to an image is not loaded until the
3787 // page is visible, even after another redirect.
3788 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3789 PrerenderDeferredImageAfterRedirect) {
3790 DisableJavascriptCalls();
3792 // The prerender will not completely load until after the swap, so wait for a
3793 // title change before calling DidPrerenderPass.
3794 scoped_ptr<TestPrerender> prerender =
3795 PrerenderTestURL(
3796 "files/prerender/prerender_deferred_image.html",
3797 FINAL_STATUS_USED, 0);
3798 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
3799 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
3800 EXPECT_EQ(0, prerender->number_of_loads());
3802 // Swap.
3803 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
3804 GetActiveWebContents());
3805 ui_test_utils::NavigateToURLWithDisposition(
3806 current_browser(), dest_url(), CURRENT_TAB,
3807 ui_test_utils::BROWSER_TEST_NONE);
3808 swap_observer.Wait();
3810 // The prerender never observes the final load.
3811 EXPECT_EQ(0, prerender->number_of_loads());
3813 // Now check DidDisplayPass.
3814 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3817 // Checks that deferred redirects in the main frame are followed.
3818 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredMainFrame) {
3819 DisableJavascriptCalls();
3820 PrerenderTestURL(
3821 "files/prerender/image-deferred.png",
3822 FINAL_STATUS_USED, 1);
3823 NavigateToDestURL();
3826 // Checks that deferred redirects in the main frame are followed, even
3827 // with a double-redirect.
3828 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3829 PrerenderDeferredMainFrameAfterRedirect) {
3830 DisableJavascriptCalls();
3831 PrerenderTestURL(
3832 CreateServerRedirect("files/prerender/image-deferred.png"),
3833 FINAL_STATUS_USED, 1);
3834 NavigateToDestURL();
3837 // Checks that deferred redirects in a synchronous XHR abort the
3838 // prerender.
3839 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredSynchronousXHR) {
3840 RestorePrerenderMode restore_prerender_mode;
3841 PrerenderManager::SetMode(
3842 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3843 PrerenderTestURL("files/prerender/prerender_deferred_sync_xhr.html",
3844 FINAL_STATUS_BAD_DEFERRED_REDIRECT, 0);
3845 NavigateToDestURL();
3848 // Checks that prerenders are not swapped for navigations with extra headers.
3849 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExtraHeadersNoSwap) {
3850 PrerenderTestURL("files/prerender/prerender_page.html",
3851 FINAL_STATUS_APP_TERMINATING, 1);
3853 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3854 ui::PAGE_TRANSITION_TYPED, false);
3855 params.extra_headers = "X-Custom-Header: 42\r\n";
3856 NavigateToURLWithParams(params, false);
3859 // Checks that prerenders are not swapped for navigations with browser-initiated
3860 // POST data.
3861 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3862 PrerenderBrowserInitiatedPostNoSwap) {
3863 PrerenderTestURL("files/prerender/prerender_page.html",
3864 FINAL_STATUS_APP_TERMINATING, 1);
3866 std::string post_data = "DATA";
3867 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3868 ui::PAGE_TRANSITION_TYPED, false);
3869 params.uses_post = true;
3870 params.browser_initiated_post_data =
3871 base::RefCountedString::TakeString(&post_data);
3872 NavigateToURLWithParams(params, false);
3875 // Checks that the prerendering of a page is canceled correctly when the
3876 // prerendered page tries to make a second navigation entry.
3877 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNewNavigationEntry) {
3878 PrerenderTestURL("files/prerender/prerender_new_entry.html",
3879 FINAL_STATUS_NEW_NAVIGATION_ENTRY,
3883 // Attempt a swap-in in a new tab. The session storage doesn't match, so it
3884 // should not swap.
3885 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageNewTab) {
3886 PrerenderTestURL("files/prerender/prerender_page.html",
3887 FINAL_STATUS_APP_TERMINATING, 1);
3889 // Open a new tab to navigate in.
3890 ui_test_utils::NavigateToURLWithDisposition(
3891 current_browser(), GURL(url::kAboutBlankURL), NEW_FOREGROUND_TAB,
3892 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
3894 // Now navigate in the new tab.
3895 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3898 // Checks that prerenders honor |should_replace_current_entry|.
3899 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReplaceCurrentEntry) {
3900 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3902 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3903 ui::PAGE_TRANSITION_TYPED, false);
3904 params.should_replace_current_entry = true;
3905 NavigateToURLWithParams(params, false);
3907 const NavigationController& controller =
3908 GetActiveWebContents()->GetController();
3909 // First entry is about:blank, second is prerender_page.html.
3910 EXPECT_TRUE(controller.GetPendingEntry() == NULL);
3911 EXPECT_EQ(2, controller.GetEntryCount());
3912 EXPECT_EQ(GURL(url::kAboutBlankURL), controller.GetEntryAtIndex(0)->GetURL());
3913 EXPECT_EQ(dest_url(), controller.GetEntryAtIndex(1)->GetURL());
3916 // Checks that <a ping> requests are not dropped in prerender.
3917 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPing) {
3918 // Count hits to a certain URL.
3919 const GURL kPingURL("http://prerender.test/ping");
3920 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3921 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3922 RequestCounter ping_counter;
3923 BrowserThread::PostTask(
3924 BrowserThread::IO, FROM_HERE,
3925 base::Bind(&CreateCountingInterceptorOnIO,
3926 kPingURL, empty_file, ping_counter.AsWeakPtr()));
3928 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3929 OpenDestURLViaClickPing(kPingURL);
3931 ping_counter.WaitForCount(1);
3934 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPPLTNormalNavigation) {
3935 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3936 ui_test_utils::NavigateToURL(current_browser(), url);
3937 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3938 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3939 histogram_tester().ExpectTotalCount(
3940 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3943 // Checks that a prerender which calls window.close() on itself is aborted.
3944 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWindowClose) {
3945 DisableLoadEventCheck();
3946 PrerenderTestURL("files/prerender/prerender_window_close.html",
3947 FINAL_STATUS_CLOSED, 0);
3950 class PrerenderIncognitoBrowserTest : public PrerenderBrowserTest {
3951 public:
3952 void SetUpOnMainThread() override {
3953 Profile* normal_profile = current_browser()->profile();
3954 set_browser(OpenURLOffTheRecord(normal_profile, GURL("about:blank")));
3955 PrerenderBrowserTest::SetUpOnMainThread();
3959 // Checks that prerendering works in incognito mode.
3960 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest, PrerenderIncognito) {
3961 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3962 NavigateToDestURL();
3965 // Checks that prerenders are aborted when an incognito profile is closed.
3966 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest,
3967 PrerenderIncognitoClosed) {
3968 scoped_ptr<TestPrerender> prerender =
3969 PrerenderTestURL("files/prerender/prerender_page.html",
3970 FINAL_STATUS_PROFILE_DESTROYED, 1);
3971 current_browser()->window()->Close();
3972 prerender->WaitForStop();
3975 class PrerenderOmniboxBrowserTest : public PrerenderBrowserTest {
3976 public:
3977 LocationBar* GetLocationBar() {
3978 return current_browser()->window()->GetLocationBar();
3981 OmniboxView* GetOmniboxView() {
3982 return GetLocationBar()->GetOmniboxView();
3985 void WaitForAutocompleteDone(OmniboxView* omnibox_view) {
3986 AutocompleteController* controller =
3987 omnibox_view->model()->popup_model()->autocomplete_controller();
3988 while (!controller->done()) {
3989 content::WindowedNotificationObserver ready_observer(
3990 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
3991 content::Source<AutocompleteController>(controller));
3992 ready_observer.Wait();
3996 predictors::AutocompleteActionPredictor* GetAutocompleteActionPredictor() {
3997 Profile* profile = current_browser()->profile();
3998 return predictors::AutocompleteActionPredictorFactory::GetForProfile(
3999 profile);
4002 scoped_ptr<TestPrerender> StartOmniboxPrerender(
4003 const GURL& url,
4004 FinalStatus expected_final_status) {
4005 scoped_ptr<TestPrerender> prerender =
4006 ExpectPrerender(expected_final_status);
4007 WebContents* web_contents = GetActiveWebContents();
4008 GetAutocompleteActionPredictor()->StartPrerendering(
4009 url,
4010 web_contents->GetController().GetDefaultSessionStorageNamespace(),
4011 gfx::Size(50, 50));
4012 prerender->WaitForStart();
4013 return prerender.Pass();
4017 // Checks that closing the omnibox popup cancels an omnibox prerender.
4018 // http://crbug.com/395152
4019 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest,
4020 DISABLED_PrerenderOmniboxCancel) {
4021 // Fake an omnibox prerender.
4022 scoped_ptr<TestPrerender> prerender = StartOmniboxPrerender(
4023 test_server()->GetURL("files/empty.html"),
4024 FINAL_STATUS_CANCELLED);
4026 // Revert the location bar. This should cancel the prerender.
4027 GetLocationBar()->Revert();
4028 prerender->WaitForStop();
4031 // Checks that accepting omnibox input abandons an omnibox prerender.
4032 // http://crbug.com/394592
4033 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest,
4034 DISABLED_PrerenderOmniboxAbandon) {
4035 // Set the abandon timeout to something high so it does not introduce
4036 // flakiness if the prerender times out before the test completes.
4037 GetPrerenderManager()->mutable_config().abandon_time_to_live =
4038 base::TimeDelta::FromDays(999);
4040 // Enter a URL into the Omnibox.
4041 OmniboxView* omnibox_view = GetOmniboxView();
4042 omnibox_view->OnBeforePossibleChange();
4043 omnibox_view->SetUserText(
4044 base::UTF8ToUTF16(test_server()->GetURL("files/empty.html?1").spec()));
4045 omnibox_view->OnAfterPossibleChange();
4046 WaitForAutocompleteDone(omnibox_view);
4048 // Fake an omnibox prerender for a different URL.
4049 scoped_ptr<TestPrerender> prerender = StartOmniboxPrerender(
4050 test_server()->GetURL("files/empty.html?2"),
4051 FINAL_STATUS_APP_TERMINATING);
4053 // The final status may be either FINAL_STATUS_APP_TERMINATING or
4054 // FINAL_STATUS_CANCELLED. Although closing the omnibox will not cancel an
4055 // abandoned prerender, the AutocompleteActionPredictor will cancel the
4056 // predictor on destruction.
4057 prerender->contents()->set_skip_final_checks(true);
4059 // Navigate to the URL entered.
4060 omnibox_view->model()->AcceptInput(CURRENT_TAB, false);
4062 // Prerender should be running, but abandoned.
4063 EXPECT_TRUE(
4064 GetAutocompleteActionPredictor()->IsPrerenderAbandonedForTesting());
4067 // Can't run tests with NaCl plugins if built with DISABLE_NACL.
4068 #if !defined(DISABLE_NACL) && !defined(DISABLE_NACL_BROWSERTESTS)
4069 class PrerenderBrowserTestWithNaCl : public PrerenderBrowserTest {
4070 public:
4071 PrerenderBrowserTestWithNaCl() {}
4072 ~PrerenderBrowserTestWithNaCl() override {}
4074 void SetUpCommandLine(base::CommandLine* command_line) override {
4075 PrerenderBrowserTest::SetUpCommandLine(command_line);
4076 command_line->AppendSwitch(switches::kEnableNaCl);
4080 // Check that NaCl plugins work when enabled, with prerendering.
4081 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithNaCl,
4082 PrerenderNaClPluginEnabled) {
4083 #if defined(OS_WIN) && defined(USE_ASH)
4084 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
4085 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
4086 switches::kAshBrowserTests))
4087 return;
4088 #endif
4090 PrerenderTestURL("files/prerender/prerender_plugin_nacl_enabled.html",
4091 FINAL_STATUS_USED,
4093 NavigateToDestURL();
4095 // To avoid any chance of a race, we have to let the script send its response
4096 // asynchronously.
4097 WebContents* web_contents =
4098 browser()->tab_strip_model()->GetActiveWebContents();
4099 bool display_test_result = false;
4100 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents,
4101 "DidDisplayReallyPass()",
4102 &display_test_result));
4103 ASSERT_TRUE(display_test_result);
4105 #endif // !defined(DISABLE_NACL)
4107 #if defined(ENABLE_TASK_MANAGER)
4109 namespace {
4111 base::string16 GetPrerenderTitlePrefix() {
4112 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
4113 base::string16());
4116 const std::vector<task_management::WebContentsTag*>& GetTrackedTags() {
4117 return task_management::WebContentsTagsManager::GetInstance()->
4118 tracked_tags();
4121 // Tests the correct recording of tags for the prerender WebContents.
4122 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, TaskManagementTagsBasic) {
4123 // Browser tests start with a single tab.
4124 EXPECT_EQ(1U, GetTrackedTags().size());
4126 // Start prerendering a page and make sure it's correctly tagged.
4127 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
4128 EXPECT_EQ(2U, GetTrackedTags().size());
4130 // Swap in the prerendered content and make sure its tag is removed.
4131 NavigateToDestURL();
4132 EXPECT_EQ(1U, GetTrackedTags().size());
4135 // Tests that the task manager will be provided by tasks that correspond to
4136 // prerendered WebContents.
4137 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, TaskManagementTasksProvided) {
4138 task_management::MockWebContentsTaskManager task_manager;
4139 // Browser tests start with a single tab.
4140 EXPECT_EQ(1U, GetTrackedTags().size());
4142 task_manager.StartObserving();
4144 // The pre-existing tab is provided.
4145 EXPECT_EQ(1U, task_manager.tasks().size());
4147 // Start prerendering a page.
4148 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
4150 EXPECT_EQ(2U, GetTrackedTags().size());
4151 ASSERT_EQ(2U, task_manager.tasks().size());
4153 const task_management::Task* task = task_manager.tasks().back();
4154 EXPECT_EQ(task_management::Task::RENDERER, task->GetType());
4155 const base::string16 title = task->title();
4156 const base::string16 expected_prefix = GetPrerenderTitlePrefix();
4157 EXPECT_TRUE(base::StartsWith(title,
4158 expected_prefix,
4159 base::CompareCase::INSENSITIVE_ASCII));
4161 NavigateToDestURL();
4162 EXPECT_EQ(1U, task_manager.tasks().size());
4165 } // namespace
4167 #endif // defined(ENABLE_TASK_MANAGER)
4169 } // namespace prerender