1 // Copyright 2015 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/permissions/permission_manager.h"
7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
8 #include "chrome/browser/permissions/permission_manager_factory.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/content_settings/core/browser/host_content_settings_map.h"
11 #include "content/public/browser/permission_type.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using content::PermissionType
;
16 using content::PermissionStatus
;
20 class PermissionManagerTestingProfile final
: public TestingProfile
{
22 PermissionManagerTestingProfile() {}
23 ~PermissionManagerTestingProfile() override
{}
25 PermissionManager
* GetPermissionManager() override
{
26 return PermissionManagerFactory::GetForProfile(this);
29 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile
);
32 } // anonymous namespace
34 class PermissionManagerTest
: public testing::Test
{
36 void OnPermissionChange(PermissionStatus permission
) {
37 callback_called_
= true;
38 callback_result_
= permission
;
42 PermissionManagerTest()
43 : url_("https://example.com"),
44 other_url_("https://foo.com"),
45 callback_called_(false),
46 callback_result_(content::PERMISSION_STATUS_ASK
) {
49 PermissionManager
* GetPermissionManager() {
50 return profile_
.GetPermissionManager();
53 HostContentSettingsMap
* GetHostContentSettingsMap() {
54 return HostContentSettingsMapFactory::GetForProfile(&profile_
);
57 void CheckPermissionStatus(PermissionType type
,
58 PermissionStatus expected
) {
59 EXPECT_EQ(expected
, GetPermissionManager()->GetPermissionStatus(
60 type
, url_
.GetOrigin(), url_
.GetOrigin()));
63 void SetPermission(ContentSettingsType type
, ContentSetting value
) {
64 HostContentSettingsMapFactory::GetForProfile(&profile_
)->SetContentSetting(
65 ContentSettingsPattern::FromURLNoWildcard(url_
),
66 ContentSettingsPattern::FromURLNoWildcard(url_
),
67 type
, std::string(), value
);
70 const GURL
& url() const {
74 const GURL
& other_url() const {
78 bool callback_called() const {
79 return callback_called_
;
82 PermissionStatus
callback_result() const { return callback_result_
; }
85 callback_called_
= false;
86 callback_result_
= content::PERMISSION_STATUS_ASK
;
91 const GURL other_url_
;
92 bool callback_called_
;
93 PermissionStatus callback_result_
;
94 content::TestBrowserThreadBundle thread_bundle_
;
95 PermissionManagerTestingProfile profile_
;
98 TEST_F(PermissionManagerTest
, GetPermissionStatusDefault
) {
99 CheckPermissionStatus(PermissionType::MIDI_SYSEX
,
100 content::PERMISSION_STATUS_ASK
);
101 CheckPermissionStatus(PermissionType::PUSH_MESSAGING
,
102 content::PERMISSION_STATUS_ASK
);
103 CheckPermissionStatus(PermissionType::NOTIFICATIONS
,
104 content::PERMISSION_STATUS_ASK
);
105 CheckPermissionStatus(PermissionType::GEOLOCATION
,
106 content::PERMISSION_STATUS_ASK
);
107 #if defined(OS_ANDROID)
108 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER
,
109 content::PERMISSION_STATUS_ASK
);
113 TEST_F(PermissionManagerTest
, GetPermissionStatusAfterSet
) {
114 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION
, CONTENT_SETTING_ALLOW
);
115 CheckPermissionStatus(PermissionType::GEOLOCATION
,
116 content::PERMISSION_STATUS_GRANTED
);
118 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, CONTENT_SETTING_ALLOW
);
119 CheckPermissionStatus(PermissionType::NOTIFICATIONS
,
120 content::PERMISSION_STATUS_GRANTED
);
122 SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX
, CONTENT_SETTING_ALLOW
);
123 CheckPermissionStatus(PermissionType::MIDI_SYSEX
,
124 content::PERMISSION_STATUS_GRANTED
);
126 SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
, CONTENT_SETTING_ALLOW
);
127 CheckPermissionStatus(PermissionType::PUSH_MESSAGING
,
128 content::PERMISSION_STATUS_GRANTED
);
130 #if defined(OS_ANDROID)
131 SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
,
132 CONTENT_SETTING_ALLOW
);
133 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER
,
134 content::PERMISSION_STATUS_GRANTED
);
138 TEST_F(PermissionManagerTest
, SameTypeChangeNotifies
) {
139 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
140 PermissionType::GEOLOCATION
, url(), url(),
141 base::Bind(&PermissionManagerTest::OnPermissionChange
,
142 base::Unretained(this)));
144 GetHostContentSettingsMap()->SetContentSetting(
145 ContentSettingsPattern::FromURLNoWildcard(url()),
146 ContentSettingsPattern::FromURLNoWildcard(url()),
147 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
149 CONTENT_SETTING_ALLOW
);
151 EXPECT_TRUE(callback_called());
152 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
154 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
157 TEST_F(PermissionManagerTest
, DifferentTypeChangeDoesNotNotify
) {
158 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
159 PermissionType::GEOLOCATION
, url(), url(),
160 base::Bind(&PermissionManagerTest::OnPermissionChange
,
161 base::Unretained(this)));
163 GetHostContentSettingsMap()->SetContentSetting(
164 ContentSettingsPattern::FromURLNoWildcard(url()),
165 ContentSettingsPattern::FromURLNoWildcard(url()),
166 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
168 CONTENT_SETTING_ALLOW
);
170 EXPECT_FALSE(callback_called());
172 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
175 TEST_F(PermissionManagerTest
, ChangeAfterUnsubscribeDoesNotNotify
) {
176 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
177 PermissionType::GEOLOCATION
, url(), url(),
178 base::Bind(&PermissionManagerTest::OnPermissionChange
,
179 base::Unretained(this)));
181 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
183 GetHostContentSettingsMap()->SetContentSetting(
184 ContentSettingsPattern::FromURLNoWildcard(url()),
185 ContentSettingsPattern::FromURLNoWildcard(url()),
186 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
188 CONTENT_SETTING_ALLOW
);
190 EXPECT_FALSE(callback_called());
193 TEST_F(PermissionManagerTest
, DifferentPrimaryPatternDoesNotNotify
) {
194 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
195 PermissionType::GEOLOCATION
, url(), url(),
196 base::Bind(&PermissionManagerTest::OnPermissionChange
,
197 base::Unretained(this)));
199 GetHostContentSettingsMap()->SetContentSetting(
200 ContentSettingsPattern::FromURLNoWildcard(other_url()),
201 ContentSettingsPattern::FromURLNoWildcard(url()),
202 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
204 CONTENT_SETTING_ALLOW
);
206 EXPECT_FALSE(callback_called());
208 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
211 TEST_F(PermissionManagerTest
, DifferentSecondaryPatternDoesNotNotify
) {
212 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
213 PermissionType::GEOLOCATION
, url(), url(),
214 base::Bind(&PermissionManagerTest::OnPermissionChange
,
215 base::Unretained(this)));
217 GetHostContentSettingsMap()->SetContentSetting(
218 ContentSettingsPattern::FromURLNoWildcard(url()),
219 ContentSettingsPattern::FromURLNoWildcard(other_url()),
220 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
222 CONTENT_SETTING_ALLOW
);
224 EXPECT_FALSE(callback_called());
226 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
229 TEST_F(PermissionManagerTest
, WildCardPatternNotifies
) {
230 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
231 PermissionType::GEOLOCATION
, url(), url(),
232 base::Bind(&PermissionManagerTest::OnPermissionChange
,
233 base::Unretained(this)));
235 GetHostContentSettingsMap()->SetContentSetting(
236 ContentSettingsPattern::Wildcard(),
237 ContentSettingsPattern::Wildcard(),
238 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
240 CONTENT_SETTING_ALLOW
);
242 EXPECT_TRUE(callback_called());
243 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
245 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
248 TEST_F(PermissionManagerTest
, ClearSettingsNotifies
) {
249 GetHostContentSettingsMap()->SetContentSetting(
250 ContentSettingsPattern::FromURLNoWildcard(url()),
251 ContentSettingsPattern::FromURLNoWildcard(url()),
252 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
254 CONTENT_SETTING_ALLOW
);
256 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
257 PermissionType::GEOLOCATION
, url(), url(),
258 base::Bind(&PermissionManagerTest::OnPermissionChange
,
259 base::Unretained(this)));
261 GetHostContentSettingsMap()->ClearSettingsForOneType(
262 CONTENT_SETTINGS_TYPE_GEOLOCATION
);
264 EXPECT_TRUE(callback_called());
265 EXPECT_EQ(content::PERMISSION_STATUS_ASK
, callback_result());
267 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
270 TEST_F(PermissionManagerTest
, NewValueCorrectlyPassed
) {
271 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
272 PermissionType::GEOLOCATION
, url(), url(),
273 base::Bind(&PermissionManagerTest::OnPermissionChange
,
274 base::Unretained(this)));
276 GetHostContentSettingsMap()->SetContentSetting(
277 ContentSettingsPattern::FromURLNoWildcard(url()),
278 ContentSettingsPattern::FromURLNoWildcard(url()),
279 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
281 CONTENT_SETTING_BLOCK
);
283 EXPECT_TRUE(callback_called());
284 EXPECT_EQ(content::PERMISSION_STATUS_DENIED
, callback_result());
286 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
289 TEST_F(PermissionManagerTest
, ChangeWithoutPermissionChangeDoesNotNotify
) {
290 GetHostContentSettingsMap()->SetContentSetting(
291 ContentSettingsPattern::FromURLNoWildcard(url()),
292 ContentSettingsPattern::FromURLNoWildcard(url()),
293 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
295 CONTENT_SETTING_ALLOW
);
297 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
298 PermissionType::GEOLOCATION
, url(), url(),
299 base::Bind(&PermissionManagerTest::OnPermissionChange
,
300 base::Unretained(this)));
302 GetHostContentSettingsMap()->SetContentSetting(
303 ContentSettingsPattern::Wildcard(),
304 ContentSettingsPattern::Wildcard(),
305 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
307 CONTENT_SETTING_ALLOW
);
309 EXPECT_FALSE(callback_called());
311 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
314 TEST_F(PermissionManagerTest
, ChangesBackAndForth
) {
315 GetHostContentSettingsMap()->SetContentSetting(
316 ContentSettingsPattern::FromURLNoWildcard(url()),
317 ContentSettingsPattern::FromURLNoWildcard(url()),
318 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
320 CONTENT_SETTING_ASK
);
322 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
323 PermissionType::GEOLOCATION
, url(), url(),
324 base::Bind(&PermissionManagerTest::OnPermissionChange
,
325 base::Unretained(this)));
327 GetHostContentSettingsMap()->SetContentSetting(
328 ContentSettingsPattern::FromURLNoWildcard(url()),
329 ContentSettingsPattern::FromURLNoWildcard(url()),
330 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
332 CONTENT_SETTING_ALLOW
);
334 EXPECT_TRUE(callback_called());
335 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
339 GetHostContentSettingsMap()->SetContentSetting(
340 ContentSettingsPattern::FromURLNoWildcard(url()),
341 ContentSettingsPattern::FromURLNoWildcard(url()),
342 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
344 CONTENT_SETTING_ASK
);
346 EXPECT_TRUE(callback_called());
347 EXPECT_EQ(content::PERMISSION_STATUS_ASK
, callback_result());
349 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);