Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: chrome/browser/content_settings/permission_observer_unittest.cc

Issue 990303002: Implement PermissionService::GetNextPermissionChange. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permission_impl
Patch Set: review comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 PermissionObserverTest : public ChromeRenderViewHostTestHarness {
14 public:
15 void OnPermissionChange(ContentSetting content_setting) {
16 callback_called_ = true;
17 content_setting_ = content_setting;
18 }
19
20 protected:
21 PermissionObserverTest()
22 : ChromeRenderViewHostTestHarness(),
23 url_("https://example.com"),
24 other_url_("https://foo.com"),
25 callback_called_(false),
26 content_setting_(CONTENT_SETTING_DEFAULT) {
27 }
28
29 PermissionObserver* GetPermissionObserver() {
30 return PermissionObserverFactory::GetForProfile(profile());
31 }
32
33 HostContentSettingsMap* GetHostContentSettingsMap() {
34 return profile()->GetHostContentSettingsMap();
35 }
36
37 const GURL& url() const {
38 return url_;
39 }
40
41 const GURL& other_url() const {
42 return other_url_;
43 }
44
45 bool callback_called() const {
46 return callback_called_;
47 }
48
49 ContentSetting content_setting() const {
50 return content_setting_;
51 }
52
53 void Reset() {
54 callback_called_ = false;
55 content_setting_ = CONTENT_SETTING_DEFAULT;
56 }
57
58 private:
59 const GURL url_;
60 const GURL other_url_;
61 bool callback_called_;
62 ContentSetting content_setting_;
63 };
64
65 TEST_F(PermissionObserverTest, SameTypeChangeNotifies) {
66 int subscription_id = GetPermissionObserver()->Subscribe(
67 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
68 base::Bind(&PermissionObserverTest::OnPermissionChange,
69 base::Unretained(this)));
70
71 GetHostContentSettingsMap()->SetContentSetting(
72 ContentSettingsPattern::FromURLNoWildcard(url()),
73 ContentSettingsPattern::FromURLNoWildcard(url()),
74 CONTENT_SETTINGS_TYPE_GEOLOCATION,
75 std::string(),
76 CONTENT_SETTING_ALLOW);
77
78 EXPECT_TRUE(callback_called());
79 EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting());
80
81 GetPermissionObserver()->Unsubscribe(subscription_id);
82 }
83
84 TEST_F(PermissionObserverTest, DifferentTypeChangeDoesNotNotify) {
85 int subscription_id = GetPermissionObserver()->Subscribe(
86 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
87 base::Bind(&PermissionObserverTest::OnPermissionChange,
88 base::Unretained(this)));
89
90 GetHostContentSettingsMap()->SetContentSetting(
91 ContentSettingsPattern::FromURLNoWildcard(url()),
92 ContentSettingsPattern::FromURLNoWildcard(url()),
93 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
94 std::string(),
95 CONTENT_SETTING_ALLOW);
96
97 EXPECT_FALSE(callback_called());
98
99 GetPermissionObserver()->Unsubscribe(subscription_id);
100 }
101
102 TEST_F(PermissionObserverTest, ChangeAfterUnsubscribeDoesNotNotify) {
103 int subscription_id = GetPermissionObserver()->Subscribe(
104 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
105 base::Bind(&PermissionObserverTest::OnPermissionChange,
106 base::Unretained(this)));
107
108 GetPermissionObserver()->Unsubscribe(subscription_id);
109
110 GetHostContentSettingsMap()->SetContentSetting(
111 ContentSettingsPattern::FromURLNoWildcard(url()),
112 ContentSettingsPattern::FromURLNoWildcard(url()),
113 CONTENT_SETTINGS_TYPE_GEOLOCATION,
114 std::string(),
115 CONTENT_SETTING_ALLOW);
116
117 EXPECT_FALSE(callback_called());
118 }
119
120 TEST_F(PermissionObserverTest, DifferentPrimaryPatternDoesNotNotify) {
121 int subscription_id = GetPermissionObserver()->Subscribe(
122 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
123 base::Bind(&PermissionObserverTest::OnPermissionChange,
124 base::Unretained(this)));
125
126 GetHostContentSettingsMap()->SetContentSetting(
127 ContentSettingsPattern::FromURLNoWildcard(other_url()),
128 ContentSettingsPattern::FromURLNoWildcard(url()),
129 CONTENT_SETTINGS_TYPE_GEOLOCATION,
130 std::string(),
131 CONTENT_SETTING_ALLOW);
132
133 EXPECT_FALSE(callback_called());
134
135 GetPermissionObserver()->Unsubscribe(subscription_id);
136 }
137
138 TEST_F(PermissionObserverTest, DifferentSecondaryPatternDoesNotNotify) {
139 int subscription_id = GetPermissionObserver()->Subscribe(
140 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
141 base::Bind(&PermissionObserverTest::OnPermissionChange,
142 base::Unretained(this)));
143
144 GetHostContentSettingsMap()->SetContentSetting(
145 ContentSettingsPattern::FromURLNoWildcard(url()),
146 ContentSettingsPattern::FromURLNoWildcard(other_url()),
147 CONTENT_SETTINGS_TYPE_GEOLOCATION,
148 std::string(),
149 CONTENT_SETTING_ALLOW);
150
151 EXPECT_FALSE(callback_called());
152
153 GetPermissionObserver()->Unsubscribe(subscription_id);
154 }
155
156 TEST_F(PermissionObserverTest, WildCardPatternNotifies) {
157 int subscription_id = GetPermissionObserver()->Subscribe(
158 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
159 base::Bind(&PermissionObserverTest::OnPermissionChange,
160 base::Unretained(this)));
161
162 GetHostContentSettingsMap()->SetContentSetting(
163 ContentSettingsPattern::Wildcard(),
164 ContentSettingsPattern::Wildcard(),
165 CONTENT_SETTINGS_TYPE_GEOLOCATION,
166 std::string(),
167 CONTENT_SETTING_ALLOW);
168
169 EXPECT_TRUE(callback_called());
170 EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting());
171
172 GetPermissionObserver()->Unsubscribe(subscription_id);
173 }
174
175 TEST_F(PermissionObserverTest, ClearSettingsNotifies) {
176 GetHostContentSettingsMap()->SetContentSetting(
177 ContentSettingsPattern::FromURLNoWildcard(url()),
178 ContentSettingsPattern::FromURLNoWildcard(url()),
179 CONTENT_SETTINGS_TYPE_GEOLOCATION,
180 std::string(),
181 CONTENT_SETTING_ALLOW);
182
183 int subscription_id = GetPermissionObserver()->Subscribe(
184 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
185 base::Bind(&PermissionObserverTest::OnPermissionChange,
186 base::Unretained(this)));
187
188 GetHostContentSettingsMap()->ClearSettingsForOneType(
189 CONTENT_SETTINGS_TYPE_GEOLOCATION);
190
191 EXPECT_TRUE(callback_called());
192 EXPECT_EQ(CONTENT_SETTING_ASK, content_setting());
193
194 GetPermissionObserver()->Unsubscribe(subscription_id);
195 }
196
197 TEST_F(PermissionObserverTest, NewValueCorrectlyPassed) {
198 int subscription_id = GetPermissionObserver()->Subscribe(
199 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
200 base::Bind(&PermissionObserverTest::OnPermissionChange,
201 base::Unretained(this)));
202
203 GetHostContentSettingsMap()->SetContentSetting(
204 ContentSettingsPattern::FromURLNoWildcard(url()),
205 ContentSettingsPattern::FromURLNoWildcard(url()),
206 CONTENT_SETTINGS_TYPE_GEOLOCATION,
207 std::string(),
208 CONTENT_SETTING_BLOCK);
209
210 EXPECT_TRUE(callback_called());
211 EXPECT_EQ(CONTENT_SETTING_BLOCK, content_setting());
212
213 GetPermissionObserver()->Unsubscribe(subscription_id);
214 }
215
216 TEST_F(PermissionObserverTest, ChangeWithoutPermissionChangeDoesNotNotify) {
217 GetHostContentSettingsMap()->SetContentSetting(
218 ContentSettingsPattern::FromURLNoWildcard(url()),
219 ContentSettingsPattern::FromURLNoWildcard(url()),
220 CONTENT_SETTINGS_TYPE_GEOLOCATION,
221 std::string(),
222 CONTENT_SETTING_ALLOW);
223
224 int subscription_id = GetPermissionObserver()->Subscribe(
225 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
226 base::Bind(&PermissionObserverTest::OnPermissionChange,
227 base::Unretained(this)));
228
229 GetHostContentSettingsMap()->SetContentSetting(
230 ContentSettingsPattern::Wildcard(),
231 ContentSettingsPattern::Wildcard(),
232 CONTENT_SETTINGS_TYPE_GEOLOCATION,
233 std::string(),
234 CONTENT_SETTING_ALLOW);
235
236 EXPECT_FALSE(callback_called());
237
238 GetPermissionObserver()->Unsubscribe(subscription_id);
239 }
240
241 TEST_F(PermissionObserverTest, ChangesBackAndForth) {
242 GetHostContentSettingsMap()->SetContentSetting(
243 ContentSettingsPattern::FromURLNoWildcard(url()),
244 ContentSettingsPattern::FromURLNoWildcard(url()),
245 CONTENT_SETTINGS_TYPE_GEOLOCATION,
246 std::string(),
247 CONTENT_SETTING_ASK);
248
249 int subscription_id = GetPermissionObserver()->Subscribe(
250 CONTENT_SETTINGS_TYPE_GEOLOCATION, url(), url(),
251 base::Bind(&PermissionObserverTest::OnPermissionChange,
252 base::Unretained(this)));
253
254 GetHostContentSettingsMap()->SetContentSetting(
255 ContentSettingsPattern::FromURLNoWildcard(url()),
256 ContentSettingsPattern::FromURLNoWildcard(url()),
257 CONTENT_SETTINGS_TYPE_GEOLOCATION,
258 std::string(),
259 CONTENT_SETTING_ALLOW);
260
261 EXPECT_TRUE(callback_called());
262 EXPECT_EQ(CONTENT_SETTING_ALLOW, content_setting());
263
264 Reset();
265
266 GetHostContentSettingsMap()->SetContentSetting(
267 ContentSettingsPattern::FromURLNoWildcard(url()),
268 ContentSettingsPattern::FromURLNoWildcard(url()),
269 CONTENT_SETTINGS_TYPE_GEOLOCATION,
270 std::string(),
271 CONTENT_SETTING_ASK);
272
273 EXPECT_TRUE(callback_called());
274 EXPECT_EQ(CONTENT_SETTING_ASK, content_setting());
275
276 GetPermissionObserver()->Unsubscribe(subscription_id);
277 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698