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.
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/run_loop.h"
18 #include "base/scoped_observer.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/test/histogram_tester.h"
23 #include "base/test/test_timeouts.h"
24 #include "base/values.h"
25 #include "chrome/browser/browsing_data/browsing_data_helper.h"
26 #include "chrome/browser/browsing_data/browsing_data_remover.h"
27 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
28 #include "chrome/browser/chrome_content_browser_client.h"
29 #include "chrome/browser/chrome_notification_types.h"
30 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
31 #include "chrome/browser/extensions/extension_apitest.h"
32 #include "chrome/browser/external_protocol/external_protocol_handler.h"
33 #include "chrome/browser/net/prediction_options.h"
34 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
35 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
36 #include "chrome/browser/prerender/prerender_contents.h"
37 #include "chrome/browser/prerender/prerender_field_trial.h"
38 #include "chrome/browser/prerender/prerender_handle.h"
39 #include "chrome/browser/prerender/prerender_link_manager.h"
40 #include "chrome/browser/prerender/prerender_link_manager_factory.h"
41 #include "chrome/browser/prerender/prerender_manager.h"
42 #include "chrome/browser/prerender/prerender_manager_factory.h"
43 #include "chrome/browser/profiles/profile.h"
44 #include "chrome/browser/profiles/profile_io_data.h"
45 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
46 #include "chrome/browser/safe_browsing/database_manager.h"
47 #include "chrome/browser/safe_browsing/local_database_manager.h"
48 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
49 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
50 #include "chrome/browser/safe_browsing/test_database_manager.h"
51 #include "chrome/browser/task_manager/task_manager.h"
52 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
53 #include "chrome/browser/ui/browser.h"
54 #include "chrome/browser/ui/browser_commands.h"
55 #include "chrome/browser/ui/browser_finder.h"
56 #include "chrome/browser/ui/browser_navigator.h"
57 #include "chrome/browser/ui/browser_window.h"
58 #include "chrome/browser/ui/location_bar/location_bar.h"
59 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
60 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
61 #include "chrome/browser/ui/omnibox/omnibox_view.h"
62 #include "chrome/browser/ui/tabs/tab_strip_model.h"
63 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
64 #include "chrome/common/chrome_paths.h"
65 #include "chrome/common/chrome_switches.h"
66 #include "chrome/common/pref_names.h"
67 #include "chrome/grit/generated_resources.h"
68 #include "chrome/test/base/in_process_browser_test.h"
69 #include "chrome/test/base/test_switches.h"
70 #include "chrome/test/base/ui_test_utils.h"
71 #include "components/content_settings/core/browser/host_content_settings_map.h"
72 #include "components/favicon/content/content_favicon_driver.h"
73 #include "components/favicon/core/favicon_driver_observer.h"
74 #include "components/variations/entropy_provider.h"
75 #include "components/variations/variations_associated_data.h"
76 #include "content/public/browser/browser_message_filter.h"
77 #include "content/public/browser/devtools_agent_host.h"
78 #include "content/public/browser/navigation_controller.h"
79 #include "content/public/browser/navigation_entry.h"
80 #include "content/public/browser/notification_service.h"
81 #include "content/public/browser/render_frame_host.h"
82 #include "content/public/browser/render_process_host.h"
83 #include "content/public/browser/render_view_host.h"
84 #include "content/public/browser/site_instance.h"
85 #include "content/public/browser/web_contents.h"
86 #include "content/public/browser/web_contents_observer.h"
87 #include "content/public/common/url_constants.h"
88 #include "content/public/test/browser_test_utils.h"
89 #include "content/public/test/ppapi_test_utils.h"
90 #include "content/public/test/test_navigation_observer.h"
91 #include "content/public/test/test_utils.h"
92 #include "extensions/common/constants.h"
93 #include "extensions/common/extension_urls.h"
94 #include "extensions/common/manifest_handlers/mime_types_handler.h"
95 #include "extensions/common/switches.h"
96 #include "extensions/test/result_catcher.h"
97 #include "net/base/escape.h"
98 #include "net/cert/x509_certificate.h"
99 #include "net/dns/mock_host_resolver.h"
100 #include "net/ssl/client_cert_store.h"
101 #include "net/ssl/ssl_cert_request_info.h"
102 #include "net/test/url_request/url_request_mock_http_job.h"
103 #include "net/url_request/url_request_context.h"
104 #include "net/url_request/url_request_context_getter.h"
105 #include "net/url_request/url_request_filter.h"
106 #include "net/url_request/url_request_interceptor.h"
107 #include "net/url_request/url_request_job.h"
108 #include "ppapi/shared_impl/ppapi_switches.h"
109 #include "ui/base/l10n/l10n_util.h"
110 #include "url/gurl.h"
112 using chrome_browser_net::NetworkPredictionOptions
;
113 using content::BrowserThread
;
114 using content::DevToolsAgentHost
;
115 using content::NavigationController
;
116 using content::OpenURLParams
;
117 using content::Referrer
;
118 using content::RenderFrameHost
;
119 using content::RenderViewHost
;
120 using content::RenderWidgetHost
;
121 using content::TestNavigationObserver
;
122 using content::WebContents
;
123 using content::WebContentsObserver
;
124 using net::NetworkChangeNotifier
;
125 using task_manager::browsertest_util::WaitForTaskManagerRows
;
127 // Prerender tests work as follows:
129 // A page with a prefetch link to the test page is loaded. Once prerendered,
130 // its Javascript function DidPrerenderPass() is called, which returns true if
131 // the page behaves as expected when prerendered.
133 // The prerendered page is then displayed on a tab. The Javascript function
134 // DidDisplayPass() is called, and returns true if the page behaved as it
135 // should while being displayed.
137 namespace prerender
{
141 class FaviconUpdateWatcher
: public favicon::FaviconDriverObserver
{
143 explicit FaviconUpdateWatcher(content::WebContents
* web_contents
)
144 : seen_(false), running_(false), scoped_observer_(this) {
145 scoped_observer_
.Add(
146 favicon::ContentFaviconDriver::FromWebContents(web_contents
));
154 message_loop_runner_
= new content::MessageLoopRunner
;
155 message_loop_runner_
->Run();
159 void OnFaviconAvailable(const gfx::Image
& image
) override
{}
160 void OnFaviconUpdated(favicon::FaviconDriver
* favicon_driver
,
161 bool icon_url_changed
) override
{
166 message_loop_runner_
->Quit();
172 ScopedObserver
<favicon::FaviconDriver
, FaviconUpdateWatcher
> scoped_observer_
;
173 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
175 DISALLOW_COPY_AND_ASSIGN(FaviconUpdateWatcher
);
178 class MockNetworkChangeNotifierWIFI
: public NetworkChangeNotifier
{
180 ConnectionType
GetCurrentConnectionType() const override
{
181 return NetworkChangeNotifier::CONNECTION_WIFI
;
185 class MockNetworkChangeNotifier4G
: public NetworkChangeNotifier
{
187 ConnectionType
GetCurrentConnectionType() const override
{
188 return NetworkChangeNotifier::CONNECTION_4G
;
192 // Constants used in the test HTML files.
193 const char* kReadyTitle
= "READY";
194 const char* kPassTitle
= "PASS";
196 std::string
CreateClientRedirect(const std::string
& dest_url
) {
197 const char* const kClientRedirectBase
= "client-redirect?";
198 return kClientRedirectBase
+ net::EscapeQueryParamValue(dest_url
, false);
201 std::string
CreateServerRedirect(const std::string
& dest_url
) {
202 const char* const kServerRedirectBase
= "server-redirect?";
203 return kServerRedirectBase
+ net::EscapeQueryParamValue(dest_url
, false);
206 // Clears the specified data using BrowsingDataRemover.
207 void ClearBrowsingData(Browser
* browser
, int remove_mask
) {
208 BrowsingDataRemover
* remover
=
209 BrowsingDataRemover::CreateForUnboundedRange(browser
->profile());
210 BrowsingDataRemoverCompletionObserver
observer(remover
);
211 remover
->Remove(remove_mask
, BrowsingDataHelper::UNPROTECTED_WEB
);
212 observer
.BlockUntilCompletion();
213 // BrowsingDataRemover deletes itself.
216 // Returns true if the prerender is expected to abort on its own, before
217 // attempting to swap it.
218 bool ShouldAbortPrerenderBeforeSwap(FinalStatus status
) {
220 case FINAL_STATUS_USED
:
221 case FINAL_STATUS_WINDOW_OPENER
:
222 case FINAL_STATUS_APP_TERMINATING
:
223 case FINAL_STATUS_PROFILE_DESTROYED
:
224 case FINAL_STATUS_CACHE_OR_HISTORY_CLEARED
:
225 // We'll crash the renderer after it's loaded.
226 case FINAL_STATUS_RENDERER_CRASHED
:
227 case FINAL_STATUS_CANCELLED
:
228 case FINAL_STATUS_DEVTOOLS_ATTACHED
:
229 case FINAL_STATUS_PAGE_BEING_CAPTURED
:
230 case FINAL_STATUS_NAVIGATION_UNCOMMITTED
:
231 case FINAL_STATUS_WOULD_HAVE_BEEN_USED
:
232 case FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE
:
239 // Convenience function to wait for a title. Handles the case when the
240 // WebContents already has the expected title.
241 void WaitForASCIITitle(WebContents
* web_contents
,
242 const char* expected_title_ascii
) {
243 base::string16 expected_title
= base::ASCIIToUTF16(expected_title_ascii
);
244 if (web_contents
->GetTitle() == expected_title
)
246 content::TitleWatcher
title_watcher(web_contents
, expected_title
);
247 EXPECT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
250 // Waits for the destruction of a RenderProcessHost's IPC channel.
251 // Used to make sure the PrerenderLinkManager's OnChannelClosed function has
252 // been called, before checking its state.
253 class ChannelDestructionWatcher
{
255 ChannelDestructionWatcher() : channel_destroyed_(false) {
258 ~ChannelDestructionWatcher() {
261 void WatchChannel(content::RenderProcessHost
* host
) {
262 host
->AddFilter(new DestructionMessageFilter(this));
265 void WaitForChannelClose() {
267 EXPECT_TRUE(channel_destroyed_
);
271 // When destroyed, calls ChannelDestructionWatcher::OnChannelDestroyed.
272 // Ignores all messages.
273 class DestructionMessageFilter
: public content::BrowserMessageFilter
{
275 explicit DestructionMessageFilter(ChannelDestructionWatcher
* watcher
)
276 : BrowserMessageFilter(0),
281 ~DestructionMessageFilter() override
{
282 content::BrowserThread::PostTask(
283 content::BrowserThread::UI
, FROM_HERE
,
284 base::Bind(&ChannelDestructionWatcher::OnChannelDestroyed
,
285 base::Unretained(watcher_
)));
288 bool OnMessageReceived(const IPC::Message
& message
) override
{
292 ChannelDestructionWatcher
* watcher_
;
294 DISALLOW_COPY_AND_ASSIGN(DestructionMessageFilter
);
297 void OnChannelDestroyed() {
298 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
300 EXPECT_FALSE(channel_destroyed_
);
301 channel_destroyed_
= true;
305 bool channel_destroyed_
;
306 base::RunLoop run_loop_
;
308 DISALLOW_COPY_AND_ASSIGN(ChannelDestructionWatcher
);
311 // A navigation observer to wait on either a new load or a swap of a
312 // WebContents. On swap, if the new WebContents is still loading, wait for that
313 // load to complete as well. Note that the load must begin after the observer is
315 class NavigationOrSwapObserver
: public WebContentsObserver
,
316 public TabStripModelObserver
{
318 // Waits for either a new load or a swap of |tab_strip_model|'s active
320 NavigationOrSwapObserver(TabStripModel
* tab_strip_model
,
321 WebContents
* web_contents
)
322 : WebContentsObserver(web_contents
),
323 tab_strip_model_(tab_strip_model
),
324 did_start_loading_(false),
325 number_of_loads_(1) {
326 CHECK_NE(TabStripModel::kNoTab
,
327 tab_strip_model
->GetIndexOfWebContents(web_contents
));
328 tab_strip_model_
->AddObserver(this);
331 // Waits for either |number_of_loads| loads or a swap of |tab_strip_model|'s
332 // active WebContents.
333 NavigationOrSwapObserver(TabStripModel
* tab_strip_model
,
334 WebContents
* web_contents
,
336 : WebContentsObserver(web_contents
),
337 tab_strip_model_(tab_strip_model
),
338 did_start_loading_(false),
339 number_of_loads_(number_of_loads
) {
340 CHECK_NE(TabStripModel::kNoTab
,
341 tab_strip_model
->GetIndexOfWebContents(web_contents
));
342 tab_strip_model_
->AddObserver(this);
345 ~NavigationOrSwapObserver() override
{
346 tab_strip_model_
->RemoveObserver(this);
349 void set_did_start_loading() {
350 did_start_loading_
= true;
357 // WebContentsObserver implementation:
358 void DidStartLoading() override
{ did_start_loading_
= true; }
359 void DidStopLoading() override
{
360 if (!did_start_loading_
)
363 if (number_of_loads_
== 0)
367 // TabStripModelObserver implementation:
368 void TabReplacedAt(TabStripModel
* tab_strip_model
,
369 WebContents
* old_contents
,
370 WebContents
* new_contents
,
371 int index
) override
{
372 if (old_contents
!= web_contents())
374 // Switch to observing the new WebContents.
375 Observe(new_contents
);
376 if (new_contents
->IsLoading()) {
377 // If the new WebContents is still loading, wait for it to complete. Only
378 // one load post-swap is supported.
379 did_start_loading_
= true;
380 number_of_loads_
= 1;
387 TabStripModel
* tab_strip_model_
;
388 bool did_start_loading_
;
389 int number_of_loads_
;
393 // Waits for a new tab to open and a navigation or swap in it.
394 class NewTabNavigationOrSwapObserver
{
396 NewTabNavigationOrSwapObserver()
398 chrome::NOTIFICATION_TAB_ADDED
,
399 base::Bind(&NewTabNavigationOrSwapObserver::OnTabAdded
,
400 base::Unretained(this))) {
401 // Watch for NOTIFICATION_TAB_ADDED. Add a callback so that the
402 // NavigationOrSwapObserver can be attached synchronously and no events are
407 new_tab_observer_
.Wait();
408 swap_observer_
->Wait();
411 bool OnTabAdded(const content::NotificationSource
& source
,
412 const content::NotificationDetails
& details
) {
415 WebContents
* new_tab
= content::Details
<WebContents
>(details
).ptr();
416 // Get the TabStripModel. Assume this is attached to a Browser.
417 TabStripModel
* tab_strip_model
=
418 static_cast<Browser
*>(new_tab
->GetDelegate())->tab_strip_model();
419 swap_observer_
.reset(new NavigationOrSwapObserver(tab_strip_model
,
421 swap_observer_
->set_did_start_loading();
426 content::WindowedNotificationObserver new_tab_observer_
;
427 scoped_ptr
<NavigationOrSwapObserver
> swap_observer_
;
430 // PrerenderContents that stops the UI message loop on DidStopLoading().
431 class TestPrerenderContents
: public PrerenderContents
{
433 TestPrerenderContents(
434 PrerenderManager
* prerender_manager
,
437 const content::Referrer
& referrer
,
439 FinalStatus expected_final_status
)
440 : PrerenderContents(prerender_manager
, profile
, url
, referrer
, origin
),
441 expected_final_status_(expected_final_status
),
442 new_render_view_host_(NULL
),
445 should_be_shown_(expected_final_status
== FINAL_STATUS_USED
),
446 skip_final_checks_(false) {
449 ~TestPrerenderContents() override
{
450 if (skip_final_checks_
)
453 if (expected_final_status_
== FINAL_STATUS_MAX
) {
454 EXPECT_EQ(MATCH_COMPLETE_REPLACEMENT
, match_complete_status());
456 EXPECT_EQ(expected_final_status_
, final_status()) <<
457 " when testing URL " << prerender_url().path() <<
458 " (Expected: " << NameFromFinalStatus(expected_final_status_
) <<
459 ", Actual: " << NameFromFinalStatus(final_status()) << ")";
461 // Prerendering RenderViewHosts should be hidden before the first
462 // navigation, so this should be happen for every PrerenderContents for
463 // which a RenderViewHost is created, regardless of whether or not it's
465 if (new_render_view_host_
)
466 EXPECT_TRUE(was_hidden_
);
468 // A used PrerenderContents will only be destroyed when we swap out
469 // WebContents, at the end of a navigation caused by a call to
470 // NavigateToURLImpl().
471 if (final_status() == FINAL_STATUS_USED
)
472 EXPECT_TRUE(new_render_view_host_
);
474 EXPECT_EQ(should_be_shown_
, was_shown_
);
477 void RenderProcessGone(base::TerminationStatus status
) override
{
478 // On quit, it's possible to end up here when render processes are closed
479 // before the PrerenderManager is destroyed. As a result, it's possible to
480 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
483 // It's also possible for this to be called after we've been notified of
484 // app termination, but before we've been deleted, which is why the second
486 if (expected_final_status_
== FINAL_STATUS_APP_TERMINATING
&&
487 final_status() != expected_final_status_
) {
488 expected_final_status_
= FINAL_STATUS_RENDERER_CRASHED
;
491 PrerenderContents::RenderProcessGone(status
);
494 bool CheckURL(const GURL
& url
) override
{
495 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
496 // the PrerenderRendererCrash test.
497 if (url
.spec() != content::kChromeUICrashURL
)
498 return PrerenderContents::CheckURL(url
);
502 // For tests that open the prerender in a new background tab, the RenderView
503 // will not have been made visible when the PrerenderContents is destroyed
504 // even though it is used.
505 void set_should_be_shown(bool value
) { should_be_shown_
= value
; }
507 // For tests which do not know whether the prerender will be used.
508 void set_skip_final_checks(bool value
) { skip_final_checks_
= value
; }
510 FinalStatus
expected_final_status() const { return expected_final_status_
; }
513 void OnRenderViewHostCreated(RenderViewHost
* new_render_view_host
) override
{
514 // Used to make sure the RenderViewHost is hidden and, if used,
515 // subsequently shown.
516 notification_registrar().Add(
518 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
519 content::Source
<RenderWidgetHost
>(new_render_view_host
));
521 new_render_view_host_
= new_render_view_host
;
523 PrerenderContents::OnRenderViewHostCreated(new_render_view_host
);
526 void Observe(int type
,
527 const content::NotificationSource
& source
,
528 const content::NotificationDetails
& details
) override
{
530 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
) {
531 EXPECT_EQ(new_render_view_host_
,
532 content::Source
<RenderWidgetHost
>(source
).ptr());
533 bool is_visible
= *content::Details
<bool>(details
).ptr();
537 } else if (is_visible
&& was_hidden_
) {
538 // Once hidden, a prerendered RenderViewHost should only be shown after
539 // being removed from the PrerenderContents for display.
540 EXPECT_FALSE(GetRenderViewHost());
545 PrerenderContents::Observe(type
, source
, details
);
548 FinalStatus expected_final_status_
;
550 // The RenderViewHost created for the prerender, if any.
551 RenderViewHost
* new_render_view_host_
;
552 // Set to true when the prerendering RenderWidget is hidden.
554 // Set to true when the prerendering RenderWidget is shown, after having been
557 // Expected final value of was_shown_. Defaults to true for
558 // FINAL_STATUS_USED, and false otherwise.
559 bool should_be_shown_
;
560 // If true, |expected_final_status_| and other shutdown checks are skipped.
561 bool skip_final_checks_
;
564 // A handle to a TestPrerenderContents whose lifetime is under the caller's
565 // control. A PrerenderContents may be destroyed at any point. This allows
566 // tracking the final status, etc.
567 class TestPrerender
: public PrerenderContents::Observer
,
568 public base::SupportsWeakPtr
<TestPrerender
> {
573 expected_number_of_loads_(0) {
575 ~TestPrerender() override
{
577 contents_
->RemoveObserver(this);
580 TestPrerenderContents
* contents() const { return contents_
; }
581 int number_of_loads() const { return number_of_loads_
; }
583 void WaitForCreate() { create_loop_
.Run(); }
584 void WaitForStart() { start_loop_
.Run(); }
585 void WaitForStop() { stop_loop_
.Run(); }
587 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR
588 // for the prerender to stop running (just to avoid a timeout if the prerender
589 // dies). Note: this does not assert equality on the number of loads; the
590 // caller must do it instead.
591 void WaitForLoads(int expected_number_of_loads
) {
592 DCHECK(!load_waiter_
);
593 DCHECK(!expected_number_of_loads_
);
594 if (number_of_loads_
< expected_number_of_loads
) {
595 load_waiter_
.reset(new base::RunLoop
);
596 expected_number_of_loads_
= expected_number_of_loads
;
598 load_waiter_
.reset();
599 expected_number_of_loads_
= 0;
601 EXPECT_LE(expected_number_of_loads
, number_of_loads_
);
604 void OnPrerenderCreated(TestPrerenderContents
* contents
) {
606 contents_
= contents
;
607 contents_
->AddObserver(this);
611 // PrerenderContents::Observer implementation:
612 void OnPrerenderStart(PrerenderContents
* contents
) override
{
616 void OnPrerenderStopLoading(PrerenderContents
* contents
) override
{
618 if (load_waiter_
&& number_of_loads_
>= expected_number_of_loads_
)
619 load_waiter_
->Quit();
622 void OnPrerenderStop(PrerenderContents
* contents
) override
{
626 // If there is a WaitForLoads call and it has yet to see the expected number
627 // of loads, stop the loop so the test fails instead of timing out.
629 load_waiter_
->Quit();
632 void OnPrerenderCreatedMatchCompleteReplacement(
633 PrerenderContents
* contents
,
634 PrerenderContents
* replacement
) override
{}
637 TestPrerenderContents
* contents_
;
638 int number_of_loads_
;
640 int expected_number_of_loads_
;
641 scoped_ptr
<base::RunLoop
> load_waiter_
;
643 base::RunLoop create_loop_
;
644 base::RunLoop start_loop_
;
645 base::RunLoop stop_loop_
;
647 DISALLOW_COPY_AND_ASSIGN(TestPrerender
);
650 // PrerenderManager that uses TestPrerenderContents.
651 class TestPrerenderContentsFactory
: public PrerenderContents::Factory
{
653 TestPrerenderContentsFactory() {}
655 ~TestPrerenderContentsFactory() override
{
656 EXPECT_TRUE(expected_contents_queue_
.empty());
659 scoped_ptr
<TestPrerender
> ExpectPrerenderContents(FinalStatus final_status
) {
660 scoped_ptr
<TestPrerender
> handle(new TestPrerender());
661 expected_contents_queue_
.push_back(
662 ExpectedContents(final_status
, handle
->AsWeakPtr()));
663 return handle
.Pass();
666 PrerenderContents
* CreatePrerenderContents(
667 PrerenderManager
* prerender_manager
,
670 const content::Referrer
& referrer
,
671 Origin origin
) override
{
672 ExpectedContents expected
;
673 if (!expected_contents_queue_
.empty()) {
674 expected
= expected_contents_queue_
.front();
675 expected_contents_queue_
.pop_front();
677 VLOG(1) << "Creating prerender contents for " << url
.path() <<
678 " with expected final status " << expected
.final_status
;
679 VLOG(1) << expected_contents_queue_
.size() << " left in the queue.";
680 TestPrerenderContents
* contents
=
681 new TestPrerenderContents(prerender_manager
,
682 profile
, url
, referrer
, origin
,
683 expected
.final_status
);
685 expected
.handle
->OnPrerenderCreated(contents
);
690 struct ExpectedContents
{
691 ExpectedContents() : final_status(FINAL_STATUS_MAX
) { }
692 ExpectedContents(FinalStatus final_status
,
693 const base::WeakPtr
<TestPrerender
>& handle
)
694 : final_status(final_status
),
698 FinalStatus final_status
;
699 base::WeakPtr
<TestPrerender
> handle
;
702 std::deque
<ExpectedContents
> expected_contents_queue_
;
705 // TODO(nparker): Switch this to use TestSafeBrowsingDatabaseManager and run
706 // with SAFE_BROWSING_DB_LOCAL || SAFE_BROWSING_DB_REMOTE.
707 #if defined(FULL_SAFE_BROWSING)
708 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
710 class FakeSafeBrowsingDatabaseManager
711 : public LocalSafeBrowsingDatabaseManager
{
713 explicit FakeSafeBrowsingDatabaseManager(SafeBrowsingService
* service
)
714 : LocalSafeBrowsingDatabaseManager(service
),
715 threat_type_(SB_THREAT_TYPE_SAFE
) {}
717 // Called on the IO thread to check if the given url is safe or not. If we
718 // can synchronously determine that the url is safe, CheckUrl returns true.
719 // Otherwise it returns false, and "client" is called asynchronously with the
720 // result when it is ready.
721 // Returns true, indicating a SAFE result, unless the URL is the fixed URL
722 // specified by the user, and the user-specified result is not SAFE
723 // (in which that result will be communicated back via a call into the
724 // client, and false will be returned).
725 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl.
726 bool CheckBrowseUrl(const GURL
& gurl
, Client
* client
) override
{
727 if (gurl
!= url_
|| threat_type_
== SB_THREAT_TYPE_SAFE
)
730 BrowserThread::PostTask(
731 BrowserThread::IO
, FROM_HERE
,
732 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone
,
733 this, gurl
, client
));
737 void SetThreatTypeForUrl(const GURL
& url
, SBThreatType threat_type
) {
739 threat_type_
= threat_type
;
743 ~FakeSafeBrowsingDatabaseManager() override
{}
745 void OnCheckBrowseURLDone(const GURL
& gurl
, Client
* client
) {
746 std::vector
<SBThreatType
> expected_threats
;
747 expected_threats
.push_back(SB_THREAT_TYPE_URL_MALWARE
);
748 expected_threats
.push_back(SB_THREAT_TYPE_URL_PHISHING
);
749 // TODO(nparker): Replace SafeBrowsingCheck w/ a call to
750 // client->OnCheckBrowseUrlResult()
751 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck
sb_check(
752 std::vector
<GURL
>(1, gurl
),
753 std::vector
<SBFullHash
>(),
755 safe_browsing_util::MALWARE
,
757 sb_check
.url_results
[0] = threat_type_
;
758 sb_check
.OnSafeBrowsingResult();
762 SBThreatType threat_type_
;
763 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager
);
766 class FakeSafeBrowsingService
: public SafeBrowsingService
{
768 FakeSafeBrowsingService() { }
770 // Returned pointer has the same lifespan as the database_manager_ refcounted
772 FakeSafeBrowsingDatabaseManager
* fake_database_manager() {
773 return fake_database_manager_
;
777 ~FakeSafeBrowsingService() override
{}
779 SafeBrowsingDatabaseManager
* CreateDatabaseManager() override
{
780 fake_database_manager_
= new FakeSafeBrowsingDatabaseManager(this);
781 return fake_database_manager_
;
785 FakeSafeBrowsingDatabaseManager
* fake_database_manager_
;
787 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService
);
790 // Factory that creates FakeSafeBrowsingService instances.
791 class TestSafeBrowsingServiceFactory
: public SafeBrowsingServiceFactory
{
793 TestSafeBrowsingServiceFactory() :
794 most_recent_service_(NULL
) { }
795 ~TestSafeBrowsingServiceFactory() override
{}
797 SafeBrowsingService
* CreateSafeBrowsingService() override
{
798 most_recent_service_
= new FakeSafeBrowsingService();
799 return most_recent_service_
;
802 FakeSafeBrowsingService
* most_recent_service() const {
803 return most_recent_service_
;
807 FakeSafeBrowsingService
* most_recent_service_
;
811 class FakeDevToolsClient
: public content::DevToolsAgentHostClient
{
813 FakeDevToolsClient() {}
814 ~FakeDevToolsClient() override
{}
815 void DispatchProtocolMessage(DevToolsAgentHost
* agent_host
,
816 const std::string
& message
) override
{}
817 void AgentHostClosed(DevToolsAgentHost
* agent_host
, bool replaced
) override
{}
820 class RestorePrerenderMode
{
822 RestorePrerenderMode() : prev_mode_(PrerenderManager::GetMode()) {
825 ~RestorePrerenderMode() { PrerenderManager::SetMode(prev_mode_
); }
827 PrerenderManager::PrerenderManagerMode prev_mode_
;
830 // URLRequestJob (and associated handler) which hangs.
831 class HangingURLRequestJob
: public net::URLRequestJob
{
833 HangingURLRequestJob(net::URLRequest
* request
,
834 net::NetworkDelegate
* network_delegate
)
835 : net::URLRequestJob(request
, network_delegate
) {
838 void Start() override
{}
841 ~HangingURLRequestJob() override
{}
844 class HangingFirstRequestInterceptor
: public net::URLRequestInterceptor
{
846 HangingFirstRequestInterceptor(const base::FilePath
& file
,
847 base::Closure callback
)
852 ~HangingFirstRequestInterceptor() override
{}
854 net::URLRequestJob
* MaybeInterceptRequest(
855 net::URLRequest
* request
,
856 net::NetworkDelegate
* network_delegate
) const override
{
859 if (!callback_
.is_null()) {
860 BrowserThread::PostTask(
861 BrowserThread::UI
, FROM_HERE
, callback_
);
863 return new HangingURLRequestJob(request
, network_delegate
);
865 return new net::URLRequestMockHTTPJob(
869 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
870 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
874 base::FilePath file_
;
875 base::Closure callback_
;
876 mutable bool first_run_
;
879 // Makes |url| never respond on the first load, and then with the contents of
880 // |file| afterwards. When the first load has been scheduled, runs |callback| on
882 void CreateHangingFirstRequestInterceptorOnIO(
883 const GURL
& url
, const base::FilePath
& file
, base::Closure callback
) {
884 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
885 scoped_ptr
<net::URLRequestInterceptor
> never_respond_handler(
886 new HangingFirstRequestInterceptor(file
, callback
));
887 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
888 url
, never_respond_handler
.Pass());
891 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks.
892 class MockHTTPJob
: public net::URLRequestMockHTTPJob
{
894 MockHTTPJob(net::URLRequest
* request
,
895 net::NetworkDelegate
* delegate
,
896 const base::FilePath
& file
)
897 : net::URLRequestMockHTTPJob(
901 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
902 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
)) {}
904 void set_start_callback(const base::Closure
& start_callback
) {
905 start_callback_
= start_callback
;
908 void Start() override
{
909 if (!start_callback_
.is_null())
910 start_callback_
.Run();
911 net::URLRequestMockHTTPJob::Start();
915 ~MockHTTPJob() override
{}
917 base::Closure start_callback_
;
920 // Dummy counter class to live on the UI thread for counting requests.
921 class RequestCounter
: public base::SupportsWeakPtr
<RequestCounter
> {
923 RequestCounter() : count_(0), expected_count_(-1) {}
924 int count() const { return count_
; }
926 void RequestStarted() {
928 if (loop_
&& count_
== expected_count_
)
932 void WaitForCount(int expected_count
) {
934 ASSERT_EQ(-1, expected_count_
);
935 if (count_
< expected_count
) {
936 expected_count_
= expected_count
;
937 loop_
.reset(new base::RunLoop
);
939 expected_count_
= -1;
943 EXPECT_EQ(expected_count
, count_
);
948 scoped_ptr
<base::RunLoop
> loop_
;
951 // Protocol handler which counts the number of requests that start.
952 class CountingInterceptor
: public net::URLRequestInterceptor
{
954 CountingInterceptor(const base::FilePath
& file
,
955 const base::WeakPtr
<RequestCounter
>& counter
)
958 weak_factory_(this) {
960 ~CountingInterceptor() override
{}
962 net::URLRequestJob
* MaybeInterceptRequest(
963 net::URLRequest
* request
,
964 net::NetworkDelegate
* network_delegate
) const override
{
965 MockHTTPJob
* job
= new MockHTTPJob(request
, network_delegate
, file_
);
966 job
->set_start_callback(base::Bind(&CountingInterceptor::RequestStarted
,
967 weak_factory_
.GetWeakPtr()));
971 void RequestStarted() {
972 BrowserThread::PostTask(
973 BrowserThread::UI
, FROM_HERE
,
974 base::Bind(&RequestCounter::RequestStarted
, counter_
));
978 base::FilePath file_
;
979 base::WeakPtr
<RequestCounter
> counter_
;
980 mutable base::WeakPtrFactory
<CountingInterceptor
> weak_factory_
;
983 // Makes |url| respond to requests with the contents of |file|, counting the
984 // number that start in |counter|.
985 void CreateCountingInterceptorOnIO(
987 const base::FilePath
& file
,
988 const base::WeakPtr
<RequestCounter
>& counter
) {
989 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
990 scoped_ptr
<net::URLRequestInterceptor
> request_interceptor(
991 new CountingInterceptor(file
, counter
));
992 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
993 url
, request_interceptor
.Pass());
996 // Makes |url| respond to requests with the contents of |file|.
997 void CreateMockInterceptorOnIO(const GURL
& url
, const base::FilePath
& file
) {
998 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
999 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
1001 net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
1002 file
, BrowserThread::GetBlockingPool()));
1005 // A ContentBrowserClient that cancels all prerenderers on OpenURL.
1006 class TestContentBrowserClient
: public chrome::ChromeContentBrowserClient
{
1008 TestContentBrowserClient() {}
1009 ~TestContentBrowserClient() override
{}
1011 // chrome::ChromeContentBrowserClient implementation.
1012 bool ShouldAllowOpenURL(content::SiteInstance
* site_instance
,
1013 const GURL
& url
) override
{
1014 PrerenderManagerFactory::GetForProfile(
1015 Profile::FromBrowserContext(site_instance
->GetBrowserContext()))
1016 ->CancelAllPrerenders();
1017 return chrome::ChromeContentBrowserClient::ShouldAllowOpenURL(site_instance
,
1022 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient
);
1025 // A ContentBrowserClient that forces cross-process navigations.
1026 class SwapProcessesContentBrowserClient
1027 : public chrome::ChromeContentBrowserClient
{
1029 SwapProcessesContentBrowserClient() {}
1030 ~SwapProcessesContentBrowserClient() override
{}
1032 // chrome::ChromeContentBrowserClient implementation.
1033 bool ShouldSwapProcessesForRedirect(
1034 content::ResourceContext
* resource_context
,
1035 const GURL
& current_url
,
1036 const GURL
& new_url
) override
{
1041 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient
);
1044 // An ExternalProtocolHandler that blocks everything and asserts it never is
1046 class NeverRunsExternalProtocolHandlerDelegate
1047 : public ExternalProtocolHandler::Delegate
{
1049 // ExternalProtocolHandler::Delegate implementation.
1050 ShellIntegration::DefaultProtocolClientWorker
* CreateShellWorker(
1051 ShellIntegration::DefaultWebClientObserver
* observer
,
1052 const std::string
& protocol
) override
{
1054 // This will crash, but it shouldn't get this far with BlockState::BLOCK
1058 ExternalProtocolHandler::BlockState
GetBlockState(
1059 const std::string
& scheme
) override
{
1060 // Block everything and fail the test.
1062 return ExternalProtocolHandler::BLOCK
;
1064 void BlockRequest() override
{}
1065 void RunExternalProtocolDialog(const GURL
& url
,
1066 int render_process_host_id
,
1068 ui::PageTransition page_transition
,
1069 bool has_user_gesture
) override
{
1072 void LaunchUrlWithoutSecurityCheck(const GURL
& url
) override
{ NOTREACHED(); }
1073 void FinishedProcessingCheck() override
{ NOTREACHED(); }
1076 base::FilePath
GetTestPath(const std::string
& file_name
) {
1077 return ui_test_utils::GetTestFilePath(
1078 base::FilePath(FILE_PATH_LITERAL("prerender")),
1079 base::FilePath().AppendASCII(file_name
));
1084 class PrerenderBrowserTest
: virtual public InProcessBrowserTest
{
1086 PrerenderBrowserTest()
1087 : autostart_test_server_(true),
1088 prerender_contents_factory_(NULL
),
1089 #if defined(FULL_SAFE_BROWSING)
1090 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
1092 call_javascript_(true),
1093 check_load_events_(true),
1094 loader_path_("files/prerender/prerender_loader.html"),
1095 explicitly_set_browser_(NULL
) {}
1097 ~PrerenderBrowserTest() override
{}
1099 content::SessionStorageNamespace
* GetSessionStorageNamespace() const {
1100 WebContents
* web_contents
= GetActiveWebContents();
1103 return web_contents
->GetController().GetDefaultSessionStorageNamespace();
1106 void SetUpInProcessBrowserTestFixture() override
{
1107 #if defined(FULL_SAFE_BROWSING)
1108 SafeBrowsingService::RegisterFactory(safe_browsing_factory_
.get());
1112 void TearDownInProcessBrowserTestFixture() override
{
1113 #if defined(FULL_SAFE_BROWSING)
1114 SafeBrowsingService::RegisterFactory(NULL
);
1118 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
1119 command_line
->AppendSwitchASCII(switches::kPrerenderMode
,
1120 switches::kPrerenderModeSwitchValueEnabled
);
1121 command_line
->AppendSwitch(switches::kEnablePepperTesting
);
1122 command_line
->AppendSwitchASCII(
1123 switches::kOverridePluginPowerSaverForTesting
, "ignore-list");
1125 ASSERT_TRUE(ppapi::RegisterPowerSaverTestPlugin(command_line
));
1128 void SetUpOnMainThread() override
{
1129 current_browser()->profile()->GetPrefs()->SetBoolean(
1130 prefs::kPromptForDownload
, false);
1131 IncreasePrerenderMemory();
1132 if (autostart_test_server_
)
1133 ASSERT_TRUE(test_server()->Start());
1134 ChromeResourceDispatcherHostDelegate::
1135 SetExternalProtocolHandlerDelegateForTesting(
1136 &external_protocol_handler_delegate_
);
1138 PrerenderManager
* prerender_manager
= GetPrerenderManager();
1139 ASSERT_TRUE(prerender_manager
);
1140 prerender_manager
->mutable_config().rate_limit_enabled
= false;
1141 ASSERT_TRUE(prerender_contents_factory_
== NULL
);
1142 prerender_contents_factory_
= new TestPrerenderContentsFactory
;
1143 prerender_manager
->SetPrerenderContentsFactory(prerender_contents_factory_
);
1146 // Convenience function to get the currently active WebContents in
1147 // current_browser().
1148 WebContents
* GetActiveWebContents() const {
1149 return current_browser()->tab_strip_model()->GetActiveWebContents();
1152 // Overload for a single expected final status
1153 scoped_ptr
<TestPrerender
> PrerenderTestURL(
1154 const std::string
& html_file
,
1155 FinalStatus expected_final_status
,
1156 int expected_number_of_loads
) {
1157 GURL url
= test_server()->GetURL(html_file
);
1158 return PrerenderTestURL(url
,
1159 expected_final_status
,
1160 expected_number_of_loads
);
1163 ScopedVector
<TestPrerender
> PrerenderTestURL(
1164 const std::string
& html_file
,
1165 const std::vector
<FinalStatus
>& expected_final_status_queue
,
1166 int expected_number_of_loads
) {
1167 GURL url
= test_server()->GetURL(html_file
);
1168 return PrerenderTestURLImpl(url
,
1169 expected_final_status_queue
,
1170 expected_number_of_loads
);
1173 scoped_ptr
<TestPrerender
> PrerenderTestURL(
1175 FinalStatus expected_final_status
,
1176 int expected_number_of_loads
) {
1177 std::vector
<FinalStatus
> expected_final_status_queue(
1178 1, expected_final_status
);
1179 std::vector
<TestPrerender
*> prerenders
;
1180 PrerenderTestURLImpl(url
,
1181 expected_final_status_queue
,
1182 expected_number_of_loads
).release(&prerenders
);
1183 CHECK_EQ(1u, prerenders
.size());
1184 return scoped_ptr
<TestPrerender
>(prerenders
[0]);
1187 void NavigateToDestURL() const {
1188 NavigateToDestURLWithDisposition(CURRENT_TAB
, true);
1191 // Opens the url in a new tab, with no opener.
1192 void NavigateToDestURLWithDisposition(
1193 WindowOpenDisposition disposition
,
1194 bool expect_swap_to_succeed
) const {
1195 NavigateToURLWithParams(
1196 content::OpenURLParams(dest_url_
, Referrer(), disposition
,
1197 ui::PAGE_TRANSITION_TYPED
, false),
1198 expect_swap_to_succeed
);
1201 void NavigateToURL(const std::string
& dest_html_file
) const {
1202 NavigateToURLWithDisposition(dest_html_file
, CURRENT_TAB
, true);
1205 void NavigateToURLWithDisposition(const std::string
& dest_html_file
,
1206 WindowOpenDisposition disposition
,
1207 bool expect_swap_to_succeed
) const {
1208 GURL dest_url
= test_server()->GetURL(dest_html_file
);
1209 NavigateToURLWithDisposition(dest_url
, disposition
, expect_swap_to_succeed
);
1212 void NavigateToURLWithDisposition(const GURL
& dest_url
,
1213 WindowOpenDisposition disposition
,
1214 bool expect_swap_to_succeed
) const {
1215 NavigateToURLWithParams(
1216 content::OpenURLParams(dest_url
, Referrer(), disposition
,
1217 ui::PAGE_TRANSITION_TYPED
, false),
1218 expect_swap_to_succeed
);
1221 void NavigateToURLWithParams(const content::OpenURLParams
& params
,
1222 bool expect_swap_to_succeed
) const {
1223 NavigateToURLImpl(params
, expect_swap_to_succeed
);
1226 void OpenDestURLViaClick() const {
1227 OpenURLViaClick(dest_url_
);
1230 void OpenURLViaClick(const GURL
& url
) const {
1231 OpenURLWithJSImpl("Click", url
, GURL(), false);
1234 void OpenDestURLViaClickTarget() const {
1235 OpenURLWithJSImpl("ClickTarget", dest_url_
, GURL(), true);
1238 void OpenDestURLViaClickPing(const GURL
& ping_url
) const {
1239 OpenURLWithJSImpl("ClickPing", dest_url_
, ping_url
, false);
1242 void OpenDestURLViaClickNewWindow() const {
1243 OpenURLWithJSImpl("ShiftClick", dest_url_
, GURL(), true);
1246 void OpenDestURLViaClickNewForegroundTab() const {
1247 #if defined(OS_MACOSX)
1248 OpenURLWithJSImpl("MetaShiftClick", dest_url_
, GURL(), true);
1250 OpenURLWithJSImpl("CtrlShiftClick", dest_url_
, GURL(), true);
1254 void OpenDestURLViaWindowOpen() const {
1255 OpenURLViaWindowOpen(dest_url_
);
1258 void OpenURLViaWindowOpen(const GURL
& url
) const {
1259 OpenURLWithJSImpl("WindowOpen", url
, GURL(), true);
1262 void RemoveLinkElement(int i
) const {
1263 GetActiveWebContents()->GetMainFrame()->ExecuteJavaScript(
1264 base::ASCIIToUTF16(base::StringPrintf("RemoveLinkElement(%d)", i
)));
1267 void ClickToNextPageAfterPrerender() {
1268 TestNavigationObserver
nav_observer(GetActiveWebContents());
1269 RenderFrameHost
* render_frame_host
= GetActiveWebContents()->GetMainFrame();
1270 render_frame_host
->ExecuteJavaScript(base::ASCIIToUTF16("ClickOpenLink()"));
1271 nav_observer
.Wait();
1274 void NavigateToNextPageAfterPrerender() const {
1275 ui_test_utils::NavigateToURL(
1277 test_server()->GetURL("files/prerender/prerender_page.html"));
1280 // Called after the prerendered page has been navigated to and then away from.
1281 // Navigates back through the history to the prerendered page.
1282 void GoBackToPrerender() {
1283 TestNavigationObserver
back_nav_observer(GetActiveWebContents());
1284 chrome::GoBack(current_browser(), CURRENT_TAB
);
1285 back_nav_observer
.Wait();
1286 bool original_prerender_page
= false;
1287 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1288 GetActiveWebContents(),
1289 "window.domAutomationController.send(IsOriginalPrerenderPage())",
1290 &original_prerender_page
));
1291 EXPECT_TRUE(original_prerender_page
);
1294 // Goes back to the page that was active before the prerender was swapped
1295 // in. This must be called when the prerendered page is the current page
1296 // in the active tab.
1297 void GoBackToPageBeforePrerender() {
1298 WebContents
* tab
= GetActiveWebContents();
1300 EXPECT_FALSE(tab
->IsLoading());
1301 TestNavigationObserver
back_nav_observer(tab
);
1302 chrome::GoBack(current_browser(), CURRENT_TAB
);
1303 back_nav_observer
.Wait();
1305 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1307 "window.domAutomationController.send(DidBackToOriginalPagePass())",
1309 EXPECT_TRUE(js_result
);
1312 bool UrlIsInPrerenderManager(const std::string
& html_file
) const {
1313 return UrlIsInPrerenderManager(test_server()->GetURL(html_file
));
1316 bool UrlIsInPrerenderManager(const GURL
& url
) const {
1317 return GetPrerenderManager()->FindPrerenderData(
1318 url
, GetSessionStorageNamespace()) != NULL
;
1321 void UseHttpsSrcServer() {
1322 if (https_src_server_
)
1324 https_src_server_
.reset(
1325 new net::SpawnedTestServer(
1326 net::SpawnedTestServer::TYPE_HTTPS
,
1327 net::SpawnedTestServer::kLocalhost
,
1328 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
1329 CHECK(https_src_server_
->Start());
1332 void DisableJavascriptCalls() {
1333 call_javascript_
= false;
1336 void DisableLoadEventCheck() {
1337 check_load_events_
= false;
1340 TaskManagerModel
* GetModel() const {
1341 return TaskManager::GetInstance()->model();
1344 PrerenderManager
* GetPrerenderManager() const {
1345 PrerenderManager
* prerender_manager
=
1346 PrerenderManagerFactory::GetForProfile(current_browser()->profile());
1347 return prerender_manager
;
1350 const PrerenderLinkManager
* GetPrerenderLinkManager() const {
1351 PrerenderLinkManager
* prerender_link_manager
=
1352 PrerenderLinkManagerFactory::GetForProfile(
1353 current_browser()->profile());
1354 return prerender_link_manager
;
1357 int GetPrerenderEventCount(int index
, const std::string
& type
) const {
1359 std::string expression
= base::StringPrintf(
1360 "window.domAutomationController.send("
1361 " GetPrerenderEventCount(%d, '%s'))", index
, type
.c_str());
1363 CHECK(content::ExecuteScriptAndExtractInt(
1364 GetActiveWebContents(), expression
, &event_count
));
1368 bool DidReceivePrerenderStartEventForLinkNumber(int index
) const {
1369 return GetPrerenderEventCount(index
, "webkitprerenderstart") > 0;
1372 int GetPrerenderLoadEventCountForLinkNumber(int index
) const {
1373 return GetPrerenderEventCount(index
, "webkitprerenderload");
1376 int GetPrerenderDomContentLoadedEventCountForLinkNumber(int index
) const {
1377 return GetPrerenderEventCount(index
, "webkitprerenderdomcontentloaded");
1380 bool DidReceivePrerenderStopEventForLinkNumber(int index
) const {
1381 return GetPrerenderEventCount(index
, "webkitprerenderstop") > 0;
1384 void WaitForPrerenderEventCount(int index
,
1385 const std::string
& type
,
1388 std::string expression
= base::StringPrintf(
1389 "WaitForPrerenderEventCount(%d, '%s', %d,"
1390 " window.domAutomationController.send.bind("
1391 " window.domAutomationController, 0))",
1392 index
, type
.c_str(), count
);
1394 CHECK(content::ExecuteScriptAndExtractInt(
1395 GetActiveWebContents(), expression
, &dummy
));
1399 bool HadPrerenderEventErrors() const {
1400 bool had_prerender_event_errors
;
1401 CHECK(content::ExecuteScriptAndExtractBool(
1402 GetActiveWebContents(),
1403 "window.domAutomationController.send(Boolean("
1404 " hadPrerenderEventErrors))",
1405 &had_prerender_event_errors
));
1406 return had_prerender_event_errors
;
1409 // Asserting on this can result in flaky tests. PrerenderHandles are
1410 // removed from the PrerenderLinkManager when the prerender is canceled from
1411 // the browser, when the prerenders are cancelled from the renderer process,
1412 // or the channel for the renderer process is closed on the IO thread. In the
1413 // last case, the code must be careful to wait for the channel to close, as it
1414 // is done asynchronously after swapping out the old process. See
1415 // ChannelDestructionWatcher.
1416 bool IsEmptyPrerenderLinkManager() const {
1417 return GetPrerenderLinkManager()->IsEmpty();
1420 size_t GetLinkPrerenderCount() const {
1421 return GetPrerenderLinkManager()->prerenders_
.size();
1424 size_t GetRunningLinkPrerenderCount() const {
1425 return GetPrerenderLinkManager()->CountRunningPrerenders();
1428 // Returns length of |prerender_manager_|'s history, or -1 on failure.
1429 int GetHistoryLength() const {
1430 scoped_ptr
<base::DictionaryValue
> prerender_dict(
1431 static_cast<base::DictionaryValue
*>(
1432 GetPrerenderManager()->GetAsValue()));
1433 if (!prerender_dict
.get())
1435 base::ListValue
* history_list
;
1436 if (!prerender_dict
->GetList("history", &history_list
))
1438 return static_cast<int>(history_list
->GetSize());
1441 #if defined(FULL_SAFE_BROWSING)
1442 FakeSafeBrowsingDatabaseManager
* GetFakeSafeBrowsingDatabaseManager() {
1443 return safe_browsing_factory_
->most_recent_service()->
1444 fake_database_manager();
1448 TestPrerenderContents
* GetPrerenderContentsFor(const GURL
& url
) const {
1449 PrerenderManager::PrerenderData
* prerender_data
=
1450 GetPrerenderManager()->FindPrerenderData(url
, NULL
);
1451 return static_cast<TestPrerenderContents
*>(
1452 prerender_data
? prerender_data
->contents() : NULL
);
1455 void SetLoaderHostOverride(const std::string
& host
) {
1456 loader_host_override_
= host
;
1457 host_resolver()->AddRule(host
, "127.0.0.1");
1460 void set_loader_path(const std::string
& path
) {
1461 loader_path_
= path
;
1464 void set_loader_query(const std::string
& query
) {
1465 loader_query_
= query
;
1468 GURL
GetCrossDomainTestUrl(const std::string
& path
) {
1469 static const std::string secondary_domain
= "www.foo.com";
1470 host_resolver()->AddRule(secondary_domain
, "127.0.0.1");
1471 std::string
url_str(base::StringPrintf(
1473 secondary_domain
.c_str(),
1474 test_server()->host_port_pair().port(),
1476 return GURL(url_str
);
1479 void set_browser(Browser
* browser
) {
1480 explicitly_set_browser_
= browser
;
1483 Browser
* current_browser() const {
1484 return explicitly_set_browser_
? explicitly_set_browser_
: browser();
1487 const GURL
& dest_url() const {
1491 void IncreasePrerenderMemory() {
1492 // Increase the memory allowed in a prerendered page above normal settings.
1493 // Debug build bots occasionally run against the default limit, and tests
1494 // were failing because the prerender was canceled due to memory exhaustion.
1495 // http://crbug.com/93076
1496 GetPrerenderManager()->mutable_config().max_bytes
= 1000 * 1024 * 1024;
1499 bool DidPrerenderPass(WebContents
* web_contents
) const {
1500 bool prerender_test_result
= false;
1501 if (!content::ExecuteScriptAndExtractBool(
1503 "window.domAutomationController.send(DidPrerenderPass())",
1504 &prerender_test_result
))
1506 return prerender_test_result
;
1509 bool DidDisplayPass(WebContents
* web_contents
) const {
1510 bool display_test_result
= false;
1511 if (!content::ExecuteScriptAndExtractBool(
1513 "window.domAutomationController.send(DidDisplayPass())",
1514 &display_test_result
))
1516 return display_test_result
;
1519 scoped_ptr
<TestPrerender
> ExpectPrerender(FinalStatus expected_final_status
) {
1520 return prerender_contents_factory_
->ExpectPrerenderContents(
1521 expected_final_status
);
1524 void AddPrerender(const GURL
& url
, int index
) {
1525 std::string javascript
= base::StringPrintf(
1526 "AddPrerender('%s', %d)", url
.spec().c_str(), index
);
1527 RenderFrameHost
* render_frame_host
= GetActiveWebContents()->GetMainFrame();
1528 render_frame_host
->ExecuteJavaScript(base::ASCIIToUTF16(javascript
));
1531 // Returns a string for pattern-matching TaskManager tab entries.
1532 base::string16
MatchTaskManagerTab(const char* page_title
) {
1533 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX
,
1534 base::ASCIIToUTF16(page_title
));
1537 // Returns a string for pattern-matching TaskManager prerender entries.
1538 base::string16
MatchTaskManagerPrerender(const char* page_title
) {
1539 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX
,
1540 base::ASCIIToUTF16(page_title
));
1543 const base::HistogramTester
& histogram_tester() { return histogram_tester_
; }
1546 bool autostart_test_server_
;
1549 // TODO(davidben): Remove this altogether so the tests don't globally assume
1550 // only one prerender.
1551 TestPrerenderContents
* GetPrerenderContents() const {
1552 return GetPrerenderContentsFor(dest_url_
);
1555 ScopedVector
<TestPrerender
> PrerenderTestURLImpl(
1556 const GURL
& prerender_url
,
1557 const std::vector
<FinalStatus
>& expected_final_status_queue
,
1558 int expected_number_of_loads
) {
1559 dest_url_
= prerender_url
;
1561 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
1562 replacement_text
.push_back(
1563 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url
.spec()));
1564 std::string replacement_path
;
1565 CHECK(net::SpawnedTestServer::GetFilePathWithReplacements(
1568 &replacement_path
));
1570 const net::SpawnedTestServer
* src_server
= test_server();
1571 if (https_src_server_
)
1572 src_server
= https_src_server_
.get();
1573 GURL loader_url
= src_server
->GetURL(
1574 replacement_path
+ "&" + loader_query_
);
1576 GURL::Replacements loader_replacements
;
1577 if (!loader_host_override_
.empty())
1578 loader_replacements
.SetHostStr(loader_host_override_
);
1579 loader_url
= loader_url
.ReplaceComponents(loader_replacements
);
1581 VLOG(1) << "Running test with queue length " <<
1582 expected_final_status_queue
.size();
1583 CHECK(!expected_final_status_queue
.empty());
1584 ScopedVector
<TestPrerender
> prerenders
;
1585 for (size_t i
= 0; i
< expected_final_status_queue
.size(); i
++) {
1586 prerenders
.push_back(
1587 prerender_contents_factory_
->ExpectPrerenderContents(
1588 expected_final_status_queue
[i
]).release());
1591 FinalStatus expected_final_status
= expected_final_status_queue
.front();
1593 // Navigate to the loader URL and then wait for the first prerender to be
1595 ui_test_utils::NavigateToURL(current_browser(), loader_url
);
1596 prerenders
[0]->WaitForCreate();
1597 prerenders
[0]->WaitForLoads(expected_number_of_loads
);
1599 if (ShouldAbortPrerenderBeforeSwap(expected_final_status
)) {
1600 // The prerender will abort on its own. Assert it does so correctly.
1601 prerenders
[0]->WaitForStop();
1602 EXPECT_FALSE(prerenders
[0]->contents());
1603 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
1605 // Otherwise, check that it prerendered correctly.
1606 TestPrerenderContents
* prerender_contents
= prerenders
[0]->contents();
1608 CHECK_NE(static_cast<PrerenderContents
*>(NULL
), prerender_contents
);
1609 EXPECT_EQ(FINAL_STATUS_MAX
, prerender_contents
->final_status());
1610 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1612 if (call_javascript_
) {
1613 // Check if page behaves as expected while in prerendered state.
1614 EXPECT_TRUE(DidPrerenderPass(prerender_contents
->prerender_contents()));
1618 // Test that the referring page received the right start and load events.
1619 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1620 if (check_load_events_
) {
1621 EXPECT_EQ(expected_number_of_loads
, prerenders
[0]->number_of_loads());
1622 EXPECT_EQ(expected_number_of_loads
,
1623 GetPrerenderLoadEventCountForLinkNumber(0));
1625 EXPECT_FALSE(HadPrerenderEventErrors());
1627 return prerenders
.Pass();
1630 void NavigateToURLImpl(const content::OpenURLParams
& params
,
1631 bool expect_swap_to_succeed
) const {
1632 ASSERT_NE(static_cast<PrerenderManager
*>(NULL
), GetPrerenderManager());
1633 // Make sure in navigating we have a URL to use in the PrerenderManager.
1634 ASSERT_NE(static_cast<PrerenderContents
*>(NULL
), GetPrerenderContents());
1636 WebContents
* web_contents
= GetPrerenderContents()->prerender_contents();
1638 // Navigate and wait for either the load to finish normally or for a swap to
1640 // TODO(davidben): The only handles CURRENT_TAB navigations, which is the
1641 // only case tested or prerendered right now.
1642 CHECK_EQ(CURRENT_TAB
, params
.disposition
);
1643 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
1644 GetActiveWebContents());
1645 WebContents
* target_web_contents
= current_browser()->OpenURL(params
);
1646 swap_observer
.Wait();
1648 if (web_contents
&& expect_swap_to_succeed
) {
1649 EXPECT_EQ(web_contents
, target_web_contents
);
1650 if (call_javascript_
)
1651 EXPECT_TRUE(DidDisplayPass(web_contents
));
1655 // Opens the prerendered page using javascript functions in the loader
1656 // page. |javascript_function_name| should be a 0 argument function which is
1657 // invoked. |new_web_contents| is true if the navigation is expected to
1658 // happen in a new WebContents via OpenURL.
1659 void OpenURLWithJSImpl(const std::string
& javascript_function_name
,
1661 const GURL
& ping_url
,
1662 bool new_web_contents
) const {
1663 WebContents
* web_contents
= GetActiveWebContents();
1664 RenderFrameHost
* render_frame_host
= web_contents
->GetMainFrame();
1665 // Extra arguments in JS are ignored.
1666 std::string javascript
= base::StringPrintf(
1667 "%s('%s', '%s')", javascript_function_name
.c_str(),
1668 url
.spec().c_str(), ping_url
.spec().c_str());
1670 if (new_web_contents
) {
1671 NewTabNavigationOrSwapObserver observer
;
1672 render_frame_host
->ExecuteJavaScriptWithUserGestureForTests(
1673 base::ASCIIToUTF16(javascript
));
1676 NavigationOrSwapObserver
observer(current_browser()->tab_strip_model(),
1678 render_frame_host
->ExecuteJavaScript(base::ASCIIToUTF16(javascript
));
1683 TestPrerenderContentsFactory
* prerender_contents_factory_
;
1684 #if defined(FULL_SAFE_BROWSING)
1685 scoped_ptr
<TestSafeBrowsingServiceFactory
> safe_browsing_factory_
;
1687 NeverRunsExternalProtocolHandlerDelegate external_protocol_handler_delegate_
;
1689 scoped_ptr
<net::SpawnedTestServer
> https_src_server_
;
1690 bool call_javascript_
;
1691 bool check_load_events_
;
1692 std::string loader_host_override_
;
1693 std::string loader_path_
;
1694 std::string loader_query_
;
1695 Browser
* explicitly_set_browser_
;
1696 base::HistogramTester histogram_tester_
;
1699 // Checks that a page is correctly prerendered in the case of a
1700 // <link rel=prerender> tag and then loaded into a tab in response to a
1702 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPage
) {
1703 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
1704 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
1705 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1706 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1707 histogram_tester().ExpectTotalCount(
1708 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1709 histogram_tester().ExpectTotalCount(
1710 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
1712 ChannelDestructionWatcher channel_close_watcher
;
1713 channel_close_watcher
.WatchChannel(
1714 GetActiveWebContents()->GetRenderProcessHost());
1715 NavigateToDestURL();
1716 channel_close_watcher
.WaitForChannelClose();
1718 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
1719 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
1721 histogram_tester().ExpectTotalCount(
1722 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
1724 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1727 // Checks that cross-domain prerenders emit the correct histograms.
1728 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageCrossDomain
) {
1729 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
1730 FINAL_STATUS_USED
, 1);
1731 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1732 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1733 histogram_tester().ExpectTotalCount(
1734 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1735 histogram_tester().ExpectTotalCount(
1736 "Prerender.webcross_PrerenderNotSwappedInPLT", 1);
1738 NavigateToDestURL();
1739 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLT", 1);
1740 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLTMatched",
1742 histogram_tester().ExpectTotalCount(
1743 "Prerender.webcross_PerceivedPLTMatchedComplete", 1);
1746 // Checks that pending prerenders launch and receive proper event treatment.
1747 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPagePending
) {
1748 scoped_ptr
<TestPrerender
> prerender
=
1749 PrerenderTestURL("files/prerender/prerender_page_pending.html",
1750 FINAL_STATUS_USED
, 1);
1752 // Navigate to the prerender.
1753 scoped_ptr
<TestPrerender
> prerender2
= ExpectPrerender(FINAL_STATUS_USED
);
1754 NavigateToDestURL();
1755 // Abort early if the original prerender didn't swap, so as not to hang.
1756 ASSERT_FALSE(prerender
->contents());
1758 // Wait for the new prerender to be ready.
1759 prerender2
->WaitForStart();
1760 prerender2
->WaitForLoads(1);
1762 const GURL prerender_page_url
=
1763 test_server()->GetURL("files/prerender/prerender_page.html");
1764 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1765 EXPECT_NE(static_cast<TestPrerenderContents
*>(NULL
),
1766 GetPrerenderContentsFor(prerender_page_url
));
1768 // Now navigate to our target page.
1769 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
1770 GetActiveWebContents());
1771 ui_test_utils::NavigateToURLWithDisposition(
1772 current_browser(), prerender_page_url
, CURRENT_TAB
,
1773 ui_test_utils::BROWSER_TEST_NONE
);
1774 swap_observer
.Wait();
1776 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1779 // Checks that pending prerenders which are canceled before they are launched
1780 // never get started.
1781 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageRemovesPending
) {
1782 PrerenderTestURL("files/prerender/prerender_page_removes_pending.html",
1783 FINAL_STATUS_USED
, 1);
1785 ChannelDestructionWatcher channel_close_watcher
;
1786 channel_close_watcher
.WatchChannel(
1787 GetActiveWebContents()->GetRenderProcessHost());
1788 NavigateToDestURL();
1789 channel_close_watcher
.WaitForChannelClose();
1791 EXPECT_FALSE(DidReceivePrerenderStartEventForLinkNumber(1));
1792 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1793 EXPECT_FALSE(HadPrerenderEventErrors());
1794 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1795 // calls did a thread/process hop to the renderer which insured pending
1796 // renderer events have arrived.
1797 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1800 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageRemovingLink
) {
1801 scoped_ptr
<TestPrerender
> prerender
=
1802 PrerenderTestURL("files/prerender/prerender_page.html",
1803 FINAL_STATUS_CANCELLED
, 1);
1805 // No ChannelDestructionWatcher is needed here, since prerenders in the
1806 // PrerenderLinkManager should be deleted by removing the links, rather than
1807 // shutting down the renderer process.
1808 RemoveLinkElement(0);
1809 prerender
->WaitForStop();
1811 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1812 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1813 EXPECT_FALSE(HadPrerenderEventErrors());
1814 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1815 // calls did a thread/process hop to the renderer which insured pending
1816 // renderer events have arrived.
1817 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1820 IN_PROC_BROWSER_TEST_F(
1821 PrerenderBrowserTest
, PrerenderPageRemovingLinkWithTwoLinks
) {
1822 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
1823 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
1825 set_loader_query("links_to_insert=2");
1826 scoped_ptr
<TestPrerender
> prerender
=
1827 PrerenderTestURL("files/prerender/prerender_page.html",
1828 FINAL_STATUS_CANCELLED
, 1);
1829 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1830 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1831 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1832 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1834 RemoveLinkElement(0);
1835 RemoveLinkElement(1);
1836 prerender
->WaitForStop();
1838 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1839 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1840 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1841 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1842 EXPECT_FALSE(HadPrerenderEventErrors());
1843 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1844 // calls did a thread/process hop to the renderer which insured pending
1845 // renderer events have arrived.
1846 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1849 IN_PROC_BROWSER_TEST_F(
1850 PrerenderBrowserTest
, PrerenderPageRemovingLinkWithTwoLinksOneLate
) {
1851 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
1852 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
1854 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
1855 scoped_ptr
<TestPrerender
> prerender
=
1856 PrerenderTestURL(url
, FINAL_STATUS_CANCELLED
, 1);
1858 // Add a second prerender for the same link. It reuses the prerender, so only
1859 // the start event fires here.
1860 AddPrerender(url
, 1);
1861 WaitForPrerenderEventCount(1, "webkitprerenderstart", 1);
1862 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1863 EXPECT_EQ(0, GetPrerenderLoadEventCountForLinkNumber(1));
1864 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1866 RemoveLinkElement(0);
1867 RemoveLinkElement(1);
1868 prerender
->WaitForStop();
1870 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1871 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1872 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1873 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1874 EXPECT_FALSE(HadPrerenderEventErrors());
1875 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1876 // calls did a thread/process hop to the renderer which insured pending
1877 // renderer events have arrived.
1878 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1881 IN_PROC_BROWSER_TEST_F(
1882 PrerenderBrowserTest
,
1883 PrerenderPageRemovingLinkWithTwoLinksRemovingOne
) {
1884 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
1885 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
1886 set_loader_query("links_to_insert=2");
1887 PrerenderTestURL("files/prerender/prerender_page.html",
1888 FINAL_STATUS_USED
, 1);
1889 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1890 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1891 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1892 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1894 RemoveLinkElement(0);
1895 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1896 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1897 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1898 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1899 EXPECT_FALSE(HadPrerenderEventErrors());
1900 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1901 // calls did a thread/process hop to the renderer which insured pending
1902 // renderer events have arrived.
1903 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1905 ChannelDestructionWatcher channel_close_watcher
;
1906 channel_close_watcher
.WatchChannel(
1907 GetActiveWebContents()->GetRenderProcessHost());
1908 NavigateToDestURL();
1909 channel_close_watcher
.WaitForChannelClose();
1911 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1914 // Checks that the visibility API works.
1915 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderVisibility
) {
1916 PrerenderTestURL("files/prerender/prerender_visibility.html",
1919 NavigateToDestURL();
1922 // Checks that the prerendering of a page is canceled correctly if we try to
1923 // swap it in before it commits.
1924 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNoCommitNoSwap
) {
1925 // Navigate to a page that triggers a prerender for a URL that never commits.
1926 const GURL
kNoCommitUrl("http://never-respond.example.com");
1927 base::FilePath
file(GetTestPath("prerender_page.html"));
1929 base::RunLoop prerender_start_loop
;
1930 BrowserThread::PostTask(
1931 BrowserThread::IO
, FROM_HERE
,
1932 base::Bind(&CreateHangingFirstRequestInterceptorOnIO
, kNoCommitUrl
, file
,
1933 prerender_start_loop
.QuitClosure()));
1934 DisableJavascriptCalls();
1935 PrerenderTestURL(kNoCommitUrl
,
1936 FINAL_STATUS_NAVIGATION_UNCOMMITTED
,
1938 // Wait for the hanging request to be scheduled.
1939 prerender_start_loop
.Run();
1941 // Navigate to the URL, but assume the contents won't be swapped in.
1942 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
1945 // Checks that client redirects don't add alias URLs until after they commit.
1946 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNoCommitNoSwap2
) {
1947 // Navigate to a page that then navigates to a URL that never commits.
1948 const GURL
kNoCommitUrl("http://never-respond.example.com");
1949 base::FilePath
file(GetTestPath("prerender_page.html"));
1951 base::RunLoop prerender_start_loop
;
1952 BrowserThread::PostTask(
1953 BrowserThread::IO
, FROM_HERE
,
1954 base::Bind(&CreateHangingFirstRequestInterceptorOnIO
, kNoCommitUrl
, file
,
1955 prerender_start_loop
.QuitClosure()));
1956 DisableJavascriptCalls();
1957 PrerenderTestURL(CreateClientRedirect(kNoCommitUrl
.spec()),
1958 FINAL_STATUS_APP_TERMINATING
, 1);
1959 // Wait for the hanging request to be scheduled.
1960 prerender_start_loop
.Run();
1962 // Navigating to the second URL should not swap.
1963 NavigateToURLWithDisposition(kNoCommitUrl
, CURRENT_TAB
, false);
1966 // Checks that the prerendering of a page is canceled correctly when a
1967 // Javascript alert is called.
1968 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderAlertBeforeOnload
) {
1969 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
1970 FINAL_STATUS_JAVASCRIPT_ALERT
,
1974 // Checks that the prerendering of a page is canceled correctly when a
1975 // Javascript alert is called.
1976 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderAlertAfterOnload
) {
1977 PrerenderTestURL("files/prerender/prerender_alert_after_onload.html",
1978 FINAL_STATUS_JAVASCRIPT_ALERT
,
1982 // Checks that plugins are not loaded while a page is being preloaded, but
1983 // are loaded when the page is displayed.
1984 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDelayLoadPlugin
) {
1985 PrerenderTestURL("files/prerender/prerender_plugin_delay_load.html",
1986 FINAL_STATUS_USED
, 1);
1987 NavigateToDestURL();
1990 // For Content Setting DETECT, checks that plugins are not loaded while
1991 // a page is being preloaded, but are loaded when the page is displayed.
1992 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderContentSettingDetect
) {
1993 HostContentSettingsMap
* content_settings_map
=
1994 current_browser()->profile()->GetHostContentSettingsMap();
1995 content_settings_map
->SetDefaultContentSetting(
1996 CONTENT_SETTINGS_TYPE_PLUGINS
, CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
);
1998 PrerenderTestURL("files/prerender/prerender_plugin_power_saver.html",
1999 FINAL_STATUS_USED
, 1);
2001 DisableJavascriptCalls();
2002 NavigateToDestURL();
2003 bool second_placeholder_present
= false;
2004 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
2005 GetActiveWebContents(), "AwaitPluginPrerollAndPlaceholder();",
2006 &second_placeholder_present
));
2007 EXPECT_TRUE(second_placeholder_present
);
2010 // For Content Setting BLOCK, checks that plugins are never loaded.
2011 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderContentSettingBlock
) {
2012 HostContentSettingsMap
* content_settings_map
=
2013 current_browser()->profile()->GetHostContentSettingsMap();
2014 content_settings_map
->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS
,
2015 CONTENT_SETTING_BLOCK
);
2017 PrerenderTestURL("files/prerender/prerender_plugin_never_load.html",
2018 FINAL_STATUS_USED
, 1);
2019 NavigateToDestURL();
2022 // Checks that we don't load a NaCl plugin when NaCl is disabled.
2023 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNaClPluginDisabled
) {
2024 PrerenderTestURL("files/prerender/prerender_plugin_nacl_disabled.html",
2027 NavigateToDestURL();
2030 // Run this check again. When we try to load aa ppapi plugin, the
2031 // "loadstart" event is asynchronously posted to a message loop.
2032 // It's possible that earlier call could have been run before the
2033 // the "loadstart" event was posted.
2034 // TODO(mmenke): While this should reliably fail on regressions, the
2035 // reliability depends on the specifics of ppapi plugin
2036 // loading. It would be great if we could avoid that.
2037 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2040 // Checks that plugins in an iframe are not loaded while a page is
2041 // being preloaded, but are loaded when the page is displayed.
2042 #if defined(USE_AURA) && !defined(OS_WIN)
2043 // http://crbug.com/103496
2044 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2045 DISABLED_PrerenderIframeDelayLoadPlugin
2046 #elif defined(OS_MACOSX)
2047 // http://crbug.com/100514
2048 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2049 DISABLED_PrerenderIframeDelayLoadPlugin
2050 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
2051 // TODO(jschuh): Failing plugin tests. crbug.com/244653
2052 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2053 DISABLED_PrerenderIframeDelayLoadPlugin
2055 #define MAYBE_PrerenderIframeDelayLoadPlugin PrerenderIframeDelayLoadPlugin
2057 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2058 MAYBE_PrerenderIframeDelayLoadPlugin
) {
2059 PrerenderTestURL("files/prerender/prerender_iframe_plugin_delay_load.html",
2062 NavigateToDestURL();
2065 // Renders a page that contains a prerender link to a page that contains an
2066 // iframe with a source that requires http authentication. This should not
2067 // prerender successfully.
2068 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHttpAuthentication
) {
2069 PrerenderTestURL("files/prerender/prerender_http_auth_container.html",
2070 FINAL_STATUS_AUTH_NEEDED
,
2074 // Checks that client-issued redirects work with prerendering.
2075 // This version navigates to the page which issues the redirection, rather
2076 // than the final destination page.
2077 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2078 PrerenderClientRedirectNavigateToFirst
) {
2079 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2082 NavigateToDestURL();
2085 // Checks that client-issued redirects work with prerendering.
2086 // This version navigates to the final destination page, rather than the
2087 // page which does the redirection.
2088 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2089 PrerenderClientRedirectNavigateToSecond
) {
2090 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2093 NavigateToURL("files/prerender/prerender_page.html");
2096 // Checks that redirects with location.replace do not cancel a prerender and
2097 // and swap when navigating to the first page.
2098 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2099 PrerenderLocationReplaceNavigateToFirst
) {
2100 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2103 NavigateToDestURL();
2106 // Checks that redirects with location.replace do not cancel a prerender and
2107 // and swap when navigating to the second.
2108 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2109 PrerenderLocationReplaceNavigateToSecond
) {
2110 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2113 NavigateToURL("files/prerender/prerender_page.html");
2116 // Checks that we get the right PPLT histograms for client redirect prerenders
2117 // and navigations when the referring page is Google.
2118 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2119 PrerenderLocationReplaceGWSHistograms
) {
2120 DisableJavascriptCalls();
2122 // The loader page should look like Google.
2123 static const char kGoogleDotCom
[] = "www.google.com";
2124 SetLoaderHostOverride(kGoogleDotCom
);
2125 set_loader_path("files/prerender/prerender_loader_with_replace_state.html");
2127 GURL dest_url
= GetCrossDomainTestUrl(
2128 "files/prerender/prerender_deferred_image.html");
2130 GURL prerender_url
= test_server()->GetURL(
2131 "files/prerender/prerender_location_replace.html?" +
2132 net::EscapeQueryParamValue(dest_url
.spec(), false) +
2134 GURL::Replacements replacements
;
2135 replacements
.SetHostStr(kGoogleDotCom
);
2136 prerender_url
= prerender_url
.ReplaceComponents(replacements
);
2138 // The prerender will not completely load until after the swap, so wait for a
2139 // title change before calling DidPrerenderPass.
2140 scoped_ptr
<TestPrerender
> prerender
=
2141 PrerenderTestURL(prerender_url
, FINAL_STATUS_USED
, 1);
2142 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
2143 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
2144 EXPECT_EQ(1, prerender
->number_of_loads());
2146 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
2147 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
2148 histogram_tester().ExpectTotalCount(
2149 "Prerender.none_PerceivedPLTMatchedComplete", 0);
2150 // Although there is a client redirect, it is dropped from histograms because
2151 // it is a Google URL. The target page itself does not load until after the
2153 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2156 GURL navigate_url
= test_server()->GetURL(
2157 "files/prerender/prerender_location_replace.html?" +
2158 net::EscapeQueryParamValue(dest_url
.spec(), false) +
2160 navigate_url
= navigate_url
.ReplaceComponents(replacements
);
2162 NavigationOrSwapObserver
swap_observer(
2163 current_browser()->tab_strip_model(),
2164 GetActiveWebContents(), 2);
2165 current_browser()->OpenURL(OpenURLParams(
2166 navigate_url
, Referrer(), CURRENT_TAB
,
2167 ui::PAGE_TRANSITION_TYPED
, false));
2168 swap_observer
.Wait();
2170 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2172 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2174 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLT", 1);
2175 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLTMatched", 1);
2176 histogram_tester().ExpectTotalCount(
2177 "Prerender.gws_PerceivedPLTMatchedComplete", 1);
2179 // The client redirect does /not/ count as a miss because it's a Google URL.
2180 histogram_tester().ExpectTotalCount("Prerender.PerceivedPLTFirstAfterMiss",
2184 // Checks that client-issued redirects work with prerendering.
2185 // This version navigates to the final destination page, rather than the
2186 // page which does the redirection via a mouse click.
2187 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2188 PrerenderClientRedirectNavigateToSecondViaClick
) {
2189 GURL prerender_url
= test_server()->GetURL(
2190 CreateClientRedirect("files/prerender/prerender_page.html"));
2191 GURL destination_url
= test_server()->GetURL(
2192 "files/prerender/prerender_page.html");
2193 PrerenderTestURL(prerender_url
, FINAL_STATUS_USED
, 2);
2194 OpenURLViaClick(destination_url
);
2197 // Checks that a page served over HTTPS is correctly prerendered.
2198 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHttps
) {
2199 net::SpawnedTestServer
https_server(
2200 net::SpawnedTestServer::TYPE_HTTPS
, net::SpawnedTestServer::kLocalhost
,
2201 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2202 ASSERT_TRUE(https_server
.Start());
2203 GURL https_url
= https_server
.GetURL("files/prerender/prerender_page.html");
2204 PrerenderTestURL(https_url
,
2207 NavigateToDestURL();
2210 // Checks that client-issued redirects within an iframe in a prerendered
2211 // page will not count as an "alias" for the prerendered page.
2212 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2213 PrerenderClientRedirectInIframe
) {
2214 std::string redirect_path
= CreateClientRedirect(
2215 "/files/prerender/prerender_embedded_content.html");
2216 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2217 replacement_text
.push_back(
2218 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path
));
2219 std::string replacement_path
;
2220 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2221 "files/prerender/prerender_with_iframe.html",
2223 &replacement_path
));
2224 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 2);
2225 EXPECT_FALSE(UrlIsInPrerenderManager(
2226 "files/prerender/prerender_embedded_content.html"));
2227 NavigateToDestURL();
2230 // Checks that server-issued redirects work with prerendering.
2231 // This version navigates to the page which issues the redirection, rather
2232 // than the final destination page.
2233 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2234 PrerenderServerRedirectNavigateToFirst
) {
2235 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2238 NavigateToDestURL();
2241 // Checks that server-issued redirects work with prerendering.
2242 // This version navigates to the final destination page, rather than the
2243 // page which does the redirection.
2244 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2245 PrerenderServerRedirectNavigateToSecond
) {
2246 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2249 NavigateToURL("files/prerender/prerender_page.html");
2252 // Checks that server-issued redirects work with prerendering.
2253 // This version navigates to the final destination page, rather than the
2254 // page which does the redirection via a mouse click.
2255 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2256 PrerenderServerRedirectNavigateToSecondViaClick
) {
2257 GURL prerender_url
= test_server()->GetURL(
2258 CreateServerRedirect("files/prerender/prerender_page.html"));
2259 GURL destination_url
= test_server()->GetURL(
2260 "files/prerender/prerender_page.html");
2261 PrerenderTestURL(prerender_url
, FINAL_STATUS_USED
, 1);
2262 OpenURLViaClick(destination_url
);
2265 // Checks that server-issued redirects within an iframe in a prerendered
2266 // page will not count as an "alias" for the prerendered page.
2267 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderServerRedirectInIframe
) {
2268 std::string redirect_path
= CreateServerRedirect(
2269 "/files/prerender/prerender_embedded_content.html");
2270 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2271 replacement_text
.push_back(
2272 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path
));
2273 std::string replacement_path
;
2274 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2275 "files/prerender/prerender_with_iframe.html",
2277 &replacement_path
));
2278 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
2279 EXPECT_FALSE(UrlIsInPrerenderManager(
2280 "files/prerender/prerender_embedded_content.html"));
2281 NavigateToDestURL();
2284 // Prerenders a page that contains an automatic download triggered through an
2285 // iframe. This should not prerender successfully.
2286 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDownloadIframe
) {
2287 PrerenderTestURL("files/prerender/prerender_download_iframe.html",
2288 FINAL_STATUS_DOWNLOAD
,
2292 // Prerenders a page that contains an automatic download triggered through
2293 // Javascript changing the window.location. This should not prerender
2295 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDownloadLocation
) {
2296 PrerenderTestURL(CreateClientRedirect("files/download-test1.lib"),
2297 FINAL_STATUS_DOWNLOAD
,
2301 // Prerenders a page that contains an automatic download triggered through a
2302 // client-issued redirect. This should not prerender successfully.
2303 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDownloadClientRedirect
) {
2304 PrerenderTestURL("files/prerender/prerender_download_refresh.html",
2305 FINAL_STATUS_DOWNLOAD
,
2309 // Checks that the referrer is set when prerendering.
2310 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderReferrer
) {
2311 PrerenderTestURL("files/prerender/prerender_referrer.html",
2314 NavigateToDestURL();
2317 // Checks that the referrer is not set when prerendering and the source page is
2319 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2320 PrerenderNoSSLReferrer
) {
2321 UseHttpsSrcServer();
2322 PrerenderTestURL("files/prerender/prerender_no_referrer.html",
2325 NavigateToDestURL();
2328 // Checks that the referrer is set when prerendering is cancelled.
2329 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCancelReferrer
) {
2330 scoped_ptr
<TestContentBrowserClient
> test_content_browser_client(
2331 new TestContentBrowserClient
);
2332 content::ContentBrowserClient
* original_browser_client
=
2333 content::SetBrowserClientForTesting(test_content_browser_client
.get());
2335 PrerenderTestURL("files/prerender/prerender_referrer.html",
2336 FINAL_STATUS_CANCELLED
,
2338 OpenDestURLViaClick();
2340 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2342 content::SetBrowserClientForTesting(original_browser_client
);
2345 // Checks that popups on a prerendered page cause cancellation.
2346 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPopup
) {
2347 PrerenderTestURL("files/prerender/prerender_popup.html",
2348 FINAL_STATUS_CREATE_NEW_WINDOW
,
2352 // Checks that registering a protocol handler causes cancellation.
2353 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderRegisterProtocolHandler
) {
2354 PrerenderTestURL("files/prerender/prerender_register_protocol_handler.html",
2355 FINAL_STATUS_REGISTER_PROTOCOL_HANDLER
,
2359 // Checks that renderers using excessive memory will be terminated.
2360 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderExcessiveMemory
) {
2361 ASSERT_TRUE(GetPrerenderManager());
2362 GetPrerenderManager()->mutable_config().max_bytes
= 30 * 1024 * 1024;
2363 // The excessive memory kill may happen before or after the load event as it
2364 // happens asynchronously with IPC calls. Even if the test does not start
2365 // allocating until after load, the browser process might notice before the
2366 // message gets through. This happens on XP debug bots because they're so
2367 // slow. Instead, don't bother checking the load event count.
2368 DisableLoadEventCheck();
2369 PrerenderTestURL("files/prerender/prerender_excessive_memory.html",
2370 FINAL_STATUS_MEMORY_LIMIT_EXCEEDED
, 0);
2373 // Checks shutdown code while a prerender is active.
2374 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderQuickQuit
) {
2375 DisableJavascriptCalls();
2376 DisableLoadEventCheck();
2377 PrerenderTestURL("files/prerender/prerender_page.html",
2378 FINAL_STATUS_APP_TERMINATING
,
2382 // Checks that we don't prerender in an infinite loop.
2383 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderInfiniteLoop
) {
2384 const char* const kHtmlFileA
= "files/prerender/prerender_infinite_a.html";
2385 const char* const kHtmlFileB
= "files/prerender/prerender_infinite_b.html";
2387 std::vector
<FinalStatus
> expected_final_status_queue
;
2388 expected_final_status_queue
.push_back(FINAL_STATUS_USED
);
2389 expected_final_status_queue
.push_back(FINAL_STATUS_APP_TERMINATING
);
2391 ScopedVector
<TestPrerender
> prerenders
=
2392 PrerenderTestURL(kHtmlFileA
, expected_final_status_queue
, 1);
2393 ASSERT_TRUE(prerenders
[0]->contents());
2394 // Assert that the pending prerender is in there already. This relies on the
2395 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2396 // the page load one.
2397 EXPECT_EQ(2U, GetLinkPrerenderCount());
2398 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2400 // Next url should be in pending list but not an active entry.
2401 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB
));
2403 NavigateToDestURL();
2405 // Make sure the PrerenderContents for the next url is now in the manager and
2406 // not pending. This relies on pending prerenders being resolved in the same
2407 // event loop iteration as OnPrerenderStop.
2408 EXPECT_TRUE(UrlIsInPrerenderManager(kHtmlFileB
));
2409 EXPECT_EQ(1U, GetLinkPrerenderCount());
2410 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2413 // Checks that we don't prerender in an infinite loop and multiple links are
2414 // handled correctly.
2415 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2416 PrerenderInfiniteLoopMultiple
) {
2417 const char* const kHtmlFileA
=
2418 "files/prerender/prerender_infinite_a_multiple.html";
2419 const char* const kHtmlFileB
=
2420 "files/prerender/prerender_infinite_b_multiple.html";
2421 const char* const kHtmlFileC
=
2422 "files/prerender/prerender_infinite_c_multiple.html";
2424 // This test is conceptually simplest if concurrency is at two, since we
2425 // don't have to worry about which of kHtmlFileB or kHtmlFileC gets evicted.
2426 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
2427 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
2429 std::vector
<FinalStatus
> expected_final_status_queue
;
2430 expected_final_status_queue
.push_back(FINAL_STATUS_USED
);
2431 expected_final_status_queue
.push_back(FINAL_STATUS_APP_TERMINATING
);
2432 expected_final_status_queue
.push_back(FINAL_STATUS_APP_TERMINATING
);
2434 ScopedVector
<TestPrerender
> prerenders
=
2435 PrerenderTestURL(kHtmlFileA
, expected_final_status_queue
, 1);
2436 ASSERT_TRUE(prerenders
[0]->contents());
2438 // Next url should be in pending list but not an active entry. This relies on
2439 // the fact that the renderer sends out the AddLinkRelPrerender IPC before
2440 // sending the page load one.
2441 EXPECT_EQ(3U, GetLinkPrerenderCount());
2442 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2443 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB
));
2444 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileC
));
2446 NavigateToDestURL();
2448 // Make sure the PrerenderContents for the next urls are now in the manager
2449 // and not pending. One and only one of the URLs (the last seen) should be the
2450 // active entry. This relies on pending prerenders being resolved in the same
2451 // event loop iteration as OnPrerenderStop.
2452 bool url_b_is_active_prerender
= UrlIsInPrerenderManager(kHtmlFileB
);
2453 bool url_c_is_active_prerender
= UrlIsInPrerenderManager(kHtmlFileC
);
2454 EXPECT_TRUE(url_b_is_active_prerender
&& url_c_is_active_prerender
);
2455 EXPECT_EQ(2U, GetLinkPrerenderCount());
2456 EXPECT_EQ(2U, GetRunningLinkPrerenderCount());
2459 // Checks that pending prerenders are aborted (and never launched) when launched
2460 // by a prerender that itself gets aborted.
2461 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderAbortPendingOnCancel
) {
2462 const char* const kHtmlFileA
= "files/prerender/prerender_infinite_a.html";
2463 const char* const kHtmlFileB
= "files/prerender/prerender_infinite_b.html";
2465 scoped_ptr
<TestPrerender
> prerender
=
2466 PrerenderTestURL(kHtmlFileA
, FINAL_STATUS_CANCELLED
, 1);
2467 ASSERT_TRUE(prerender
->contents());
2468 // Assert that the pending prerender is in there already. This relies on the
2469 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2470 // the page load one.
2471 EXPECT_EQ(2U, GetLinkPrerenderCount());
2472 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2474 // Next url should be in pending list but not an active entry.
2475 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB
));
2477 // Cancel the prerender.
2478 GetPrerenderManager()->CancelAllPrerenders();
2479 prerender
->WaitForStop();
2481 // All prerenders are now gone.
2482 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
2485 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, OpenTaskManagerBeforePrerender
) {
2486 const base::string16 any_prerender
= MatchTaskManagerPrerender("*");
2487 const base::string16 any_tab
= MatchTaskManagerTab("*");
2488 const base::string16 original
= MatchTaskManagerTab("Preloader");
2489 const base::string16 prerender
= MatchTaskManagerPrerender("Prerender Page");
2490 const base::string16 final
= MatchTaskManagerTab("Prerender Page");
2492 // Show the task manager. This populates the model.
2493 chrome::OpenTaskManager(current_browser());
2494 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2495 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2497 // Prerender a page in addition to the original tab.
2498 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
2500 // A TaskManager entry should appear like "Prerender: Prerender Page"
2501 // alongside the original tab entry. There should be just these two entries.
2502 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender
));
2503 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original
));
2504 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final
));
2505 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender
));
2506 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2508 // Swap in the prerendered content.
2509 NavigateToDestURL();
2511 // The "Prerender: " TaskManager entry should disappear, being replaced by a
2512 // "Tab: Prerender Page" entry, and nothing else.
2513 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender
));
2514 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original
));
2515 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final
));
2516 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2517 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2520 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, OpenTaskManagerAfterPrerender
) {
2521 const base::string16 any_prerender
= MatchTaskManagerPrerender("*");
2522 const base::string16 any_tab
= MatchTaskManagerTab("*");
2523 const base::string16 original
= MatchTaskManagerTab("Preloader");
2524 const base::string16 prerender
= MatchTaskManagerPrerender("Prerender Page");
2525 const base::string16 final
= MatchTaskManagerTab("Prerender Page");
2527 // Start with two resources.
2528 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
2530 // Show the task manager. This populates the model. Importantly, we're doing
2531 // this after the prerender WebContents already exists - the task manager
2532 // needs to find it, it can't just listen for creation.
2533 chrome::OpenTaskManager(current_browser());
2535 // A TaskManager entry should appear like "Prerender: Prerender Page"
2536 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender
));
2537 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original
));
2538 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final
));
2539 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender
));
2540 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2543 NavigateToDestURL();
2545 // The "Prerender: Prerender Page" TaskManager row should disappear, being
2546 // replaced by "Tab: Prerender Page"
2547 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender
));
2548 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original
));
2549 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final
));
2550 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2551 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2554 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, OpenTaskManagerAfterSwapIn
) {
2555 const base::string16 any_prerender
= MatchTaskManagerPrerender("*");
2556 const base::string16 any_tab
= MatchTaskManagerTab("*");
2557 const base::string16 final
= MatchTaskManagerTab("Prerender Page");
2559 // Prerender, and swap it in.
2560 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
2561 NavigateToDestURL();
2563 // Show the task manager. This populates the model. Importantly, we're doing
2564 // this after the prerender has been swapped in.
2565 chrome::OpenTaskManager(current_browser());
2567 // We should not see a prerender resource in the task manager, just a normal
2569 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final
));
2570 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2571 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2574 // Checks that audio loads are deferred on prerendering.
2575 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5Audio
) {
2576 PrerenderTestURL("files/prerender/prerender_html5_audio.html",
2579 NavigateToDestURL();
2580 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2583 // Checks that audio loads are deferred on prerendering and played back when
2584 // the prerender is swapped in if autoplay is set.
2585 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5AudioAutoplay
) {
2586 PrerenderTestURL("files/prerender/prerender_html5_audio_autoplay.html",
2589 NavigateToDestURL();
2590 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2593 // Checks that audio loads are deferred on prerendering and played back when
2594 // the prerender is swapped in if js starts playing.
2595 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5AudioJsplay
) {
2596 PrerenderTestURL("files/prerender/prerender_html5_audio_jsplay.html",
2599 NavigateToDestURL();
2600 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2603 // Checks that video loads are deferred on prerendering.
2604 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5Video
) {
2605 PrerenderTestURL("files/prerender/prerender_html5_video.html",
2608 NavigateToDestURL();
2609 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2612 // Checks that video tags inserted by javascript are deferred and played
2613 // correctly on swap in.
2614 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5VideoJs
) {
2615 PrerenderTestURL("files/prerender/prerender_html5_video_script.html",
2618 NavigateToDestURL();
2619 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2622 // Checks for correct network events by using a busy sleep the javascript.
2623 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5VideoNetwork
) {
2624 DisableJavascriptCalls();
2625 scoped_ptr
<TestPrerender
> prerender
=
2626 PrerenderTestURL("files/prerender/prerender_html5_video_network.html",
2629 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
2630 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
2631 NavigateToDestURL();
2632 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2635 // Checks that scripts can retrieve the correct window size while prerendering.
2636 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderWindowSize
) {
2637 PrerenderTestURL("files/prerender/prerender_size.html",
2640 NavigateToDestURL();
2643 // TODO(jam): http://crbug.com/350550
2644 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
2646 // Checks that prerenderers will terminate when the RenderView crashes.
2647 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderRendererCrash
) {
2648 scoped_ptr
<TestPrerender
> prerender
=
2649 PrerenderTestURL("files/prerender/prerender_page.html",
2650 FINAL_STATUS_RENDERER_CRASHED
,
2653 // Navigate to about:crash and then wait for the renderer to crash.
2654 ASSERT_TRUE(prerender
->contents());
2655 ASSERT_TRUE(prerender
->contents()->prerender_contents());
2656 prerender
->contents()->prerender_contents()->GetController().
2658 GURL(content::kChromeUICrashURL
),
2659 content::Referrer(),
2660 ui::PAGE_TRANSITION_TYPED
,
2662 prerender
->WaitForStop();
2666 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2667 PrerenderPageWithFragment
) {
2668 PrerenderTestURL("files/prerender/prerender_page.html#fragment",
2672 ChannelDestructionWatcher channel_close_watcher
;
2673 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
2674 GetActiveWebContents()->GetRenderProcessHost());
2675 NavigateToDestURL();
2676 channel_close_watcher
.WaitForChannelClose();
2678 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2681 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2682 PrerenderPageWithRedirectedFragment
) {
2684 CreateClientRedirect("files/prerender/prerender_page.html#fragment"),
2688 ChannelDestructionWatcher channel_close_watcher
;
2689 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
2690 GetActiveWebContents()->GetRenderProcessHost());
2691 NavigateToDestURL();
2692 channel_close_watcher
.WaitForChannelClose();
2694 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2697 // Checks that we do not use a prerendered page when navigating from
2698 // the main page to a fragment.
2699 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2700 PrerenderPageNavigateFragment
) {
2701 PrerenderTestURL("files/prerender/no_prerender_page.html",
2702 FINAL_STATUS_APP_TERMINATING
,
2704 NavigateToURLWithDisposition(
2705 "files/prerender/no_prerender_page.html#fragment",
2706 CURRENT_TAB
, false);
2709 // Checks that we do not use a prerendered page when we prerender a fragment
2710 // but navigate to the main page.
2711 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2712 PrerenderFragmentNavigatePage
) {
2713 PrerenderTestURL("files/prerender/no_prerender_page.html#fragment",
2714 FINAL_STATUS_APP_TERMINATING
,
2716 NavigateToURLWithDisposition(
2717 "files/prerender/no_prerender_page.html",
2718 CURRENT_TAB
, false);
2721 // Checks that we do not use a prerendered page when we prerender a fragment
2722 // but navigate to a different fragment on the same page.
2723 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2724 PrerenderFragmentNavigateFragment
) {
2725 PrerenderTestURL("files/prerender/no_prerender_page.html#other_fragment",
2726 FINAL_STATUS_APP_TERMINATING
,
2728 NavigateToURLWithDisposition(
2729 "files/prerender/no_prerender_page.html#fragment",
2730 CURRENT_TAB
, false);
2733 // Checks that we do not use a prerendered page when the page uses a client
2734 // redirect to refresh from a fragment on the same page.
2735 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2736 PrerenderClientRedirectFromFragment
) {
2738 CreateClientRedirect("files/prerender/no_prerender_page.html#fragment"),
2739 FINAL_STATUS_APP_TERMINATING
,
2741 NavigateToURLWithDisposition(
2742 "files/prerender/no_prerender_page.html",
2743 CURRENT_TAB
, false);
2746 // Checks that we do not use a prerendered page when the page uses a client
2747 // redirect to refresh to a fragment on the same page.
2748 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2749 PrerenderClientRedirectToFragment
) {
2751 CreateClientRedirect("files/prerender/no_prerender_page.html"),
2752 FINAL_STATUS_APP_TERMINATING
,
2754 NavigateToURLWithDisposition(
2755 "files/prerender/no_prerender_page.html#fragment",
2756 CURRENT_TAB
, false);
2759 // Checks that we correctly use a prerendered page when the page uses JS to set
2760 // the window.location.hash to a fragment on the same page.
2761 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2762 PrerenderPageChangeFragmentLocationHash
) {
2763 PrerenderTestURL("files/prerender/prerender_fragment_location_hash.html",
2766 NavigateToURL("files/prerender/prerender_fragment_location_hash.html");
2769 // Checks that prerendering a PNG works correctly.
2770 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderImagePng
) {
2771 DisableJavascriptCalls();
2772 PrerenderTestURL("files/prerender/image.png", FINAL_STATUS_USED
, 1);
2773 NavigateToDestURL();
2776 // Checks that prerendering a JPG works correctly.
2777 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderImageJpeg
) {
2778 DisableJavascriptCalls();
2779 PrerenderTestURL("files/prerender/image.jpeg", FINAL_STATUS_USED
, 1);
2780 NavigateToDestURL();
2783 // Checks that a prerender of a CRX will result in a cancellation due to
2785 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCrx
) {
2786 PrerenderTestURL("files/prerender/extension.crx", FINAL_STATUS_DOWNLOAD
, 0);
2789 // Checks that xhr GET requests allow prerenders.
2790 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrGet
) {
2791 PrerenderTestURL("files/prerender/prerender_xhr_get.html",
2794 NavigateToDestURL();
2797 // Checks that xhr HEAD requests allow prerenders.
2798 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrHead
) {
2799 PrerenderTestURL("files/prerender/prerender_xhr_head.html",
2802 NavigateToDestURL();
2805 // Checks that xhr OPTIONS requests allow prerenders.
2806 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrOptions
) {
2807 PrerenderTestURL("files/prerender/prerender_xhr_options.html",
2810 NavigateToDestURL();
2813 // Checks that xhr TRACE requests allow prerenders.
2814 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrTrace
) {
2815 PrerenderTestURL("files/prerender/prerender_xhr_trace.html",
2818 NavigateToDestURL();
2821 // Checks that xhr POST requests allow prerenders.
2822 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrPost
) {
2823 PrerenderTestURL("files/prerender/prerender_xhr_post.html",
2826 NavigateToDestURL();
2829 // Checks that xhr PUT cancels prerenders.
2830 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrPut
) {
2831 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
2832 FINAL_STATUS_INVALID_HTTP_METHOD
,
2836 // Checks that xhr DELETE cancels prerenders.
2837 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrDelete
) {
2838 PrerenderTestURL("files/prerender/prerender_xhr_delete.html",
2839 FINAL_STATUS_INVALID_HTTP_METHOD
,
2843 // Checks that a top-level page which would trigger an SSL error is canceled.
2844 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLErrorTopLevel
) {
2845 net::SpawnedTestServer::SSLOptions ssl_options
;
2846 ssl_options
.server_certificate
=
2847 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
2848 net::SpawnedTestServer
https_server(
2849 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2850 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2851 ASSERT_TRUE(https_server
.Start());
2852 GURL https_url
= https_server
.GetURL("files/prerender/prerender_page.html");
2853 PrerenderTestURL(https_url
,
2854 FINAL_STATUS_SSL_ERROR
,
2858 // Checks that an SSL error that comes from a subresource does not cancel
2859 // the page. Non-main-frame requests are simply cancelled if they run into
2861 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLErrorSubresource
) {
2862 net::SpawnedTestServer::SSLOptions ssl_options
;
2863 ssl_options
.server_certificate
=
2864 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
2865 net::SpawnedTestServer
https_server(
2866 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2867 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2868 ASSERT_TRUE(https_server
.Start());
2869 GURL https_url
= https_server
.GetURL("files/prerender/image.jpeg");
2870 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2871 replacement_text
.push_back(
2872 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url
.spec()));
2873 std::string replacement_path
;
2874 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2875 "files/prerender/prerender_with_image.html",
2877 &replacement_path
));
2878 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
2879 NavigateToDestURL();
2882 // Checks that an SSL error that comes from an iframe does not cancel
2883 // the page. Non-main-frame requests are simply cancelled if they run into
2885 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLErrorIframe
) {
2886 net::SpawnedTestServer::SSLOptions ssl_options
;
2887 ssl_options
.server_certificate
=
2888 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
2889 net::SpawnedTestServer
https_server(
2890 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2891 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2892 ASSERT_TRUE(https_server
.Start());
2893 GURL https_url
= https_server
.GetURL(
2894 "files/prerender/prerender_embedded_content.html");
2895 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2896 replacement_text
.push_back(
2897 std::make_pair("REPLACE_WITH_URL", https_url
.spec()));
2898 std::string replacement_path
;
2899 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2900 "files/prerender/prerender_with_iframe.html",
2902 &replacement_path
));
2903 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
2904 NavigateToDestURL();
2907 // Checks that we cancel correctly when window.print() is called.
2908 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPrint
) {
2909 DisableLoadEventCheck();
2910 PrerenderTestURL("files/prerender/prerender_print.html",
2911 FINAL_STATUS_WINDOW_PRINT
,
2915 // Checks that prerenders do not get swapped into target pages that have opened
2916 // popups; the BrowsingInstance is not empty.
2917 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderTargetHasPopup
) {
2918 PrerenderTestURL("files/prerender/prerender_page.html",
2919 FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE
,
2921 OpenURLViaWindowOpen(GURL(url::kAboutBlankURL
));
2923 // Switch back to the current tab and attempt to swap it in.
2924 current_browser()->tab_strip_model()->ActivateTabAt(0, true);
2925 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
2928 class TestClientCertStore
: public net::ClientCertStore
{
2930 TestClientCertStore() {}
2931 ~TestClientCertStore() override
{}
2933 // net::ClientCertStore:
2934 void GetClientCerts(const net::SSLCertRequestInfo
& cert_request_info
,
2935 net::CertificateList
* selected_certs
,
2936 const base::Closure
& callback
) override
{
2937 *selected_certs
= net::CertificateList(
2938 1, scoped_refptr
<net::X509Certificate
>(
2939 new net::X509Certificate("test", "test", base::Time(), base::Time())));
2944 scoped_ptr
<net::ClientCertStore
> CreateCertStore() {
2945 return scoped_ptr
<net::ClientCertStore
>(new TestClientCertStore
);
2948 // Checks that a top-level page which would normally request an SSL client
2949 // certificate will never be seen since it's an https top-level resource.
2950 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2951 PrerenderSSLClientCertTopLevel
) {
2952 ProfileIOData::FromResourceContext(
2953 current_browser()->profile()->GetResourceContext())->
2954 set_client_cert_store_factory_for_testing(
2955 base::Bind(&CreateCertStore
));
2956 net::SpawnedTestServer::SSLOptions ssl_options
;
2957 ssl_options
.request_client_certificate
= true;
2958 net::SpawnedTestServer
https_server(
2959 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2960 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2961 ASSERT_TRUE(https_server
.Start());
2962 GURL https_url
= https_server
.GetURL("files/prerender/prerender_page.html");
2963 PrerenderTestURL(https_url
, FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED
, 0);
2966 // Checks that an SSL Client Certificate request that originates from a
2967 // subresource will cancel the prerendered page.
2968 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2969 PrerenderSSLClientCertSubresource
) {
2970 ProfileIOData::FromResourceContext(
2971 current_browser()->profile()->GetResourceContext())->
2972 set_client_cert_store_factory_for_testing(
2973 base::Bind(&CreateCertStore
));
2974 net::SpawnedTestServer::SSLOptions ssl_options
;
2975 ssl_options
.request_client_certificate
= true;
2976 net::SpawnedTestServer
https_server(
2977 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2978 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2979 ASSERT_TRUE(https_server
.Start());
2980 GURL https_url
= https_server
.GetURL("files/prerender/image.jpeg");
2981 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2982 replacement_text
.push_back(
2983 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url
.spec()));
2984 std::string replacement_path
;
2985 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2986 "files/prerender/prerender_with_image.html",
2988 &replacement_path
));
2989 PrerenderTestURL(replacement_path
,
2990 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED
,
2994 // Checks that an SSL Client Certificate request that originates from an
2995 // iframe will cancel the prerendered page.
2996 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLClientCertIframe
) {
2997 ProfileIOData::FromResourceContext(
2998 current_browser()->profile()->GetResourceContext())->
2999 set_client_cert_store_factory_for_testing(
3000 base::Bind(&CreateCertStore
));
3001 net::SpawnedTestServer::SSLOptions ssl_options
;
3002 ssl_options
.request_client_certificate
= true;
3003 net::SpawnedTestServer
https_server(
3004 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
3005 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3006 ASSERT_TRUE(https_server
.Start());
3007 GURL https_url
= https_server
.GetURL(
3008 "files/prerender/prerender_embedded_content.html");
3009 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3010 replacement_text
.push_back(
3011 std::make_pair("REPLACE_WITH_URL", https_url
.spec()));
3012 std::string replacement_path
;
3013 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3014 "files/prerender/prerender_with_iframe.html",
3016 &replacement_path
));
3017 PrerenderTestURL(replacement_path
,
3018 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED
,
3022 #if defined(FULL_SAFE_BROWSING)
3023 // Ensures that we do not prerender pages with a safe browsing
3025 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSafeBrowsingTopLevel
) {
3026 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
3027 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3028 url
, SB_THREAT_TYPE_URL_MALWARE
);
3029 PrerenderTestURL("files/prerender/prerender_page.html",
3030 FINAL_STATUS_SAFE_BROWSING
, 0);
3033 // Ensures that server redirects to a malware page will cancel prerenders.
3034 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3035 PrerenderSafeBrowsingServerRedirect
) {
3036 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
3037 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3038 url
, SB_THREAT_TYPE_URL_MALWARE
);
3039 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
3040 FINAL_STATUS_SAFE_BROWSING
,
3044 // Ensures that client redirects to a malware page will cancel prerenders.
3045 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3046 PrerenderSafeBrowsingClientRedirect
) {
3047 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
3048 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3049 url
, SB_THREAT_TYPE_URL_MALWARE
);
3050 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
3051 FINAL_STATUS_SAFE_BROWSING
,
3055 // Ensures that we do not prerender pages which have a malware subresource.
3056 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSafeBrowsingSubresource
) {
3057 GURL image_url
= test_server()->GetURL("files/prerender/image.jpeg");
3058 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3059 image_url
, SB_THREAT_TYPE_URL_MALWARE
);
3060 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3061 replacement_text
.push_back(
3062 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3063 std::string replacement_path
;
3064 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3065 "files/prerender/prerender_with_image.html",
3067 &replacement_path
));
3068 PrerenderTestURL(replacement_path
,
3069 FINAL_STATUS_SAFE_BROWSING
,
3073 // Ensures that we do not prerender pages which have a malware iframe.
3074 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSafeBrowsingIframe
) {
3075 GURL iframe_url
= test_server()->GetURL(
3076 "files/prerender/prerender_embedded_content.html");
3077 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3078 iframe_url
, SB_THREAT_TYPE_URL_MALWARE
);
3079 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3080 replacement_text
.push_back(
3081 std::make_pair("REPLACE_WITH_URL", iframe_url
.spec()));
3082 std::string replacement_path
;
3083 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3084 "files/prerender/prerender_with_iframe.html",
3086 &replacement_path
));
3087 PrerenderTestURL(replacement_path
,
3088 FINAL_STATUS_SAFE_BROWSING
,
3094 // Checks that a local storage read will not cause prerender to fail.
3095 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderLocalStorageRead
) {
3096 PrerenderTestURL("files/prerender/prerender_localstorage_read.html",
3099 NavigateToDestURL();
3102 // Checks that a local storage write will not cause prerender to fail.
3103 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderLocalStorageWrite
) {
3104 PrerenderTestURL("files/prerender/prerender_localstorage_write.html",
3107 NavigateToDestURL();
3110 // Checks that the favicon is properly loaded on prerender.
3111 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderFavicon
) {
3112 scoped_ptr
<TestPrerender
> prerender
=
3113 PrerenderTestURL("files/prerender/prerender_favicon.html",
3116 NavigateToDestURL();
3118 favicon::FaviconDriver
* favicon_driver
=
3119 favicon::ContentFaviconDriver::FromWebContents(GetActiveWebContents());
3120 if (!favicon_driver
->FaviconIsValid()) {
3121 // If the favicon has not been set yet, wait for it to be.
3122 FaviconUpdateWatcher
favicon_update_watcher(GetActiveWebContents());
3123 favicon_update_watcher
.Wait();
3125 EXPECT_TRUE(favicon_driver
->FaviconIsValid());
3128 // Checks that when a prerendered page is swapped in to a referring page, the
3129 // unload handlers on the referring page are executed.
3130 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderUnload
) {
3131 // Matches URL in prerender_loader_with_unload.html.
3132 const GURL
unload_url("http://unload-url.test");
3133 base::FilePath empty_file
= ui_test_utils::GetTestFilePath(
3134 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3135 RequestCounter unload_counter
;
3136 BrowserThread::PostTask(
3137 BrowserThread::IO
, FROM_HERE
,
3138 base::Bind(&CreateCountingInterceptorOnIO
,
3139 unload_url
, empty_file
, unload_counter
.AsWeakPtr()));
3141 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3142 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3143 NavigateToDestURL();
3144 unload_counter
.WaitForCount(1);
3147 // Checks that a hanging unload on the referring page of a prerender swap does
3148 // not crash the browser on exit.
3149 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHangingUnload
) {
3150 // Matches URL in prerender_loader_with_unload.html.
3151 const GURL
hang_url("http://unload-url.test");
3152 base::FilePath empty_file
= ui_test_utils::GetTestFilePath(
3153 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3154 BrowserThread::PostTask(
3155 BrowserThread::IO
, FROM_HERE
,
3156 base::Bind(&CreateHangingFirstRequestInterceptorOnIO
,
3157 hang_url
, empty_file
,
3160 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3161 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3162 NavigateToDestURL();
3166 // Checks that when the history is cleared, prerendering is cancelled and
3167 // prerendering history is cleared.
3168 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClearHistory
) {
3169 scoped_ptr
<TestPrerender
> prerender
=
3170 PrerenderTestURL("files/prerender/prerender_page.html",
3171 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED
,
3174 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_HISTORY
);
3175 prerender
->WaitForStop();
3177 // Make sure prerender history was cleared.
3178 EXPECT_EQ(0, GetHistoryLength());
3181 // Checks that when the cache is cleared, prerenders are cancelled but
3182 // prerendering history is not cleared.
3183 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClearCache
) {
3184 scoped_ptr
<TestPrerender
> prerender
=
3185 PrerenderTestURL("files/prerender/prerender_page.html",
3186 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED
,
3189 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_CACHE
);
3190 prerender
->WaitForStop();
3192 // Make sure prerender history was not cleared. Not a vital behavior, but
3193 // used to compare with PrerenderClearHistory test.
3194 EXPECT_EQ(1, GetHistoryLength());
3197 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCancelAll
) {
3198 scoped_ptr
<TestPrerender
> prerender
=
3199 PrerenderTestURL("files/prerender/prerender_page.html",
3200 FINAL_STATUS_CANCELLED
,
3203 GetPrerenderManager()->CancelAllPrerenders();
3204 prerender
->WaitForStop();
3206 EXPECT_FALSE(prerender
->contents());
3209 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderEvents
) {
3210 scoped_ptr
<TestPrerender
> prerender
=
3211 PrerenderTestURL("files/prerender/prerender_page.html",
3212 FINAL_STATUS_CANCELLED
, 1);
3214 GetPrerenderManager()->CancelAllPrerenders();
3215 prerender
->WaitForStop();
3217 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
3218 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
3219 EXPECT_FALSE(HadPrerenderEventErrors());
3222 // Cancels the prerender of a page with its own prerender. The second prerender
3223 // should never be started.
3224 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3225 PrerenderCancelPrerenderWithPrerender
) {
3226 scoped_ptr
<TestPrerender
> prerender
=
3227 PrerenderTestURL("files/prerender/prerender_infinite_a.html",
3228 FINAL_STATUS_CANCELLED
,
3231 GetPrerenderManager()->CancelAllPrerenders();
3232 prerender
->WaitForStop();
3234 EXPECT_FALSE(prerender
->contents());
3237 // Prerendering and history tests.
3238 // The prerendered page is navigated to in several ways [navigate via
3239 // omnibox, click on link, key-modified click to open in background tab, etc],
3240 // followed by a navigation to another page from the prerendered page, followed
3241 // by a back navigation.
3243 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNavigateClickGoBack
) {
3244 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3247 NavigateToDestURL();
3248 ClickToNextPageAfterPrerender();
3249 GoBackToPrerender();
3252 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNavigateNavigateGoBack
) {
3253 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3256 NavigateToDestURL();
3257 NavigateToNextPageAfterPrerender();
3258 GoBackToPrerender();
3261 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickClickGoBack
) {
3262 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3265 OpenDestURLViaClick();
3266 ClickToNextPageAfterPrerender();
3267 GoBackToPrerender();
3270 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickNavigateGoBack
) {
3271 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3274 OpenDestURLViaClick();
3275 NavigateToNextPageAfterPrerender();
3276 GoBackToPrerender();
3279 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickNewWindow
) {
3280 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3281 FINAL_STATUS_APP_TERMINATING
, 1);
3282 OpenDestURLViaClickNewWindow();
3285 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickNewForegroundTab
) {
3286 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3287 FINAL_STATUS_APP_TERMINATING
, 1);
3288 OpenDestURLViaClickNewForegroundTab();
3291 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3292 NavigateToPrerenderedPageWhenDevToolsAttached
) {
3293 DisableJavascriptCalls();
3294 WebContents
* web_contents
=
3295 current_browser()->tab_strip_model()->GetActiveWebContents();
3296 scoped_refptr
<DevToolsAgentHost
> agent(
3297 DevToolsAgentHost::GetOrCreateFor(web_contents
));
3298 FakeDevToolsClient client
;
3299 agent
->AttachClient(&client
);
3300 const char* url
= "files/prerender/prerender_page.html";
3301 PrerenderTestURL(url
, FINAL_STATUS_DEVTOOLS_ATTACHED
, 1);
3302 NavigateToURLWithDisposition(url
, CURRENT_TAB
, false);
3303 agent
->DetachClient();
3306 // Validate that the sessionStorage namespace remains the same when swapping
3307 // in a prerendered page.
3308 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSessionStorage
) {
3309 set_loader_path("files/prerender/prerender_loader_with_session_storage.html");
3310 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
3313 NavigateToDestURL();
3314 GoBackToPageBeforePrerender();
3317 // Checks that the control group works. An XHR PUT cannot be detected in the
3319 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, ControlGroup
) {
3320 RestorePrerenderMode restore_prerender_mode
;
3321 PrerenderManager::SetMode(
3322 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP
);
3323 DisableJavascriptCalls();
3324 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3325 FINAL_STATUS_WOULD_HAVE_BEEN_USED
, 0);
3326 NavigateToDestURL();
3329 // Checks that the control group correctly hits WOULD_HAVE_BEEN_USED
3330 // renderer-initiated navigations. (This verifies that the ShouldFork logic
3331 // behaves correctly.)
3332 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, ControlGroupRendererInitiated
) {
3333 RestorePrerenderMode restore_prerender_mode
;
3334 PrerenderManager::SetMode(
3335 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP
);
3336 DisableJavascriptCalls();
3337 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3338 FINAL_STATUS_WOULD_HAVE_BEEN_USED
, 0);
3339 OpenDestURLViaClick();
3342 // Make sure that the MatchComplete dummy works in the normal case. Once
3343 // a prerender is cancelled because of a script, a dummy must be created to
3344 // account for the MatchComplete case, and it must have a final status of
3345 // FINAL_STATUS_WOULD_HAVE_BEEN_USED.
3346 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, MatchCompleteDummy
) {
3347 RestorePrerenderMode restore_prerender_mode
;
3348 PrerenderManager::SetMode(
3349 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3351 std::vector
<FinalStatus
> expected_final_status_queue
;
3352 expected_final_status_queue
.push_back(FINAL_STATUS_INVALID_HTTP_METHOD
);
3353 expected_final_status_queue
.push_back(FINAL_STATUS_WOULD_HAVE_BEEN_USED
);
3354 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3355 expected_final_status_queue
, 1);
3356 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3357 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3358 histogram_tester().ExpectTotalCount(
3359 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3360 histogram_tester().ExpectTotalCount(
3361 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
3363 NavigateToDestURL();
3364 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3365 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3367 histogram_tester().ExpectTotalCount(
3368 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3371 // Checks that the referrer policy is used when prerendering.
3372 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderReferrerPolicy
) {
3373 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3374 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3377 NavigateToDestURL();
3380 // Checks that the referrer policy is used when prerendering on HTTPS.
3381 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3382 PrerenderSSLReferrerPolicy
) {
3383 UseHttpsSrcServer();
3384 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3385 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3388 NavigateToDestURL();
3391 // Checks that the referrer policy is used when prerendering is cancelled.
3392 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCancelReferrerPolicy
) {
3393 scoped_ptr
<TestContentBrowserClient
> test_content_browser_client(
3394 new TestContentBrowserClient
);
3395 content::ContentBrowserClient
* original_browser_client
=
3396 content::SetBrowserClientForTesting(test_content_browser_client
.get());
3398 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3399 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3400 FINAL_STATUS_CANCELLED
,
3402 OpenDestURLViaClick();
3404 bool display_test_result
= false;
3405 WebContents
* web_contents
=
3406 browser()->tab_strip_model()->GetActiveWebContents();
3407 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
3409 "window.domAutomationController.send(DidDisplayPass())",
3410 &display_test_result
));
3411 EXPECT_TRUE(display_test_result
);
3413 content::SetBrowserClientForTesting(original_browser_client
);
3416 // Test interaction of the webNavigation and tabs API with prerender.
3417 class PrerenderBrowserTestWithExtensions
: public PrerenderBrowserTest
,
3418 public ExtensionApiTest
{
3420 PrerenderBrowserTestWithExtensions() {
3421 // The individual tests start the test server through ExtensionApiTest, so
3422 // the port number can be passed through to the extension.
3423 autostart_test_server_
= false;
3426 void SetUp() override
{ PrerenderBrowserTest::SetUp(); }
3428 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
3429 PrerenderBrowserTest::SetUpCommandLine(command_line
);
3430 ExtensionApiTest::SetUpCommandLine(command_line
);
3433 void SetUpInProcessBrowserTestFixture() override
{
3434 PrerenderBrowserTest::SetUpInProcessBrowserTestFixture();
3435 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
3438 void TearDownInProcessBrowserTestFixture() override
{
3439 PrerenderBrowserTest::TearDownInProcessBrowserTestFixture();
3440 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
3443 void SetUpOnMainThread() override
{
3444 PrerenderBrowserTest::SetUpOnMainThread();
3448 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions
, WebNavigation
) {
3449 ASSERT_TRUE(StartSpawnedTestServer());
3450 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3452 // Wait for the extension to set itself up and return control to us.
3453 ASSERT_TRUE(RunExtensionTest("webnavigation/prerender")) << message_
;
3455 extensions::ResultCatcher catcher
;
3457 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3459 ChannelDestructionWatcher channel_close_watcher
;
3460 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
3461 GetActiveWebContents()->GetRenderProcessHost());
3462 NavigateToDestURL();
3463 channel_close_watcher
.WaitForChannelClose();
3465 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3466 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
3469 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions
, TabsApi
) {
3470 ASSERT_TRUE(StartSpawnedTestServer());
3471 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3473 // Wait for the extension to set itself up and return control to us.
3474 ASSERT_TRUE(RunExtensionTest("tabs/on_replaced")) << message_
;
3476 extensions::ResultCatcher catcher
;
3478 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3480 ChannelDestructionWatcher channel_close_watcher
;
3481 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
3482 GetActiveWebContents()->GetRenderProcessHost());
3483 NavigateToDestURL();
3484 channel_close_watcher
.WaitForChannelClose();
3486 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3487 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
3490 // Test that prerenders abort when navigating to a stream.
3491 // See chrome/browser/extensions/api/streams_private/streams_private_apitest.cc
3492 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions
, StreamsTest
) {
3493 RestorePrerenderMode restore_prerender_mode
;
3494 PrerenderManager::SetMode(
3495 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3497 ASSERT_TRUE(StartSpawnedTestServer());
3499 const extensions::Extension
* extension
= LoadExtension(
3500 test_data_dir_
.AppendASCII("streams_private/handle_mime_type"));
3501 ASSERT_TRUE(extension
);
3502 EXPECT_EQ(std::string(extension_misc::kMimeHandlerPrivateTestExtensionId
),
3504 MimeTypesHandler
* handler
= MimeTypesHandler::GetHandler(extension
);
3505 ASSERT_TRUE(handler
);
3506 EXPECT_TRUE(handler
->CanHandleMIMEType("application/msword"));
3508 PrerenderTestURL("files/prerender/document.doc", FINAL_STATUS_DOWNLOAD
, 0);
3510 // Sanity-check that the extension would have picked up the stream in a normal
3511 // navigation had prerender not intercepted it.
3512 // streams_private/handle_mime_type reports success if it has handled the
3513 // application/msword type.
3514 extensions::ResultCatcher catcher
;
3515 NavigateToDestURL();
3516 EXPECT_TRUE(catcher
.GetNextResult());
3519 // Checks that non-http/https/chrome-extension subresource cancels the
3521 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3522 PrerenderCancelSubresourceUnsupportedScheme
) {
3523 GURL image_url
= GURL("invalidscheme://www.google.com/test.jpg");
3524 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3525 replacement_text
.push_back(
3526 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3527 std::string replacement_path
;
3528 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3529 "files/prerender/prerender_with_image.html",
3531 &replacement_path
));
3532 PrerenderTestURL(replacement_path
, FINAL_STATUS_UNSUPPORTED_SCHEME
, 0);
3535 // Ensure that about:blank is permitted for any subresource.
3536 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3537 PrerenderAllowAboutBlankSubresource
) {
3538 GURL image_url
= GURL("about:blank");
3539 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3540 replacement_text
.push_back(
3541 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3542 std::string replacement_path
;
3543 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3544 "files/prerender/prerender_with_image.html",
3546 &replacement_path
));
3547 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
3548 NavigateToDestURL();
3551 // Checks that non-http/https/chrome-extension subresource cancels the prerender
3553 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3554 PrerenderCancelSubresourceRedirectUnsupportedScheme
) {
3555 GURL image_url
= test_server()->GetURL(
3556 CreateServerRedirect("invalidscheme://www.google.com/test.jpg"));
3557 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3558 replacement_text
.push_back(
3559 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3560 std::string replacement_path
;
3561 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3562 "files/prerender/prerender_with_image.html",
3564 &replacement_path
));
3565 PrerenderTestURL(replacement_path
, FINAL_STATUS_UNSUPPORTED_SCHEME
, 0);
3568 // Checks that chrome-extension subresource does not cancel the prerender.
3569 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3570 PrerenderKeepSubresourceExtensionScheme
) {
3571 GURL image_url
= GURL("chrome-extension://abcdefg/test.jpg");
3572 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3573 replacement_text
.push_back(
3574 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3575 std::string replacement_path
;
3576 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3577 "files/prerender/prerender_with_image.html",
3579 &replacement_path
));
3580 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
3581 NavigateToDestURL();
3584 // Checks that redirect to chrome-extension subresource does not cancel the
3586 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3587 PrerenderKeepSubresourceRedirectExtensionScheme
) {
3588 GURL image_url
= test_server()->GetURL(
3589 CreateServerRedirect("chrome-extension://abcdefg/test.jpg"));
3590 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3591 replacement_text
.push_back(
3592 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3593 std::string replacement_path
;
3594 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3595 "files/prerender/prerender_with_image.html",
3597 &replacement_path
));
3598 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
3599 NavigateToDestURL();
3602 // Checks that non-http/https main page redirects cancel the prerender.
3603 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3604 PrerenderCancelMainFrameRedirectUnsupportedScheme
) {
3605 GURL url
= test_server()->GetURL(
3606 CreateServerRedirect("invalidscheme://www.google.com/test.html"));
3607 PrerenderTestURL(url
, FINAL_STATUS_UNSUPPORTED_SCHEME
, 0);
3610 // Checks that media source video loads are deferred on prerendering.
3611 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5MediaSourceVideo
) {
3612 PrerenderTestURL("files/prerender/prerender_html5_video_media_source.html",
3615 NavigateToDestURL();
3616 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
3619 // Checks that a prerender that creates an audio stream (via a WebAudioDevice)
3621 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderWebAudioDevice
) {
3622 DisableLoadEventCheck();
3623 PrerenderTestURL("files/prerender/prerender_web_audio_device.html",
3624 FINAL_STATUS_CREATING_AUDIO_STREAM
, 0);
3627 // Checks that prerenders do not swap in to WebContents being captured.
3628 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCapturedWebContents
) {
3629 PrerenderTestURL("files/prerender/prerender_page.html",
3630 FINAL_STATUS_PAGE_BEING_CAPTURED
, 1);
3631 WebContents
* web_contents
= GetActiveWebContents();
3632 web_contents
->IncrementCapturerCount(gfx::Size());
3633 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
3634 web_contents
->DecrementCapturerCount();
3637 // Checks that prerenders are aborted on cross-process navigation from
3638 // a server redirect.
3639 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3640 PrerenderCrossProcessServerRedirect
) {
3641 // Force everything to be a process swap.
3642 SwapProcessesContentBrowserClient test_browser_client
;
3643 content::ContentBrowserClient
* original_browser_client
=
3644 content::SetBrowserClientForTesting(&test_browser_client
);
3647 CreateServerRedirect("files/prerender/prerender_page.html"),
3648 FINAL_STATUS_OPEN_URL
, 0);
3650 content::SetBrowserClientForTesting(original_browser_client
);
3653 // Checks that URLRequests for prerenders being aborted on cross-process
3654 // navigation from a server redirect are cleaned up, so they don't keep cache
3656 // See http://crbug.com/341134
3657 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3658 PrerenderCrossProcessServerRedirectNoHang
) {
3659 const char kDestPath
[] = "files/prerender/prerender_page.html";
3660 // Force everything to be a process swap.
3661 SwapProcessesContentBrowserClient test_browser_client
;
3662 content::ContentBrowserClient
* original_browser_client
=
3663 content::SetBrowserClientForTesting(&test_browser_client
);
3665 PrerenderTestURL(CreateServerRedirect(kDestPath
), FINAL_STATUS_OPEN_URL
, 0);
3667 ui_test_utils::NavigateToURL(
3669 test_server()->GetURL(kDestPath
));
3671 content::SetBrowserClientForTesting(original_browser_client
);
3674 // Checks that prerenders are aborted on cross-process navigation from
3675 // a client redirect.
3676 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3677 PrerenderCrossProcessClientRedirect
) {
3678 // Cross-process navigation logic for renderer-initiated navigations
3679 // is partially controlled by the renderer, namely
3680 // ChromeContentRendererClient. This test instead relies on the Web
3681 // Store triggering such navigations.
3682 std::string webstore_url
= extension_urls::GetWebstoreLaunchURL();
3684 // Mock out requests to the Web Store.
3685 base::FilePath
file(GetTestPath("prerender_page.html"));
3686 BrowserThread::PostTask(
3687 BrowserThread::IO
, FROM_HERE
,
3688 base::Bind(&CreateMockInterceptorOnIO
, GURL(webstore_url
), file
));
3690 PrerenderTestURL(CreateClientRedirect(webstore_url
),
3691 FINAL_STATUS_OPEN_URL
, 1);
3694 // Checks that canceling a MatchComplete dummy doesn't result in two
3696 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, CancelMatchCompleteDummy
) {
3697 RestorePrerenderMode restore_prerender_mode
;
3698 PrerenderManager::SetMode(
3699 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3701 std::vector
<FinalStatus
> expected_final_status_queue
;
3702 expected_final_status_queue
.push_back(FINAL_STATUS_JAVASCRIPT_ALERT
);
3703 expected_final_status_queue
.push_back(FINAL_STATUS_CANCELLED
);
3704 ScopedVector
<TestPrerender
> prerenders
=
3705 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
3706 expected_final_status_queue
, 0);
3708 // Cancel the MatchComplete dummy.
3709 GetPrerenderManager()->CancelAllPrerenders();
3710 prerenders
[1]->WaitForStop();
3712 // Check the referring page only got one copy of the event.
3713 EXPECT_FALSE(HadPrerenderEventErrors());
3716 // Checks that a deferred redirect to an image is not loaded until the page is
3717 // visible. Also test the right histogram events are emitted in this case.
3718 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDeferredImage
) {
3719 DisableJavascriptCalls();
3721 // The prerender will not completely load until after the swap, so wait for a
3722 // title change before calling DidPrerenderPass.
3723 scoped_ptr
<TestPrerender
> prerender
=
3725 "files/prerender/prerender_deferred_image.html",
3726 FINAL_STATUS_USED
, 0);
3727 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
3728 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
3729 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
3730 EXPECT_EQ(0, prerender
->number_of_loads());
3731 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3732 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3733 histogram_tester().ExpectTotalCount(
3734 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3735 histogram_tester().ExpectTotalCount(
3736 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3739 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
3740 GetActiveWebContents());
3741 ui_test_utils::NavigateToURLWithDisposition(
3742 current_browser(), dest_url(), CURRENT_TAB
,
3743 ui_test_utils::BROWSER_TEST_NONE
);
3744 swap_observer
.Wait();
3746 // The prerender never observes the final load.
3747 EXPECT_EQ(0, prerender
->number_of_loads());
3749 // Now check DidDisplayPass.
3750 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3752 histogram_tester().ExpectTotalCount(
3753 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3754 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3755 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3757 histogram_tester().ExpectTotalCount(
3758 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3761 // Checks that a deferred redirect to an image is not loaded until the
3762 // page is visible, even after another redirect.
3763 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3764 PrerenderDeferredImageAfterRedirect
) {
3765 DisableJavascriptCalls();
3767 // The prerender will not completely load until after the swap, so wait for a
3768 // title change before calling DidPrerenderPass.
3769 scoped_ptr
<TestPrerender
> prerender
=
3771 "files/prerender/prerender_deferred_image.html",
3772 FINAL_STATUS_USED
, 0);
3773 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
3774 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
3775 EXPECT_EQ(0, prerender
->number_of_loads());
3778 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
3779 GetActiveWebContents());
3780 ui_test_utils::NavigateToURLWithDisposition(
3781 current_browser(), dest_url(), CURRENT_TAB
,
3782 ui_test_utils::BROWSER_TEST_NONE
);
3783 swap_observer
.Wait();
3785 // The prerender never observes the final load.
3786 EXPECT_EQ(0, prerender
->number_of_loads());
3788 // Now check DidDisplayPass.
3789 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3792 // Checks that deferred redirects in the main frame are followed.
3793 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDeferredMainFrame
) {
3794 DisableJavascriptCalls();
3796 "files/prerender/image-deferred.png",
3797 FINAL_STATUS_USED
, 1);
3798 NavigateToDestURL();
3801 // Checks that deferred redirects in the main frame are followed, even
3802 // with a double-redirect.
3803 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3804 PrerenderDeferredMainFrameAfterRedirect
) {
3805 DisableJavascriptCalls();
3807 CreateServerRedirect("files/prerender/image-deferred.png"),
3808 FINAL_STATUS_USED
, 1);
3809 NavigateToDestURL();
3812 // Checks that deferred redirects in a synchronous XHR abort the
3814 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDeferredSynchronousXHR
) {
3815 RestorePrerenderMode restore_prerender_mode
;
3816 PrerenderManager::SetMode(
3817 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3818 PrerenderTestURL("files/prerender/prerender_deferred_sync_xhr.html",
3819 FINAL_STATUS_BAD_DEFERRED_REDIRECT
, 0);
3820 NavigateToDestURL();
3823 // Checks that prerenders are not swapped for navigations with extra headers.
3824 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderExtraHeadersNoSwap
) {
3825 PrerenderTestURL("files/prerender/prerender_page.html",
3826 FINAL_STATUS_APP_TERMINATING
, 1);
3828 content::OpenURLParams
params(dest_url(), Referrer(), CURRENT_TAB
,
3829 ui::PAGE_TRANSITION_TYPED
, false);
3830 params
.extra_headers
= "X-Custom-Header: 42\r\n";
3831 NavigateToURLWithParams(params
, false);
3834 // Checks that prerenders are not swapped for navigations with browser-initiated
3836 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3837 PrerenderBrowserInitiatedPostNoSwap
) {
3838 PrerenderTestURL("files/prerender/prerender_page.html",
3839 FINAL_STATUS_APP_TERMINATING
, 1);
3841 std::string post_data
= "DATA";
3842 content::OpenURLParams
params(dest_url(), Referrer(), CURRENT_TAB
,
3843 ui::PAGE_TRANSITION_TYPED
, false);
3844 params
.uses_post
= true;
3845 params
.browser_initiated_post_data
=
3846 base::RefCountedString::TakeString(&post_data
);
3847 NavigateToURLWithParams(params
, false);
3850 // Checks that the prerendering of a page is canceled correctly when the
3851 // prerendered page tries to make a second navigation entry.
3852 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNewNavigationEntry
) {
3853 PrerenderTestURL("files/prerender/prerender_new_entry.html",
3854 FINAL_STATUS_NEW_NAVIGATION_ENTRY
,
3858 // Attempt a swap-in in a new tab. The session storage doesn't match, so it
3860 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageNewTab
) {
3861 PrerenderTestURL("files/prerender/prerender_page.html",
3862 FINAL_STATUS_APP_TERMINATING
, 1);
3864 // Open a new tab to navigate in.
3865 ui_test_utils::NavigateToURLWithDisposition(
3866 current_browser(), GURL(url::kAboutBlankURL
), NEW_FOREGROUND_TAB
,
3867 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
3869 // Now navigate in the new tab.
3870 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
3873 // Checks that prerenders honor |should_replace_current_entry|.
3874 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderReplaceCurrentEntry
) {
3875 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3877 content::OpenURLParams
params(dest_url(), Referrer(), CURRENT_TAB
,
3878 ui::PAGE_TRANSITION_TYPED
, false);
3879 params
.should_replace_current_entry
= true;
3880 NavigateToURLWithParams(params
, false);
3882 const NavigationController
& controller
=
3883 GetActiveWebContents()->GetController();
3884 // First entry is about:blank, second is prerender_page.html.
3885 EXPECT_TRUE(controller
.GetPendingEntry() == NULL
);
3886 EXPECT_EQ(2, controller
.GetEntryCount());
3887 EXPECT_EQ(GURL(url::kAboutBlankURL
), controller
.GetEntryAtIndex(0)->GetURL());
3888 EXPECT_EQ(dest_url(), controller
.GetEntryAtIndex(1)->GetURL());
3891 // Checks that <a ping> requests are not dropped in prerender.
3892 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPing
) {
3893 // Count hits to a certain URL.
3894 const GURL
kPingURL("http://prerender.test/ping");
3895 base::FilePath empty_file
= ui_test_utils::GetTestFilePath(
3896 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3897 RequestCounter ping_counter
;
3898 BrowserThread::PostTask(
3899 BrowserThread::IO
, FROM_HERE
,
3900 base::Bind(&CreateCountingInterceptorOnIO
,
3901 kPingURL
, empty_file
, ping_counter
.AsWeakPtr()));
3903 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3904 OpenDestURLViaClickPing(kPingURL
);
3906 ping_counter
.WaitForCount(1);
3909 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPPLTNormalNavigation
) {
3910 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
3911 ui_test_utils::NavigateToURL(current_browser(), url
);
3912 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3913 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3914 histogram_tester().ExpectTotalCount(
3915 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3918 // Checks that a prerender which calls window.close() on itself is aborted.
3919 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderWindowClose
) {
3920 DisableLoadEventCheck();
3921 PrerenderTestURL("files/prerender/prerender_window_close.html",
3922 FINAL_STATUS_CLOSED
, 0);
3925 class PrerenderIncognitoBrowserTest
: public PrerenderBrowserTest
{
3927 void SetUpOnMainThread() override
{
3928 Profile
* normal_profile
= current_browser()->profile();
3929 set_browser(ui_test_utils::OpenURLOffTheRecord(
3930 normal_profile
, GURL("about:blank")));
3931 PrerenderBrowserTest::SetUpOnMainThread();
3935 // Checks that prerendering works in incognito mode.
3936 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest
, PrerenderIncognito
) {
3937 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3938 NavigateToDestURL();
3941 // Checks that prerenders are aborted when an incognito profile is closed.
3942 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest
,
3943 PrerenderIncognitoClosed
) {
3944 scoped_ptr
<TestPrerender
> prerender
=
3945 PrerenderTestURL("files/prerender/prerender_page.html",
3946 FINAL_STATUS_PROFILE_DESTROYED
, 1);
3947 current_browser()->window()->Close();
3948 prerender
->WaitForStop();
3951 class PrerenderOmniboxBrowserTest
: public PrerenderBrowserTest
{
3953 LocationBar
* GetLocationBar() {
3954 return current_browser()->window()->GetLocationBar();
3957 OmniboxView
* GetOmniboxView() {
3958 return GetLocationBar()->GetOmniboxView();
3961 void WaitForAutocompleteDone(OmniboxView
* omnibox_view
) {
3962 AutocompleteController
* controller
=
3963 omnibox_view
->model()->popup_model()->autocomplete_controller();
3964 while (!controller
->done()) {
3965 content::WindowedNotificationObserver
ready_observer(
3966 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
3967 content::Source
<AutocompleteController
>(controller
));
3968 ready_observer
.Wait();
3972 predictors::AutocompleteActionPredictor
* GetAutocompleteActionPredictor() {
3973 Profile
* profile
= current_browser()->profile();
3974 return predictors::AutocompleteActionPredictorFactory::GetForProfile(
3978 scoped_ptr
<TestPrerender
> StartOmniboxPrerender(
3980 FinalStatus expected_final_status
) {
3981 scoped_ptr
<TestPrerender
> prerender
=
3982 ExpectPrerender(expected_final_status
);
3983 WebContents
* web_contents
= GetActiveWebContents();
3984 GetAutocompleteActionPredictor()->StartPrerendering(
3986 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
3988 prerender
->WaitForStart();
3989 return prerender
.Pass();
3993 // Checks that closing the omnibox popup cancels an omnibox prerender.
3994 // http://crbug.com/395152
3995 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest
,
3996 DISABLED_PrerenderOmniboxCancel
) {
3997 // Fake an omnibox prerender.
3998 scoped_ptr
<TestPrerender
> prerender
= StartOmniboxPrerender(
3999 test_server()->GetURL("files/empty.html"),
4000 FINAL_STATUS_CANCELLED
);
4002 // Revert the location bar. This should cancel the prerender.
4003 GetLocationBar()->Revert();
4004 prerender
->WaitForStop();
4007 // Checks that accepting omnibox input abandons an omnibox prerender.
4008 // http://crbug.com/394592
4009 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest
,
4010 DISABLED_PrerenderOmniboxAbandon
) {
4011 // Set the abandon timeout to something high so it does not introduce
4012 // flakiness if the prerender times out before the test completes.
4013 GetPrerenderManager()->mutable_config().abandon_time_to_live
=
4014 base::TimeDelta::FromDays(999);
4016 // Enter a URL into the Omnibox.
4017 OmniboxView
* omnibox_view
= GetOmniboxView();
4018 omnibox_view
->OnBeforePossibleChange();
4019 omnibox_view
->SetUserText(
4020 base::UTF8ToUTF16(test_server()->GetURL("files/empty.html?1").spec()));
4021 omnibox_view
->OnAfterPossibleChange();
4022 WaitForAutocompleteDone(omnibox_view
);
4024 // Fake an omnibox prerender for a different URL.
4025 scoped_ptr
<TestPrerender
> prerender
= StartOmniboxPrerender(
4026 test_server()->GetURL("files/empty.html?2"),
4027 FINAL_STATUS_APP_TERMINATING
);
4029 // The final status may be either FINAL_STATUS_APP_TERMINATING or
4030 // FINAL_STATUS_CANCELLED. Although closing the omnibox will not cancel an
4031 // abandoned prerender, the AutocompleteActionPredictor will cancel the
4032 // predictor on destruction.
4033 prerender
->contents()->set_skip_final_checks(true);
4035 // Navigate to the URL entered.
4036 omnibox_view
->model()->AcceptInput(CURRENT_TAB
, false);
4038 // Prerender should be running, but abandoned.
4040 GetAutocompleteActionPredictor()->IsPrerenderAbandonedForTesting());
4043 // Can't run tests with NaCl plugins if built with DISABLE_NACL.
4044 #if !defined(DISABLE_NACL)
4045 class PrerenderBrowserTestWithNaCl
: public PrerenderBrowserTest
{
4047 PrerenderBrowserTestWithNaCl() {}
4048 ~PrerenderBrowserTestWithNaCl() override
{}
4050 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
4051 PrerenderBrowserTest::SetUpCommandLine(command_line
);
4052 command_line
->AppendSwitch(switches::kEnableNaCl
);
4056 // Check that NaCl plugins work when enabled, with prerendering.
4057 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithNaCl
,
4058 PrerenderNaClPluginEnabled
) {
4059 #if defined(OS_WIN) && defined(USE_ASH)
4060 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
4061 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
4062 switches::kAshBrowserTests
))
4066 PrerenderTestURL("files/prerender/prerender_plugin_nacl_enabled.html",
4069 NavigateToDestURL();
4071 // To avoid any chance of a race, we have to let the script send its response
4073 WebContents
* web_contents
=
4074 browser()->tab_strip_model()->GetActiveWebContents();
4075 bool display_test_result
= false;
4076 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents
,
4077 "DidDisplayReallyPass()",
4078 &display_test_result
));
4079 ASSERT_TRUE(display_test_result
);
4081 #endif // !defined(DISABLE_NACL)
4083 } // namespace prerender