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/memory/ref_counted_memory.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/path_service.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/test/histogram_tester.h"
21 #include "base/test/test_timeouts.h"
22 #include "base/values.h"
23 #include "chrome/browser/browsing_data/browsing_data_helper.h"
24 #include "chrome/browser/browsing_data/browsing_data_remover.h"
25 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h"
26 #include "chrome/browser/chrome_content_browser_client.h"
27 #include "chrome/browser/chrome_notification_types.h"
28 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
29 #include "chrome/browser/extensions/extension_apitest.h"
30 #include "chrome/browser/external_protocol/external_protocol_handler.h"
31 #include "chrome/browser/favicon/favicon_tab_helper.h"
32 #include "chrome/browser/net/prediction_options.h"
33 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
34 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
35 #include "chrome/browser/prerender/prerender_contents.h"
36 #include "chrome/browser/prerender/prerender_field_trial.h"
37 #include "chrome/browser/prerender/prerender_handle.h"
38 #include "chrome/browser/prerender/prerender_link_manager.h"
39 #include "chrome/browser/prerender/prerender_link_manager_factory.h"
40 #include "chrome/browser/prerender/prerender_manager.h"
41 #include "chrome/browser/prerender/prerender_manager_factory.h"
42 #include "chrome/browser/profiles/profile.h"
43 #include "chrome/browser/profiles/profile_io_data.h"
44 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
45 #include "chrome/browser/safe_browsing/database_manager.h"
46 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
47 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
48 #include "chrome/browser/task_manager/task_manager.h"
49 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
50 #include "chrome/browser/ui/browser.h"
51 #include "chrome/browser/ui/browser_commands.h"
52 #include "chrome/browser/ui/browser_finder.h"
53 #include "chrome/browser/ui/browser_navigator.h"
54 #include "chrome/browser/ui/browser_window.h"
55 #include "chrome/browser/ui/location_bar/location_bar.h"
56 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
57 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
58 #include "chrome/browser/ui/omnibox/omnibox_view.h"
59 #include "chrome/browser/ui/tabs/tab_strip_model.h"
60 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
61 #include "chrome/common/chrome_paths.h"
62 #include "chrome/common/chrome_switches.h"
63 #include "chrome/common/pref_names.h"
64 #include "chrome/grit/generated_resources.h"
65 #include "chrome/test/base/in_process_browser_test.h"
66 #include "chrome/test/base/test_switches.h"
67 #include "chrome/test/base/ui_test_utils.h"
68 #include "components/content_settings/core/browser/host_content_settings_map.h"
69 #include "components/variations/entropy_provider.h"
70 #include "components/variations/variations_associated_data.h"
71 #include "content/public/browser/browser_message_filter.h"
72 #include "content/public/browser/devtools_agent_host.h"
73 #include "content/public/browser/navigation_controller.h"
74 #include "content/public/browser/navigation_entry.h"
75 #include "content/public/browser/notification_service.h"
76 #include "content/public/browser/render_frame_host.h"
77 #include "content/public/browser/render_process_host.h"
78 #include "content/public/browser/render_view_host.h"
79 #include "content/public/browser/site_instance.h"
80 #include "content/public/browser/web_contents.h"
81 #include "content/public/browser/web_contents_observer.h"
82 #include "content/public/common/url_constants.h"
83 #include "content/public/test/browser_test_utils.h"
84 #include "content/public/test/test_navigation_observer.h"
85 #include "content/public/test/test_utils.h"
86 #include "extensions/common/constants.h"
87 #include "extensions/common/extension_urls.h"
88 #include "extensions/common/manifest_handlers/mime_types_handler.h"
89 #include "extensions/common/switches.h"
90 #include "extensions/test/result_catcher.h"
91 #include "net/base/escape.h"
92 #include "net/cert/x509_certificate.h"
93 #include "net/dns/mock_host_resolver.h"
94 #include "net/ssl/client_cert_store.h"
95 #include "net/ssl/ssl_cert_request_info.h"
96 #include "net/test/url_request/url_request_mock_http_job.h"
97 #include "net/url_request/url_request_context.h"
98 #include "net/url_request/url_request_context_getter.h"
99 #include "net/url_request/url_request_filter.h"
100 #include "net/url_request/url_request_interceptor.h"
101 #include "net/url_request/url_request_job.h"
102 #include "ui/base/l10n/l10n_util.h"
103 #include "url/gurl.h"
105 using chrome_browser_net::NetworkPredictionOptions
;
106 using content::BrowserThread
;
107 using content::DevToolsAgentHost
;
108 using content::NavigationController
;
109 using content::OpenURLParams
;
110 using content::Referrer
;
111 using content::RenderFrameHost
;
112 using content::RenderViewHost
;
113 using content::RenderWidgetHost
;
114 using content::TestNavigationObserver
;
115 using content::WebContents
;
116 using content::WebContentsObserver
;
117 using net::NetworkChangeNotifier
;
118 using task_manager::browsertest_util::WaitForTaskManagerRows
;
120 // Prerender tests work as follows:
122 // A page with a prefetch link to the test page is loaded. Once prerendered,
123 // its Javascript function DidPrerenderPass() is called, which returns true if
124 // the page behaves as expected when prerendered.
126 // The prerendered page is then displayed on a tab. The Javascript function
127 // DidDisplayPass() is called, and returns true if the page behaved as it
128 // should while being displayed.
130 namespace prerender
{
134 class MockNetworkChangeNotifierWIFI
: public NetworkChangeNotifier
{
136 ConnectionType
GetCurrentConnectionType() const override
{
137 return NetworkChangeNotifier::CONNECTION_WIFI
;
141 class MockNetworkChangeNotifier4G
: public NetworkChangeNotifier
{
143 ConnectionType
GetCurrentConnectionType() const override
{
144 return NetworkChangeNotifier::CONNECTION_4G
;
148 // Constants used in the test HTML files.
149 const char* kReadyTitle
= "READY";
150 const char* kPassTitle
= "PASS";
152 std::string
CreateClientRedirect(const std::string
& dest_url
) {
153 const char* const kClientRedirectBase
= "client-redirect?";
154 return kClientRedirectBase
+ net::EscapeQueryParamValue(dest_url
, false);
157 std::string
CreateServerRedirect(const std::string
& dest_url
) {
158 const char* const kServerRedirectBase
= "server-redirect?";
159 return kServerRedirectBase
+ net::EscapeQueryParamValue(dest_url
, false);
162 // Clears the specified data using BrowsingDataRemover.
163 void ClearBrowsingData(Browser
* browser
, int remove_mask
) {
164 BrowsingDataRemover
* remover
=
165 BrowsingDataRemover::CreateForUnboundedRange(browser
->profile());
166 BrowsingDataRemoverCompletionObserver
observer(remover
);
167 remover
->Remove(remove_mask
, BrowsingDataHelper::UNPROTECTED_WEB
);
168 observer
.BlockUntilCompletion();
169 // BrowsingDataRemover deletes itself.
172 // Returns true if the prerender is expected to abort on its own, before
173 // attempting to swap it.
174 bool ShouldAbortPrerenderBeforeSwap(FinalStatus status
) {
176 case FINAL_STATUS_USED
:
177 case FINAL_STATUS_WINDOW_OPENER
:
178 case FINAL_STATUS_APP_TERMINATING
:
179 case FINAL_STATUS_PROFILE_DESTROYED
:
180 case FINAL_STATUS_CACHE_OR_HISTORY_CLEARED
:
181 // We'll crash the renderer after it's loaded.
182 case FINAL_STATUS_RENDERER_CRASHED
:
183 case FINAL_STATUS_CANCELLED
:
184 case FINAL_STATUS_DEVTOOLS_ATTACHED
:
185 case FINAL_STATUS_PAGE_BEING_CAPTURED
:
186 case FINAL_STATUS_NAVIGATION_UNCOMMITTED
:
187 case FINAL_STATUS_WOULD_HAVE_BEEN_USED
:
188 case FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE
:
195 // Convenience function to wait for a title. Handles the case when the
196 // WebContents already has the expected title.
197 void WaitForASCIITitle(WebContents
* web_contents
,
198 const char* expected_title_ascii
) {
199 base::string16 expected_title
= base::ASCIIToUTF16(expected_title_ascii
);
200 if (web_contents
->GetTitle() == expected_title
)
202 content::TitleWatcher
title_watcher(web_contents
, expected_title
);
203 EXPECT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
206 // Waits for the destruction of a RenderProcessHost's IPC channel.
207 // Used to make sure the PrerenderLinkManager's OnChannelClosed function has
208 // been called, before checking its state.
209 class ChannelDestructionWatcher
{
211 ChannelDestructionWatcher() : channel_destroyed_(false) {
214 ~ChannelDestructionWatcher() {
217 void WatchChannel(content::RenderProcessHost
* host
) {
218 host
->AddFilter(new DestructionMessageFilter(this));
221 void WaitForChannelClose() {
223 EXPECT_TRUE(channel_destroyed_
);
227 // When destroyed, calls ChannelDestructionWatcher::OnChannelDestroyed.
228 // Ignores all messages.
229 class DestructionMessageFilter
: public content::BrowserMessageFilter
{
231 explicit DestructionMessageFilter(ChannelDestructionWatcher
* watcher
)
232 : BrowserMessageFilter(0),
237 ~DestructionMessageFilter() override
{
238 content::BrowserThread::PostTask(
239 content::BrowserThread::UI
, FROM_HERE
,
240 base::Bind(&ChannelDestructionWatcher::OnChannelDestroyed
,
241 base::Unretained(watcher_
)));
244 bool OnMessageReceived(const IPC::Message
& message
) override
{
248 ChannelDestructionWatcher
* watcher_
;
250 DISALLOW_COPY_AND_ASSIGN(DestructionMessageFilter
);
253 void OnChannelDestroyed() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
256 EXPECT_FALSE(channel_destroyed_
);
257 channel_destroyed_
= true;
261 bool channel_destroyed_
;
262 base::RunLoop run_loop_
;
264 DISALLOW_COPY_AND_ASSIGN(ChannelDestructionWatcher
);
267 // A navigation observer to wait on either a new load or a swap of a
268 // WebContents. On swap, if the new WebContents is still loading, wait for that
269 // load to complete as well. Note that the load must begin after the observer is
271 class NavigationOrSwapObserver
: public WebContentsObserver
,
272 public TabStripModelObserver
{
274 // Waits for either a new load or a swap of |tab_strip_model|'s active
276 NavigationOrSwapObserver(TabStripModel
* tab_strip_model
,
277 WebContents
* web_contents
)
278 : WebContentsObserver(web_contents
),
279 tab_strip_model_(tab_strip_model
),
280 did_start_loading_(false),
281 number_of_loads_(1) {
282 CHECK_NE(TabStripModel::kNoTab
,
283 tab_strip_model
->GetIndexOfWebContents(web_contents
));
284 tab_strip_model_
->AddObserver(this);
287 // Waits for either |number_of_loads| loads or a swap of |tab_strip_model|'s
288 // active WebContents.
289 NavigationOrSwapObserver(TabStripModel
* tab_strip_model
,
290 WebContents
* web_contents
,
292 : WebContentsObserver(web_contents
),
293 tab_strip_model_(tab_strip_model
),
294 did_start_loading_(false),
295 number_of_loads_(number_of_loads
) {
296 CHECK_NE(TabStripModel::kNoTab
,
297 tab_strip_model
->GetIndexOfWebContents(web_contents
));
298 tab_strip_model_
->AddObserver(this);
301 ~NavigationOrSwapObserver() override
{
302 tab_strip_model_
->RemoveObserver(this);
305 void set_did_start_loading() {
306 did_start_loading_
= true;
313 // WebContentsObserver implementation:
314 void DidStartLoading(RenderViewHost
* render_view_host
) override
{
315 did_start_loading_
= true;
317 void DidStopLoading(RenderViewHost
* render_view_host
) override
{
318 if (!did_start_loading_
)
321 if (number_of_loads_
== 0)
325 // TabStripModelObserver implementation:
326 void TabReplacedAt(TabStripModel
* tab_strip_model
,
327 WebContents
* old_contents
,
328 WebContents
* new_contents
,
329 int index
) override
{
330 if (old_contents
!= web_contents())
332 // Switch to observing the new WebContents.
333 Observe(new_contents
);
334 if (new_contents
->IsLoading()) {
335 // If the new WebContents is still loading, wait for it to complete. Only
336 // one load post-swap is supported.
337 did_start_loading_
= true;
338 number_of_loads_
= 1;
345 TabStripModel
* tab_strip_model_
;
346 bool did_start_loading_
;
347 int number_of_loads_
;
351 // Waits for a new tab to open and a navigation or swap in it.
352 class NewTabNavigationOrSwapObserver
{
354 NewTabNavigationOrSwapObserver()
356 chrome::NOTIFICATION_TAB_ADDED
,
357 base::Bind(&NewTabNavigationOrSwapObserver::OnTabAdded
,
358 base::Unretained(this))) {
359 // Watch for NOTIFICATION_TAB_ADDED. Add a callback so that the
360 // NavigationOrSwapObserver can be attached synchronously and no events are
365 new_tab_observer_
.Wait();
366 swap_observer_
->Wait();
369 bool OnTabAdded(const content::NotificationSource
& source
,
370 const content::NotificationDetails
& details
) {
373 WebContents
* new_tab
= content::Details
<WebContents
>(details
).ptr();
374 // Get the TabStripModel. Assume this is attached to a Browser.
375 TabStripModel
* tab_strip_model
=
376 static_cast<Browser
*>(new_tab
->GetDelegate())->tab_strip_model();
377 swap_observer_
.reset(new NavigationOrSwapObserver(tab_strip_model
,
379 swap_observer_
->set_did_start_loading();
384 content::WindowedNotificationObserver new_tab_observer_
;
385 scoped_ptr
<NavigationOrSwapObserver
> swap_observer_
;
388 // PrerenderContents that stops the UI message loop on DidStopLoading().
389 class TestPrerenderContents
: public PrerenderContents
{
391 TestPrerenderContents(
392 PrerenderManager
* prerender_manager
,
395 const content::Referrer
& referrer
,
397 FinalStatus expected_final_status
)
398 : PrerenderContents(prerender_manager
, profile
, url
,
399 referrer
, origin
, PrerenderManager::kNoExperiment
),
400 expected_final_status_(expected_final_status
),
401 new_render_view_host_(NULL
),
404 should_be_shown_(expected_final_status
== FINAL_STATUS_USED
),
405 skip_final_checks_(false) {
408 ~TestPrerenderContents() override
{
409 if (skip_final_checks_
)
412 if (expected_final_status_
== FINAL_STATUS_MAX
) {
413 EXPECT_EQ(MATCH_COMPLETE_REPLACEMENT
, match_complete_status());
415 EXPECT_EQ(expected_final_status_
, final_status()) <<
416 " when testing URL " << prerender_url().path() <<
417 " (Expected: " << NameFromFinalStatus(expected_final_status_
) <<
418 ", Actual: " << NameFromFinalStatus(final_status()) << ")";
420 // Prerendering RenderViewHosts should be hidden before the first
421 // navigation, so this should be happen for every PrerenderContents for
422 // which a RenderViewHost is created, regardless of whether or not it's
424 if (new_render_view_host_
)
425 EXPECT_TRUE(was_hidden_
);
427 // A used PrerenderContents will only be destroyed when we swap out
428 // WebContents, at the end of a navigation caused by a call to
429 // NavigateToURLImpl().
430 if (final_status() == FINAL_STATUS_USED
)
431 EXPECT_TRUE(new_render_view_host_
);
433 EXPECT_EQ(should_be_shown_
, was_shown_
);
436 void RenderProcessGone(base::TerminationStatus status
) override
{
437 // On quit, it's possible to end up here when render processes are closed
438 // before the PrerenderManager is destroyed. As a result, it's possible to
439 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
442 // It's also possible for this to be called after we've been notified of
443 // app termination, but before we've been deleted, which is why the second
445 if (expected_final_status_
== FINAL_STATUS_APP_TERMINATING
&&
446 final_status() != expected_final_status_
) {
447 expected_final_status_
= FINAL_STATUS_RENDERER_CRASHED
;
450 PrerenderContents::RenderProcessGone(status
);
453 bool CheckURL(const GURL
& url
) override
{
454 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
455 // the PrerenderRendererCrash test.
456 if (url
.spec() != content::kChromeUICrashURL
)
457 return PrerenderContents::CheckURL(url
);
461 // For tests that open the prerender in a new background tab, the RenderView
462 // will not have been made visible when the PrerenderContents is destroyed
463 // even though it is used.
464 void set_should_be_shown(bool value
) { should_be_shown_
= value
; }
466 // For tests which do not know whether the prerender will be used.
467 void set_skip_final_checks(bool value
) { skip_final_checks_
= value
; }
469 FinalStatus
expected_final_status() const { return expected_final_status_
; }
472 void OnRenderViewHostCreated(RenderViewHost
* new_render_view_host
) override
{
473 // Used to make sure the RenderViewHost is hidden and, if used,
474 // subsequently shown.
475 notification_registrar().Add(
477 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
,
478 content::Source
<RenderWidgetHost
>(new_render_view_host
));
480 new_render_view_host_
= new_render_view_host
;
482 PrerenderContents::OnRenderViewHostCreated(new_render_view_host
);
485 void Observe(int type
,
486 const content::NotificationSource
& source
,
487 const content::NotificationDetails
& details
) override
{
489 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED
) {
490 EXPECT_EQ(new_render_view_host_
,
491 content::Source
<RenderWidgetHost
>(source
).ptr());
492 bool is_visible
= *content::Details
<bool>(details
).ptr();
496 } else if (is_visible
&& was_hidden_
) {
497 // Once hidden, a prerendered RenderViewHost should only be shown after
498 // being removed from the PrerenderContents for display.
499 EXPECT_FALSE(GetRenderViewHost());
504 PrerenderContents::Observe(type
, source
, details
);
507 FinalStatus expected_final_status_
;
509 // The RenderViewHost created for the prerender, if any.
510 RenderViewHost
* new_render_view_host_
;
511 // Set to true when the prerendering RenderWidget is hidden.
513 // Set to true when the prerendering RenderWidget is shown, after having been
516 // Expected final value of was_shown_. Defaults to true for
517 // FINAL_STATUS_USED, and false otherwise.
518 bool should_be_shown_
;
519 // If true, |expected_final_status_| and other shutdown checks are skipped.
520 bool skip_final_checks_
;
523 // A handle to a TestPrerenderContents whose lifetime is under the caller's
524 // control. A PrerenderContents may be destroyed at any point. This allows
525 // tracking the final status, etc.
526 class TestPrerender
: public PrerenderContents::Observer
,
527 public base::SupportsWeakPtr
<TestPrerender
> {
532 expected_number_of_loads_(0) {
534 ~TestPrerender() override
{
536 contents_
->RemoveObserver(this);
539 TestPrerenderContents
* contents() const { return contents_
; }
540 int number_of_loads() const { return number_of_loads_
; }
542 void WaitForCreate() { create_loop_
.Run(); }
543 void WaitForStart() { start_loop_
.Run(); }
544 void WaitForStop() { stop_loop_
.Run(); }
546 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR
547 // for the prerender to stop running (just to avoid a timeout if the prerender
548 // dies). Note: this does not assert equality on the number of loads; the
549 // caller must do it instead.
550 void WaitForLoads(int expected_number_of_loads
) {
551 DCHECK(!load_waiter_
);
552 DCHECK(!expected_number_of_loads_
);
553 if (number_of_loads_
< expected_number_of_loads
) {
554 load_waiter_
.reset(new base::RunLoop
);
555 expected_number_of_loads_
= expected_number_of_loads
;
557 load_waiter_
.reset();
558 expected_number_of_loads_
= 0;
560 EXPECT_LE(expected_number_of_loads
, number_of_loads_
);
563 void OnPrerenderCreated(TestPrerenderContents
* contents
) {
565 contents_
= contents
;
566 contents_
->AddObserver(this);
570 // PrerenderContents::Observer implementation:
571 void OnPrerenderStart(PrerenderContents
* contents
) override
{
575 void OnPrerenderStopLoading(PrerenderContents
* contents
) override
{
577 if (load_waiter_
&& number_of_loads_
>= expected_number_of_loads_
)
578 load_waiter_
->Quit();
581 void OnPrerenderStop(PrerenderContents
* contents
) override
{
585 // If there is a WaitForLoads call and it has yet to see the expected number
586 // of loads, stop the loop so the test fails instead of timing out.
588 load_waiter_
->Quit();
591 void OnPrerenderCreatedMatchCompleteReplacement(
592 PrerenderContents
* contents
,
593 PrerenderContents
* replacement
) override
{}
596 TestPrerenderContents
* contents_
;
597 int number_of_loads_
;
599 int expected_number_of_loads_
;
600 scoped_ptr
<base::RunLoop
> load_waiter_
;
602 base::RunLoop create_loop_
;
603 base::RunLoop start_loop_
;
604 base::RunLoop stop_loop_
;
606 DISALLOW_COPY_AND_ASSIGN(TestPrerender
);
609 // PrerenderManager that uses TestPrerenderContents.
610 class TestPrerenderContentsFactory
: public PrerenderContents::Factory
{
612 TestPrerenderContentsFactory() {}
614 ~TestPrerenderContentsFactory() override
{
615 EXPECT_TRUE(expected_contents_queue_
.empty());
618 scoped_ptr
<TestPrerender
> ExpectPrerenderContents(FinalStatus final_status
) {
619 scoped_ptr
<TestPrerender
> handle(new TestPrerender());
620 expected_contents_queue_
.push_back(
621 ExpectedContents(final_status
, handle
->AsWeakPtr()));
622 return handle
.Pass();
625 PrerenderContents
* CreatePrerenderContents(
626 PrerenderManager
* prerender_manager
,
629 const content::Referrer
& referrer
,
631 uint8 experiment_id
) override
{
632 ExpectedContents expected
;
633 if (!expected_contents_queue_
.empty()) {
634 expected
= expected_contents_queue_
.front();
635 expected_contents_queue_
.pop_front();
637 VLOG(1) << "Creating prerender contents for " << url
.path() <<
638 " with expected final status " << expected
.final_status
;
639 VLOG(1) << expected_contents_queue_
.size() << " left in the queue.";
640 TestPrerenderContents
* contents
=
641 new TestPrerenderContents(prerender_manager
,
642 profile
, url
, referrer
, origin
,
643 expected
.final_status
);
645 expected
.handle
->OnPrerenderCreated(contents
);
650 struct ExpectedContents
{
651 ExpectedContents() : final_status(FINAL_STATUS_MAX
) { }
652 ExpectedContents(FinalStatus final_status
,
653 const base::WeakPtr
<TestPrerender
>& handle
)
654 : final_status(final_status
),
658 FinalStatus final_status
;
659 base::WeakPtr
<TestPrerender
> handle
;
662 std::deque
<ExpectedContents
> expected_contents_queue_
;
665 #if defined(FULL_SAFE_BROWSING)
666 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for
668 class FakeSafeBrowsingDatabaseManager
: public SafeBrowsingDatabaseManager
{
670 explicit FakeSafeBrowsingDatabaseManager(SafeBrowsingService
* service
)
671 : SafeBrowsingDatabaseManager(service
),
672 threat_type_(SB_THREAT_TYPE_SAFE
) { }
674 // Called on the IO thread to check if the given url is safe or not. If we
675 // can synchronously determine that the url is safe, CheckUrl returns true.
676 // Otherwise it returns false, and "client" is called asynchronously with the
677 // result when it is ready.
678 // Returns true, indicating a SAFE result, unless the URL is the fixed URL
679 // specified by the user, and the user-specified result is not SAFE
680 // (in which that result will be communicated back via a call into the
681 // client, and false will be returned).
682 // Overrides SafeBrowsingService::CheckBrowseUrl.
683 bool CheckBrowseUrl(const GURL
& gurl
, Client
* client
) override
{
684 if (gurl
!= url_
|| threat_type_
== SB_THREAT_TYPE_SAFE
)
687 BrowserThread::PostTask(
688 BrowserThread::IO
, FROM_HERE
,
689 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone
,
690 this, gurl
, client
));
694 void SetThreatTypeForUrl(const GURL
& url
, SBThreatType threat_type
) {
696 threat_type_
= threat_type
;
700 ~FakeSafeBrowsingDatabaseManager() override
{}
702 void OnCheckBrowseURLDone(const GURL
& gurl
, Client
* client
) {
703 std::vector
<SBThreatType
> expected_threats
;
704 expected_threats
.push_back(SB_THREAT_TYPE_URL_MALWARE
);
705 expected_threats
.push_back(SB_THREAT_TYPE_URL_PHISHING
);
706 SafeBrowsingDatabaseManager::SafeBrowsingCheck
sb_check(
707 std::vector
<GURL
>(1, gurl
),
708 std::vector
<SBFullHash
>(),
710 safe_browsing_util::MALWARE
,
712 sb_check
.url_results
[0] = threat_type_
;
713 client
->OnSafeBrowsingResult(sb_check
);
717 SBThreatType threat_type_
;
718 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager
);
721 class FakeSafeBrowsingService
: public SafeBrowsingService
{
723 FakeSafeBrowsingService() { }
725 // Returned pointer has the same lifespan as the database_manager_ refcounted
727 FakeSafeBrowsingDatabaseManager
* fake_database_manager() {
728 return fake_database_manager_
;
732 ~FakeSafeBrowsingService() override
{}
734 SafeBrowsingDatabaseManager
* CreateDatabaseManager() override
{
735 fake_database_manager_
= new FakeSafeBrowsingDatabaseManager(this);
736 return fake_database_manager_
;
740 FakeSafeBrowsingDatabaseManager
* fake_database_manager_
;
742 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService
);
745 // Factory that creates FakeSafeBrowsingService instances.
746 class TestSafeBrowsingServiceFactory
: public SafeBrowsingServiceFactory
{
748 TestSafeBrowsingServiceFactory() :
749 most_recent_service_(NULL
) { }
750 ~TestSafeBrowsingServiceFactory() override
{}
752 SafeBrowsingService
* CreateSafeBrowsingService() override
{
753 most_recent_service_
= new FakeSafeBrowsingService();
754 return most_recent_service_
;
757 FakeSafeBrowsingService
* most_recent_service() const {
758 return most_recent_service_
;
762 FakeSafeBrowsingService
* most_recent_service_
;
766 class FakeDevToolsClient
: public content::DevToolsAgentHostClient
{
768 FakeDevToolsClient() {}
769 ~FakeDevToolsClient() override
{}
770 void DispatchProtocolMessage(DevToolsAgentHost
* agent_host
,
771 const std::string
& message
) override
{}
772 void AgentHostClosed(DevToolsAgentHost
* agent_host
, bool replaced
) override
{}
775 class RestorePrerenderMode
{
777 RestorePrerenderMode() : prev_mode_(PrerenderManager::GetMode()) {
780 ~RestorePrerenderMode() { PrerenderManager::SetMode(prev_mode_
); }
782 PrerenderManager::PrerenderManagerMode prev_mode_
;
785 // URLRequestJob (and associated handler) which hangs.
786 class HangingURLRequestJob
: public net::URLRequestJob
{
788 HangingURLRequestJob(net::URLRequest
* request
,
789 net::NetworkDelegate
* network_delegate
)
790 : net::URLRequestJob(request
, network_delegate
) {
793 void Start() override
{}
796 ~HangingURLRequestJob() override
{}
799 class HangingFirstRequestInterceptor
: public net::URLRequestInterceptor
{
801 HangingFirstRequestInterceptor(const base::FilePath
& file
,
802 base::Closure callback
)
807 ~HangingFirstRequestInterceptor() override
{}
809 net::URLRequestJob
* MaybeInterceptRequest(
810 net::URLRequest
* request
,
811 net::NetworkDelegate
* network_delegate
) const override
{
814 if (!callback_
.is_null()) {
815 BrowserThread::PostTask(
816 BrowserThread::UI
, FROM_HERE
, callback_
);
818 return new HangingURLRequestJob(request
, network_delegate
);
820 return new net::URLRequestMockHTTPJob(
824 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
825 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
829 base::FilePath file_
;
830 base::Closure callback_
;
831 mutable bool first_run_
;
834 // Makes |url| never respond on the first load, and then with the contents of
835 // |file| afterwards. When the first load has been scheduled, runs |callback| on
837 void CreateHangingFirstRequestInterceptorOnIO(
838 const GURL
& url
, const base::FilePath
& file
, base::Closure callback
) {
839 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
840 scoped_ptr
<net::URLRequestInterceptor
> never_respond_handler(
841 new HangingFirstRequestInterceptor(file
, callback
));
842 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
843 url
, never_respond_handler
.Pass());
846 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks.
847 class MockHTTPJob
: public net::URLRequestMockHTTPJob
{
849 MockHTTPJob(net::URLRequest
* request
,
850 net::NetworkDelegate
* delegate
,
851 const base::FilePath
& file
)
852 : net::URLRequestMockHTTPJob(
856 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
857 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
)) {}
859 void set_start_callback(const base::Closure
& start_callback
) {
860 start_callback_
= start_callback
;
863 void Start() override
{
864 if (!start_callback_
.is_null())
865 start_callback_
.Run();
866 net::URLRequestMockHTTPJob::Start();
870 ~MockHTTPJob() override
{}
872 base::Closure start_callback_
;
875 // Dummy counter class to live on the UI thread for counting requests.
876 class RequestCounter
: public base::SupportsWeakPtr
<RequestCounter
> {
878 RequestCounter() : count_(0), expected_count_(-1) {}
879 int count() const { return count_
; }
881 void RequestStarted() {
883 if (loop_
&& count_
== expected_count_
)
887 void WaitForCount(int expected_count
) {
889 ASSERT_EQ(-1, expected_count_
);
890 if (count_
< expected_count
) {
891 expected_count_
= expected_count
;
892 loop_
.reset(new base::RunLoop
);
894 expected_count_
= -1;
898 EXPECT_EQ(expected_count
, count_
);
903 scoped_ptr
<base::RunLoop
> loop_
;
906 // Protocol handler which counts the number of requests that start.
907 class CountingInterceptor
: public net::URLRequestInterceptor
{
909 CountingInterceptor(const base::FilePath
& file
,
910 const base::WeakPtr
<RequestCounter
>& counter
)
913 weak_factory_(this) {
915 ~CountingInterceptor() override
{}
917 net::URLRequestJob
* MaybeInterceptRequest(
918 net::URLRequest
* request
,
919 net::NetworkDelegate
* network_delegate
) const override
{
920 MockHTTPJob
* job
= new MockHTTPJob(request
, network_delegate
, file_
);
921 job
->set_start_callback(base::Bind(&CountingInterceptor::RequestStarted
,
922 weak_factory_
.GetWeakPtr()));
926 void RequestStarted() {
927 BrowserThread::PostTask(
928 BrowserThread::UI
, FROM_HERE
,
929 base::Bind(&RequestCounter::RequestStarted
, counter_
));
933 base::FilePath file_
;
934 base::WeakPtr
<RequestCounter
> counter_
;
935 mutable base::WeakPtrFactory
<CountingInterceptor
> weak_factory_
;
938 // Makes |url| respond to requests with the contents of |file|, counting the
939 // number that start in |counter|.
940 void CreateCountingInterceptorOnIO(
942 const base::FilePath
& file
,
943 const base::WeakPtr
<RequestCounter
>& counter
) {
944 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
945 scoped_ptr
<net::URLRequestInterceptor
> request_interceptor(
946 new CountingInterceptor(file
, counter
));
947 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
948 url
, request_interceptor
.Pass());
951 // Makes |url| respond to requests with the contents of |file|.
952 void CreateMockInterceptorOnIO(const GURL
& url
, const base::FilePath
& file
) {
953 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
954 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
956 net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
957 file
, BrowserThread::GetBlockingPool()));
960 // A ContentBrowserClient that cancels all prerenderers on OpenURL.
961 class TestContentBrowserClient
: public chrome::ChromeContentBrowserClient
{
963 TestContentBrowserClient() {}
964 ~TestContentBrowserClient() override
{}
966 // chrome::ChromeContentBrowserClient implementation.
967 bool ShouldAllowOpenURL(content::SiteInstance
* site_instance
,
968 const GURL
& url
) override
{
969 PrerenderManagerFactory::GetForProfile(
970 Profile::FromBrowserContext(site_instance
->GetBrowserContext()))
971 ->CancelAllPrerenders();
972 return chrome::ChromeContentBrowserClient::ShouldAllowOpenURL(site_instance
,
977 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient
);
980 // A ContentBrowserClient that forces cross-process navigations.
981 class SwapProcessesContentBrowserClient
982 : public chrome::ChromeContentBrowserClient
{
984 SwapProcessesContentBrowserClient() {}
985 ~SwapProcessesContentBrowserClient() override
{}
987 // chrome::ChromeContentBrowserClient implementation.
988 bool ShouldSwapProcessesForRedirect(
989 content::ResourceContext
* resource_context
,
990 const GURL
& current_url
,
991 const GURL
& new_url
) override
{
996 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient
);
999 // An ExternalProtocolHandler that blocks everything and asserts it never is
1001 class NeverRunsExternalProtocolHandlerDelegate
1002 : public ExternalProtocolHandler::Delegate
{
1004 // ExternalProtocolHandler::Delegate implementation.
1005 ShellIntegration::DefaultProtocolClientWorker
* CreateShellWorker(
1006 ShellIntegration::DefaultWebClientObserver
* observer
,
1007 const std::string
& protocol
) override
{
1009 // This will crash, but it shouldn't get this far with BlockState::BLOCK
1013 ExternalProtocolHandler::BlockState
GetBlockState(
1014 const std::string
& scheme
) override
{
1015 // Block everything and fail the test.
1017 return ExternalProtocolHandler::BLOCK
;
1019 void BlockRequest() override
{}
1020 void RunExternalProtocolDialog(const GURL
& url
,
1021 int render_process_host_id
,
1022 int routing_id
) override
{
1025 void LaunchUrlWithoutSecurityCheck(const GURL
& url
) override
{ NOTREACHED(); }
1026 void FinishedProcessingCheck() override
{ NOTREACHED(); }
1029 base::FilePath
GetTestPath(const std::string
& file_name
) {
1030 return ui_test_utils::GetTestFilePath(
1031 base::FilePath(FILE_PATH_LITERAL("prerender")),
1032 base::FilePath().AppendASCII(file_name
));
1037 // Many of these tests are flaky. See http://crbug.com/249179
1038 class PrerenderBrowserTest
: virtual public InProcessBrowserTest
{
1040 PrerenderBrowserTest()
1041 : autostart_test_server_(true),
1042 prerender_contents_factory_(NULL
),
1043 #if defined(FULL_SAFE_BROWSING)
1044 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
1046 call_javascript_(true),
1047 check_load_events_(true),
1048 loader_path_("files/prerender/prerender_loader.html"),
1049 explicitly_set_browser_(NULL
) {}
1051 ~PrerenderBrowserTest() override
{}
1053 content::SessionStorageNamespace
* GetSessionStorageNamespace() const {
1054 WebContents
* web_contents
= GetActiveWebContents();
1057 return web_contents
->GetController().GetDefaultSessionStorageNamespace();
1060 void SetUpInProcessBrowserTestFixture() override
{
1061 #if defined(FULL_SAFE_BROWSING)
1062 SafeBrowsingService::RegisterFactory(safe_browsing_factory_
.get());
1066 void TearDownInProcessBrowserTestFixture() override
{
1067 #if defined(FULL_SAFE_BROWSING)
1068 SafeBrowsingService::RegisterFactory(NULL
);
1072 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
1073 command_line
->AppendSwitchASCII(switches::kPrerenderMode
,
1074 switches::kPrerenderModeSwitchValueEnabled
);
1075 #if defined(OS_MACOSX)
1076 // The plugins directory isn't read by default on the Mac, so it needs to be
1077 // explicitly registered.
1078 base::FilePath app_dir
;
1079 PathService::Get(chrome::DIR_APP
, &app_dir
);
1080 command_line
->AppendSwitchPath(
1081 switches::kExtraPluginDir
,
1082 app_dir
.Append(FILE_PATH_LITERAL("plugins")));
1084 command_line
->AppendSwitch(switches::kAlwaysAuthorizePlugins
);
1085 command_line
->AppendSwitch(switches::kEnableNpapi
);
1088 void SetPreference(NetworkPredictionOptions value
) {
1089 browser()->profile()->GetPrefs()->SetInteger(
1090 prefs::kNetworkPredictionOptions
, value
);
1093 void CreateTestFieldTrial(const std::string
& name
,
1094 const std::string
& group_name
) {
1095 base::FieldTrial
* trial
= base::FieldTrialList::CreateFieldTrial(
1100 // Verifies, for the current field trial, whether
1101 // ShouldDisableLocalPredictorDueToPreferencesAndNetwork produces the desired
1103 void TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
1104 bool preference_wifi_network_wifi
,
1105 bool preference_wifi_network_4g
,
1106 bool preference_always_network_wifi
,
1107 bool preference_always_network_4g
,
1108 bool preference_never_network_wifi
,
1109 bool preference_never_network_4g
) {
1110 Profile
* profile
= browser()->profile();
1112 // Set real NetworkChangeNotifier singleton aside.
1113 scoped_ptr
<NetworkChangeNotifier::DisableForTest
> disable_for_test(
1114 new NetworkChangeNotifier::DisableForTest
);
1116 // Set preference to WIFI_ONLY: prefetch when not on cellular.
1117 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY
);
1119 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifierWIFI
);
1121 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile
),
1122 preference_wifi_network_wifi
);
1125 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifier4G
);
1127 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile
),
1128 preference_wifi_network_4g
);
1131 // Set preference to ALWAYS: always prefetch.
1132 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS
);
1134 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifierWIFI
);
1136 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile
),
1137 preference_always_network_wifi
);
1140 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifier4G
);
1142 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile
),
1143 preference_always_network_4g
);
1146 // Set preference to NEVER: never prefetch.
1147 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER
);
1149 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifierWIFI
);
1151 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile
),
1152 preference_never_network_wifi
);
1155 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifier4G
);
1157 ShouldDisableLocalPredictorDueToPreferencesAndNetwork(profile
),
1158 preference_never_network_4g
);
1162 void SetUpOnMainThread() override
{
1163 current_browser()->profile()->GetPrefs()->SetBoolean(
1164 prefs::kPromptForDownload
, false);
1165 IncreasePrerenderMemory();
1166 if (autostart_test_server_
)
1167 ASSERT_TRUE(test_server()->Start());
1168 ChromeResourceDispatcherHostDelegate::
1169 SetExternalProtocolHandlerDelegateForTesting(
1170 &external_protocol_handler_delegate_
);
1172 PrerenderManager
* prerender_manager
= GetPrerenderManager();
1173 ASSERT_TRUE(prerender_manager
);
1174 prerender_manager
->mutable_config().rate_limit_enabled
= false;
1175 ASSERT_TRUE(prerender_contents_factory_
== NULL
);
1176 prerender_contents_factory_
= new TestPrerenderContentsFactory
;
1177 prerender_manager
->SetPrerenderContentsFactory(prerender_contents_factory_
);
1180 // Convenience function to get the currently active WebContents in
1181 // current_browser().
1182 WebContents
* GetActiveWebContents() const {
1183 return current_browser()->tab_strip_model()->GetActiveWebContents();
1186 // Overload for a single expected final status
1187 scoped_ptr
<TestPrerender
> PrerenderTestURL(
1188 const std::string
& html_file
,
1189 FinalStatus expected_final_status
,
1190 int expected_number_of_loads
) {
1191 GURL url
= test_server()->GetURL(html_file
);
1192 return PrerenderTestURL(url
,
1193 expected_final_status
,
1194 expected_number_of_loads
);
1197 ScopedVector
<TestPrerender
> PrerenderTestURL(
1198 const std::string
& html_file
,
1199 const std::vector
<FinalStatus
>& expected_final_status_queue
,
1200 int expected_number_of_loads
) {
1201 GURL url
= test_server()->GetURL(html_file
);
1202 return PrerenderTestURLImpl(url
,
1203 expected_final_status_queue
,
1204 expected_number_of_loads
);
1207 scoped_ptr
<TestPrerender
> PrerenderTestURL(
1209 FinalStatus expected_final_status
,
1210 int expected_number_of_loads
) {
1211 std::vector
<FinalStatus
> expected_final_status_queue(
1212 1, expected_final_status
);
1213 std::vector
<TestPrerender
*> prerenders
;
1214 PrerenderTestURLImpl(url
,
1215 expected_final_status_queue
,
1216 expected_number_of_loads
).release(&prerenders
);
1217 CHECK_EQ(1u, prerenders
.size());
1218 return scoped_ptr
<TestPrerender
>(prerenders
[0]);
1221 // Navigates to a URL, unrelated to prerendering
1222 void NavigateStraightToURL(const std::string dest_html_file
) {
1223 ui_test_utils::NavigateToURL(current_browser(),
1224 test_server()->GetURL(dest_html_file
));
1227 void NavigateToDestURL() const {
1228 NavigateToDestURLWithDisposition(CURRENT_TAB
, true);
1231 // Opens the url in a new tab, with no opener.
1232 void NavigateToDestURLWithDisposition(
1233 WindowOpenDisposition disposition
,
1234 bool expect_swap_to_succeed
) const {
1235 NavigateToURLWithParams(
1236 content::OpenURLParams(dest_url_
, Referrer(), disposition
,
1237 ui::PAGE_TRANSITION_TYPED
, false),
1238 expect_swap_to_succeed
);
1241 void NavigateToURL(const std::string
& dest_html_file
) const {
1242 NavigateToURLWithDisposition(dest_html_file
, CURRENT_TAB
, true);
1245 void NavigateToURLWithDisposition(const std::string
& dest_html_file
,
1246 WindowOpenDisposition disposition
,
1247 bool expect_swap_to_succeed
) const {
1248 GURL dest_url
= test_server()->GetURL(dest_html_file
);
1249 NavigateToURLWithDisposition(dest_url
, disposition
, expect_swap_to_succeed
);
1252 void NavigateToURLWithDisposition(const GURL
& dest_url
,
1253 WindowOpenDisposition disposition
,
1254 bool expect_swap_to_succeed
) const {
1255 NavigateToURLWithParams(
1256 content::OpenURLParams(dest_url
, Referrer(), disposition
,
1257 ui::PAGE_TRANSITION_TYPED
, false),
1258 expect_swap_to_succeed
);
1261 void NavigateToURLWithParams(const content::OpenURLParams
& params
,
1262 bool expect_swap_to_succeed
) const {
1263 NavigateToURLImpl(params
, expect_swap_to_succeed
);
1266 void OpenDestURLViaClick() const {
1267 OpenURLViaClick(dest_url_
);
1270 void OpenURLViaClick(const GURL
& url
) const {
1271 OpenURLWithJSImpl("Click", url
, GURL(), false);
1274 void OpenDestURLViaClickTarget() const {
1275 OpenURLWithJSImpl("ClickTarget", dest_url_
, GURL(), true);
1278 void OpenDestURLViaClickPing(const GURL
& ping_url
) const {
1279 OpenURLWithJSImpl("ClickPing", dest_url_
, ping_url
, false);
1282 void OpenDestURLViaClickNewWindow() const {
1283 OpenURLWithJSImpl("ShiftClick", dest_url_
, GURL(), true);
1286 void OpenDestURLViaClickNewForegroundTab() const {
1287 #if defined(OS_MACOSX)
1288 OpenURLWithJSImpl("MetaShiftClick", dest_url_
, GURL(), true);
1290 OpenURLWithJSImpl("CtrlShiftClick", dest_url_
, GURL(), true);
1294 void OpenDestURLViaWindowOpen() const {
1295 OpenURLViaWindowOpen(dest_url_
);
1298 void OpenURLViaWindowOpen(const GURL
& url
) const {
1299 OpenURLWithJSImpl("WindowOpen", url
, GURL(), true);
1302 void RemoveLinkElement(int i
) const {
1303 GetActiveWebContents()->GetMainFrame()->ExecuteJavaScript(
1304 base::ASCIIToUTF16(base::StringPrintf("RemoveLinkElement(%d)", i
)));
1307 void ClickToNextPageAfterPrerender() {
1308 TestNavigationObserver
nav_observer(GetActiveWebContents());
1309 RenderFrameHost
* render_frame_host
= GetActiveWebContents()->GetMainFrame();
1310 render_frame_host
->ExecuteJavaScript(base::ASCIIToUTF16("ClickOpenLink()"));
1311 nav_observer
.Wait();
1314 void NavigateToNextPageAfterPrerender() const {
1315 ui_test_utils::NavigateToURL(
1317 test_server()->GetURL("files/prerender/prerender_page.html"));
1320 // Called after the prerendered page has been navigated to and then away from.
1321 // Navigates back through the history to the prerendered page.
1322 void GoBackToPrerender() {
1323 TestNavigationObserver
back_nav_observer(GetActiveWebContents());
1324 chrome::GoBack(current_browser(), CURRENT_TAB
);
1325 back_nav_observer
.Wait();
1326 bool original_prerender_page
= false;
1327 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1328 GetActiveWebContents(),
1329 "window.domAutomationController.send(IsOriginalPrerenderPage())",
1330 &original_prerender_page
));
1331 EXPECT_TRUE(original_prerender_page
);
1334 // Goes back to the page that was active before the prerender was swapped
1335 // in. This must be called when the prerendered page is the current page
1336 // in the active tab.
1337 void GoBackToPageBeforePrerender() {
1338 WebContents
* tab
= GetActiveWebContents();
1340 EXPECT_FALSE(tab
->IsLoading());
1341 TestNavigationObserver
back_nav_observer(tab
);
1342 chrome::GoBack(current_browser(), CURRENT_TAB
);
1343 back_nav_observer
.Wait();
1345 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
1347 "window.domAutomationController.send(DidBackToOriginalPagePass())",
1349 EXPECT_TRUE(js_result
);
1352 bool UrlIsInPrerenderManager(const std::string
& html_file
) const {
1353 return UrlIsInPrerenderManager(test_server()->GetURL(html_file
));
1356 bool UrlIsInPrerenderManager(const GURL
& url
) const {
1357 return GetPrerenderManager()->FindPrerenderData(
1358 url
, GetSessionStorageNamespace()) != NULL
;
1361 void UseHttpsSrcServer() {
1362 if (https_src_server_
)
1364 https_src_server_
.reset(
1365 new net::SpawnedTestServer(
1366 net::SpawnedTestServer::TYPE_HTTPS
,
1367 net::SpawnedTestServer::kLocalhost
,
1368 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
1369 CHECK(https_src_server_
->Start());
1372 void DisableJavascriptCalls() {
1373 call_javascript_
= false;
1376 void DisableLoadEventCheck() {
1377 check_load_events_
= false;
1380 TaskManagerModel
* GetModel() const {
1381 return TaskManager::GetInstance()->model();
1384 PrerenderManager
* GetPrerenderManager() const {
1385 PrerenderManager
* prerender_manager
=
1386 PrerenderManagerFactory::GetForProfile(current_browser()->profile());
1387 return prerender_manager
;
1390 const PrerenderLinkManager
* GetPrerenderLinkManager() const {
1391 PrerenderLinkManager
* prerender_link_manager
=
1392 PrerenderLinkManagerFactory::GetForProfile(
1393 current_browser()->profile());
1394 return prerender_link_manager
;
1397 int GetPrerenderEventCount(int index
, const std::string
& type
) const {
1399 std::string expression
= base::StringPrintf(
1400 "window.domAutomationController.send("
1401 " GetPrerenderEventCount(%d, '%s'))", index
, type
.c_str());
1403 CHECK(content::ExecuteScriptAndExtractInt(
1404 GetActiveWebContents(), expression
, &event_count
));
1408 bool DidReceivePrerenderStartEventForLinkNumber(int index
) const {
1409 return GetPrerenderEventCount(index
, "webkitprerenderstart") > 0;
1412 int GetPrerenderLoadEventCountForLinkNumber(int index
) const {
1413 return GetPrerenderEventCount(index
, "webkitprerenderload");
1416 int GetPrerenderDomContentLoadedEventCountForLinkNumber(int index
) const {
1417 return GetPrerenderEventCount(index
, "webkitprerenderdomcontentloaded");
1420 bool DidReceivePrerenderStopEventForLinkNumber(int index
) const {
1421 return GetPrerenderEventCount(index
, "webkitprerenderstop") > 0;
1424 void WaitForPrerenderEventCount(int index
,
1425 const std::string
& type
,
1428 std::string expression
= base::StringPrintf(
1429 "WaitForPrerenderEventCount(%d, '%s', %d,"
1430 " window.domAutomationController.send.bind("
1431 " window.domAutomationController, 0))",
1432 index
, type
.c_str(), count
);
1434 CHECK(content::ExecuteScriptAndExtractInt(
1435 GetActiveWebContents(), expression
, &dummy
));
1439 bool HadPrerenderEventErrors() const {
1440 bool had_prerender_event_errors
;
1441 CHECK(content::ExecuteScriptAndExtractBool(
1442 GetActiveWebContents(),
1443 "window.domAutomationController.send(Boolean("
1444 " hadPrerenderEventErrors))",
1445 &had_prerender_event_errors
));
1446 return had_prerender_event_errors
;
1449 // Asserting on this can result in flaky tests. PrerenderHandles are
1450 // removed from the PrerenderLinkManager when the prerender is canceled from
1451 // the browser, when the prerenders are cancelled from the renderer process,
1452 // or the channel for the renderer process is closed on the IO thread. In the
1453 // last case, the code must be careful to wait for the channel to close, as it
1454 // is done asynchronously after swapping out the old process. See
1455 // ChannelDestructionWatcher.
1456 bool IsEmptyPrerenderLinkManager() const {
1457 return GetPrerenderLinkManager()->IsEmpty();
1460 size_t GetLinkPrerenderCount() const {
1461 return GetPrerenderLinkManager()->prerenders_
.size();
1464 size_t GetRunningLinkPrerenderCount() const {
1465 return GetPrerenderLinkManager()->CountRunningPrerenders();
1468 // Returns length of |prerender_manager_|'s history, or -1 on failure.
1469 int GetHistoryLength() const {
1470 scoped_ptr
<base::DictionaryValue
> prerender_dict(
1471 static_cast<base::DictionaryValue
*>(
1472 GetPrerenderManager()->GetAsValue()));
1473 if (!prerender_dict
.get())
1475 base::ListValue
* history_list
;
1476 if (!prerender_dict
->GetList("history", &history_list
))
1478 return static_cast<int>(history_list
->GetSize());
1481 #if defined(FULL_SAFE_BROWSING)
1482 FakeSafeBrowsingDatabaseManager
* GetFakeSafeBrowsingDatabaseManager() {
1483 return safe_browsing_factory_
->most_recent_service()->
1484 fake_database_manager();
1488 TestPrerenderContents
* GetPrerenderContentsFor(const GURL
& url
) const {
1489 PrerenderManager::PrerenderData
* prerender_data
=
1490 GetPrerenderManager()->FindPrerenderData(url
, NULL
);
1491 return static_cast<TestPrerenderContents
*>(
1492 prerender_data
? prerender_data
->contents() : NULL
);
1495 void SetLoaderHostOverride(const std::string
& host
) {
1496 loader_host_override_
= host
;
1497 host_resolver()->AddRule(host
, "127.0.0.1");
1500 void set_loader_path(const std::string
& path
) {
1501 loader_path_
= path
;
1504 void set_loader_query(const std::string
& query
) {
1505 loader_query_
= query
;
1508 GURL
GetCrossDomainTestUrl(const std::string
& path
) {
1509 static const std::string secondary_domain
= "www.foo.com";
1510 host_resolver()->AddRule(secondary_domain
, "127.0.0.1");
1511 std::string
url_str(base::StringPrintf(
1513 secondary_domain
.c_str(),
1514 test_server()->host_port_pair().port(),
1516 return GURL(url_str
);
1519 void set_browser(Browser
* browser
) {
1520 explicitly_set_browser_
= browser
;
1523 Browser
* current_browser() const {
1524 return explicitly_set_browser_
? explicitly_set_browser_
: browser();
1527 const GURL
& dest_url() const {
1531 void IncreasePrerenderMemory() {
1532 // Increase the memory allowed in a prerendered page above normal settings.
1533 // Debug build bots occasionally run against the default limit, and tests
1534 // were failing because the prerender was canceled due to memory exhaustion.
1535 // http://crbug.com/93076
1536 GetPrerenderManager()->mutable_config().max_bytes
= 1000 * 1024 * 1024;
1539 bool DidPrerenderPass(WebContents
* web_contents
) const {
1540 bool prerender_test_result
= false;
1541 if (!content::ExecuteScriptAndExtractBool(
1543 "window.domAutomationController.send(DidPrerenderPass())",
1544 &prerender_test_result
))
1546 return prerender_test_result
;
1549 bool DidDisplayPass(WebContents
* web_contents
) const {
1550 bool display_test_result
= false;
1551 if (!content::ExecuteScriptAndExtractBool(
1553 "window.domAutomationController.send(DidDisplayPass())",
1554 &display_test_result
))
1556 return display_test_result
;
1559 scoped_ptr
<TestPrerender
> ExpectPrerender(FinalStatus expected_final_status
) {
1560 return prerender_contents_factory_
->ExpectPrerenderContents(
1561 expected_final_status
);
1564 void AddPrerender(const GURL
& url
, int index
) {
1565 std::string javascript
= base::StringPrintf(
1566 "AddPrerender('%s', %d)", url
.spec().c_str(), index
);
1567 RenderFrameHost
* render_frame_host
= GetActiveWebContents()->GetMainFrame();
1568 render_frame_host
->ExecuteJavaScript(base::ASCIIToUTF16(javascript
));
1571 // Returns a string for pattern-matching TaskManager tab entries.
1572 base::string16
MatchTaskManagerTab(const char* page_title
) {
1573 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX
,
1574 base::ASCIIToUTF16(page_title
));
1577 // Returns a string for pattern-matching TaskManager prerender entries.
1578 base::string16
MatchTaskManagerPrerender(const char* page_title
) {
1579 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX
,
1580 base::ASCIIToUTF16(page_title
));
1583 void RunJSReturningString(const char* js
, std::string
* result
) {
1585 content::ExecuteScriptAndExtractString(
1586 GetActiveWebContents(),
1587 base::StringPrintf("window.domAutomationController.send(%s)",
1592 void RunJS(const char* js
) {
1593 ASSERT_TRUE(content::ExecuteScript(
1594 GetActiveWebContents(),
1595 base::StringPrintf("window.domAutomationController.send(%s)",
1599 const base::HistogramTester
& histogram_tester() { return histogram_tester_
; }
1602 bool autostart_test_server_
;
1605 // TODO(davidben): Remove this altogether so the tests don't globally assume
1606 // only one prerender.
1607 TestPrerenderContents
* GetPrerenderContents() const {
1608 return GetPrerenderContentsFor(dest_url_
);
1611 ScopedVector
<TestPrerender
> PrerenderTestURLImpl(
1612 const GURL
& prerender_url
,
1613 const std::vector
<FinalStatus
>& expected_final_status_queue
,
1614 int expected_number_of_loads
) {
1615 dest_url_
= prerender_url
;
1617 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
1618 replacement_text
.push_back(
1619 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url
.spec()));
1620 std::string replacement_path
;
1621 CHECK(net::SpawnedTestServer::GetFilePathWithReplacements(
1624 &replacement_path
));
1626 const net::SpawnedTestServer
* src_server
= test_server();
1627 if (https_src_server_
)
1628 src_server
= https_src_server_
.get();
1629 GURL loader_url
= src_server
->GetURL(
1630 replacement_path
+ "&" + loader_query_
);
1632 GURL::Replacements loader_replacements
;
1633 if (!loader_host_override_
.empty())
1634 loader_replacements
.SetHostStr(loader_host_override_
);
1635 loader_url
= loader_url
.ReplaceComponents(loader_replacements
);
1637 VLOG(1) << "Running test with queue length " <<
1638 expected_final_status_queue
.size();
1639 CHECK(!expected_final_status_queue
.empty());
1640 ScopedVector
<TestPrerender
> prerenders
;
1641 for (size_t i
= 0; i
< expected_final_status_queue
.size(); i
++) {
1642 prerenders
.push_back(
1643 prerender_contents_factory_
->ExpectPrerenderContents(
1644 expected_final_status_queue
[i
]).release());
1647 FinalStatus expected_final_status
= expected_final_status_queue
.front();
1649 // Navigate to the loader URL and then wait for the first prerender to be
1651 ui_test_utils::NavigateToURL(current_browser(), loader_url
);
1652 prerenders
[0]->WaitForCreate();
1653 prerenders
[0]->WaitForLoads(expected_number_of_loads
);
1655 if (ShouldAbortPrerenderBeforeSwap(expected_final_status
)) {
1656 // The prerender will abort on its own. Assert it does so correctly.
1657 prerenders
[0]->WaitForStop();
1658 EXPECT_FALSE(prerenders
[0]->contents());
1659 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
1661 // Otherwise, check that it prerendered correctly.
1662 TestPrerenderContents
* prerender_contents
= prerenders
[0]->contents();
1664 CHECK_NE(static_cast<PrerenderContents
*>(NULL
), prerender_contents
);
1665 EXPECT_EQ(FINAL_STATUS_MAX
, prerender_contents
->final_status());
1666 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1668 if (call_javascript_
) {
1669 // Check if page behaves as expected while in prerendered state.
1670 EXPECT_TRUE(DidPrerenderPass(prerender_contents
->prerender_contents()));
1674 // Test that the referring page received the right start and load events.
1675 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1676 if (check_load_events_
) {
1677 EXPECT_EQ(expected_number_of_loads
, prerenders
[0]->number_of_loads());
1678 EXPECT_EQ(expected_number_of_loads
,
1679 GetPrerenderLoadEventCountForLinkNumber(0));
1681 EXPECT_FALSE(HadPrerenderEventErrors());
1683 return prerenders
.Pass();
1686 void NavigateToURLImpl(const content::OpenURLParams
& params
,
1687 bool expect_swap_to_succeed
) const {
1688 ASSERT_NE(static_cast<PrerenderManager
*>(NULL
), GetPrerenderManager());
1689 // Make sure in navigating we have a URL to use in the PrerenderManager.
1690 ASSERT_NE(static_cast<PrerenderContents
*>(NULL
), GetPrerenderContents());
1692 WebContents
* web_contents
= GetPrerenderContents()->prerender_contents();
1694 // Navigate and wait for either the load to finish normally or for a swap to
1696 // TODO(davidben): The only handles CURRENT_TAB navigations, which is the
1697 // only case tested or prerendered right now.
1698 CHECK_EQ(CURRENT_TAB
, params
.disposition
);
1699 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
1700 GetActiveWebContents());
1701 WebContents
* target_web_contents
= current_browser()->OpenURL(params
);
1702 swap_observer
.Wait();
1704 if (web_contents
&& expect_swap_to_succeed
) {
1705 EXPECT_EQ(web_contents
, target_web_contents
);
1706 if (call_javascript_
)
1707 EXPECT_TRUE(DidDisplayPass(web_contents
));
1711 // Opens the prerendered page using javascript functions in the loader
1712 // page. |javascript_function_name| should be a 0 argument function which is
1713 // invoked. |new_web_contents| is true if the navigation is expected to
1714 // happen in a new WebContents via OpenURL.
1715 void OpenURLWithJSImpl(const std::string
& javascript_function_name
,
1717 const GURL
& ping_url
,
1718 bool new_web_contents
) const {
1719 WebContents
* web_contents
= GetActiveWebContents();
1720 RenderFrameHost
* render_frame_host
= web_contents
->GetMainFrame();
1721 // Extra arguments in JS are ignored.
1722 std::string javascript
= base::StringPrintf(
1723 "%s('%s', '%s')", javascript_function_name
.c_str(),
1724 url
.spec().c_str(), ping_url
.spec().c_str());
1726 if (new_web_contents
) {
1727 NewTabNavigationOrSwapObserver observer
;
1729 ExecuteJavaScriptForTests(base::ASCIIToUTF16(javascript
));
1732 NavigationOrSwapObserver
observer(current_browser()->tab_strip_model(),
1734 render_frame_host
->ExecuteJavaScript(base::ASCIIToUTF16(javascript
));
1739 TestPrerenderContentsFactory
* prerender_contents_factory_
;
1740 #if defined(FULL_SAFE_BROWSING)
1741 scoped_ptr
<TestSafeBrowsingServiceFactory
> safe_browsing_factory_
;
1743 NeverRunsExternalProtocolHandlerDelegate external_protocol_handler_delegate_
;
1745 scoped_ptr
<net::SpawnedTestServer
> https_src_server_
;
1746 bool call_javascript_
;
1747 bool check_load_events_
;
1748 std::string loader_host_override_
;
1749 std::string loader_path_
;
1750 std::string loader_query_
;
1751 Browser
* explicitly_set_browser_
;
1752 base::HistogramTester histogram_tester_
;
1753 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
1756 // Checks that a page is correctly prerendered in the case of a
1757 // <link rel=prerender> tag and then loaded into a tab in response to a
1759 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPage
) {
1760 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
1761 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
1762 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1763 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1764 histogram_tester().ExpectTotalCount(
1765 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1766 histogram_tester().ExpectTotalCount(
1767 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
1769 ChannelDestructionWatcher channel_close_watcher
;
1770 channel_close_watcher
.WatchChannel(
1771 GetActiveWebContents()->GetRenderProcessHost());
1772 NavigateToDestURL();
1773 channel_close_watcher
.WaitForChannelClose();
1775 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
1776 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
1778 histogram_tester().ExpectTotalCount(
1779 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
1781 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1784 // Checks that cross-domain prerenders emit the correct histograms.
1785 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageCrossDomain
) {
1786 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
1787 FINAL_STATUS_USED
, 1);
1788 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
1789 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
1790 histogram_tester().ExpectTotalCount(
1791 "Prerender.none_PerceivedPLTMatchedComplete", 0);
1792 histogram_tester().ExpectTotalCount(
1793 "Prerender.webcross_PrerenderNotSwappedInPLT", 1);
1795 NavigateToDestURL();
1796 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLT", 1);
1797 histogram_tester().ExpectTotalCount("Prerender.webcross_PerceivedPLTMatched",
1799 histogram_tester().ExpectTotalCount(
1800 "Prerender.webcross_PerceivedPLTMatchedComplete", 1);
1803 // Checks that pending prerenders launch and receive proper event treatment.
1804 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPagePending
) {
1805 scoped_ptr
<TestPrerender
> prerender
=
1806 PrerenderTestURL("files/prerender/prerender_page_pending.html",
1807 FINAL_STATUS_USED
, 1);
1809 // Navigate to the prerender.
1810 scoped_ptr
<TestPrerender
> prerender2
= ExpectPrerender(FINAL_STATUS_USED
);
1811 NavigateToDestURL();
1812 // Abort early if the original prerender didn't swap, so as not to hang.
1813 ASSERT_FALSE(prerender
->contents());
1815 // Wait for the new prerender to be ready.
1816 prerender2
->WaitForStart();
1817 prerender2
->WaitForLoads(1);
1819 const GURL prerender_page_url
=
1820 test_server()->GetURL("files/prerender/prerender_page.html");
1821 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1822 EXPECT_NE(static_cast<TestPrerenderContents
*>(NULL
),
1823 GetPrerenderContentsFor(prerender_page_url
));
1825 // Now navigate to our target page.
1826 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
1827 GetActiveWebContents());
1828 ui_test_utils::NavigateToURLWithDisposition(
1829 current_browser(), prerender_page_url
, CURRENT_TAB
,
1830 ui_test_utils::BROWSER_TEST_NONE
);
1831 swap_observer
.Wait();
1833 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1836 // Checks that pending prerenders which are canceled before they are launched
1837 // never get started.
1838 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageRemovesPending
) {
1839 PrerenderTestURL("files/prerender/prerender_page_removes_pending.html",
1840 FINAL_STATUS_USED
, 1);
1842 ChannelDestructionWatcher channel_close_watcher
;
1843 channel_close_watcher
.WatchChannel(
1844 GetActiveWebContents()->GetRenderProcessHost());
1845 NavigateToDestURL();
1846 channel_close_watcher
.WaitForChannelClose();
1848 EXPECT_FALSE(DidReceivePrerenderStartEventForLinkNumber(1));
1849 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1850 EXPECT_FALSE(HadPrerenderEventErrors());
1851 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1852 // calls did a thread/process hop to the renderer which insured pending
1853 // renderer events have arrived.
1854 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
1857 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageRemovingLink
) {
1858 scoped_ptr
<TestPrerender
> prerender
=
1859 PrerenderTestURL("files/prerender/prerender_page.html",
1860 FINAL_STATUS_CANCELLED
, 1);
1862 // No ChannelDestructionWatcher is needed here, since prerenders in the
1863 // PrerenderLinkManager should be deleted by removing the links, rather than
1864 // shutting down the renderer process.
1865 RemoveLinkElement(0);
1866 prerender
->WaitForStop();
1868 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1869 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1870 EXPECT_FALSE(HadPrerenderEventErrors());
1871 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1872 // calls did a thread/process hop to the renderer which insured pending
1873 // renderer events have arrived.
1874 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1877 IN_PROC_BROWSER_TEST_F(
1878 PrerenderBrowserTest
, PrerenderPageRemovingLinkWithTwoLinks
) {
1879 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
1880 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
1882 set_loader_query("links_to_insert=2");
1883 scoped_ptr
<TestPrerender
> prerender
=
1884 PrerenderTestURL("files/prerender/prerender_page.html",
1885 FINAL_STATUS_CANCELLED
, 1);
1886 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1887 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1888 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1889 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1891 RemoveLinkElement(0);
1892 RemoveLinkElement(1);
1893 prerender
->WaitForStop();
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_TRUE(IsEmptyPrerenderLinkManager());
1906 IN_PROC_BROWSER_TEST_F(
1907 PrerenderBrowserTest
, PrerenderPageRemovingLinkWithTwoLinksOneLate
) {
1908 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
1909 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
1911 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
1912 scoped_ptr
<TestPrerender
> prerender
=
1913 PrerenderTestURL(url
, FINAL_STATUS_CANCELLED
, 1);
1915 // Add a second prerender for the same link. It reuses the prerender, so only
1916 // the start event fires here.
1917 AddPrerender(url
, 1);
1918 WaitForPrerenderEventCount(1, "webkitprerenderstart", 1);
1919 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1920 EXPECT_EQ(0, GetPrerenderLoadEventCountForLinkNumber(1));
1921 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1923 RemoveLinkElement(0);
1924 RemoveLinkElement(1);
1925 prerender
->WaitForStop();
1927 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1928 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1929 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1930 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1931 EXPECT_FALSE(HadPrerenderEventErrors());
1932 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1933 // calls did a thread/process hop to the renderer which insured pending
1934 // renderer events have arrived.
1935 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1938 IN_PROC_BROWSER_TEST_F(
1939 PrerenderBrowserTest
,
1940 PrerenderPageRemovingLinkWithTwoLinksRemovingOne
) {
1941 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
1942 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
1943 set_loader_query("links_to_insert=2");
1944 PrerenderTestURL("files/prerender/prerender_page.html",
1945 FINAL_STATUS_USED
, 1);
1946 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1947 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1948 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1949 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1951 RemoveLinkElement(0);
1952 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
1953 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(0));
1954 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(1));
1955 EXPECT_FALSE(DidReceivePrerenderStopEventForLinkNumber(1));
1956 EXPECT_FALSE(HadPrerenderEventErrors());
1957 // IsEmptyPrerenderLinkManager() is not racy because the earlier DidReceive*
1958 // calls did a thread/process hop to the renderer which insured pending
1959 // renderer events have arrived.
1960 EXPECT_FALSE(IsEmptyPrerenderLinkManager());
1962 ChannelDestructionWatcher channel_close_watcher
;
1963 channel_close_watcher
.WatchChannel(
1964 GetActiveWebContents()->GetRenderProcessHost());
1965 NavigateToDestURL();
1966 channel_close_watcher
.WaitForChannelClose();
1968 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
1971 // Checks that the visibility API works.
1972 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderVisibility
) {
1973 PrerenderTestURL("files/prerender/prerender_visibility.html",
1976 NavigateToDestURL();
1979 // Checks that the prerendering of a page is canceled correctly if we try to
1980 // swap it in before it commits.
1981 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNoCommitNoSwap
) {
1982 // Navigate to a page that triggers a prerender for a URL that never commits.
1983 const GURL
kNoCommitUrl("http://never-respond.example.com");
1984 base::FilePath
file(GetTestPath("prerender_page.html"));
1986 base::RunLoop prerender_start_loop
;
1987 BrowserThread::PostTask(
1988 BrowserThread::IO
, FROM_HERE
,
1989 base::Bind(&CreateHangingFirstRequestInterceptorOnIO
, kNoCommitUrl
, file
,
1990 prerender_start_loop
.QuitClosure()));
1991 DisableJavascriptCalls();
1992 PrerenderTestURL(kNoCommitUrl
,
1993 FINAL_STATUS_NAVIGATION_UNCOMMITTED
,
1995 // Wait for the hanging request to be scheduled.
1996 prerender_start_loop
.Run();
1998 // Navigate to the URL, but assume the contents won't be swapped in.
1999 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
2002 // Checks that client redirects don't add alias URLs until after they commit.
2003 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNoCommitNoSwap2
) {
2004 // Navigate to a page that then navigates to a URL that never commits.
2005 const GURL
kNoCommitUrl("http://never-respond.example.com");
2006 base::FilePath
file(GetTestPath("prerender_page.html"));
2008 base::RunLoop prerender_start_loop
;
2009 BrowserThread::PostTask(
2010 BrowserThread::IO
, FROM_HERE
,
2011 base::Bind(&CreateHangingFirstRequestInterceptorOnIO
, kNoCommitUrl
, file
,
2012 prerender_start_loop
.QuitClosure()));
2013 DisableJavascriptCalls();
2014 PrerenderTestURL(CreateClientRedirect(kNoCommitUrl
.spec()),
2015 FINAL_STATUS_APP_TERMINATING
, 1);
2016 // Wait for the hanging request to be scheduled.
2017 prerender_start_loop
.Run();
2019 // Navigating to the second URL should not swap.
2020 NavigateToURLWithDisposition(kNoCommitUrl
, CURRENT_TAB
, false);
2023 // Checks that the prerendering of a page is canceled correctly when a
2024 // Javascript alert is called.
2025 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderAlertBeforeOnload
) {
2026 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
2027 FINAL_STATUS_JAVASCRIPT_ALERT
,
2031 // Checks that the prerendering of a page is canceled correctly when a
2032 // Javascript alert is called.
2033 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderAlertAfterOnload
) {
2034 PrerenderTestURL("files/prerender/prerender_alert_after_onload.html",
2035 FINAL_STATUS_JAVASCRIPT_ALERT
,
2039 // Checks that plugins are not loaded while a page is being preloaded, but
2040 // are loaded when the page is displayed.
2041 #if defined(USE_AURA) && !defined(OS_WIN)
2042 // http://crbug.com/103496
2043 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2044 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2045 #elif defined(OS_MACOSX)
2046 // http://crbug.com/100514
2047 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2048 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2049 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
2050 // TODO(jschuh): Failing plugin tests. crbug.com/244653
2051 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2052 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2053 #elif defined(OS_LINUX)
2054 // http://crbug.com/306715
2055 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
2056 #define MAYBE_PrerenderPluginPowerSaver DISABLED_PrerenderPluginPowerSaver
2058 #define MAYBE_PrerenderDelayLoadPlugin PrerenderDelayLoadPlugin
2059 #define MAYBE_PrerenderPluginPowerSaver PrerenderPluginPowerSaver
2061 // http://crbug.com/306715
2062 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, MAYBE_PrerenderDelayLoadPlugin
) {
2063 PrerenderTestURL("files/prerender/plugin_delay_load.html",
2066 NavigateToDestURL();
2069 // For enabled Plugin Power Saver, checks that plugins are not loaded while
2070 // a page is being preloaded, but are loaded when the page is displayed.
2071 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, MAYBE_PrerenderPluginPowerSaver
) {
2072 // Enable click-to-play.
2073 HostContentSettingsMap
* content_settings_map
=
2074 current_browser()->profile()->GetHostContentSettingsMap();
2075 content_settings_map
->SetDefaultContentSetting(
2076 CONTENT_SETTINGS_TYPE_PLUGINS
, CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
);
2078 PrerenderTestURL("files/prerender/prerender_plugin_power_saver.html",
2079 FINAL_STATUS_USED
, 1);
2080 NavigateToDestURL();
2083 // Checks that we don't load a NaCl plugin when NaCl is disabled.
2084 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNaClPluginDisabled
) {
2085 PrerenderTestURL("files/prerender/prerender_plugin_nacl_disabled.html",
2088 NavigateToDestURL();
2091 // Run this check again. When we try to load aa ppapi plugin, the
2092 // "loadstart" event is asynchronously posted to a message loop.
2093 // It's possible that earlier call could have been run before the
2094 // the "loadstart" event was posted.
2095 // TODO(mmenke): While this should reliably fail on regressions, the
2096 // reliability depends on the specifics of ppapi plugin
2097 // loading. It would be great if we could avoid that.
2098 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2101 // Checks that plugins in an iframe are not loaded while a page is
2102 // being preloaded, but are loaded when the page is displayed.
2103 #if defined(USE_AURA) && !defined(OS_WIN)
2104 // http://crbug.com/103496
2105 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2106 DISABLED_PrerenderIframeDelayLoadPlugin
2107 #elif defined(OS_MACOSX)
2108 // http://crbug.com/100514
2109 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2110 DISABLED_PrerenderIframeDelayLoadPlugin
2111 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
2112 // TODO(jschuh): Failing plugin tests. crbug.com/244653
2113 #define MAYBE_PrerenderIframeDelayLoadPlugin \
2114 DISABLED_PrerenderIframeDelayLoadPlugin
2116 #define MAYBE_PrerenderIframeDelayLoadPlugin PrerenderIframeDelayLoadPlugin
2118 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2119 MAYBE_PrerenderIframeDelayLoadPlugin
) {
2120 PrerenderTestURL("files/prerender/prerender_iframe_plugin_delay_load.html",
2123 NavigateToDestURL();
2126 // Renders a page that contains a prerender link to a page that contains an
2127 // iframe with a source that requires http authentication. This should not
2128 // prerender successfully.
2129 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHttpAuthentication
) {
2130 PrerenderTestURL("files/prerender/prerender_http_auth_container.html",
2131 FINAL_STATUS_AUTH_NEEDED
,
2135 // Checks that client-issued redirects work with prerendering.
2136 // This version navigates to the page which issues the redirection, rather
2137 // than the final destination page.
2138 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2139 PrerenderClientRedirectNavigateToFirst
) {
2140 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2143 NavigateToDestURL();
2146 // Checks that client-issued redirects work with prerendering.
2147 // This version navigates to the final destination page, rather than the
2148 // page which does the redirection.
2149 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2150 PrerenderClientRedirectNavigateToSecond
) {
2151 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
2154 NavigateToURL("files/prerender/prerender_page.html");
2157 // Checks that redirects with location.replace do not cancel a prerender and
2158 // and swap when navigating to the first page.
2159 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2160 PrerenderLocationReplaceNavigateToFirst
) {
2161 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2164 NavigateToDestURL();
2167 // Checks that redirects with location.replace do not cancel a prerender and
2168 // and swap when navigating to the second.
2169 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2170 PrerenderLocationReplaceNavigateToSecond
) {
2171 PrerenderTestURL("files/prerender/prerender_location_replace.html",
2174 NavigateToURL("files/prerender/prerender_page.html");
2177 // Checks that we get the right PPLT histograms for client redirect prerenders
2178 // and navigations when the referring page is Google.
2179 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2180 PrerenderLocationReplaceGWSHistograms
) {
2181 DisableJavascriptCalls();
2183 // The loader page should look like Google.
2184 static const char kGoogleDotCom
[] = "www.google.com";
2185 SetLoaderHostOverride(kGoogleDotCom
);
2186 set_loader_path("files/prerender/prerender_loader_with_replace_state.html");
2188 GURL dest_url
= GetCrossDomainTestUrl(
2189 "files/prerender/prerender_deferred_image.html");
2191 GURL prerender_url
= test_server()->GetURL(
2192 "files/prerender/prerender_location_replace.html?" +
2193 net::EscapeQueryParamValue(dest_url
.spec(), false) +
2195 GURL::Replacements replacements
;
2196 replacements
.SetHostStr(kGoogleDotCom
);
2197 prerender_url
= prerender_url
.ReplaceComponents(replacements
);
2199 // The prerender will not completely load until after the swap, so wait for a
2200 // title change before calling DidPrerenderPass.
2201 scoped_ptr
<TestPrerender
> prerender
=
2202 PrerenderTestURL(prerender_url
, FINAL_STATUS_USED
, 1);
2203 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
2204 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
2205 EXPECT_EQ(1, prerender
->number_of_loads());
2207 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
2208 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
2209 histogram_tester().ExpectTotalCount(
2210 "Prerender.none_PerceivedPLTMatchedComplete", 0);
2211 // Although there is a client redirect, it is dropped from histograms because
2212 // it is a Google URL. The target page itself does not load until after the
2214 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2217 GURL navigate_url
= test_server()->GetURL(
2218 "files/prerender/prerender_location_replace.html?" +
2219 net::EscapeQueryParamValue(dest_url
.spec(), false) +
2221 navigate_url
= navigate_url
.ReplaceComponents(replacements
);
2223 NavigationOrSwapObserver
swap_observer(
2224 current_browser()->tab_strip_model(),
2225 GetActiveWebContents(), 2);
2226 current_browser()->OpenURL(OpenURLParams(
2227 navigate_url
, Referrer(), CURRENT_TAB
,
2228 ui::PAGE_TRANSITION_TYPED
, false));
2229 swap_observer
.Wait();
2231 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2233 histogram_tester().ExpectTotalCount("Prerender.gws_PrerenderNotSwappedInPLT",
2235 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLT", 1);
2236 histogram_tester().ExpectTotalCount("Prerender.gws_PerceivedPLTMatched", 1);
2237 histogram_tester().ExpectTotalCount(
2238 "Prerender.gws_PerceivedPLTMatchedComplete", 1);
2240 // The client redirect does /not/ count as a miss because it's a Google URL.
2241 histogram_tester().ExpectTotalCount("Prerender.PerceivedPLTFirstAfterMiss",
2245 // Checks that client-issued redirects work with prerendering.
2246 // This version navigates to the final destination page, rather than the
2247 // page which does the redirection via a mouse click.
2248 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2249 PrerenderClientRedirectNavigateToSecondViaClick
) {
2250 GURL prerender_url
= test_server()->GetURL(
2251 CreateClientRedirect("files/prerender/prerender_page.html"));
2252 GURL destination_url
= test_server()->GetURL(
2253 "files/prerender/prerender_page.html");
2254 PrerenderTestURL(prerender_url
, FINAL_STATUS_USED
, 2);
2255 OpenURLViaClick(destination_url
);
2258 // Checks that a page served over HTTPS is correctly prerendered.
2259 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHttps
) {
2260 net::SpawnedTestServer
https_server(
2261 net::SpawnedTestServer::TYPE_HTTPS
, net::SpawnedTestServer::kLocalhost
,
2262 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2263 ASSERT_TRUE(https_server
.Start());
2264 GURL https_url
= https_server
.GetURL("files/prerender/prerender_page.html");
2265 PrerenderTestURL(https_url
,
2268 NavigateToDestURL();
2271 // Checks that client-issued redirects within an iframe in a prerendered
2272 // page will not count as an "alias" for the prerendered page.
2273 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2274 PrerenderClientRedirectInIframe
) {
2275 std::string redirect_path
= CreateClientRedirect(
2276 "/files/prerender/prerender_embedded_content.html");
2277 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2278 replacement_text
.push_back(
2279 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path
));
2280 std::string replacement_path
;
2281 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2282 "files/prerender/prerender_with_iframe.html",
2284 &replacement_path
));
2285 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 2);
2286 EXPECT_FALSE(UrlIsInPrerenderManager(
2287 "files/prerender/prerender_embedded_content.html"));
2288 NavigateToDestURL();
2291 // Checks that server-issued redirects work with prerendering.
2292 // This version navigates to the page which issues the redirection, rather
2293 // than the final destination page.
2294 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2295 PrerenderServerRedirectNavigateToFirst
) {
2296 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2299 NavigateToDestURL();
2302 // Checks that server-issued redirects work with prerendering.
2303 // This version navigates to the final destination page, rather than the
2304 // page which does the redirection.
2305 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2306 PrerenderServerRedirectNavigateToSecond
) {
2307 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
2310 NavigateToURL("files/prerender/prerender_page.html");
2313 // Checks that server-issued redirects work with prerendering.
2314 // This version navigates to the final destination page, rather than the
2315 // page which does the redirection via a mouse click.
2316 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2317 PrerenderServerRedirectNavigateToSecondViaClick
) {
2318 GURL prerender_url
= test_server()->GetURL(
2319 CreateServerRedirect("files/prerender/prerender_page.html"));
2320 GURL destination_url
= test_server()->GetURL(
2321 "files/prerender/prerender_page.html");
2322 PrerenderTestURL(prerender_url
, FINAL_STATUS_USED
, 1);
2323 OpenURLViaClick(destination_url
);
2326 // Checks that server-issued redirects within an iframe in a prerendered
2327 // page will not count as an "alias" for the prerendered page.
2328 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderServerRedirectInIframe
) {
2329 std::string redirect_path
= CreateServerRedirect(
2330 "/files/prerender/prerender_embedded_content.html");
2331 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2332 replacement_text
.push_back(
2333 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path
));
2334 std::string replacement_path
;
2335 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2336 "files/prerender/prerender_with_iframe.html",
2338 &replacement_path
));
2339 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
2340 EXPECT_FALSE(UrlIsInPrerenderManager(
2341 "files/prerender/prerender_embedded_content.html"));
2342 NavigateToDestURL();
2345 // Prerenders a page that contains an automatic download triggered through an
2346 // iframe. This should not prerender successfully.
2347 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDownloadIframe
) {
2348 PrerenderTestURL("files/prerender/prerender_download_iframe.html",
2349 FINAL_STATUS_DOWNLOAD
,
2353 // Prerenders a page that contains an automatic download triggered through
2354 // Javascript changing the window.location. This should not prerender
2356 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDownloadLocation
) {
2357 PrerenderTestURL(CreateClientRedirect("files/download-test1.lib"),
2358 FINAL_STATUS_DOWNLOAD
,
2362 // Prerenders a page that contains an automatic download triggered through a
2363 // client-issued redirect. This should not prerender successfully.
2364 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDownloadClientRedirect
) {
2365 PrerenderTestURL("files/prerender/prerender_download_refresh.html",
2366 FINAL_STATUS_DOWNLOAD
,
2370 // Checks that the referrer is set when prerendering.
2371 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderReferrer
) {
2372 PrerenderTestURL("files/prerender/prerender_referrer.html",
2375 NavigateToDestURL();
2378 // Checks that the referrer is not set when prerendering and the source page is
2380 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2381 PrerenderNoSSLReferrer
) {
2382 UseHttpsSrcServer();
2383 PrerenderTestURL("files/prerender/prerender_no_referrer.html",
2386 NavigateToDestURL();
2389 // Checks that the referrer is set when prerendering is cancelled.
2390 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCancelReferrer
) {
2391 scoped_ptr
<TestContentBrowserClient
> test_content_browser_client(
2392 new TestContentBrowserClient
);
2393 content::ContentBrowserClient
* original_browser_client
=
2394 content::SetBrowserClientForTesting(test_content_browser_client
.get());
2396 PrerenderTestURL("files/prerender/prerender_referrer.html",
2397 FINAL_STATUS_CANCELLED
,
2399 OpenDestURLViaClick();
2401 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
2403 content::SetBrowserClientForTesting(original_browser_client
);
2406 // Checks that popups on a prerendered page cause cancellation.
2407 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPopup
) {
2408 PrerenderTestURL("files/prerender/prerender_popup.html",
2409 FINAL_STATUS_CREATE_NEW_WINDOW
,
2413 // Checks that registering a protocol handler causes cancellation.
2414 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderRegisterProtocolHandler
) {
2415 PrerenderTestURL("files/prerender/prerender_register_protocol_handler.html",
2416 FINAL_STATUS_REGISTER_PROTOCOL_HANDLER
,
2420 // Checks that renderers using excessive memory will be terminated.
2421 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderExcessiveMemory
) {
2422 ASSERT_TRUE(GetPrerenderManager());
2423 GetPrerenderManager()->mutable_config().max_bytes
= 30 * 1024 * 1024;
2424 // The excessive memory kill may happen before or after the load event as it
2425 // happens asynchronously with IPC calls. Even if the test does not start
2426 // allocating until after load, the browser process might notice before the
2427 // message gets through. This happens on XP debug bots because they're so
2428 // slow. Instead, don't bother checking the load event count.
2429 DisableLoadEventCheck();
2430 PrerenderTestURL("files/prerender/prerender_excessive_memory.html",
2431 FINAL_STATUS_MEMORY_LIMIT_EXCEEDED
, 0);
2434 // Checks shutdown code while a prerender is active.
2435 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderQuickQuit
) {
2436 DisableJavascriptCalls();
2437 DisableLoadEventCheck();
2438 PrerenderTestURL("files/prerender/prerender_page.html",
2439 FINAL_STATUS_APP_TERMINATING
,
2443 // Checks that we don't prerender in an infinite loop.
2444 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderInfiniteLoop
) {
2445 const char* const kHtmlFileA
= "files/prerender/prerender_infinite_a.html";
2446 const char* const kHtmlFileB
= "files/prerender/prerender_infinite_b.html";
2448 std::vector
<FinalStatus
> expected_final_status_queue
;
2449 expected_final_status_queue
.push_back(FINAL_STATUS_USED
);
2450 expected_final_status_queue
.push_back(FINAL_STATUS_APP_TERMINATING
);
2452 ScopedVector
<TestPrerender
> prerenders
=
2453 PrerenderTestURL(kHtmlFileA
, expected_final_status_queue
, 1);
2454 ASSERT_TRUE(prerenders
[0]->contents());
2455 // Assert that the pending prerender is in there already. This relies on the
2456 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2457 // the page load one.
2458 EXPECT_EQ(2U, GetLinkPrerenderCount());
2459 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2461 // Next url should be in pending list but not an active entry.
2462 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB
));
2464 NavigateToDestURL();
2466 // Make sure the PrerenderContents for the next url is now in the manager and
2467 // not pending. This relies on pending prerenders being resolved in the same
2468 // event loop iteration as OnPrerenderStop.
2469 EXPECT_TRUE(UrlIsInPrerenderManager(kHtmlFileB
));
2470 EXPECT_EQ(1U, GetLinkPrerenderCount());
2471 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2474 // Checks that we don't prerender in an infinite loop and multiple links are
2475 // handled correctly.
2476 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2477 PrerenderInfiniteLoopMultiple
) {
2478 const char* const kHtmlFileA
=
2479 "files/prerender/prerender_infinite_a_multiple.html";
2480 const char* const kHtmlFileB
=
2481 "files/prerender/prerender_infinite_b_multiple.html";
2482 const char* const kHtmlFileC
=
2483 "files/prerender/prerender_infinite_c_multiple.html";
2485 // This test is conceptually simplest if concurrency is at two, since we
2486 // don't have to worry about which of kHtmlFileB or kHtmlFileC gets evicted.
2487 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
2488 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
2490 std::vector
<FinalStatus
> expected_final_status_queue
;
2491 expected_final_status_queue
.push_back(FINAL_STATUS_USED
);
2492 expected_final_status_queue
.push_back(FINAL_STATUS_APP_TERMINATING
);
2493 expected_final_status_queue
.push_back(FINAL_STATUS_APP_TERMINATING
);
2495 ScopedVector
<TestPrerender
> prerenders
=
2496 PrerenderTestURL(kHtmlFileA
, expected_final_status_queue
, 1);
2497 ASSERT_TRUE(prerenders
[0]->contents());
2499 // Next url should be in pending list but not an active entry. This relies on
2500 // the fact that the renderer sends out the AddLinkRelPrerender IPC before
2501 // sending the page load one.
2502 EXPECT_EQ(3U, GetLinkPrerenderCount());
2503 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2504 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB
));
2505 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileC
));
2507 NavigateToDestURL();
2509 // Make sure the PrerenderContents for the next urls are now in the manager
2510 // and not pending. One and only one of the URLs (the last seen) should be the
2511 // active entry. This relies on pending prerenders being resolved in the same
2512 // event loop iteration as OnPrerenderStop.
2513 bool url_b_is_active_prerender
= UrlIsInPrerenderManager(kHtmlFileB
);
2514 bool url_c_is_active_prerender
= UrlIsInPrerenderManager(kHtmlFileC
);
2515 EXPECT_TRUE(url_b_is_active_prerender
&& url_c_is_active_prerender
);
2516 EXPECT_EQ(2U, GetLinkPrerenderCount());
2517 EXPECT_EQ(2U, GetRunningLinkPrerenderCount());
2520 // Checks that pending prerenders are aborted (and never launched) when launched
2521 // by a prerender that itself gets aborted.
2522 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderAbortPendingOnCancel
) {
2523 const char* const kHtmlFileA
= "files/prerender/prerender_infinite_a.html";
2524 const char* const kHtmlFileB
= "files/prerender/prerender_infinite_b.html";
2526 scoped_ptr
<TestPrerender
> prerender
=
2527 PrerenderTestURL(kHtmlFileA
, FINAL_STATUS_CANCELLED
, 1);
2528 ASSERT_TRUE(prerender
->contents());
2529 // Assert that the pending prerender is in there already. This relies on the
2530 // fact that the renderer sends out the AddLinkRelPrerender IPC before sending
2531 // the page load one.
2532 EXPECT_EQ(2U, GetLinkPrerenderCount());
2533 EXPECT_EQ(1U, GetRunningLinkPrerenderCount());
2535 // Next url should be in pending list but not an active entry.
2536 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB
));
2538 // Cancel the prerender.
2539 GetPrerenderManager()->CancelAllPrerenders();
2540 prerender
->WaitForStop();
2542 // All prerenders are now gone.
2543 EXPECT_TRUE(IsEmptyPrerenderLinkManager());
2546 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, OpenTaskManagerBeforePrerender
) {
2547 const base::string16 any_prerender
= MatchTaskManagerPrerender("*");
2548 const base::string16 any_tab
= MatchTaskManagerTab("*");
2549 const base::string16 original
= MatchTaskManagerTab("Preloader");
2550 const base::string16 prerender
= MatchTaskManagerPrerender("Prerender Page");
2551 const base::string16 final
= MatchTaskManagerTab("Prerender Page");
2553 // Show the task manager. This populates the model.
2554 chrome::OpenTaskManager(current_browser());
2555 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2556 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2558 // Prerender a page in addition to the original tab.
2559 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
2561 // A TaskManager entry should appear like "Prerender: Prerender Page"
2562 // alongside the original tab entry. There should be just these two entries.
2563 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender
));
2564 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original
));
2565 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final
));
2566 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender
));
2567 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2569 // Swap in the prerendered content.
2570 NavigateToDestURL();
2572 // The "Prerender: " TaskManager entry should disappear, being replaced by a
2573 // "Tab: Prerender Page" entry, and nothing else.
2574 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender
));
2575 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original
));
2576 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final
));
2577 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2578 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2581 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, OpenTaskManagerAfterPrerender
) {
2582 const base::string16 any_prerender
= MatchTaskManagerPrerender("*");
2583 const base::string16 any_tab
= MatchTaskManagerTab("*");
2584 const base::string16 original
= MatchTaskManagerTab("Preloader");
2585 const base::string16 prerender
= MatchTaskManagerPrerender("Prerender Page");
2586 const base::string16 final
= MatchTaskManagerTab("Prerender Page");
2588 // Start with two resources.
2589 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
2591 // Show the task manager. This populates the model. Importantly, we're doing
2592 // this after the prerender WebContents already exists - the task manager
2593 // needs to find it, it can't just listen for creation.
2594 chrome::OpenTaskManager(current_browser());
2596 // A TaskManager entry should appear like "Prerender: Prerender Page"
2597 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, prerender
));
2598 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, original
));
2599 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, final
));
2600 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_prerender
));
2601 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2604 NavigateToDestURL();
2606 // The "Prerender: Prerender Page" TaskManager row should disappear, being
2607 // replaced by "Tab: Prerender Page"
2608 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, prerender
));
2609 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, original
));
2610 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final
));
2611 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2612 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2615 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, OpenTaskManagerAfterSwapIn
) {
2616 const base::string16 any_prerender
= MatchTaskManagerPrerender("*");
2617 const base::string16 any_tab
= MatchTaskManagerTab("*");
2618 const base::string16 final
= MatchTaskManagerTab("Prerender Page");
2620 // Prerender, and swap it in.
2621 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
2622 NavigateToDestURL();
2624 // Show the task manager. This populates the model. Importantly, we're doing
2625 // this after the prerender has been swapped in.
2626 chrome::OpenTaskManager(current_browser());
2628 // We should not see a prerender resource in the task manager, just a normal
2630 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, final
));
2631 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, any_tab
));
2632 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, any_prerender
));
2635 // Checks that audio loads are deferred on prerendering.
2636 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5Audio
) {
2637 PrerenderTestURL("files/prerender/prerender_html5_audio.html",
2640 NavigateToDestURL();
2641 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2644 // Checks that audio loads are deferred on prerendering and played back when
2645 // the prerender is swapped in if autoplay is set.
2646 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5AudioAutoplay
) {
2647 PrerenderTestURL("files/prerender/prerender_html5_audio_autoplay.html",
2650 NavigateToDestURL();
2651 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2654 // Checks that audio loads are deferred on prerendering and played back when
2655 // the prerender is swapped in if js starts playing.
2656 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5AudioJsplay
) {
2657 PrerenderTestURL("files/prerender/prerender_html5_audio_jsplay.html",
2660 NavigateToDestURL();
2661 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2664 // Checks that video loads are deferred on prerendering.
2665 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5Video
) {
2666 PrerenderTestURL("files/prerender/prerender_html5_video.html",
2669 NavigateToDestURL();
2670 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2673 // Checks that video tags inserted by javascript are deferred and played
2674 // correctly on swap in.
2675 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5VideoJs
) {
2676 PrerenderTestURL("files/prerender/prerender_html5_video_script.html",
2679 NavigateToDestURL();
2680 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2683 // Checks for correct network events by using a busy sleep the javascript.
2684 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5VideoNetwork
) {
2685 DisableJavascriptCalls();
2686 scoped_ptr
<TestPrerender
> prerender
=
2687 PrerenderTestURL("files/prerender/prerender_html5_video_network.html",
2690 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
2691 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
2692 NavigateToDestURL();
2693 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
2696 // Checks that scripts can retrieve the correct window size while prerendering.
2697 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderWindowSize
) {
2698 PrerenderTestURL("files/prerender/prerender_size.html",
2701 NavigateToDestURL();
2704 // TODO(jam): http://crbug.com/350550
2705 #if !(defined(OS_CHROMEOS) && defined(ADDRESS_SANITIZER))
2707 // Checks that prerenderers will terminate when the RenderView crashes.
2708 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderRendererCrash
) {
2709 scoped_ptr
<TestPrerender
> prerender
=
2710 PrerenderTestURL("files/prerender/prerender_page.html",
2711 FINAL_STATUS_RENDERER_CRASHED
,
2714 // Navigate to about:crash and then wait for the renderer to crash.
2715 ASSERT_TRUE(prerender
->contents());
2716 ASSERT_TRUE(prerender
->contents()->prerender_contents());
2717 prerender
->contents()->prerender_contents()->GetController().
2719 GURL(content::kChromeUICrashURL
),
2720 content::Referrer(),
2721 ui::PAGE_TRANSITION_TYPED
,
2723 prerender
->WaitForStop();
2727 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2728 PrerenderPageWithFragment
) {
2729 PrerenderTestURL("files/prerender/prerender_page.html#fragment",
2733 ChannelDestructionWatcher channel_close_watcher
;
2734 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
2735 GetActiveWebContents()->GetRenderProcessHost());
2736 NavigateToDestURL();
2737 channel_close_watcher
.WaitForChannelClose();
2739 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2742 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2743 PrerenderPageWithRedirectedFragment
) {
2745 CreateClientRedirect("files/prerender/prerender_page.html#fragment"),
2749 ChannelDestructionWatcher channel_close_watcher
;
2750 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
2751 GetActiveWebContents()->GetRenderProcessHost());
2752 NavigateToDestURL();
2753 channel_close_watcher
.WaitForChannelClose();
2755 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
2758 // Checks that we do not use a prerendered page when navigating from
2759 // the main page to a fragment.
2760 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2761 PrerenderPageNavigateFragment
) {
2762 PrerenderTestURL("files/prerender/no_prerender_page.html",
2763 FINAL_STATUS_APP_TERMINATING
,
2765 NavigateToURLWithDisposition(
2766 "files/prerender/no_prerender_page.html#fragment",
2767 CURRENT_TAB
, false);
2770 // Checks that we do not use a prerendered page when we prerender a fragment
2771 // but navigate to the main page.
2772 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2773 PrerenderFragmentNavigatePage
) {
2774 PrerenderTestURL("files/prerender/no_prerender_page.html#fragment",
2775 FINAL_STATUS_APP_TERMINATING
,
2777 NavigateToURLWithDisposition(
2778 "files/prerender/no_prerender_page.html",
2779 CURRENT_TAB
, false);
2782 // Checks that we do not use a prerendered page when we prerender a fragment
2783 // but navigate to a different fragment on the same page.
2784 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2785 PrerenderFragmentNavigateFragment
) {
2786 PrerenderTestURL("files/prerender/no_prerender_page.html#other_fragment",
2787 FINAL_STATUS_APP_TERMINATING
,
2789 NavigateToURLWithDisposition(
2790 "files/prerender/no_prerender_page.html#fragment",
2791 CURRENT_TAB
, false);
2794 // Checks that we do not use a prerendered page when the page uses a client
2795 // redirect to refresh from a fragment on the same page.
2796 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2797 PrerenderClientRedirectFromFragment
) {
2799 CreateClientRedirect("files/prerender/no_prerender_page.html#fragment"),
2800 FINAL_STATUS_APP_TERMINATING
,
2802 NavigateToURLWithDisposition(
2803 "files/prerender/no_prerender_page.html",
2804 CURRENT_TAB
, false);
2807 // Checks that we do not use a prerendered page when the page uses a client
2808 // redirect to refresh to a fragment on the same page.
2809 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2810 PrerenderClientRedirectToFragment
) {
2812 CreateClientRedirect("files/prerender/no_prerender_page.html"),
2813 FINAL_STATUS_APP_TERMINATING
,
2815 NavigateToURLWithDisposition(
2816 "files/prerender/no_prerender_page.html#fragment",
2817 CURRENT_TAB
, false);
2820 // Checks that we correctly use a prerendered page when the page uses JS to set
2821 // the window.location.hash to a fragment on the same page.
2822 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
2823 PrerenderPageChangeFragmentLocationHash
) {
2824 PrerenderTestURL("files/prerender/prerender_fragment_location_hash.html",
2827 NavigateToURL("files/prerender/prerender_fragment_location_hash.html");
2830 // Checks that prerendering a PNG works correctly.
2831 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderImagePng
) {
2832 DisableJavascriptCalls();
2833 PrerenderTestURL("files/prerender/image.png", FINAL_STATUS_USED
, 1);
2834 NavigateToDestURL();
2837 // Checks that prerendering a JPG works correctly.
2838 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderImageJpeg
) {
2839 DisableJavascriptCalls();
2840 PrerenderTestURL("files/prerender/image.jpeg", FINAL_STATUS_USED
, 1);
2841 NavigateToDestURL();
2844 // Checks that a prerender of a CRX will result in a cancellation due to
2846 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCrx
) {
2847 PrerenderTestURL("files/prerender/extension.crx", FINAL_STATUS_DOWNLOAD
, 0);
2850 // Checks that xhr GET requests allow prerenders.
2851 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrGet
) {
2852 PrerenderTestURL("files/prerender/prerender_xhr_get.html",
2855 NavigateToDestURL();
2858 // Checks that xhr HEAD requests allow prerenders.
2859 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrHead
) {
2860 PrerenderTestURL("files/prerender/prerender_xhr_head.html",
2863 NavigateToDestURL();
2866 // Checks that xhr OPTIONS requests allow prerenders.
2867 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrOptions
) {
2868 PrerenderTestURL("files/prerender/prerender_xhr_options.html",
2871 NavigateToDestURL();
2874 // Checks that xhr TRACE requests allow prerenders.
2875 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrTrace
) {
2876 PrerenderTestURL("files/prerender/prerender_xhr_trace.html",
2879 NavigateToDestURL();
2882 // Checks that xhr POST requests allow prerenders.
2883 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrPost
) {
2884 PrerenderTestURL("files/prerender/prerender_xhr_post.html",
2887 NavigateToDestURL();
2890 // Checks that xhr PUT cancels prerenders.
2891 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrPut
) {
2892 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
2893 FINAL_STATUS_INVALID_HTTP_METHOD
,
2897 // Checks that xhr DELETE cancels prerenders.
2898 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderXhrDelete
) {
2899 PrerenderTestURL("files/prerender/prerender_xhr_delete.html",
2900 FINAL_STATUS_INVALID_HTTP_METHOD
,
2904 // Checks that a top-level page which would trigger an SSL error is canceled.
2905 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLErrorTopLevel
) {
2906 net::SpawnedTestServer::SSLOptions ssl_options
;
2907 ssl_options
.server_certificate
=
2908 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
2909 net::SpawnedTestServer
https_server(
2910 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2911 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2912 ASSERT_TRUE(https_server
.Start());
2913 GURL https_url
= https_server
.GetURL("files/prerender/prerender_page.html");
2914 PrerenderTestURL(https_url
,
2915 FINAL_STATUS_SSL_ERROR
,
2919 // Checks that an SSL error that comes from a subresource does not cancel
2920 // the page. Non-main-frame requests are simply cancelled if they run into
2922 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLErrorSubresource
) {
2923 net::SpawnedTestServer::SSLOptions ssl_options
;
2924 ssl_options
.server_certificate
=
2925 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
2926 net::SpawnedTestServer
https_server(
2927 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2928 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2929 ASSERT_TRUE(https_server
.Start());
2930 GURL https_url
= https_server
.GetURL("files/prerender/image.jpeg");
2931 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2932 replacement_text
.push_back(
2933 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url
.spec()));
2934 std::string replacement_path
;
2935 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2936 "files/prerender/prerender_with_image.html",
2938 &replacement_path
));
2939 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
2940 NavigateToDestURL();
2943 // Checks that an SSL error that comes from an iframe does not cancel
2944 // the page. Non-main-frame requests are simply cancelled if they run into
2946 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLErrorIframe
) {
2947 net::SpawnedTestServer::SSLOptions ssl_options
;
2948 ssl_options
.server_certificate
=
2949 net::SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
;
2950 net::SpawnedTestServer
https_server(
2951 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
2952 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
2953 ASSERT_TRUE(https_server
.Start());
2954 GURL https_url
= https_server
.GetURL(
2955 "files/prerender/prerender_embedded_content.html");
2956 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
2957 replacement_text
.push_back(
2958 std::make_pair("REPLACE_WITH_URL", https_url
.spec()));
2959 std::string replacement_path
;
2960 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
2961 "files/prerender/prerender_with_iframe.html",
2963 &replacement_path
));
2964 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
2965 NavigateToDestURL();
2968 // Checks that we cancel correctly when window.print() is called.
2969 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPrint
) {
2970 DisableLoadEventCheck();
2971 PrerenderTestURL("files/prerender/prerender_print.html",
2972 FINAL_STATUS_WINDOW_PRINT
,
2976 // Checks that prerenders do not get swapped into target pages that have opened
2977 // popups; the BrowsingInstance is not empty.
2978 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderTargetHasPopup
) {
2979 PrerenderTestURL("files/prerender/prerender_page.html",
2980 FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE
,
2982 OpenURLViaWindowOpen(GURL(url::kAboutBlankURL
));
2984 // Switch back to the current tab and attempt to swap it in.
2985 current_browser()->tab_strip_model()->ActivateTabAt(0, true);
2986 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
2989 class TestClientCertStore
: public net::ClientCertStore
{
2991 TestClientCertStore() {}
2992 ~TestClientCertStore() override
{}
2994 // net::ClientCertStore:
2995 void GetClientCerts(const net::SSLCertRequestInfo
& cert_request_info
,
2996 net::CertificateList
* selected_certs
,
2997 const base::Closure
& callback
) override
{
2998 *selected_certs
= net::CertificateList(
2999 1, scoped_refptr
<net::X509Certificate
>(
3000 new net::X509Certificate("test", "test", base::Time(), base::Time())));
3005 scoped_ptr
<net::ClientCertStore
> CreateCertStore() {
3006 return scoped_ptr
<net::ClientCertStore
>(new TestClientCertStore
);
3009 // Checks that a top-level page which would normally request an SSL client
3010 // certificate will never be seen since it's an https top-level resource.
3011 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3012 PrerenderSSLClientCertTopLevel
) {
3013 ProfileIOData::FromResourceContext(
3014 current_browser()->profile()->GetResourceContext())->
3015 set_client_cert_store_factory_for_testing(
3016 base::Bind(&CreateCertStore
));
3017 net::SpawnedTestServer::SSLOptions ssl_options
;
3018 ssl_options
.request_client_certificate
= true;
3019 net::SpawnedTestServer
https_server(
3020 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
3021 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3022 ASSERT_TRUE(https_server
.Start());
3023 GURL https_url
= https_server
.GetURL("files/prerender/prerender_page.html");
3024 PrerenderTestURL(https_url
, FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED
, 0);
3027 // Checks that an SSL Client Certificate request that originates from a
3028 // subresource will cancel the prerendered page.
3029 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3030 PrerenderSSLClientCertSubresource
) {
3031 ProfileIOData::FromResourceContext(
3032 current_browser()->profile()->GetResourceContext())->
3033 set_client_cert_store_factory_for_testing(
3034 base::Bind(&CreateCertStore
));
3035 net::SpawnedTestServer::SSLOptions ssl_options
;
3036 ssl_options
.request_client_certificate
= true;
3037 net::SpawnedTestServer
https_server(
3038 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
3039 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3040 ASSERT_TRUE(https_server
.Start());
3041 GURL https_url
= https_server
.GetURL("files/prerender/image.jpeg");
3042 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3043 replacement_text
.push_back(
3044 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url
.spec()));
3045 std::string replacement_path
;
3046 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3047 "files/prerender/prerender_with_image.html",
3049 &replacement_path
));
3050 PrerenderTestURL(replacement_path
,
3051 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED
,
3055 // Checks that an SSL Client Certificate request that originates from an
3056 // iframe will cancel the prerendered page.
3057 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSSLClientCertIframe
) {
3058 ProfileIOData::FromResourceContext(
3059 current_browser()->profile()->GetResourceContext())->
3060 set_client_cert_store_factory_for_testing(
3061 base::Bind(&CreateCertStore
));
3062 net::SpawnedTestServer::SSLOptions ssl_options
;
3063 ssl_options
.request_client_certificate
= true;
3064 net::SpawnedTestServer
https_server(
3065 net::SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
3066 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
3067 ASSERT_TRUE(https_server
.Start());
3068 GURL https_url
= https_server
.GetURL(
3069 "files/prerender/prerender_embedded_content.html");
3070 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3071 replacement_text
.push_back(
3072 std::make_pair("REPLACE_WITH_URL", https_url
.spec()));
3073 std::string replacement_path
;
3074 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3075 "files/prerender/prerender_with_iframe.html",
3077 &replacement_path
));
3078 PrerenderTestURL(replacement_path
,
3079 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED
,
3083 #if defined(FULL_SAFE_BROWSING)
3084 // Ensures that we do not prerender pages with a safe browsing
3086 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSafeBrowsingTopLevel
) {
3087 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
3088 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3089 url
, SB_THREAT_TYPE_URL_MALWARE
);
3090 PrerenderTestURL("files/prerender/prerender_page.html",
3091 FINAL_STATUS_SAFE_BROWSING
, 0);
3094 // Ensures that server redirects to a malware page will cancel prerenders.
3095 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3096 PrerenderSafeBrowsingServerRedirect
) {
3097 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
3098 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3099 url
, SB_THREAT_TYPE_URL_MALWARE
);
3100 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
3101 FINAL_STATUS_SAFE_BROWSING
,
3105 // Ensures that client redirects to a malware page will cancel prerenders.
3106 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3107 PrerenderSafeBrowsingClientRedirect
) {
3108 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
3109 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3110 url
, SB_THREAT_TYPE_URL_MALWARE
);
3111 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
3112 FINAL_STATUS_SAFE_BROWSING
,
3116 // Ensures that we do not prerender pages which have a malware subresource.
3117 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSafeBrowsingSubresource
) {
3118 GURL image_url
= test_server()->GetURL("files/prerender/image.jpeg");
3119 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3120 image_url
, SB_THREAT_TYPE_URL_MALWARE
);
3121 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3122 replacement_text
.push_back(
3123 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3124 std::string replacement_path
;
3125 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3126 "files/prerender/prerender_with_image.html",
3128 &replacement_path
));
3129 PrerenderTestURL(replacement_path
,
3130 FINAL_STATUS_SAFE_BROWSING
,
3134 // Ensures that we do not prerender pages which have a malware iframe.
3135 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSafeBrowsingIframe
) {
3136 GURL iframe_url
= test_server()->GetURL(
3137 "files/prerender/prerender_embedded_content.html");
3138 GetFakeSafeBrowsingDatabaseManager()->SetThreatTypeForUrl(
3139 iframe_url
, SB_THREAT_TYPE_URL_MALWARE
);
3140 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3141 replacement_text
.push_back(
3142 std::make_pair("REPLACE_WITH_URL", iframe_url
.spec()));
3143 std::string replacement_path
;
3144 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3145 "files/prerender/prerender_with_iframe.html",
3147 &replacement_path
));
3148 PrerenderTestURL(replacement_path
,
3149 FINAL_STATUS_SAFE_BROWSING
,
3155 // Checks that a local storage read will not cause prerender to fail.
3156 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderLocalStorageRead
) {
3157 PrerenderTestURL("files/prerender/prerender_localstorage_read.html",
3160 NavigateToDestURL();
3163 // Checks that a local storage write will not cause prerender to fail.
3164 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderLocalStorageWrite
) {
3165 PrerenderTestURL("files/prerender/prerender_localstorage_write.html",
3168 NavigateToDestURL();
3171 // Checks that the favicon is properly loaded on prerender.
3172 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderFavicon
) {
3173 scoped_ptr
<TestPrerender
> prerender
=
3174 PrerenderTestURL("files/prerender/prerender_favicon.html",
3177 NavigateToDestURL();
3179 if (!FaviconTabHelper::FromWebContents(
3180 GetActiveWebContents())->FaviconIsValid()) {
3181 // If the favicon has not been set yet, wait for it to be.
3182 content::WindowedNotificationObserver
favicon_update_watcher(
3183 chrome::NOTIFICATION_FAVICON_UPDATED
,
3184 content::Source
<WebContents
>(GetActiveWebContents()));
3185 favicon_update_watcher
.Wait();
3187 EXPECT_TRUE(FaviconTabHelper::FromWebContents(
3188 GetActiveWebContents())->FaviconIsValid());
3191 // Checks that when a prerendered page is swapped in to a referring page, the
3192 // unload handlers on the referring page are executed.
3193 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderUnload
) {
3194 // Matches URL in prerender_loader_with_unload.html.
3195 const GURL
unload_url("http://unload-url.test");
3196 base::FilePath empty_file
= ui_test_utils::GetTestFilePath(
3197 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3198 RequestCounter unload_counter
;
3199 BrowserThread::PostTask(
3200 BrowserThread::IO
, FROM_HERE
,
3201 base::Bind(&CreateCountingInterceptorOnIO
,
3202 unload_url
, empty_file
, unload_counter
.AsWeakPtr()));
3204 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3205 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3206 NavigateToDestURL();
3207 unload_counter
.WaitForCount(1);
3210 // Checks that a hanging unload on the referring page of a prerender swap does
3211 // not crash the browser on exit.
3212 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHangingUnload
) {
3213 // Matches URL in prerender_loader_with_unload.html.
3214 const GURL
hang_url("http://unload-url.test");
3215 base::FilePath empty_file
= ui_test_utils::GetTestFilePath(
3216 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3217 BrowserThread::PostTask(
3218 BrowserThread::IO
, FROM_HERE
,
3219 base::Bind(&CreateHangingFirstRequestInterceptorOnIO
,
3220 hang_url
, empty_file
,
3223 set_loader_path("files/prerender/prerender_loader_with_unload.html");
3224 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3225 NavigateToDestURL();
3229 // Checks that when the history is cleared, prerendering is cancelled and
3230 // prerendering history is cleared.
3231 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClearHistory
) {
3232 scoped_ptr
<TestPrerender
> prerender
=
3233 PrerenderTestURL("files/prerender/prerender_page.html",
3234 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED
,
3237 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_HISTORY
);
3238 prerender
->WaitForStop();
3240 // Make sure prerender history was cleared.
3241 EXPECT_EQ(0, GetHistoryLength());
3244 // Checks that when the cache is cleared, prerenders are cancelled but
3245 // prerendering history is not cleared.
3246 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClearCache
) {
3247 scoped_ptr
<TestPrerender
> prerender
=
3248 PrerenderTestURL("files/prerender/prerender_page.html",
3249 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED
,
3252 ClearBrowsingData(current_browser(), BrowsingDataRemover::REMOVE_CACHE
);
3253 prerender
->WaitForStop();
3255 // Make sure prerender history was not cleared. Not a vital behavior, but
3256 // used to compare with PrerenderClearHistory test.
3257 EXPECT_EQ(1, GetHistoryLength());
3260 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCancelAll
) {
3261 scoped_ptr
<TestPrerender
> prerender
=
3262 PrerenderTestURL("files/prerender/prerender_page.html",
3263 FINAL_STATUS_CANCELLED
,
3266 GetPrerenderManager()->CancelAllPrerenders();
3267 prerender
->WaitForStop();
3269 EXPECT_FALSE(prerender
->contents());
3272 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderEvents
) {
3273 scoped_ptr
<TestPrerender
> prerender
=
3274 PrerenderTestURL("files/prerender/prerender_page.html",
3275 FINAL_STATUS_CANCELLED
, 1);
3277 GetPrerenderManager()->CancelAllPrerenders();
3278 prerender
->WaitForStop();
3280 EXPECT_TRUE(DidReceivePrerenderStartEventForLinkNumber(0));
3281 EXPECT_TRUE(DidReceivePrerenderStopEventForLinkNumber(0));
3282 EXPECT_FALSE(HadPrerenderEventErrors());
3285 // Cancels the prerender of a page with its own prerender. The second prerender
3286 // should never be started.
3287 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3288 PrerenderCancelPrerenderWithPrerender
) {
3289 scoped_ptr
<TestPrerender
> prerender
=
3290 PrerenderTestURL("files/prerender/prerender_infinite_a.html",
3291 FINAL_STATUS_CANCELLED
,
3294 GetPrerenderManager()->CancelAllPrerenders();
3295 prerender
->WaitForStop();
3297 EXPECT_FALSE(prerender
->contents());
3300 // Prerendering and history tests.
3301 // The prerendered page is navigated to in several ways [navigate via
3302 // omnibox, click on link, key-modified click to open in background tab, etc],
3303 // followed by a navigation to another page from the prerendered page, followed
3304 // by a back navigation.
3306 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNavigateClickGoBack
) {
3307 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3310 NavigateToDestURL();
3311 ClickToNextPageAfterPrerender();
3312 GoBackToPrerender();
3315 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNavigateNavigateGoBack
) {
3316 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3319 NavigateToDestURL();
3320 NavigateToNextPageAfterPrerender();
3321 GoBackToPrerender();
3324 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickClickGoBack
) {
3325 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3328 OpenDestURLViaClick();
3329 ClickToNextPageAfterPrerender();
3330 GoBackToPrerender();
3333 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickNavigateGoBack
) {
3334 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3337 OpenDestURLViaClick();
3338 NavigateToNextPageAfterPrerender();
3339 GoBackToPrerender();
3342 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickNewWindow
) {
3343 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3344 FINAL_STATUS_APP_TERMINATING
, 1);
3345 OpenDestURLViaClickNewWindow();
3348 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderClickNewForegroundTab
) {
3349 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
3350 FINAL_STATUS_APP_TERMINATING
, 1);
3351 OpenDestURLViaClickNewForegroundTab();
3354 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3355 NavigateToPrerenderedPageWhenDevToolsAttached
) {
3356 DisableJavascriptCalls();
3357 WebContents
* web_contents
=
3358 current_browser()->tab_strip_model()->GetActiveWebContents();
3359 scoped_refptr
<DevToolsAgentHost
> agent(
3360 DevToolsAgentHost::GetOrCreateFor(web_contents
));
3361 FakeDevToolsClient client
;
3362 agent
->AttachClient(&client
);
3363 const char* url
= "files/prerender/prerender_page.html";
3364 PrerenderTestURL(url
, FINAL_STATUS_DEVTOOLS_ATTACHED
, 1);
3365 NavigateToURLWithDisposition(url
, CURRENT_TAB
, false);
3366 agent
->DetachClient();
3369 // Validate that the sessionStorage namespace remains the same when swapping
3370 // in a prerendered page.
3371 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderSessionStorage
) {
3372 set_loader_path("files/prerender/prerender_loader_with_session_storage.html");
3373 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
3376 NavigateToDestURL();
3377 GoBackToPageBeforePrerender();
3380 // Checks that the control group works. An XHR PUT cannot be detected in the
3382 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, ControlGroup
) {
3383 RestorePrerenderMode restore_prerender_mode
;
3384 PrerenderManager::SetMode(
3385 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP
);
3386 DisableJavascriptCalls();
3387 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3388 FINAL_STATUS_WOULD_HAVE_BEEN_USED
, 0);
3389 NavigateToDestURL();
3392 // Checks that the control group correctly hits WOULD_HAVE_BEEN_USED
3393 // renderer-initiated navigations. (This verifies that the ShouldFork logic
3394 // behaves correctly.)
3395 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, ControlGroupRendererInitiated
) {
3396 RestorePrerenderMode restore_prerender_mode
;
3397 PrerenderManager::SetMode(
3398 PrerenderManager::PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP
);
3399 DisableJavascriptCalls();
3400 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3401 FINAL_STATUS_WOULD_HAVE_BEEN_USED
, 0);
3402 OpenDestURLViaClick();
3405 // Make sure that the MatchComplete dummy works in the normal case. Once
3406 // a prerender is cancelled because of a script, a dummy must be created to
3407 // account for the MatchComplete case, and it must have a final status of
3408 // FINAL_STATUS_WOULD_HAVE_BEEN_USED.
3409 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, MatchCompleteDummy
) {
3410 RestorePrerenderMode restore_prerender_mode
;
3411 PrerenderManager::SetMode(
3412 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3414 std::vector
<FinalStatus
> expected_final_status_queue
;
3415 expected_final_status_queue
.push_back(FINAL_STATUS_INVALID_HTTP_METHOD
);
3416 expected_final_status_queue
.push_back(FINAL_STATUS_WOULD_HAVE_BEEN_USED
);
3417 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
3418 expected_final_status_queue
, 1);
3419 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3420 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3421 histogram_tester().ExpectTotalCount(
3422 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3423 histogram_tester().ExpectTotalCount(
3424 "Prerender.websame_PrerenderNotSwappedInPLT", 1);
3426 NavigateToDestURL();
3427 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3428 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3430 histogram_tester().ExpectTotalCount(
3431 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3434 class PrerenderBrowserTestWithNaCl
: public PrerenderBrowserTest
{
3436 PrerenderBrowserTestWithNaCl() {}
3437 ~PrerenderBrowserTestWithNaCl() override
{}
3439 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
3440 PrerenderBrowserTest::SetUpCommandLine(command_line
);
3441 command_line
->AppendSwitch(switches::kEnableNaCl
);
3445 // Check that NaCl plugins work when enabled, with prerendering.
3446 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithNaCl
,
3447 PrerenderNaClPluginEnabled
) {
3448 #if defined(OS_WIN) && defined(USE_ASH)
3449 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
3450 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
3451 switches::kAshBrowserTests
))
3455 PrerenderTestURL("files/prerender/prerender_plugin_nacl_enabled.html",
3458 NavigateToDestURL();
3460 // To avoid any chance of a race, we have to let the script send its response
3462 WebContents
* web_contents
=
3463 browser()->tab_strip_model()->GetActiveWebContents();
3464 bool display_test_result
= false;
3465 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents
,
3466 "DidDisplayReallyPass()",
3467 &display_test_result
));
3468 ASSERT_TRUE(display_test_result
);
3471 // Checks that the referrer policy is used when prerendering.
3472 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderReferrerPolicy
) {
3473 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3474 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3477 NavigateToDestURL();
3480 // Checks that the referrer policy is used when prerendering on HTTPS.
3481 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3482 PrerenderSSLReferrerPolicy
) {
3483 UseHttpsSrcServer();
3484 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3485 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3488 NavigateToDestURL();
3491 // Checks that the referrer policy is used when prerendering is cancelled.
3492 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCancelReferrerPolicy
) {
3493 scoped_ptr
<TestContentBrowserClient
> test_content_browser_client(
3494 new TestContentBrowserClient
);
3495 content::ContentBrowserClient
* original_browser_client
=
3496 content::SetBrowserClientForTesting(test_content_browser_client
.get());
3498 set_loader_path("files/prerender/prerender_loader_with_referrer_policy.html");
3499 PrerenderTestURL("files/prerender/prerender_referrer_policy.html",
3500 FINAL_STATUS_CANCELLED
,
3502 OpenDestURLViaClick();
3504 bool display_test_result
= false;
3505 WebContents
* web_contents
=
3506 browser()->tab_strip_model()->GetActiveWebContents();
3507 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
3509 "window.domAutomationController.send(DidDisplayPass())",
3510 &display_test_result
));
3511 EXPECT_TRUE(display_test_result
);
3513 content::SetBrowserClientForTesting(original_browser_client
);
3516 // Test interaction of the webNavigation and tabs API with prerender.
3517 class PrerenderBrowserTestWithExtensions
: public PrerenderBrowserTest
,
3518 public ExtensionApiTest
{
3520 PrerenderBrowserTestWithExtensions() {
3521 // The individual tests start the test server through ExtensionApiTest, so
3522 // the port number can be passed through to the extension.
3523 autostart_test_server_
= false;
3526 void SetUp() override
{ PrerenderBrowserTest::SetUp(); }
3528 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
3529 PrerenderBrowserTest::SetUpCommandLine(command_line
);
3530 ExtensionApiTest::SetUpCommandLine(command_line
);
3533 void SetUpInProcessBrowserTestFixture() override
{
3534 PrerenderBrowserTest::SetUpInProcessBrowserTestFixture();
3535 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
3538 void TearDownInProcessBrowserTestFixture() override
{
3539 PrerenderBrowserTest::TearDownInProcessBrowserTestFixture();
3540 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
3543 void SetUpOnMainThread() override
{
3544 PrerenderBrowserTest::SetUpOnMainThread();
3548 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions
, WebNavigation
) {
3549 ASSERT_TRUE(StartSpawnedTestServer());
3550 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3552 // Wait for the extension to set itself up and return control to us.
3553 ASSERT_TRUE(RunExtensionTest("webnavigation/prerender")) << message_
;
3555 extensions::ResultCatcher catcher
;
3557 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3559 ChannelDestructionWatcher channel_close_watcher
;
3560 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
3561 GetActiveWebContents()->GetRenderProcessHost());
3562 NavigateToDestURL();
3563 channel_close_watcher
.WaitForChannelClose();
3565 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3566 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
3569 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions
, TabsApi
) {
3570 ASSERT_TRUE(StartSpawnedTestServer());
3571 extensions::FrameNavigationState::set_allow_extension_scheme(true);
3573 // Wait for the extension to set itself up and return control to us.
3574 ASSERT_TRUE(RunExtensionTest("tabs/on_replaced")) << message_
;
3576 extensions::ResultCatcher catcher
;
3578 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3580 ChannelDestructionWatcher channel_close_watcher
;
3581 channel_close_watcher
.WatchChannel(browser()->tab_strip_model()->
3582 GetActiveWebContents()->GetRenderProcessHost());
3583 NavigateToDestURL();
3584 channel_close_watcher
.WaitForChannelClose();
3586 ASSERT_TRUE(IsEmptyPrerenderLinkManager());
3587 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
3590 // Test that prerenders abort when navigating to a stream.
3591 // See chrome/browser/extensions/api/streams_private/streams_private_apitest.cc
3592 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTestWithExtensions
, StreamsTest
) {
3593 RestorePrerenderMode restore_prerender_mode
;
3594 PrerenderManager::SetMode(
3595 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3597 ASSERT_TRUE(StartSpawnedTestServer());
3599 const extensions::Extension
* extension
= LoadExtension(
3600 test_data_dir_
.AppendASCII("streams_private/handle_mime_type"));
3601 ASSERT_TRUE(extension
);
3602 EXPECT_EQ(std::string(extension_misc::kMimeHandlerPrivateTestExtensionId
),
3604 MimeTypesHandler
* handler
= MimeTypesHandler::GetHandler(extension
);
3605 ASSERT_TRUE(handler
);
3606 EXPECT_TRUE(handler
->CanHandleMIMEType("application/msword"));
3608 PrerenderTestURL("files/prerender/document.doc", FINAL_STATUS_DOWNLOAD
, 0);
3610 // Sanity-check that the extension would have picked up the stream in a normal
3611 // navigation had prerender not intercepted it.
3612 // streams_private/handle_mime_type reports success if it has handled the
3613 // application/msword type.
3614 extensions::ResultCatcher catcher
;
3615 NavigateToDestURL();
3616 EXPECT_TRUE(catcher
.GetNextResult());
3619 // Checks that non-http/https/chrome-extension subresource cancels the
3621 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3622 PrerenderCancelSubresourceUnsupportedScheme
) {
3623 GURL image_url
= GURL("invalidscheme://www.google.com/test.jpg");
3624 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3625 replacement_text
.push_back(
3626 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3627 std::string replacement_path
;
3628 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3629 "files/prerender/prerender_with_image.html",
3631 &replacement_path
));
3632 PrerenderTestURL(replacement_path
, FINAL_STATUS_UNSUPPORTED_SCHEME
, 0);
3635 // Ensure that about:blank is permitted for any subresource.
3636 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3637 PrerenderAllowAboutBlankSubresource
) {
3638 GURL image_url
= GURL("about:blank");
3639 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3640 replacement_text
.push_back(
3641 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3642 std::string replacement_path
;
3643 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3644 "files/prerender/prerender_with_image.html",
3646 &replacement_path
));
3647 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
3648 NavigateToDestURL();
3651 // Checks that non-http/https/chrome-extension subresource cancels the prerender
3653 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3654 PrerenderCancelSubresourceRedirectUnsupportedScheme
) {
3655 GURL image_url
= test_server()->GetURL(
3656 CreateServerRedirect("invalidscheme://www.google.com/test.jpg"));
3657 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3658 replacement_text
.push_back(
3659 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3660 std::string replacement_path
;
3661 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3662 "files/prerender/prerender_with_image.html",
3664 &replacement_path
));
3665 PrerenderTestURL(replacement_path
, FINAL_STATUS_UNSUPPORTED_SCHEME
, 0);
3668 // Checks that chrome-extension subresource does not cancel the prerender.
3669 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3670 PrerenderKeepSubresourceExtensionScheme
) {
3671 GURL image_url
= GURL("chrome-extension://abcdefg/test.jpg");
3672 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3673 replacement_text
.push_back(
3674 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3675 std::string replacement_path
;
3676 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3677 "files/prerender/prerender_with_image.html",
3679 &replacement_path
));
3680 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
3681 NavigateToDestURL();
3684 // Checks that redirect to chrome-extension subresource does not cancel the
3686 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3687 PrerenderKeepSubresourceRedirectExtensionScheme
) {
3688 GURL image_url
= test_server()->GetURL(
3689 CreateServerRedirect("chrome-extension://abcdefg/test.jpg"));
3690 std::vector
<net::SpawnedTestServer::StringPair
> replacement_text
;
3691 replacement_text
.push_back(
3692 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url
.spec()));
3693 std::string replacement_path
;
3694 ASSERT_TRUE(net::SpawnedTestServer::GetFilePathWithReplacements(
3695 "files/prerender/prerender_with_image.html",
3697 &replacement_path
));
3698 PrerenderTestURL(replacement_path
, FINAL_STATUS_USED
, 1);
3699 NavigateToDestURL();
3702 // Checks that non-http/https main page redirects cancel the prerender.
3703 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3704 PrerenderCancelMainFrameRedirectUnsupportedScheme
) {
3705 GURL url
= test_server()->GetURL(
3706 CreateServerRedirect("invalidscheme://www.google.com/test.html"));
3707 PrerenderTestURL(url
, FINAL_STATUS_UNSUPPORTED_SCHEME
, 0);
3710 // Checks that media source video loads are deferred on prerendering.
3711 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderHTML5MediaSourceVideo
) {
3712 PrerenderTestURL("files/prerender/prerender_html5_video_media_source.html",
3715 NavigateToDestURL();
3716 WaitForASCIITitle(GetActiveWebContents(), kPassTitle
);
3719 // Checks that a prerender that creates an audio stream (via a WebAudioDevice)
3721 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderWebAudioDevice
) {
3722 DisableLoadEventCheck();
3723 PrerenderTestURL("files/prerender/prerender_web_audio_device.html",
3724 FINAL_STATUS_CREATING_AUDIO_STREAM
, 0);
3727 // Checks that prerenders do not swap in to WebContents being captured.
3728 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCapturedWebContents
) {
3729 PrerenderTestURL("files/prerender/prerender_page.html",
3730 FINAL_STATUS_PAGE_BEING_CAPTURED
, 1);
3731 WebContents
* web_contents
= GetActiveWebContents();
3732 web_contents
->IncrementCapturerCount(gfx::Size());
3733 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
3734 web_contents
->DecrementCapturerCount();
3737 // Checks that prerenders are aborted on cross-process navigation from
3738 // a server redirect.
3739 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3740 PrerenderCrossProcessServerRedirect
) {
3741 // Force everything to be a process swap.
3742 SwapProcessesContentBrowserClient test_browser_client
;
3743 content::ContentBrowserClient
* original_browser_client
=
3744 content::SetBrowserClientForTesting(&test_browser_client
);
3747 CreateServerRedirect("files/prerender/prerender_page.html"),
3748 FINAL_STATUS_OPEN_URL
, 0);
3750 content::SetBrowserClientForTesting(original_browser_client
);
3753 // Checks that URLRequests for prerenders being aborted on cross-process
3754 // navigation from a server redirect are cleaned up, so they don't keep cache
3756 // See http://crbug.com/341134
3757 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3758 PrerenderCrossProcessServerRedirectNoHang
) {
3759 const char kDestPath
[] = "files/prerender/prerender_page.html";
3760 // Force everything to be a process swap.
3761 SwapProcessesContentBrowserClient test_browser_client
;
3762 content::ContentBrowserClient
* original_browser_client
=
3763 content::SetBrowserClientForTesting(&test_browser_client
);
3765 PrerenderTestURL(CreateServerRedirect(kDestPath
), FINAL_STATUS_OPEN_URL
, 0);
3767 ui_test_utils::NavigateToURL(
3769 test_server()->GetURL(kDestPath
));
3771 content::SetBrowserClientForTesting(original_browser_client
);
3774 // Checks that prerenders are aborted on cross-process navigation from
3775 // a client redirect.
3776 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3777 PrerenderCrossProcessClientRedirect
) {
3778 // Cross-process navigation logic for renderer-initiated navigations
3779 // is partially controlled by the renderer, namely
3780 // ChromeContentRendererClient. This test instead relies on the Web
3781 // Store triggering such navigations.
3782 std::string webstore_url
= extension_urls::GetWebstoreLaunchURL();
3784 // Mock out requests to the Web Store.
3785 base::FilePath
file(GetTestPath("prerender_page.html"));
3786 BrowserThread::PostTask(
3787 BrowserThread::IO
, FROM_HERE
,
3788 base::Bind(&CreateMockInterceptorOnIO
, GURL(webstore_url
), file
));
3790 PrerenderTestURL(CreateClientRedirect(webstore_url
),
3791 FINAL_STATUS_OPEN_URL
, 1);
3794 // Checks that canceling a MatchComplete dummy doesn't result in two
3796 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, CancelMatchCompleteDummy
) {
3797 RestorePrerenderMode restore_prerender_mode
;
3798 PrerenderManager::SetMode(
3799 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3801 std::vector
<FinalStatus
> expected_final_status_queue
;
3802 expected_final_status_queue
.push_back(FINAL_STATUS_JAVASCRIPT_ALERT
);
3803 expected_final_status_queue
.push_back(FINAL_STATUS_CANCELLED
);
3804 ScopedVector
<TestPrerender
> prerenders
=
3805 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
3806 expected_final_status_queue
, 0);
3808 // Cancel the MatchComplete dummy.
3809 GetPrerenderManager()->CancelAllPrerenders();
3810 prerenders
[1]->WaitForStop();
3812 // Check the referring page only got one copy of the event.
3813 EXPECT_FALSE(HadPrerenderEventErrors());
3816 // Checks that a deferred redirect to an image is not loaded until the page is
3817 // visible. Also test the right histogram events are emitted in this case.
3818 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDeferredImage
) {
3819 DisableJavascriptCalls();
3821 // The prerender will not completely load until after the swap, so wait for a
3822 // title change before calling DidPrerenderPass.
3823 scoped_ptr
<TestPrerender
> prerender
=
3825 "files/prerender/prerender_deferred_image.html",
3826 FINAL_STATUS_USED
, 0);
3827 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
3828 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0));
3829 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
3830 EXPECT_EQ(0, prerender
->number_of_loads());
3831 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
3832 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
3833 histogram_tester().ExpectTotalCount(
3834 "Prerender.none_PerceivedPLTMatchedComplete", 0);
3835 histogram_tester().ExpectTotalCount(
3836 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3839 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
3840 GetActiveWebContents());
3841 ui_test_utils::NavigateToURLWithDisposition(
3842 current_browser(), dest_url(), CURRENT_TAB
,
3843 ui_test_utils::BROWSER_TEST_NONE
);
3844 swap_observer
.Wait();
3846 // The prerender never observes the final load.
3847 EXPECT_EQ(0, prerender
->number_of_loads());
3849 // Now check DidDisplayPass.
3850 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3852 histogram_tester().ExpectTotalCount(
3853 "Prerender.websame_PrerenderNotSwappedInPLT", 0);
3854 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLT", 1);
3855 histogram_tester().ExpectTotalCount("Prerender.websame_PerceivedPLTMatched",
3857 histogram_tester().ExpectTotalCount(
3858 "Prerender.websame_PerceivedPLTMatchedComplete", 1);
3861 // Checks that a deferred redirect to an image is not loaded until the
3862 // page is visible, even after another redirect.
3863 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3864 PrerenderDeferredImageAfterRedirect
) {
3865 DisableJavascriptCalls();
3867 // The prerender will not completely load until after the swap, so wait for a
3868 // title change before calling DidPrerenderPass.
3869 scoped_ptr
<TestPrerender
> prerender
=
3871 "files/prerender/prerender_deferred_image.html",
3872 FINAL_STATUS_USED
, 0);
3873 WaitForASCIITitle(prerender
->contents()->prerender_contents(), kReadyTitle
);
3874 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
3875 EXPECT_EQ(0, prerender
->number_of_loads());
3878 NavigationOrSwapObserver
swap_observer(current_browser()->tab_strip_model(),
3879 GetActiveWebContents());
3880 ui_test_utils::NavigateToURLWithDisposition(
3881 current_browser(), dest_url(), CURRENT_TAB
,
3882 ui_test_utils::BROWSER_TEST_NONE
);
3883 swap_observer
.Wait();
3885 // The prerender never observes the final load.
3886 EXPECT_EQ(0, prerender
->number_of_loads());
3888 // Now check DidDisplayPass.
3889 EXPECT_TRUE(DidDisplayPass(GetActiveWebContents()));
3892 // Checks that deferred redirects in the main frame are followed.
3893 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDeferredMainFrame
) {
3894 DisableJavascriptCalls();
3896 "files/prerender/image-deferred.png",
3897 FINAL_STATUS_USED
, 1);
3898 NavigateToDestURL();
3901 // Checks that deferred redirects in the main frame are followed, even
3902 // with a double-redirect.
3903 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3904 PrerenderDeferredMainFrameAfterRedirect
) {
3905 DisableJavascriptCalls();
3907 CreateServerRedirect("files/prerender/image-deferred.png"),
3908 FINAL_STATUS_USED
, 1);
3909 NavigateToDestURL();
3912 // Checks that deferred redirects in a synchronous XHR abort the
3914 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderDeferredSynchronousXHR
) {
3915 RestorePrerenderMode restore_prerender_mode
;
3916 PrerenderManager::SetMode(
3917 PrerenderManager::PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP
);
3918 PrerenderTestURL("files/prerender/prerender_deferred_sync_xhr.html",
3919 FINAL_STATUS_BAD_DEFERRED_REDIRECT
, 0);
3920 NavigateToDestURL();
3923 // Checks that prerenders are not swapped for navigations with extra headers.
3924 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderExtraHeadersNoSwap
) {
3925 PrerenderTestURL("files/prerender/prerender_page.html",
3926 FINAL_STATUS_APP_TERMINATING
, 1);
3928 content::OpenURLParams
params(dest_url(), Referrer(), CURRENT_TAB
,
3929 ui::PAGE_TRANSITION_TYPED
, false);
3930 params
.extra_headers
= "X-Custom-Header: 42\r\n";
3931 NavigateToURLWithParams(params
, false);
3934 // Checks that prerenders are not swapped for navigations with browser-initiated
3936 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
3937 PrerenderBrowserInitiatedPostNoSwap
) {
3938 PrerenderTestURL("files/prerender/prerender_page.html",
3939 FINAL_STATUS_APP_TERMINATING
, 1);
3941 std::string post_data
= "DATA";
3942 content::OpenURLParams
params(dest_url(), Referrer(), CURRENT_TAB
,
3943 ui::PAGE_TRANSITION_TYPED
, false);
3944 params
.uses_post
= true;
3945 params
.browser_initiated_post_data
=
3946 base::RefCountedString::TakeString(&post_data
);
3947 NavigateToURLWithParams(params
, false);
3950 // Checks that the prerendering of a page is canceled correctly when the
3951 // prerendered page tries to make a second navigation entry.
3952 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderNewNavigationEntry
) {
3953 PrerenderTestURL("files/prerender/prerender_new_entry.html",
3954 FINAL_STATUS_NEW_NAVIGATION_ENTRY
,
3958 // Attempt a swap-in in a new tab. The session storage doesn't match, so it
3960 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPageNewTab
) {
3961 PrerenderTestURL("files/prerender/prerender_page.html",
3962 FINAL_STATUS_APP_TERMINATING
, 1);
3964 // Open a new tab to navigate in.
3965 ui_test_utils::NavigateToURLWithDisposition(
3966 current_browser(), GURL(url::kAboutBlankURL
), NEW_FOREGROUND_TAB
,
3967 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
3969 // Now navigate in the new tab.
3970 NavigateToDestURLWithDisposition(CURRENT_TAB
, false);
3973 // Checks that prerenders honor |should_replace_current_entry|.
3974 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderReplaceCurrentEntry
) {
3975 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
3977 content::OpenURLParams
params(dest_url(), Referrer(), CURRENT_TAB
,
3978 ui::PAGE_TRANSITION_TYPED
, false);
3979 params
.should_replace_current_entry
= true;
3980 NavigateToURLWithParams(params
, false);
3982 const NavigationController
& controller
=
3983 GetActiveWebContents()->GetController();
3984 // First entry is about:blank, second is prerender_page.html.
3985 EXPECT_TRUE(controller
.GetPendingEntry() == NULL
);
3986 EXPECT_EQ(2, controller
.GetEntryCount());
3987 EXPECT_EQ(GURL(url::kAboutBlankURL
), controller
.GetEntryAtIndex(0)->GetURL());
3988 EXPECT_EQ(dest_url(), controller
.GetEntryAtIndex(1)->GetURL());
3991 // Checks that <a ping> requests are not dropped in prerender.
3992 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPing
) {
3993 // Count hits to a certain URL.
3994 const GURL
kPingURL("http://prerender.test/ping");
3995 base::FilePath empty_file
= ui_test_utils::GetTestFilePath(
3996 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("empty.html")));
3997 RequestCounter ping_counter
;
3998 BrowserThread::PostTask(
3999 BrowserThread::IO
, FROM_HERE
,
4000 base::Bind(&CreateCountingInterceptorOnIO
,
4001 kPingURL
, empty_file
, ping_counter
.AsWeakPtr()));
4003 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
4004 OpenDestURLViaClickPing(kPingURL
);
4006 ping_counter
.WaitForCount(1);
4009 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderPPLTNormalNavigation
) {
4010 GURL url
= test_server()->GetURL("files/prerender/prerender_page.html");
4011 ui_test_utils::NavigateToURL(current_browser(), url
);
4012 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1);
4013 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLTMatched", 0);
4014 histogram_tester().ExpectTotalCount(
4015 "Prerender.none_PerceivedPLTMatchedComplete", 0);
4018 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
4019 PrerenderCookieChangeConflictTest
) {
4020 NavigateStraightToURL(
4021 "files/prerender/prerender_cookie.html?set=1&key=c&value=1");
4023 GURL url
= test_server()->GetURL(
4024 "files/prerender/prerender_cookie.html?set=1&key=c&value=2");
4026 scoped_ptr
<TestPrerender
> prerender
=
4027 ExpectPrerender(FINAL_STATUS_COOKIE_CONFLICT
);
4028 AddPrerender(url
, 1);
4029 prerender
->WaitForStart();
4030 prerender
->WaitForLoads(1);
4031 // Ensure that in the prerendered page, querying the cookie again
4032 // via javascript yields the same value that was set during load.
4033 EXPECT_TRUE(DidPrerenderPass(prerender
->contents()->prerender_contents()));
4035 // The prerender has loaded. Ensure that the change is not visible
4038 RunJSReturningString("GetCookie('c')", &value
);
4039 ASSERT_EQ(value
, "1");
4041 // Make a conflicting cookie change, which should cancel the prerender.
4042 RunJS("SetCookie('c', '3')");
4043 prerender
->WaitForStop();
4046 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderCookieChangeUseTest
) {
4047 // Permit 2 concurrent prerenders.
4048 GetPrerenderManager()->mutable_config().max_link_concurrency
= 2;
4049 GetPrerenderManager()->mutable_config().max_link_concurrency_per_launcher
= 2;
4051 // Go to a first URL setting the cookie to value "1".
4052 NavigateStraightToURL(
4053 "files/prerender/prerender_cookie.html?set=1&key=c&value=1");
4055 // Prerender a URL setting the cookie to value "2".
4056 GURL url
= test_server()->GetURL(
4057 "files/prerender/prerender_cookie.html?set=1&key=c&value=2");
4059 scoped_ptr
<TestPrerender
> prerender1
= ExpectPrerender(FINAL_STATUS_USED
);
4060 AddPrerender(url
, 1);
4061 prerender1
->WaitForStart();
4062 prerender1
->WaitForLoads(1);
4064 // Launch a second prerender, setting the cookie to value "3".
4065 scoped_ptr
<TestPrerender
> prerender2
=
4066 ExpectPrerender(FINAL_STATUS_COOKIE_CONFLICT
);
4067 AddPrerender(test_server()->GetURL(
4068 "files/prerender/prerender_cookie.html?set=1&key=c&value=3"), 1);
4069 prerender2
->WaitForStart();
4070 prerender2
->WaitForLoads(1);
4072 // Both prerenders have loaded. Ensure that the visible tab is still
4073 // unchanged and cannot see their changes.
4076 RunJSReturningString("GetCookie('c')", &value
);
4077 ASSERT_EQ(value
, "1");
4079 // Navigate to the prerendered URL. The first prerender should be swapped in,
4080 // and the changes should now be visible. The second prerender should
4081 // be cancelled due to the conflict.
4082 ui_test_utils::NavigateToURLWithDisposition(
4086 ui_test_utils::BROWSER_TEST_NONE
);
4087 RunJSReturningString("GetCookie('c')", &value
);
4088 ASSERT_EQ(value
, "2");
4089 prerender2
->WaitForStop();
4092 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
4093 PrerenderCookieChangeConflictHTTPHeaderTest
) {
4094 NavigateStraightToURL(
4095 "files/prerender/prerender_cookie.html?set=1&key=c&value=1");
4097 GURL url
= test_server()->GetURL("set-cookie?c=2");
4098 scoped_ptr
<TestPrerender
> prerender
=
4099 ExpectPrerender(FINAL_STATUS_COOKIE_CONFLICT
);
4100 AddPrerender(url
, 1);
4101 prerender
->WaitForStart();
4102 prerender
->WaitForLoads(1);
4104 // The prerender has loaded. Ensure that the change is not visible
4107 RunJSReturningString("GetCookie('c')", &value
);
4108 ASSERT_EQ(value
, "1");
4110 // Make a conflicting cookie change, which should cancel the prerender.
4111 RunJS("SetCookie('c', '3')");
4112 prerender
->WaitForStop();
4115 // Checks that a prerender which calls window.close() on itself is aborted.
4116 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
, PrerenderWindowClose
) {
4117 DisableLoadEventCheck();
4118 PrerenderTestURL("files/prerender/prerender_window_close.html",
4119 FINAL_STATUS_CLOSED
, 0);
4122 class PrerenderIncognitoBrowserTest
: public PrerenderBrowserTest
{
4124 void SetUpOnMainThread() override
{
4125 Profile
* normal_profile
= current_browser()->profile();
4126 set_browser(ui_test_utils::OpenURLOffTheRecord(
4127 normal_profile
, GURL("about:blank")));
4128 PrerenderBrowserTest::SetUpOnMainThread();
4132 // Checks that prerendering works in incognito mode.
4133 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest
, PrerenderIncognito
) {
4134 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED
, 1);
4135 NavigateToDestURL();
4138 // Checks that prerenders are aborted when an incognito profile is closed.
4139 IN_PROC_BROWSER_TEST_F(PrerenderIncognitoBrowserTest
,
4140 PrerenderIncognitoClosed
) {
4141 scoped_ptr
<TestPrerender
> prerender
=
4142 PrerenderTestURL("files/prerender/prerender_page.html",
4143 FINAL_STATUS_PROFILE_DESTROYED
, 1);
4144 current_browser()->window()->Close();
4145 prerender
->WaitForStop();
4148 class PrerenderOmniboxBrowserTest
: public PrerenderBrowserTest
{
4150 LocationBar
* GetLocationBar() {
4151 return current_browser()->window()->GetLocationBar();
4154 OmniboxView
* GetOmniboxView() {
4155 return GetLocationBar()->GetOmniboxView();
4158 void WaitForAutocompleteDone(OmniboxView
* omnibox_view
) {
4159 AutocompleteController
* controller
=
4160 omnibox_view
->model()->popup_model()->autocomplete_controller();
4161 while (!controller
->done()) {
4162 content::WindowedNotificationObserver
ready_observer(
4163 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
4164 content::Source
<AutocompleteController
>(controller
));
4165 ready_observer
.Wait();
4169 predictors::AutocompleteActionPredictor
* GetAutocompleteActionPredictor() {
4170 Profile
* profile
= current_browser()->profile();
4171 return predictors::AutocompleteActionPredictorFactory::GetForProfile(
4175 scoped_ptr
<TestPrerender
> StartOmniboxPrerender(
4177 FinalStatus expected_final_status
) {
4178 scoped_ptr
<TestPrerender
> prerender
=
4179 ExpectPrerender(expected_final_status
);
4180 WebContents
* web_contents
= GetActiveWebContents();
4181 GetAutocompleteActionPredictor()->StartPrerendering(
4183 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
4185 prerender
->WaitForStart();
4186 return prerender
.Pass();
4190 // Checks that closing the omnibox popup cancels an omnibox prerender.
4191 // http://crbug.com/395152
4192 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest
,
4193 DISABLED_PrerenderOmniboxCancel
) {
4194 // Ensure the cookie store has been loaded.
4195 if (!GetPrerenderManager()->cookie_store_loaded()) {
4197 GetPrerenderManager()->set_on_cookie_store_loaded_cb_for_testing(
4198 loop
.QuitClosure());
4202 // Fake an omnibox prerender.
4203 scoped_ptr
<TestPrerender
> prerender
= StartOmniboxPrerender(
4204 test_server()->GetURL("files/empty.html"),
4205 FINAL_STATUS_CANCELLED
);
4207 // Revert the location bar. This should cancel the prerender.
4208 GetLocationBar()->Revert();
4209 prerender
->WaitForStop();
4212 // Checks that accepting omnibox input abandons an omnibox prerender.
4213 // http://crbug.com/394592
4214 IN_PROC_BROWSER_TEST_F(PrerenderOmniboxBrowserTest
,
4215 DISABLED_PrerenderOmniboxAbandon
) {
4216 // Set the abandon timeout to something high so it does not introduce
4217 // flakiness if the prerender times out before the test completes.
4218 GetPrerenderManager()->mutable_config().abandon_time_to_live
=
4219 base::TimeDelta::FromDays(999);
4221 // Ensure the cookie store has been loaded.
4222 if (!GetPrerenderManager()->cookie_store_loaded()) {
4224 GetPrerenderManager()->set_on_cookie_store_loaded_cb_for_testing(
4225 loop
.QuitClosure());
4229 // Enter a URL into the Omnibox.
4230 OmniboxView
* omnibox_view
= GetOmniboxView();
4231 omnibox_view
->OnBeforePossibleChange();
4232 omnibox_view
->SetUserText(
4233 base::UTF8ToUTF16(test_server()->GetURL("files/empty.html?1").spec()));
4234 omnibox_view
->OnAfterPossibleChange();
4235 WaitForAutocompleteDone(omnibox_view
);
4237 // Fake an omnibox prerender for a different URL.
4238 scoped_ptr
<TestPrerender
> prerender
= StartOmniboxPrerender(
4239 test_server()->GetURL("files/empty.html?2"),
4240 FINAL_STATUS_APP_TERMINATING
);
4242 // The final status may be either FINAL_STATUS_APP_TERMINATING or
4243 // FINAL_STATUS_CANCELLED. Although closing the omnibox will not cancel an
4244 // abandoned prerender, the AutocompleteActionPredictor will cancel the
4245 // predictor on destruction.
4246 prerender
->contents()->set_skip_final_checks(true);
4248 // Navigate to the URL entered.
4249 omnibox_view
->model()->AcceptInput(CURRENT_TAB
, false);
4251 // Prerender should be running, but abandoned.
4253 GetAutocompleteActionPredictor()->IsPrerenderAbandonedForTesting());
4256 // Prefetch should be allowed depending on preference and network type.
4257 // This test is for the bsae case: no Finch overrides should never disable.
4258 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
4259 LocalPredictorDisableWorksBaseCase
) {
4260 TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
4261 false /*preference_wifi_network_wifi*/,
4262 false /*preference_wifi_network_4g*/,
4263 false /*preference_always_network_wifi*/,
4264 false /*preference_always_network_4g*/,
4265 false /*preference_never_network_wifi*/,
4266 false /*preference_never_network_4g*/);
4269 // Prefetch should be allowed depending on preference and network type.
4270 // LocalPredictorOnCellularOnly should disable all wifi cases.
4271 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
4272 LocalPredictorDisableWorksCellularOnly
) {
4273 CreateTestFieldTrial("PrerenderLocalPredictorSpec",
4274 "LocalPredictorOnCellularOnly=Enabled");
4275 TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
4276 true /*preference_wifi_network_wifi*/,
4277 false /*preference_wifi_network_4g*/,
4278 true /*preference_always_network_wifi*/,
4279 false /*preference_always_network_4g*/,
4280 true /*preference_never_network_wifi*/,
4281 false /*preference_never_network_4g*/);
4284 // Prefetch should be allowed depending on preference and network type.
4285 // LocalPredictorNetworkPredictionEnabledOnly should disable whenever
4286 // network predictions will not be exercised.
4287 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
4288 LocalPredictorDisableWorksNetworkPredictionEnableOnly
) {
4289 CreateTestFieldTrial("PrerenderLocalPredictorSpec",
4290 "LocalPredictorNetworkPredictionEnabledOnly=Enabled");
4291 TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
4292 false /*preference_wifi_network_wifi*/,
4293 true /*preference_wifi_network_4g*/,
4294 false /*preference_always_network_wifi*/,
4295 false /*preference_always_network_4g*/,
4296 true /*preference_never_network_wifi*/,
4297 true /*preference_never_network_4g*/);
4300 // Prefetch should be allowed depending on preference and network type.
4301 // If LocalPredictorNetworkPredictionEnabledOnly and
4302 // LocalPredictorOnCellularOnly are both selected, we must disable whenever
4303 // network predictions are not exercised, or when we are on wifi.
4304 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest
,
4305 LocalPredictorDisableWorksBothOptions
) {
4306 CreateTestFieldTrial("PrerenderLocalPredictorSpec",
4307 "LocalPredictorOnCellularOnly=Enabled:"
4308 "LocalPredictorNetworkPredictionEnabledOnly=Enabled");
4309 TestShouldDisableLocalPredictorPreferenceNetworkMatrix(
4310 true /*preference_wifi_network_wifi*/,
4311 true /*preference_wifi_network_4g*/,
4312 true /*preference_always_network_wifi*/,
4313 false /*preference_always_network_4g*/,
4314 true /*preference_never_network_wifi*/,
4315 true /*preference_never_network_4g*/);
4318 } // namespace prerender