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