blob: 191985bb25e2f9dff96caaa1a27b02f3a11b09a9 [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 kResourcePrioritiesFieldTrialName[];
169 static const char kFakeGroupName[];
170 static const char kDefaultGroupName[];
bmcquade9dd54cc2015-06-22 16:56:52171
bmcquade5d2d9cf32015-06-19 17:42:28172 BlinkSettingsFieldTrialTest()
173 : trial_list_(NULL),
174 command_line_(base::CommandLine::NO_PROGRAM) {}
175
176 void SetUp() override {
177 command_line_.AppendSwitchASCII(
178 switches::kProcessType, switches::kRendererProcess);
179 }
180
181 void TearDown() override {
182 variations::testing::ClearAllVariationParams();
183 }
184
pmeenan9ac669682015-08-17 14:57:03185 void CreateFieldTrial(const char* trial_name, const char* group_name) {
186 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
bmcquade5d2d9cf32015-06-19 17:42:28187 }
188
bmcquade9dd54cc2015-06-22 16:56:52189 void CreateFieldTrialWithParams(
190 const char* trial_name,
pmeenan9ac669682015-08-17 14:57:03191 const char* group_name,
bmcquade9dd54cc2015-06-22 16:56:52192 const char* key1, const char* value1,
193 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28194 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52195 params.insert(std::make_pair(key1, value1));
196 params.insert(std::make_pair(key2, value2));
pmeenan9ac669682015-08-17 14:57:03197 CreateFieldTrial(trial_name, kFakeGroupName);
198 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
bmcquade5d2d9cf32015-06-19 17:42:28199 }
200
201 void AppendContentBrowserClientSwitches() {
202 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
203 }
204
205 const base::CommandLine& command_line() const {
206 return command_line_;
207 }
208
209 void AppendBlinkSettingsSwitch(const char* value) {
210 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
211 }
212
213 private:
214 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28215
216 ChromeContentBrowserClient client_;
217 base::FieldTrialList trial_list_;
218 base::CommandLine command_line_;
219
220 content::TestBrowserThreadBundle thread_bundle_;
221};
222
bmcquade9dd54cc2015-06-22 16:56:52223const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
bmcquade5d2d9cf32015-06-19 17:42:28224 "BackgroundHtmlParserTokenLimits";
bmcquade9dd54cc2015-06-22 16:56:52225const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
226 "LowPriorityIFrames";
pmeenan9ac669682015-08-17 14:57:03227const char BlinkSettingsFieldTrialTest::kResourcePrioritiesFieldTrialName[] =
228 "ResourcePriorities";
229const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
230const char BlinkSettingsFieldTrialTest::kDefaultGroupName[] = "Default";
bmcquade5d2d9cf32015-06-19 17:42:28231
232TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
233 AppendContentBrowserClientSwitches();
234 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
235}
236
237TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
pmeenan9ac669682015-08-17 14:57:03238 CreateFieldTrial(kParserFieldTrialName, kFakeGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28239 AppendContentBrowserClientSwitches();
240 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
241}
242
243TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
244 AppendBlinkSettingsSwitch("foo");
pmeenan9ac669682015-08-17 14:57:03245 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52246 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28247 AppendContentBrowserClientSwitches();
248 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
249 EXPECT_EQ("foo",
250 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
251}
252
253TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
pmeenan9ac669682015-08-17 14:57:03254 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52255 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28256 AppendContentBrowserClientSwitches();
257 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
258 EXPECT_EQ("key1=value1,key2=value2",
259 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
260}
261
bmcquade9dd54cc2015-06-22 16:56:52262TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) {
pmeenan9ac669682015-08-17 14:57:03263 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52264 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03265 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52266 "keyA", "valueA", "keyB", "valueB");
267 AppendContentBrowserClientSwitches();
268 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
269 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
270 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
271}
272
273TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) {
pmeenan9ac669682015-08-17 14:57:03274 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52275 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03276 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52277 "key2", "duplicate", "key3", "value3");
278 AppendContentBrowserClientSwitches();
279 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
280 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
281 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
282}
283
pmeenan9ac669682015-08-17 14:57:03284TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDefault) {
285 CreateFieldTrial(kResourcePrioritiesFieldTrialName, kDefaultGroupName);
286 AppendContentBrowserClientSwitches();
287 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
288}
289
290TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesEverythingEnabled) {
291 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
292 "Everything_11111_1_1_10");
293 AppendContentBrowserClientSwitches();
294 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
295 EXPECT_EQ("fetchDeferLateScripts=true,"
296 "fetchIncreaseFontPriority=true,"
297 "fetchIncreaseAsyncScriptPriority=true,"
298 "fetchIncreasePriorities=true",
299 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
300}
301
302TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDeferLateScripts) {
303 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
304 "LateScripts_10000_0_1_10");
305 AppendContentBrowserClientSwitches();
306 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
307 EXPECT_EQ("fetchDeferLateScripts=true",
308 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
309}
310
311TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesFontsEnabled) {
312 CreateFieldTrial(kResourcePrioritiesFieldTrialName, "FontOnly_01000_0_1_10");
313 AppendContentBrowserClientSwitches();
314 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
315 EXPECT_EQ("fetchIncreaseFontPriority=true",
316 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
317}
318
319TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreaseAsyncScript) {
320 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
321 "AsyncScript_00100_0_1_10");
322 AppendContentBrowserClientSwitches();
323 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
324 EXPECT_EQ("fetchIncreaseAsyncScriptPriority=true",
325 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
326}
327
328TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreasePriorities) {
329 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
330 "IncreasePriorities_00010_0_1_10");
331 AppendContentBrowserClientSwitches();
332 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
333 EXPECT_EQ("fetchIncreasePriorities=true",
334 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
335}
336
jam1c5a91492016-02-24 20:47:53337#if !defined(OS_ANDROID)
[email protected]10a9bf92013-11-13 23:34:48338namespace content {
339
340class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
341 protected:
dchenge1bc7982014-10-30 00:32:40342 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48343 BrowserWithTestWindowTest::SetUp();
344 field_trial_list_.reset(new base::FieldTrialList(
345 new metrics::SHA1EntropyProvider("42")));
346 }
347
348 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
349 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
350 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
351 TemplateURLService* template_url_service =
352 TemplateURLServiceFactory::GetForProfile(browser()->profile());
thestigf80564462015-09-29 23:12:08353 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
[email protected]10a9bf92013-11-13 23:34:48354
355 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53356 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48357 data.SetURL("http://foo.com/url?bar={searchTerms}");
358 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28359 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48360 // Takes ownership.
361 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26362 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48363 }
364
dcheng4af48582016-04-19 00:29:35365 std::unique_ptr<base::FieldTrialList> field_trial_list_;
[email protected]10a9bf92013-11-13 23:34:48366};
367
368TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
369 const GURL url_original("chrome://newtab");
370 const GURL url_rewritten("https://www.example.com/newtab");
371 InstallTemplateURLWithNewTabPage(url_rewritten);
372 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
373 "Group1 use_cacheable_ntp:1"));
374
375 AddTab(browser(), GURL("chrome://blank"));
376 NavigateAndCommitActiveTab(url_original);
377
378 NavigationEntry* entry = browser()->tab_strip_model()->
379 GetActiveWebContents()->GetController().GetLastCommittedEntry();
380 ASSERT_TRUE(entry != NULL);
381 EXPECT_EQ(url_rewritten, entry->GetURL());
382 EXPECT_EQ(url_original, entry->GetVirtualURL());
383}
384
385} // namespace content
jam1c5a91492016-02-24 20:47:53386#endif // !defined(OS_ANDROID)