OLD | NEW |
---|---|
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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 public: | 149 public: |
150 void SetUp() override { | 150 void SetUp() override { |
151 ChromeRenderViewHostTestHarness::SetUp(); | 151 ChromeRenderViewHostTestHarness::SetUp(); |
152 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 152 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
153 g_temp_history_dir = temp_dir_.GetPath(); | 153 g_temp_history_dir = temp_dir_.GetPath(); |
154 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 154 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
155 profile(), &BuildTestHistoryService); | 155 profile(), &BuildTestHistoryService); |
156 SiteEngagementScore::SetParamValuesForTesting(); | 156 SiteEngagementScore::SetParamValuesForTesting(); |
157 } | 157 } |
158 | 158 |
159 void TearDown() override { | |
160 if (service_with_test_clock_) { | |
161 service_with_test_clock_->Shutdown(); | |
162 service_with_test_clock_.reset(); | |
163 } | |
164 ChromeRenderViewHostTestHarness::TearDown(); | |
165 } | |
166 | |
159 void NavigateWithTransitionAndExpectHigherScore( | 167 void NavigateWithTransitionAndExpectHigherScore( |
160 SiteEngagementService* service, | 168 SiteEngagementService* service, |
161 const GURL& url, | 169 const GURL& url, |
162 ui::PageTransition transition) { | 170 ui::PageTransition transition) { |
163 double prev_score = service->GetScore(url); | 171 double prev_score = service->GetScore(url); |
164 controller().LoadURL(url, content::Referrer(), transition, std::string()); | 172 controller().LoadURL(url, content::Referrer(), transition, std::string()); |
165 int pending_id = controller().GetPendingEntry()->GetUniqueID(); | 173 int pending_id = controller().GetPendingEntry()->GetUniqueID(); |
166 content::WebContentsTester::For(web_contents()) | 174 content::WebContentsTester::For(web_contents()) |
167 ->TestDidNavigate(web_contents()->GetMainFrame(), pending_id, true, | 175 ->TestDidNavigate(web_contents()->GetMainFrame(), pending_id, true, |
168 url, transition); | 176 url, transition); |
(...skipping 29 matching lines...) Expand all Loading... | |
198 base::RunLoop run_loop; | 206 base::RunLoop run_loop; |
199 content::BrowserThread::PostTaskAndReply( | 207 content::BrowserThread::PostTaskAndReply( |
200 thread_id, FROM_HERE, | 208 thread_id, FROM_HERE, |
201 base::Bind(&SiteEngagementServiceTest::CheckScoreFromSettings, | 209 base::Bind(&SiteEngagementServiceTest::CheckScoreFromSettings, |
202 base::Unretained(this), settings_map, url, &score), | 210 base::Unretained(this), settings_map, url, &score), |
203 run_loop.QuitClosure()); | 211 run_loop.QuitClosure()); |
204 run_loop.Run(); | 212 run_loop.Run(); |
205 return score; | 213 return score; |
206 } | 214 } |
207 | 215 |
216 void CreateServiceWithTestClock(SiteEngagementService** service, | |
benwells
2017/03/21 23:44:51
Thanks for factoring out this too. We can delete a
sense (YandexTeam)
2017/03/22 07:38:21
Okay, I think we can do this.
| |
217 base::SimpleTestClock** clock) { | |
218 DCHECK(service && clock); | |
219 DCHECK(!service_with_test_clock_); | |
220 (*clock) = new base::SimpleTestClock(); | |
221 (*service) = new SiteEngagementService(profile(), base::WrapUnique(*clock)); | |
222 service_with_test_clock_ = base::WrapUnique(*service); | |
223 } | |
224 | |
208 private: | 225 private: |
209 void CheckScoreFromSettings(HostContentSettingsMap* settings_map, | 226 void CheckScoreFromSettings(HostContentSettingsMap* settings_map, |
210 const GURL& url, | 227 const GURL& url, |
211 double *score) { | 228 double *score) { |
212 *score = SiteEngagementService::GetScoreFromSettings(settings_map, url); | 229 *score = SiteEngagementService::GetScoreFromSettings(settings_map, url); |
213 } | 230 } |
214 | 231 |
215 base::ScopedTempDir temp_dir_; | 232 base::ScopedTempDir temp_dir_; |
233 std::unique_ptr<SiteEngagementService> service_with_test_clock_; | |
216 }; | 234 }; |
217 | 235 |
218 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { | 236 TEST_F(SiteEngagementServiceTest, GetMedianEngagement) { |
219 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 237 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
220 ASSERT_TRUE(service); | 238 ASSERT_TRUE(service); |
221 | 239 |
222 GURL url1("http://www.google.com/"); | 240 GURL url1("http://www.google.com/"); |
223 GURL url2("https://www.google.com/"); | 241 GURL url2("https://www.google.com/"); |
224 GURL url3("https://drive.google.com/"); | 242 GURL url3("https://drive.google.com/"); |
225 GURL url4("https://maps.google.com/"); | 243 GURL url4("https://maps.google.com/"); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 service->HandleMediaPlaying(web_contents(), true); | 432 service->HandleMediaPlaying(web_contents(), true); |
415 EXPECT_EQ(0, service->GetScore(url3)); | 433 EXPECT_EQ(0, service->GetScore(url3)); |
416 | 434 |
417 NavigateAndCommit(url4); | 435 NavigateAndCommit(url4); |
418 service->HandleUserInput(web_contents(), | 436 service->HandleUserInput(web_contents(), |
419 SiteEngagementMetrics::ENGAGEMENT_KEYPRESS); | 437 SiteEngagementMetrics::ENGAGEMENT_KEYPRESS); |
420 EXPECT_EQ(0, service->GetScore(url4)); | 438 EXPECT_EQ(0, service->GetScore(url4)); |
421 } | 439 } |
422 | 440 |
423 TEST_F(SiteEngagementServiceTest, LastShortcutLaunch) { | 441 TEST_F(SiteEngagementServiceTest, LastShortcutLaunch) { |
424 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 442 SiteEngagementService* service; |
425 std::unique_ptr<SiteEngagementService> service( | 443 base::SimpleTestClock* clock; |
426 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 444 CreateServiceWithTestClock(&service, &clock); |
427 | 445 |
428 base::HistogramTester histograms; | 446 base::HistogramTester histograms; |
429 | 447 |
430 base::Time current_day = GetReferenceTime(); | 448 base::Time current_day = GetReferenceTime(); |
431 clock->SetNow(current_day - base::TimeDelta::FromDays(5)); | 449 clock->SetNow(current_day - base::TimeDelta::FromDays(5)); |
432 | 450 |
433 // The https and http versions of www.google.com should be separate. But | 451 // The https and http versions of www.google.com should be separate. But |
434 // different paths on the same origin should be treated the same. | 452 // different paths on the same origin should be treated the same. |
435 GURL url1("https://www.google.com/"); | 453 GURL url1("https://www.google.com/"); |
436 GURL url2("http://www.google.com/"); | 454 GURL url2("http://www.google.com/"); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
478 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(10)); | 496 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(10)); |
479 EXPECT_DOUBLE_EQ(1.0, service->GetScore(url1)); | 497 EXPECT_DOUBLE_EQ(1.0, service->GetScore(url1)); |
480 EXPECT_DOUBLE_EQ(5.0, service->GetScore(url2)); | 498 EXPECT_DOUBLE_EQ(5.0, service->GetScore(url2)); |
481 | 499 |
482 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(11)); | 500 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(11)); |
483 EXPECT_DOUBLE_EQ(1.0, service->GetScore(url1)); | 501 EXPECT_DOUBLE_EQ(1.0, service->GetScore(url1)); |
484 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url2)); | 502 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url2)); |
485 } | 503 } |
486 | 504 |
487 TEST_F(SiteEngagementServiceTest, NotificationPermission) { | 505 TEST_F(SiteEngagementServiceTest, NotificationPermission) { |
488 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 506 SiteEngagementService* service; |
489 std::unique_ptr<SiteEngagementService> service( | 507 base::SimpleTestClock* clock; |
490 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 508 CreateServiceWithTestClock(&service, &clock); |
491 | 509 |
492 GURL url1("https://www.google.com/"); | 510 GURL url1("https://www.google.com/"); |
493 GURL url2("http://www.google.com/"); | 511 GURL url2("http://www.google.com/"); |
494 GURL url3("https://drive.google.com/"); | 512 GURL url3("https://drive.google.com/"); |
495 clock->SetNow(GetReferenceTime()); | 513 clock->SetNow(GetReferenceTime()); |
496 | 514 |
497 EXPECT_EQ(0, service->GetScore(url1)); | 515 EXPECT_EQ(0, service->GetScore(url1)); |
498 EXPECT_EQ(0, service->GetScore(url2)); | 516 EXPECT_EQ(0, service->GetScore(url2)); |
499 EXPECT_EQ(0, service->GetScore(url3)); | 517 EXPECT_EQ(0, service->GetScore(url3)); |
500 | 518 |
(...skipping 24 matching lines...) Expand all Loading... | |
525 settings_map->SetContentSettingDefaultScope( | 543 settings_map->SetContentSettingDefaultScope( |
526 url1, url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), | 544 url1, url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
527 CONTENT_SETTING_BLOCK); | 545 CONTENT_SETTING_BLOCK); |
528 | 546 |
529 EXPECT_EQ(1, service->GetScore(url1)); | 547 EXPECT_EQ(1, service->GetScore(url1)); |
530 } | 548 } |
531 | 549 |
532 TEST_F(SiteEngagementServiceTest, CheckHistograms) { | 550 TEST_F(SiteEngagementServiceTest, CheckHistograms) { |
533 base::HistogramTester histograms; | 551 base::HistogramTester histograms; |
534 | 552 |
535 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 553 SiteEngagementService* service; |
536 std::unique_ptr<SiteEngagementService> service( | 554 base::SimpleTestClock* clock; |
537 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 555 CreateServiceWithTestClock(&service, &clock); |
538 | 556 |
539 base::Time current_day = GetReferenceTime(); | 557 base::Time current_day = GetReferenceTime(); |
540 clock->SetNow(current_day); | 558 clock->SetNow(current_day); |
541 | 559 |
542 // Histograms should start empty as the testing SiteEngagementService | 560 // Histograms should start empty as the testing SiteEngagementService |
543 // constructor does not record metrics. | 561 // constructor does not record metrics. |
544 histograms.ExpectTotalCount(SiteEngagementMetrics::kTotalEngagementHistogram, | 562 histograms.ExpectTotalCount(SiteEngagementMetrics::kTotalEngagementHistogram, |
545 0); | 563 0); |
546 histograms.ExpectTotalCount(SiteEngagementMetrics::kTotalOriginsHistogram, 0); | 564 histograms.ExpectTotalCount(SiteEngagementMetrics::kTotalOriginsHistogram, 0); |
547 histograms.ExpectTotalCount(SiteEngagementMetrics::kMeanEngagementHistogram, | 565 histograms.ExpectTotalCount(SiteEngagementMetrics::kMeanEngagementHistogram, |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
815 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 33, 1); | 833 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 33, 1); |
816 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 66, 1); | 834 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 66, 1); |
817 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 33, 1); | 835 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 33, 1); |
818 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 66, 1); | 836 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 66, 1); |
819 } | 837 } |
820 | 838 |
821 // Expect that sites that have reached zero engagement are cleaned up. Expect | 839 // Expect that sites that have reached zero engagement are cleaned up. Expect |
822 // engagement times to be reset if too much time has passed since the last | 840 // engagement times to be reset if too much time has passed since the last |
823 // engagement. | 841 // engagement. |
824 TEST_F(SiteEngagementServiceTest, CleanupEngagementScores) { | 842 TEST_F(SiteEngagementServiceTest, CleanupEngagementScores) { |
825 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 843 SiteEngagementService* service; |
826 std::unique_ptr<SiteEngagementService> service( | 844 base::SimpleTestClock* clock; |
827 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 845 CreateServiceWithTestClock(&service, &clock); |
828 | 846 |
829 // Set the base time to be 3 weeks past the stale period in the past. | 847 // Set the base time to be 3 weeks past the stale period in the past. |
830 // Use a 1 second offset to make sure scores don't yet decay. | 848 // Use a 1 second offset to make sure scores don't yet decay. |
831 base::TimeDelta one_second = base::TimeDelta::FromSeconds(1); | 849 base::TimeDelta one_second = base::TimeDelta::FromSeconds(1); |
832 base::TimeDelta one_day = base::TimeDelta::FromDays(1); | 850 base::TimeDelta one_day = base::TimeDelta::FromDays(1); |
833 base::TimeDelta decay_period = | 851 base::TimeDelta decay_period = |
834 base::TimeDelta::FromHours(SiteEngagementScore::GetDecayPeriodInHours()); | 852 base::TimeDelta::FromHours(SiteEngagementScore::GetDecayPeriodInHours()); |
835 base::TimeDelta shorter_than_decay_period = decay_period - one_second; | 853 base::TimeDelta shorter_than_decay_period = decay_period - one_second; |
836 | 854 |
837 base::Time max_decay_time = GetReferenceTime() - service->GetMaxDecayPeriod(); | 855 base::Time max_decay_time = GetReferenceTime() - service->GetMaxDecayPeriod(); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1035 EXPECT_EQ(0, service->GetScore(url1)); | 1053 EXPECT_EQ(0, service->GetScore(url1)); |
1036 EXPECT_EQ(clock->Now() - decay_period, service->GetLastEngagementTime()); | 1054 EXPECT_EQ(clock->Now() - decay_period, service->GetLastEngagementTime()); |
1037 } | 1055 } |
1038 } | 1056 } |
1039 | 1057 |
1040 TEST_F(SiteEngagementServiceTest, CleanupEngagementScoresProportional) { | 1058 TEST_F(SiteEngagementServiceTest, CleanupEngagementScoresProportional) { |
1041 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); | 1059 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); |
1042 SetParamValue(SiteEngagementScore::DECAY_POINTS, 0); | 1060 SetParamValue(SiteEngagementScore::DECAY_POINTS, 0); |
1043 SetParamValue(SiteEngagementScore::SCORE_CLEANUP_THRESHOLD, 0.5); | 1061 SetParamValue(SiteEngagementScore::SCORE_CLEANUP_THRESHOLD, 0.5); |
1044 | 1062 |
1045 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1063 SiteEngagementService* service; |
1046 std::unique_ptr<SiteEngagementService> service( | 1064 base::SimpleTestClock* clock; |
1047 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1065 CreateServiceWithTestClock(&service, &clock); |
1048 | 1066 |
1049 base::Time current_day = GetReferenceTime(); | 1067 base::Time current_day = GetReferenceTime(); |
1050 clock->SetNow(current_day); | 1068 clock->SetNow(current_day); |
1051 | 1069 |
1052 GURL url1("https://www.google.com/"); | 1070 GURL url1("https://www.google.com/"); |
1053 GURL url2("https://www.somewhereelse.com/"); | 1071 GURL url2("https://www.somewhereelse.com/"); |
1054 | 1072 |
1055 service->AddPoints(url1, 1.0); | 1073 service->AddPoints(url1, 1.0); |
1056 service->AddPoints(url2, 1.2); | 1074 service->AddPoints(url2, 1.2); |
1057 | 1075 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1093 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); | 1111 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); |
1094 NavigateWithTransitionAndExpectEqualScore(service, url, | 1112 NavigateWithTransitionAndExpectEqualScore(service, url, |
1095 ui::PAGE_TRANSITION_LINK); | 1113 ui::PAGE_TRANSITION_LINK); |
1096 NavigateWithTransitionAndExpectEqualScore(service, url, | 1114 NavigateWithTransitionAndExpectEqualScore(service, url, |
1097 ui::PAGE_TRANSITION_RELOAD); | 1115 ui::PAGE_TRANSITION_RELOAD); |
1098 NavigateWithTransitionAndExpectEqualScore(service, url, | 1116 NavigateWithTransitionAndExpectEqualScore(service, url, |
1099 ui::PAGE_TRANSITION_FORM_SUBMIT); | 1117 ui::PAGE_TRANSITION_FORM_SUBMIT); |
1100 } | 1118 } |
1101 | 1119 |
1102 TEST_F(SiteEngagementServiceTest, IsBootstrapped) { | 1120 TEST_F(SiteEngagementServiceTest, IsBootstrapped) { |
1103 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1121 SiteEngagementService* service; |
1104 std::unique_ptr<SiteEngagementService> service( | 1122 base::SimpleTestClock* clock; |
1105 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1123 CreateServiceWithTestClock(&service, &clock); |
1106 | 1124 |
1107 base::Time current_day = GetReferenceTime(); | 1125 base::Time current_day = GetReferenceTime(); |
1108 clock->SetNow(current_day); | 1126 clock->SetNow(current_day); |
1109 | 1127 |
1110 GURL url1("https://www.google.com/"); | 1128 GURL url1("https://www.google.com/"); |
1111 GURL url2("https://www.somewhereelse.com/"); | 1129 GURL url2("https://www.somewhereelse.com/"); |
1112 | 1130 |
1113 EXPECT_FALSE(service->IsBootstrapped()); | 1131 EXPECT_FALSE(service->IsBootstrapped()); |
1114 | 1132 |
1115 service->AddPoints(url1, 5.0); | 1133 service->AddPoints(url1, 5.0); |
1116 EXPECT_FALSE(service->IsBootstrapped()); | 1134 EXPECT_FALSE(service->IsBootstrapped()); |
1117 | 1135 |
1118 service->AddPoints(url2, 5.0); | 1136 service->AddPoints(url2, 5.0); |
1119 EXPECT_TRUE(service->IsBootstrapped()); | 1137 EXPECT_TRUE(service->IsBootstrapped()); |
1120 | 1138 |
1121 clock->SetNow(current_day + base::TimeDelta::FromDays(8)); | 1139 clock->SetNow(current_day + base::TimeDelta::FromDays(8)); |
1122 EXPECT_FALSE(service->IsBootstrapped()); | 1140 EXPECT_FALSE(service->IsBootstrapped()); |
1123 } | 1141 } |
1124 | 1142 |
1125 TEST_F(SiteEngagementServiceTest, CleanupOriginsOnHistoryDeletion) { | 1143 TEST_F(SiteEngagementServiceTest, CleanupOriginsOnHistoryDeletion) { |
1126 // Enable proportional decay to ensure that the undecay that happens to | 1144 // Enable proportional decay to ensure that the undecay that happens to |
1127 // balance out history deletion also accounts for the proportional decay. | 1145 // balance out history deletion also accounts for the proportional decay. |
1128 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); | 1146 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); |
1129 | 1147 |
1130 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1148 SiteEngagementService* engagement; |
1131 std::unique_ptr<SiteEngagementService> engagement( | 1149 base::SimpleTestClock* clock; |
1132 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1150 CreateServiceWithTestClock(&engagement, &clock); |
1133 ASSERT_TRUE(engagement.get()); | |
1134 | 1151 |
1135 GURL origin1("http://www.google.com/"); | 1152 GURL origin1("http://www.google.com/"); |
1136 GURL origin1a("http://www.google.com/search?q=asdf"); | 1153 GURL origin1a("http://www.google.com/search?q=asdf"); |
1137 GURL origin1b("http://www.google.com/maps/search?q=asdf"); | 1154 GURL origin1b("http://www.google.com/maps/search?q=asdf"); |
1138 GURL origin2("https://drive.google.com/"); | 1155 GURL origin2("https://drive.google.com/"); |
1139 GURL origin2a("https://drive.google.com/somedoc"); | 1156 GURL origin2a("https://drive.google.com/somedoc"); |
1140 GURL origin3("http://notdeleted.com/"); | 1157 GURL origin3("http://notdeleted.com/"); |
1141 GURL origin4("http://decayed.com/"); | 1158 GURL origin4("http://decayed.com/"); |
1142 GURL origin4a("http://decayed.com/index.html"); | 1159 GURL origin4a("http://decayed.com/index.html"); |
1143 | 1160 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1249 static_assert(blink::mojom::EngagementLevel::LOW != | 1266 static_assert(blink::mojom::EngagementLevel::LOW != |
1250 blink::mojom::EngagementLevel::MEDIUM, | 1267 blink::mojom::EngagementLevel::MEDIUM, |
1251 "enum values should not be equal"); | 1268 "enum values should not be equal"); |
1252 static_assert(blink::mojom::EngagementLevel::MEDIUM != | 1269 static_assert(blink::mojom::EngagementLevel::MEDIUM != |
1253 blink::mojom::EngagementLevel::HIGH, | 1270 blink::mojom::EngagementLevel::HIGH, |
1254 "enum values should not be equal"); | 1271 "enum values should not be equal"); |
1255 static_assert(blink::mojom::EngagementLevel::HIGH != | 1272 static_assert(blink::mojom::EngagementLevel::HIGH != |
1256 blink::mojom::EngagementLevel::MAX, | 1273 blink::mojom::EngagementLevel::MAX, |
1257 "enum values should not be equal"); | 1274 "enum values should not be equal"); |
1258 | 1275 |
1259 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1276 SiteEngagementService* service; |
1260 std::unique_ptr<SiteEngagementService> service( | 1277 base::SimpleTestClock* clock; |
1261 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1278 CreateServiceWithTestClock(&service, &clock); |
1262 | 1279 |
1263 base::Time current_day = GetReferenceTime(); | 1280 base::Time current_day = GetReferenceTime(); |
1264 clock->SetNow(current_day); | 1281 clock->SetNow(current_day); |
1265 | 1282 |
1266 GURL url1("https://www.google.com/"); | 1283 GURL url1("https://www.google.com/"); |
1267 GURL url2("http://www.google.com/"); | 1284 GURL url2("http://www.google.com/"); |
1268 | 1285 |
1269 EXPECT_EQ(blink::mojom::EngagementLevel::NONE, | 1286 EXPECT_EQ(blink::mojom::EngagementLevel::NONE, |
1270 service->GetEngagementLevel(url1)); | 1287 service->GetEngagementLevel(url1)); |
1271 EXPECT_EQ(blink::mojom::EngagementLevel::NONE, | 1288 EXPECT_EQ(blink::mojom::EngagementLevel::NONE, |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1441 service->HandleMediaPlaying(web_contents(), true); | 1458 service->HandleMediaPlaying(web_contents(), true); |
1442 tester.Wait(); | 1459 tester.Wait(); |
1443 | 1460 |
1444 EXPECT_TRUE(tester.callback_called()); | 1461 EXPECT_TRUE(tester.callback_called()); |
1445 EXPECT_FALSE(tester_not_called.callback_called()); | 1462 EXPECT_FALSE(tester_not_called.callback_called()); |
1446 tester.Observe(nullptr); | 1463 tester.Observe(nullptr); |
1447 } | 1464 } |
1448 } | 1465 } |
1449 | 1466 |
1450 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { | 1467 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { |
1451 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1468 SiteEngagementService* service; |
1452 std::unique_ptr<SiteEngagementService> service( | 1469 base::SimpleTestClock* clock; |
1453 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1470 CreateServiceWithTestClock(&service, &clock); |
1454 | 1471 |
1455 base::Time current_day = GetReferenceTime(); | 1472 base::Time current_day = GetReferenceTime(); |
1456 clock->SetNow(current_day); | 1473 clock->SetNow(current_day); |
1457 base::HistogramTester histograms; | 1474 base::HistogramTester histograms; |
1458 GURL origin1("http://www.google.com/"); | 1475 GURL origin1("http://www.google.com/"); |
1459 GURL origin2("http://drive.google.com/"); | 1476 GURL origin2("http://drive.google.com/"); |
1460 | 1477 |
1461 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1478 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1462 0); | 1479 0); |
1463 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1480 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1546 kMorePeriodsThanNeededToDecayMaxScore + 1); | 1563 kMorePeriodsThanNeededToDecayMaxScore + 1); |
1547 } | 1564 } |
1548 | 1565 |
1549 TEST_F(SiteEngagementServiceTest, LastEngagementTime) { | 1566 TEST_F(SiteEngagementServiceTest, LastEngagementTime) { |
1550 // The last engagement time should start off null in prefs and in the service. | 1567 // The last engagement time should start off null in prefs and in the service. |
1551 base::Time last_engagement_time = base::Time::FromInternalValue( | 1568 base::Time last_engagement_time = base::Time::FromInternalValue( |
1552 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); | 1569 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
1553 | 1570 |
1554 ASSERT_TRUE(last_engagement_time.is_null()); | 1571 ASSERT_TRUE(last_engagement_time.is_null()); |
1555 | 1572 |
1556 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1573 SiteEngagementService* service; |
1557 std::unique_ptr<SiteEngagementService> service( | 1574 base::SimpleTestClock* clock; |
1558 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1575 CreateServiceWithTestClock(&service, &clock); |
1559 | 1576 |
1560 ASSERT_TRUE(service->GetLastEngagementTime().is_null()); | 1577 ASSERT_TRUE(service->GetLastEngagementTime().is_null()); |
1561 | 1578 |
1562 base::Time current_day = GetReferenceTime(); | 1579 base::Time current_day = GetReferenceTime(); |
1563 clock->SetNow(current_day); | 1580 clock->SetNow(current_day); |
1564 | 1581 |
1565 // Add points should set the last engagement time in the service, and persist | 1582 // Add points should set the last engagement time in the service, and persist |
1566 // it to disk. | 1583 // it to disk. |
1567 GURL origin("http://www.google.com/"); | 1584 GURL origin("http://www.google.com/"); |
1568 service->AddPoints(origin, 1); | 1585 service->AddPoints(origin, 1); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1600 service->AddPoints(origin, 3); | 1617 service->AddPoints(origin, 3); |
1601 | 1618 |
1602 last_engagement_time = base::Time::FromInternalValue( | 1619 last_engagement_time = base::Time::FromInternalValue( |
1603 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); | 1620 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
1604 | 1621 |
1605 EXPECT_EQ(later_in_day, last_engagement_time); | 1622 EXPECT_EQ(later_in_day, last_engagement_time); |
1606 EXPECT_EQ(later_in_day, service->GetLastEngagementTime()); | 1623 EXPECT_EQ(later_in_day, service->GetLastEngagementTime()); |
1607 } | 1624 } |
1608 | 1625 |
1609 TEST_F(SiteEngagementServiceTest, CleanupMovesScoreBackToNow) { | 1626 TEST_F(SiteEngagementServiceTest, CleanupMovesScoreBackToNow) { |
1610 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1627 SiteEngagementService* service; |
1611 std::unique_ptr<SiteEngagementService> service( | 1628 base::SimpleTestClock* clock; |
1612 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1629 CreateServiceWithTestClock(&service, &clock); |
1613 base::Time last_engagement_time; | 1630 base::Time last_engagement_time; |
1614 | 1631 |
1615 base::Time current_day = GetReferenceTime(); | 1632 base::Time current_day = GetReferenceTime(); |
1616 clock->SetNow(current_day); | 1633 clock->SetNow(current_day); |
1617 | 1634 |
1618 GURL origin("http://www.google.com/"); | 1635 GURL origin("http://www.google.com/"); |
1619 service->AddPoints(origin, 1); | 1636 service->AddPoints(origin, 1); |
1620 EXPECT_EQ(1, service->GetScore(origin)); | 1637 EXPECT_EQ(1, service->GetScore(origin)); |
1621 EXPECT_EQ(current_day, service->GetLastEngagementTime()); | 1638 EXPECT_EQ(current_day, service->GetLastEngagementTime()); |
1622 | 1639 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1670 service->AddPoints(origin1, 1); | 1687 service->AddPoints(origin1, 1); |
1671 | 1688 |
1672 EXPECT_EQ(clock->Now(), | 1689 EXPECT_EQ(clock->Now(), |
1673 service->CreateEngagementScore(origin).last_engagement_time()); | 1690 service->CreateEngagementScore(origin).last_engagement_time()); |
1674 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime()); | 1691 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime()); |
1675 EXPECT_EQ(12, service->GetScore(origin)); | 1692 EXPECT_EQ(12, service->GetScore(origin)); |
1676 EXPECT_EQ(1, service->GetScore(origin1)); | 1693 EXPECT_EQ(1, service->GetScore(origin1)); |
1677 } | 1694 } |
1678 | 1695 |
1679 TEST_F(SiteEngagementServiceTest, CleanupMovesScoreBackToRebase) { | 1696 TEST_F(SiteEngagementServiceTest, CleanupMovesScoreBackToRebase) { |
1680 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1697 SiteEngagementService* service; |
1681 std::unique_ptr<SiteEngagementService> service( | 1698 base::SimpleTestClock* clock; |
1682 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1699 CreateServiceWithTestClock(&service, &clock); |
1683 base::Time last_engagement_time; | 1700 base::Time last_engagement_time; |
1684 | 1701 |
1685 base::Time current_day = GetReferenceTime(); | 1702 base::Time current_day = GetReferenceTime(); |
1686 clock->SetNow(current_day); | 1703 clock->SetNow(current_day); |
1687 | 1704 |
1688 GURL origin("http://www.google.com/"); | 1705 GURL origin("http://www.google.com/"); |
1689 service->ResetBaseScoreForURL(origin, 5); | 1706 service->ResetBaseScoreForURL(origin, 5); |
1690 service->AddPoints(origin, 5); | 1707 service->AddPoints(origin, 5); |
1691 EXPECT_EQ(10, service->GetScore(origin)); | 1708 EXPECT_EQ(10, service->GetScore(origin)); |
1692 EXPECT_EQ(current_day, service->GetLastEngagementTime()); | 1709 EXPECT_EQ(current_day, service->GetLastEngagementTime()); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1809 | 1826 |
1810 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | 1827 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
1811 settings_map, url1)); | 1828 settings_map, url1)); |
1812 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | 1829 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
1813 settings_map, url2)); | 1830 settings_map, url2)); |
1814 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | 1831 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
1815 incognito_settings_map, url1)); | 1832 incognito_settings_map, url1)); |
1816 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | 1833 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
1817 incognito_settings_map, url2)); | 1834 incognito_settings_map, url2)); |
1818 } | 1835 } |
OLD | NEW |