OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/android/preferences/important_sites_util.h" | 5 #include "chrome/browser/engagement/important_sites_util.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
13 #include "base/metrics/sample_vector.h" | 13 #include "base/metrics/sample_vector.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 CROSSED_NOTIFICATIONS_AND_ENGAGEMENT = 3, | 62 CROSSED_NOTIFICATIONS_AND_ENGAGEMENT = 3, |
63 CROSSED_REASON_UNKNOWN = 7, | 63 CROSSED_REASON_UNKNOWN = 7, |
64 }; | 64 }; |
65 | 65 |
66 } // namespace | 66 } // namespace |
67 | 67 |
68 class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { | 68 class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { |
69 public: | 69 public: |
70 void SetUp() override { | 70 void SetUp() override { |
71 ChromeRenderViewHostTestHarness::SetUp(); | 71 ChromeRenderViewHostTestHarness::SetUp(); |
| 72 SiteEngagementScore::SetParamValuesForTesting(); |
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
73 g_temp_history_dir = temp_dir_.GetPath(); | 74 g_temp_history_dir = temp_dir_.GetPath(); |
74 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 75 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
75 profile(), &BuildTestHistoryService); | 76 profile(), &BuildTestHistoryService); |
76 SiteEngagementScore::SetParamValuesForTesting(); | |
77 } | 77 } |
78 | 78 |
79 void AddContentSetting(ContentSettingsType type, | 79 void AddContentSetting(ContentSettingsType type, |
80 ContentSetting setting, | 80 ContentSetting setting, |
81 const GURL& origin) { | 81 const GURL& origin) { |
82 HostContentSettingsMapFactory::GetForProfile(profile()) | 82 HostContentSettingsMapFactory::GetForProfile(profile()) |
83 ->SetContentSettingCustomScope( | 83 ->SetContentSettingCustomScope( |
84 ContentSettingsPattern::FromURLNoWildcard(origin), | 84 ContentSettingsPattern::FromURLNoWildcard(origin), |
85 ContentSettingsPattern::Wildcard(), type, | 85 ContentSettingsPattern::Wildcard(), type, |
86 content_settings::ResourceIdentifier(), setting); | 86 content_settings::ResourceIdentifier(), setting); |
(...skipping 19 matching lines...) Expand all Loading... |
106 const std::vector<GURL>& expected_sorted_origins, | 106 const std::vector<GURL>& expected_sorted_origins, |
107 const std::vector<ImportantDomainInfo>& important_sites) { | 107 const std::vector<ImportantDomainInfo>& important_sites) { |
108 ASSERT_EQ(domains.size(), important_sites.size()); | 108 ASSERT_EQ(domains.size(), important_sites.size()); |
109 ASSERT_EQ(expected_sorted_origins.size(), important_sites.size()); | 109 ASSERT_EQ(expected_sorted_origins.size(), important_sites.size()); |
110 for (size_t i = 0; i < important_sites.size(); i++) { | 110 for (size_t i = 0; i < important_sites.size(); i++) { |
111 EXPECT_EQ(domains[i], important_sites[i].registerable_domain); | 111 EXPECT_EQ(domains[i], important_sites[i].registerable_domain); |
112 EXPECT_EQ(expected_sorted_origins[i], important_sites[i].example_origin); | 112 EXPECT_EQ(expected_sorted_origins[i], important_sites[i].example_origin); |
113 } | 113 } |
114 } | 114 } |
115 | 115 |
| 116 void ExpectImportantResultsEqualUnordered( |
| 117 const std::vector<std::string>& domains, |
| 118 const std::vector<GURL>& expected_sorted_origins, |
| 119 const std::vector<ImportantDomainInfo>& important_sites) { |
| 120 ASSERT_EQ(domains.size(), important_sites.size()); |
| 121 ASSERT_EQ(expected_sorted_origins.size(), important_sites.size()); |
| 122 |
| 123 std::vector<std::string> actual_domains; |
| 124 std::vector<GURL> actual_origins; |
| 125 for (size_t i = 0; i < important_sites.size(); i++) { |
| 126 actual_domains.push_back(important_sites[i].registerable_domain); |
| 127 actual_origins.push_back(important_sites[i].example_origin); |
| 128 } |
| 129 EXPECT_THAT(actual_domains, testing::UnorderedElementsAreArray(domains)); |
| 130 EXPECT_THAT(actual_origins, |
| 131 testing::UnorderedElementsAreArray(expected_sorted_origins)); |
| 132 } |
| 133 |
116 private: | 134 private: |
117 base::ScopedTempDir temp_dir_; | 135 base::ScopedTempDir temp_dir_; |
118 BookmarkModel* model_ = nullptr; | 136 BookmarkModel* model_ = nullptr; |
119 }; | 137 }; |
120 | 138 |
121 TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { | 139 TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { |
122 EXPECT_TRUE(ImportantSitesUtil::GetImportantRegisterableDomains( | 140 EXPECT_TRUE(ImportantSitesUtil::GetImportantRegisterableDomains( |
123 profile(), kNumImportantSites) | 141 profile(), kNumImportantSites) |
124 .empty()); | 142 .empty()); |
125 } | 143 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 EXPECT_EQ(3u, important_sites.size()); | 230 EXPECT_EQ(3u, important_sites.size()); |
213 | 231 |
214 // Add the rest, which should put us over the limit. | 232 // Add the rest, which should put us over the limit. |
215 AddBookmark(url6); | 233 AddBookmark(url6); |
216 AddBookmark(url7); | 234 AddBookmark(url7); |
217 // Too many bookmarks! Nothing shows up now. | 235 // Too many bookmarks! Nothing shows up now. |
218 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 236 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
219 profile(), kNumImportantSites); | 237 profile(), kNumImportantSites); |
220 EXPECT_EQ(0u, important_sites.size()); | 238 EXPECT_EQ(0u, important_sites.size()); |
221 | 239 |
222 // If we add some site engagement, these should show up in order (even though | 240 // If we add some site engagement, they should show up (even though the site |
223 // the engagement is too low for a signal by itself). | 241 // engagement score is too low for a signal by itself). |
224 service->ResetScoreForURL(url1, 2); | 242 service->ResetScoreForURL(url1, 2); |
225 service->ResetScoreForURL(url4, 3); | 243 service->ResetScoreForURL(url4, 3); |
226 service->ResetScoreForURL(url7, 0); | 244 service->ResetScoreForURL(url7, 0); |
227 | 245 |
228 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 246 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
229 profile(), kNumImportantSites); | 247 profile(), kNumImportantSites); |
230 ASSERT_EQ(2u, important_sites.size()); | 248 ASSERT_EQ(2u, important_sites.size()); |
231 std::vector<std::string> expected_sorted_domains = {"google.com", | 249 std::vector<std::string> expected_sorted_domains = {"google.com", |
232 "chrome.com"}; | 250 "chrome.com"}; |
233 std::vector<GURL> expected_sorted_origins = {url1, url4}; | 251 std::vector<GURL> expected_sorted_origins = {url1, url4}; |
234 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, | 252 ExpectImportantResultsEqualUnordered( |
235 important_sites); | 253 expected_sorted_domains, expected_sorted_origins, important_sites); |
236 } | 254 } |
237 | 255 |
238 TEST_F(ImportantSitesUtilTest, Blacklisting) { | 256 TEST_F(ImportantSitesUtilTest, Blacklisting) { |
239 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 257 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
240 ASSERT_TRUE(service); | 258 ASSERT_TRUE(service); |
241 | 259 |
242 GURL url1("http://www.google.com/"); | 260 GURL url1("http://www.google.com/"); |
243 | 261 |
244 // Set a bunch of positive signals. | 262 // Set a bunch of positive signals. |
245 service->ResetScoreForURL(url1, 5); | 263 service->ResetScoreForURL(url1, 5); |
246 AddBookmark(url1); | 264 AddBookmark(url1); |
247 AddContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW, | 265 AddContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW, |
248 url1); | 266 url1); |
249 | 267 |
250 // Important fetch 1. | 268 // Important fetch 1. |
251 std::vector<ImportantDomainInfo> important_sites = | 269 std::vector<ImportantDomainInfo> important_sites = |
252 ImportantSitesUtil::GetImportantRegisterableDomains(profile(), | 270 ImportantSitesUtil::GetImportantRegisterableDomains(profile(), |
253 kNumImportantSites); | 271 kNumImportantSites); |
254 std::vector<std::string> expected_sorted_domains = {"google.com"}; | 272 std::vector<std::string> expected_sorted_domains = {"google.com"}; |
255 std::vector<GURL> expected_sorted_origins = {url1}; | 273 std::vector<GURL> expected_sorted_origins = {url1}; |
256 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, | 274 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, |
257 important_sites); | 275 important_sites); |
258 ASSERT_EQ(1u, important_sites.size()); | 276 ASSERT_EQ(1u, important_sites.size()); |
259 // Record ignore twice. | 277 // Record ignore twice. |
260 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 278 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
261 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 279 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 280 {"google.com"}, {important_sites[0].reason_bitfield}); |
262 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 281 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
263 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 282 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 283 {"google.com"}, {important_sites[0].reason_bitfield}); |
264 | 284 |
265 // Important fetch 2. | 285 // Important fetch 2. |
266 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 286 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
267 profile(), kNumImportantSites); | 287 profile(), kNumImportantSites); |
268 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, | 288 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, |
269 important_sites); | 289 important_sites); |
270 // We shouldn't blacklist after first two times. | 290 // We shouldn't blacklist after first two times. |
271 ASSERT_EQ(1u, important_sites.size()); | 291 ASSERT_EQ(1u, important_sites.size()); |
272 | 292 |
273 // Record ignore 3rd time. | 293 // Record ignore 3rd time. |
274 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 294 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
275 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 295 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 296 {"google.com"}, {important_sites[0].reason_bitfield}); |
276 | 297 |
277 // Important fetch 3. We should be blacklisted now. | 298 // Important fetch 3. We should be blacklisted now. |
278 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 299 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
279 profile(), kNumImportantSites); | 300 profile(), kNumImportantSites); |
280 ASSERT_EQ(0u, important_sites.size()); | 301 ASSERT_EQ(0u, important_sites.size()); |
281 } | 302 } |
282 | 303 |
283 TEST_F(ImportantSitesUtilTest, BlacklistingReset) { | 304 TEST_F(ImportantSitesUtilTest, BlacklistingReset) { |
284 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 305 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
285 ASSERT_TRUE(service); | 306 ASSERT_TRUE(service); |
286 | 307 |
287 GURL url1("http://www.google.com/"); | 308 GURL url1("http://www.google.com/"); |
288 | 309 |
289 // Set a bunch of positive signals. | 310 // Set a bunch of positive signals. |
290 service->ResetScoreForURL(url1, 5); | 311 service->ResetScoreForURL(url1, 5); |
291 AddBookmark(url1); | 312 AddBookmark(url1); |
292 AddContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW, | 313 AddContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW, |
293 url1); | 314 url1); |
294 | 315 |
295 std::vector<ImportantDomainInfo> important_sites = | 316 std::vector<ImportantDomainInfo> important_sites = |
296 ImportantSitesUtil::GetImportantRegisterableDomains(profile(), | 317 ImportantSitesUtil::GetImportantRegisterableDomains(profile(), |
297 kNumImportantSites); | 318 kNumImportantSites); |
298 | 319 |
299 // Record ignored twice. | 320 // Record ignored twice. |
300 ASSERT_EQ(1u, important_sites.size()); | 321 ASSERT_EQ(1u, important_sites.size()); |
301 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 322 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
302 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 323 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 324 {"google.com"}, {important_sites[0].reason_bitfield}); |
303 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 325 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
304 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 326 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 327 {"google.com"}, {important_sites[0].reason_bitfield}); |
305 | 328 |
306 // Important fetch, we should still be there. | 329 // Important fetch, we should still be there. |
307 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 330 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
308 profile(), kNumImportantSites); | 331 profile(), kNumImportantSites); |
309 std::vector<std::string> expected_sorted_domains = {"google.com"}; | 332 std::vector<std::string> expected_sorted_domains = {"google.com"}; |
310 std::vector<GURL> expected_sorted_origins = {url1}; | 333 std::vector<GURL> expected_sorted_origins = {url1}; |
311 ASSERT_EQ(1u, important_sites.size()); | 334 ASSERT_EQ(1u, important_sites.size()); |
312 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, | 335 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, |
313 important_sites); | 336 important_sites); |
314 | 337 |
315 // Record NOT ignored. | 338 // Record NOT ignored. |
316 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 339 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
317 profile(), {"google.com"}, {important_sites[0].reason_bitfield}, {}, {}); | 340 profile(), {"google.com"}, {important_sites[0].reason_bitfield}, |
| 341 std::vector<std::string>(), std::vector<int32_t>()); |
318 | 342 |
319 // Record ignored twice again. | 343 // Record ignored twice again. |
320 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 344 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
321 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 345 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 346 {"google.com"}, {important_sites[0].reason_bitfield}); |
322 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 347 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
323 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 348 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 349 {"google.com"}, {important_sites[0].reason_bitfield}); |
324 | 350 |
325 // Important fetch, we should still be there. | 351 // Important fetch, we should still be there. |
326 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 352 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
327 profile(), kNumImportantSites); | 353 profile(), kNumImportantSites); |
328 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, | 354 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, |
329 important_sites); | 355 important_sites); |
330 | 356 |
331 // Record ignored 3rd time in a row. | 357 // Record ignored 3rd time in a row. |
332 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 358 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
333 profile(), {}, {}, {"google.com"}, {important_sites[0].reason_bitfield}); | 359 profile(), std::vector<std::string>(), std::vector<int32_t>(), |
| 360 {"google.com"}, {important_sites[0].reason_bitfield}); |
334 | 361 |
335 // Blacklisted now. | 362 // Blacklisted now. |
336 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 363 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
337 profile(), kNumImportantSites); | 364 profile(), kNumImportantSites); |
338 EXPECT_EQ(0u, important_sites.size()); | 365 EXPECT_EQ(0u, important_sites.size()); |
339 } | 366 } |
340 | 367 |
341 TEST_F(ImportantSitesUtilTest, Metrics) { | 368 TEST_F(ImportantSitesUtilTest, Metrics) { |
342 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 369 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
343 ASSERT_TRUE(service); | 370 ASSERT_TRUE(service); |
(...skipping 12 matching lines...) Expand all Loading... |
356 | 383 |
357 std::vector<ImportantDomainInfo> important_sites = | 384 std::vector<ImportantDomainInfo> important_sites = |
358 ImportantSitesUtil::GetImportantRegisterableDomains(profile(), | 385 ImportantSitesUtil::GetImportantRegisterableDomains(profile(), |
359 kNumImportantSites); | 386 kNumImportantSites); |
360 | 387 |
361 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 388 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
362 profile(), {"google.com", "youtube.com"}, | 389 profile(), {"google.com", "youtube.com"}, |
363 {important_sites[0].reason_bitfield, important_sites[1].reason_bitfield}, | 390 {important_sites[0].reason_bitfield, important_sites[1].reason_bitfield}, |
364 {"bad.com"}, {important_sites[2].reason_bitfield}); | 391 {"bad.com"}, {important_sites[2].reason_bitfield}); |
365 | 392 |
366 EXPECT_THAT(histogram_tester.GetAllSamples( | 393 EXPECT_THAT( |
367 "Storage.ImportantSites.CBDChosenReason"), | 394 histogram_tester.GetAllSamples("Storage.ImportantSites.CBDChosenReason"), |
368 testing::ElementsAre( | 395 testing::ElementsAre(base::Bucket(ENGAGEMENT, 1), |
369 base::Bucket(ENGAGEMENT, 1), | 396 base::Bucket(BOOKMARKS, 1), |
370 base::Bucket(BOOKMARKS, 1), | 397 base::Bucket(NOTIFICATIONS, 1))); |
371 base::Bucket(NOTIFICATIONS, 1))); | |
372 | 398 |
373 EXPECT_THAT( | 399 EXPECT_THAT( |
374 histogram_tester.GetAllSamples("Storage.ImportantSites.CBDIgnoredReason"), | 400 histogram_tester.GetAllSamples("Storage.ImportantSites.CBDIgnoredReason"), |
375 testing::ElementsAre(base::Bucket(BOOKMARKS, 1))); | 401 testing::ElementsAre(base::Bucket(BOOKMARKS, 1))); |
376 | 402 |
377 // Bookmarks are "unknown", as they were added after the crossed reasons. | 403 // Bookmarks are "unknown", as they were added after the crossed reasons. |
378 EXPECT_THAT(histogram_tester.GetAllSamples( | 404 EXPECT_THAT(histogram_tester.GetAllSamples( |
379 "Storage.BlacklistedImportantSites.Reason"), | 405 "Storage.BlacklistedImportantSites.Reason"), |
380 testing::ElementsAre( | 406 testing::ElementsAre( |
381 base::Bucket(CROSSED_NOTIFICATIONS_AND_ENGAGEMENT, 1), | 407 base::Bucket(CROSSED_NOTIFICATIONS_AND_ENGAGEMENT, 1), |
382 base::Bucket(CROSSED_REASON_UNKNOWN, 1))); | 408 base::Bucket(CROSSED_REASON_UNKNOWN, 1))); |
383 } | 409 } |
OLD | NEW |