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/permissions/permission_manager_factory.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "components/content_settings/core/browser/host_content_settings_map.h"
10 #include "content/public/browser/permission_type.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using content::PermissionType
;
15 using content::PermissionStatus
;
19 class PermissionManagerTestingProfile final
: public TestingProfile
{
21 PermissionManagerTestingProfile() {}
22 ~PermissionManagerTestingProfile() override
{}
24 PermissionManager
* GetPermissionManager() override
{
25 return PermissionManagerFactory::GetForProfile(this);
28 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile
);
31 } // anonymous namespace
33 class PermissionManagerTest
: public testing::Test
{
35 void OnPermissionChange(content::PermissionStatus permission
) {
36 callback_called_
= true;
37 callback_result_
= permission
;
41 PermissionManagerTest()
42 : url_("https://example.com"),
43 other_url_("https://foo.com"),
44 callback_called_(false),
45 callback_result_(content::PERMISSION_STATUS_ASK
) {
48 PermissionManager
* GetPermissionManager() {
49 return profile_
.GetPermissionManager();
52 HostContentSettingsMap
* GetHostContentSettingsMap() {
53 return profile_
.GetHostContentSettingsMap();
56 void CheckPermissionStatus(PermissionType type
,
57 PermissionStatus expected
) {
58 EXPECT_EQ(expected
, GetPermissionManager()->GetPermissionStatus(
59 type
, url_
.GetOrigin(), url_
.GetOrigin()));
62 void SetPermission(ContentSettingsType type
, ContentSetting value
) {
63 profile_
.GetHostContentSettingsMap()->SetContentSetting(
64 ContentSettingsPattern::FromURLNoWildcard(url_
),
65 ContentSettingsPattern::FromURLNoWildcard(url_
),
66 type
, std::string(), value
);
69 const GURL
& url() const {
73 const GURL
& other_url() const {
77 bool callback_called() const {
78 return callback_called_
;
81 content::PermissionStatus
callback_result() const {
82 return callback_result_
;
86 callback_called_
= false;
87 callback_result_
= content::PERMISSION_STATUS_ASK
;
92 const GURL other_url_
;
93 bool callback_called_
;
94 content::PermissionStatus callback_result_
;
95 content::TestBrowserThreadBundle thread_bundle_
;
96 PermissionManagerTestingProfile profile_
;
99 TEST_F(PermissionManagerTest
, GetPermissionStatusDefault
) {
100 CheckPermissionStatus(PermissionType::MIDI_SYSEX
,
101 content::PERMISSION_STATUS_ASK
);
102 CheckPermissionStatus(PermissionType::PUSH_MESSAGING
,
103 content::PERMISSION_STATUS_ASK
);
104 CheckPermissionStatus(PermissionType::NOTIFICATIONS
,
105 content::PERMISSION_STATUS_ASK
);
106 CheckPermissionStatus(PermissionType::GEOLOCATION
,
107 content::PERMISSION_STATUS_ASK
);
108 #if defined(OS_ANDROID)
109 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER
,
110 content::PERMISSION_STATUS_ASK
);
114 TEST_F(PermissionManagerTest
, GetPermissionStatusAfterSet
) {
115 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION
, CONTENT_SETTING_ALLOW
);
116 CheckPermissionStatus(PermissionType::GEOLOCATION
,
117 content::PERMISSION_STATUS_GRANTED
);
119 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, CONTENT_SETTING_ALLOW
);
120 CheckPermissionStatus(PermissionType::NOTIFICATIONS
,
121 content::PERMISSION_STATUS_GRANTED
);
123 SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX
, CONTENT_SETTING_ALLOW
);
124 CheckPermissionStatus(PermissionType::MIDI_SYSEX
,
125 content::PERMISSION_STATUS_GRANTED
);
127 SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
, CONTENT_SETTING_ALLOW
);
128 CheckPermissionStatus(PermissionType::PUSH_MESSAGING
,
129 content::PERMISSION_STATUS_GRANTED
);
131 #if defined(OS_ANDROID)
132 SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
,
133 CONTENT_SETTING_ALLOW
);
134 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER
,
135 content::PERMISSION_STATUS_GRANTED
);
139 TEST_F(PermissionManagerTest
, SameTypeChangeNotifies
) {
140 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
141 PermissionType::GEOLOCATION
, url(), url(),
142 base::Bind(&PermissionManagerTest::OnPermissionChange
,
143 base::Unretained(this)));
145 GetHostContentSettingsMap()->SetContentSetting(
146 ContentSettingsPattern::FromURLNoWildcard(url()),
147 ContentSettingsPattern::FromURLNoWildcard(url()),
148 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
150 CONTENT_SETTING_ALLOW
);
152 EXPECT_TRUE(callback_called());
153 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
155 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
158 TEST_F(PermissionManagerTest
, DifferentTypeChangeDoesNotNotify
) {
159 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
160 PermissionType::GEOLOCATION
, url(), url(),
161 base::Bind(&PermissionManagerTest::OnPermissionChange
,
162 base::Unretained(this)));
164 GetHostContentSettingsMap()->SetContentSetting(
165 ContentSettingsPattern::FromURLNoWildcard(url()),
166 ContentSettingsPattern::FromURLNoWildcard(url()),
167 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
169 CONTENT_SETTING_ALLOW
);
171 EXPECT_FALSE(callback_called());
173 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
176 TEST_F(PermissionManagerTest
, ChangeAfterUnsubscribeDoesNotNotify
) {
177 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
178 PermissionType::GEOLOCATION
, url(), url(),
179 base::Bind(&PermissionManagerTest::OnPermissionChange
,
180 base::Unretained(this)));
182 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
184 GetHostContentSettingsMap()->SetContentSetting(
185 ContentSettingsPattern::FromURLNoWildcard(url()),
186 ContentSettingsPattern::FromURLNoWildcard(url()),
187 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
189 CONTENT_SETTING_ALLOW
);
191 EXPECT_FALSE(callback_called());
194 TEST_F(PermissionManagerTest
, DifferentPrimaryPatternDoesNotNotify
) {
195 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
196 PermissionType::GEOLOCATION
, url(), url(),
197 base::Bind(&PermissionManagerTest::OnPermissionChange
,
198 base::Unretained(this)));
200 GetHostContentSettingsMap()->SetContentSetting(
201 ContentSettingsPattern::FromURLNoWildcard(other_url()),
202 ContentSettingsPattern::FromURLNoWildcard(url()),
203 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
205 CONTENT_SETTING_ALLOW
);
207 EXPECT_FALSE(callback_called());
209 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
212 TEST_F(PermissionManagerTest
, DifferentSecondaryPatternDoesNotNotify
) {
213 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
214 PermissionType::GEOLOCATION
, url(), url(),
215 base::Bind(&PermissionManagerTest::OnPermissionChange
,
216 base::Unretained(this)));
218 GetHostContentSettingsMap()->SetContentSetting(
219 ContentSettingsPattern::FromURLNoWildcard(url()),
220 ContentSettingsPattern::FromURLNoWildcard(other_url()),
221 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
223 CONTENT_SETTING_ALLOW
);
225 EXPECT_FALSE(callback_called());
227 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
230 TEST_F(PermissionManagerTest
, WildCardPatternNotifies
) {
231 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
232 PermissionType::GEOLOCATION
, url(), url(),
233 base::Bind(&PermissionManagerTest::OnPermissionChange
,
234 base::Unretained(this)));
236 GetHostContentSettingsMap()->SetContentSetting(
237 ContentSettingsPattern::Wildcard(),
238 ContentSettingsPattern::Wildcard(),
239 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
241 CONTENT_SETTING_ALLOW
);
243 EXPECT_TRUE(callback_called());
244 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
246 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
249 TEST_F(PermissionManagerTest
, ClearSettingsNotifies
) {
250 GetHostContentSettingsMap()->SetContentSetting(
251 ContentSettingsPattern::FromURLNoWildcard(url()),
252 ContentSettingsPattern::FromURLNoWildcard(url()),
253 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
255 CONTENT_SETTING_ALLOW
);
257 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
258 PermissionType::GEOLOCATION
, url(), url(),
259 base::Bind(&PermissionManagerTest::OnPermissionChange
,
260 base::Unretained(this)));
262 GetHostContentSettingsMap()->ClearSettingsForOneType(
263 CONTENT_SETTINGS_TYPE_GEOLOCATION
);
265 EXPECT_TRUE(callback_called());
266 EXPECT_EQ(content::PERMISSION_STATUS_ASK
, callback_result());
268 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
271 TEST_F(PermissionManagerTest
, NewValueCorrectlyPassed
) {
272 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
273 PermissionType::GEOLOCATION
, url(), url(),
274 base::Bind(&PermissionManagerTest::OnPermissionChange
,
275 base::Unretained(this)));
277 GetHostContentSettingsMap()->SetContentSetting(
278 ContentSettingsPattern::FromURLNoWildcard(url()),
279 ContentSettingsPattern::FromURLNoWildcard(url()),
280 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
282 CONTENT_SETTING_BLOCK
);
284 EXPECT_TRUE(callback_called());
285 EXPECT_EQ(content::PERMISSION_STATUS_DENIED
, callback_result());
287 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
290 TEST_F(PermissionManagerTest
, ChangeWithoutPermissionChangeDoesNotNotify
) {
291 GetHostContentSettingsMap()->SetContentSetting(
292 ContentSettingsPattern::FromURLNoWildcard(url()),
293 ContentSettingsPattern::FromURLNoWildcard(url()),
294 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
296 CONTENT_SETTING_ALLOW
);
298 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
299 PermissionType::GEOLOCATION
, url(), url(),
300 base::Bind(&PermissionManagerTest::OnPermissionChange
,
301 base::Unretained(this)));
303 GetHostContentSettingsMap()->SetContentSetting(
304 ContentSettingsPattern::Wildcard(),
305 ContentSettingsPattern::Wildcard(),
306 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
308 CONTENT_SETTING_ALLOW
);
310 EXPECT_FALSE(callback_called());
312 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
315 TEST_F(PermissionManagerTest
, ChangesBackAndForth
) {
316 GetHostContentSettingsMap()->SetContentSetting(
317 ContentSettingsPattern::FromURLNoWildcard(url()),
318 ContentSettingsPattern::FromURLNoWildcard(url()),
319 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
321 CONTENT_SETTING_ASK
);
323 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
324 PermissionType::GEOLOCATION
, url(), url(),
325 base::Bind(&PermissionManagerTest::OnPermissionChange
,
326 base::Unretained(this)));
328 GetHostContentSettingsMap()->SetContentSetting(
329 ContentSettingsPattern::FromURLNoWildcard(url()),
330 ContentSettingsPattern::FromURLNoWildcard(url()),
331 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
333 CONTENT_SETTING_ALLOW
);
335 EXPECT_TRUE(callback_called());
336 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
340 GetHostContentSettingsMap()->SetContentSetting(
341 ContentSettingsPattern::FromURLNoWildcard(url()),
342 ContentSettingsPattern::FromURLNoWildcard(url()),
343 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
345 CONTENT_SETTING_ASK
);
347 EXPECT_TRUE(callback_called());
348 EXPECT_EQ(content::PERMISSION_STATUS_ASK
, callback_result());
350 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);