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"
32 using ChromeContentBrowserClientTest
= testing::Test
;
34 TEST_F(ChromeContentBrowserClientTest
, ShouldAssignSiteForURL
) {
35 ChromeContentBrowserClient client
;
36 EXPECT_FALSE(client
.ShouldAssignSiteForURL(GURL("chrome-native://test")));
37 EXPECT_TRUE(client
.ShouldAssignSiteForURL(GURL("http://www.google.com")));
38 EXPECT_TRUE(client
.ShouldAssignSiteForURL(GURL("https://www.google.com")));
41 // BrowserWithTestWindowTest doesn't work on iOS and Android.
42 #if !defined(OS_ANDROID) && !defined(OS_IOS)
44 using ChromeContentBrowserClientWindowTest
= BrowserWithTestWindowTest
;
46 static void DidOpenURLForWindowTest(content::WebContents
** target_contents
,
47 content::WebContents
* opened_contents
) {
48 DCHECK(target_contents
);
50 *target_contents
= opened_contents
;
53 // This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
54 // URLs to be opened in new tabs and activated, changing the active tabs after
55 // each call and increasing the tab count by 2.
56 TEST_F(ChromeContentBrowserClientWindowTest
, OpenURL
) {
57 ChromeContentBrowserClient client
;
59 int previous_count
= browser()->tab_strip_model()->count();
61 GURL urls
[] = { GURL("https://www.google.com"),
62 GURL("https://www.chromium.org") };
64 for (const GURL
& url
: urls
) {
65 content::OpenURLParams
params(url
,
68 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
70 // TODO(peter): We should have more in-depth browser tests for the window
71 // opening functionality, which also covers Android. This test can currently
72 // only be ran on platforms where OpenURL is implemented synchronously.
73 // See https://crbug.com/457667.
74 content::WebContents
* web_contents
= nullptr;
75 client
.OpenURL(browser()->profile(),
77 base::Bind(&DidOpenURLForWindowTest
, &web_contents
));
79 EXPECT_TRUE(web_contents
);
81 content::WebContents
* active_contents
= browser()->tab_strip_model()->
82 GetActiveWebContents();
83 EXPECT_EQ(web_contents
, active_contents
);
84 EXPECT_EQ(url
, active_contents
->GetVisibleURL());
87 EXPECT_EQ(previous_count
+ 2, browser()->tab_strip_model()->count());
90 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
92 #if defined(ENABLE_WEBRTC)
94 // NOTE: Any updates to the expectations in these tests should also be done in
95 // the browser test WebRtcDisableEncryptionFlagBrowserTest.
96 class DisableWebRtcEncryptionFlagTest
: public testing::Test
{
98 DisableWebRtcEncryptionFlagTest()
99 : from_command_line_(base::CommandLine::NO_PROGRAM
),
100 to_command_line_(base::CommandLine::NO_PROGRAM
) {}
103 void SetUp() override
{
104 from_command_line_
.AppendSwitch(switches::kDisableWebRtcEncryption
);
107 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel
) {
108 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
114 base::CommandLine from_command_line_
;
115 base::CommandLine to_command_line_
;
117 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest
);
120 TEST_F(DisableWebRtcEncryptionFlagTest
, UnknownChannel
) {
121 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN
);
122 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
125 TEST_F(DisableWebRtcEncryptionFlagTest
, CanaryChannel
) {
126 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY
);
127 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
130 TEST_F(DisableWebRtcEncryptionFlagTest
, DevChannel
) {
131 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV
);
132 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
135 TEST_F(DisableWebRtcEncryptionFlagTest
, BetaChannel
) {
136 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA
);
137 #if defined(OS_ANDROID)
138 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
140 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
144 TEST_F(DisableWebRtcEncryptionFlagTest
, StableChannel
) {
145 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE
);
146 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
149 #endif // ENABLE_WEBRTC
151 class BlinkSettingsFieldTrialTest
: public testing::Test
{
153 static const char kParserFieldTrialName
[];
154 static const char kIFrameFieldTrialName
[];
156 BlinkSettingsFieldTrialTest()
158 command_line_(base::CommandLine::NO_PROGRAM
) {}
160 void SetUp() override
{
161 command_line_
.AppendSwitchASCII(
162 switches::kProcessType
, switches::kRendererProcess
);
165 void TearDown() override
{
166 variations::testing::ClearAllVariationParams();
169 void CreateFieldTrial(const char* trial_name
) {
170 base::FieldTrialList::CreateFieldTrial(trial_name
, kGroupName
);
173 void CreateFieldTrialWithParams(
174 const char* trial_name
,
175 const char* key1
, const char* value1
,
176 const char* key2
, const char* value2
) {
177 std::map
<std::string
, std::string
> params
;
178 params
.insert(std::make_pair(key1
, value1
));
179 params
.insert(std::make_pair(key2
, value2
));
180 CreateFieldTrial(trial_name
);
181 variations::AssociateVariationParams(trial_name
, kGroupName
, params
);
184 void AppendContentBrowserClientSwitches() {
185 client_
.AppendExtraCommandLineSwitches(&command_line_
, kFakeChildProcessId
);
188 const base::CommandLine
& command_line() const {
189 return command_line_
;
192 void AppendBlinkSettingsSwitch(const char* value
) {
193 command_line_
.AppendSwitchASCII(switches::kBlinkSettings
, value
);
197 static const int kFakeChildProcessId
= 1;
198 static const char kGroupName
[];
200 ChromeContentBrowserClient client_
;
201 base::FieldTrialList trial_list_
;
202 base::CommandLine command_line_
;
204 content::TestBrowserThreadBundle thread_bundle_
;
207 const char BlinkSettingsFieldTrialTest::kParserFieldTrialName
[] =
208 "BackgroundHtmlParserTokenLimits";
209 const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName
[] =
210 "LowPriorityIFrames";
211 const char BlinkSettingsFieldTrialTest::kGroupName
[] = "FakeGroup";
213 TEST_F(BlinkSettingsFieldTrialTest
, NoFieldTrial
) {
214 AppendContentBrowserClientSwitches();
215 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings
));
218 TEST_F(BlinkSettingsFieldTrialTest
, FieldTrialWithoutParams
) {
219 CreateFieldTrial(kParserFieldTrialName
);
220 AppendContentBrowserClientSwitches();
221 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings
));
224 TEST_F(BlinkSettingsFieldTrialTest
, BlinkSettingsSwitchAlreadySpecified
) {
225 AppendBlinkSettingsSwitch("foo");
226 CreateFieldTrialWithParams(kParserFieldTrialName
,
227 "key1", "value1", "key2", "value2");
228 AppendContentBrowserClientSwitches();
229 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
231 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
234 TEST_F(BlinkSettingsFieldTrialTest
, FieldTrialEnabled
) {
235 CreateFieldTrialWithParams(kParserFieldTrialName
,
236 "key1", "value1", "key2", "value2");
237 AppendContentBrowserClientSwitches();
238 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
239 EXPECT_EQ("key1=value1,key2=value2",
240 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
243 TEST_F(BlinkSettingsFieldTrialTest
, MultipleFieldTrialsEnabled
) {
244 CreateFieldTrialWithParams(kParserFieldTrialName
,
245 "key1", "value1", "key2", "value2");
246 CreateFieldTrialWithParams(kIFrameFieldTrialName
,
247 "keyA", "valueA", "keyB", "valueB");
248 AppendContentBrowserClientSwitches();
249 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
250 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
251 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
254 TEST_F(BlinkSettingsFieldTrialTest
, MultipleFieldTrialsDuplicateKeys
) {
255 CreateFieldTrialWithParams(kParserFieldTrialName
,
256 "key1", "value1", "key2", "value2");
257 CreateFieldTrialWithParams(kIFrameFieldTrialName
,
258 "key2", "duplicate", "key3", "value3");
259 AppendContentBrowserClientSwitches();
260 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
261 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
262 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
265 } // namespace chrome
267 #if !defined(OS_IOS) && !defined(OS_ANDROID)
270 class InstantNTPURLRewriteTest
: public BrowserWithTestWindowTest
{
272 void SetUp() override
{
273 BrowserWithTestWindowTest::SetUp();
274 field_trial_list_
.reset(new base::FieldTrialList(
275 new metrics::SHA1EntropyProvider("42")));
278 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url
) {
279 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
280 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
281 TemplateURLService
* template_url_service
=
282 TemplateURLServiceFactory::GetForProfile(browser()->profile());
283 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
285 TemplateURLData data
;
286 data
.SetShortName(base::ASCIIToUTF16("foo.com"));
287 data
.SetURL("http://foo.com/url?bar={searchTerms}");
288 data
.new_tab_url
= new_tab_page_url
.spec();
289 TemplateURL
* template_url
= new TemplateURL(data
);
291 template_url_service
->Add(template_url
);
292 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
295 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
298 TEST_F(InstantNTPURLRewriteTest
, UberURLHandler_InstantExtendedNewTabPage
) {
299 const GURL
url_original("chrome://newtab");
300 const GURL
url_rewritten("https://www.example.com/newtab");
301 InstallTemplateURLWithNewTabPage(url_rewritten
);
302 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
303 "Group1 use_cacheable_ntp:1"));
305 AddTab(browser(), GURL("chrome://blank"));
306 NavigateAndCommitActiveTab(url_original
);
308 NavigationEntry
* entry
= browser()->tab_strip_model()->
309 GetActiveWebContents()->GetController().GetLastCommittedEntry();
310 ASSERT_TRUE(entry
!= NULL
);
311 EXPECT_EQ(url_rewritten
, entry
->GetURL());
312 EXPECT_EQ(url_original
, entry
->GetVirtualURL());
315 } // namespace content
316 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)