blob: 88a062064fe2be72b758d2c6a23358533310d39c [file] [log] [blame]
[email protected]3d831992013-07-04 01:13:291// 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.
4
5#include "chrome/browser/chrome_content_browser_client.h"
6
bmcquade5d2d9cf32015-06-19 17:42:287#include <map>
8
[email protected]30335fdf02014-02-26 19:51:279#include "base/command_line.h"
avie4d7b6f2015-12-26 00:59:1810#include "base/macros.h"
[email protected]10a9bf92013-11-13 23:34:4811#include "base/metrics/field_trial.h"
mpearson3c6d7af2015-05-13 23:59:5312#include "base/strings/utf_string_conversions.h"
avie4d7b6f2015-12-26 00:59:1813#include "build/build_config.h"
[email protected]10a9bf92013-11-13 23:34:4814#include "chrome/browser/search_engines/template_url_service_factory.h"
timvolodinea47291a2015-01-23 14:04:4315#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]bf5c532d2014-07-05 00:29:5316#include "components/search_engines/template_url_service.h"
[email protected]10a9bf92013-11-13 23:34:4817#include "components/variations/entropy_provider.h"
bmcquade5d2d9cf32015-06-19 17:42:2818#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2219#include "components/version_info/version_info.h"
[email protected]10a9bf92013-11-13 23:34:4820#include "content/public/browser/navigation_controller.h"
21#include "content/public/browser/navigation_entry.h"
22#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2723#include "content/public/common/content_switches.h"
bmcquade5d2d9cf32015-06-19 17:42:2824#include "content/public/test/test_browser_thread_bundle.h"
[email protected]3d831992013-07-04 01:13:2925#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1326#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2927
jam1c5a91492016-02-24 20:47:5328#if !defined(OS_ANDROID)
thestigf80564462015-09-29 23:12:0829#include "chrome/browser/ui/browser.h"
30#include "chrome/browser/ui/tabs/tab_strip_model.h"
bengre8a146f2016-03-10 01:20:2231#include "chrome/common/pref_names.h"
thestigf80564462015-09-29 23:12:0832#include "chrome/test/base/browser_with_test_window_test.h"
33#include "chrome/test/base/search_test_utils.h"
34#endif
35
mlamourieb40d552015-02-05 00:57:0836using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2937
[email protected]3d831992013-07-04 01:13:2938TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
39 ChromeContentBrowserClient client;
40 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
41 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
42 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
43}
44
jam1c5a91492016-02-24 20:47:5345// BrowserWithTestWindowTest doesn't work on Android.
46#if !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:0847
peterbbcccc12015-02-11 22:23:3348using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
49
50static void DidOpenURLForWindowTest(content::WebContents** target_contents,
51 content::WebContents* opened_contents) {
52 DCHECK(target_contents);
53
54 *target_contents = opened_contents;
55}
56
bengre8a146f2016-03-10 01:20:2257TEST_F(ChromeContentBrowserClientWindowTest, IsDataSaverEnabled) {
58 ChromeContentBrowserClient client;
59 content::BrowserContext* context = browser()->profile();
60 EXPECT_FALSE(client.IsDataSaverEnabled(context));
61 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDataSaverEnabled, true);
62 EXPECT_TRUE(client.IsDataSaverEnabled(context));
63}
64
mlamourieb40d552015-02-05 00:57:0865// This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
66// URLs to be opened in new tabs and activated, changing the active tabs after
67// each call and increasing the tab count by 2.
68TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
69 ChromeContentBrowserClient client;
70
71 int previous_count = browser()->tab_strip_model()->count();
72
73 GURL urls[] = { GURL("https://www.google.com"),
74 GURL("https://www.chromium.org") };
75
76 for (const GURL& url : urls) {
77 content::OpenURLParams params(url,
78 content::Referrer(),
79 NEW_FOREGROUND_TAB,
80 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
81 false);
peterbbcccc12015-02-11 22:23:3382 // TODO(peter): We should have more in-depth browser tests for the window
83 // opening functionality, which also covers Android. This test can currently
84 // only be ran on platforms where OpenURL is implemented synchronously.
85 // See https://crbug.com/457667.
86 content::WebContents* web_contents = nullptr;
87 client.OpenURL(browser()->profile(),
88 params,
89 base::Bind(&DidOpenURLForWindowTest, &web_contents));
90
91 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:0892
93 content::WebContents* active_contents = browser()->tab_strip_model()->
94 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:3395 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:0896 EXPECT_EQ(url, active_contents->GetVisibleURL());
97 }
98
99 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
100}
101
jam1c5a91492016-02-24 20:47:53102#endif // !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:08103
brettwa68ea2b2015-02-01 02:54:07104#if defined(ENABLE_WEBRTC)
105
[email protected]30335fdf02014-02-26 19:51:27106// NOTE: Any updates to the expectations in these tests should also be done in
107// the browser test WebRtcDisableEncryptionFlagBrowserTest.
108class DisableWebRtcEncryptionFlagTest : public testing::Test {
109 public:
110 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:17111 : from_command_line_(base::CommandLine::NO_PROGRAM),
112 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27113
114 protected:
dchenge1bc7982014-10-30 00:32:40115 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27116 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
117 }
118
sdefresne6e883e42015-07-30 08:05:54119 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
[email protected]30335fdf02014-02-26 19:51:27120 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
121 &to_command_line_,
122 from_command_line_,
123 channel);
124 }
125
avi3ef9ec9e2014-12-22 22:50:17126 base::CommandLine from_command_line_;
127 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27128
thestigf80564462015-09-29 23:12:08129 private:
[email protected]30335fdf02014-02-26 19:51:27130 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
131};
132
133TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
sdefresne6e883e42015-07-30 08:05:54134 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN);
[email protected]30335fdf02014-02-26 19:51:27135 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
136}
137
138TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
sdefresne6e883e42015-07-30 08:05:54139 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY);
[email protected]30335fdf02014-02-26 19:51:27140 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
141}
142
143TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
sdefresne6e883e42015-07-30 08:05:54144 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV);
[email protected]30335fdf02014-02-26 19:51:27145 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
146}
147
148TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
sdefresne6e883e42015-07-30 08:05:54149 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA);
[email protected]30335fdf02014-02-26 19:51:27150#if defined(OS_ANDROID)
151 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
152#else
153 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
154#endif
155}
156
157TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
sdefresne6e883e42015-07-30 08:05:54158 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE);
[email protected]30335fdf02014-02-26 19:51:27159 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
160}
161
brettwa68ea2b2015-02-01 02:54:07162#endif // ENABLE_WEBRTC
163
bmcquade5d2d9cf32015-06-19 17:42:28164class BlinkSettingsFieldTrialTest : public testing::Test {
165 public:
bmcquade9dd54cc2015-06-22 16:56:52166 static const char kParserFieldTrialName[];
167 static const char kIFrameFieldTrialName[];
pmeenan9ac669682015-08-17 14:57:03168 static const char kFakeGroupName[];
169 static const char kDefaultGroupName[];
bmcquade9dd54cc2015-06-22 16:56:52170
bmcquade5d2d9cf32015-06-19 17:42:28171 BlinkSettingsFieldTrialTest()
172 : trial_list_(NULL),
173 command_line_(base::CommandLine::NO_PROGRAM) {}
174
175 void SetUp() override {
176 command_line_.AppendSwitchASCII(
177 switches::kProcessType, switches::kRendererProcess);
178 }
179
180 void TearDown() override {
181 variations::testing::ClearAllVariationParams();
182 }
183
pmeenan9ac669682015-08-17 14:57:03184 void CreateFieldTrial(const char* trial_name, const char* group_name) {
185 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
bmcquade5d2d9cf32015-06-19 17:42:28186 }
187
bmcquade9dd54cc2015-06-22 16:56:52188 void CreateFieldTrialWithParams(
189 const char* trial_name,
pmeenan9ac669682015-08-17 14:57:03190 const char* group_name,
bmcquade9dd54cc2015-06-22 16:56:52191 const char* key1, const char* value1,
192 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28193 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52194 params.insert(std::make_pair(key1, value1));
195 params.insert(std::make_pair(key2, value2));
pmeenan9ac669682015-08-17 14:57:03196 CreateFieldTrial(trial_name, kFakeGroupName);
197 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
bmcquade5d2d9cf32015-06-19 17:42:28198 }
199
200 void AppendContentBrowserClientSwitches() {
201 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
202 }
203
204 const base::CommandLine& command_line() const {
205 return command_line_;
206 }
207
208 void AppendBlinkSettingsSwitch(const char* value) {
209 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
210 }
211
212 private:
213 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28214
215 ChromeContentBrowserClient client_;
216 base::FieldTrialList trial_list_;
217 base::CommandLine command_line_;
218
219 content::TestBrowserThreadBundle thread_bundle_;
220};
221
bmcquade9dd54cc2015-06-22 16:56:52222const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
bmcquade5d2d9cf32015-06-19 17:42:28223 "BackgroundHtmlParserTokenLimits";
bmcquade9dd54cc2015-06-22 16:56:52224const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
225 "LowPriorityIFrames";
pmeenan9ac669682015-08-17 14:57:03226const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
227const char BlinkSettingsFieldTrialTest::kDefaultGroupName[] = "Default";
bmcquade5d2d9cf32015-06-19 17:42:28228
229TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
230 AppendContentBrowserClientSwitches();
231 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
232}
233
234TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
pmeenan9ac669682015-08-17 14:57:03235 CreateFieldTrial(kParserFieldTrialName, kFakeGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28236 AppendContentBrowserClientSwitches();
237 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
238}
239
240TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
241 AppendBlinkSettingsSwitch("foo");
pmeenan9ac669682015-08-17 14:57:03242 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52243 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28244 AppendContentBrowserClientSwitches();
245 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
246 EXPECT_EQ("foo",
247 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
248}
249
250TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
pmeenan9ac669682015-08-17 14:57:03251 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52252 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28253 AppendContentBrowserClientSwitches();
254 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
255 EXPECT_EQ("key1=value1,key2=value2",
256 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
257}
258
bmcquade9dd54cc2015-06-22 16:56:52259TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) {
pmeenan9ac669682015-08-17 14:57:03260 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52261 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03262 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52263 "keyA", "valueA", "keyB", "valueB");
264 AppendContentBrowserClientSwitches();
265 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
266 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
267 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
268}
269
270TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) {
pmeenan9ac669682015-08-17 14:57:03271 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52272 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03273 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52274 "key2", "duplicate", "key3", "value3");
275 AppendContentBrowserClientSwitches();
276 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
277 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
278 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
279}
280
jam1c5a91492016-02-24 20:47:53281#if !defined(OS_ANDROID)
[email protected]10a9bf92013-11-13 23:34:48282namespace content {
283
284class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
285 protected:
dchenge1bc7982014-10-30 00:32:40286 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48287 BrowserWithTestWindowTest::SetUp();
288 field_trial_list_.reset(new base::FieldTrialList(
289 new metrics::SHA1EntropyProvider("42")));
290 }
291
292 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
293 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
294 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
295 TemplateURLService* template_url_service =
296 TemplateURLServiceFactory::GetForProfile(browser()->profile());
thestigf80564462015-09-29 23:12:08297 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
[email protected]10a9bf92013-11-13 23:34:48298
299 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53300 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48301 data.SetURL("http://foo.com/url?bar={searchTerms}");
302 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28303 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48304 // Takes ownership.
305 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26306 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48307 }
308
dcheng4af48582016-04-19 00:29:35309 std::unique_ptr<base::FieldTrialList> field_trial_list_;
[email protected]10a9bf92013-11-13 23:34:48310};
311
312TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
313 const GURL url_original("chrome://newtab");
314 const GURL url_rewritten("https://www.example.com/newtab");
315 InstallTemplateURLWithNewTabPage(url_rewritten);
316 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
317 "Group1 use_cacheable_ntp:1"));
318
319 AddTab(browser(), GURL("chrome://blank"));
320 NavigateAndCommitActiveTab(url_original);
321
322 NavigationEntry* entry = browser()->tab_strip_model()->
323 GetActiveWebContents()->GetController().GetLastCommittedEntry();
324 ASSERT_TRUE(entry != NULL);
325 EXPECT_EQ(url_rewritten, entry->GetURL());
326 EXPECT_EQ(url_original, entry->GetVirtualURL());
327}
328
329} // namespace content
jam1c5a91492016-02-24 20:47:53330#endif // !defined(OS_ANDROID)