OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/permissions/permission_manager.h" | 5 #include "chrome/browser/permissions/permission_manager.h" |
6 | 6 |
7 #include "chrome/browser/permissions/permission_manager_factory.h" | 7 #include "chrome/browser/permissions/permission_manager_factory.h" |
8 #include "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.h" |
9 #include "components/content_settings/core/browser/host_content_settings_map.h" | 9 #include "components/content_settings/core/browser/host_content_settings_map.h" |
10 #include "content/public/browser/permission_type.h" | 10 #include "content/public/browser/permission_type.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 return PermissionManagerFactory::GetForProfile(this); | 25 return PermissionManagerFactory::GetForProfile(this); |
26 } | 26 } |
27 | 27 |
28 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); | 28 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); |
29 }; | 29 }; |
30 | 30 |
31 } // anonymous namespace | 31 } // anonymous namespace |
32 | 32 |
33 class PermissionManagerTest : public testing::Test { | 33 class PermissionManagerTest : public testing::Test { |
34 public: | 34 public: |
35 PermissionManagerTest() : url_("https://example.com") {} | 35 void OnPermissionChange(content::PermissionStatus permission) { |
| 36 callback_called_ = true; |
| 37 callback_result_ = permission; |
| 38 } |
| 39 |
| 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) { |
| 46 } |
| 47 |
| 48 PermissionManager* GetPermissionManager() { |
| 49 return profile_.GetPermissionManager(); |
| 50 } |
| 51 |
| 52 HostContentSettingsMap* GetHostContentSettingsMap() { |
| 53 return profile_.GetHostContentSettingsMap(); |
| 54 } |
36 | 55 |
37 void CheckPermissionStatus(PermissionType type, | 56 void CheckPermissionStatus(PermissionType type, |
38 PermissionStatus expected) { | 57 PermissionStatus expected) { |
39 EXPECT_EQ(expected, | 58 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus( |
40 profile_.GetPermissionManager()->GetPermissionStatus( | 59 type, url_.GetOrigin(), url_.GetOrigin())); |
41 type, url_.GetOrigin(), url_.GetOrigin())); | |
42 } | 60 } |
43 | 61 |
44 void SetPermission(ContentSettingsType type, ContentSetting value) { | 62 void SetPermission(ContentSettingsType type, ContentSetting value) { |
45 profile_.GetHostContentSettingsMap()->SetContentSetting( | 63 profile_.GetHostContentSettingsMap()->SetContentSetting( |
46 ContentSettingsPattern::FromURLNoWildcard(url_), | 64 ContentSettingsPattern::FromURLNoWildcard(url_), |
47 ContentSettingsPattern::FromURLNoWildcard(url_), | 65 ContentSettingsPattern::FromURLNoWildcard(url_), |
48 type, std::string(), value); | 66 type, std::string(), value); |
49 } | 67 } |
50 | 68 |
| 69 const GURL& url() const { |
| 70 return url_; |
| 71 } |
| 72 |
| 73 const GURL& other_url() const { |
| 74 return other_url_; |
| 75 } |
| 76 |
| 77 bool callback_called() const { |
| 78 return callback_called_; |
| 79 } |
| 80 |
| 81 content::PermissionStatus callback_result() const { |
| 82 return callback_result_; |
| 83 } |
| 84 |
| 85 void Reset() { |
| 86 callback_called_ = false; |
| 87 callback_result_ = content::PERMISSION_STATUS_ASK; |
| 88 } |
| 89 |
51 private: | 90 private: |
52 const GURL url_; | 91 const GURL url_; |
| 92 const GURL other_url_; |
| 93 bool callback_called_; |
| 94 content::PermissionStatus callback_result_; |
53 content::TestBrowserThreadBundle thread_bundle_; | 95 content::TestBrowserThreadBundle thread_bundle_; |
54 PermissionManagerTestingProfile profile_; | 96 PermissionManagerTestingProfile profile_; |
55 }; | 97 }; |
56 | 98 |
57 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { | 99 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { |
58 CheckPermissionStatus(PermissionType::MIDI_SYSEX, | 100 CheckPermissionStatus(PermissionType::MIDI_SYSEX, |
59 content::PERMISSION_STATUS_ASK); | 101 content::PERMISSION_STATUS_ASK); |
60 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, | 102 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, |
61 content::PERMISSION_STATUS_ASK); | 103 content::PERMISSION_STATUS_ASK); |
62 CheckPermissionStatus(PermissionType::NOTIFICATIONS, | 104 CheckPermissionStatus(PermissionType::NOTIFICATIONS, |
(...skipping 23 matching lines...) Expand all Loading... |
86 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, | 128 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, |
87 content::PERMISSION_STATUS_GRANTED); | 129 content::PERMISSION_STATUS_GRANTED); |
88 | 130 |
89 #if defined(OS_ANDROID) | 131 #if defined(OS_ANDROID) |
90 SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, | 132 SetPermission(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, |
91 CONTENT_SETTING_ALLOW); | 133 CONTENT_SETTING_ALLOW); |
92 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, | 134 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, |
93 content::PERMISSION_STATUS_GRANTED); | 135 content::PERMISSION_STATUS_GRANTED); |
94 #endif | 136 #endif |
95 } | 137 } |
| 138 |
| 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))); |
| 144 |
| 145 GetHostContentSettingsMap()->SetContentSetting( |
| 146 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 147 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 148 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 149 std::string(), |
| 150 CONTENT_SETTING_ALLOW); |
| 151 |
| 152 EXPECT_TRUE(callback_called()); |
| 153 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED, callback_result()); |
| 154 |
| 155 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 156 } |
| 157 |
| 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))); |
| 163 |
| 164 GetHostContentSettingsMap()->SetContentSetting( |
| 165 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 166 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 167 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 168 std::string(), |
| 169 CONTENT_SETTING_ALLOW); |
| 170 |
| 171 EXPECT_FALSE(callback_called()); |
| 172 |
| 173 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 174 } |
| 175 |
| 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))); |
| 181 |
| 182 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 183 |
| 184 GetHostContentSettingsMap()->SetContentSetting( |
| 185 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 186 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 187 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 188 std::string(), |
| 189 CONTENT_SETTING_ALLOW); |
| 190 |
| 191 EXPECT_FALSE(callback_called()); |
| 192 } |
| 193 |
| 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))); |
| 199 |
| 200 GetHostContentSettingsMap()->SetContentSetting( |
| 201 ContentSettingsPattern::FromURLNoWildcard(other_url()), |
| 202 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 203 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 204 std::string(), |
| 205 CONTENT_SETTING_ALLOW); |
| 206 |
| 207 EXPECT_FALSE(callback_called()); |
| 208 |
| 209 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 210 } |
| 211 |
| 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))); |
| 217 |
| 218 GetHostContentSettingsMap()->SetContentSetting( |
| 219 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 220 ContentSettingsPattern::FromURLNoWildcard(other_url()), |
| 221 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 222 std::string(), |
| 223 CONTENT_SETTING_ALLOW); |
| 224 |
| 225 EXPECT_FALSE(callback_called()); |
| 226 |
| 227 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 228 } |
| 229 |
| 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))); |
| 235 |
| 236 GetHostContentSettingsMap()->SetContentSetting( |
| 237 ContentSettingsPattern::Wildcard(), |
| 238 ContentSettingsPattern::Wildcard(), |
| 239 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 240 std::string(), |
| 241 CONTENT_SETTING_ALLOW); |
| 242 |
| 243 EXPECT_TRUE(callback_called()); |
| 244 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED, callback_result()); |
| 245 |
| 246 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 247 } |
| 248 |
| 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); |
| 256 |
| 257 int subscription_id = GetPermissionManager()->SubscribePermissionStatusChange( |
| 258 PermissionType::GEOLOCATION, url(), url(), |
| 259 base::Bind(&PermissionManagerTest::OnPermissionChange, |
| 260 base::Unretained(this))); |
| 261 |
| 262 GetHostContentSettingsMap()->ClearSettingsForOneType( |
| 263 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 264 |
| 265 EXPECT_TRUE(callback_called()); |
| 266 EXPECT_EQ(content::PERMISSION_STATUS_ASK, callback_result()); |
| 267 |
| 268 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 269 } |
| 270 |
| 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))); |
| 276 |
| 277 GetHostContentSettingsMap()->SetContentSetting( |
| 278 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 279 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 280 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 281 std::string(), |
| 282 CONTENT_SETTING_BLOCK); |
| 283 |
| 284 EXPECT_TRUE(callback_called()); |
| 285 EXPECT_EQ(content::PERMISSION_STATUS_DENIED, callback_result()); |
| 286 |
| 287 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 288 } |
| 289 |
| 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); |
| 297 |
| 298 int subscription_id = GetPermissionManager()->SubscribePermissionStatusChange( |
| 299 PermissionType::GEOLOCATION, url(), url(), |
| 300 base::Bind(&PermissionManagerTest::OnPermissionChange, |
| 301 base::Unretained(this))); |
| 302 |
| 303 GetHostContentSettingsMap()->SetContentSetting( |
| 304 ContentSettingsPattern::Wildcard(), |
| 305 ContentSettingsPattern::Wildcard(), |
| 306 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 307 std::string(), |
| 308 CONTENT_SETTING_ALLOW); |
| 309 |
| 310 EXPECT_FALSE(callback_called()); |
| 311 |
| 312 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 313 } |
| 314 |
| 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); |
| 322 |
| 323 int subscription_id = GetPermissionManager()->SubscribePermissionStatusChange( |
| 324 PermissionType::GEOLOCATION, url(), url(), |
| 325 base::Bind(&PermissionManagerTest::OnPermissionChange, |
| 326 base::Unretained(this))); |
| 327 |
| 328 GetHostContentSettingsMap()->SetContentSetting( |
| 329 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 330 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 331 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 332 std::string(), |
| 333 CONTENT_SETTING_ALLOW); |
| 334 |
| 335 EXPECT_TRUE(callback_called()); |
| 336 EXPECT_EQ(content::PERMISSION_STATUS_GRANTED, callback_result()); |
| 337 |
| 338 Reset(); |
| 339 |
| 340 GetHostContentSettingsMap()->SetContentSetting( |
| 341 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 342 ContentSettingsPattern::FromURLNoWildcard(url()), |
| 343 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 344 std::string(), |
| 345 CONTENT_SETTING_ASK); |
| 346 |
| 347 EXPECT_TRUE(callback_called()); |
| 348 EXPECT_EQ(content::PERMISSION_STATUS_ASK, callback_result()); |
| 349 |
| 350 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 351 } |
OLD | NEW |