Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_browsertest.cc
blob12f1b7da0589c7345479796a2eb7da0c01519a3f
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/extensions/api/web_navigation/web_navigation_api.h"
32 #include "chrome/browser/extensions/extension_apitest.h"
33 #include "chrome/browser/external_protocol/external_protocol_handler.h"
34 #include "chrome/browser/net/prediction_options.h"
35 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
36 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
37 #include "chrome/browser/prerender/prerender_contents.h"
38 #include "chrome/browser/prerender/prerender_field_trial.h"
39 #include "chrome/browser/prerender/prerender_handle.h"
40 #include "chrome/browser/prerender/prerender_link_manager.h"
41 #include "chrome/browser/prerender/prerender_link_manager_factory.h"
42 #include "chrome/browser/prerender/prerender_manager.h"
43 #include "chrome/browser/prerender/prerender_manager_factory.h"
44 #include "chrome/browser/profiles/profile.h"
45 #include "chrome/browser/profiles/profile_io_data.h"
46 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
47 #include "chrome/browser/safe_browsing/database_manager.h"
48 #include "chrome/browser/safe_browsing/local_database_manager.h"
49 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
50 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
51 #include "chrome/browser/safe_browsing/test_database_manager.h"
52 #include "chrome/browser/task_management/providers/web_contents/web_contents_tags_manager.h"
53 #include "chrome/browser/task_management/task_management_browsertest_util.h"
54 #include "chrome/browser/task_manager/task_manager.h"
55 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
56 #include "chrome/browser/ui/browser.h"
57 #include "chrome/browser/ui/browser_commands.h"
58 #include "chrome/browser/ui/browser_finder.h"
59 #include "chrome/browser/ui/browser_navigator.h"
60 #include "chrome/browser/ui/browser_window.h"
61 #include "chrome/browser/ui/location_bar/location_bar.h"
62 #include "chrome/browser/ui/tabs/tab_strip_model.h"
63 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
64 #include "chrome/common/chrome_paths.h"
65 #include "chrome/common/chrome_switches.h"
66 #include "chrome/common/pref_names.h"
67 #include "chrome/grit/generated_resources.h"
68 #include "chrome/test/base/in_process_browser_test.h"
69 #include "chrome/test/base/test_switches.h"
70 #include "chrome/test/base/ui_test_utils.h"
71 #include "components/content_settings/core/browser/host_content_settings_map.h"
72 #include "components/favicon/content/content_favicon_driver.h"
73 #include "components/favicon/core/favicon_driver_observer.h"
74 #include "components/omnibox/browser/omnibox_edit_model.h"
75 #include "components/omnibox/browser/omnibox_popup_model.h"
76 #include "components/omnibox/browser/omnibox_view.h"
77 #include "components/variations/entropy_provider.h"
78 #include "components/variations/variations_associated_data.h"
79 #include "content/public/browser/browser_message_filter.h"
80 #include "content/public/browser/devtools_agent_host.h"
81 #include "content/public/browser/navigation_controller.h"
82 #include "content/public/browser/navigation_entry.h"
83 #include "content/public/browser/notification_service.h"
84 #include "content/public/browser/render_frame_host.h"
85 #include "content/public/browser/render_process_host.h"
86 #include "content/public/browser/render_view_host.h"
87 #include "content/public/browser/site_instance.h"
88 #include "content/public/browser/web_contents.h"
89 #include "content/public/browser/web_contents_observer.h"
90 #include "content/public/common/url_constants.h"
91 #include "content/public/test/browser_test_utils.h"
92 #include "content/public/test/ppapi_test_utils.h"
93 #include "content/public/test/test_navigation_observer.h"
94 #include "content/public/test/test_utils.h"
95 #include "extensions/common/constants.h"
96 #include "extensions/common/extension_urls.h"
97 #include "extensions/common/manifest_handlers/mime_types_handler.h"
98 #include "extensions/common/switches.h"
99 #include "extensions/test/result_catcher.h"
100 #include "net/base/escape.h"
101 #include "net/cert/x509_certificate.h"
102 #include "net/dns/mock_host_resolver.h"
103 #include "net/ssl/client_cert_store.h"
104 #include "net/ssl/ssl_cert_request_info.h"
105 #include "net/test/url_request/url_request_mock_http_job.h"
106 #include "net/url_request/url_request_context.h"
107 #include "net/url_request/url_request_context_getter.h"
108 #include "net/url_request/url_request_filter.h"
109 #include "net/url_request/url_request_interceptor.h"
110 #include "net/url_request/url_request_job.h"
111 #include "ppapi/shared_impl/ppapi_switches.h"
112 #include "ui/base/l10n/l10n_util.h"
113 #include "url/gurl.h"
115 using chrome_browser_net::NetworkPredictionOptions;
116 using content::BrowserThread;
117 using content::DevToolsAgentHost;
118 using content::NavigationController;
119 using content::OpenURLParams;
120 using content::Referrer;
121 using content::RenderFrameHost;
122 using content::RenderViewHost;
123 using content::RenderWidgetHost;
124 using content::TestNavigationObserver;
125 using content::WebContents;
126 using content::WebContentsObserver;
127 using net::NetworkChangeNotifier;
128 using task_manager::browsertest_util::WaitForTaskManagerRows;
130 // Prerender tests work as follows:
132 // A page with a prefetch link to the test page is loaded. Once prerendered,
133 // its Javascript function DidPrerenderPass() is called, which returns true if
134 // the page behaves as expected when prerendered.
136 // The prerendered page is then displayed on a tab. The Javascript function
137 // DidDisplayPass() is called, and returns true if the page behaved as it
138 // should while being displayed.
140 namespace prerender {
142 namespace {
144 class FaviconUpdateWatcher : public favicon::FaviconDriverObserver {
145 public:
146 explicit FaviconUpdateWatcher(content::WebContents* web_contents)
147 : seen_(false), running_(false), scoped_observer_(this) {
148 scoped_observer_.Add(
149 favicon::ContentFaviconDriver::FromWebContents(web_contents));
152 void Wait() {
153 if (seen_)
154 return;
156 running_ = true;
157 message_loop_runner_ = new content::MessageLoopRunner;
158 message_loop_runner_->Run();
161 private:
162 void OnFaviconAvailable(const gfx::Image& image) override {}
163 void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
164 bool icon_url_changed) override {
165 seen_ = true;
166 if (!running_)
167 return;
169 message_loop_runner_->Quit();
170 running_ = false;
173 bool seen_;
174 bool running_;
175 ScopedObserver<favicon::FaviconDriver, FaviconUpdateWatcher> scoped_observer_;
176 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
178 DISALLOW_COPY_AND_ASSIGN(FaviconUpdateWatcher);
181 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
182 public:
183 ConnectionType GetCurrentConnectionType() const override {
184 return NetworkChangeNotifier::CONNECTION_WIFI;
188 class MockNetworkChangeNotifier4G : public NetworkChangeNotifier {
189 public:
190 ConnectionType GetCurrentConnectionType() const override {
191 return NetworkChangeNotifier::CONNECTION_4G;
195 // Constants used in the test HTML files.
196 const char* kReadyTitle = "READY";
197 const char* kPassTitle = "PASS";
199 std::string CreateClientRedirect(const std::string& dest_url) {
200 const char* const kClientRedirectBase = "client-redirect?";
201 return kClientRedirectBase + net::EscapeQueryParamValue(dest_url, false);
204 std::string CreateServerRedirect(const std::string& dest_url) {
205 const char* const kServerRedirectBase = "server-redirect?";
206 return kServerRedirectBase + net::EscapeQueryParamValue(dest_url, false);
209 // Clears the specified data using BrowsingDataRemover.
210 void ClearBrowsingData(Browser* browser, int remove_mask) {
211 BrowsingDataRemover* remover =
212 BrowsingDataRemover::CreateForUnboundedRange(browser->profile());
213 BrowsingDataRemoverCompletionObserver observer(remover);
214 remover->Remove(remove_mask, BrowsingDataHelper::UNPROTECTED_WEB);
215 observer.BlockUntilCompletion();
216 // BrowsingDataRemover deletes itself.
219 // Returns true if the prerender is expected to abort on its own, before
220 // attempting to swap it.
221 bool ShouldAbortPrerenderBeforeSwap(FinalStatus status) {
222 switch (status) {
223 case FINAL_STATUS_USED:
224 case FINAL_STATUS_WINDOW_OPENER:
225 case FINAL_STATUS_APP_TERMINATING:
226 case FINAL_STATUS_PROFILE_DESTROYED:
227 case FINAL_STATUS_CACHE_OR_HISTORY_CLEARED:
228 // We'll crash the renderer after it's loaded.
229 case FINAL_STATUS_RENDERER_CRASHED:
230 case FINAL_STATUS_CANCELLED:
231 case FINAL_STATUS_DEVTOOLS_ATTACHED:
232 case FINAL_STATUS_PAGE_BEING_CAPTURED:
233 case FINAL_STATUS_NAVIGATION_UNCOMMITTED:
234 case FINAL_STATUS_WOULD_HAVE_BEEN_USED:
235 case FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE:
236 return false;
237 default:
238 return true;
242 // Convenience function to wait for a title. Handles the case when the
243 // WebContents already has the expected title.
244 void WaitForASCIITitle(WebContents* web_contents,
245 const char* expected_title_ascii) {
246 base::string16 expected_title = base::ASCIIToUTF16(expected_title_ascii);
247 if (web_contents->GetTitle() == expected_title)
248 return;
249 content::TitleWatcher title_watcher(web_contents, expected_title);
250 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
253 // Waits for the destruction of a RenderProcessHost's IPC channel.
254 // Used to make sure the PrerenderLinkManager's OnChannelClosed function has
255 // been called, before checking its state.
256 class ChannelDestructionWatcher {
257 public:
258 ChannelDestructionWatcher() : channel_destroyed_(false) {
261 ~ChannelDestructionWatcher() {
264 void WatchChannel(content::RenderProcessHost* host) {
265 host->AddFilter(new DestructionMessageFilter(this));
268 void WaitForChannelClose() {
269 run_loop_.Run();
270 EXPECT_TRUE(channel_destroyed_);
273 private:
274 // When destroyed, calls ChannelDestructionWatcher::OnChannelDestroyed.
275 // Ignores all messages.
276 class DestructionMessageFilter : public content::BrowserMessageFilter {
277 public:
278 explicit DestructionMessageFilter(ChannelDestructionWatcher* watcher)
279 : BrowserMessageFilter(0),
280 watcher_(watcher) {
283 private:
284 ~DestructionMessageFilter() override {
285 content::BrowserThread::PostTask(
286 content::BrowserThread::UI, FROM_HERE,
287 base::Bind(&ChannelDestructionWatcher::OnChannelDestroyed,
288 base::Unretained(watcher_)));
291 bool OnMessageReceived(const IPC::Message& message) override {
292 return false;
295 ChannelDestructionWatcher* watcher_;
297 DISALLOW_COPY_AND_ASSIGN(DestructionMessageFilter);
300 void OnChannelDestroyed() {
301 DCHECK_CURRENTLY_ON(BrowserThread::UI);
303 EXPECT_FALSE(channel_destroyed_);
304 channel_destroyed_ = true;
305 run_loop_.Quit();
308 bool channel_destroyed_;
309 base::RunLoop run_loop_;
311 DISALLOW_COPY_AND_ASSIGN(ChannelDestructionWatcher);
314 // A navigation observer to wait on either a new load or a swap of a
315 // WebContents. On swap, if the new WebContents is still loading, wait for that
316 // load to complete as well. Note that the load must begin after the observer is
317 // attached.
318 class NavigationOrSwapObserver : public WebContentsObserver,
319 public TabStripModelObserver {
320 public:
321 // Waits for either a new load or a swap of |tab_strip_model|'s active
322 // WebContents.
323 NavigationOrSwapObserver(TabStripModel* tab_strip_model,
324 WebContents* web_contents)
325 : WebContentsObserver(web_contents),
326 tab_strip_model_(tab_strip_model),
327 did_start_loading_(false),
328 number_of_loads_(1) {
329 CHECK_NE(TabStripModel::kNoTab,
330 tab_strip_model->GetIndexOfWebContents(web_contents));
331 tab_strip_model_->AddObserver(this);
334 // Waits for either |number_of_loads| loads or a swap of |tab_strip_model|'s
335 // active WebContents.
336 NavigationOrSwapObserver(TabStripModel* tab_strip_model,
337 WebContents* web_contents,
338 int number_of_loads)
339 : WebContentsObserver(web_contents),
340 tab_strip_model_(tab_strip_model),
341 did_start_loading_(false),
342 number_of_loads_(number_of_loads) {
343 CHECK_NE(TabStripModel::kNoTab,
344 tab_strip_model->GetIndexOfWebContents(web_contents));
345 tab_strip_model_->AddObserver(this);
348 ~NavigationOrSwapObserver() override {
349 tab_strip_model_->RemoveObserver(this);
352 void set_did_start_loading() {
353 did_start_loading_ = true;
356 void Wait() {
357 loop_.Run();
360 // WebContentsObserver implementation:
361 void DidStartLoading() override { did_start_loading_ = true; }
362 void DidStopLoading() override {
363 if (!did_start_loading_)
364 return;
365 number_of_loads_--;
366 if (number_of_loads_ == 0)
367 loop_.Quit();
370 // TabStripModelObserver implementation:
371 void TabReplacedAt(TabStripModel* tab_strip_model,
372 WebContents* old_contents,
373 WebContents* new_contents,
374 int index) override {
375 if (old_contents != web_contents())
376 return;
377 // Switch to observing the new WebContents.
378 Observe(new_contents);
379 if (new_contents->IsLoading()) {
380 // If the new WebContents is still loading, wait for it to complete. Only
381 // one load post-swap is supported.
382 did_start_loading_ = true;
383 number_of_loads_ = 1;
384 } else {
385 loop_.Quit();
389 private:
390 TabStripModel* tab_strip_model_;
391 bool did_start_loading_;
392 int number_of_loads_;
393 base::RunLoop loop_;
396 // Waits for a new tab to open and a navigation or swap in it.
397 class NewTabNavigationOrSwapObserver {
398 public:
399 NewTabNavigationOrSwapObserver()
400 : new_tab_observer_(
401 chrome::NOTIFICATION_TAB_ADDED,
402 base::Bind(&NewTabNavigationOrSwapObserver::OnTabAdded,
403 base::Unretained(this))) {
404 // Watch for NOTIFICATION_TAB_ADDED. Add a callback so that the
405 // NavigationOrSwapObserver can be attached synchronously and no events are
406 // missed.
409 void Wait() {
410 new_tab_observer_.Wait();
411 swap_observer_->Wait();
414 bool OnTabAdded(const content::NotificationSource& source,
415 const content::NotificationDetails& details) {
416 if (swap_observer_)
417 return true;
418 WebContents* new_tab = content::Details<WebContents>(details).ptr();
419 // Get the TabStripModel. Assume this is attached to a Browser.
420 TabStripModel* tab_strip_model =
421 static_cast<Browser*>(new_tab->GetDelegate())->tab_strip_model();
422 swap_observer_.reset(new NavigationOrSwapObserver(tab_strip_model,
423 new_tab));
424 swap_observer_->set_did_start_loading();
425 return true;
428 private:
429 content::WindowedNotificationObserver new_tab_observer_;
430 scoped_ptr<NavigationOrSwapObserver> swap_observer_;
433 // PrerenderContents that stops the UI message loop on DidStopLoading().
434 class TestPrerenderContents : public PrerenderContents {
435 public:
436 TestPrerenderContents(
437 PrerenderManager* prerender_manager,
438 Profile* profile,
439 const GURL& url,
440 const content::Referrer& referrer,
441 Origin origin,
442 FinalStatus expected_final_status)
443 : PrerenderContents(prerender_manager, profile, url, referrer, origin),
444 expected_final_status_(expected_final_status),
445 new_render_view_host_(NULL),
446 was_hidden_(false),
447 was_shown_(false),
448 should_be_shown_(expected_final_status == FINAL_STATUS_USED),
449 skip_final_checks_(false) {
452 ~TestPrerenderContents() override {
453 if (skip_final_checks_)
454 return;
456 if (expected_final_status_ == FINAL_STATUS_MAX) {
457 EXPECT_EQ(MATCH_COMPLETE_REPLACEMENT, match_complete_status());
458 } else {
459 EXPECT_EQ(expected_final_status_, final_status()) <<
460 " when testing URL " << prerender_url().path() <<
461 " (Expected: " << NameFromFinalStatus(expected_final_status_) <<
462 ", Actual: " << NameFromFinalStatus(final_status()) << ")";
464 // Prerendering RenderViewHosts should be hidden before the first
465 // navigation, so this should be happen for every PrerenderContents for
466 // which a RenderViewHost is created, regardless of whether or not it's
467 // used.
468 if (new_render_view_host_)
469 EXPECT_TRUE(was_hidden_);
471 // A used PrerenderContents will only be destroyed when we swap out
472 // WebContents, at the end of a navigation caused by a call to
473 // NavigateToURLImpl().
474 if (final_status() == FINAL_STATUS_USED)
475 EXPECT_TRUE(new_render_view_host_);
477 EXPECT_EQ(should_be_shown_, was_shown_);
480 void RenderProcessGone(base::TerminationStatus status) override {
481 // On quit, it's possible to end up here when render processes are closed
482 // before the PrerenderManager is destroyed. As a result, it's possible to
483 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
484 // on quit.
486 // It's also possible for this to be called after we've been notified of
487 // app termination, but before we've been deleted, which is why the second
488 // check is needed.
489 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING &&
490 final_status() != expected_final_status_) {
491 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED;
494 PrerenderContents::RenderProcessGone(status);
497 bool CheckURL(const GURL& url) override {
498 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
499 // the PrerenderRendererCrash test.
500 if (url.spec() != content::kChromeUICrashURL)
501 return PrerenderContents::CheckURL(url);
502 return true;
505 // For tests that open the prerender in a new background tab, the RenderView
506 // will not have been made visible when the PrerenderContents is destroyed
507 // even though it is used.
508 void set_should_be_shown(bool value) { should_be_shown_ = value; }
510 // For tests which do not know whether the prerender will be used.
511 void set_skip_final_checks(bool value) { skip_final_checks_ = value; }
513 FinalStatus expected_final_status() const { return expected_final_status_; }
515 private:
516 void OnRenderViewHostCreated(RenderViewHost* new_render_view_host) override {
517 // Used to make sure the RenderViewHost is hidden and, if used,
518 // subsequently shown.
519 notification_registrar().Add(
520 this,
521 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
522 content::Source<RenderWidgetHost>(new_render_view_host));
524 new_render_view_host_ = new_render_view_host;
526 PrerenderContents::OnRenderViewHostCreated(new_render_view_host);
529 void Observe(int type,
530 const content::NotificationSource& source,
531 const content::NotificationDetails& details) override {
532 if (type ==
533 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED) {
534 EXPECT_EQ(new_render_view_host_,
535 content::Source<RenderWidgetHost>(source).ptr());
536 bool is_visible = *content::Details<bool>(details).ptr();
538 if (!is_visible) {
539 was_hidden_ = true;
540 } else if (is_visible && was_hidden_) {
541 // Once hidden, a prerendered RenderViewHost should only be shown after
542 // being removed from the PrerenderContents for display.
543 EXPECT_FALSE(GetRenderViewHost());
544 was_shown_ = true;
546 return;
548 PrerenderContents::Observe(type, source, details);
551 FinalStatus expected_final_status_;
553 // The RenderViewHost created for the prerender, if any.
554 RenderViewHost* new_render_view_host_;
555 // Set to true when the prerendering RenderWidget is hidden.
556 bool was_hidden_;
557 // Set to true when the prerendering RenderWidget is shown, after having been
558 // hidden.
559 bool was_shown_;
560 // Expected final value of was_shown_. Defaults to true for
561 // FINAL_STATUS_USED, and false otherwise.
562 bool should_be_shown_;
563 // If true, |expected_final_status_| and other shutdown checks are skipped.
564 bool skip_final_checks_;
567 // A handle to a TestPrerenderContents whose lifetime is under the caller's
568 // control. A PrerenderContents may be destroyed at any point. This allows
569 // tracking the final status, etc.
570 class TestPrerender : public PrerenderContents::Observer,
571 public base::SupportsWeakPtr<TestPrerender> {
572 public:
573 TestPrerender()
574 : contents_(NULL),
575 number_of_loads_(0),
576 expected_number_of_loads_(0) {
578 ~TestPrerender() override {
579 if (contents_)
580 contents_->RemoveObserver(this);
583 TestPrerenderContents* contents() const { return contents_; }
584 int number_of_loads() const { return number_of_loads_; }
586 void WaitForCreate() { create_loop_.Run(); }
587 void WaitForStart() { start_loop_.Run(); }
588 void WaitForStop() { stop_loop_.Run(); }
590 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR
591 // for the prerender to stop running (just to avoid a timeout if the prerender
592 // dies). Note: this does not assert equality on the number of loads; the
593 // caller must do it instead.
594 void WaitForLoads(int expected_number_of_loads) {
595 DCHECK(!load_waiter_);
596 DCHECK(!expected_number_of_loads_);
597 if (number_of_loads_ < expected_number_of_loads) {
598 load_waiter_.reset(new base::RunLoop);
599 expected_number_of_loads_ = expected_number_of_loads;
600 load_waiter_->Run();
601 load_waiter_.reset();
602 expected_number_of_loads_ = 0;
604 EXPECT_LE(expected_number_of_loads, number_of_loads_);
607 void OnPrerenderCreated(TestPrerenderContents* contents) {
608 DCHECK(!contents_);
609 contents_ = contents;
610 contents_->AddObserver(this);
611 create_loop_.Quit();
614 // PrerenderContents::Observer implementation:
615 void OnPrerenderStart(PrerenderContents* contents) override {
616 start_loop_.Quit();
619 void OnPrerenderStopLoading(PrerenderContents* contents) override {
620 number_of_loads_++;
621 if (load_waiter_ && number_of_loads_ >= expected_number_of_loads_)
622 load_waiter_->Quit();
625 void OnPrerenderStop(PrerenderContents* contents) override {
626 DCHECK(contents_);
627 contents_ = NULL;
628 stop_loop_.Quit();
629 // If there is a WaitForLoads call and it has yet to see the expected number
630 // of loads, stop the loop so the test fails instead of timing out.
631 if (load_waiter_)
632 load_waiter_->Quit();
635 void OnPrerenderCreatedMatchCompleteReplacement(
636 PrerenderContents* contents,
637 PrerenderContents* replacement) override {}
639 private:
640 TestPrerenderContents* contents_;
641 int number_of_loads_;
643 int expected_number_of_loads_;
644 scoped_ptr<base::RunLoop> load_waiter_;
646 base::RunLoop create_loop_;
647 base::RunLoop start_loop_;
648 base::RunLoop stop_loop_;
650 DISALLOW_COPY_AND_ASSIGN(TestPrerender);
653 // PrerenderManager that uses TestPrerenderContents.
654 class TestPrerenderContentsFactory : public PrerenderContents::Factory {
655 public:
656 TestPrerenderContentsFactory() {}
658 ~TestPrerenderContentsFactory() override {
659 EXPECT_TRUE(expected_contents_queue_.empty());
662 scoped_ptr<TestPrerender> ExpectPrerenderContents(FinalStatus final_status) {
663 scoped_ptr<TestPrerender> handle(new TestPrerender());
664 expected_contents_queue_.push_back(
665 ExpectedContents(final_status, handle->AsWeakPtr()));
666 return handle.Pass();
669 PrerenderContents* CreatePrerenderContents(
670 PrerenderManager* prerender_manager,
671 Profile* profile,
672 const GURL& url,
673 const content::Referrer& referrer,
674 Origin origin) override {
675 ExpectedContents expected;
676 if (!expected_contents_queue_.empty()) {
677 expected = expected_contents_queue_.front();
678 expected_contents_queue_.pop_front();
680 VLOG(1) << "Creating prerender contents for " << url.path() <<
681 " with expected final status " << expected.final_status;
682 VLOG(1) << expected_contents_queue_.size() << " left in the queue.";
683 TestPrerenderContents* contents =
684 new TestPrerenderContents(prerender_manager,
685 profile, url, referrer, origin,
686 expected.final_status);
687 if (expected.handle)
688 expected.handle->OnPrerenderCreated(contents);
689 return contents;
692 private:
693 struct ExpectedContents {
694 ExpectedContents() : final_status(FINAL_STATUS_MAX) { }
695 ExpectedContents(FinalStatus final_status,
696 const base::WeakPtr<TestPrerender>& handle)
697 : final_status(final_status),
698 handle(handle) {
701 FinalStatus final_status;
702 base::WeakPtr<TestPrerender> handle;
705 std::deque<ExpectedContents> expected_contents_queue_;
708 // TODO(nparker): Switch this to use TestSafeBrowsingDatabaseManager and run
709 // with SAFE_BROWSING_DB_LOCAL || SAFE_BROWSING_DB_REMOTE.
710 #if defined(FULL_SAFE_BROWSING)
711 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
712 // a given URL.
713 class FakeSafeBrowsingDatabaseManager
714 : public LocalSafeBrowsingDatabaseManager {
715 public:
716 explicit FakeSafeBrowsingDatabaseManager(SafeBrowsingService* service)
717 : LocalSafeBrowsingDatabaseManager(service),
718 threat_type_(SB_THREAT_TYPE_SAFE) {}
720 // Called on the IO thread to check if the given url is safe or not. If we
721 // can synchronously determine that the url is safe, CheckUrl returns true.
722 // Otherwise it returns false, and "client" is called asynchronously with the
723 // result when it is ready.
724 // Returns true, indicating a SAFE result, unless the URL is the fixed URL
725 // specified by the user, and the user-specified result is not SAFE
726 // (in which that result will be communicated back via a call into the
727 // client, and false will be returned).
728 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl.
729 bool CheckBrowseUrl(const GURL& gurl, Client* client) override {
730 if (gurl != url_ || threat_type_ == SB_THREAT_TYPE_SAFE)
731 return true;
733 BrowserThread::PostTask(
734 BrowserThread::IO, FROM_HERE,
735 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone,
736 this, gurl, client));
737 return false;
740 void SetThreatTypeForUrl(const GURL& url, SBThreatType threat_type) {
741 url_ = url;
742 threat_type_ = threat_type;
745 private:
746 ~FakeSafeBrowsingDatabaseManager() override {}
748 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) {
749 std::vector<SBThreatType> expected_threats;
750 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
751 expected_threats.push_back(SB_THREAT_TYPE_URL_PHISHING);
752 // TODO(nparker): Replace SafeBrowsingCheck w/ a call to
753 // client->OnCheckBrowseUrlResult()
754 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check(
755 std::vector<GURL>(1, gurl),
756 std::vector<SBFullHash>(),
757 client,
758 safe_browsing_util::MALWARE,
759 expected_threats);
760 sb_check.url_results[0] = threat_type_;
761 sb_check.OnSafeBrowsingResult();
764 GURL url_;
765 SBThreatType threat_type_;
766 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
769 class FakeSafeBrowsingService : public SafeBrowsingService {
770 public:
771 FakeSafeBrowsingService() { }
773 // Returned pointer has the same lifespan as the database_manager_ refcounted
774 // object.
775 FakeSafeBrowsingDatabaseManager* fake_database_manager() {
776 return fake_database_manager_;
779 protected:
780 ~FakeSafeBrowsingService() override {}
782 SafeBrowsingDatabaseManager* CreateDatabaseManager() override {
783 fake_database_manager_ = new FakeSafeBrowsingDatabaseManager(this);
784 return fake_database_manager_;
787 private:
788 FakeSafeBrowsingDatabaseManager* fake_database_manager_;
790 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService);
793 // Factory that creates FakeSafeBrowsingService instances.
794 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
795 public:
796 TestSafeBrowsingServiceFactory() :
797 most_recent_service_(NULL) { }
798 ~TestSafeBrowsingServiceFactory() override {}
800 SafeBrowsingService* CreateSafeBrowsingService() override {
801 most_recent_service_ = new FakeSafeBrowsingService();
802 return most_recent_service_;
805 FakeSafeBrowsingService* most_recent_service() const {
806 return most_recent_service_;
809 private:
810 FakeSafeBrowsingService* most_recent_service_;
812 #endif
814 class FakeDevToolsClient : public content::DevToolsAgentHostClient {
815 public:
816 FakeDevToolsClient() {}
817 ~FakeDevToolsClient() override {}
818 void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
819 const std::string& message) override {}
820 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {}
823 class RestorePrerenderMode {
824 public:
825 RestorePrerenderMode() : prev_mode_(PrerenderManager::GetMode()) {
828 ~RestorePrerenderMode() { PrerenderManager::SetMode(prev_mode_); }
829 private:
830 PrerenderManager::PrerenderManagerMode prev_mode_;
833 // URLRequestJob (and associated handler) which hangs.
834 class HangingURLRequestJob : public net::URLRequestJob {
835 public:
836 HangingURLRequestJob(net::URLRequest* request,
837 net::NetworkDelegate* network_delegate)
838 : net::URLRequestJob(request, network_delegate) {
841 void Start() override {}
843 private:
844 ~HangingURLRequestJob() override {}
847 class HangingFirstRequestInterceptor : public net::URLRequestInterceptor {
848 public:
849 HangingFirstRequestInterceptor(const base::FilePath& file,
850 base::Closure callback)
851 : file_(file),
852 callback_(callback),
853 first_run_(true) {
855 ~HangingFirstRequestInterceptor() override {}
857 net::URLRequestJob* MaybeInterceptRequest(
858 net::URLRequest* request,
859 net::NetworkDelegate* network_delegate) const override {
860 if (first_run_) {
861 first_run_ = false;
862 if (!callback_.is_null()) {
863 BrowserThread::PostTask(
864 BrowserThread::UI, FROM_HERE, callback_);
866 return new HangingURLRequestJob(request, network_delegate);
868 return new net::URLRequestMockHTTPJob(
869 request,
870 network_delegate,
871 file_,
872 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
873 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
876 private:
877 base::FilePath file_;
878 base::Closure callback_;
879 mutable bool first_run_;
882 // Makes |url| never respond on the first load, and then with the contents of
883 // |file| afterwards. When the first load has been scheduled, runs |callback| on
884 // the UI thread.
885 void CreateHangingFirstRequestInterceptorOnIO(
886 const GURL& url, const base::FilePath& file, base::Closure callback) {
887 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
888 scoped_ptr<net::URLRequestInterceptor> never_respond_handler(
889 new HangingFirstRequestInterceptor(file, callback));
890 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
891 url, never_respond_handler.Pass());
894 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks.
895 class MockHTTPJob : public net::URLRequestMockHTTPJob {
896 public:
897 MockHTTPJob(net::URLRequest* request,
898 net::NetworkDelegate* delegate,
899 const base::FilePath& file)
900 : net::URLRequestMockHTTPJob(
901 request,
902 delegate,
903 file,
904 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
905 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
907 void set_start_callback(const base::Closure& start_callback) {
908 start_callback_ = start_callback;
911 void Start() override {
912 if (!start_callback_.is_null())
913 start_callback_.Run();
914 net::URLRequestMockHTTPJob::Start();
917 private:
918 ~MockHTTPJob() override {}
920 base::Closure start_callback_;
923 // Dummy counter class to live on the UI thread for counting requests.
924 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> {
925 public:
926 RequestCounter() : count_(0), expected_count_(-1) {}
927 int count() const { return count_; }
929 void RequestStarted() {
930 count_++;
931 if (loop_ && count_ == expected_count_)
932 loop_->Quit();
935 void WaitForCount(int expected_count) {
936 ASSERT_TRUE(!loop_);
937 ASSERT_EQ(-1, expected_count_);
938 if (count_ < expected_count) {
939 expected_count_ = expected_count;
940 loop_.reset(new base::RunLoop);
941 loop_->Run();
942 expected_count_ = -1;
943 loop_.reset();
946 EXPECT_EQ(expected_count, count_);
948 private:
949 int count_;
950 int expected_count_;
951 scoped_ptr<base::RunLoop> loop_;
954 // Protocol handler which counts the number of requests that start.
955 class CountingInterceptor : public net::URLRequestInterceptor {
956 public:
957 CountingInterceptor(const base::FilePath& file,
958 const base::WeakPtr<RequestCounter>& counter)
959 : file_(file),
960 counter_(counter),
961 weak_factory_(this) {
963 ~CountingInterceptor() override {}
965 net::URLRequestJob* MaybeInterceptRequest(
966 net::URLRequest* request,
967 net::NetworkDelegate* network_delegate) const override {
968 MockHTTPJob* job = new MockHTTPJob(request, network_delegate, file_);
969 job->set_start_callback(base::Bind(&CountingInterceptor::RequestStarted,
970 weak_factory_.GetWeakPtr()));
971 return job;
974 void RequestStarted() {
975 BrowserThread::PostTask(
976 BrowserThread::UI, FROM_HERE,
977 base::Bind(&RequestCounter::RequestStarted, counter_));
980 private:
981 base::FilePath file_;
982 base::WeakPtr<RequestCounter> counter_;
983 mutable base::WeakPtrFactory<CountingInterceptor> weak_factory_;
986 // Makes |url| respond to requests with the contents of |file|, counting the
987 // number that start in |counter|.
988 void CreateCountingInterceptorOnIO(
989 const GURL& url,
990 const base::FilePath& file,
991 const base::WeakPtr<RequestCounter>& counter) {
992 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
993 scoped_ptr<net::URLRequestInterceptor> request_interceptor(
994 new CountingInterceptor(file, counter));
995 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
996 url, request_interceptor.Pass());
999 // Makes |url| respond to requests with the contents of |file|.
1000 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
1001 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1002 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
1003 url,
1004 net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
1005 file, BrowserThread::GetBlockingPool()));
1008 // A ContentBrowserClient that cancels all prerenderers on OpenURL.
1009 class TestContentBrowserClient : public chrome::ChromeContentBrowserClient {
1010 public:
1011 TestContentBrowserClient() {}
1012 ~TestContentBrowserClient() override {}
1014 // chrome::ChromeContentBrowserClient implementation.
1015 bool ShouldAllowOpenURL(content::SiteInstance* site_instance,
1016 const GURL& url) override {
1017 PrerenderManagerFactory::GetForProfile(
1018 Profile::FromBrowserContext(site_instance->GetBrowserContext()))
1019 ->CancelAllPrerenders();
1020 return chrome::ChromeContentBrowserClient::ShouldAllowOpenURL(site_instance,
1021 url);
1024 private:
1025 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient);
1028 // A ContentBrowserClient that forces cross-process navigations.
1029 class SwapProcessesContentBrowserClient
1030 : public chrome::ChromeContentBrowserClient {
1031 public:
1032 SwapProcessesContentBrowserClient() {}
1033 ~SwapProcessesContentBrowserClient() override {}
1035 // chrome::ChromeContentBrowserClient implementation.
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 current_browser()->profile()->GetHostContentSettingsMap();
2001 content_settings_map->SetDefaultContentSetting(
2002 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_DETECT_IMPORTANT_CONTENT);
2004 PrerenderTestURL("files/prerender/prerender_plugin_power_saver.html",
2005 FINAL_STATUS_USED, 1);
2007 DisableJavascriptCalls();
2008 NavigateToDestURL();
2009 bool second_placeholder_present = false;
2010 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
2011 GetActiveWebContents(), "AwaitPluginPrerollAndPlaceholder();",
2012 &second_placeholder_present));
2013 EXPECT_TRUE(second_placeholder_present);
2016 // For Content Setting BLOCK, checks that plugins are never loaded.
2017 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderContentSettingBlock) {
2018 HostContentSettingsMap* content_settings_map =
2019 current_browser()->profile()->GetHostContentSettingsMap();
2020 content_settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS,
2021 CONTENT_SETTING_BLOCK);
2023 PrerenderTestURL("files/prerender/prerender_plugin_never_load.html",
2024 FINAL_STATUS_USED, 1);
2025 NavigateToDestURL();
2028 // Checks that we don't load a NaCl plugin when NaCl is disabled.
2029 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNaClPluginDisabled) {
2030 PrerenderTestURL("files/prerender/prerender_plugin_nacl_disabled.html",
2031 FINAL_STATUS_USED,
2033 NavigateToDestURL();
2036 // Run this check again. When we try to load aa ppapi plugin, the
2037 // "loadstart" event is asynchronously posted to a message loop.
2038 // It's possible that earlier call could have been run before the
2039 // the "loadstart" event was posted.
2040 // TODO(mmenke): While this should reliably fail on regressions, the
2041 // reliability depends on the specifics of ppapi plugin
2042 // loading. It would be great if we could avoid that.
2043 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2046 // Checks that plugins in an iframe are not loaded while a page is
2047 // being preloaded, but are loaded when the page is displayed.
2048 #if defined(USE_AURA) && !defined(OS_WIN)
2049 // http://crbug.com/103496
2050 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2051 DISABLED_PrerenderIframeDelayLoadPlugin
2052 #elif defined(OS_MACOSX)
2053 // http://crbug.com/100514
2054 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2055 DISABLED_PrerenderIframeDelayLoadPlugin
2056 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
2057 // TODO(jschuh): Failing plugin tests. crbug.com/244653
2058 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2059 DISABLED_PrerenderIframeDelayLoadPlugin
2060 #else
2061 #define MAYBE_PrerenderIframeDelayLoadPlugin PrerenderIframeDelayLoadPlugin
2062 #endif
2063 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2064 MAYBE_PrerenderIframeDelayLoadPlugin) {
2065 PrerenderTestURL("files/prerender/prerender_iframe_plugin_delay_load.html",
2066 FINAL_STATUS_USED,
2068 NavigateToDestURL();
2071 // Renders a page that contains a prerender link to a page that contains an
2072 // iframe with a source that requires http authentication. This should not
2073 // prerender successfully.
2074 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttpAuthentication) {
2075 PrerenderTestURL("files/prerender/prerender_http_auth_container.html",
2076 FINAL_STATUS_AUTH_NEEDED,
2080 // Checks that client-issued redirects work with prerendering.
2081 // This version navigates to the page which issues the redirection, rather
2082 // than the final destination page.
2083 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2084 PrerenderClientRedirectNavigateToFirst) {
2085 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2086 FINAL_STATUS_USED,
2088 NavigateToDestURL();
2091 // Checks that client-issued redirects work with prerendering.
2092 // This version navigates to the final destination page, rather than the
2093 // page which does the redirection.
2094 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2095 PrerenderClientRedirectNavigateToSecond) {
2096 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2097 FINAL_STATUS_USED,
2099 NavigateToURL("files/prerender/prerender_page.html");
2102 // Checks that redirects with location.replace do not cancel a prerender and
2103 // and swap when navigating to the first page.
2104 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2105 PrerenderLocationReplaceNavigateToFirst) {
2106 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2107 FINAL_STATUS_USED,
2109 NavigateToDestURL();
2112 // Checks that redirects with location.replace do not cancel a prerender and
2113 // and swap when navigating to the second.
2114 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2115 PrerenderLocationReplaceNavigateToSecond) {
2116 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2117 FINAL_STATUS_USED,
2119 NavigateToURL("files/prerender/prerender_page.html");
2122 // Checks that we get the right PPLT histograms for client redirect prerenders
2123 // and navigations when the referring page is Google.
2124 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2125 PrerenderLocationReplaceGWSHistograms) {
2126 DisableJavascriptCalls();
2128 // The loader page should look like Google.
2129 static const char kGoogleDotCom[] = "www.google.com";
2130 SetLoaderHostOverride(kGoogleDotCom);
2131 set_loader_path("files/prerender/prerender_loader_with_replace_state.html");
2133 GURL dest_url = GetCrossDomainTestUrl(
2134 "files/prerender/prerender_deferred_image.html");
2136 GURL prerender_url = test_server()->GetURL(
2137 "files/prerender/prerender_location_replace.html?" +
2138 net::EscapeQueryParamValue(dest_url.spec(), false) +
2139 "#prerender");
2140 GURL::Replacements replacements;
2141 replacements.SetHostStr(kGoogleDotCom);
2142 prerender_url = prerender_url.ReplaceComponents(replacements);
2144 // The prerender will not completely load until after the swap, so wait for a
2145 // title change before calling DidPrerenderPass.
2146 scoped_ptr<TestPrerender> prerender =
2147 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 1);
2148 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
2149 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
2150 EXPECT_EQ(1, prerender->number_of_loads());
2152 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
2153 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
2154 histogram_tester().ExpectTotalCount(
2155 "Prerender.none_PerceivedPLTMatchedComplete", 0);
2156 // Although there is a client redirect, it is dropped from histograms because
2157 // it is a Google URL. The target page itself does not load until after the
2158 // swap.
2159 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2162 GURL navigate_url = test_server()->GetURL(
2163 "files/prerender/prerender_location_replace.html?" +
2164 net::EscapeQueryParamValue(dest_url.spec(), false) +
2165 "#navigate");
2166 navigate_url = navigate_url.ReplaceComponents(replacements);
2168 NavigationOrSwapObserver swap_observer(
2169 current_browser()->tab_strip_model(),
2170 GetActiveWebContents(), 2);
2171 current_browser()->OpenURL(OpenURLParams(
2172 navigate_url, Referrer(), CURRENT_TAB,
2173 ui::PAGE_TRANSITION_TYPED, false));
2174 swap_observer.Wait();
2176 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2178 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2180 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLT", 1);
2181 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLTMatched", 1);
2182 histogram_tester().ExpectTotalCount(
2183 "Prerender.gws_PerceivedPLTMatchedComplete", 1);
2185 // The client redirect does /not/ count as a miss because it's a Google URL.
2186 histogram_tester().ExpectTotalCount("Prerender.PerceivedPLTFirstAfterMiss",
2190 // Checks that client-issued redirects work with prerendering.
2191 // This version navigates to the final destination page, rather than the
2192 // page which does the redirection via a mouse click.
2193 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2194 PrerenderClientRedirectNavigateToSecondViaClick) {
2195 GURL prerender_url = test_server()->GetURL(
2196 CreateClientRedirect("files/prerender/prerender_page.html"));
2197 GURL destination_url = test_server()->GetURL(
2198 "files/prerender/prerender_page.html");
2199 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 2);
2200 OpenURLViaClick(destination_url);
2203 // Checks that a page served over HTTPS is correctly prerendered.
2204 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttps) {
2205 net::SpawnedTestServer https_server(
2206 net::SpawnedTestServer::TYPE_HTTPS, net::SpawnedTestServer::kLocalhost,
2207 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2208 ASSERT_TRUE(https_server.Start());
2209 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2210 PrerenderTestURL(https_url,
2211 FINAL_STATUS_USED,
2213 NavigateToDestURL();
2216 // Checks that client-issued redirects within an iframe in a prerendered
2217 // page will not count as an "alias" for the prerendered page.
2218 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2219 PrerenderClientRedirectInIframe) {
2220 std::string redirect_path = CreateClientRedirect(
2221 "/files/prerender/prerender_embedded_content.html");
2222 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2223 replacement_text.push_back(
2224 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
2225 std::string replacement_path;
2226 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2227 "files/prerender/prerender_with_iframe.html",
2228 replacement_text,
2229 &replacement_path));
2230 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 2);
2231 EXPECT_FALSE(UrlIsInPrerenderManager(
2232 "files/prerender/prerender_embedded_content.html"));
2233 NavigateToDestURL();
2236 // Checks that server-issued redirects work with prerendering.
2237 // This version navigates to the page which issues the redirection, rather
2238 // than the final destination page.
2239 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2240 PrerenderServerRedirectNavigateToFirst) {
2241 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2242 FINAL_STATUS_USED,
2244 NavigateToDestURL();
2247 // Checks that server-issued redirects work with prerendering.
2248 // This version navigates to the final destination page, rather than the
2249 // page which does the redirection.
2250 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2251 PrerenderServerRedirectNavigateToSecond) {
2252 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2253 FINAL_STATUS_USED,
2255 NavigateToURL("files/prerender/prerender_page.html");
2258 // Checks that server-issued redirects work with prerendering.
2259 // This version navigates to the final destination page, rather than the
2260 // page which does the redirection via a mouse click.
2261 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2262 PrerenderServerRedirectNavigateToSecondViaClick) {
2263 GURL prerender_url = test_server()->GetURL(
2264 CreateServerRedirect("files/prerender/prerender_page.html"));
2265 GURL destination_url = test_server()->GetURL(
2266 "files/prerender/prerender_page.html");
2267 PrerenderTestURL(prerender_url, FINAL_STATUS_USED, 1);
2268 OpenURLViaClick(destination_url);
2271 // Checks that server-issued redirects within an iframe in a prerendered
2272 // page will not count as an "alias" for the prerendered page.
2273 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderServerRedirectInIframe) {
2274 std::string redirect_path = CreateServerRedirect(
2275 "/files/prerender/prerender_embedded_content.html");
2276 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2277 replacement_text.push_back(
2278 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
2279 std::string replacement_path;
2280 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2281 "files/prerender/prerender_with_iframe.html",
2282 replacement_text,
2283 &replacement_path));
2284 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2285 EXPECT_FALSE(UrlIsInPrerenderManager(
2286 "files/prerender/prerender_embedded_content.html"));
2287 NavigateToDestURL();
2290 // Prerenders a page that contains an automatic download triggered through an
2291 // iframe. This should not prerender successfully.
2292 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadIframe) {
2293 PrerenderTestURL("files/prerender/prerender_download_iframe.html",
2294 FINAL_STATUS_DOWNLOAD,
2298 // Prerenders a page that contains an automatic download triggered through
2299 // Javascript changing the window.location. This should not prerender
2300 // successfully
2301 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadLocation) {
2302 PrerenderTestURL(CreateClientRedirect("files/download-test1.lib"),
2303 FINAL_STATUS_DOWNLOAD,
2307 // Prerenders a page that contains an automatic download triggered through a
2308 // client-issued redirect. This should not prerender successfully.
2309 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadClientRedirect) {
2310 PrerenderTestURL("files/prerender/prerender_download_refresh.html",
2311 FINAL_STATUS_DOWNLOAD,
2315 // Checks that the referrer is set when prerendering.
2316 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrer) {
2317 PrerenderTestURL("files/prerender/prerender_referrer.html",
2318 FINAL_STATUS_USED,
2320 NavigateToDestURL();
2323 // Checks that the referrer is not set when prerendering and the source page is
2324 // HTTPS.
2325 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2326 PrerenderNoSSLReferrer) {
2327 UseHttpsSrcServer();
2328 PrerenderTestURL("files/prerender/prerender_no_referrer.html",
2329 FINAL_STATUS_USED,
2331 NavigateToDestURL();
2334 // Checks that the referrer is set when prerendering is cancelled.
2335 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelReferrer) {
2336 scoped_ptr<TestContentBrowserClient> test_content_browser_client(
2337 new TestContentBrowserClient);
2338 content::ContentBrowserClient* original_browser_client =
2339 content::SetBrowserClientForTesting(test_content_browser_client.get());
2341 PrerenderTestURL("files/prerender/prerender_referrer.html",
2342 FINAL_STATUS_CANCELLED,
2344 OpenDestURLViaClick();
2346 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2348 content::SetBrowserClientForTesting(original_browser_client);
2351 // Checks that popups on a prerendered page cause cancellation.
2352 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPopup) {
2353 PrerenderTestURL("files/prerender/prerender_popup.html",
2354 FINAL_STATUS_CREATE_NEW_WINDOW,
2358 // Checks that registering a protocol handler causes cancellation.
2359 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRegisterProtocolHandler) {
2360 PrerenderTestURL("files/prerender/prerender_register_protocol_handler.html",
2361 FINAL_STATUS_REGISTER_PROTOCOL_HANDLER,
2365 // Checks that renderers using excessive memory will be terminated.
2366 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExcessiveMemory) {
2367 ASSERT_TRUE(GetPrerenderManager());
2368 GetPrerenderManager()->mutable_config().max_bytes = 30 * 1024 * 1024;
2369 // The excessive memory kill may happen before or after the load event as it
2370 // happens asynchronously with IPC calls. Even if the test does not start
2371 // allocating until after load, the browser process might notice before the
2372 // message gets through. This happens on XP debug bots because they're so
2373 // slow. Instead, don't bother checking the load event count.
2374 DisableLoadEventCheck();
2375 PrerenderTestURL("files/prerender/prerender_excessive_memory.html",
2376 FINAL_STATUS_MEMORY_LIMIT_EXCEEDED, 0);
2379 // Checks shutdown code while a prerender is active.
2380 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderQuickQuit) {
2381 DisableJavascriptCalls();
2382 DisableLoadEventCheck();
2383 PrerenderTestURL("files/prerender/prerender_page.html",
2384 FINAL_STATUS_APP_TERMINATING,
2388 // Checks that we don't prerender in an infinite loop.
2389 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderInfiniteLoop) {
2390 const char* const kHtmlFileA = "files/prerender/prerender_infinite_a.html";
2391 const char* const kHtmlFileB = "files/prerender/prerender_infinite_b.html";
2393 std::vector<FinalStatus> expected_final_status_queue;
2394 expected_final_status_queue.push_back(FINAL_STATUS_USED);
2395 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2397 ScopedVector<TestPrerender> prerenders =
2398 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
2399 ASSERT_TRUE(prerenders[0]->contents());
2400 // Assert that the pending prerender is in there already. This relies on the
2401 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2402 // the page load one.
2403 EXPECT_EQ(2U, GetLinkPrerenderCount());
2404 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2406 // Next url should be in pending list but not an active entry.
2407 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2409 NavigateToDestURL();
2411 // Make sure the PrerenderContents for the next url is now in the manager and
2412 // not pending. This relies on pending prerenders being resolved in the same
2413 // event loop iteration as OnPrerenderStop.
2414 EXPECT_TRUE(UrlIsInPrerenderManager(kHtmlFileB));
2415 EXPECT_EQ(1U, GetLinkPrerenderCount());
2416 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2419 // Checks that we don't prerender in an infinite loop and multiple links are
2420 // handled correctly.
2421 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2422 PrerenderInfiniteLoopMultiple) {
2423 const char* const kHtmlFileA =
2424 "files/prerender/prerender_infinite_a_multiple.html";
2425 const char* const kHtmlFileB =
2426 "files/prerender/prerender_infinite_b_multiple.html";
2427 const char* const kHtmlFileC =
2428 "files/prerender/prerender_infinite_c_multiple.html";
2430 // This test is conceptually simplest if concurrency is at two, since we
2431 // don't have to worry about which of kHtmlFileB or kHtmlFileC gets evicted.
2432 GetPrerenderManager()->mutable_config().max_link_concurrency = 2;
2433 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher = 2;
2435 std::vector<FinalStatus> expected_final_status_queue;
2436 expected_final_status_queue.push_back(FINAL_STATUS_USED);
2437 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2438 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
2440 ScopedVector<TestPrerender> prerenders =
2441 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
2442 ASSERT_TRUE(prerenders[0]->contents());
2444 // Next url should be in pending list but not an active entry. This relies on
2445 // the fact that the renderer sends out the AddLinkRelPrerender IPC before
2446 // sending the page load one.
2447 EXPECT_EQ(3U, GetLinkPrerenderCount());
2448 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2449 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2450 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileC));
2452 NavigateToDestURL();
2454 // Make sure the PrerenderContents for the next urls are now in the manager
2455 // and not pending. One and only one of the URLs (the last seen) should be the
2456 // active entry. This relies on pending prerenders being resolved in the same
2457 // event loop iteration as OnPrerenderStop.
2458 bool url_b_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileB);
2459 bool url_c_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileC);
2460 EXPECT_TRUE(url_b_is_active_prerender && url_c_is_active_prerender);
2461 EXPECT_EQ(2U, GetLinkPrerenderCount());
2462 EXPECT_EQ(2U, GetRunningLinkPrerenderCount());
2465 // Checks that pending prerenders are aborted (and never launched) when launched
2466 // by a prerender that itself gets aborted.
2467 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAbortPendingOnCancel) {
2468 const char* const kHtmlFileA = "files/prerender/prerender_infinite_a.html";
2469 const char* const kHtmlFileB = "files/prerender/prerender_infinite_b.html";
2471 scoped_ptr<TestPrerender> prerender =
2472 PrerenderTestURL(kHtmlFileA, FINAL_STATUS_CANCELLED, 1);
2473 ASSERT_TRUE(prerender->contents());
2474 // Assert that the pending prerender is in there already. This relies on the
2475 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2476 // the page load one.
2477 EXPECT_EQ(2U, GetLinkPrerenderCount());
2478 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2480 // Next url should be in pending list but not an active entry.
2481 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
2483 // Cancel the prerender.
2484 GetPrerenderManager()->CancelAllPrerenders();
2485 prerender->WaitForStop();
2487 // All prerenders are now gone.
2488 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
2491 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerBeforePrerender) {
2492 // This test is for the old implementation of the task manager. We must
2493 // explicitly disable the new one.
2494 task_manager::browsertest_util::EnableOldTaskManager();
2496 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2497 const base::string16 any_tab = MatchTaskManagerTab("*");
2498 const base::string16 original = MatchTaskManagerTab("Preloader");
2499 const base::string16 prerender = MatchTaskManagerPrerender("Prerender Page");
2500 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2502 // Show the task manager. This populates the model.
2503 chrome::OpenTaskManager(current_browser());
2504 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2505 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2507 // Prerender a page in addition to the original tab.
2508 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2510 // A TaskManager entry should appear like "Prerender: Prerender Page"
2511 // alongside the original tab entry. There should be just these two entries.
2512 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender));
2513 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original));
2514 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final));
2515 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender));
2516 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2518 // Swap in the prerendered content.
2519 NavigateToDestURL();
2521 // The "Prerender: " TaskManager entry should disappear, being replaced by a
2522 // "Tab: Prerender Page" entry, and nothing else.
2523 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender));
2524 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original));
2525 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2526 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2527 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2530 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerAfterPrerender) {
2531 // This test is for the old implementation of the task manager. We must
2532 // explicitly disable the new one.
2533 task_manager::browsertest_util::EnableOldTaskManager();
2535 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2536 const base::string16 any_tab = MatchTaskManagerTab("*");
2537 const base::string16 original = MatchTaskManagerTab("Preloader");
2538 const base::string16 prerender = MatchTaskManagerPrerender("Prerender Page");
2539 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2541 // Start with two resources.
2542 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2544 // Show the task manager. This populates the model. Importantly, we're doing
2545 // this after the prerender WebContents already exists - the task manager
2546 // needs to find it, it can't just listen for creation.
2547 chrome::OpenTaskManager(current_browser());
2549 // A TaskManager entry should appear like "Prerender: Prerender Page"
2550 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender));
2551 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original));
2552 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final));
2553 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender));
2554 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2556 // Swap in the tab.
2557 NavigateToDestURL();
2559 // The "Prerender: Prerender Page" TaskManager row should disappear, being
2560 // replaced by "Tab: Prerender Page"
2561 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender));
2562 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original));
2563 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2564 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2565 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2568 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, OpenTaskManagerAfterSwapIn) {
2569 // This test is for the old implementation of the task manager. We must
2570 // explicitly disable the new one.
2571 task_manager::browsertest_util::EnableOldTaskManager();
2573 const base::string16 any_prerender = MatchTaskManagerPrerender("*");
2574 const base::string16 any_tab = MatchTaskManagerTab("*");
2575 const base::string16 final = MatchTaskManagerTab("Prerender Page");
2577 // Prerender, and swap it in.
2578 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
2579 NavigateToDestURL();
2581 // Show the task manager. This populates the model. Importantly, we're doing
2582 // this after the prerender has been swapped in.
2583 chrome::OpenTaskManager(current_browser());
2585 // We should not see a prerender resource in the task manager, just a normal
2586 // page.
2587 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final));
2588 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab));
2589 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender));
2592 // Checks that audio loads are deferred on prerendering.
2593 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Audio) {
2594 PrerenderTestURL("files/prerender/prerender_html5_audio.html",
2595 FINAL_STATUS_USED,
2597 NavigateToDestURL();
2598 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2601 // Checks that audio loads are deferred on prerendering and played back when
2602 // the prerender is swapped in if autoplay is set.
2603 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioAutoplay) {
2604 PrerenderTestURL("files/prerender/prerender_html5_audio_autoplay.html",
2605 FINAL_STATUS_USED,
2607 NavigateToDestURL();
2608 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2611 // Checks that audio loads are deferred on prerendering and played back when
2612 // the prerender is swapped in if js starts playing.
2613 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioJsplay) {
2614 PrerenderTestURL("files/prerender/prerender_html5_audio_jsplay.html",
2615 FINAL_STATUS_USED,
2617 NavigateToDestURL();
2618 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2621 // Checks that video loads are deferred on prerendering.
2622 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Video) {
2623 PrerenderTestURL("files/prerender/prerender_html5_video.html",
2624 FINAL_STATUS_USED,
2626 NavigateToDestURL();
2627 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2630 // Checks that video tags inserted by javascript are deferred and played
2631 // correctly on swap in.
2632 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoJs) {
2633 PrerenderTestURL("files/prerender/prerender_html5_video_script.html",
2634 FINAL_STATUS_USED,
2636 NavigateToDestURL();
2637 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2640 // Checks for correct network events by using a busy sleep the javascript.
2641 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoNetwork) {
2642 DisableJavascriptCalls();
2643 scoped_ptr<TestPrerender> prerender =
2644 PrerenderTestURL("files/prerender/prerender_html5_video_network.html",
2645 FINAL_STATUS_USED,
2647 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
2648 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
2649 NavigateToDestURL();
2650 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
2653 // Checks that scripts can retrieve the correct window size while prerendering.
2654 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWindowSize) {
2655 PrerenderTestURL("files/prerender/prerender_size.html",
2656 FINAL_STATUS_USED,
2658 NavigateToDestURL();
2661 // TODO(jam): http://crbug.com/350550
2662 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
2664 // Checks that prerenderers will terminate when the RenderView crashes.
2665 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRendererCrash) {
2666 scoped_ptr<TestPrerender> prerender =
2667 PrerenderTestURL("files/prerender/prerender_page.html",
2668 FINAL_STATUS_RENDERER_CRASHED,
2671 // Navigate to about:crash and then wait for the renderer to crash.
2672 ASSERT_TRUE(prerender->contents());
2673 ASSERT_TRUE(prerender->contents()->prerender_contents());
2674 prerender->contents()->prerender_contents()->GetController().
2675 LoadURL(
2676 GURL(content::kChromeUICrashURL),
2677 content::Referrer(),
2678 ui::PAGE_TRANSITION_TYPED,
2679 std::string());
2680 prerender->WaitForStop();
2682 #endif
2684 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2685 PrerenderPageWithFragment) {
2686 PrerenderTestURL("files/prerender/prerender_page.html#fragment",
2687 FINAL_STATUS_USED,
2690 ChannelDestructionWatcher channel_close_watcher;
2691 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
2692 GetActiveWebContents()->GetRenderProcessHost());
2693 NavigateToDestURL();
2694 channel_close_watcher.WaitForChannelClose();
2696 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2699 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2700 PrerenderPageWithRedirectedFragment) {
2701 PrerenderTestURL(
2702 CreateClientRedirect("files/prerender/prerender_page.html#fragment"),
2703 FINAL_STATUS_USED,
2706 ChannelDestructionWatcher channel_close_watcher;
2707 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
2708 GetActiveWebContents()->GetRenderProcessHost());
2709 NavigateToDestURL();
2710 channel_close_watcher.WaitForChannelClose();
2712 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2715 // Checks that we do not use a prerendered page when navigating from
2716 // the main page to a fragment.
2717 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2718 PrerenderPageNavigateFragment) {
2719 PrerenderTestURL("files/prerender/no_prerender_page.html",
2720 FINAL_STATUS_APP_TERMINATING,
2722 NavigateToURLWithDisposition(
2723 "files/prerender/no_prerender_page.html#fragment",
2724 CURRENT_TAB, false);
2727 // Checks that we do not use a prerendered page when we prerender a fragment
2728 // but navigate to the main page.
2729 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2730 PrerenderFragmentNavigatePage) {
2731 PrerenderTestURL("files/prerender/no_prerender_page.html#fragment",
2732 FINAL_STATUS_APP_TERMINATING,
2734 NavigateToURLWithDisposition(
2735 "files/prerender/no_prerender_page.html",
2736 CURRENT_TAB, false);
2739 // Checks that we do not use a prerendered page when we prerender a fragment
2740 // but navigate to a different fragment on the same page.
2741 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2742 PrerenderFragmentNavigateFragment) {
2743 PrerenderTestURL("files/prerender/no_prerender_page.html#other_fragment",
2744 FINAL_STATUS_APP_TERMINATING,
2746 NavigateToURLWithDisposition(
2747 "files/prerender/no_prerender_page.html#fragment",
2748 CURRENT_TAB, false);
2751 // Checks that we do not use a prerendered page when the page uses a client
2752 // redirect to refresh from a fragment on the same page.
2753 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2754 PrerenderClientRedirectFromFragment) {
2755 PrerenderTestURL(
2756 CreateClientRedirect("files/prerender/no_prerender_page.html#fragment"),
2757 FINAL_STATUS_APP_TERMINATING,
2759 NavigateToURLWithDisposition(
2760 "files/prerender/no_prerender_page.html",
2761 CURRENT_TAB, false);
2764 // Checks that we do not use a prerendered page when the page uses a client
2765 // redirect to refresh to a fragment on the same page.
2766 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2767 PrerenderClientRedirectToFragment) {
2768 PrerenderTestURL(
2769 CreateClientRedirect("files/prerender/no_prerender_page.html"),
2770 FINAL_STATUS_APP_TERMINATING,
2772 NavigateToURLWithDisposition(
2773 "files/prerender/no_prerender_page.html#fragment",
2774 CURRENT_TAB, false);
2777 // Checks that we correctly use a prerendered page when the page uses JS to set
2778 // the window.location.hash to a fragment on the same page.
2779 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2780 PrerenderPageChangeFragmentLocationHash) {
2781 PrerenderTestURL("files/prerender/prerender_fragment_location_hash.html",
2782 FINAL_STATUS_USED,
2784 NavigateToURL("files/prerender/prerender_fragment_location_hash.html");
2787 // Checks that prerendering a PNG works correctly.
2788 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImagePng) {
2789 DisableJavascriptCalls();
2790 PrerenderTestURL("files/prerender/image.png", FINAL_STATUS_USED, 1);
2791 NavigateToDestURL();
2794 // Checks that prerendering a JPG works correctly.
2795 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImageJpeg) {
2796 DisableJavascriptCalls();
2797 PrerenderTestURL("files/prerender/image.jpeg", FINAL_STATUS_USED, 1);
2798 NavigateToDestURL();
2801 // Checks that a prerender of a CRX will result in a cancellation due to
2802 // download.
2803 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCrx) {
2804 PrerenderTestURL("files/prerender/extension.crx", FINAL_STATUS_DOWNLOAD, 0);
2807 // Checks that xhr GET requests allow prerenders.
2808 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrGet) {
2809 PrerenderTestURL("files/prerender/prerender_xhr_get.html",
2810 FINAL_STATUS_USED,
2812 NavigateToDestURL();
2815 // Checks that xhr HEAD requests allow prerenders.
2816 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrHead) {
2817 PrerenderTestURL("files/prerender/prerender_xhr_head.html",
2818 FINAL_STATUS_USED,
2820 NavigateToDestURL();
2823 // Checks that xhr OPTIONS requests allow prerenders.
2824 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrOptions) {
2825 PrerenderTestURL("files/prerender/prerender_xhr_options.html",
2826 FINAL_STATUS_USED,
2828 NavigateToDestURL();
2831 // Checks that xhr TRACE requests allow prerenders.
2832 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrTrace) {
2833 PrerenderTestURL("files/prerender/prerender_xhr_trace.html",
2834 FINAL_STATUS_USED,
2836 NavigateToDestURL();
2839 // Checks that xhr POST requests allow prerenders.
2840 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPost) {
2841 PrerenderTestURL("files/prerender/prerender_xhr_post.html",
2842 FINAL_STATUS_USED,
2844 NavigateToDestURL();
2847 // Checks that xhr PUT cancels prerenders.
2848 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPut) {
2849 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
2850 FINAL_STATUS_INVALID_HTTP_METHOD,
2854 // Checks that xhr DELETE cancels prerenders.
2855 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrDelete) {
2856 PrerenderTestURL("files/prerender/prerender_xhr_delete.html",
2857 FINAL_STATUS_INVALID_HTTP_METHOD,
2861 // Checks that a top-level page which would trigger an SSL error is canceled.
2862 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorTopLevel) {
2863 net::SpawnedTestServer::SSLOptions ssl_options;
2864 ssl_options.server_certificate =
2865 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2866 net::SpawnedTestServer https_server(
2867 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2868 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2869 ASSERT_TRUE(https_server.Start());
2870 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2871 PrerenderTestURL(https_url,
2872 FINAL_STATUS_SSL_ERROR,
2876 // Checks that an SSL error that comes from a subresource does not cancel
2877 // the page. Non-main-frame requests are simply cancelled if they run into
2878 // an SSL problem.
2879 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorSubresource) {
2880 net::SpawnedTestServer::SSLOptions ssl_options;
2881 ssl_options.server_certificate =
2882 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2883 net::SpawnedTestServer https_server(
2884 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2885 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2886 ASSERT_TRUE(https_server.Start());
2887 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
2888 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2889 replacement_text.push_back(
2890 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
2891 std::string replacement_path;
2892 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2893 "files/prerender/prerender_with_image.html",
2894 replacement_text,
2895 &replacement_path));
2896 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2897 NavigateToDestURL();
2900 // Checks that an SSL error that comes from an iframe does not cancel
2901 // the page. Non-main-frame requests are simply cancelled if they run into
2902 // an SSL problem.
2903 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorIframe) {
2904 net::SpawnedTestServer::SSLOptions ssl_options;
2905 ssl_options.server_certificate =
2906 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME;
2907 net::SpawnedTestServer https_server(
2908 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2909 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2910 ASSERT_TRUE(https_server.Start());
2911 GURL https_url = https_server.GetURL(
2912 "files/prerender/prerender_embedded_content.html");
2913 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
2914 replacement_text.push_back(
2915 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
2916 std::string replacement_path;
2917 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2918 "files/prerender/prerender_with_iframe.html",
2919 replacement_text,
2920 &replacement_path));
2921 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
2922 NavigateToDestURL();
2925 // Checks that we cancel correctly when window.print() is called.
2926 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPrint) {
2927 DisableLoadEventCheck();
2928 PrerenderTestURL("files/prerender/prerender_print.html",
2929 FINAL_STATUS_WINDOW_PRINT,
2933 // Checks that prerenders do not get swapped into target pages that have opened
2934 // popups; the BrowsingInstance is not empty.
2935 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderTargetHasPopup) {
2936 PrerenderTestURL("files/prerender/prerender_page.html",
2937 FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE,
2939 OpenURLViaWindowOpen(GURL(url::kAboutBlankURL));
2941 // Switch back to the current tab and attempt to swap it in.
2942 current_browser()->tab_strip_model()->ActivateTabAt(0, true);
2943 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
2946 class TestClientCertStore : public net::ClientCertStore {
2947 public:
2948 TestClientCertStore() {}
2949 ~TestClientCertStore() override {}
2951 // net::ClientCertStore:
2952 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info,
2953 net::CertificateList* selected_certs,
2954 const base::Closure& callback) override {
2955 *selected_certs = net::CertificateList(
2956 1, scoped_refptr<net::X509Certificate>(
2957 new net::X509Certificate("test", "test", base::Time(), base::Time())));
2958 callback.Run();
2962 scoped_ptr<net::ClientCertStore> CreateCertStore() {
2963 return scoped_ptr<net::ClientCertStore>(new TestClientCertStore);
2966 // Checks that a top-level page which would normally request an SSL client
2967 // certificate will never be seen since it's an https top-level resource.
2968 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2969 PrerenderSSLClientCertTopLevel) {
2970 ProfileIOData::FromResourceContext(
2971 current_browser()->profile()->GetResourceContext())->
2972 set_client_cert_store_factory_for_testing(
2973 base::Bind(&CreateCertStore));
2974 net::SpawnedTestServer::SSLOptions ssl_options;
2975 ssl_options.request_client_certificate = true;
2976 net::SpawnedTestServer https_server(
2977 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2978 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2979 ASSERT_TRUE(https_server.Start());
2980 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
2981 PrerenderTestURL(https_url, FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED, 0);
2984 // Checks that an SSL Client Certificate request that originates from a
2985 // subresource will cancel the prerendered page.
2986 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
2987 PrerenderSSLClientCertSubresource) {
2988 ProfileIOData::FromResourceContext(
2989 current_browser()->profile()->GetResourceContext())->
2990 set_client_cert_store_factory_for_testing(
2991 base::Bind(&CreateCertStore));
2992 net::SpawnedTestServer::SSLOptions ssl_options;
2993 ssl_options.request_client_certificate = true;
2994 net::SpawnedTestServer https_server(
2995 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
2996 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2997 ASSERT_TRUE(https_server.Start());
2998 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
2999 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3000 replacement_text.push_back(
3001 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
3002 std::string replacement_path;
3003 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3004 "files/prerender/prerender_with_image.html",
3005 replacement_text,
3006 &replacement_path));
3007 PrerenderTestURL(replacement_path,
3008 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
3012 // Checks that an SSL Client Certificate request that originates from an
3013 // iframe will cancel the prerendered page.
3014 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLClientCertIframe) {
3015 ProfileIOData::FromResourceContext(
3016 current_browser()->profile()->GetResourceContext())->
3017 set_client_cert_store_factory_for_testing(
3018 base::Bind(&CreateCertStore));
3019 net::SpawnedTestServer::SSLOptions ssl_options;
3020 ssl_options.request_client_certificate = true;
3021 net::SpawnedTestServer https_server(
3022 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
3023 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3024 ASSERT_TRUE(https_server.Start());
3025 GURL https_url = https_server.GetURL(
3026 "files/prerender/prerender_embedded_content.html");
3027 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3028 replacement_text.push_back(
3029 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
3030 std::string replacement_path;
3031 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3032 "files/prerender/prerender_with_iframe.html",
3033 replacement_text,
3034 &replacement_path));
3035 PrerenderTestURL(replacement_path,
3036 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
3040 #if defined(FULL_SAFE_BROWSING)
3041 // Ensures that we do not prerender pages with a safe browsing
3042 // interstitial.
3043 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingTopLevel) {
3044 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3045 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3046 url, SB_THREAT_TYPE_URL_MALWARE);
3047 PrerenderTestURL("files/prerender/prerender_page.html",
3048 FINAL_STATUS_SAFE_BROWSING, 0);
3051 // Ensures that server redirects to a malware page will cancel prerenders.
3052 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3053 PrerenderSafeBrowsingServerRedirect) {
3054 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3055 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3056 url, SB_THREAT_TYPE_URL_MALWARE);
3057 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
3058 FINAL_STATUS_SAFE_BROWSING,
3062 // Ensures that client redirects to a malware page will cancel prerenders.
3063 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3064 PrerenderSafeBrowsingClientRedirect) {
3065 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3066 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3067 url, SB_THREAT_TYPE_URL_MALWARE);
3068 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
3069 FINAL_STATUS_SAFE_BROWSING,
3073 // Ensures that we do not prerender pages which have a malware subresource.
3074 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingSubresource) {
3075 GURL image_url = test_server()->GetURL("files/prerender/image.jpeg");
3076 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3077 image_url, SB_THREAT_TYPE_URL_MALWARE);
3078 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3079 replacement_text.push_back(
3080 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3081 std::string replacement_path;
3082 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3083 "files/prerender/prerender_with_image.html",
3084 replacement_text,
3085 &replacement_path));
3086 PrerenderTestURL(replacement_path,
3087 FINAL_STATUS_SAFE_BROWSING,
3091 // Ensures that we do not prerender pages which have a malware iframe.
3092 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingIframe) {
3093 GURL iframe_url = test_server()->GetURL(
3094 "files/prerender/prerender_embedded_content.html");
3095 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3096 iframe_url, SB_THREAT_TYPE_URL_MALWARE);
3097 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3098 replacement_text.push_back(
3099 std::make_pair("REPLACE_WITH_URL", iframe_url.spec()));
3100 std::string replacement_path;
3101 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3102 "files/prerender/prerender_with_iframe.html",
3103 replacement_text,
3104 &replacement_path));
3105 PrerenderTestURL(replacement_path,
3106 FINAL_STATUS_SAFE_BROWSING,
3110 #endif
3112 // Checks that a local storage read will not cause prerender to fail.
3113 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageRead) {
3114 PrerenderTestURL("files/prerender/prerender_localstorage_read.html",
3115 FINAL_STATUS_USED,
3117 NavigateToDestURL();
3120 // Checks that a local storage write will not cause prerender to fail.
3121 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) {
3122 PrerenderTestURL("files/prerender/prerender_localstorage_write.html",
3123 FINAL_STATUS_USED,
3125 NavigateToDestURL();
3128 // Checks that the favicon is properly loaded on prerender.
3129 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderFavicon) {
3130 scoped_ptr<TestPrerender> prerender =
3131 PrerenderTestURL("files/prerender/prerender_favicon.html",
3132 FINAL_STATUS_USED,
3134 NavigateToDestURL();
3136 favicon::FaviconDriver* favicon_driver =
3137 favicon::ContentFaviconDriver::FromWebContents(GetActiveWebContents());
3138 if (!favicon_driver->FaviconIsValid()) {
3139 // If the favicon has not been set yet, wait for it to be.
3140 FaviconUpdateWatcher favicon_update_watcher(GetActiveWebContents());
3141 favicon_update_watcher.Wait();
3143 EXPECT_TRUE(favicon_driver->FaviconIsValid());
3146 // Checks that when a prerendered page is swapped in to a referring page, the
3147 // unload handlers on the referring page are executed.
3148 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderUnload) {
3149 // Matches URL in prerender_loader_with_unload.html.
3150 const GURL unload_url("http://unload-url.test");
3151 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3152 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3153 RequestCounter unload_counter;
3154 BrowserThread::PostTask(
3155 BrowserThread::IO, FROM_HERE,
3156 base::Bind(&CreateCountingInterceptorOnIO,
3157 unload_url, empty_file, unload_counter.AsWeakPtr()));
3159 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3160 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3161 NavigateToDestURL();
3162 unload_counter.WaitForCount(1);
3165 // Checks that a hanging unload on the referring page of a prerender swap does
3166 // not crash the browser on exit.
3167 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHangingUnload) {
3168 // Matches URL in prerender_loader_with_unload.html.
3169 const GURL hang_url("http://unload-url.test");
3170 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3171 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3172 BrowserThread::PostTask(
3173 BrowserThread::IO, FROM_HERE,
3174 base::Bind(&CreateHangingFirstRequestInterceptorOnIO,
3175 hang_url, empty_file,
3176 base::Closure()));
3178 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3179 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3180 NavigateToDestURL();
3184 // Checks that when the history is cleared, prerendering is cancelled and
3185 // prerendering history is cleared.
3186 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearHistory) {
3187 scoped_ptr<TestPrerender> prerender =
3188 PrerenderTestURL("files/prerender/prerender_page.html",
3189 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
3192 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_HISTORY);
3193 prerender->WaitForStop();
3195 // Make sure prerender history was cleared.
3196 EXPECT_EQ(0, GetHistoryLength());
3199 // Checks that when the cache is cleared, prerenders are cancelled but
3200 // prerendering history is not cleared.
3201 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearCache) {
3202 scoped_ptr<TestPrerender> prerender =
3203 PrerenderTestURL("files/prerender/prerender_page.html",
3204 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
3207 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_CACHE);
3208 prerender->WaitForStop();
3210 // Make sure prerender history was not cleared. Not a vital behavior, but
3211 // used to compare with PrerenderClearHistory test.
3212 EXPECT_EQ(1, GetHistoryLength());
3215 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelAll) {
3216 scoped_ptr<TestPrerender> prerender =
3217 PrerenderTestURL("files/prerender/prerender_page.html",
3218 FINAL_STATUS_CANCELLED,
3221 GetPrerenderManager()->CancelAllPrerenders();
3222 prerender->WaitForStop();
3224 EXPECT_FALSE(prerender->contents());
3227 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderEvents) {
3228 scoped_ptr<TestPrerender> prerender =
3229 PrerenderTestURL("files/prerender/prerender_page.html",
3230 FINAL_STATUS_CANCELLED, 1);
3232 GetPrerenderManager()->CancelAllPrerenders();
3233 prerender->WaitForStop();
3235 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
3236 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
3237 EXPECT_FALSE(HadPrerenderEventErrors());
3240 // Cancels the prerender of a page with its own prerender. The second prerender
3241 // should never be started.
3242 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3243 PrerenderCancelPrerenderWithPrerender) {
3244 scoped_ptr<TestPrerender> prerender =
3245 PrerenderTestURL("files/prerender/prerender_infinite_a.html",
3246 FINAL_STATUS_CANCELLED,
3249 GetPrerenderManager()->CancelAllPrerenders();
3250 prerender->WaitForStop();
3252 EXPECT_FALSE(prerender->contents());
3255 // Prerendering and history tests.
3256 // The prerendered page is navigated to in several ways [navigate via
3257 // omnibox, click on link, key-modified click to open in background tab, etc],
3258 // followed by a navigation to another page from the prerendered page, followed
3259 // by a back navigation.
3261 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNavigateClickGoBack) {
3262 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3263 FINAL_STATUS_USED,
3265 NavigateToDestURL();
3266 ClickToNextPageAfterPrerender();
3267 GoBackToPrerender();
3270 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNavigateNavigateGoBack) {
3271 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3272 FINAL_STATUS_USED,
3274 NavigateToDestURL();
3275 NavigateToNextPageAfterPrerender();
3276 GoBackToPrerender();
3279 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickClickGoBack) {
3280 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3281 FINAL_STATUS_USED,
3283 OpenDestURLViaClick();
3284 ClickToNextPageAfterPrerender();
3285 GoBackToPrerender();
3288 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNavigateGoBack) {
3289 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3290 FINAL_STATUS_USED,
3292 OpenDestURLViaClick();
3293 NavigateToNextPageAfterPrerender();
3294 GoBackToPrerender();
3297 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewWindow) {
3298 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3299 FINAL_STATUS_APP_TERMINATING, 1);
3300 OpenDestURLViaClickNewWindow();
3303 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewForegroundTab) {
3304 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3305 FINAL_STATUS_APP_TERMINATING, 1);
3306 OpenDestURLViaClickNewForegroundTab();
3309 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3310 NavigateToPrerenderedPageWhenDevToolsAttached) {
3311 DisableJavascriptCalls();
3312 WebContents* web_contents =
3313 current_browser()->tab_strip_model()->GetActiveWebContents();
3314 scoped_refptr<DevToolsAgentHost> agent(
3315 DevToolsAgentHost::GetOrCreateFor(web_contents));
3316 FakeDevToolsClient client;
3317 agent->AttachClient(&client);
3318 const char* url = "files/prerender/prerender_page.html";
3319 PrerenderTestURL(url, FINAL_STATUS_DEVTOOLS_ATTACHED, 1);
3320 NavigateToURLWithDisposition(url, CURRENT_TAB, false);
3321 agent->DetachClient();
3324 // Validate that the sessionStorage namespace remains the same when swapping
3325 // in a prerendered page.
3326 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSessionStorage) {
3327 set_loader_path("files/prerender/prerender_loader_with_session_storage.html");
3328 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
3329 FINAL_STATUS_USED,
3331 NavigateToDestURL();
3332 GoBackToPageBeforePrerender();
3335 // Checks that the control group works. An XHR PUT cannot be detected in the
3336 // control group.
3337 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, ControlGroup) {
3338 RestorePrerenderMode restore_prerender_mode;
3339 PrerenderManager::SetMode(
3340 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
3341 DisableJavascriptCalls();
3342 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3343 FINAL_STATUS_WOULD_HAVE_BEEN_USED, 0);
3344 NavigateToDestURL();
3347 // Checks that the control group correctly hits WOULD_HAVE_BEEN_USED
3348 // renderer-initiated navigations. (This verifies that the ShouldFork logic
3349 // behaves correctly.)
3350 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, ControlGroupRendererInitiated) {
3351 RestorePrerenderMode restore_prerender_mode;
3352 PrerenderManager::SetMode(
3353 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP);
3354 DisableJavascriptCalls();
3355 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3356 FINAL_STATUS_WOULD_HAVE_BEEN_USED, 0);
3357 OpenDestURLViaClick();
3360 // Make sure that the MatchComplete dummy works in the normal case. Once
3361 // a prerender is cancelled because of a script, a dummy must be created to
3362 // account for the MatchComplete case, and it must have a final status of
3363 // FINAL_STATUS_WOULD_HAVE_BEEN_USED.
3364 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, MatchCompleteDummy) {
3365 RestorePrerenderMode restore_prerender_mode;
3366 PrerenderManager::SetMode(
3367 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3369 std::vector<FinalStatus> expected_final_status_queue;
3370 expected_final_status_queue.push_back(FINAL_STATUS_INVALID_HTTP_METHOD);
3371 expected_final_status_queue.push_back(FINAL_STATUS_WOULD_HAVE_BEEN_USED);
3372 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3373 expected_final_status_queue, 1);
3374 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3375 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3376 histogram_tester().ExpectTotalCount(
3377 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3378 histogram_tester().ExpectTotalCount(
3379 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
3381 NavigateToDestURL();
3382 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3383 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3385 histogram_tester().ExpectTotalCount(
3386 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3389 // Checks that the referrer policy is used when prerendering.
3390 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrerPolicy) {
3391 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3392 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3393 FINAL_STATUS_USED,
3395 NavigateToDestURL();
3398 // Checks that the referrer policy is used when prerendering on HTTPS.
3399 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3400 PrerenderSSLReferrerPolicy) {
3401 UseHttpsSrcServer();
3402 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3403 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3404 FINAL_STATUS_USED,
3406 NavigateToDestURL();
3409 // Checks that the referrer policy is used when prerendering is cancelled.
3410 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelReferrerPolicy) {
3411 scoped_ptr<TestContentBrowserClient> test_content_browser_client(
3412 new TestContentBrowserClient);
3413 content::ContentBrowserClient* original_browser_client =
3414 content::SetBrowserClientForTesting(test_content_browser_client.get());
3416 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3417 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3418 FINAL_STATUS_CANCELLED,
3420 OpenDestURLViaClick();
3422 bool display_test_result = false;
3423 WebContents* web_contents =
3424 browser()->tab_strip_model()->GetActiveWebContents();
3425 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
3426 web_contents,
3427 "window.domAutomationController.send(DidDisplayPass())",
3428 &display_test_result));
3429 EXPECT_TRUE(display_test_result);
3431 content::SetBrowserClientForTesting(original_browser_client);
3434 // Test interaction of the webNavigation and tabs API with prerender.
3435 class PrerenderBrowserTestWithExtensions : public PrerenderBrowserTest,
3436 public ExtensionApiTest {
3437 public:
3438 PrerenderBrowserTestWithExtensions() {
3439 // The individual tests start the test server through ExtensionApiTest, so
3440 // the port number can be passed through to the extension.
3441 autostart_test_server_ = false;
3444 void SetUp() override { PrerenderBrowserTest::SetUp(); }
3446 void SetUpCommandLine(base::CommandLine* command_line) override {
3447 PrerenderBrowserTest::SetUpCommandLine(command_line);
3448 ExtensionApiTest::SetUpCommandLine(command_line);
3451 void SetUpInProcessBrowserTestFixture() override {
3452 PrerenderBrowserTest::SetUpInProcessBrowserTestFixture();
3453 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
3456 void TearDownInProcessBrowserTestFixture() override {
3457 PrerenderBrowserTest::TearDownInProcessBrowserTestFixture();
3458 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
3461 void TearDownOnMainThread() override {
3462 PrerenderBrowserTest::TearDownOnMainThread();
3463 ExtensionApiTest::TearDownOnMainThread();
3466 void SetUpOnMainThread() override {
3467 PrerenderBrowserTest::SetUpOnMainThread();
3471 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, WebNavigation) {
3472 ASSERT_TRUE(StartSpawnedTestServer());
3473 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3475 // Wait for the extension to set itself up and return control to us.
3476 ASSERT_TRUE(RunExtensionTest("webnavigation/prerender")) << message_;
3478 extensions::ResultCatcher catcher;
3480 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3482 ChannelDestructionWatcher channel_close_watcher;
3483 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
3484 GetActiveWebContents()->GetRenderProcessHost());
3485 NavigateToDestURL();
3486 channel_close_watcher.WaitForChannelClose();
3488 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3489 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
3492 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, TabsApi) {
3493 ASSERT_TRUE(StartSpawnedTestServer());
3494 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3496 // Wait for the extension to set itself up and return control to us.
3497 ASSERT_TRUE(RunExtensionTest("tabs/on_replaced")) << message_;
3499 extensions::ResultCatcher catcher;
3501 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3503 ChannelDestructionWatcher channel_close_watcher;
3504 channel_close_watcher.WatchChannel(browser()->tab_strip_model()->
3505 GetActiveWebContents()->GetRenderProcessHost());
3506 NavigateToDestURL();
3507 channel_close_watcher.WaitForChannelClose();
3509 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3510 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
3513 // Test that prerenders abort when navigating to a stream.
3514 // See chrome/browser/extensions/api/streams_private/streams_private_apitest.cc
3515 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions, StreamsTest) {
3516 RestorePrerenderMode restore_prerender_mode;
3517 PrerenderManager::SetMode(
3518 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3520 ASSERT_TRUE(StartSpawnedTestServer());
3522 const extensions::Extension* extension = LoadExtension(
3523 test_data_dir_.AppendASCII("streams_private/handle_mime_type"));
3524 ASSERT_TRUE(extension);
3525 EXPECT_EQ(std::string(extension_misc::kMimeHandlerPrivateTestExtensionId),
3526 extension->id());
3527 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
3528 ASSERT_TRUE(handler);
3529 EXPECT_TRUE(handler->CanHandleMIMEType("application/msword"));
3531 PrerenderTestURL("files/prerender/document.doc", FINAL_STATUS_DOWNLOAD, 0);
3533 // Sanity-check that the extension would have picked up the stream in a normal
3534 // navigation had prerender not intercepted it.
3535 // streams_private/handle_mime_type reports success if it has handled the
3536 // application/msword type.
3537 extensions::ResultCatcher catcher;
3538 NavigateToDestURL();
3539 EXPECT_TRUE(catcher.GetNextResult());
3542 // Checks that non-http/https/chrome-extension subresource cancels the
3543 // prerender.
3544 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3545 PrerenderCancelSubresourceUnsupportedScheme) {
3546 GURL image_url = GURL("invalidscheme://www.google.com/test.jpg");
3547 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3548 replacement_text.push_back(
3549 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3550 std::string replacement_path;
3551 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3552 "files/prerender/prerender_with_image.html",
3553 replacement_text,
3554 &replacement_path));
3555 PrerenderTestURL(replacement_path, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3558 // Ensure that about:blank is permitted for any subresource.
3559 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3560 PrerenderAllowAboutBlankSubresource) {
3561 GURL image_url = GURL("about:blank");
3562 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3563 replacement_text.push_back(
3564 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3565 std::string replacement_path;
3566 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3567 "files/prerender/prerender_with_image.html",
3568 replacement_text,
3569 &replacement_path));
3570 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3571 NavigateToDestURL();
3574 // Checks that non-http/https/chrome-extension subresource cancels the prerender
3575 // on redirect.
3576 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3577 PrerenderCancelSubresourceRedirectUnsupportedScheme) {
3578 GURL image_url = test_server()->GetURL(
3579 CreateServerRedirect("invalidscheme://www.google.com/test.jpg"));
3580 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3581 replacement_text.push_back(
3582 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3583 std::string replacement_path;
3584 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3585 "files/prerender/prerender_with_image.html",
3586 replacement_text,
3587 &replacement_path));
3588 PrerenderTestURL(replacement_path, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3591 // Checks that chrome-extension subresource does not cancel the prerender.
3592 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3593 PrerenderKeepSubresourceExtensionScheme) {
3594 GURL image_url = GURL("chrome-extension://abcdefg/test.jpg");
3595 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3596 replacement_text.push_back(
3597 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3598 std::string replacement_path;
3599 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3600 "files/prerender/prerender_with_image.html",
3601 replacement_text,
3602 &replacement_path));
3603 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3604 NavigateToDestURL();
3607 // Checks that redirect to chrome-extension subresource does not cancel the
3608 // prerender.
3609 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3610 PrerenderKeepSubresourceRedirectExtensionScheme) {
3611 GURL image_url = test_server()->GetURL(
3612 CreateServerRedirect("chrome-extension://abcdefg/test.jpg"));
3613 std::vector<net::SpawnedTestServer::StringPair> replacement_text;
3614 replacement_text.push_back(
3615 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
3616 std::string replacement_path;
3617 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3618 "files/prerender/prerender_with_image.html",
3619 replacement_text,
3620 &replacement_path));
3621 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
3622 NavigateToDestURL();
3625 // Checks that non-http/https main page redirects cancel the prerender.
3626 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3627 PrerenderCancelMainFrameRedirectUnsupportedScheme) {
3628 GURL url = test_server()->GetURL(
3629 CreateServerRedirect("invalidscheme://www.google.com/test.html"));
3630 PrerenderTestURL(url, FINAL_STATUS_UNSUPPORTED_SCHEME, 0);
3633 // Checks that media source video loads are deferred on prerendering.
3634 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5MediaSourceVideo) {
3635 PrerenderTestURL("files/prerender/prerender_html5_video_media_source.html",
3636 FINAL_STATUS_USED,
3638 NavigateToDestURL();
3639 WaitForASCIITitle(GetActiveWebContents(), kPassTitle);
3642 // Checks that a prerender that creates an audio stream (via a WebAudioDevice)
3643 // is cancelled.
3644 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWebAudioDevice) {
3645 DisableLoadEventCheck();
3646 PrerenderTestURL("files/prerender/prerender_web_audio_device.html",
3647 FINAL_STATUS_CREATING_AUDIO_STREAM, 0);
3650 // Checks that prerenders do not swap in to WebContents being captured.
3651 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCapturedWebContents) {
3652 PrerenderTestURL("files/prerender/prerender_page.html",
3653 FINAL_STATUS_PAGE_BEING_CAPTURED, 1);
3654 WebContents* web_contents = GetActiveWebContents();
3655 web_contents->IncrementCapturerCount(gfx::Size());
3656 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3657 web_contents->DecrementCapturerCount();
3660 // Checks that prerenders are aborted on cross-process navigation from
3661 // a server redirect.
3662 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3663 PrerenderCrossProcessServerRedirect) {
3664 // Force everything to be a process swap.
3665 SwapProcessesContentBrowserClient test_browser_client;
3666 content::ContentBrowserClient* original_browser_client =
3667 content::SetBrowserClientForTesting(&test_browser_client);
3669 PrerenderTestURL(
3670 CreateServerRedirect("files/prerender/prerender_page.html"),
3671 FINAL_STATUS_OPEN_URL, 0);
3673 content::SetBrowserClientForTesting(original_browser_client);
3676 // Checks that URLRequests for prerenders being aborted on cross-process
3677 // navigation from a server redirect are cleaned up, so they don't keep cache
3678 // entries locked.
3679 // See http://crbug.com/341134
3680 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3681 PrerenderCrossProcessServerRedirectNoHang) {
3682 const char kDestPath[] = "files/prerender/prerender_page.html";
3683 // Force everything to be a process swap.
3684 SwapProcessesContentBrowserClient test_browser_client;
3685 content::ContentBrowserClient* original_browser_client =
3686 content::SetBrowserClientForTesting(&test_browser_client);
3688 PrerenderTestURL(CreateServerRedirect(kDestPath), FINAL_STATUS_OPEN_URL, 0);
3690 ui_test_utils::NavigateToURL(
3691 browser(),
3692 test_server()->GetURL(kDestPath));
3694 content::SetBrowserClientForTesting(original_browser_client);
3697 // Checks that prerenders are aborted on cross-process navigation from
3698 // a client redirect.
3699 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3700 PrerenderCrossProcessClientRedirect) {
3701 // Cross-process navigation logic for renderer-initiated navigations
3702 // is partially controlled by the renderer, namely
3703 // ChromeContentRendererClient. This test instead relies on the Web
3704 // Store triggering such navigations.
3705 std::string webstore_url = extension_urls::GetWebstoreLaunchURL();
3707 // Mock out requests to the Web Store.
3708 base::FilePath file(GetTestPath("prerender_page.html"));
3709 BrowserThread::PostTask(
3710 BrowserThread::IO, FROM_HERE,
3711 base::Bind(&CreateMockInterceptorOnIO, GURL(webstore_url), file));
3713 PrerenderTestURL(CreateClientRedirect(webstore_url),
3714 FINAL_STATUS_OPEN_URL, 1);
3717 // Checks that canceling a MatchComplete dummy doesn't result in two
3718 // stop events.
3719 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, CancelMatchCompleteDummy) {
3720 RestorePrerenderMode restore_prerender_mode;
3721 PrerenderManager::SetMode(
3722 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3724 std::vector<FinalStatus> expected_final_status_queue;
3725 expected_final_status_queue.push_back(FINAL_STATUS_JAVASCRIPT_ALERT);
3726 expected_final_status_queue.push_back(FINAL_STATUS_CANCELLED);
3727 ScopedVector<TestPrerender> prerenders =
3728 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
3729 expected_final_status_queue, 0);
3731 // Cancel the MatchComplete dummy.
3732 GetPrerenderManager()->CancelAllPrerenders();
3733 prerenders[1]->WaitForStop();
3735 // Check the referring page only got one copy of the event.
3736 EXPECT_FALSE(HadPrerenderEventErrors());
3739 // Checks that a deferred redirect to an image is not loaded until the page is
3740 // visible. Also test the right histogram events are emitted in this case.
3741 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredImage) {
3742 DisableJavascriptCalls();
3744 // The prerender will not completely load until after the swap, so wait for a
3745 // title change before calling DidPrerenderPass.
3746 scoped_ptr<TestPrerender> prerender =
3747 PrerenderTestURL(
3748 "files/prerender/prerender_deferred_image.html",
3749 FINAL_STATUS_USED, 0);
3750 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
3751 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
3752 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
3753 EXPECT_EQ(0, prerender->number_of_loads());
3754 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3755 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3756 histogram_tester().ExpectTotalCount(
3757 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3758 histogram_tester().ExpectTotalCount(
3759 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3761 // Swap.
3762 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
3763 GetActiveWebContents());
3764 ui_test_utils::NavigateToURLWithDisposition(
3765 current_browser(), dest_url(), CURRENT_TAB,
3766 ui_test_utils::BROWSER_TEST_NONE);
3767 swap_observer.Wait();
3769 // The prerender never observes the final load.
3770 EXPECT_EQ(0, prerender->number_of_loads());
3772 // Now check DidDisplayPass.
3773 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3775 histogram_tester().ExpectTotalCount(
3776 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3777 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3778 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3780 histogram_tester().ExpectTotalCount(
3781 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3784 // Checks that a deferred redirect to an image is not loaded until the
3785 // page is visible, even after another redirect.
3786 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3787 PrerenderDeferredImageAfterRedirect) {
3788 DisableJavascriptCalls();
3790 // The prerender will not completely load until after the swap, so wait for a
3791 // title change before calling DidPrerenderPass.
3792 scoped_ptr<TestPrerender> prerender =
3793 PrerenderTestURL(
3794 "files/prerender/prerender_deferred_image.html",
3795 FINAL_STATUS_USED, 0);
3796 WaitForASCIITitle(prerender->contents()->prerender_contents(), kReadyTitle);
3797 EXPECT_TRUE(DidPrerenderPass(prerender->contents()->prerender_contents()));
3798 EXPECT_EQ(0, prerender->number_of_loads());
3800 // Swap.
3801 NavigationOrSwapObserver swap_observer(current_browser()->tab_strip_model(),
3802 GetActiveWebContents());
3803 ui_test_utils::NavigateToURLWithDisposition(
3804 current_browser(), dest_url(), CURRENT_TAB,
3805 ui_test_utils::BROWSER_TEST_NONE);
3806 swap_observer.Wait();
3808 // The prerender never observes the final load.
3809 EXPECT_EQ(0, prerender->number_of_loads());
3811 // Now check DidDisplayPass.
3812 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3815 // Checks that deferred redirects in the main frame are followed.
3816 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredMainFrame) {
3817 DisableJavascriptCalls();
3818 PrerenderTestURL(
3819 "files/prerender/image-deferred.png",
3820 FINAL_STATUS_USED, 1);
3821 NavigateToDestURL();
3824 // Checks that deferred redirects in the main frame are followed, even
3825 // with a double-redirect.
3826 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3827 PrerenderDeferredMainFrameAfterRedirect) {
3828 DisableJavascriptCalls();
3829 PrerenderTestURL(
3830 CreateServerRedirect("files/prerender/image-deferred.png"),
3831 FINAL_STATUS_USED, 1);
3832 NavigateToDestURL();
3835 // Checks that deferred redirects in a synchronous XHR abort the
3836 // prerender.
3837 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDeferredSynchronousXHR) {
3838 RestorePrerenderMode restore_prerender_mode;
3839 PrerenderManager::SetMode(
3840 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP);
3841 PrerenderTestURL("files/prerender/prerender_deferred_sync_xhr.html",
3842 FINAL_STATUS_BAD_DEFERRED_REDIRECT, 0);
3843 NavigateToDestURL();
3846 // Checks that prerenders are not swapped for navigations with extra headers.
3847 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExtraHeadersNoSwap) {
3848 PrerenderTestURL("files/prerender/prerender_page.html",
3849 FINAL_STATUS_APP_TERMINATING, 1);
3851 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3852 ui::PAGE_TRANSITION_TYPED, false);
3853 params.extra_headers = "X-Custom-Header: 42\r\n";
3854 NavigateToURLWithParams(params, false);
3857 // Checks that prerenders are not swapped for navigations with browser-initiated
3858 // POST data.
3859 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3860 PrerenderBrowserInitiatedPostNoSwap) {
3861 PrerenderTestURL("files/prerender/prerender_page.html",
3862 FINAL_STATUS_APP_TERMINATING, 1);
3864 std::string post_data = "DATA";
3865 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3866 ui::PAGE_TRANSITION_TYPED, false);
3867 params.uses_post = true;
3868 params.browser_initiated_post_data =
3869 base::RefCountedString::TakeString(&post_data);
3870 NavigateToURLWithParams(params, false);
3873 // Checks that the prerendering of a page is canceled correctly when the
3874 // prerendered page tries to make a second navigation entry.
3875 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNewNavigationEntry) {
3876 PrerenderTestURL("files/prerender/prerender_new_entry.html",
3877 FINAL_STATUS_NEW_NAVIGATION_ENTRY,
3881 // Attempt a swap-in in a new tab. The session storage doesn't match, so it
3882 // should not swap.
3883 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageNewTab) {
3884 PrerenderTestURL("files/prerender/prerender_page.html",
3885 FINAL_STATUS_APP_TERMINATING, 1);
3887 // Open a new tab to navigate in.
3888 ui_test_utils::NavigateToURLWithDisposition(
3889 current_browser(), GURL(url::kAboutBlankURL), NEW_FOREGROUND_TAB,
3890 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
3892 // Now navigate in the new tab.
3893 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3896 // Checks that prerenders honor |should_replace_current_entry|.
3897 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReplaceCurrentEntry) {
3898 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3900 content::OpenURLParams params(dest_url(), Referrer(), CURRENT_TAB,
3901 ui::PAGE_TRANSITION_TYPED, false);
3902 params.should_replace_current_entry = true;
3903 NavigateToURLWithParams(params, false);
3905 const NavigationController& controller =
3906 GetActiveWebContents()->GetController();
3907 // First entry is about:blank, second is prerender_page.html.
3908 EXPECT_TRUE(controller.GetPendingEntry() == NULL);
3909 EXPECT_EQ(2, controller.GetEntryCount());
3910 EXPECT_EQ(GURL(url::kAboutBlankURL), controller.GetEntryAtIndex(0)->GetURL());
3911 EXPECT_EQ(dest_url(), controller.GetEntryAtIndex(1)->GetURL());
3914 // Checks that <a ping> requests are not dropped in prerender.
3915 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPing) {
3916 // Count hits to a certain URL.
3917 const GURL kPingURL("http://prerender.test/ping");
3918 base::FilePath empty_file = ui_test_utils::GetTestFilePath(
3919 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3920 RequestCounter ping_counter;
3921 BrowserThread::PostTask(
3922 BrowserThread::IO, FROM_HERE,
3923 base::Bind(&CreateCountingInterceptorOnIO,
3924 kPingURL, empty_file, ping_counter.AsWeakPtr()));
3926 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3927 OpenDestURLViaClickPing(kPingURL);
3929 ping_counter.WaitForCount(1);
3932 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPPLTNormalNavigation) {
3933 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
3934 ui_test_utils::NavigateToURL(current_browser(), url);
3935 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3936 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3937 histogram_tester().ExpectTotalCount(
3938 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3941 // Checks that a prerender which calls window.close() on itself is aborted.
3942 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWindowClose) {
3943 DisableLoadEventCheck();
3944 PrerenderTestURL("files/prerender/prerender_window_close.html",
3945 FINAL_STATUS_CLOSED, 0);
3948 class PrerenderIncognitoBrowserTest : public PrerenderBrowserTest {
3949 public:
3950 void SetUpOnMainThread() override {
3951 Profile* normal_profile = current_browser()->profile();
3952 set_browser(OpenURLOffTheRecord(normal_profile, GURL("about:blank")));
3953 PrerenderBrowserTest::SetUpOnMainThread();
3957 // Checks that prerendering works in incognito mode.
3958 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest, PrerenderIncognito) {
3959 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
3960 NavigateToDestURL();
3963 // Checks that prerenders are aborted when an incognito profile is closed.
3964 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest,
3965 PrerenderIncognitoClosed) {
3966 scoped_ptr<TestPrerender> prerender =
3967 PrerenderTestURL("files/prerender/prerender_page.html",
3968 FINAL_STATUS_PROFILE_DESTROYED, 1);
3969 current_browser()->window()->Close();
3970 prerender->WaitForStop();
3973 class PrerenderOmniboxBrowserTest : public PrerenderBrowserTest {
3974 public:
3975 LocationBar* GetLocationBar() {
3976 return current_browser()->window()->GetLocationBar();
3979 OmniboxView* GetOmniboxView() {
3980 return GetLocationBar()->GetOmniboxView();
3983 void WaitForAutocompleteDone(OmniboxView* omnibox_view) {
3984 AutocompleteController* controller =
3985 omnibox_view->model()->popup_model()->autocomplete_controller();
3986 while (!controller->done()) {
3987 content::WindowedNotificationObserver ready_observer(
3988 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
3989 content::Source<AutocompleteController>(controller));
3990 ready_observer.Wait();
3994 predictors::AutocompleteActionPredictor* GetAutocompleteActionPredictor() {
3995 Profile* profile = current_browser()->profile();
3996 return predictors::AutocompleteActionPredictorFactory::GetForProfile(
3997 profile);
4000 scoped_ptr<TestPrerender> StartOmniboxPrerender(
4001 const GURL& url,
4002 FinalStatus expected_final_status) {
4003 scoped_ptr<TestPrerender> prerender =
4004 ExpectPrerender(expected_final_status);
4005 WebContents* web_contents = GetActiveWebContents();
4006 GetAutocompleteActionPredictor()->StartPrerendering(
4007 url,
4008 web_contents->GetController().GetDefaultSessionStorageNamespace(),
4009 gfx::Size(50, 50));
4010 prerender->WaitForStart();
4011 return prerender.Pass();
4015 // Checks that closing the omnibox popup cancels an omnibox prerender.
4016 // http://crbug.com/395152
4017 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest,
4018 DISABLED_PrerenderOmniboxCancel) {
4019 // Fake an omnibox prerender.
4020 scoped_ptr<TestPrerender> prerender = StartOmniboxPrerender(
4021 test_server()->GetURL("files/empty.html"),
4022 FINAL_STATUS_CANCELLED);
4024 // Revert the location bar. This should cancel the prerender.
4025 GetLocationBar()->Revert();
4026 prerender->WaitForStop();
4029 // Checks that accepting omnibox input abandons an omnibox prerender.
4030 // http://crbug.com/394592
4031 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest,
4032 DISABLED_PrerenderOmniboxAbandon) {
4033 // Set the abandon timeout to something high so it does not introduce
4034 // flakiness if the prerender times out before the test completes.
4035 GetPrerenderManager()->mutable_config().abandon_time_to_live =
4036 base::TimeDelta::FromDays(999);
4038 // Enter a URL into the Omnibox.
4039 OmniboxView* omnibox_view = GetOmniboxView();
4040 omnibox_view->OnBeforePossibleChange();
4041 omnibox_view->SetUserText(
4042 base::UTF8ToUTF16(test_server()->GetURL("files/empty.html?1").spec()));
4043 omnibox_view->OnAfterPossibleChange();
4044 WaitForAutocompleteDone(omnibox_view);
4046 // Fake an omnibox prerender for a different URL.
4047 scoped_ptr<TestPrerender> prerender = StartOmniboxPrerender(
4048 test_server()->GetURL("files/empty.html?2"),
4049 FINAL_STATUS_APP_TERMINATING);
4051 // The final status may be either FINAL_STATUS_APP_TERMINATING or
4052 // FINAL_STATUS_CANCELLED. Although closing the omnibox will not cancel an
4053 // abandoned prerender, the AutocompleteActionPredictor will cancel the
4054 // predictor on destruction.
4055 prerender->contents()->set_skip_final_checks(true);
4057 // Navigate to the URL entered.
4058 omnibox_view->model()->AcceptInput(CURRENT_TAB, false);
4060 // Prerender should be running, but abandoned.
4061 EXPECT_TRUE(
4062 GetAutocompleteActionPredictor()->IsPrerenderAbandonedForTesting());
4065 // Can't run tests with NaCl plugins if built with DISABLE_NACL.
4066 #if !defined(DISABLE_NACL) && !defined(DISABLE_NACL_BROWSERTESTS)
4067 class PrerenderBrowserTestWithNaCl : public PrerenderBrowserTest {
4068 public:
4069 PrerenderBrowserTestWithNaCl() {}
4070 ~PrerenderBrowserTestWithNaCl() override {}
4072 void SetUpCommandLine(base::CommandLine* command_line) override {
4073 PrerenderBrowserTest::SetUpCommandLine(command_line);
4074 command_line->AppendSwitch(switches::kEnableNaCl);
4078 // Check that NaCl plugins work when enabled, with prerendering.
4079 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithNaCl,
4080 PrerenderNaClPluginEnabled) {
4081 #if defined(OS_WIN) && defined(USE_ASH)
4082 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
4083 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
4084 switches::kAshBrowserTests))
4085 return;
4086 #endif
4088 PrerenderTestURL("files/prerender/prerender_plugin_nacl_enabled.html",
4089 FINAL_STATUS_USED,
4091 NavigateToDestURL();
4093 // To avoid any chance of a race, we have to let the script send its response
4094 // asynchronously.
4095 WebContents* web_contents =
4096 browser()->tab_strip_model()->GetActiveWebContents();
4097 bool display_test_result = false;
4098 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents,
4099 "DidDisplayReallyPass()",
4100 &display_test_result));
4101 ASSERT_TRUE(display_test_result);
4103 #endif // !defined(DISABLE_NACL)
4105 #if defined(ENABLE_TASK_MANAGER)
4107 namespace {
4109 base::string16 GetPrerenderTitlePrefix() {
4110 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
4111 base::string16());
4114 const std::vector<task_management::WebContentsTag*>& GetTrackedTags() {
4115 return task_management::WebContentsTagsManager::GetInstance()->
4116 tracked_tags();
4119 // Tests the correct recording of tags for the prerender WebContents.
4120 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, TaskManagementTagsBasic) {
4121 // Browser tests start with a single tab.
4122 EXPECT_EQ(1U, GetTrackedTags().size());
4124 // Start prerendering a page and make sure it's correctly tagged.
4125 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
4126 EXPECT_EQ(2U, GetTrackedTags().size());
4128 // Swap in the prerendered content and make sure its tag is removed.
4129 NavigateToDestURL();
4130 EXPECT_EQ(1U, GetTrackedTags().size());
4133 // Tests that the task manager will be provided by tasks that correspond to
4134 // prerendered WebContents.
4135 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, TaskManagementTasksProvided) {
4136 task_management::MockWebContentsTaskManager task_manager;
4137 // Browser tests start with a single tab.
4138 EXPECT_EQ(1U, GetTrackedTags().size());
4140 task_manager.StartObserving();
4142 // The pre-existing tab is provided.
4143 EXPECT_EQ(1U, task_manager.tasks().size());
4145 // Start prerendering a page.
4146 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
4148 EXPECT_EQ(2U, GetTrackedTags().size());
4149 ASSERT_EQ(2U, task_manager.tasks().size());
4151 const task_management::Task* task = task_manager.tasks().back();
4152 EXPECT_EQ(task_management::Task::RENDERER, task->GetType());
4153 const base::string16 title = task->title();
4154 const base::string16 expected_prefix = GetPrerenderTitlePrefix();
4155 EXPECT_TRUE(base::StartsWith(title,
4156 expected_prefix,
4157 base::CompareCase::INSENSITIVE_ASCII));
4159 NavigateToDestURL();
4160 EXPECT_EQ(1U, task_manager.tasks().size());
4163 } // namespace
4165 #endif // defined(ENABLE_TASK_MANAGER)
4167 } // namespace prerender