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(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 PermissionStatus
callback_result() const { return callback_result_
; }
84 callback_called_
= false;
85 callback_result_
= content::PERMISSION_STATUS_ASK
;
90 const GURL other_url_
;
91 bool callback_called_
;
92 PermissionStatus callback_result_
;
93 content::TestBrowserThreadBundle thread_bundle_
;
94 PermissionManagerTestingProfile profile_
;
97 TEST_F(PermissionManagerTest
, GetPermissionStatusDefault
) {
98 CheckPermissionStatus(PermissionType::MIDI_SYSEX
,
99 content::PERMISSION_STATUS_ASK
);
100 CheckPermissionStatus(PermissionType::PUSH_MESSAGING
,
101 content::PERMISSION_STATUS_ASK
);
102 CheckPermissionStatus(PermissionType::NOTIFICATIONS
,
103 content::PERMISSION_STATUS_ASK
);
104 CheckPermissionStatus(PermissionType::GEOLOCATION
,
105 content::PERMISSION_STATUS_ASK
);
106 #if defined(OS_ANDROID)
107 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER
,
108 content::PERMISSION_STATUS_ASK
);
112 TEST_F(PermissionManagerTest
, GetPermissionStatusAfterSet
) {
113 SetPermission(CONTENT_SETTINGS_TYPE_GEOLOCATION
, CONTENT_SETTING_ALLOW
);
114 CheckPermissionStatus(PermissionType::GEOLOCATION
,
115 content::PERMISSION_STATUS_GRANTED
);
117 SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, CONTENT_SETTING_ALLOW
);
118 CheckPermissionStatus(PermissionType::NOTIFICATIONS
,
119 content::PERMISSION_STATUS_GRANTED
);
121 SetPermission(CONTENT_SETTINGS_TYPE_MIDI_SYSEX
, CONTENT_SETTING_ALLOW
);
122 CheckPermissionStatus(PermissionType::MIDI_SYSEX
,
123 content::PERMISSION_STATUS_GRANTED
);
125 SetPermission(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
, CONTENT_SETTING_ALLOW
);
126 CheckPermissionStatus(PermissionType::PUSH_MESSAGING
,
127 content::PERMISSION_STATUS_GRANTED
);
129 #if defined(OS_ANDROID)
130 SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
,
131 CONTENT_SETTING_ALLOW
);
132 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER
,
133 content::PERMISSION_STATUS_GRANTED
);
137 TEST_F(PermissionManagerTest
, SameTypeChangeNotifies
) {
138 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
139 PermissionType::GEOLOCATION
, url(), url(),
140 base::Bind(&PermissionManagerTest::OnPermissionChange
,
141 base::Unretained(this)));
143 GetHostContentSettingsMap()->SetContentSetting(
144 ContentSettingsPattern::FromURLNoWildcard(url()),
145 ContentSettingsPattern::FromURLNoWildcard(url()),
146 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
148 CONTENT_SETTING_ALLOW
);
150 EXPECT_TRUE(callback_called());
151 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
153 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
156 TEST_F(PermissionManagerTest
, DifferentTypeChangeDoesNotNotify
) {
157 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
158 PermissionType::GEOLOCATION
, url(), url(),
159 base::Bind(&PermissionManagerTest::OnPermissionChange
,
160 base::Unretained(this)));
162 GetHostContentSettingsMap()->SetContentSetting(
163 ContentSettingsPattern::FromURLNoWildcard(url()),
164 ContentSettingsPattern::FromURLNoWildcard(url()),
165 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
167 CONTENT_SETTING_ALLOW
);
169 EXPECT_FALSE(callback_called());
171 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
174 TEST_F(PermissionManagerTest
, ChangeAfterUnsubscribeDoesNotNotify
) {
175 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
176 PermissionType::GEOLOCATION
, url(), url(),
177 base::Bind(&PermissionManagerTest::OnPermissionChange
,
178 base::Unretained(this)));
180 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
182 GetHostContentSettingsMap()->SetContentSetting(
183 ContentSettingsPattern::FromURLNoWildcard(url()),
184 ContentSettingsPattern::FromURLNoWildcard(url()),
185 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
187 CONTENT_SETTING_ALLOW
);
189 EXPECT_FALSE(callback_called());
192 TEST_F(PermissionManagerTest
, DifferentPrimaryPatternDoesNotNotify
) {
193 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
194 PermissionType::GEOLOCATION
, url(), url(),
195 base::Bind(&PermissionManagerTest::OnPermissionChange
,
196 base::Unretained(this)));
198 GetHostContentSettingsMap()->SetContentSetting(
199 ContentSettingsPattern::FromURLNoWildcard(other_url()),
200 ContentSettingsPattern::FromURLNoWildcard(url()),
201 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
203 CONTENT_SETTING_ALLOW
);
205 EXPECT_FALSE(callback_called());
207 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
210 TEST_F(PermissionManagerTest
, DifferentSecondaryPatternDoesNotNotify
) {
211 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
212 PermissionType::GEOLOCATION
, url(), url(),
213 base::Bind(&PermissionManagerTest::OnPermissionChange
,
214 base::Unretained(this)));
216 GetHostContentSettingsMap()->SetContentSetting(
217 ContentSettingsPattern::FromURLNoWildcard(url()),
218 ContentSettingsPattern::FromURLNoWildcard(other_url()),
219 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
221 CONTENT_SETTING_ALLOW
);
223 EXPECT_FALSE(callback_called());
225 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
228 TEST_F(PermissionManagerTest
, WildCardPatternNotifies
) {
229 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
230 PermissionType::GEOLOCATION
, url(), url(),
231 base::Bind(&PermissionManagerTest::OnPermissionChange
,
232 base::Unretained(this)));
234 GetHostContentSettingsMap()->SetContentSetting(
235 ContentSettingsPattern::Wildcard(),
236 ContentSettingsPattern::Wildcard(),
237 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
239 CONTENT_SETTING_ALLOW
);
241 EXPECT_TRUE(callback_called());
242 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
244 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
247 TEST_F(PermissionManagerTest
, ClearSettingsNotifies
) {
248 GetHostContentSettingsMap()->SetContentSetting(
249 ContentSettingsPattern::FromURLNoWildcard(url()),
250 ContentSettingsPattern::FromURLNoWildcard(url()),
251 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
253 CONTENT_SETTING_ALLOW
);
255 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
256 PermissionType::GEOLOCATION
, url(), url(),
257 base::Bind(&PermissionManagerTest::OnPermissionChange
,
258 base::Unretained(this)));
260 GetHostContentSettingsMap()->ClearSettingsForOneType(
261 CONTENT_SETTINGS_TYPE_GEOLOCATION
);
263 EXPECT_TRUE(callback_called());
264 EXPECT_EQ(content::PERMISSION_STATUS_ASK
, callback_result());
266 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
269 TEST_F(PermissionManagerTest
, NewValueCorrectlyPassed
) {
270 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
271 PermissionType::GEOLOCATION
, url(), url(),
272 base::Bind(&PermissionManagerTest::OnPermissionChange
,
273 base::Unretained(this)));
275 GetHostContentSettingsMap()->SetContentSetting(
276 ContentSettingsPattern::FromURLNoWildcard(url()),
277 ContentSettingsPattern::FromURLNoWildcard(url()),
278 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
280 CONTENT_SETTING_BLOCK
);
282 EXPECT_TRUE(callback_called());
283 EXPECT_EQ(content::PERMISSION_STATUS_DENIED
, callback_result());
285 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
288 TEST_F(PermissionManagerTest
, ChangeWithoutPermissionChangeDoesNotNotify
) {
289 GetHostContentSettingsMap()->SetContentSetting(
290 ContentSettingsPattern::FromURLNoWildcard(url()),
291 ContentSettingsPattern::FromURLNoWildcard(url()),
292 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
294 CONTENT_SETTING_ALLOW
);
296 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
297 PermissionType::GEOLOCATION
, url(), url(),
298 base::Bind(&PermissionManagerTest::OnPermissionChange
,
299 base::Unretained(this)));
301 GetHostContentSettingsMap()->SetContentSetting(
302 ContentSettingsPattern::Wildcard(),
303 ContentSettingsPattern::Wildcard(),
304 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
306 CONTENT_SETTING_ALLOW
);
308 EXPECT_FALSE(callback_called());
310 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);
313 TEST_F(PermissionManagerTest
, ChangesBackAndForth
) {
314 GetHostContentSettingsMap()->SetContentSetting(
315 ContentSettingsPattern::FromURLNoWildcard(url()),
316 ContentSettingsPattern::FromURLNoWildcard(url()),
317 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
319 CONTENT_SETTING_ASK
);
321 int subscription_id
= GetPermissionManager()->SubscribePermissionStatusChange(
322 PermissionType::GEOLOCATION
, url(), url(),
323 base::Bind(&PermissionManagerTest::OnPermissionChange
,
324 base::Unretained(this)));
326 GetHostContentSettingsMap()->SetContentSetting(
327 ContentSettingsPattern::FromURLNoWildcard(url()),
328 ContentSettingsPattern::FromURLNoWildcard(url()),
329 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
331 CONTENT_SETTING_ALLOW
);
333 EXPECT_TRUE(callback_called());
334 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED
, callback_result());
338 GetHostContentSettingsMap()->SetContentSetting(
339 ContentSettingsPattern::FromURLNoWildcard(url()),
340 ContentSettingsPattern::FromURLNoWildcard(url()),
341 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
343 CONTENT_SETTING_ASK
);
345 EXPECT_TRUE(callback_called());
346 EXPECT_EQ(content::PERMISSION_STATUS_ASK
, callback_result());
348 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id
);