blob: 1d5455df45540a484fc76a0f8f1081721c8ee2cb [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"
[email protected]10a9bf92013-11-13 23:34:4810#include "base/metrics/field_trial.h"
mpearson3c6d7af2015-05-13 23:59:5311#include "base/strings/utf_string_conversions.h"
[email protected]10a9bf92013-11-13 23:34:4812#include "chrome/browser/search_engines/template_url_service_factory.h"
timvolodinea47291a2015-01-23 14:04:4313#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]bf5c532d2014-07-05 00:29:5314#include "components/search_engines/template_url_service.h"
[email protected]10a9bf92013-11-13 23:34:4815#include "components/variations/entropy_provider.h"
bmcquade5d2d9cf32015-06-19 17:42:2816#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2217#include "components/version_info/version_info.h"
[email protected]10a9bf92013-11-13 23:34:4818#include "content/public/browser/navigation_controller.h"
19#include "content/public/browser/navigation_entry.h"
20#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2721#include "content/public/common/content_switches.h"
bmcquade5d2d9cf32015-06-19 17:42:2822#include "content/public/test/test_browser_thread_bundle.h"
[email protected]3d831992013-07-04 01:13:2923#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1324#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2925
thestigf80564462015-09-29 23:12:0826#if !defined(OS_ANDROID) && !defined(OS_IOS)
27#include "chrome/browser/ui/browser.h"
28#include "chrome/browser/ui/tabs/tab_strip_model.h"
29#include "chrome/test/base/browser_with_test_window_test.h"
30#include "chrome/test/base/search_test_utils.h"
31#endif
32
mlamourieb40d552015-02-05 00:57:0833using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2934
[email protected]3d831992013-07-04 01:13:2935TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
36 ChromeContentBrowserClient client;
37 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
38 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
39 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
40}
41
mlamourieb40d552015-02-05 00:57:0842// BrowserWithTestWindowTest doesn't work on iOS and Android.
43#if !defined(OS_ANDROID) && !defined(OS_IOS)
44
peterbbcccc12015-02-11 22:23:3345using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
46
47static void DidOpenURLForWindowTest(content::WebContents** target_contents,
48 content::WebContents* opened_contents) {
49 DCHECK(target_contents);
50
51 *target_contents = opened_contents;
52}
53
mlamourieb40d552015-02-05 00:57:0854// This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
55// URLs to be opened in new tabs and activated, changing the active tabs after
56// each call and increasing the tab count by 2.
57TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
58 ChromeContentBrowserClient client;
59
60 int previous_count = browser()->tab_strip_model()->count();
61
62 GURL urls[] = { GURL("https://www.google.com"),
63 GURL("https://www.chromium.org") };
64
65 for (const GURL& url : urls) {
66 content::OpenURLParams params(url,
67 content::Referrer(),
68 NEW_FOREGROUND_TAB,
69 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
70 false);
peterbbcccc12015-02-11 22:23:3371 // TODO(peter): We should have more in-depth browser tests for the window
72 // opening functionality, which also covers Android. This test can currently
73 // only be ran on platforms where OpenURL is implemented synchronously.
74 // See https://crbug.com/457667.
75 content::WebContents* web_contents = nullptr;
76 client.OpenURL(browser()->profile(),
77 params,
78 base::Bind(&DidOpenURLForWindowTest, &web_contents));
79
80 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:0881
82 content::WebContents* active_contents = browser()->tab_strip_model()->
83 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:3384 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:0885 EXPECT_EQ(url, active_contents->GetVisibleURL());
86 }
87
88 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
89}
90
thestigf80564462015-09-29 23:12:0891#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
mlamourieb40d552015-02-05 00:57:0892
brettwa68ea2b2015-02-01 02:54:0793#if defined(ENABLE_WEBRTC)
94
[email protected]30335fdf02014-02-26 19:51:2795// NOTE: Any updates to the expectations in these tests should also be done in
96// the browser test WebRtcDisableEncryptionFlagBrowserTest.
97class DisableWebRtcEncryptionFlagTest : public testing::Test {
98 public:
99 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:17100 : from_command_line_(base::CommandLine::NO_PROGRAM),
101 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27102
103 protected:
dchenge1bc7982014-10-30 00:32:40104 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27105 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
106 }
107
sdefresne6e883e42015-07-30 08:05:54108 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
[email protected]30335fdf02014-02-26 19:51:27109 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
110 &to_command_line_,
111 from_command_line_,
112 channel);
113 }
114
avi3ef9ec9e2014-12-22 22:50:17115 base::CommandLine from_command_line_;
116 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27117
thestigf80564462015-09-29 23:12:08118 private:
[email protected]30335fdf02014-02-26 19:51:27119 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
120};
121
122TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
sdefresne6e883e42015-07-30 08:05:54123 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN);
[email protected]30335fdf02014-02-26 19:51:27124 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
125}
126
127TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
sdefresne6e883e42015-07-30 08:05:54128 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY);
[email protected]30335fdf02014-02-26 19:51:27129 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
130}
131
132TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
sdefresne6e883e42015-07-30 08:05:54133 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV);
[email protected]30335fdf02014-02-26 19:51:27134 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
135}
136
137TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
sdefresne6e883e42015-07-30 08:05:54138 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA);
[email protected]30335fdf02014-02-26 19:51:27139#if defined(OS_ANDROID)
140 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
141#else
142 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
143#endif
144}
145
146TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
sdefresne6e883e42015-07-30 08:05:54147 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE);
[email protected]30335fdf02014-02-26 19:51:27148 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
149}
150
brettwa68ea2b2015-02-01 02:54:07151#endif // ENABLE_WEBRTC
152
bmcquade5d2d9cf32015-06-19 17:42:28153class BlinkSettingsFieldTrialTest : public testing::Test {
154 public:
bmcquade9dd54cc2015-06-22 16:56:52155 static const char kParserFieldTrialName[];
156 static const char kIFrameFieldTrialName[];
pmeenan9ac669682015-08-17 14:57:03157 static const char kResourcePrioritiesFieldTrialName[];
158 static const char kFakeGroupName[];
159 static const char kDefaultGroupName[];
bmcquade9dd54cc2015-06-22 16:56:52160
bmcquade5d2d9cf32015-06-19 17:42:28161 BlinkSettingsFieldTrialTest()
162 : trial_list_(NULL),
163 command_line_(base::CommandLine::NO_PROGRAM) {}
164
165 void SetUp() override {
166 command_line_.AppendSwitchASCII(
167 switches::kProcessType, switches::kRendererProcess);
168 }
169
170 void TearDown() override {
171 variations::testing::ClearAllVariationParams();
172 }
173
pmeenan9ac669682015-08-17 14:57:03174 void CreateFieldTrial(const char* trial_name, const char* group_name) {
175 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
bmcquade5d2d9cf32015-06-19 17:42:28176 }
177
bmcquade9dd54cc2015-06-22 16:56:52178 void CreateFieldTrialWithParams(
179 const char* trial_name,
pmeenan9ac669682015-08-17 14:57:03180 const char* group_name,
bmcquade9dd54cc2015-06-22 16:56:52181 const char* key1, const char* value1,
182 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28183 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52184 params.insert(std::make_pair(key1, value1));
185 params.insert(std::make_pair(key2, value2));
pmeenan9ac669682015-08-17 14:57:03186 CreateFieldTrial(trial_name, kFakeGroupName);
187 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
bmcquade5d2d9cf32015-06-19 17:42:28188 }
189
190 void AppendContentBrowserClientSwitches() {
191 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
192 }
193
194 const base::CommandLine& command_line() const {
195 return command_line_;
196 }
197
198 void AppendBlinkSettingsSwitch(const char* value) {
199 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
200 }
201
202 private:
203 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28204
205 ChromeContentBrowserClient client_;
206 base::FieldTrialList trial_list_;
207 base::CommandLine command_line_;
208
209 content::TestBrowserThreadBundle thread_bundle_;
210};
211
bmcquade9dd54cc2015-06-22 16:56:52212const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
bmcquade5d2d9cf32015-06-19 17:42:28213 "BackgroundHtmlParserTokenLimits";
bmcquade9dd54cc2015-06-22 16:56:52214const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
215 "LowPriorityIFrames";
pmeenan9ac669682015-08-17 14:57:03216const char BlinkSettingsFieldTrialTest::kResourcePrioritiesFieldTrialName[] =
217 "ResourcePriorities";
218const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
219const char BlinkSettingsFieldTrialTest::kDefaultGroupName[] = "Default";
bmcquade5d2d9cf32015-06-19 17:42:28220
221TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
222 AppendContentBrowserClientSwitches();
223 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
224}
225
226TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
pmeenan9ac669682015-08-17 14:57:03227 CreateFieldTrial(kParserFieldTrialName, kFakeGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28228 AppendContentBrowserClientSwitches();
229 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
230}
231
232TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
233 AppendBlinkSettingsSwitch("foo");
pmeenan9ac669682015-08-17 14:57:03234 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52235 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28236 AppendContentBrowserClientSwitches();
237 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
238 EXPECT_EQ("foo",
239 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
240}
241
242TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
pmeenan9ac669682015-08-17 14:57:03243 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52244 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28245 AppendContentBrowserClientSwitches();
246 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
247 EXPECT_EQ("key1=value1,key2=value2",
248 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
249}
250
bmcquade9dd54cc2015-06-22 16:56:52251TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) {
pmeenan9ac669682015-08-17 14:57:03252 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52253 "key1", "value1", "key2", "value2");
pmeenan9ac669682015-08-17 14:57:03254 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
bmcquade9dd54cc2015-06-22 16:56:52255 "keyA", "valueA", "keyB", "valueB");
256 AppendContentBrowserClientSwitches();
257 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
258 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
259 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
260}
261
262TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) {
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 "key2", "duplicate", "key3", "value3");
267 AppendContentBrowserClientSwitches();
268 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
269 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
270 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
271}
272
pmeenan9ac669682015-08-17 14:57:03273TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDefault) {
274 CreateFieldTrial(kResourcePrioritiesFieldTrialName, kDefaultGroupName);
275 AppendContentBrowserClientSwitches();
276 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
277}
278
279TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesEverythingEnabled) {
280 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
281 "Everything_11111_1_1_10");
282 AppendContentBrowserClientSwitches();
283 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
284 EXPECT_EQ("fetchDeferLateScripts=true,"
285 "fetchIncreaseFontPriority=true,"
286 "fetchIncreaseAsyncScriptPriority=true,"
287 "fetchIncreasePriorities=true",
288 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
289}
290
291TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDeferLateScripts) {
292 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
293 "LateScripts_10000_0_1_10");
294 AppendContentBrowserClientSwitches();
295 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
296 EXPECT_EQ("fetchDeferLateScripts=true",
297 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
298}
299
300TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesFontsEnabled) {
301 CreateFieldTrial(kResourcePrioritiesFieldTrialName, "FontOnly_01000_0_1_10");
302 AppendContentBrowserClientSwitches();
303 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
304 EXPECT_EQ("fetchIncreaseFontPriority=true",
305 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
306}
307
308TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreaseAsyncScript) {
309 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
310 "AsyncScript_00100_0_1_10");
311 AppendContentBrowserClientSwitches();
312 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
313 EXPECT_EQ("fetchIncreaseAsyncScriptPriority=true",
314 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
315}
316
317TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreasePriorities) {
318 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
319 "IncreasePriorities_00010_0_1_10");
320 AppendContentBrowserClientSwitches();
321 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
322 EXPECT_EQ("fetchIncreasePriorities=true",
323 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
324}
325
[email protected]10a9bf92013-11-13 23:34:48326#if !defined(OS_IOS) && !defined(OS_ANDROID)
327namespace content {
328
329class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
330 protected:
dchenge1bc7982014-10-30 00:32:40331 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48332 BrowserWithTestWindowTest::SetUp();
333 field_trial_list_.reset(new base::FieldTrialList(
334 new metrics::SHA1EntropyProvider("42")));
335 }
336
337 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
338 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
339 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
340 TemplateURLService* template_url_service =
341 TemplateURLServiceFactory::GetForProfile(browser()->profile());
thestigf80564462015-09-29 23:12:08342 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
[email protected]10a9bf92013-11-13 23:34:48343
344 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53345 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48346 data.SetURL("http://foo.com/url?bar={searchTerms}");
347 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28348 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48349 // Takes ownership.
350 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26351 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48352 }
353
354 scoped_ptr<base::FieldTrialList> field_trial_list_;
355};
356
357TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
358 const GURL url_original("chrome://newtab");
359 const GURL url_rewritten("https://www.example.com/newtab");
360 InstallTemplateURLWithNewTabPage(url_rewritten);
361 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
362 "Group1 use_cacheable_ntp:1"));
363
364 AddTab(browser(), GURL("chrome://blank"));
365 NavigateAndCommitActiveTab(url_original);
366
367 NavigationEntry* entry = browser()->tab_strip_model()->
368 GetActiveWebContents()->GetController().GetLastCommittedEntry();
369 ASSERT_TRUE(entry != NULL);
370 EXPECT_EQ(url_rewritten, entry->GetURL());
371 EXPECT_EQ(url_original, entry->GetVirtualURL());
372}
373
374} // namespace content
375#endif // !defined(OS_IOS) && !defined(OS_ANDROID)