blob: 45ecd0a3457d4a13ae7355251007ff30d29b46d9 [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"
31#include "chrome/test/base/browser_with_test_window_test.h"
32#include "chrome/test/base/search_test_utils.h"
33#endif
34
mlamourieb40d552015-02-05 00:57:0835using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2936
[email protected]3d831992013-07-04 01:13:2937TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
38 ChromeContentBrowserClient client;
39 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
40 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
41 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
42}
43
jam1c5a91492016-02-24 20:47:5344// BrowserWithTestWindowTest doesn't work on Android.
45#if !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:0846
peterbbcccc12015-02-11 22:23:3347using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
48
49static void DidOpenURLForWindowTest(content::WebContents** target_contents,
50 content::WebContents* opened_contents) {
51 DCHECK(target_contents);
52
53 *target_contents = opened_contents;
54}
55
mlamourieb40d552015-02-05 00:57:0856// This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
57// URLs to be opened in new tabs and activated, changing the active tabs after
58// each call and increasing the tab count by 2.
59TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
60 ChromeContentBrowserClient client;
61
62 int previous_count = browser()->tab_strip_model()->count();
63
64 GURL urls[] = { GURL("https://www.google.com"),
65 GURL("https://www.chromium.org") };
66
67 for (const GURL& url : urls) {
68 content::OpenURLParams params(url,
69 content::Referrer(),
70 NEW_FOREGROUND_TAB,
71 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
72 false);
peterbbcccc12015-02-11 22:23:3373 // TODO(peter): We should have more in-depth browser tests for the window
74 // opening functionality, which also covers Android. This test can currently
75 // only be ran on platforms where OpenURL is implemented synchronously.
76 // See https://crbug.com/457667.
77 content::WebContents* web_contents = nullptr;
78 client.OpenURL(browser()->profile(),
79 params,
80 base::Bind(&DidOpenURLForWindowTest, &web_contents));
81
82 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:0883
84 content::WebContents* active_contents = browser()->tab_strip_model()->
85 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:3386 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:0887 EXPECT_EQ(url, active_contents->GetVisibleURL());
88 }
89
90 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
91}
92
jam1c5a91492016-02-24 20:47:5393#endif // !defined(OS_ANDROID)
mlamourieb40d552015-02-05 00:57:0894
brettwa68ea2b2015-02-01 02:54:0795#if defined(ENABLE_WEBRTC)
96
[email protected]30335fdf02014-02-26 19:51:2797// NOTE: Any updates to the expectations in these tests should also be done in
98// the browser test WebRtcDisableEncryptionFlagBrowserTest.
99class DisableWebRtcEncryptionFlagTest : public testing::Test {
100 public:
101 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:17102 : from_command_line_(base::CommandLine::NO_PROGRAM),
103 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27104
105 protected:
dchenge1bc7982014-10-30 00:32:40106 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27107 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
108 }
109
sdefresne6e883e42015-07-30 08:05:54110 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
[email protected]30335fdf02014-02-26 19:51:27111 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
112 &to_command_line_,
113 from_command_line_,
114 channel);
115 }
116
avi3ef9ec9e2014-12-22 22:50:17117 base::CommandLine from_command_line_;
118 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27119
thestigf80564462015-09-29 23:12:08120 private:
[email protected]30335fdf02014-02-26 19:51:27121 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
122};
123
124TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
sdefresne6e883e42015-07-30 08:05:54125 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN);
[email protected]30335fdf02014-02-26 19:51:27126 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
127}
128
129TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
sdefresne6e883e42015-07-30 08:05:54130 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY);
[email protected]30335fdf02014-02-26 19:51:27131 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
132}
133
134TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
sdefresne6e883e42015-07-30 08:05:54135 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV);
[email protected]30335fdf02014-02-26 19:51:27136 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
137}
138
139TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
sdefresne6e883e42015-07-30 08:05:54140 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA);
[email protected]30335fdf02014-02-26 19:51:27141#if defined(OS_ANDROID)
142 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
143#else
144 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
145#endif
146}
147
148TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
sdefresne6e883e42015-07-30 08:05:54149 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE);
[email protected]30335fdf02014-02-26 19:51:27150 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
151}
152
brettwa68ea2b2015-02-01 02:54:07153#endif // ENABLE_WEBRTC
154
bmcquade5d2d9cf32015-06-19 17:42:28155class BlinkSettingsFieldTrialTest : public testing::Test {
156 public:
bmcquade9dd54cc2015-06-22 16:56:52157 static const char kParserFieldTrialName[];
158 static const char kIFrameFieldTrialName[];
pmeenan9ac669682015-08-17 14:57:03159 static const char kResourcePrioritiesFieldTrialName[];
160 static const char kFakeGroupName[];
161 static const char kDefaultGroupName[];
bmcquade9dd54cc2015-06-22 16:56:52162
bmcquade5d2d9cf32015-06-19 17:42:28163 BlinkSettingsFieldTrialTest()
164 : trial_list_(NULL),
165 command_line_(base::CommandLine::NO_PROGRAM) {}
166
167 void SetUp() override {
168 command_line_.AppendSwitchASCII(
169 switches::kProcessType, switches::kRendererProcess);
170 }
171
172 void TearDown() override {
173 variations::testing::ClearAllVariationParams();
174 }
175
pmeenan9ac669682015-08-17 14:57:03176 void CreateFieldTrial(const char* trial_name, const char* group_name) {
177 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
bmcquade5d2d9cf32015-06-19 17:42:28178 }
179
bmcquade9dd54cc2015-06-22 16:56:52180 void CreateFieldTrialWithParams(
181 const char* trial_name,
pmeenan9ac669682015-08-17 14:57:03182 const char* group_name,
bmcquade9dd54cc2015-06-22 16:56:52183 const char* key1, const char* value1,
184 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28185 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52186 params.insert(std::make_pair(key1, value1));
187 params.insert(std::make_pair(key2, value2));
pmeenan9ac669682015-08-17 14:57:03188 CreateFieldTrial(trial_name, kFakeGroupName);
189 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
bmcquade5d2d9cf32015-06-19 17:42:28190 }
191
192 void AppendContentBrowserClientSwitches() {
193 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
194 }
195
196 const base::CommandLine& command_line() const {
197 return command_line_;
198 }
199
200 void AppendBlinkSettingsSwitch(const char* value) {
201 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
202 }
203
204 private:
205 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28206
207 ChromeContentBrowserClient client_;
208 base::FieldTrialList trial_list_;
209 base::CommandLine command_line_;
210
211 content::TestBrowserThreadBundle thread_bundle_;
212};
213
bmcquade9dd54cc2015-06-22 16:56:52214const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
bmcquade5d2d9cf32015-06-19 17:42:28215 "BackgroundHtmlParserTokenLimits";
bmcquade9dd54cc2015-06-22 16:56:52216const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
217 "LowPriorityIFrames";
pmeenan9ac669682015-08-17 14:57:03218const char BlinkSettingsFieldTrialTest::kResourcePrioritiesFieldTrialName[] =
219 "ResourcePriorities";
220const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
221const char BlinkSettingsFieldTrialTest::kDefaultGroupName[] = "Default";
bmcquade5d2d9cf32015-06-19 17:42:28222
223TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
224 AppendContentBrowserClientSwitches();
225 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
226}
227
228TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
pmeenan9ac669682015-08-17 14:57:03229 CreateFieldTrial(kParserFieldTrialName, kFakeGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28230 AppendContentBrowserClientSwitches();
231 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
232}
233
234TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
235 AppendBlinkSettingsSwitch("foo");
pmeenan9ac669682015-08-17 14:57:03236 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52237 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28238 AppendContentBrowserClientSwitches();
239 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
240 EXPECT_EQ("foo",
241 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
242}
243
244TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
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("key1=value1,key2=value2",
250 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
251}
252
bmcquade9dd54cc2015-06-22 16:56:52253TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) {
pmeenan9ac669682015-08-17 14:57:03254 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52255 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03256 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52257 "keyA", "valueA", "keyB", "valueB");
258 AppendContentBrowserClientSwitches();
259 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
260 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
261 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
262}
263
264TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) {
pmeenan9ac669682015-08-17 14:57:03265 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52266 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03267 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52268 "key2", "duplicate", "key3", "value3");
269 AppendContentBrowserClientSwitches();
270 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
271 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
272 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
273}
274
pmeenan9ac669682015-08-17 14:57:03275TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDefault) {
276 CreateFieldTrial(kResourcePrioritiesFieldTrialName, kDefaultGroupName);
277 AppendContentBrowserClientSwitches();
278 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
279}
280
281TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesEverythingEnabled) {
282 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
283 "Everything_11111_1_1_10");
284 AppendContentBrowserClientSwitches();
285 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
286 EXPECT_EQ("fetchDeferLateScripts=true,"
287 "fetchIncreaseFontPriority=true,"
288 "fetchIncreaseAsyncScriptPriority=true,"
289 "fetchIncreasePriorities=true",
290 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
291}
292
293TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDeferLateScripts) {
294 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
295 "LateScripts_10000_0_1_10");
296 AppendContentBrowserClientSwitches();
297 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
298 EXPECT_EQ("fetchDeferLateScripts=true",
299 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
300}
301
302TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesFontsEnabled) {
303 CreateFieldTrial(kResourcePrioritiesFieldTrialName, "FontOnly_01000_0_1_10");
304 AppendContentBrowserClientSwitches();
305 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
306 EXPECT_EQ("fetchIncreaseFontPriority=true",
307 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
308}
309
310TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreaseAsyncScript) {
311 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
312 "AsyncScript_00100_0_1_10");
313 AppendContentBrowserClientSwitches();
314 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
315 EXPECT_EQ("fetchIncreaseAsyncScriptPriority=true",
316 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
317}
318
319TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreasePriorities) {
320 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
321 "IncreasePriorities_00010_0_1_10");
322 AppendContentBrowserClientSwitches();
323 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
324 EXPECT_EQ("fetchIncreasePriorities=true",
325 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
326}
327
jam1c5a91492016-02-24 20:47:53328#if !defined(OS_ANDROID)
[email protected]10a9bf92013-11-13 23:34:48329namespace content {
330
331class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
332 protected:
dchenge1bc7982014-10-30 00:32:40333 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48334 BrowserWithTestWindowTest::SetUp();
335 field_trial_list_.reset(new base::FieldTrialList(
336 new metrics::SHA1EntropyProvider("42")));
337 }
338
339 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
340 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
341 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
342 TemplateURLService* template_url_service =
343 TemplateURLServiceFactory::GetForProfile(browser()->profile());
thestigf80564462015-09-29 23:12:08344 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
[email protected]10a9bf92013-11-13 23:34:48345
346 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53347 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48348 data.SetURL("http://foo.com/url?bar={searchTerms}");
349 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28350 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48351 // Takes ownership.
352 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26353 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48354 }
355
356 scoped_ptr<base::FieldTrialList> field_trial_list_;
357};
358
359TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
360 const GURL url_original("chrome://newtab");
361 const GURL url_rewritten("https://www.example.com/newtab");
362 InstallTemplateURLWithNewTabPage(url_rewritten);
363 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
364 "Group1 use_cacheable_ntp:1"));
365
366 AddTab(browser(), GURL("chrome://blank"));
367 NavigateAndCommitActiveTab(url_original);
368
369 NavigationEntry* entry = browser()->tab_strip_model()->
370 GetActiveWebContents()->GetController().GetLastCommittedEntry();
371 ASSERT_TRUE(entry != NULL);
372 EXPECT_EQ(url_rewritten, entry->GetURL());
373 EXPECT_EQ(url_original, entry->GetVirtualURL());
374}
375
376} // namespace content
jam1c5a91492016-02-24 20:47:53377#endif // !defined(OS_ANDROID)