blob: 9e98585cd57d573636140c4b4b0c4a18c7652b9f [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"
[email protected]10a9bf92013-11-13 23:34:4821#include "content/public/browser/navigation_controller.h"
22#include "content/public/browser/navigation_entry.h"
23#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2724#include "content/public/common/content_switches.h"
bmcquade5d2d9cf32015-06-19 17:42:2825#include "content/public/test/test_browser_thread_bundle.h"
[email protected]3d831992013-07-04 01:13:2926#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1327#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2928
29namespace chrome {
30
mlamourieb40d552015-02-05 00:57:0831using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2932
[email protected]3d831992013-07-04 01:13:2933TEST_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")));
38}
39
mlamourieb40d552015-02-05 00:57:0840// BrowserWithTestWindowTest doesn't work on iOS and Android.
41#if !defined(OS_ANDROID) && !defined(OS_IOS)
42
peterbbcccc12015-02-11 22:23:3343using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
44
45static void DidOpenURLForWindowTest(content::WebContents** target_contents,
46 content::WebContents* opened_contents) {
47 DCHECK(target_contents);
48
49 *target_contents = opened_contents;
50}
51
mlamourieb40d552015-02-05 00:57:0852// 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.
55TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
56 ChromeContentBrowserClient client;
57
58 int previous_count = browser()->tab_strip_model()->count();
59
60 GURL urls[] = { GURL("https://www.google.com"),
61 GURL("https://www.chromium.org") };
62
63 for (const GURL& url : urls) {
64 content::OpenURLParams params(url,
65 content::Referrer(),
66 NEW_FOREGROUND_TAB,
67 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
68 false);
peterbbcccc12015-02-11 22:23:3369 // 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(),
75 params,
76 base::Bind(&DidOpenURLForWindowTest, &web_contents));
77
78 EXPECT_TRUE(web_contents);
mlamourieb40d552015-02-05 00:57:0879
80 content::WebContents* active_contents = browser()->tab_strip_model()->
81 GetActiveWebContents();
peterbbcccc12015-02-11 22:23:3382 EXPECT_EQ(web_contents, active_contents);
mlamourieb40d552015-02-05 00:57:0883 EXPECT_EQ(url, active_contents->GetVisibleURL());
84 }
85
86 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
87}
88
89#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
90
brettwa68ea2b2015-02-01 02:54:0791#if defined(ENABLE_WEBRTC)
92
[email protected]30335fdf02014-02-26 19:51:2793// NOTE: Any updates to the expectations in these tests should also be done in
94// the browser test WebRtcDisableEncryptionFlagBrowserTest.
95class DisableWebRtcEncryptionFlagTest : public testing::Test {
96 public:
97 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:1798 : from_command_line_(base::CommandLine::NO_PROGRAM),
99 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:27100
101 protected:
dchenge1bc7982014-10-30 00:32:40102 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:27103 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
104 }
105
106 void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel) {
107 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
108 &to_command_line_,
109 from_command_line_,
110 channel);
111 }
112
avi3ef9ec9e2014-12-22 22:50:17113 base::CommandLine from_command_line_;
114 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:27115
116 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
117};
118
119TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
120 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN);
121 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
122}
123
124TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
125 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY);
126 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
127}
128
129TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
130 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV);
131 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
132}
133
134TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
135 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA);
136#if defined(OS_ANDROID)
137 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
138#else
139 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
140#endif
141}
142
143TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
144 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE);
145 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
146}
147
brettwa68ea2b2015-02-01 02:54:07148#endif // ENABLE_WEBRTC
149
bmcquade5d2d9cf32015-06-19 17:42:28150class BlinkSettingsFieldTrialTest : public testing::Test {
151 public:
152 BlinkSettingsFieldTrialTest()
153 : trial_list_(NULL),
154 command_line_(base::CommandLine::NO_PROGRAM) {}
155
156 void SetUp() override {
157 command_line_.AppendSwitchASCII(
158 switches::kProcessType, switches::kRendererProcess);
159 }
160
161 void TearDown() override {
162 variations::testing::ClearAllVariationParams();
163 }
164
165 void CreateFieldTrial() {
166 base::FieldTrialList::CreateFieldTrial(kFieldTrialName, kGroupName);
167 }
168
169 void CreateFieldTrialWithParams() {
170 CreateFieldTrial();
171 std::map<std::string, std::string> params;
172 params.insert(std::make_pair("key1", "value1"));
173 params.insert(std::make_pair("key2", "value2"));
174 variations::AssociateVariationParams(kFieldTrialName, kGroupName, params);
175 }
176
177 void AppendContentBrowserClientSwitches() {
178 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
179 }
180
181 const base::CommandLine& command_line() const {
182 return command_line_;
183 }
184
185 void AppendBlinkSettingsSwitch(const char* value) {
186 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
187 }
188
189 private:
190 static const int kFakeChildProcessId = 1;
191 static const char kFieldTrialName[];
192 static const char kGroupName[];
193
194 ChromeContentBrowserClient client_;
195 base::FieldTrialList trial_list_;
196 base::CommandLine command_line_;
197
198 content::TestBrowserThreadBundle thread_bundle_;
199};
200
201const char BlinkSettingsFieldTrialTest::kFieldTrialName[] =
202 "BackgroundHtmlParserTokenLimits";
203const char BlinkSettingsFieldTrialTest::kGroupName[] = "FakeGroup";
204
205TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
206 AppendContentBrowserClientSwitches();
207 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
208}
209
210TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
211 CreateFieldTrial();
212 AppendContentBrowserClientSwitches();
213 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
214}
215
216TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
217 AppendBlinkSettingsSwitch("foo");
218 CreateFieldTrialWithParams();
219 AppendContentBrowserClientSwitches();
220 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
221 EXPECT_EQ("foo",
222 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
223}
224
225TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
226 CreateFieldTrialWithParams();
227 AppendContentBrowserClientSwitches();
228 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
229 EXPECT_EQ("key1=value1,key2=value2",
230 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
231}
232
[email protected]3d831992013-07-04 01:13:29233} // namespace chrome
[email protected]10a9bf92013-11-13 23:34:48234
235#if !defined(OS_IOS) && !defined(OS_ANDROID)
236namespace content {
237
238class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
239 protected:
dchenge1bc7982014-10-30 00:32:40240 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48241 BrowserWithTestWindowTest::SetUp();
242 field_trial_list_.reset(new base::FieldTrialList(
243 new metrics::SHA1EntropyProvider("42")));
244 }
245
246 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
247 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
248 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
249 TemplateURLService* template_url_service =
250 TemplateURLServiceFactory::GetForProfile(browser()->profile());
251 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
252
253 TemplateURLData data;
mpearson3c6d7af2015-05-13 23:59:53254 data.SetShortName(base::ASCIIToUTF16("foo.com"));
[email protected]10a9bf92013-11-13 23:34:48255 data.SetURL("http://foo.com/url?bar={searchTerms}");
256 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28257 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48258 // Takes ownership.
259 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26260 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48261 }
262
263 scoped_ptr<base::FieldTrialList> field_trial_list_;
264};
265
266TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
267 const GURL url_original("chrome://newtab");
268 const GURL url_rewritten("https://www.example.com/newtab");
269 InstallTemplateURLWithNewTabPage(url_rewritten);
270 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
271 "Group1 use_cacheable_ntp:1"));
272
273 AddTab(browser(), GURL("chrome://blank"));
274 NavigateAndCommitActiveTab(url_original);
275
276 NavigationEntry* entry = browser()->tab_strip_model()->
277 GetActiveWebContents()->GetController().GetLastCommittedEntry();
278 ASSERT_TRUE(entry != NULL);
279 EXPECT_EQ(url_rewritten, entry->GetURL());
280 EXPECT_EQ(url_original, entry->GetVirtualURL());
281}
282
283} // namespace content
284#endif // !defined(OS_IOS) && !defined(OS_ANDROID)