Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / chrome_content_browser_client_unittest.cc
blobbbe874753e9edbd3c3bc264cea149787161c6a1d
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"
7 #include <map>
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"
28 #include "url/gurl.h"
30 namespace chrome {
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,
66 content::Referrer(),
67 NEW_FOREGROUND_TAB,
68 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
69 false);
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(),
76 params,
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 {
97 public:
98 DisableWebRtcEncryptionFlagTest()
99 : from_command_line_(base::CommandLine::NO_PROGRAM),
100 to_command_line_(base::CommandLine::NO_PROGRAM) {}
102 protected:
103 void SetUp() override {
104 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
107 void MaybeCopyDisableWebRtcEncryptionSwitch(version_info::Channel channel) {
108 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
109 &to_command_line_,
110 from_command_line_,
111 channel);
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));
139 #else
140 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
141 #endif
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 {
152 public:
153 static const char kParserFieldTrialName[];
154 static const char kIFrameFieldTrialName[];
155 static const char kResourcePrioritiesFieldTrialName[];
156 static const char kFakeGroupName[];
157 static const char kDefaultGroupName[];
159 BlinkSettingsFieldTrialTest()
160 : trial_list_(NULL),
161 command_line_(base::CommandLine::NO_PROGRAM) {}
163 void SetUp() override {
164 command_line_.AppendSwitchASCII(
165 switches::kProcessType, switches::kRendererProcess);
168 void TearDown() override {
169 variations::testing::ClearAllVariationParams();
172 void CreateFieldTrial(const char* trial_name, const char* group_name) {
173 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
176 void CreateFieldTrialWithParams(
177 const char* trial_name,
178 const char* group_name,
179 const char* key1, const char* value1,
180 const char* key2, const char* value2) {
181 std::map<std::string, std::string> params;
182 params.insert(std::make_pair(key1, value1));
183 params.insert(std::make_pair(key2, value2));
184 CreateFieldTrial(trial_name, kFakeGroupName);
185 variations::AssociateVariationParams(trial_name, kFakeGroupName, params);
188 void AppendContentBrowserClientSwitches() {
189 client_.AppendExtraCommandLineSwitches(&command_line_, kFakeChildProcessId);
192 const base::CommandLine& command_line() const {
193 return command_line_;
196 void AppendBlinkSettingsSwitch(const char* value) {
197 command_line_.AppendSwitchASCII(switches::kBlinkSettings, value);
200 private:
201 static const int kFakeChildProcessId = 1;
203 ChromeContentBrowserClient client_;
204 base::FieldTrialList trial_list_;
205 base::CommandLine command_line_;
207 content::TestBrowserThreadBundle thread_bundle_;
210 const char BlinkSettingsFieldTrialTest::kParserFieldTrialName[] =
211 "BackgroundHtmlParserTokenLimits";
212 const char BlinkSettingsFieldTrialTest::kIFrameFieldTrialName[] =
213 "LowPriorityIFrames";
214 const char BlinkSettingsFieldTrialTest::kResourcePrioritiesFieldTrialName[] =
215 "ResourcePriorities";
216 const char BlinkSettingsFieldTrialTest::kFakeGroupName[] = "FakeGroup";
217 const char BlinkSettingsFieldTrialTest::kDefaultGroupName[] = "Default";
219 TEST_F(BlinkSettingsFieldTrialTest, NoFieldTrial) {
220 AppendContentBrowserClientSwitches();
221 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
224 TEST_F(BlinkSettingsFieldTrialTest, FieldTrialWithoutParams) {
225 CreateFieldTrial(kParserFieldTrialName, kFakeGroupName);
226 AppendContentBrowserClientSwitches();
227 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
230 TEST_F(BlinkSettingsFieldTrialTest, BlinkSettingsSwitchAlreadySpecified) {
231 AppendBlinkSettingsSwitch("foo");
232 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
233 "key1", "value1", "key2", "value2");
234 AppendContentBrowserClientSwitches();
235 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
236 EXPECT_EQ("foo",
237 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
240 TEST_F(BlinkSettingsFieldTrialTest, FieldTrialEnabled) {
241 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
242 "key1", "value1", "key2", "value2");
243 AppendContentBrowserClientSwitches();
244 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
245 EXPECT_EQ("key1=value1,key2=value2",
246 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
249 TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsEnabled) {
250 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
251 "key1", "value1", "key2", "value2");
252 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
253 "keyA", "valueA", "keyB", "valueB");
254 AppendContentBrowserClientSwitches();
255 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
256 EXPECT_EQ("key1=value1,key2=value2,keyA=valueA,keyB=valueB",
257 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
260 TEST_F(BlinkSettingsFieldTrialTest, MultipleFieldTrialsDuplicateKeys) {
261 CreateFieldTrialWithParams(kParserFieldTrialName, kFakeGroupName,
262 "key1", "value1", "key2", "value2");
263 CreateFieldTrialWithParams(kIFrameFieldTrialName, kFakeGroupName,
264 "key2", "duplicate", "key3", "value3");
265 AppendContentBrowserClientSwitches();
266 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
267 EXPECT_EQ("key1=value1,key2=value2,key2=duplicate,key3=value3",
268 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
271 TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDefault) {
272 CreateFieldTrial(kResourcePrioritiesFieldTrialName, kDefaultGroupName);
273 AppendContentBrowserClientSwitches();
274 EXPECT_FALSE(command_line().HasSwitch(switches::kBlinkSettings));
277 TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesEverythingEnabled) {
278 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
279 "Everything_11111_1_1_10");
280 AppendContentBrowserClientSwitches();
281 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
282 EXPECT_EQ("fetchDeferLateScripts=true,"
283 "fetchIncreaseFontPriority=true,"
284 "fetchIncreaseAsyncScriptPriority=true,"
285 "fetchIncreasePriorities=true",
286 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
289 TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesDeferLateScripts) {
290 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
291 "LateScripts_10000_0_1_10");
292 AppendContentBrowserClientSwitches();
293 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
294 EXPECT_EQ("fetchDeferLateScripts=true",
295 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
298 TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesFontsEnabled) {
299 CreateFieldTrial(kResourcePrioritiesFieldTrialName, "FontOnly_01000_0_1_10");
300 AppendContentBrowserClientSwitches();
301 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
302 EXPECT_EQ("fetchIncreaseFontPriority=true",
303 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
306 TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreaseAsyncScript) {
307 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
308 "AsyncScript_00100_0_1_10");
309 AppendContentBrowserClientSwitches();
310 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
311 EXPECT_EQ("fetchIncreaseAsyncScriptPriority=true",
312 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
315 TEST_F(BlinkSettingsFieldTrialTest, ResourcePrioritiesIncreasePriorities) {
316 CreateFieldTrial(kResourcePrioritiesFieldTrialName,
317 "IncreasePriorities_00010_0_1_10");
318 AppendContentBrowserClientSwitches();
319 EXPECT_TRUE(command_line().HasSwitch(switches::kBlinkSettings));
320 EXPECT_EQ("fetchIncreasePriorities=true",
321 command_line().GetSwitchValueASCII(switches::kBlinkSettings));
324 } // namespace chrome
326 #if !defined(OS_IOS) && !defined(OS_ANDROID)
327 namespace content {
329 class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
330 protected:
331 void SetUp() override {
332 BrowserWithTestWindowTest::SetUp();
333 field_trial_list_.reset(new base::FieldTrialList(
334 new metrics::SHA1EntropyProvider("42")));
337 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
338 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
339 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
340 TemplateURLService* template_url_service =
341 TemplateURLServiceFactory::GetForProfile(browser()->profile());
342 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
344 TemplateURLData data;
345 data.SetShortName(base::ASCIIToUTF16("foo.com"));
346 data.SetURL("http://foo.com/url?bar={searchTerms}");
347 data.new_tab_url = new_tab_page_url.spec();
348 TemplateURL* template_url = new TemplateURL(data);
349 // Takes ownership.
350 template_url_service->Add(template_url);
351 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
354 scoped_ptr<base::FieldTrialList> field_trial_list_;
357 TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
358 const GURL url_original("chrome://newtab");
359 const GURL url_rewritten("https://www.example.com/newtab");
360 InstallTemplateURLWithNewTabPage(url_rewritten);
361 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
362 "Group1 use_cacheable_ntp:1"));
364 AddTab(browser(), GURL("chrome://blank"));
365 NavigateAndCommitActiveTab(url_original);
367 NavigationEntry* entry = browser()->tab_strip_model()->
368 GetActiveWebContents()->GetController().GetLastCommittedEntry();
369 ASSERT_TRUE(entry != NULL);
370 EXPECT_EQ(url_rewritten, entry->GetURL());
371 EXPECT_EQ(url_original, entry->GetVirtualURL());
374 } // namespace content
375 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)