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

Side by Side Diff: chrome/browser/engagement/site_engagement_service_unittest.cc

Issue 1901563002: Set site engagement timestamps to privacy-respectful values when history is cleared. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: scoped_ptr is now unique_ptr Created 4 years, 8 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 2015 The Chromium Authors. All rights reserved. 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 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/engagement/site_engagement_service.h" 5 #include "chrome/browser/engagement/site_engagement_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 TEST_F(SiteEngagementScoreTest, Reset) { 426 TEST_F(SiteEngagementScoreTest, Reset) {
427 base::Time current_day = GetReferenceTime(); 427 base::Time current_day = GetReferenceTime();
428 428
429 test_clock_.SetNow(current_day); 429 test_clock_.SetNow(current_day);
430 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); 430 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
431 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.Score()); 431 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.Score());
432 432
433 current_day += base::TimeDelta::FromDays(7); 433 current_day += base::TimeDelta::FromDays(7);
434 test_clock_.SetNow(current_day); 434 test_clock_.SetNow(current_day);
435 435
436 score_.Reset(20.0); 436 score_.Reset(20.0, nullptr);
437 EXPECT_DOUBLE_EQ(20.0, score_.Score()); 437 EXPECT_DOUBLE_EQ(20.0, score_.Score());
438 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); 438 EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
439 EXPECT_EQ(current_day, score_.last_engagement_time_); 439 EXPECT_EQ(current_day, score_.last_engagement_time_);
440 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());
440 441
441 // Adding points after the reset should work as normal. 442 // Adding points after the reset should work as normal.
442 score_.AddPoints(5); 443 score_.AddPoints(5);
443 EXPECT_EQ(25.0, score_.Score()); 444 EXPECT_EQ(25.0, score_.Score());
444 445
445 // The decay should happen one decay period from 446 // The decay should happen one decay period from the current time.
446 test_clock_.SetNow(current_day + 447 test_clock_.SetNow(current_day +
447 base::TimeDelta::FromDays( 448 base::TimeDelta::FromDays(
448 SiteEngagementScore::GetDecayPeriodInDays() + 1)); 449 SiteEngagementScore::GetDecayPeriodInDays() + 1));
449 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.Score()); 450 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.Score());
451
452 // Ensure that manually setting a time works as expected.
453 score_.AddPoints(5);
454 test_clock_.SetNow(GetReferenceTime());
455 base::Time now = test_clock_.Now();
456 score_.Reset(10.0, &now);
457
458 EXPECT_DOUBLE_EQ(10.0, score_.Score());
459 EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
460 EXPECT_EQ(now, score_.last_engagement_time_);
461 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());
462
463 score_.set_last_shortcut_launch_time(test_clock_.Now());
464 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3));
465 now = test_clock_.Now();
466 score_.Reset(15.0, &now);
467
468 // 5 bonus from the last shortcut launch.
469 EXPECT_DOUBLE_EQ(20.0, score_.Score());
470 EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
471 EXPECT_EQ(now, score_.last_engagement_time_);
472 EXPECT_EQ(now, score_.last_shortcut_launch_time_);
450 } 473 }
451 474
452 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { 475 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness {
453 public: 476 public:
454 void SetUp() override { 477 void SetUp() override {
455 ChromeRenderViewHostTestHarness::SetUp(); 478 ChromeRenderViewHostTestHarness::SetUp();
456 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 479 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
457 g_temp_history_dir = temp_dir_.path(); 480 g_temp_history_dir = temp_dir_.path();
458 HistoryServiceFactory::GetInstance()->SetTestingFactory( 481 HistoryServiceFactory::GetInstance()->SetTestingFactory(
459 profile(), &BuildTestHistoryService); 482 profile(), &BuildTestHistoryService);
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after
1097 EXPECT_FALSE(service->IsBootstrapped()); 1120 EXPECT_FALSE(service->IsBootstrapped());
1098 1121
1099 service->AddPoints(url2, 5.0); 1122 service->AddPoints(url2, 5.0);
1100 EXPECT_TRUE(service->IsBootstrapped()); 1123 EXPECT_TRUE(service->IsBootstrapped());
1101 1124
1102 clock->SetNow(current_day + base::TimeDelta::FromDays(10)); 1125 clock->SetNow(current_day + base::TimeDelta::FromDays(10));
1103 EXPECT_FALSE(service->IsBootstrapped()); 1126 EXPECT_FALSE(service->IsBootstrapped());
1104 } 1127 }
1105 1128
1106 TEST_F(SiteEngagementServiceTest, CleanupOriginsOnHistoryDeletion) { 1129 TEST_F(SiteEngagementServiceTest, CleanupOriginsOnHistoryDeletion) {
1107 SiteEngagementService* engagement = 1130 base::SimpleTestClock* clock = new base::SimpleTestClock();
1108 SiteEngagementServiceFactory::GetForProfile(profile()); 1131 std::unique_ptr<SiteEngagementService> engagement(
1109 ASSERT_TRUE(engagement); 1132 new SiteEngagementService(profile(), base::WrapUnique(clock)));
1133 ASSERT_TRUE(engagement.get());
1110 1134
1111 GURL origin1("http://www.google.com/"); 1135 GURL origin1("http://www.google.com/");
1112 GURL origin1a("http://www.google.com/search?q=asdf"); 1136 GURL origin1a("http://www.google.com/search?q=asdf");
1137 GURL origin1b("http://www.google.com/maps/search?q=asdf");
1113 GURL origin2("https://drive.google.com/"); 1138 GURL origin2("https://drive.google.com/");
1114 GURL origin2a("https://drive.google.com/somedoc"); 1139 GURL origin2a("https://drive.google.com/somedoc");
1115 GURL origin3("http://notdeleted.com/"); 1140 GURL origin3("http://notdeleted.com/");
1141 GURL origin4("http://decayed.com/");
1142 GURL origin4a("http://decayed.com/index.html");
1116 1143
1117 base::Time today = GetReferenceTime(); 1144 base::Time today = GetReferenceTime();
1118 base::Time yesterday = GetReferenceTime() - base::TimeDelta::FromDays(1); 1145 base::Time yesterday = GetReferenceTime() - base::TimeDelta::FromDays(1);
1119 base::Time yesterday_afternoon = GetReferenceTime() - 1146 base::Time yesterday_afternoon = GetReferenceTime() -
1120 base::TimeDelta::FromDays(1) + 1147 base::TimeDelta::FromDays(1) +
1121 base::TimeDelta::FromHours(4); 1148 base::TimeDelta::FromHours(4);
1149 base::Time yesterday_week = GetReferenceTime() - base::TimeDelta::FromDays(8);
1150 clock->SetNow(today);
1122 1151
1123 history::HistoryService* history = HistoryServiceFactory::GetForProfile( 1152 history::HistoryService* history = HistoryServiceFactory::GetForProfile(
1124 profile(), ServiceAccessType::IMPLICIT_ACCESS); 1153 profile(), ServiceAccessType::IMPLICIT_ACCESS);
1125 1154
1126 history->AddPage(origin1, yesterday_afternoon, history::SOURCE_BROWSED); 1155 history->AddPage(origin1, yesterday_afternoon, history::SOURCE_BROWSED);
1127 history->AddPage(origin1a, today, history::SOURCE_BROWSED); 1156 history->AddPage(origin1a, yesterday_week, history::SOURCE_BROWSED);
1128 engagement->AddPoints(origin1, 5.0); 1157 history->AddPage(origin1b, today, history::SOURCE_BROWSED);
1158 engagement->AddPoints(origin1, 3.0);
1129 1159
1130 history->AddPage(origin2, yesterday_afternoon, history::SOURCE_BROWSED); 1160 history->AddPage(origin2, yesterday_afternoon, history::SOURCE_BROWSED);
1131 history->AddPage(origin2a, yesterday_afternoon, history::SOURCE_BROWSED); 1161 history->AddPage(origin2a, yesterday_afternoon, history::SOURCE_BROWSED);
1132 engagement->AddPoints(origin2, 5.0); 1162 engagement->AddPoints(origin2, 5.0);
1133 1163
1134 history->AddPage(origin3, today, history::SOURCE_BROWSED); 1164 history->AddPage(origin3, today, history::SOURCE_BROWSED);
1135 engagement->AddPoints(origin3, 5.0); 1165 engagement->AddPoints(origin3, 5.0);
1136 1166
1137 EXPECT_EQ(5.0, engagement->GetScore(origin1)); 1167 history->AddPage(origin4, yesterday_week, history::SOURCE_BROWSED);
1168 history->AddPage(origin4a, yesterday_afternoon, history::SOURCE_BROWSED);
1169 engagement->AddPoints(origin4, 5.0);
1170
1171 EXPECT_EQ(3.0, engagement->GetScore(origin1));
1138 EXPECT_EQ(5.0, engagement->GetScore(origin2)); 1172 EXPECT_EQ(5.0, engagement->GetScore(origin2));
1139 EXPECT_EQ(5.0, engagement->GetScore(origin3)); 1173 EXPECT_EQ(5.0, engagement->GetScore(origin3));
1174 EXPECT_EQ(5.0, engagement->GetScore(origin4));
1140 1175
1141 { 1176 {
1142 SiteEngagementChangeWaiter waiter(profile()); 1177 SiteEngagementChangeWaiter waiter(profile());
1143 1178
1144 base::CancelableTaskTracker task_tracker; 1179 base::CancelableTaskTracker task_tracker;
1145 // Expire origin1, origin2 and origin2a. 1180 // Expire origin1, origin2, origin2a, and origin4's most recent visit.
1146 history->ExpireHistoryBetween(std::set<GURL>(), yesterday, today, 1181 history->ExpireHistoryBetween(std::set<GURL>(), yesterday, today,
1147 base::Bind(&base::DoNothing), &task_tracker); 1182 base::Bind(&base::DoNothing), &task_tracker);
1148 waiter.Wait(); 1183 waiter.Wait();
1149 1184
1150 // origin2 is cleaned up because all its urls are deleted. origin1a is still 1185 // origin2 is cleaned up because all its urls are deleted. origin1a and
1151 // in history, but 50% of urls have been deleted, thus halving origin1's 1186 // origin1b are still in history, but 33% of urls have been deleted, thus
1152 // score. origin3 is untouched. 1187 // cutting origin1's score by 1/3. origin3 is untouched. origin4 has 1 URL
1153 EXPECT_EQ(2.5, engagement->GetScore(origin1)); 1188 // deleted and 1 remaining, but its most recent visit is more than 1 week in
1189 // the past. Ensure that its scored is halved, and not decayed further.
1190 EXPECT_EQ(2, engagement->GetScore(origin1));
1154 EXPECT_EQ(0, engagement->GetScore(origin2)); 1191 EXPECT_EQ(0, engagement->GetScore(origin2));
1155 EXPECT_EQ(5.0, engagement->GetScore(origin3)); 1192 EXPECT_EQ(5.0, engagement->GetScore(origin3));
1156 EXPECT_EQ(7.5, engagement->GetTotalEngagementPoints()); 1193 EXPECT_EQ(2.5, engagement->GetScore(origin4));
1194 EXPECT_EQ(9.5, engagement->GetTotalEngagementPoints());
1157 } 1195 }
1158 1196
1159 { 1197 {
1160 SiteEngagementChangeWaiter waiter(profile()); 1198 SiteEngagementChangeWaiter waiter(profile());
1161 1199
1162 // Expire origin1a. 1200 // Expire origin1a.
1163 std::vector<history::ExpireHistoryArgs> expire_list; 1201 std::vector<history::ExpireHistoryArgs> expire_list;
1164 history::ExpireHistoryArgs args; 1202 history::ExpireHistoryArgs args;
1165 args.urls.insert(origin1a); 1203 args.urls.insert(origin1a);
1204 args.SetTimeRangeForOneDay(yesterday_week);
1205 expire_list.push_back(args);
1206
1207 base::CancelableTaskTracker task_tracker;
1208 history->ExpireHistory(expire_list, base::Bind(&base::DoNothing),
1209 &task_tracker);
1210 waiter.Wait();
1211
1212 // origin1's score should be halved again. origin3 and origin4 remain
1213 // untouched.
1214 EXPECT_EQ(1, engagement->GetScore(origin1));
1215 EXPECT_EQ(0, engagement->GetScore(origin2));
1216 EXPECT_EQ(5.0, engagement->GetScore(origin3));
1217 EXPECT_EQ(2.5, engagement->GetScore(origin4));
1218 EXPECT_EQ(8.5, engagement->GetTotalEngagementPoints());
1219 }
1220
1221 {
1222 SiteEngagementChangeWaiter waiter(profile());
1223
1224 // Expire origin1b.
1225 std::vector<history::ExpireHistoryArgs> expire_list;
1226 history::ExpireHistoryArgs args;
1227 args.urls.insert(origin1b);
1166 args.SetTimeRangeForOneDay(today); 1228 args.SetTimeRangeForOneDay(today);
1167 expire_list.push_back(args); 1229 expire_list.push_back(args);
1168 1230
1169 base::CancelableTaskTracker task_tracker; 1231 base::CancelableTaskTracker task_tracker;
1170 history->ExpireHistory(expire_list, base::Bind(&base::DoNothing), 1232 history->ExpireHistory(expire_list, base::Bind(&base::DoNothing),
1171 &task_tracker); 1233 &task_tracker);
1172 waiter.Wait(); 1234 waiter.Wait();
1173 1235
1174 // Only origin3 remains. 1236 // origin1 should be removed. origin3 and origin4 remain untouched.
1175 EXPECT_EQ(0, engagement->GetScore(origin1)); 1237 EXPECT_EQ(0, engagement->GetScore(origin1));
1176 EXPECT_EQ(0, engagement->GetScore(origin2)); 1238 EXPECT_EQ(0, engagement->GetScore(origin2));
1177 EXPECT_EQ(5.0, engagement->GetScore(origin3)); 1239 EXPECT_EQ(5.0, engagement->GetScore(origin3));
1178 EXPECT_EQ(5.0, engagement->GetTotalEngagementPoints()); 1240 EXPECT_EQ(2.5, engagement->GetScore(origin4));
1241 EXPECT_EQ(7.5, engagement->GetTotalEngagementPoints());
1179 } 1242 }
1180 } 1243 }
1181 1244
1182 TEST_F(SiteEngagementServiceTest, EngagementLevel) { 1245 TEST_F(SiteEngagementServiceTest, EngagementLevel) {
1183 static_assert(SiteEngagementService::ENGAGEMENT_LEVEL_NONE != 1246 static_assert(SiteEngagementService::ENGAGEMENT_LEVEL_NONE !=
1184 SiteEngagementService::ENGAGEMENT_LEVEL_LOW, 1247 SiteEngagementService::ENGAGEMENT_LEVEL_LOW,
1185 "enum values should not be equal"); 1248 "enum values should not be equal");
1186 static_assert(SiteEngagementService::ENGAGEMENT_LEVEL_LOW != 1249 static_assert(SiteEngagementService::ENGAGEMENT_LEVEL_LOW !=
1187 SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM, 1250 SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM,
1188 "enum values should not be equal"); 1251 "enum values should not be equal");
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 0, 2); 1437 0, 2);
1375 1438
1376 // Add more points and ensure no more samples are present. 1439 // Add more points and ensure no more samples are present.
1377 service->AddPoints(origin1, 0.01); 1440 service->AddPoints(origin1, 0.01);
1378 service->AddPoints(origin2, 0.01); 1441 service->AddPoints(origin2, 0.01);
1379 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, 1442 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram,
1380 4); 1443 4);
1381 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1444 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1382 4); 1445 4);
1383 } 1446 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.cc ('k') | components/history/core/browser/history_backend.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698