Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/content_settings/permission_observer.h" | |
| 6 | |
| 7 #include "chrome/browser/content_settings/permission_observer_factory.h" | |
| 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 9 #include "chrome/test/base/testing_profile.h" | |
| 10 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 class PermissionObserverTests : public ChromeRenderViewHostTestHarness { | |
|
Bernhard Bauer
2015/03/11 14:06:18
The usual style is to use singular for the test fi
mlamouri (slow - plz ping)
2015/03/18 16:24:40
I will not point that permission_context_base_unit
Bernhard Bauer
2015/03/23 10:20:43
Feel free to complain about that to the person who
| |
| 14 public: | |
| 15 void OnPermissionChange(ContentSetting content_setting) { | |
| 16 callback_called_ = true; | |
| 17 content_setting_ = content_setting; | |
| 18 } | |
| 19 | |
| 20 protected: | |
| 21 PermissionObserverTests() | |
| 22 : ChromeRenderViewHostTestHarness(), | |
| 23 url_("https://example.com"), | |
| 24 callback_called_(false), | |
| 25 content_setting_(CONTENT_SETTING_DEFAULT) { | |
| 26 } | |
| 27 | |
| 28 PermissionObserver* GetPermissionObserver() { | |
| 29 return PermissionObserverFactory::GetForProfile(profile()); | |
| 30 } | |
| 31 | |
| 32 HostContentSettingsMap* GetHostContentSettingsMap() { | |
| 33 return profile()->GetHostContentSettingsMap(); | |
| 34 } | |
| 35 | |
| 36 const GURL& url() const { | |
| 37 return url_; | |
|
Bernhard Bauer
2015/03/11 14:06:18
If this URL is constant, you could just directly u
mlamouri (slow - plz ping)
2015/03/18 16:24:40
I used to do that and I stopped because it was add
Bernhard Bauer
2015/03/23 10:20:43
What additional cost is that?
Bernhard Bauer
2015/03/23 18:26:18
Ping?
| |
| 38 } | |
| 39 | |
| 40 bool callback_called() const { | |
| 41 return callback_called_; | |
| 42 } | |
| 43 | |
| 44 ContentSetting content_setting() const { | |
| 45 return content_setting_; | |
| 46 } | |
| 47 | |
| 48 void Reset() { | |
| 49 callback_called_ = false; | |
| 50 content_setting_ = CONTENT_SETTING_DEFAULT; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 const GURL url_; | |
| 55 bool callback_called_; | |
| 56 ContentSetting content_setting_; | |
| 57 }; | |
| 58 | |
| 59 TEST_F(PermissionObserverTests, SameTypeChangeNotifies) { | |
| 60 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 61 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 62 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 63 base::Unretained(this))); | |
| 64 | |
| 65 GetHostContentSettingsMap()->SetContentSetting( | |
| 66 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 67 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 68 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 69 std::string(), | |
| 70 CONTENT_SETTING_ALLOW); | |
| 71 | |
| 72 EXPECT_TRUE(callback_called()); | |
| 73 EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting()); | |
| 74 | |
| 75 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 76 } | |
| 77 | |
| 78 TEST_F(PermissionObserverTests, DifferentTypeChangeDoesNotNotify) { | |
| 79 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 80 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 81 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 82 base::Unretained(this))); | |
| 83 | |
| 84 GetHostContentSettingsMap()->SetContentSetting( | |
| 85 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 86 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 87 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 88 std::string(), | |
| 89 CONTENT_SETTING_ALLOW); | |
| 90 | |
| 91 EXPECT_FALSE(callback_called()); | |
| 92 | |
| 93 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 94 } | |
| 95 | |
| 96 TEST_F(PermissionObserverTests, ChangeAfterUnsubscribeDoesNotNotify) { | |
| 97 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 98 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 99 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 100 base::Unretained(this))); | |
| 101 | |
| 102 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 103 | |
| 104 GetHostContentSettingsMap()->SetContentSetting( | |
| 105 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 106 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 107 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 108 std::string(), | |
| 109 CONTENT_SETTING_ALLOW); | |
| 110 | |
| 111 EXPECT_FALSE(callback_called()); | |
| 112 } | |
| 113 | |
| 114 TEST_F(PermissionObserverTests, DifferentPrimaryPatternDoesNotNotify) { | |
| 115 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 116 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 117 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 118 base::Unretained(this))); | |
| 119 | |
| 120 GetHostContentSettingsMap()->SetContentSetting( | |
| 121 ContentSettingsPattern::FromURLNoWildcard(GURL("https://foo.com")), | |
| 122 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 123 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 124 std::string(), | |
| 125 CONTENT_SETTING_ALLOW); | |
| 126 | |
| 127 EXPECT_FALSE(callback_called()); | |
| 128 | |
| 129 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 130 } | |
| 131 | |
| 132 TEST_F(PermissionObserverTests, DifferentSecondaryPatternDoesNotNotify) { | |
| 133 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 134 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 135 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 136 base::Unretained(this))); | |
| 137 | |
| 138 GetHostContentSettingsMap()->SetContentSetting( | |
| 139 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 140 ContentSettingsPattern::FromURLNoWildcard(GURL("https://foo.com")), | |
| 141 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 142 std::string(), | |
| 143 CONTENT_SETTING_ALLOW); | |
| 144 | |
| 145 EXPECT_FALSE(callback_called()); | |
| 146 | |
| 147 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 148 } | |
| 149 | |
| 150 TEST_F(PermissionObserverTests, WildCardPatternNotifies) { | |
| 151 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 152 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 153 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 154 base::Unretained(this))); | |
| 155 | |
| 156 GetHostContentSettingsMap()->SetContentSetting( | |
| 157 ContentSettingsPattern::Wildcard(), | |
| 158 ContentSettingsPattern::Wildcard(), | |
| 159 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 160 std::string(), | |
| 161 CONTENT_SETTING_ALLOW); | |
| 162 | |
| 163 EXPECT_TRUE(callback_called()); | |
| 164 EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting()); | |
| 165 | |
| 166 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 167 } | |
| 168 | |
| 169 TEST_F(PermissionObserverTests, ClearSettingsNotifies) { | |
| 170 GetHostContentSettingsMap()->SetContentSetting( | |
| 171 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 172 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 173 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 174 std::string(), | |
| 175 CONTENT_SETTING_ALLOW); | |
| 176 | |
| 177 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 178 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 179 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 180 base::Unretained(this))); | |
| 181 | |
| 182 GetHostContentSettingsMap()->ClearSettingsForOneType( | |
| 183 CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 184 | |
| 185 EXPECT_TRUE(callback_called()); | |
| 186 EXPECT_EQ(CONTENT_SETTING_ASK, content_setting()); | |
| 187 | |
| 188 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 189 } | |
| 190 | |
| 191 TEST_F(PermissionObserverTests, NewValueCorrectlyPassed) { | |
| 192 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 193 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 194 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 195 base::Unretained(this))); | |
| 196 | |
| 197 GetHostContentSettingsMap()->SetContentSetting( | |
| 198 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 199 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 200 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 201 std::string(), | |
| 202 CONTENT_SETTING_BLOCK); | |
| 203 | |
| 204 EXPECT_TRUE(callback_called()); | |
| 205 EXPECT_EQ(CONTENT_SETTING_BLOCK, content_setting()); | |
| 206 | |
| 207 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 208 } | |
| 209 | |
| 210 TEST_F(PermissionObserverTests, ChangeWithoutPermissionChangeDoesNotNotify) { | |
| 211 GetHostContentSettingsMap()->SetContentSetting( | |
| 212 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 213 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 214 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 215 std::string(), | |
| 216 CONTENT_SETTING_ALLOW); | |
| 217 | |
| 218 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 219 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 220 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 221 base::Unretained(this))); | |
| 222 | |
| 223 GetHostContentSettingsMap()->SetContentSetting( | |
| 224 ContentSettingsPattern::Wildcard(), | |
| 225 ContentSettingsPattern::Wildcard(), | |
| 226 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 227 std::string(), | |
| 228 CONTENT_SETTING_ALLOW); | |
| 229 | |
| 230 EXPECT_FALSE(callback_called()); | |
| 231 | |
| 232 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 233 } | |
| 234 | |
| 235 TEST_F(PermissionObserverTests, ChangesBackAndForth) { | |
| 236 GetHostContentSettingsMap()->SetContentSetting( | |
| 237 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 238 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 239 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 240 std::string(), | |
| 241 CONTENT_SETTING_ASK); | |
| 242 | |
| 243 int subscription_id = GetPermissionObserver()->Subscribe( | |
| 244 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(), | |
| 245 base::Bind(&PermissionObserverTests::OnPermissionChange, | |
| 246 base::Unretained(this))); | |
| 247 | |
| 248 GetHostContentSettingsMap()->SetContentSetting( | |
| 249 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 250 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 251 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 252 std::string(), | |
| 253 CONTENT_SETTING_ALLOW); | |
| 254 | |
| 255 EXPECT_TRUE(callback_called()); | |
| 256 EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting()); | |
| 257 | |
| 258 Reset(); | |
| 259 | |
| 260 GetHostContentSettingsMap()->SetContentSetting( | |
| 261 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 262 ContentSettingsPattern::FromURLNoWildcard(url()), | |
| 263 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 264 std::string(), | |
| 265 CONTENT_SETTING_ASK); | |
| 266 | |
| 267 EXPECT_TRUE(callback_called()); | |
| 268 EXPECT_EQ(CONTENT_SETTING_ASK, content_setting()); | |
| 269 | |
| 270 GetPermissionObserver()->Unsubscribe(subscription_id); | |
| 271 } | |
| OLD | NEW |