blob: fd29ffc51bf212496fb428f65215f4d1d3606ed5 [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
26typedef testing::Test ChromeContentBrowserClientTest;
27
[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
brettwa68ea2b2015-02-01 02:54:0735#if defined(ENABLE_WEBRTC)
36
[email protected]30335fdf02014-02-26 19:51:2737// NOTE: Any updates to the expectations in these tests should also be done in
38// the browser test WebRtcDisableEncryptionFlagBrowserTest.
39class DisableWebRtcEncryptionFlagTest : public testing::Test {
40 public:
41 DisableWebRtcEncryptionFlagTest()
avi3ef9ec9e2014-12-22 22:50:1742 : from_command_line_(base::CommandLine::NO_PROGRAM),
43 to_command_line_(base::CommandLine::NO_PROGRAM) {}
[email protected]30335fdf02014-02-26 19:51:2744
45 protected:
dchenge1bc7982014-10-30 00:32:4046 void SetUp() override {
[email protected]30335fdf02014-02-26 19:51:2747 from_command_line_.AppendSwitch(switches::kDisableWebRtcEncryption);
48 }
49
50 void MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::Channel channel) {
51 ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
52 &to_command_line_,
53 from_command_line_,
54 channel);
55 }
56
avi3ef9ec9e2014-12-22 22:50:1757 base::CommandLine from_command_line_;
58 base::CommandLine to_command_line_;
[email protected]30335fdf02014-02-26 19:51:2759
60 DISALLOW_COPY_AND_ASSIGN(DisableWebRtcEncryptionFlagTest);
61};
62
63TEST_F(DisableWebRtcEncryptionFlagTest, UnknownChannel) {
64 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_UNKNOWN);
65 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
66}
67
68TEST_F(DisableWebRtcEncryptionFlagTest, CanaryChannel) {
69 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_CANARY);
70 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
71}
72
73TEST_F(DisableWebRtcEncryptionFlagTest, DevChannel) {
74 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_DEV);
75 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
76}
77
78TEST_F(DisableWebRtcEncryptionFlagTest, BetaChannel) {
79 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_BETA);
80#if defined(OS_ANDROID)
81 EXPECT_TRUE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
82#else
83 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
84#endif
85}
86
87TEST_F(DisableWebRtcEncryptionFlagTest, StableChannel) {
88 MaybeCopyDisableWebRtcEncryptionSwitch(VersionInfo::CHANNEL_STABLE);
89 EXPECT_FALSE(to_command_line_.HasSwitch(switches::kDisableWebRtcEncryption));
90}
91
brettwa68ea2b2015-02-01 02:54:0792#endif // ENABLE_WEBRTC
93
[email protected]3d831992013-07-04 01:13:2994} // namespace chrome
[email protected]10a9bf92013-11-13 23:34:4895
96#if !defined(OS_IOS) && !defined(OS_ANDROID)
97namespace content {
98
99class InstantNTPURLRewriteTest : public BrowserWithTestWindowTest {
100 protected:
dchenge1bc7982014-10-30 00:32:40101 void SetUp() override {
[email protected]10a9bf92013-11-13 23:34:48102 BrowserWithTestWindowTest::SetUp();
103 field_trial_list_.reset(new base::FieldTrialList(
104 new metrics::SHA1EntropyProvider("42")));
105 }
106
107 void InstallTemplateURLWithNewTabPage(GURL new_tab_page_url) {
108 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
109 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
110 TemplateURLService* template_url_service =
111 TemplateURLServiceFactory::GetForProfile(browser()->profile());
112 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
113
114 TemplateURLData data;
115 data.SetURL("http://foo.com/url?bar={searchTerms}");
116 data.new_tab_url = new_tab_page_url.spec();
[email protected]168d08722014-06-18 07:13:28117 TemplateURL* template_url = new TemplateURL(data);
[email protected]10a9bf92013-11-13 23:34:48118 // Takes ownership.
119 template_url_service->Add(template_url);
[email protected]f1cb5582014-04-25 07:35:26120 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
[email protected]10a9bf92013-11-13 23:34:48121 }
122
123 scoped_ptr<base::FieldTrialList> field_trial_list_;
124};
125
126TEST_F(InstantNTPURLRewriteTest, UberURLHandler_InstantExtendedNewTabPage) {
127 const GURL url_original("chrome://newtab");
128 const GURL url_rewritten("https://www.example.com/newtab");
129 InstallTemplateURLWithNewTabPage(url_rewritten);
130 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
131 "Group1 use_cacheable_ntp:1"));
132
133 AddTab(browser(), GURL("chrome://blank"));
134 NavigateAndCommitActiveTab(url_original);
135
136 NavigationEntry* entry = browser()->tab_strip_model()->
137 GetActiveWebContents()->GetController().GetLastCommittedEntry();
138 ASSERT_TRUE(entry != NULL);
139 EXPECT_EQ(url_rewritten, entry->GetURL());
140 EXPECT_EQ(url_original, entry->GetVirtualURL());
141}
142
143} // namespace content
144#endif // !defined(OS_IOS) && !defined(OS_ANDROID)
timvolodinea47291a2015-01-23 14:04:43145
146namespace chrome {
147
148// For testing permissions related functionality.
149class PermissionBrowserClientTest : public testing::Test {
150 public:
151 PermissionBrowserClientTest() : url_("https://www.google.com") {}
152
153 void CheckPermissionStatus(content::PermissionType type,
154 content::PermissionStatus expected) {
155 EXPECT_EQ(expected, client_.GetPermissionStatus(type, &profile_,
156 url_.GetOrigin(),
157 url_.GetOrigin()));
158 }
159
160 void SetPermission(ContentSettingsType type, ContentSetting value) {
161 profile_.GetHostContentSettingsMap()->SetContentSetting(
162 ContentSettingsPattern::FromURLNoWildcard(url_),
163 ContentSettingsPattern::FromURLNoWildcard(url_),
164 type, std::string(), value);
165 }
166
167 private:
168 content::TestBrowserThreadBundle thread_bundle_;
169 ChromeContentBrowserClient client_;
170 TestingProfile profile_;
171 GURL url_;
172};
173
174TEST_F(PermissionBrowserClientTest, GetPermissionStatusDefault) {
175 using namespace content;
176 CheckPermissionStatus(PERMISSION_MIDI_SYSEX, PERMISSION_STATUS_ASK);
177 CheckPermissionStatus(PERMISSION_PUSH_MESSAGING, PERMISSION_STATUS_ASK);
178 CheckPermissionStatus(PERMISSION_NOTIFICATIONS, PERMISSION_STATUS_ASK);
179 CheckPermissionStatus(PERMISSION_GEOLOCATION, PERMISSION_STATUS_ASK);
180#if defined(OS_ANDROID)
xhwang8624637f2015-01-28 21:09:59181 CheckPermissionStatus(PERMISSION_PROTECTED_MEDIA_IDENTIFIER,
182 PERMISSION_STATUS_ASK);
timvolodinea47291a2015-01-23 14:04:43183#endif
184}
185
186TEST_F(PermissionBrowserClientTest, GetPermissionStatusAfterSet) {
187 using namespace content;
188 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW);
189 CheckPermissionStatus(PERMISSION_GEOLOCATION, PERMISSION_STATUS_GRANTED);
190
191 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW);
192 CheckPermissionStatus(PERMISSION_NOTIFICATIONS, PERMISSION_STATUS_GRANTED);
193
194 SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, CONTENT_SETTING_ALLOW);
195 CheckPermissionStatus(PERMISSION_MIDI_SYSEX, PERMISSION_STATUS_GRANTED);
196
197 SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW);
198 CheckPermissionStatus(PERMISSION_PUSH_MESSAGING, PERMISSION_STATUS_GRANTED);
199
200#if defined(OS_ANDROID)
201 SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER,
202 CONTENT_SETTING_ALLOW);
xhwang8624637f2015-01-28 21:09:59203 CheckPermissionStatus(PERMISSION_PROTECTED_MEDIA_IDENTIFIER,
204 PERMISSION_STATUS_GRANTED);
timvolodinea47291a2015-01-23 14:04:43205#endif
206}
207
208} // namespace chrome