Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / permissions / permission_manager_unittest.cc
blob68c9d87ace8a4a65ce5226552325bf1a0ae0bbf6
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;
17 namespace {
19 class PermissionManagerTestingProfile final : public TestingProfile {
20 public:
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 {
34 public:
35 void OnPermissionChange(content::PermissionStatus permission) {
36 callback_called_ = true;
37 callback_result_ = permission;
40 protected:
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 {
70 return url_;
73 const GURL& other_url() const {
74 return other_url_;
77 bool callback_called() const {
78 return callback_called_;
81 content::PermissionStatus callback_result() const {
82 return callback_result_;
85 void Reset() {
86 callback_called_ = false;
87 callback_result_ = content::PERMISSION_STATUS_ASK;
90 private:
91 const GURL url_;
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);
111 #endif
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);
136 #endif
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,
149 std::string(),
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,
168 std::string(),
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,
188 std::string(),
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,
204 std::string(),
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,
222 std::string(),
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,
240 std::string(),
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,
254 std::string(),
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,
281 std::string(),
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,
295 std::string(),
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,
307 std::string(),
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,
320 std::string(),
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,
332 std::string(),
333 CONTENT_SETTING_ALLOW);
335 EXPECT_TRUE(callback_called());
336 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED, callback_result());
338 Reset();
340 GetHostContentSettingsMap()->SetContentSetting(
341 ContentSettingsPattern::FromURLNoWildcard(url()),
342 ContentSettingsPattern::FromURLNoWildcard(url()),
343 CONTENT_SETTINGS_TYPE_GEOLOCATION,
344 std::string(),
345 CONTENT_SETTING_ASK);
347 EXPECT_TRUE(callback_called());
348 EXPECT_EQ(content::PERMISSION_STATUS_ASK, callback_result());
350 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id);