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" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "base/test/simple_test_clock.h" | 14 #include "base/test/simple_test_clock.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
17 #include "chrome/browser/engagement/site_engagement_helper.h" | 17 #include "chrome/browser/engagement/site_engagement_helper.h" |
18 #include "chrome/browser/engagement/site_engagement_metrics.h" | 18 #include "chrome/browser/engagement/site_engagement_metrics.h" |
19 #include "chrome/browser/engagement/site_engagement_score.h" | 19 #include "chrome/browser/engagement/site_engagement_score.h" |
20 #include "chrome/browser/history/history_service_factory.h" | 20 #include "chrome/browser/history/history_service_factory.h" |
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/pref_names.h" |
23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
24 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
25 #include "components/content_settings/core/browser/content_settings_observer.h" | 26 #include "components/content_settings/core/browser/content_settings_observer.h" |
26 #include "components/content_settings/core/browser/host_content_settings_map.h" | 27 #include "components/content_settings/core/browser/host_content_settings_map.h" |
27 #include "components/history/core/browser/history_database_params.h" | 28 #include "components/history/core/browser/history_database_params.h" |
28 #include "components/history/core/browser/history_service.h" | 29 #include "components/history/core/browser/history_service.h" |
29 #include "components/history/core/test/test_history_database.h" | 30 #include "components/history/core/test/test_history_database.h" |
| 31 #include "components/prefs/pref_service.h" |
30 #include "content/public/browser/navigation_entry.h" | 32 #include "content/public/browser/navigation_entry.h" |
31 #include "content/public/browser/page_navigator.h" | 33 #include "content/public/browser/page_navigator.h" |
32 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
33 #include "content/public/test/web_contents_tester.h" | 35 #include "content/public/test/web_contents_tester.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
35 | 37 |
36 namespace { | 38 namespace { |
37 | 39 |
38 base::FilePath g_temp_history_dir; | 40 base::FilePath g_temp_history_dir; |
39 | 41 |
40 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; | 42 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; |
41 const int kMoreDaysThanNeededToMaxTotalEngagement = 40; | 43 const int kMoreDaysThanNeededToMaxTotalEngagement = 40; |
42 const int kLessPeriodsThanNeededToDecayMaxScore = 2; | |
43 const int kMorePeriodsThanNeededToDecayMaxScore = 40; | 44 const int kMorePeriodsThanNeededToDecayMaxScore = 40; |
44 | 45 |
45 // Waits until a change is observed in site engagement content settings. | 46 // Waits until a change is observed in site engagement content settings. |
46 class SiteEngagementChangeWaiter : public content_settings::Observer { | 47 class SiteEngagementChangeWaiter : public content_settings::Observer { |
47 public: | 48 public: |
48 explicit SiteEngagementChangeWaiter(Profile* profile) : profile_(profile) { | 49 explicit SiteEngagementChangeWaiter(Profile* profile) : profile_(profile) { |
49 HostContentSettingsMapFactory::GetForProfile(profile)->AddObserver(this); | 50 HostContentSettingsMapFactory::GetForProfile(profile)->AddObserver(this); |
50 } | 51 } |
51 ~SiteEngagementChangeWaiter() override { | 52 ~SiteEngagementChangeWaiter() override { |
52 HostContentSettingsMapFactory::GetForProfile(profile_)->RemoveObserver( | 53 HostContentSettingsMapFactory::GetForProfile(profile_)->RemoveObserver( |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 for (const std::string& histogram_name : engagement_bucket_histogram_names) | 660 for (const std::string& histogram_name : engagement_bucket_histogram_names) |
660 histograms.ExpectTotalCount(histogram_name, 3); | 661 histograms.ExpectTotalCount(histogram_name, 3); |
661 | 662 |
662 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 100, 1); | 663 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 100, 1); |
663 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 33, 1); | 664 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 33, 1); |
664 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 66, 1); | 665 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 66, 1); |
665 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 33, 1); | 666 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 33, 1); |
666 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 66, 1); | 667 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 66, 1); |
667 } | 668 } |
668 | 669 |
669 // Expect that sites that have reached zero engagement are cleaned up. | 670 // Expect that sites that have reached zero engagement are cleaned up. Expect |
| 671 // engagement times to be reset if too much time has passed since the last |
| 672 // engagement. |
670 TEST_F(SiteEngagementServiceTest, CleanupEngagementScores) { | 673 TEST_F(SiteEngagementServiceTest, CleanupEngagementScores) { |
671 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 674 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
672 std::unique_ptr<SiteEngagementService> service( | 675 std::unique_ptr<SiteEngagementService> service( |
673 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 676 new SiteEngagementService(profile(), base::WrapUnique(clock))); |
674 | 677 |
675 base::Time current_day = GetReferenceTime(); | 678 // Set the base time to be (obsoletion period + 2 * (decay period - 1 second)) |
676 clock->SetNow(current_day); | 679 // in the past. The one second offset ensures that engagement won't decay |
| 680 // (yet). |
| 681 base::TimeDelta one_second = base::TimeDelta::FromSeconds(1); |
| 682 base::TimeDelta obsoletion_period = base::TimeDelta::FromHours( |
| 683 SiteEngagementScore::GetObsoleteLastEngagementPeriodInHours()); |
| 684 base::TimeDelta decay_period = |
| 685 base::TimeDelta::FromDays(SiteEngagementScore::GetDecayPeriodInDays()); |
| 686 base::TimeDelta shorter_than_decay_period = decay_period - one_second; |
| 687 base::Time base_time = |
| 688 GetReferenceTime() - obsoletion_period - shorter_than_decay_period * 2; |
| 689 clock->SetNow(base_time); |
677 | 690 |
678 // The https and http versions of www.google.com should be separate. | 691 // The https and http versions of www.google.com should be separate. |
679 GURL url1("https://www.google.com/"); | 692 GURL url1("https://www.google.com/"); |
680 GURL url2("http://www.google.com/"); | 693 GURL url2("http://www.google.com/"); |
| 694 GURL url3("http://maps.google.com/"); |
| 695 GURL url4("http://drive.google.com/"); |
681 | 696 |
682 EXPECT_EQ(0, service->GetScore(url1)); | 697 EXPECT_EQ(0, service->GetScore(url1)); |
683 EXPECT_EQ(0, service->GetScore(url2)); | 698 EXPECT_EQ(0, service->GetScore(url2)); |
| 699 EXPECT_EQ(0, service->GetScore(url3)); |
| 700 EXPECT_EQ(0, service->GetScore(url4)); |
684 | 701 |
685 // Add the maximum number of points for one day. | 702 // Add the maximum number of points for one day for two origins. Leave url2 |
| 703 // alone after this. |
686 service->AddPoints(url1, 5.0); | 704 service->AddPoints(url1, 5.0); |
| 705 service->AddPoints(url2, 5.0); |
687 EXPECT_EQ(5.0, service->GetScore(url1)); | 706 EXPECT_EQ(5.0, service->GetScore(url1)); |
688 service->AddPoints(url2, 5.0); | |
689 EXPECT_EQ(5.0, service->GetScore(url2)); | 707 EXPECT_EQ(5.0, service->GetScore(url2)); |
690 | 708 |
691 // Add more points by moving to another day. | 709 // Add more points by moving to another day. This is (obsoletion_period |
692 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(1)); | 710 // + shorter_than_decay_period) in the past. |
| 711 base_time += shorter_than_decay_period; |
| 712 clock->SetNow(base_time); |
| 713 service->AddPoints(url1, 2.5); |
| 714 service->AddPoints(url3, 5.0); |
| 715 EXPECT_EQ(7.5, service->GetScore(url1)); |
| 716 EXPECT_EQ(5.0, service->GetScore(url3)); |
693 | 717 |
694 service->AddPoints(url1, 5.0); | 718 // Add more points by moving to another day. This is obsoletion_period in |
| 719 // the past. |
| 720 base_time += shorter_than_decay_period; |
| 721 clock->SetNow(base_time); |
| 722 service->AddPoints(url1, 2.5); |
| 723 service->AddPoints(url4, 5.0); |
695 EXPECT_EQ(10.0, service->GetScore(url1)); | 724 EXPECT_EQ(10.0, service->GetScore(url1)); |
| 725 EXPECT_EQ(5.0, service->GetScore(url4)); |
696 | 726 |
697 { | 727 { |
698 // Decay one origin to zero by advancing time and expect the engagement | 728 clock->SetNow(GetReferenceTime()); |
699 // score to be cleaned up. The other score was changed a day later so it | 729 ASSERT_TRUE(service->IsLastEngagementObsolete()); |
700 // will not have decayed at all. | 730 |
| 731 // Run a cleanup. Last engagement times will be reset relative to today. |
| 732 // After the reset, url2 was last accessed > decay_period ago, so it will |
| 733 // still decay. Everything else will be within a decay period and not decay. |
| 734 service->CleanupEngagementScores(true); |
| 735 |
| 736 std::map<GURL, double> score_map = service->GetScoreMap(); |
| 737 EXPECT_EQ(3u, score_map.size()); |
| 738 EXPECT_EQ(10, score_map[url1]); |
| 739 EXPECT_EQ(5.0, score_map[url3]); |
| 740 EXPECT_EQ(5.0, score_map[url4]); |
| 741 EXPECT_EQ(0, service->GetScore(url2)); |
| 742 |
| 743 EXPECT_EQ(clock->Now(), |
| 744 service->CreateEngagementScore(url1).last_engagement_time()); |
| 745 EXPECT_EQ(clock->Now() - shorter_than_decay_period, |
| 746 service->CreateEngagementScore(url3).last_engagement_time()); |
| 747 EXPECT_EQ(clock->Now(), |
| 748 service->CreateEngagementScore(url4).last_engagement_time()); |
| 749 } |
| 750 |
| 751 { |
| 752 // Advance time by a second and expect url3 to be decayed to zero. Run a |
| 753 // cleanup without adjusting times and expect url3 to be purged. |
| 754 clock->SetNow(GetReferenceTime() + one_second); |
| 755 ASSERT_FALSE(service->IsLastEngagementObsolete()); |
| 756 |
| 757 std::map<GURL, double> score_map = service->GetScoreMap(); |
| 758 EXPECT_EQ(3u, score_map.size()); |
| 759 EXPECT_EQ(10, score_map[url1]); |
| 760 EXPECT_EQ(0, score_map[url3]); |
| 761 EXPECT_EQ(5.0, score_map[url4]); |
| 762 |
| 763 service->CleanupEngagementScores(false); |
| 764 |
| 765 score_map = service->GetScoreMap(); |
| 766 EXPECT_EQ(2u, score_map.size()); |
| 767 EXPECT_EQ(10, score_map[url1]); |
| 768 EXPECT_EQ(5.0, score_map[url4]); |
| 769 EXPECT_EQ(0, service->GetScore(url3)); |
| 770 } |
| 771 |
| 772 { |
| 773 // Decay the final two origins to zero and expect them to be cleaned up. |
| 774 // This needs to be done in two calls or else the score will be obsolete. |
701 clock->SetNow( | 775 clock->SetNow( |
702 GetReferenceTime() + | 776 GetReferenceTime() + |
703 base::TimeDelta::FromDays(SiteEngagementScore::GetDecayPeriodInDays())); | 777 base::TimeDelta::FromDays(SiteEngagementScore::GetDecayPeriodInDays()) + |
| 778 one_second); |
| 779 ASSERT_FALSE(service->IsLastEngagementObsolete()); |
704 | 780 |
705 std::map<GURL, double> score_map = service->GetScoreMap(); | 781 std::map<GURL, double> score_map = service->GetScoreMap(); |
706 EXPECT_EQ(2u, score_map.size()); | 782 EXPECT_EQ(2u, score_map.size()); |
707 EXPECT_EQ(10, score_map[url1]); | 783 EXPECT_EQ(5, score_map[url1]); |
708 EXPECT_EQ(0, score_map[url2]); | 784 EXPECT_EQ(0, score_map[url4]); |
709 | 785 |
710 service->CleanupEngagementScores(); | 786 service->CleanupEngagementScores(false); |
711 | 787 |
712 score_map = service->GetScoreMap(); | 788 score_map = service->GetScoreMap(); |
713 EXPECT_EQ(1u, score_map.size()); | 789 EXPECT_EQ(1u, score_map.size()); |
714 EXPECT_EQ(10, score_map[url1]); | 790 EXPECT_EQ(5, score_map[url1]); |
715 EXPECT_EQ(0, service->GetScore(url2)); | 791 EXPECT_EQ(0, service->GetScore(url4)); |
716 } | |
717 | 792 |
718 { | |
719 // Decay the other origin to zero by advancing time and expect the | |
720 // engagement score to be cleaned up. | |
721 clock->SetNow(GetReferenceTime() + | 793 clock->SetNow(GetReferenceTime() + |
722 base::TimeDelta::FromDays( | 794 2 * base::TimeDelta::FromDays( |
723 3 * SiteEngagementScore::GetDecayPeriodInDays())); | 795 SiteEngagementScore::GetDecayPeriodInDays()) + |
| 796 one_second); |
| 797 ASSERT_FALSE(service->IsLastEngagementObsolete()); |
724 | 798 |
725 std::map<GURL, double> score_map = service->GetScoreMap(); | 799 score_map = service->GetScoreMap(); |
726 EXPECT_EQ(1u, score_map.size()); | 800 EXPECT_EQ(1u, score_map.size()); |
727 EXPECT_EQ(0, score_map[url1]); | 801 EXPECT_EQ(0, score_map[url1]); |
728 | 802 |
729 service->CleanupEngagementScores(); | 803 service->CleanupEngagementScores(false); |
730 | 804 |
731 score_map = service->GetScoreMap(); | 805 score_map = service->GetScoreMap(); |
732 EXPECT_EQ(0u, score_map.size()); | 806 EXPECT_EQ(0u, score_map.size()); |
733 EXPECT_EQ(0, service->GetScore(url1)); | 807 EXPECT_EQ(0, service->GetScore(url1)); |
734 } | 808 } |
735 } | 809 } |
736 | 810 |
737 TEST_F(SiteEngagementServiceTest, NavigationAccumulation) { | 811 TEST_F(SiteEngagementServiceTest, NavigationAccumulation) { |
738 GURL url("https://www.google.com/"); | 812 GURL url("https://www.google.com/"); |
739 | 813 |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1056 // Trigger decay and histogram hit. | 1130 // Trigger decay and histogram hit. |
1057 service->AddPoints(origin1, 0.01); | 1131 service->AddPoints(origin1, 0.01); |
1058 histograms.ExpectUniqueSample( | 1132 histograms.ExpectUniqueSample( |
1059 SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1133 SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1060 SiteEngagementScore::kMaxPoints, 1); | 1134 SiteEngagementScore::kMaxPoints, 1); |
1061 histograms.ExpectUniqueSample( | 1135 histograms.ExpectUniqueSample( |
1062 SiteEngagementMetrics::kScoreDecayedToHistogram, | 1136 SiteEngagementMetrics::kScoreDecayedToHistogram, |
1063 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(), | 1137 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(), |
1064 1); | 1138 1); |
1065 | 1139 |
1066 // Check histograms after a few decay periods. | 1140 // Check histograms after another decay period. |
1067 clock->SetNow(current_day + base::TimeDelta::FromDays( | 1141 clock->SetNow(current_day + |
1068 kLessPeriodsThanNeededToDecayMaxScore * | 1142 base::TimeDelta::FromDays( |
1069 SiteEngagementScore::GetDecayPeriodInDays())); | 1143 2 * SiteEngagementScore::GetDecayPeriodInDays())); |
1070 // Trigger decay and histogram hit. | 1144 // Trigger decay and histogram hit. |
1071 service->AddPoints(origin1, 0.01); | 1145 service->AddPoints(origin1, 0.01); |
1072 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1146 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1073 2); | 1147 2); |
1074 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1148 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1075 2); | 1149 2); |
1076 | 1150 |
1077 // Check decay to zero. | 1151 // Check decay to zero. Start at the 3rd decay period (we have had two |
1078 clock->SetNow(current_day + base::TimeDelta::FromDays( | 1152 // already). This will be 40 decays in total. |
1079 kMorePeriodsThanNeededToDecayMaxScore * | 1153 for (int i = 3; i <= kMorePeriodsThanNeededToDecayMaxScore; ++i) { |
1080 SiteEngagementScore::GetDecayPeriodInDays())); | 1154 clock->SetNow(current_day + |
1081 // Trigger decay and histogram hit. | 1155 base::TimeDelta::FromDays( |
1082 service->AddPoints(origin1, 0.01); | 1156 i * SiteEngagementScore::GetDecayPeriodInDays())); |
| 1157 // Trigger decay and histogram hit. |
| 1158 service->AddPoints(origin1, 0.01); |
| 1159 } |
1083 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1160 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1084 3); | 1161 kMorePeriodsThanNeededToDecayMaxScore); |
1085 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1162 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1086 3); | 1163 kMorePeriodsThanNeededToDecayMaxScore); |
| 1164 // It should have taken (20 - 3) = 17 of the 38 decays to get to zero, since |
| 1165 // we started from 95. Expect the remaining 21 decays to be to bucket 0 (and |
| 1166 // hence 20 from bucket 0). |
| 1167 histograms.ExpectBucketCount( |
| 1168 SiteEngagementMetrics::kScoreDecayedFromHistogram, 0, 20); |
1087 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1169 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1088 0, 1); | 1170 0, 21); |
1089 // Trigger decay and histogram hit for origin2, checking an independent decay. | 1171 // Trigger decay and histogram hit for origin2, checking an independent decay. |
1090 service->AddPoints(origin2, 0.01); | 1172 service->AddPoints(origin2, 0.01); |
1091 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1173 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1092 4); | 1174 kMorePeriodsThanNeededToDecayMaxScore + 1); |
1093 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1175 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1094 4); | 1176 kMorePeriodsThanNeededToDecayMaxScore + 1); |
1095 histograms.ExpectBucketCount( | 1177 histograms.ExpectBucketCount( |
1096 SiteEngagementMetrics::kScoreDecayedFromHistogram, 0, 1); | 1178 SiteEngagementMetrics::kScoreDecayedFromHistogram, 0, 21); |
1097 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1179 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1098 0, 2); | 1180 0, 22); |
1099 | 1181 |
1100 // Add more points and ensure no more samples are present. | 1182 // Add more points and ensure no more samples are present. |
1101 service->AddPoints(origin1, 0.01); | 1183 service->AddPoints(origin1, 0.01); |
1102 service->AddPoints(origin2, 0.01); | 1184 service->AddPoints(origin2, 0.01); |
1103 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1185 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1104 4); | 1186 kMorePeriodsThanNeededToDecayMaxScore + 1); |
1105 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1187 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1106 4); | 1188 kMorePeriodsThanNeededToDecayMaxScore + 1); |
1107 } | 1189 } |
| 1190 |
| 1191 TEST_F(SiteEngagementServiceTest, PersistLastEngagementTime) { |
| 1192 // The last engagement time should start off null in prefs and in the service. |
| 1193 base::Time last_engagement_time = base::Time::FromInternalValue( |
| 1194 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
| 1195 |
| 1196 ASSERT_TRUE(last_engagement_time.is_null()); |
| 1197 |
| 1198 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 1199 std::unique_ptr<SiteEngagementService> service( |
| 1200 new SiteEngagementService(profile(), base::WrapUnique(clock))); |
| 1201 |
| 1202 ASSERT_TRUE(service->last_engagement_time_.is_null()); |
| 1203 |
| 1204 base::Time current_day = GetReferenceTime(); |
| 1205 clock->SetNow(current_day); |
| 1206 |
| 1207 // Add points should set the last engagement time in the service, but not |
| 1208 // persist it to disk. |
| 1209 GURL origin("http://www.google.com/"); |
| 1210 service->AddPoints(origin, 1); |
| 1211 |
| 1212 last_engagement_time = base::Time::FromInternalValue( |
| 1213 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
| 1214 |
| 1215 ASSERT_TRUE(last_engagement_time.is_null()); |
| 1216 ASSERT_FALSE(service->last_engagement_time_.is_null()); |
| 1217 EXPECT_EQ(service->last_engagement_time_, current_day); |
| 1218 |
| 1219 // Running a cleanup and updating last engagement times should persist the |
| 1220 // last engagement time to disk. |
| 1221 current_day += |
| 1222 base::TimeDelta::FromHours( |
| 1223 SiteEngagementScore::GetObsoleteLastEngagementPeriodInHours()); |
| 1224 clock->SetNow(current_day); |
| 1225 service->CleanupEngagementScores(true); |
| 1226 |
| 1227 last_engagement_time = base::Time::FromInternalValue( |
| 1228 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
| 1229 |
| 1230 ASSERT_FALSE(last_engagement_time.is_null()); |
| 1231 EXPECT_EQ(last_engagement_time, current_day); |
| 1232 EXPECT_EQ(service->last_engagement_time_, current_day); |
| 1233 |
| 1234 // Add some more points and ensure the value isn't persisted. |
| 1235 base::Time later_in_day = current_day + base::TimeDelta::FromSeconds(30); |
| 1236 clock->SetNow(later_in_day); |
| 1237 service->AddPoints(origin, 3); |
| 1238 |
| 1239 last_engagement_time = base::Time::FromInternalValue( |
| 1240 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
| 1241 |
| 1242 ASSERT_FALSE(last_engagement_time.is_null()); |
| 1243 EXPECT_EQ(last_engagement_time, current_day); |
| 1244 ASSERT_FALSE(service->last_engagement_time_.is_null()); |
| 1245 EXPECT_EQ(service->last_engagement_time_, later_in_day); |
| 1246 |
| 1247 // When the service is destroyed, the time should be persisted to disk. |
| 1248 service.reset(nullptr); |
| 1249 last_engagement_time = base::Time::FromInternalValue( |
| 1250 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
| 1251 |
| 1252 ASSERT_FALSE(last_engagement_time.is_null()); |
| 1253 EXPECT_EQ(last_engagement_time, later_in_day); |
| 1254 } |
OLD | NEW |