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

Side by Side Diff: chrome/browser/permissions/permission_decision_auto_blocker_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: Address comments 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
(Empty)
1 // Copyright 2016 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/permissions/permission_decision_auto_blocker.h"
6
7 #include "base/bind.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/browser/permission_type.h"
11
12 namespace {
13
14 bool FilterGoogle(const GURL& url) {
15 return url == GURL("https://www.google.com");
16 }
17
18 bool FilterAll(const GURL& url) {
19 return true;
20 }
21
22 } // namespace
23
24 class PermissionDecisionAutoBlockerUnitTest
25 : public ChromeRenderViewHostTestHarness {
26 protected:
27 int GetDismissalCount(const GURL& url,
28 content::PermissionType permission) {
29 return PermissionDecisionAutoBlocker(profile()).GetActionCountForTest(
30 url, permission, PermissionDecisionAutoBlocker::kPromptDismissCountKey);
31 }
32
33 int GetIgnoreCount(const GURL& url, content::PermissionType permission) {
34 return PermissionDecisionAutoBlocker(profile()).GetActionCountForTest(
35 url, permission, PermissionDecisionAutoBlocker::kPromptIgnoreCountKey);
36 }
37
38 int RecordDismiss(const GURL& url, content::PermissionType permission) {
39 PermissionDecisionAutoBlocker blocker(profile());
40 blocker.ShouldChangeDismissalToBlock(url, permission);
41 return blocker.GetActionCountForTest(
42 url, permission, PermissionDecisionAutoBlocker::kPromptDismissCountKey);
43 }
44
45 int RecordIgnore(const GURL& url, content::PermissionType permission) {
46 return PermissionDecisionAutoBlocker(profile()).RecordIgnore(url,
47 permission);
48 }
49 };
50
51 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) {
52 GURL url1("https://www.google.com");
53 GURL url2("https://www.example.com");
54
55 // Record some dismissals.
56 EXPECT_EQ(1, RecordDismiss(url1, content::PermissionType::GEOLOCATION));
57 EXPECT_EQ(2, RecordDismiss(url1, content::PermissionType::GEOLOCATION));
58 EXPECT_EQ(3, RecordDismiss(url1, content::PermissionType::GEOLOCATION));
59
60 EXPECT_EQ(1, RecordDismiss(url2, content::PermissionType::GEOLOCATION));
61 EXPECT_EQ(1, RecordDismiss(url1, content::PermissionType::NOTIFICATIONS));
62
63 // Record some ignores.
64 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::MIDI_SYSEX));
65 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::DURABLE_STORAGE));
66 EXPECT_EQ(1, RecordIgnore(url2, content::PermissionType::GEOLOCATION));
67 EXPECT_EQ(2, RecordIgnore(url2, content::PermissionType::GEOLOCATION));
68
69 PermissionDecisionAutoBlocker::RemoveCountsByUrl(profile(),
70 base::Bind(&FilterGoogle));
71
72 // Expect that url1's actions are gone, but url2's remain.
73 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION));
74 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS));
75 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::MIDI_SYSEX));
76 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::DURABLE_STORAGE));
77
78 EXPECT_EQ(1, GetDismissalCount(url2, content::PermissionType::GEOLOCATION));
79 EXPECT_EQ(2, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION));
80
81 // Add some more actions.
82 EXPECT_EQ(1, RecordDismiss(url1, content::PermissionType::GEOLOCATION));
83 EXPECT_EQ(1, RecordDismiss(url1, content::PermissionType::NOTIFICATIONS));
84 EXPECT_EQ(2, RecordDismiss(url2, content::PermissionType::GEOLOCATION));
85
86 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::GEOLOCATION));
87 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::NOTIFICATIONS));
88 EXPECT_EQ(1, RecordIgnore(url1, content::PermissionType::DURABLE_STORAGE));
89 EXPECT_EQ(1, RecordIgnore(url2, content::PermissionType::MIDI_SYSEX));
90
91 // Remove everything and expect that it's all gone.
92 PermissionDecisionAutoBlocker::RemoveCountsByUrl(profile(),
93 base::Bind(&FilterAll));
94
95 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::GEOLOCATION));
96 EXPECT_EQ(0, GetDismissalCount(url1, content::PermissionType::NOTIFICATIONS));
97 EXPECT_EQ(0, GetDismissalCount(url2, content::PermissionType::GEOLOCATION));
98
99 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::GEOLOCATION));
100 EXPECT_EQ(0, GetIgnoreCount(url1, content::PermissionType::NOTIFICATIONS));
101 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::GEOLOCATION));
102 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::DURABLE_STORAGE));
103 EXPECT_EQ(0, GetIgnoreCount(url2, content::PermissionType::MIDI_SYSEX));
104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698