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 "chrome/browser/chrome_content_browser_client.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "components/variations/entropy_provider.h"
20 #include "components/variations/variations_associated_data.h"
21 #include "components/version_info/version_info.h"
22 #include "content/public/browser/navigation_controller.h"
23 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "testing/gtest/include/gtest/gtest.h"
30 using ChromeContentBrowserClientTest
= testing::Test
;
32 TEST_F(ChromeContentBrowserClientTest
, ShouldAssignSiteForURL
) {
33 ChromeContentBrowserClient client
;
34 EXPECT_FALSE(client
.ShouldAssignSiteForURL(GURL("chrome-native://test")));
35 EXPECT_TRUE(client
.ShouldAssignSiteForURL(GURL("http://www.google.com")));
36 EXPECT_TRUE(client
.ShouldAssignSiteForURL(GURL("https://www.google.com")));
39 // BrowserWithTestWindowTest doesn't work on iOS and Android.
40 #if !defined(OS_ANDROID) && !defined(OS_IOS)
42 using ChromeContentBrowserClientWindowTest
= BrowserWithTestWindowTest
;
44 static void DidOpenURLForWindowTest(content::WebContents
** target_contents
,
45 content::WebContents
* opened_contents
) {
46 DCHECK(target_contents
);
48 *target_contents
= opened_contents
;
51 // This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
52 // URLs to be opened in new tabs and activated, changing the active tabs after
53 // each call and increasing the tab count by 2.
54 TEST_F(ChromeContentBrowserClientWindowTest
, OpenURL
) {
55 ChromeContentBrowserClient client
;
57 int previous_count
= browser()->tab_strip_model()->count();
59 GURL urls
[] = { GURL("https://www.google.com"),
60 GURL("https://www.chromium.org") };
62 for (const GURL
& url
: urls
) {
63 content::OpenURLParams
params(url
,
66 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
68 // TODO(peter): We should have more in-depth browser tests for the window
69 // opening functionality, which also covers Android. This test can currently
70 // only be ran on platforms where OpenURL is implemented synchronously.
71 // See https://crbug.com/457667.
72 content::WebContents
* web_contents
= nullptr;
73 client
.OpenURL(browser()->profile(),
75 base::Bind(&DidOpenURLForWindowTest
, &web_contents
));
77 EXPECT_TRUE(web_contents
);
79 content::WebContents
* active_contents
= browser()->tab_strip_model()->
80 GetActiveWebContents();
81 EXPECT_EQ(web_contents
, active_contents
);
82 EXPECT_EQ(url
, active_contents
->GetVisibleURL());
85 EXPECT_EQ(previous_count
+ 2, browser()->tab_strip_model()->count());
88 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
90 #if defined(ENABLE_WEBRTC)
92 // NOTE: Any updates to the expectations in these tests should also be done in
93 // the browser test WebRtcDisableEncryptionFlagBrowserTest.
94 class DisableWebRtcEncryptionFlagTest
: public testing::Test
{
96 DisableWebRtcEncryptionFlagTest()
97 : from_command_line_(base::CommandLine::NO_PROGRAM
),
98 to_command_line_(base::CommandLine::NO_PROGRAM
) {}
101 void SetUp() override
{
102 from_command_line_
.AppendSwitch(switches::kDisableWebRtcEncryption
);
105 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel
) {
106 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
112 base::CommandLine from_command_line_
;
113 base::CommandLine to_command_line_
;
115 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest
);
118 TEST_F(DisableWebRtcEncryptionFlagTest
, UnknownChannel
) {
119 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN
);
120 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
123 TEST_F(DisableWebRtcEncryptionFlagTest
, CanaryChannel
) {
124 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY
);
125 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
128 TEST_F(DisableWebRtcEncryptionFlagTest
, DevChannel
) {
129 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV
);
130 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
133 TEST_F(DisableWebRtcEncryptionFlagTest
, BetaChannel
) {
134 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA
);
135 #if defined(OS_ANDROID)
136 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
138 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
142 TEST_F(DisableWebRtcEncryptionFlagTest
, StableChannel
) {
143 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE
);
144 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
147 #endif // ENABLE_WEBRTC
149 class BlinkSettingsFieldTrialTest
: public testing::Test
{
151 static const char kParserFieldTrialName
[];
152 static const char kIFrameFieldTrialName
[];
153 static const char kResourcePrioritiesFieldTrialName
[];
154 static const char kFakeGroupName
[];
155 static const char kDefaultGroupName
[];
157 BlinkSettingsFieldTrialTest()
159 command_line_(base::CommandLine::NO_PROGRAM
) {}
161 void SetUp() override
{
162 command_line_
.AppendSwitchASCII(
163 switches::kProcessType
, switches::kRendererProcess
);
166 void TearDown() override
{
167 variations::testing::ClearAllVariationParams();
170 void CreateFieldTrial(const char* trial_name
, const char* group_name
) {
171 base::FieldTrialList::CreateFieldTrial(trial_name
, group_name
);
174 void CreateFieldTrialWithParams(
175 const char* trial_name
,
176 const char* group_name
,
177 const char* key1
, const char* value1
,
178 const char* key2
, const char* value2
) {
179 std::map
<std::string
, std::string
> params
;
180 params
.insert(std::make_pair(key1
, value1
));
181 params
.insert(std::make_pair(key2
, value2
));
182 CreateFieldTrial(trial_name
, kFakeGroupName
);
183 variations::AssociateVariationParams(trial_name
, kFakeGroupName
, params
);
186 void AppendContentBrowserClientSwitches() {
187 client_
.AppendExtraCommandLineSwitches(&command_line_
, kFakeChildProcessId
);
190 const base::CommandLine
& command_line() const {
191 return command_line_
;
194 void AppendBlinkSettingsSwitch(const char* value
) {
195 command_line_
.AppendSwitchASCII(switches::kBlinkSettings
, value
);
199 static const int kFakeChildProcessId
= 1;
201 ChromeContentBrowserClient client_
;
202 base::FieldTrialList trial_list_
;
203 base::CommandLine command_line_
;
205 content::TestBrowserThreadBundle thread_bundle_
;
208 const char BlinkSettingsFieldTrialTest::kParserFieldTrialName
[] =
209 "BackgroundHtmlParserTokenLimits";
210 const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName
[] =
211 "LowPriorityIFrames";
212 const char BlinkSettingsFieldTrialTest::kResourcePrioritiesFieldTrialName
[] =
213 "ResourcePriorities";
214 const char BlinkSettingsFieldTrialTest::kFakeGroupName
[] = "FakeGroup";
215 const char BlinkSettingsFieldTrialTest::kDefaultGroupName
[] = "Default";
217 TEST_F(BlinkSettingsFieldTrialTest
, NoFieldTrial
) {
218 AppendContentBrowserClientSwitches();
219 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings
));
222 TEST_F(BlinkSettingsFieldTrialTest
, FieldTrialWithoutParams
) {
223 CreateFieldTrial(kParserFieldTrialName
, kFakeGroupName
);
224 AppendContentBrowserClientSwitches();
225 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings
));
228 TEST_F(BlinkSettingsFieldTrialTest
, BlinkSettingsSwitchAlreadySpecified
) {
229 AppendBlinkSettingsSwitch("foo");
230 CreateFieldTrialWithParams(kParserFieldTrialName
, kFakeGroupName
,
231 "key1", "value1", "key2", "value2");
232 AppendContentBrowserClientSwitches();
233 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
235 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
238 TEST_F(BlinkSettingsFieldTrialTest
, FieldTrialEnabled
) {
239 CreateFieldTrialWithParams(kParserFieldTrialName
, kFakeGroupName
,
240 "key1", "value1", "key2", "value2");
241 AppendContentBrowserClientSwitches();
242 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
243 EXPECT_EQ("key1=value1,key2=value2",
244 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
247 TEST_F(BlinkSettingsFieldTrialTest
, MultipleFieldTrialsEnabled
) {
248 CreateFieldTrialWithParams(kParserFieldTrialName
, kFakeGroupName
,
249 "key1", "value1", "key2", "value2");
250 CreateFieldTrialWithParams(kIFrameFieldTrialName
, kFakeGroupName
,
251 "keyA", "valueA", "keyB", "valueB");
252 AppendContentBrowserClientSwitches();
253 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
254 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
255 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
258 TEST_F(BlinkSettingsFieldTrialTest
, MultipleFieldTrialsDuplicateKeys
) {
259 CreateFieldTrialWithParams(kParserFieldTrialName
, kFakeGroupName
,
260 "key1", "value1", "key2", "value2");
261 CreateFieldTrialWithParams(kIFrameFieldTrialName
, kFakeGroupName
,
262 "key2", "duplicate", "key3", "value3");
263 AppendContentBrowserClientSwitches();
264 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
265 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
266 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
269 TEST_F(BlinkSettingsFieldTrialTest
, ResourcePrioritiesDefault
) {
270 CreateFieldTrial(kResourcePrioritiesFieldTrialName
, kDefaultGroupName
);
271 AppendContentBrowserClientSwitches();
272 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings
));
275 TEST_F(BlinkSettingsFieldTrialTest
, ResourcePrioritiesEverythingEnabled
) {
276 CreateFieldTrial(kResourcePrioritiesFieldTrialName
,
277 "Everything_11111_1_1_10");
278 AppendContentBrowserClientSwitches();
279 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
280 EXPECT_EQ("fetchDeferLateScripts=true,"
281 "fetchIncreaseFontPriority=true,"
282 "fetchIncreaseAsyncScriptPriority=true,"
283 "fetchIncreasePriorities=true",
284 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
287 TEST_F(BlinkSettingsFieldTrialTest
, ResourcePrioritiesDeferLateScripts
) {
288 CreateFieldTrial(kResourcePrioritiesFieldTrialName
,
289 "LateScripts_10000_0_1_10");
290 AppendContentBrowserClientSwitches();
291 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
292 EXPECT_EQ("fetchDeferLateScripts=true",
293 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
296 TEST_F(BlinkSettingsFieldTrialTest
, ResourcePrioritiesFontsEnabled
) {
297 CreateFieldTrial(kResourcePrioritiesFieldTrialName
, "FontOnly_01000_0_1_10");
298 AppendContentBrowserClientSwitches();
299 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
300 EXPECT_EQ("fetchIncreaseFontPriority=true",
301 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
304 TEST_F(BlinkSettingsFieldTrialTest
, ResourcePrioritiesIncreaseAsyncScript
) {
305 CreateFieldTrial(kResourcePrioritiesFieldTrialName
,
306 "AsyncScript_00100_0_1_10");
307 AppendContentBrowserClientSwitches();
308 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
309 EXPECT_EQ("fetchIncreaseAsyncScriptPriority=true",
310 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
313 TEST_F(BlinkSettingsFieldTrialTest
, ResourcePrioritiesIncreasePriorities
) {
314 CreateFieldTrial(kResourcePrioritiesFieldTrialName
,
315 "IncreasePriorities_00010_0_1_10");
316 AppendContentBrowserClientSwitches();
317 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
318 EXPECT_EQ("fetchIncreasePriorities=true",
319 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
322 #if !defined(OS_IOS) && !defined(OS_ANDROID)
325 class InstantNTPURLRewriteTest
: public BrowserWithTestWindowTest
{
327 void SetUp() override
{
328 BrowserWithTestWindowTest::SetUp();
329 field_trial_list_
.reset(new base::FieldTrialList(
330 new metrics::SHA1EntropyProvider("42")));
333 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url
) {
334 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
335 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
336 TemplateURLService
* template_url_service
=
337 TemplateURLServiceFactory::GetForProfile(browser()->profile());
338 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
340 TemplateURLData data
;
341 data
.SetShortName(base::ASCIIToUTF16("foo.com"));
342 data
.SetURL("http://foo.com/url?bar={searchTerms}");
343 data
.new_tab_url
= new_tab_page_url
.spec();
344 TemplateURL
* template_url
= new TemplateURL(data
);
346 template_url_service
->Add(template_url
);
347 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
350 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
353 TEST_F(InstantNTPURLRewriteTest
, UberURLHandler_InstantExtendedNewTabPage
) {
354 const GURL
url_original("chrome://newtab");
355 const GURL
url_rewritten("https://www.example.com/newtab");
356 InstallTemplateURLWithNewTabPage(url_rewritten
);
357 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
358 "Group1 use_cacheable_ntp:1"));
360 AddTab(browser(), GURL("chrome://blank"));
361 NavigateAndCommitActiveTab(url_original
);
363 NavigationEntry
* entry
= browser()->tab_strip_model()->
364 GetActiveWebContents()->GetController().GetLastCommittedEntry();
365 ASSERT_TRUE(entry
!= NULL
);
366 EXPECT_EQ(url_rewritten
, entry
->GetURL());
367 EXPECT_EQ(url_original
, entry
->GetVirtualURL());
370 } // namespace content
371 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)