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 "content/public/browser/navigation_controller.h"
22 #include "content/public/browser/navigation_entry.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "testing/gtest/include/gtest/gtest.h"
31 using ChromeContentBrowserClientTest
= testing::Test
;
33 TEST_F(ChromeContentBrowserClientTest
, ShouldAssignSiteForURL
) {
34 ChromeContentBrowserClient client
;
35 EXPECT_FALSE(client
.ShouldAssignSiteForURL(GURL("chrome-native://test")));
36 EXPECT_TRUE(client
.ShouldAssignSiteForURL(GURL("http://www.google.com")));
37 EXPECT_TRUE(client
.ShouldAssignSiteForURL(GURL("https://www.google.com")));
40 // BrowserWithTestWindowTest doesn't work on iOS and Android.
41 #if !defined(OS_ANDROID) && !defined(OS_IOS)
43 using ChromeContentBrowserClientWindowTest
= BrowserWithTestWindowTest
;
45 static void DidOpenURLForWindowTest(content::WebContents
** target_contents
,
46 content::WebContents
* opened_contents
) {
47 DCHECK(target_contents
);
49 *target_contents
= opened_contents
;
52 // This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
53 // URLs to be opened in new tabs and activated, changing the active tabs after
54 // each call and increasing the tab count by 2.
55 TEST_F(ChromeContentBrowserClientWindowTest
, OpenURL
) {
56 ChromeContentBrowserClient client
;
58 int previous_count
= browser()->tab_strip_model()->count();
60 GURL urls
[] = { GURL("https://www.google.com"),
61 GURL("https://www.chromium.org") };
63 for (const GURL
& url
: urls
) {
64 content::OpenURLParams
params(url
,
67 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
69 // TODO(peter): We should have more in-depth browser tests for the window
70 // opening functionality, which also covers Android. This test can currently
71 // only be ran on platforms where OpenURL is implemented synchronously.
72 // See https://crbug.com/457667.
73 content::WebContents
* web_contents
= nullptr;
74 client
.OpenURL(browser()->profile(),
76 base::Bind(&DidOpenURLForWindowTest
, &web_contents
));
78 EXPECT_TRUE(web_contents
);
80 content::WebContents
* active_contents
= browser()->tab_strip_model()->
81 GetActiveWebContents();
82 EXPECT_EQ(web_contents
, active_contents
);
83 EXPECT_EQ(url
, active_contents
->GetVisibleURL());
86 EXPECT_EQ(previous_count
+ 2, browser()->tab_strip_model()->count());
89 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
91 #if defined(ENABLE_WEBRTC)
93 // NOTE: Any updates to the expectations in these tests should also be done in
94 // the browser test WebRtcDisableEncryptionFlagBrowserTest.
95 class DisableWebRtcEncryptionFlagTest
: public testing::Test
{
97 DisableWebRtcEncryptionFlagTest()
98 : from_command_line_(base::CommandLine::NO_PROGRAM
),
99 to_command_line_(base::CommandLine::NO_PROGRAM
) {}
102 void SetUp() override
{
103 from_command_line_
.AppendSwitch(switches::kDisableWebRtcEncryption
);
106 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel
) {
107 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
113 base::CommandLine from_command_line_
;
114 base::CommandLine to_command_line_
;
116 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest
);
119 TEST_F(DisableWebRtcEncryptionFlagTest
, UnknownChannel
) {
120 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN
);
121 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
124 TEST_F(DisableWebRtcEncryptionFlagTest
, CanaryChannel
) {
125 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY
);
126 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
129 TEST_F(DisableWebRtcEncryptionFlagTest
, DevChannel
) {
130 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV
);
131 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
134 TEST_F(DisableWebRtcEncryptionFlagTest
, BetaChannel
) {
135 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA
);
136 #if defined(OS_ANDROID)
137 EXPECT_TRUE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
139 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
143 TEST_F(DisableWebRtcEncryptionFlagTest
, StableChannel
) {
144 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE
);
145 EXPECT_FALSE(to_command_line_
.HasSwitch(switches::kDisableWebRtcEncryption
));
148 #endif // ENABLE_WEBRTC
150 class BlinkSettingsFieldTrialTest
: public testing::Test
{
152 static const char kParserFieldTrialName
[];
153 static const char kIFrameFieldTrialName
[];
155 BlinkSettingsFieldTrialTest()
157 command_line_(base::CommandLine::NO_PROGRAM
) {}
159 void SetUp() override
{
160 command_line_
.AppendSwitchASCII(
161 switches::kProcessType
, switches::kRendererProcess
);
164 void TearDown() override
{
165 variations::testing::ClearAllVariationParams();
168 void CreateFieldTrial(const char* trial_name
) {
169 base::FieldTrialList::CreateFieldTrial(trial_name
, kGroupName
);
172 void CreateFieldTrialWithParams(
173 const char* trial_name
,
174 const char* key1
, const char* value1
,
175 const char* key2
, const char* value2
) {
176 std::map
<std::string
, std::string
> params
;
177 params
.insert(std::make_pair(key1
, value1
));
178 params
.insert(std::make_pair(key2
, value2
));
179 CreateFieldTrial(trial_name
);
180 variations::AssociateVariationParams(trial_name
, kGroupName
, params
);
183 void AppendContentBrowserClientSwitches() {
184 client_
.AppendExtraCommandLineSwitches(&command_line_
, kFakeChildProcessId
);
187 const base::CommandLine
& command_line() const {
188 return command_line_
;
191 void AppendBlinkSettingsSwitch(const char* value
) {
192 command_line_
.AppendSwitchASCII(switches::kBlinkSettings
, value
);
196 static const int kFakeChildProcessId
= 1;
197 static const char kGroupName
[];
199 ChromeContentBrowserClient client_
;
200 base::FieldTrialList trial_list_
;
201 base::CommandLine command_line_
;
203 content::TestBrowserThreadBundle thread_bundle_
;
206 const char BlinkSettingsFieldTrialTest::kParserFieldTrialName
[] =
207 "BackgroundHtmlParserTokenLimits";
208 const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName
[] =
209 "LowPriorityIFrames";
210 const char BlinkSettingsFieldTrialTest::kGroupName
[] = "FakeGroup";
212 TEST_F(BlinkSettingsFieldTrialTest
, NoFieldTrial
) {
213 AppendContentBrowserClientSwitches();
214 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings
));
217 TEST_F(BlinkSettingsFieldTrialTest
, FieldTrialWithoutParams
) {
218 CreateFieldTrial(kParserFieldTrialName
);
219 AppendContentBrowserClientSwitches();
220 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings
));
223 TEST_F(BlinkSettingsFieldTrialTest
, BlinkSettingsSwitchAlreadySpecified
) {
224 AppendBlinkSettingsSwitch("foo");
225 CreateFieldTrialWithParams(kParserFieldTrialName
,
226 "key1", "value1", "key2", "value2");
227 AppendContentBrowserClientSwitches();
228 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
230 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
233 TEST_F(BlinkSettingsFieldTrialTest
, FieldTrialEnabled
) {
234 CreateFieldTrialWithParams(kParserFieldTrialName
,
235 "key1", "value1", "key2", "value2");
236 AppendContentBrowserClientSwitches();
237 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
238 EXPECT_EQ("key1=value1,key2=value2",
239 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
242 TEST_F(BlinkSettingsFieldTrialTest
, MultipleFieldTrialsEnabled
) {
243 CreateFieldTrialWithParams(kParserFieldTrialName
,
244 "key1", "value1", "key2", "value2");
245 CreateFieldTrialWithParams(kIFrameFieldTrialName
,
246 "keyA", "valueA", "keyB", "valueB");
247 AppendContentBrowserClientSwitches();
248 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
249 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
250 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
253 TEST_F(BlinkSettingsFieldTrialTest
, MultipleFieldTrialsDuplicateKeys
) {
254 CreateFieldTrialWithParams(kParserFieldTrialName
,
255 "key1", "value1", "key2", "value2");
256 CreateFieldTrialWithParams(kIFrameFieldTrialName
,
257 "key2", "duplicate", "key3", "value3");
258 AppendContentBrowserClientSwitches();
259 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings
));
260 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
261 command_line().GetSwitchValueASCII(switches::kBlinkSettings
));
264 } // namespace chrome
266 #if !defined(OS_IOS) && !defined(OS_ANDROID)
269 class InstantNTPURLRewriteTest
: public BrowserWithTestWindowTest
{
271 void SetUp() override
{
272 BrowserWithTestWindowTest::SetUp();
273 field_trial_list_
.reset(new base::FieldTrialList(
274 new metrics::SHA1EntropyProvider("42")));
277 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url
) {
278 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
279 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
280 TemplateURLService
* template_url_service
=
281 TemplateURLServiceFactory::GetForProfile(browser()->profile());
282 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
284 TemplateURLData data
;
285 data
.SetShortName(base::ASCIIToUTF16("foo.com"));
286 data
.SetURL("http://foo.com/url?bar={searchTerms}");
287 data
.new_tab_url
= new_tab_page_url
.spec();
288 TemplateURL
* template_url
= new TemplateURL(data
);
290 template_url_service
->Add(template_url
);
291 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
294 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
297 TEST_F(InstantNTPURLRewriteTest
, UberURLHandler_InstantExtendedNewTabPage
) {
298 const GURL
url_original("chrome://newtab");
299 const GURL
url_rewritten("https://www.example.com/newtab");
300 InstallTemplateURLWithNewTabPage(url_rewritten
);
301 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
302 "Group1 use_cacheable_ntp:1"));
304 AddTab(browser(), GURL("chrome://blank"));
305 NavigateAndCommitActiveTab(url_original
);
307 NavigationEntry
* entry
= browser()->tab_strip_model()->
308 GetActiveWebContents()->GetController().GetLastCommittedEntry();
309 ASSERT_TRUE(entry
!= NULL
);
310 EXPECT_EQ(url_rewritten
, entry
->GetURL());
311 EXPECT_EQ(url_original
, entry
->GetVirtualURL());
314 } // namespace content
315 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)