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

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

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