blob: 42c45c5f069ae45b63369a33164bbfdf66fd1548 [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
[email protected]30335fdf02014-02-26 19:51:277#include "base/command_line.h"
[email protected]10a9bf92013-11-13 23:34:488#include "base/metrics/field_trial.h"
[email protected]10a9bf92013-11-13 23:34:489#include "chrome/browser/search_engines/template_url_service_factory.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/tabs/tab_strip_model.h"
12#include "chrome/test/base/browser_with_test_window_test.h"
13#include "chrome/test/base/ui_test_utils.h"
timvolodinea47291a2015-01-23 14:04:4314#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]bf5c532d2014-07-05 00:29:5315#include "components/search_engines/template_url_service.h"
[email protected]10a9bf92013-11-13 23:34:4816#include "components/variations/entropy_provider.h"
17#include "content/public/browser/navigation_controller.h"
18#include "content/public/browser/navigation_entry.h"
19#include "content/public/browser/web_contents.h"
[email protected]30335fdf02014-02-26 19:51:2720#include "content/public/common/content_switches.h"
[email protected]3d831992013-07-04 01:13:2921#include "testing/gtest/include/gtest/gtest.h"
[email protected]5f5ef802013-07-04 16:11:1322#include "url/gurl.h"
[email protected]3d831992013-07-04 01:13:2923
24namespace chrome {
25
mlamourieb40d552015-02-05 00:57:0826using ChromeContentBrowserClientTest = testing::Test;
[email protected]3d831992013-07-04 01:13:2927
[email protected]3d831992013-07-04 01:13:2928TEST_F(ChromeContentBrowserClientTest, ShouldAssignSiteForURL) {
29 ChromeContentBrowserClient client;
30 EXPECT_FALSE(client.ShouldAssignSiteForURL(GURL("chrome-native://test")));
31 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("http://www.google.com")));
32 EXPECT_TRUE(client.ShouldAssignSiteForURL(GURL("https://www.google.com")));
33}
34
mlamourieb40d552015-02-05 00:57:0835using ChromeContentBrowserClientWindowTest = BrowserWithTestWindowTest;
36
37// BrowserWithTestWindowTest doesn't work on iOS and Android.
38#if !defined(OS_ANDROID) && !defined(OS_IOS)
39
40// This test opens two URLs using ContentBrowserClient::OpenURL. It expects the
41// URLs to be opened in new tabs and activated, changing the active tabs after
42// each call and increasing the tab count by 2.
43TEST_F(ChromeContentBrowserClientWindowTest, OpenURL) {
44 ChromeContentBrowserClient client;
45
46 int previous_count = browser()->tab_strip_model()->count();
47
48 GURL urls[] = { GURL("https://www.google.com"),
49 GURL("https://www.chromium.org") };
50
51 for (const GURL& url : urls) {
52 content::OpenURLParams params(url,
53 content::Referrer(),
54 NEW_FOREGROUND_TAB,
55 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
56 false);
57 content::WebContents* contents =
58 client.OpenURL(browser()->profile(), params);
59 EXPECT_TRUE(contents);
60
61 content::WebContents* active_contents = browser()->tab_strip_model()->
62 GetActiveWebContents();
63 EXPECT_EQ(contents, active_contents);
64 EXPECT_EQ(url, active_contents->GetVisibleURL());
65 }
66
67 EXPECT_EQ(previous_count + 2, browser()->tab_strip_model()->count());
68}
69
70#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
71
brettwa68ea2b2015-02-01 02:54:0772#if defined(ENABLE_WEBRTC)
73
[email protected]30335fdf02014-02-26 19:51:2774// NOTE: Any updates to the expectations in these tests should also be done in
75// the browser test WebRtcDisableEncryptionFlagBrowserTest.
76class DisableWebRtcEncryptionFlagTest : public testing::Test {
77 public:
78 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:1779 : from_command_line_(base::CommandLine::NO_PROGRAM),
80 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:2781
82 protected:
dchenge1bc7982014-10-30 00:32:4083 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:2784 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
85 }
86
87 void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel) {
88 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
89 &to_command_line_,
90 from_command_line_,
91 channel);
92 }
93
avi3ef9ec9e2014-12-22 22:50:1794 base::CommandLine from_command_line_;
95 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:2796
97 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
98};
99
100TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
101 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN);
102 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
103}
104
105TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
106 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY);
107 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
108}
109
110TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
111 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV);
112 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
113}
114
115TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
116 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA);
117#if defined(OS_ANDROID)
118 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
119#else
120 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
121#endif
122}
123
124TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
125 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE);
126 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
127}
128
brettwa68ea2b2015-02-01 02:54:07129#endif // ENABLE_WEBRTC
130
[email protected]3d831992013-07-04 01:13:29131} // namespace chrome
[email protected]10a9bf92013-11-13 23:34:48132
133#if !defined(OS_IOS) && !defined(OS_ANDROID)
134namespace content {
135
136class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
137 protected:
dchenge1bc7982014-10-30 00:32:40138 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48139 BrowserWithTestWindowTest::SetUp();
140 field_trial_list_.reset(new base::FieldTrialList(
141 new metrics::SHA1EntropyProvider("42")));
142 }
143
144 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
145 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
146 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
147 TemplateURLService* template_url_service =
148 TemplateURLServiceFactory::GetForProfile(browser()->profile());
149 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
150
151 TemplateURLData data;
152 data.SetURL("http://foo.com/url?bar={searchTerms}");
153 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28154 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48155 // Takes ownership.
156 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26157 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48158 }
159
160 scoped_ptr<base::FieldTrialList> field_trial_list_;
161};
162
163TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
164 const GURL url_original("chrome://newtab");
165 const GURL url_rewritten("https://www.example.com/newtab");
166 InstallTemplateURLWithNewTabPage(url_rewritten);
167 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
168 "Group1 use_cacheable_ntp:1"));
169
170 AddTab(browser(), GURL("chrome://blank"));
171 NavigateAndCommitActiveTab(url_original);
172
173 NavigationEntry* entry = browser()->tab_strip_model()->
174 GetActiveWebContents()->GetController().GetLastCommittedEntry();
175 ASSERT_TRUE(entry != NULL);
176 EXPECT_EQ(url_rewritten, entry->GetURL());
177 EXPECT_EQ(url_original, entry->GetVirtualURL());
178}
179
180} // namespace content
181#endif // !defined(OS_IOS) && !defined(OS_ANDROID)
timvolodinea47291a2015-01-23 14:04:43182
183namespace chrome {
184
185// For testing permissions related functionality.
186class PermissionBrowserClientTest : public testing::Test {
187 public:
188 PermissionBrowserClientTest() : url_("https://www.google.com") {}
189
190 void CheckPermissionStatus(content::PermissionType type,
191 content::PermissionStatus expected) {
192 EXPECT_EQ(expected, client_.GetPermissionStatus(type, &profile_,
193 url_.GetOrigin(),
194 url_.GetOrigin()));
195 }
196
197 void SetPermission(ContentSettingsType type, ContentSetting value) {
198 profile_.GetHostContentSettingsMap()->SetContentSetting(
199 ContentSettingsPattern::FromURLNoWildcard(url_),
200 ContentSettingsPattern::FromURLNoWildcard(url_),
201 type, std::string(), value);
202 }
203
204 private:
205 content::TestBrowserThreadBundle thread_bundle_;
206 ChromeContentBrowserClient client_;
207 TestingProfile profile_;
208 GURL url_;
209};
210
211TEST_F(PermissionBrowserClientTest, GetPermissionStatusDefault) {
212 using namespace content;
213 CheckPermissionStatus(PERMISSION_MIDI_SYSEX, PERMISSION_STATUS_ASK);
214 CheckPermissionStatus(PERMISSION_PUSH_MESSAGING, PERMISSION_STATUS_ASK);
215 CheckPermissionStatus(PERMISSION_NOTIFICATIONS, PERMISSION_STATUS_ASK);
216 CheckPermissionStatus(PERMISSION_GEOLOCATION, PERMISSION_STATUS_ASK);
217#if defined(OS_ANDROID)
xhwang8624637f2015-01-28 21:09:59218 CheckPermissionStatus(PERMISSION_PROTECTED_MEDIA_IDENTIFIER,
219 PERMISSION_STATUS_ASK);
timvolodinea47291a2015-01-23 14:04:43220#endif
221}
222
223TEST_F(PermissionBrowserClientTest, GetPermissionStatusAfterSet) {
224 using namespace content;
225 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW);
226 CheckPermissionStatus(PERMISSION_GEOLOCATION, PERMISSION_STATUS_GRANTED);
227
228 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
229 CheckPermissionStatus(PERMISSION_NOTIFICATIONS, PERMISSION_STATUS_GRANTED);
230
231 SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, CONTENT_SETTING_ALLOW);
232 CheckPermissionStatus(PERMISSION_MIDI_SYSEX, PERMISSION_STATUS_GRANTED);
233
234 SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW);
235 CheckPermissionStatus(PERMISSION_PUSH_MESSAGING, PERMISSION_STATUS_GRANTED);
236
237#if defined(OS_ANDROID)
238 SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER,
239 CONTENT_SETTING_ALLOW);
xhwang8624637f2015-01-28 21:09:59240 CheckPermissionStatus(PERMISSION_PROTECTED_MEDIA_IDENTIFIER,
241 PERMISSION_STATUS_GRANTED);
timvolodinea47291a2015-01-23 14:04:43242#endif
243}
244
245} // namespace chrome