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

Side by Side Diff: chrome/browser/subresource_filter/subresource_filter_content_settings_manager_unittest.cc

Issue 2795053002: [subresource_filter] Implement the "Smart" UI on Android (Closed)
Patch Set: rebase on #468985 Created 3 years, 7 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/subresource_filter/subresource_filter_content_settings_ manager.h" 5 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_ manager.h"
6 6
7 #include <set>
8 #include <utility>
9
7 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/run_loop.h"
13 #include "base/task/cancelable_task_tracker.h"
8 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
15 #include "base/test/simple_test_clock.h"
16 #include "base/time/time.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
18 #include "chrome/browser/history/history_service_factory.h"
10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" 19 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
20 #include "chrome/browser/subresource_filter/subresource_filter_profile_context.h "
11 #include "chrome/browser/subresource_filter/subresource_filter_profile_context_f actory.h" 21 #include "chrome/browser/subresource_filter/subresource_filter_profile_context_f actory.h"
12 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h" 23 #include "components/content_settings/core/browser/host_content_settings_map.h"
24 #include "components/content_settings/core/common/content_settings.h"
25 #include "components/history/core/browser/history_service.h"
26 #include "components/history/core/test/history_service_test_util.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 27 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "url/gurl.h"
16 30
17 namespace { 31 namespace {
18 32
19 const char kActionsHistogram[] = "SubresourceFilter.Actions"; 33 const char kActionsHistogram[] = "SubresourceFilter.Actions";
20 34
21 class SubresourceFilterContentSettingsManagerTest : public testing::Test { 35 class SubresourceFilterContentSettingsManagerTest : public testing::Test {
22 public: 36 public:
23 SubresourceFilterContentSettingsManagerTest() {} 37 SubresourceFilterContentSettingsManagerTest() {}
24 38
25 void SetUp() override { 39 void SetUp() override {
26 SubresourceFilterProfileContextFactory::EnsureForProfile(&testing_profile_); 40 settings_manager_ =
41 SubresourceFilterProfileContextFactory::GetForProfile(&testing_profile_)
42 ->settings_manager();
43 auto test_clock = base::MakeUnique<base::SimpleTestClock>();
44 test_clock_ = test_clock.get();
45 settings_manager_->set_clock_for_testing(std::move(test_clock));
27 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); 46 histogram_tester().ExpectTotalCount(kActionsHistogram, 0);
28 } 47 }
29 48
30 HostContentSettingsMap* GetSettingsMap() { 49 HostContentSettingsMap* GetSettingsMap() {
31 return HostContentSettingsMapFactory::GetForProfile(&testing_profile_); 50 return HostContentSettingsMapFactory::GetForProfile(profile());
32 } 51 }
33 52
34 const base::HistogramTester& histogram_tester() { return histogram_tester_; } 53 const base::HistogramTester& histogram_tester() { return histogram_tester_; }
35 54
55 SubresourceFilterContentSettingsManager* settings_manager() {
56 return settings_manager_;
57 }
58
59 TestingProfile* profile() { return &testing_profile_; }
60
61 ContentSetting GetContentSettingMatchingUrlExactly(const GURL& url) {
msramek 2017/05/08 16:40:26 nit: The name is a bit misleading, because no patt
Charlie Harrison 2017/05/08 20:57:21 Renamed to GetContentSettingMatchingUrlWithEmptyPa
62 ContentSettingsForOneType host_settings;
63 GetSettingsMap()->GetSettingsForOneType(
64 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER,
65 std::string(), &host_settings);
66 GURL url_with_empty_path = url.GetWithEmptyPath();
67 for (const auto& it : host_settings) {
68 // Need GURL conversion to get rid of unnecessary default ports.
69 if (GURL(it.primary_pattern.ToString()) == url_with_empty_path)
70 return it.setting;
71 }
72 return CONTENT_SETTING_DEFAULT;
73 }
74
75 base::SimpleTestClock* test_clock() { return test_clock_; }
76
36 private: 77 private:
78 base::ScopedTempDir scoped_dir_;
79
37 content::TestBrowserThreadBundle thread_bundle_; 80 content::TestBrowserThreadBundle thread_bundle_;
38 TestingProfile testing_profile_; 81 TestingProfile testing_profile_;
39 base::HistogramTester histogram_tester_; 82 base::HistogramTester histogram_tester_;
40 83
84 // Owned by the testing_profile_.
85 SubresourceFilterContentSettingsManager* settings_manager_ = nullptr;
86
87 // Owned by the settings_manager_.
88 base::SimpleTestClock* test_clock_ = nullptr;
89
41 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerTest); 90 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerTest);
42 }; 91 };
43 92
93 // It isn't very cheap to initialize the history service. Tests that need it can
94 // use this harness.
95 class SubresourceFilterContentSettingsManagerHistoryTest
96 : public SubresourceFilterContentSettingsManagerTest {
97 public:
98 void SetUp() override {
99 ASSERT_TRUE(profile()->CreateHistoryService(true /* delete_file */,
100 false /* no_db */));
101 SubresourceFilterContentSettingsManagerTest::SetUp();
102 }
103 };
104
44 TEST_F(SubresourceFilterContentSettingsManagerTest, IrrelevantSetting) { 105 TEST_F(SubresourceFilterContentSettingsManagerTest, IrrelevantSetting) {
45 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, 106 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS,
46 CONTENT_SETTING_ALLOW); 107 CONTENT_SETTING_ALLOW);
47 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); 108 histogram_tester().ExpectTotalCount(kActionsHistogram, 0);
48 } 109 }
49 110
50 TEST_F(SubresourceFilterContentSettingsManagerTest, DefaultSetting) { 111 TEST_F(SubresourceFilterContentSettingsManagerTest, DefaultSetting) {
51 GetSettingsMap()->SetDefaultContentSetting( 112 GetSettingsMap()->SetDefaultContentSetting(
52 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, CONTENT_SETTING_ALLOW); 113 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, CONTENT_SETTING_ALLOW);
53 histogram_tester().ExpectBucketCount(kActionsHistogram, 114 histogram_tester().ExpectBucketCount(kActionsHistogram,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 152
92 GetSettingsMap()->SetContentSettingCustomScope( 153 GetSettingsMap()->SetContentSettingCustomScope(
93 primary_pattern, secondary_pattern, 154 primary_pattern, secondary_pattern,
94 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), 155 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(),
95 CONTENT_SETTING_ALLOW); 156 CONTENT_SETTING_ALLOW);
96 histogram_tester().ExpectTotalCount(kActionsHistogram, 2); 157 histogram_tester().ExpectTotalCount(kActionsHistogram, 2);
97 histogram_tester().ExpectBucketCount(kActionsHistogram, 158 histogram_tester().ExpectBucketCount(kActionsHistogram,
98 kActionContentSettingsWildcardUpdate, 2); 159 kActionContentSettingsWildcardUpdate, 2);
99 } 160 }
100 161
162 TEST_F(SubresourceFilterContentSettingsManagerTest, SmartUI) {
163 if (!settings_manager()->should_use_smart_ui())
164 return;
165
166 GURL url("https://example.test/");
167 GURL url2("https://example.test/path");
168 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url));
169 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2));
170
171 EXPECT_EQ(CONTENT_SETTING_DEFAULT, GetContentSettingMatchingUrlExactly(url));
172 settings_manager()->OnDidShowUI(url);
173
174 // Subsequent navigations to same-domains should not show UI.
msramek 2017/05/08 16:40:26 nit: Same domain or same origin? The website setti
Charlie Harrison 2017/05/08 20:57:21 Same origin, sorry for lack of precision.
175 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url));
176 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2));
177
178 // Showing the UI should trigger a forced content setting update, but no
179 // metrics should be recorded.
180 histogram_tester().ExpectBucketCount(kActionsHistogram,
181 kActionContentSettingsAllowed, 0);
182
183 // Fast forward the clock.
184 test_clock()->Advance(
185 SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain);
186 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url));
187 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2));
188 }
189
190 // If the user manually sets a content setting to block the feature, the smart
191 // UI should be reset.
192 TEST_F(SubresourceFilterContentSettingsManagerTest,
193 SmartUIWithOverride_Resets) {
194 if (!settings_manager()->should_use_smart_ui())
195 return;
196
197 GURL url("https://example.test/");
198 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url));
199
200 settings_manager()->OnDidShowUI(url);
201
202 // Subsequent navigations to same-domains should not show UI.
203 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url));
204
205 // The user changed their mind, make sure the feature is showing up in the
206 // settings UI. i.e. the setting should be non-default.
207 EXPECT_EQ(CONTENT_SETTING_ALLOW, settings_manager()->GetSitePermission(url));
208 GetSettingsMap()->SetContentSettingDefaultScope(
209 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(),
210 CONTENT_SETTING_BLOCK);
211
212 histogram_tester().ExpectBucketCount(kActionsHistogram,
213 kActionContentSettingsBlocked, 1);
214 histogram_tester().ExpectBucketCount(kActionsHistogram,
215 kActionContentSettingsBlockedFromUI, 0);
216 histogram_tester().ExpectBucketCount(
217 kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 1);
218
219 // Smart UI should be reset.
220 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url));
221 }
222
223 TEST_F(SubresourceFilterContentSettingsManagerTest,
224 OnlyLogUserInitiatedChanges) {
225 GURL url("https://example.test/");
226 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url));
227
228 settings_manager()->OnDidShowUI(url);
229 histogram_tester().ExpectTotalCount(kActionsHistogram, 0);
230
231 // Simulate changing the setting via the infobar UI.
232 settings_manager()->SetSitePermission(url, CONTENT_SETTING_BLOCK,
233 true /* from_ui */);
234 histogram_tester().ExpectBucketCount(kActionsHistogram,
235 kActionContentSettingsBlockedFromUI, 1);
236
237 // The standard "Block" histograms are only triggered when blocking from the
238 // settings UI, not our standard UI.
239 histogram_tester().ExpectBucketCount(kActionsHistogram,
240 kActionContentSettingsBlocked, 0);
241 histogram_tester().ExpectBucketCount(
242 kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 0);
243
244 GetSettingsMap()->SetContentSettingDefaultScope(
245 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(),
246 CONTENT_SETTING_ALLOW);
247 histogram_tester().ExpectBucketCount(kActionsHistogram,
248 kActionContentSettingsAllowed, 1);
249
250 // Simulate de-activation of the site. Should not log any metrics.
251 settings_manager()->SetSitePermission(url, CONTENT_SETTING_DEFAULT,
252 false /* from_ui */);
253 histogram_tester().ExpectBucketCount(kActionsHistogram,
254 kActionContentSettingsWildcardUpdate, 0);
255 }
256
257 TEST_F(SubresourceFilterContentSettingsManagerHistoryTest,
258 HistoryUrlDeleted_ClearsWebsiteSetting) {
259 if (!settings_manager()->should_use_smart_ui())
260 return;
261
262 auto* history_service = HistoryServiceFactory::GetForProfile(
263 profile(), ServiceAccessType::EXPLICIT_ACCESS);
264 ASSERT_TRUE(history_service);
265
266 // Ensure the website setting is set.
267 GURL url("https://example.test");
268 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url));
269 settings_manager()->OnDidShowUI(url);
270
271 // Simulate adding the page to the history.
272 history_service->AddPage(url, base::Time::Now(), history::SOURCE_BROWSED);
msramek 2017/05/08 16:40:26 Please add one more URL, otherwise this test canno
273 history::BlockUntilHistoryProcessesPendingRequests(history_service);
274
275 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url));
276
277 // Deleting the URL from history should clear the setting for this URL
278 history_service->DeleteURL(url);
279 history::BlockUntilHistoryProcessesPendingRequests(history_service);
280
281 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url));
282 }
283
284 TEST_F(SubresourceFilterContentSettingsManagerHistoryTest,
285 AllHistoryUrlDeleted_ClearsWebsiteSetting) {
286 if (!settings_manager()->should_use_smart_ui())
287 return;
288
289 auto* history_service = HistoryServiceFactory::GetForProfile(
290 profile(), ServiceAccessType::EXPLICIT_ACCESS);
291 ASSERT_TRUE(history_service);
292
293 GURL url1("https://example.test");
294 GURL url2("https://example.test");
295 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url1));
296 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2));
297 settings_manager()->OnDidShowUI(url1);
298 settings_manager()->OnDidShowUI(url2);
299
300 // Simulate adding the pages to the history.
301 history_service->AddPage(url1, base::Time::Now(), history::SOURCE_BROWSED);
302 history_service->AddPage(url2, base::Time::Now(), history::SOURCE_BROWSED);
303 history::BlockUntilHistoryProcessesPendingRequests(history_service);
304
305 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url1));
306 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2));
307
308 // Deleting all the URLs should clear everything.
309 base::RunLoop run_loop;
310 base::CancelableTaskTracker task_tracker;
311 history_service->ExpireHistoryBetween(std::set<GURL>(), base::Time(),
312 base::Time(), run_loop.QuitClosure(),
313 &task_tracker);
314 run_loop.Run();
315
316 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url1));
317 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2));
318 }
319
101 } // namespace 320 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698