blob: 5b1317b6159d34a1506717d01a0e638ce9b3378d [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"
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"
timvolodinea47291a2015-01-23 14:04:4317#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]bf5c532d2014-07-05 00:29:5318#include "components/search_engines/template_url_service.h"
[email protected]10a9bf92013-11-13 23:34:4819#include "components/variations/entropy_provider.h"
bmcquade5d2d9cf32015-06-19 17:42:2820#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2221#include "components/version_info/version_info.h"
[email protected]10a9bf92013-11-13 23:34:4822#include "content/public/browser/navigation_controller.h"
23#include "content/public/browser/navigation_entry.h"
24#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2725#include "content/public/common/content_switches.h"
bmcquade5d2d9cf32015-06-19 17:42:2826#include "content/public/test/test_browser_thread_bundle.h"
[email protected]3d831992013-07-04 01:13:2927#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1328#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2929
30namespace chrome {
31
mlamourieb40d552015-02-05 00:57:0832using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2933
[email protected]3d831992013-07-04 01:13:2934TEST_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")));
39}
40
mlamourieb40d552015-02-05 00:57:0841// BrowserWithTestWindowTest doesn't work on iOS and Android.
42#if !defined(OS_ANDROID) && !defined(OS_IOS)
43
peterbbcccc12015-02-11 22:23:3344using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
45
46static void DidOpenURLForWindowTest(content::WebContents** target_contents,
47 content::WebContents* opened_contents) {
48 DCHECK(target_contents);
49
50 *target_contents = opened_contents;
51}
52
mlamourieb40d552015-02-05 00:57:0853// 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.
56TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
57 ChromeContentBrowserClient client;
58
59 int previous_count = browser()->tab_strip_model()->count();
60
61 GURL urls[] = { GURL("https://www.google.com"),
62 GURL("https://www.chromium.org") };
63
64 for (const GURL& url : urls) {
65 content::OpenURLParams params(url,
66 content::Referrer(),
67 NEW_FOREGROUND_TAB,
68 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
69 false);
peterbbcccc12015-02-11 22:23:3370 // 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(),
76 params,
77 base::Bind(&DidOpenURLForWindowTest, &web_contents));
78
79 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:0880
81 content::WebContents* active_contents = browser()->tab_strip_model()->
82 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:3383 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:0884 EXPECT_EQ(url, active_contents->GetVisibleURL());
85 }
86
87 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
88}
89
90#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
91
brettwa68ea2b2015-02-01 02:54:0792#if defined(ENABLE_WEBRTC)
93
[email protected]30335fdf02014-02-26 19:51:2794// NOTE: Any updates to the expectations in these tests should also be done in
95// the browser test WebRtcDisableEncryptionFlagBrowserTest.
96class DisableWebRtcEncryptionFlagTest : public testing::Test {
97 public:
98 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:1799 : from_command_line_(base::CommandLine::NO_PROGRAM),
100 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27101
102 protected:
dchenge1bc7982014-10-30 00:32:40103 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27104 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
105 }
106
sdefresne6e883e42015-07-30 08:05:54107 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
[email protected]30335fdf02014-02-26 19:51:27108 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
109 &to_command_line_,
110 from_command_line_,
111 channel);
112 }
113
avi3ef9ec9e2014-12-22 22:50:17114 base::CommandLine from_command_line_;
115 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27116
117 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
118};
119
120TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
sdefresne6e883e42015-07-30 08:05:54121 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::UNKNOWN);
[email protected]30335fdf02014-02-26 19:51:27122 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
123}
124
125TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
sdefresne6e883e42015-07-30 08:05:54126 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::CANARY);
[email protected]30335fdf02014-02-26 19:51:27127 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
128}
129
130TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
sdefresne6e883e42015-07-30 08:05:54131 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::DEV);
[email protected]30335fdf02014-02-26 19:51:27132 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
133}
134
135TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
sdefresne6e883e42015-07-30 08:05:54136 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::BETA);
[email protected]30335fdf02014-02-26 19:51:27137#if defined(OS_ANDROID)
138 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
139#else
140 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
141#endif
142}
143
144TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
sdefresne6e883e42015-07-30 08:05:54145 MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel::STABLE);
[email protected]30335fdf02014-02-26 19:51:27146 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
147}
148
brettwa68ea2b2015-02-01 02:54:07149#endif // ENABLE_WEBRTC
150
bmcquade5d2d9cf32015-06-19 17:42:28151class BlinkSettingsFieldTrialTest : public testing::Test {
152 public:
bmcquade9dd54cc2015-06-22 16:56:52153 static const char kParserFieldTrialName[];
154 static const char kIFrameFieldTrialName[];
155
bmcquade5d2d9cf32015-06-19 17:42:28156 BlinkSettingsFieldTrialTest()
157 : trial_list_(NULL),
158 command_line_(base::CommandLine::NO_PROGRAM) {}
159
160 void SetUp() override {
161 command_line_.AppendSwitchASCII(
162 switches::kProcessType, switches::kRendererProcess);
163 }
164
165 void TearDown() override {
166 variations::testing::ClearAllVariationParams();
167 }
168
bmcquade9dd54cc2015-06-22 16:56:52169 void CreateFieldTrial(const char* trial_name) {
170 base::FieldTrialList::CreateFieldTrial(trial_name, kGroupName);
bmcquade5d2d9cf32015-06-19 17:42:28171 }
172
bmcquade9dd54cc2015-06-22 16:56:52173 void CreateFieldTrialWithParams(
174 const char* trial_name,
175 const char* key1, const char* value1,
176 const char* key2, const char* value2) {
bmcquade5d2d9cf32015-06-19 17:42:28177 std::map<std::string, std::string> params;
bmcquade9dd54cc2015-06-22 16:56:52178 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);
bmcquade5d2d9cf32015-06-19 17:42:28182 }
183
184 void AppendContentBrowserClientSwitches() {
185 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
186 }
187
188 const base::CommandLine& command_line() const {
189 return command_line_;
190 }
191
192 void AppendBlinkSettingsSwitch(const char* value) {
193 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
194 }
195
196 private:
197 static const int kFakeChildProcessId = 1;
bmcquade5d2d9cf32015-06-19 17:42:28198 static const char kGroupName[];
199
200 ChromeContentBrowserClient client_;
201 base::FieldTrialList trial_list_;
202 base::CommandLine command_line_;
203
204 content::TestBrowserThreadBundle thread_bundle_;
205};
206
bmcquade9dd54cc2015-06-22 16:56:52207const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
bmcquade5d2d9cf32015-06-19 17:42:28208 "BackgroundHtmlParserTokenLimits";
bmcquade9dd54cc2015-06-22 16:56:52209const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
210 "LowPriorityIFrames";
bmcquade5d2d9cf32015-06-19 17:42:28211const char BlinkSettingsFieldTrialTest::kGroupName[] = "FakeGroup";
212
213TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
214 AppendContentBrowserClientSwitches();
215 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
216}
217
218TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
bmcquade9dd54cc2015-06-22 16:56:52219 CreateFieldTrial(kParserFieldTrialName);
bmcquade5d2d9cf32015-06-19 17:42:28220 AppendContentBrowserClientSwitches();
221 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
222}
223
224TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
225 AppendBlinkSettingsSwitch("foo");
bmcquade9dd54cc2015-06-22 16:56:52226 CreateFieldTrialWithParams(kParserFieldTrialName,
227 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28228 AppendContentBrowserClientSwitches();
229 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
230 EXPECT_EQ("foo",
231 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
232}
233
234TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
bmcquade9dd54cc2015-06-22 16:56:52235 CreateFieldTrialWithParams(kParserFieldTrialName,
236 "key1", "value1", "key2", "value2");
bmcquade5d2d9cf32015-06-19 17:42:28237 AppendContentBrowserClientSwitches();
238 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
239 EXPECT_EQ("key1=value1,key2=value2",
240 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
241}
242
bmcquade9dd54cc2015-06-22 16:56:52243TEST_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));
252}
253
254TEST_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));
263}
264
[email protected]3d831992013-07-04 01:13:29265} // namespace chrome
[email protected]10a9bf92013-11-13 23:34:48266
267#if !defined(OS_IOS) && !defined(OS_ANDROID)
268namespace content {
269
270class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
271 protected:
dchenge1bc7982014-10-30 00:32:40272 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48273 BrowserWithTestWindowTest::SetUp();
274 field_trial_list_.reset(new base::FieldTrialList(
275 new metrics::SHA1EntropyProvider("42")));
276 }
277
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);
284
285 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53286 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48287 data.SetURL("http://foo.com/url?bar={searchTerms}");
288 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28289 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48290 // Takes ownership.
291 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26292 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48293 }
294
295 scoped_ptr<base::FieldTrialList> field_trial_list_;
296};
297
298TEST_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"));
304
305 AddTab(browser(), GURL("chrome://blank"));
306 NavigateAndCommitActiveTab(url_original);
307
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());
313}
314
315} // namespace content
316#endif // !defined(OS_IOS) && !defined(OS_ANDROID)