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

Side by Side Diff: chrome/browser/permissions/permission_context_base_unittest.cc

Issue 2184823007: Add a feature which, when enabled, blocks permissions after X prompt dismissals. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove unnecessary PermissionUtil changes Created 4 years, 4 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_context_base.h" 5 #include "chrome/browser/permissions/permission_context_base.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/feature_list.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
13 #include "base/test/mock_entropy_provider.h" 14 #include "base/test/mock_entropy_provider.h"
14 #include "build/build_config.h" 15 #include "build/build_config.h"
15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
16 #include "chrome/browser/infobars/infobar_service.h" 17 #include "chrome/browser/infobars/infobar_service.h"
17 #include "chrome/browser/permissions/permission_queue_controller.h" 18 #include "chrome/browser/permissions/permission_queue_controller.h"
18 #include "chrome/browser/permissions/permission_request_id.h" 19 #include "chrome/browser/permissions/permission_request_id.h"
19 #include "chrome/browser/permissions/permission_util.h" 20 #include "chrome/browser/permissions/permission_util.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 base::Unretained(&permission_context))); 184 base::Unretained(&permission_context)));
184 185
185 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK); 186 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK);
186 EXPECT_EQ(1u, permission_context.decisions().size()); 187 EXPECT_EQ(1u, permission_context.decisions().size());
187 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); 188 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]);
188 EXPECT_TRUE(permission_context.tab_context_updated()); 189 EXPECT_TRUE(permission_context.tab_context_updated());
189 EXPECT_EQ(CONTENT_SETTING_ASK, 190 EXPECT_EQ(CONTENT_SETTING_ASK,
190 permission_context.GetContentSettingFromMap(url, url)); 191 permission_context.GetContentSettingFromMap(url, url));
191 } 192 }
192 193
194 void DismissMultipleTimesAndExpectBlock(
195 const GURL& url,
196 content::PermissionType permission_type,
197 ContentSettingsType content_settings_type) {
198 TestPermissionContext permission_context(
199 profile(), permission_type, content_settings_type);
200
201 // Dismiss three times. The third dismiss should change the decision from
202 // dismiss to block, and hence change the persisted content setting.
203 for (unsigned int i = 0; i < 3; ++i) {
204 ContentSetting expected =
205 (i < 2) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK;
206 const PermissionRequestID id(
207 web_contents()->GetRenderProcessHost()->GetID(),
208 web_contents()->GetMainFrame()->GetRoutingID(), i);
209 permission_context.RequestPermission(
210 web_contents(), id, url, true /* user_gesture */,
211 base::Bind(&TestPermissionContext::TrackPermissionDecision,
212 base::Unretained(&permission_context)));
213
214 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK);
215 EXPECT_EQ(i+1, permission_context.decisions().size());
raymes 2016/08/01 05:09:11 nit: i + 1
dominickn 2016/08/03 05:38:46 Done.
216 EXPECT_EQ(expected, permission_context.decisions()[i]);
217 EXPECT_TRUE(permission_context.tab_context_updated());
218 EXPECT_EQ(expected,
219 permission_context.GetContentSettingFromMap(url, url));
220 }
221 }
222
223 void TestBlockOnSeveralDismissals_TestContent() {
224 GURL url("https://www.google.com");
225 NavigateAndCommit(url);
226
227 {
228 TestPermissionContext permission_context(
229 profile(), content::PermissionType::GEOLOCATION,
230 CONTENT_SETTINGS_TYPE_GEOLOCATION);
231
232 // First, ensure that > 3 dismissals behaves correctly.
233 for (unsigned int i = 0; i < 4; ++i) {
234 const PermissionRequestID id(
235 web_contents()->GetRenderProcessHost()->GetID(),
236 web_contents()->GetMainFrame()->GetRoutingID(), i);
237 permission_context.RequestPermission(
238 web_contents(), id, url, true /* user_gesture */,
239 base::Bind(&TestPermissionContext::TrackPermissionDecision,
240 base::Unretained(&permission_context)));
241
242 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK);
243 EXPECT_EQ(i+1, permission_context.decisions().size());
raymes 2016/08/01 05:09:11 nit: i + 1
dominickn 2016/08/03 05:38:46 Done.
244 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[i]);
245 EXPECT_TRUE(permission_context.tab_context_updated());
246 EXPECT_EQ(CONTENT_SETTING_ASK,
247 permission_context.GetContentSettingFromMap(url, url));
248 }
249 }
250
251 // Flush the dismissal counts. Enable the block on too many dismissals
252 // feature, which is disabled by default.
253 HostContentSettingsMapFactory::GetForProfile(profile())
254 ->ClearSettingsForOneType(
255 CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT);
256 base::FeatureList::ClearInstanceForTesting();
257 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
258 feature_list->InitializeFromCommandLine(
259 "BlockPromptsIfDismissedOften<PermissionPromptsUX", "");
260 base::FeatureList::SetInstance(std::move(feature_list));
261
262 // Sanity check independence per permission type by checking two of them.
263 DismissMultipleTimesAndExpectBlock(url,
264 content::PermissionType::GEOLOCATION,
265 CONTENT_SETTINGS_TYPE_GEOLOCATION);
266 DismissMultipleTimesAndExpectBlock(url,
267 content::PermissionType::NOTIFICATIONS,
268 CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
269 }
raymes 2016/08/01 05:09:11 Should we also test changing the parameter?
dominickn 2016/08/03 05:38:46 Done.
270
193 void TestRequestPermissionInvalidUrl( 271 void TestRequestPermissionInvalidUrl(
194 content::PermissionType permission_type, 272 content::PermissionType permission_type,
195 ContentSettingsType content_settings_type) { 273 ContentSettingsType content_settings_type) {
196 TestPermissionContext permission_context(profile(), permission_type, 274 TestPermissionContext permission_context(profile(), permission_type,
197 content_settings_type); 275 content_settings_type);
198 GURL url; 276 GURL url;
199 ASSERT_FALSE(url.is_valid()); 277 ASSERT_FALSE(url.is_valid());
200 NavigateAndCommit(url); 278 NavigateAndCommit(url);
201 279
202 const PermissionRequestID id( 280 const PermissionRequestID id(
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 TEST_F(PermissionContextBaseTests, TestAskAndGrant) { 404 TEST_F(PermissionContextBaseTests, TestAskAndGrant) {
327 TestAskAndGrant_TestContent(); 405 TestAskAndGrant_TestContent();
328 } 406 }
329 407
330 // Simulates clicking Dismiss (X) in the infobar/bubble. 408 // Simulates clicking Dismiss (X) in the infobar/bubble.
331 // The permission should be denied but not saved for future use. 409 // The permission should be denied but not saved for future use.
332 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) { 410 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) {
333 TestAskAndDismiss_TestContent(); 411 TestAskAndDismiss_TestContent();
334 } 412 }
335 413
414 // Simulates clicking Dismiss (X) in the infobar/bubble with the block on too
415 // many dismissals feature active. On Android, this logic is contained in
416 // PermissionQueueController, so don't run this test there.
417 // The permission should be blocked after several dismissals.
raymes 2016/08/01 05:09:11 I think this should work on Android - RespondToPer
dominickn 2016/08/03 05:38:46 After much spelunking, got the tests working on An
418 #if !defined(OS_ANDROID)
419 TEST_F(PermissionContextBaseTests, TestDismissUntilBlocked) {
420 TestBlockOnSeveralDismissals_TestContent();
raymes 2016/08/01 05:09:11 Hmm it feels strange not to inline this, but I see
dominickn 2016/08/03 05:38:46 I mainly did it for consistency with the other tes
421 }
422 #endif
423
336 // Simulates non-valid requesting URL. 424 // Simulates non-valid requesting URL.
337 // The permission should be denied but not saved for future use. 425 // The permission should be denied but not saved for future use.
338 TEST_F(PermissionContextBaseTests, TestNonValidRequestingUrl) { 426 TEST_F(PermissionContextBaseTests, TestNonValidRequestingUrl) {
339 TestRequestPermissionInvalidUrl(content::PermissionType::GEOLOCATION, 427 TestRequestPermissionInvalidUrl(content::PermissionType::GEOLOCATION,
340 CONTENT_SETTINGS_TYPE_GEOLOCATION); 428 CONTENT_SETTINGS_TYPE_GEOLOCATION);
341 TestRequestPermissionInvalidUrl(content::PermissionType::NOTIFICATIONS, 429 TestRequestPermissionInvalidUrl(content::PermissionType::NOTIFICATIONS,
342 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 430 CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
343 TestRequestPermissionInvalidUrl(content::PermissionType::MIDI_SYSEX, 431 TestRequestPermissionInvalidUrl(content::PermissionType::MIDI_SYSEX,
344 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); 432 CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
345 TestRequestPermissionInvalidUrl(content::PermissionType::PUSH_MESSAGING, 433 TestRequestPermissionInvalidUrl(content::PermissionType::PUSH_MESSAGING,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 TestParallelRequests(CONTENT_SETTING_ALLOW); 508 TestParallelRequests(CONTENT_SETTING_ALLOW);
421 } 509 }
422 510
423 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { 511 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) {
424 TestParallelRequests(CONTENT_SETTING_BLOCK); 512 TestParallelRequests(CONTENT_SETTING_BLOCK);
425 } 513 }
426 514
427 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { 515 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) {
428 TestParallelRequests(CONTENT_SETTING_ASK); 516 TestParallelRequests(CONTENT_SETTING_ASK);
429 } 517 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698