1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/net/prediction_options.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "net/base/network_change_notifier.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
22 using chrome_browser_net::NetworkPredictionOptions
;
23 using net::NetworkChangeNotifier
;
27 const char kPrefetchPage
[] = "/prerender/simple_prefetch.html";
29 class MockNetworkChangeNotifierWIFI
: public NetworkChangeNotifier
{
31 ConnectionType
GetCurrentConnectionType() const override
{
32 return NetworkChangeNotifier::CONNECTION_WIFI
;
36 class MockNetworkChangeNotifier4G
: public NetworkChangeNotifier
{
38 ConnectionType
GetCurrentConnectionType() const override
{
39 return NetworkChangeNotifier::CONNECTION_4G
;
43 class PrefetchBrowserTestBase
: public InProcessBrowserTest
{
45 explicit PrefetchBrowserTestBase(bool disabled_via_field_trial
)
46 : disabled_via_field_trial_(disabled_via_field_trial
) {}
48 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
49 if (disabled_via_field_trial_
) {
50 command_line
->AppendSwitchASCII(switches::kForceFieldTrials
,
51 "Prefetch/ExperimentDisabled/");
55 void SetUpOnMainThread() override
{
56 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
57 InProcessBrowserTest::SetUpOnMainThread();
60 void SetPreference(NetworkPredictionOptions value
) {
61 browser()->profile()->GetPrefs()->SetInteger(
62 prefs::kNetworkPredictionOptions
, value
);
65 bool RunPrefetchExperiment(bool expect_success
, Browser
* browser
) {
66 GURL url
= embedded_test_server()->GetURL(kPrefetchPage
);
68 const base::string16 expected_title
=
69 expect_success
? base::ASCIIToUTF16("link onload")
70 : base::ASCIIToUTF16("link onerror");
71 content::TitleWatcher
title_watcher(
72 browser
->tab_strip_model()->GetActiveWebContents(), expected_title
);
73 ui_test_utils::NavigateToURL(browser
, url
);
74 return expected_title
== title_watcher
.WaitAndGetTitle();
78 bool disabled_via_field_trial_
;
81 class PrefetchBrowserTestPrediction
: public PrefetchBrowserTestBase
{
83 PrefetchBrowserTestPrediction() : PrefetchBrowserTestBase(false) {}
86 class PrefetchBrowserTestPredictionDisabled
: public PrefetchBrowserTestBase
{
88 PrefetchBrowserTestPredictionDisabled() : PrefetchBrowserTestBase(true) {}
91 // Prefetch is disabled via field experiment. Prefetch should be dropped.
92 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionDisabled
,
94 EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
95 // Should not prefetch even if preference is ALWAYS.
96 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS
);
97 EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
100 // Prefetch should be allowed depending on preference and network type.
101 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPrediction
, PreferenceWorks
) {
102 // Set real NetworkChangeNotifier singleton aside.
103 scoped_ptr
<NetworkChangeNotifier::DisableForTest
> disable_for_test(
104 new NetworkChangeNotifier::DisableForTest
);
106 // Preference defaults to WIFI_ONLY: prefetch when not on cellular.
108 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifierWIFI
);
109 EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
112 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifier4G
);
113 EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
116 // Set preference to ALWAYS: always prefetch.
117 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS
);
119 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifierWIFI
);
120 EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
123 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifier4G
);
124 EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
127 // Set preference to NEVER: never prefetch.
128 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER
);
130 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifierWIFI
);
131 EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
134 scoped_ptr
<NetworkChangeNotifier
> mock(new MockNetworkChangeNotifier4G
);
135 EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
139 // Bug 339909: When in incognito mode the browser crashed due to an
140 // uninitialized preference member. Verify that it no longer does.
141 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPrediction
, IncognitoTest
) {
142 Profile
* incognito_profile
= browser()->profile()->GetOffTheRecordProfile();
143 Browser
* incognito_browser
= new Browser(
144 Browser::CreateParams(incognito_profile
, browser()->host_desktop_type()));
146 // Navigate just to have a tab in this window, otherwise there is no
147 // WebContents for the incognito browser.
148 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
150 EXPECT_TRUE(RunPrefetchExperiment(true, incognito_browser
));