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

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

Issue 2082953002: Prevent site engagement scores from decaying when Chrome isn't in use. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 36 hours != 3 days Created 4 years, 5 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"
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 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 EXPECT_DOUBLE_EQ(7.0, service->GetScore(url2)); 402 EXPECT_DOUBLE_EQ(7.0, service->GetScore(url2));
402 403
403 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(1)); 404 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(1));
404 EXPECT_DOUBLE_EQ(2.0, service->GetScore(url1)); 405 EXPECT_DOUBLE_EQ(2.0, service->GetScore(url1));
405 EXPECT_DOUBLE_EQ(7.0, service->GetScore(url2)); 406 EXPECT_DOUBLE_EQ(7.0, service->GetScore(url2));
406 407
407 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(7)); 408 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(7));
408 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url1)); 409 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url1));
409 EXPECT_DOUBLE_EQ(5.0, service->GetScore(url2)); 410 EXPECT_DOUBLE_EQ(5.0, service->GetScore(url2));
410 411
412 service->AddPoints(url1, 1.0);
calamity 2016/06/28 01:48:25 Why did this appear?
dominickn 2016/06/28 01:54:49 We trigger a cleanup otherwise (> 10 days from las
411 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(10)); 413 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(10));
412 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url1)); 414 EXPECT_DOUBLE_EQ(1.0, service->GetScore(url1));
413 EXPECT_DOUBLE_EQ(5.0, service->GetScore(url2)); 415 EXPECT_DOUBLE_EQ(5.0, service->GetScore(url2));
414 416
415 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(11)); 417 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(11));
416 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url1)); 418 EXPECT_DOUBLE_EQ(1.0, service->GetScore(url1));
417 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url2)); 419 EXPECT_DOUBLE_EQ(0.0, service->GetScore(url2));
418 } 420 }
419 421
420 TEST_F(SiteEngagementServiceTest, CheckHistograms) { 422 TEST_F(SiteEngagementServiceTest, CheckHistograms) {
421 base::HistogramTester histograms; 423 base::HistogramTester histograms;
422 424
423 base::SimpleTestClock* clock = new base::SimpleTestClock(); 425 base::SimpleTestClock* clock = new base::SimpleTestClock();
424 std::unique_ptr<SiteEngagementService> service( 426 std::unique_ptr<SiteEngagementService> service(
425 new SiteEngagementService(profile(), base::WrapUnique(clock))); 427 new SiteEngagementService(profile(), base::WrapUnique(clock)));
426 428
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 for (const std::string& histogram_name : engagement_bucket_histogram_names) 701 for (const std::string& histogram_name : engagement_bucket_histogram_names)
700 histograms.ExpectTotalCount(histogram_name, 3); 702 histograms.ExpectTotalCount(histogram_name, 3);
701 703
702 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 100, 1); 704 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 100, 1);
703 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 33, 1); 705 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 33, 1);
704 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 66, 1); 706 histograms.ExpectBucketCount(engagement_bucket_histogram_names[0], 66, 1);
705 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 33, 1); 707 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 33, 1);
706 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 66, 1); 708 histograms.ExpectBucketCount(engagement_bucket_histogram_names[1], 66, 1);
707 } 709 }
708 710
709 // Expect that sites that have reached zero engagement are cleaned up. 711 // Expect that sites that have reached zero engagement are cleaned up. Expect
712 // engagement times to be reset if too much time has passed since the last
713 // engagement.
710 TEST_F(SiteEngagementServiceTest, CleanupEngagementScores) { 714 TEST_F(SiteEngagementServiceTest, CleanupEngagementScores) {
711 base::SimpleTestClock* clock = new base::SimpleTestClock(); 715 base::SimpleTestClock* clock = new base::SimpleTestClock();
712 std::unique_ptr<SiteEngagementService> service( 716 std::unique_ptr<SiteEngagementService> service(
713 new SiteEngagementService(profile(), base::WrapUnique(clock))); 717 new SiteEngagementService(profile(), base::WrapUnique(clock)));
714 718
715 base::Time current_day = GetReferenceTime(); 719 // Set the base time to be 3 weeks past the stale period in the past.
716 clock->SetNow(current_day); 720 // Use a 1 second offset to make sure scores don't yet decay.
721 base::TimeDelta one_second = base::TimeDelta::FromSeconds(1);
722 base::TimeDelta one_day = base::TimeDelta::FromDays(1);
723 base::TimeDelta decay_period =
724 base::TimeDelta::FromDays(SiteEngagementScore::GetDecayPeriodInDays());
725 base::TimeDelta shorter_than_decay_period = decay_period - one_second;
726
727 base::Time max_decay_time = GetReferenceTime() - service->GetMaxDecayPeriod();
728 base::Time stale_time = GetReferenceTime() - service->GetStalePeriod();
729 base::Time base_time = stale_time - shorter_than_decay_period * 4;
730 clock->SetNow(base_time);
717 731
718 // The https and http versions of www.google.com should be separate. 732 // The https and http versions of www.google.com should be separate.
719 GURL url1("https://www.google.com/"); 733 GURL url1("https://www.google.com/");
720 GURL url2("http://www.google.com/"); 734 GURL url2("http://www.google.com/");
735 GURL url3("http://maps.google.com/");
736 GURL url4("http://drive.google.com/");
721 737
722 EXPECT_EQ(0, service->GetScore(url1)); 738 EXPECT_EQ(0, service->GetScore(url1));
723 EXPECT_EQ(0, service->GetScore(url2)); 739 EXPECT_EQ(0, service->GetScore(url2));
724 740 EXPECT_EQ(0, service->GetScore(url3));
725 // Add the maximum number of points for one day. 741 EXPECT_EQ(0, service->GetScore(url4));
742
743 // Add some points
744 service->AddPoints(url1, 1.0);
745 service->AddPoints(url2, 5.0);
746 EXPECT_EQ(1.0, service->GetScore(url1));
747 EXPECT_EQ(5.0, service->GetScore(url2));
748
749 // Add more to url2 over the next few days. Leave it completely alone after
750 // this.
751 clock->SetNow(base_time + one_day);
752 service->AddPoints(url2, 5.0);
753 EXPECT_EQ(10.0, service->GetScore(url2));
754
755 clock->SetNow(base_time + 2 * one_day);
756 service->AddPoints(url2, 5.0);
757 EXPECT_EQ(15.0, service->GetScore(url2));
758
759 clock->SetNow(base_time + 3 * one_day);
760 service->AddPoints(url2, 2.0);
761 EXPECT_EQ(17.0, service->GetScore(url2));
762 base::Time url2_last_modified = clock->Now();
763
764 // Move to (3 * shorter_than_decay_period) before the stale period.
765 base_time += shorter_than_decay_period;
766 clock->SetNow(base_time);
767 service->AddPoints(url1, 1.0);
768 service->AddPoints(url3, 5.0);
769 EXPECT_EQ(2.0, service->GetScore(url1));
770 EXPECT_EQ(5.0, service->GetScore(url3));
771
772 // Add more to url3, and then leave it alone.
773 clock->SetNow(base_time + one_day);
726 service->AddPoints(url1, 5.0); 774 service->AddPoints(url1, 5.0);
727 EXPECT_EQ(5.0, service->GetScore(url1)); 775 service->AddPoints(url3, 5.0);
728 service->AddPoints(url2, 5.0); 776 EXPECT_EQ(7.0, service->GetScore(url1));
729 EXPECT_EQ(5.0, service->GetScore(url2)); 777 EXPECT_EQ(10.0, service->GetScore(url3));
730 778
731 // Add more points by moving to another day. 779 // Move to (2 * shorter_than_decay_period) before the stale period.
732 clock->SetNow(GetReferenceTime() + base::TimeDelta::FromDays(1)); 780 base_time += shorter_than_decay_period;
733 781 clock->SetNow(base_time);
734 service->AddPoints(url1, 5.0); 782 service->AddPoints(url1, 5.0);
735 EXPECT_EQ(10.0, service->GetScore(url1)); 783 service->AddPoints(url4, 5.0);
736 784 EXPECT_EQ(12.0, service->GetScore(url1));
737 { 785 EXPECT_EQ(5.0, service->GetScore(url4));
738 // Decay one origin to zero by advancing time and expect the engagement 786
739 // score to be cleaned up. The other score was changed a day later so it 787 // Move to shorter_than_decay_period before the stale period.
740 // will not have decayed at all. 788 base_time += shorter_than_decay_period;
741 clock->SetNow( 789 clock->SetNow(base_time);
742 GetReferenceTime() + 790 service->AddPoints(url1, 1.5);
743 base::TimeDelta::FromDays(SiteEngagementScore::GetDecayPeriodInDays())); 791 service->AddPoints(url4, 2.0);
744 792 EXPECT_EQ(13.5, service->GetScore(url1));
745 std::map<GURL, double> score_map = service->GetScoreMap(); 793 EXPECT_EQ(7.0, service->GetScore(url4));
746 EXPECT_EQ(2u, score_map.size()); 794
747 EXPECT_EQ(10, score_map[url1]); 795 // After cleanup, url2 should be last modified offset to max_decay_time by the
796 // current offset to now.
797 url2_last_modified = max_decay_time - (clock->Now() - url2_last_modified);
798 base_time = GetReferenceTime();
799
800 {
801 clock->SetNow(base_time);
802 ASSERT_TRUE(service->IsLastEngagementStale());
803
804 // Run a cleanup. Last engagement times will be reset relative to
805 // max_decay_time. After the reset, url2 will go through 3 decays, url3
806 // will go through 2 decays, and url1/url4 will go through 1 decay. This
807 // decay is uncommitted!
808 service->CleanupEngagementScores(true);
809 ASSERT_FALSE(service->IsLastEngagementStale());
810
811 std::map<GURL, double> score_map = service->GetScoreMap();
812 EXPECT_EQ(3u, score_map.size());
813 EXPECT_EQ(8.5, score_map[url1]);
814 EXPECT_EQ(2.0, score_map[url2]);
815 EXPECT_EQ(2.0, score_map[url4]);
816 EXPECT_EQ(0, service->GetScore(url3));
817
818 EXPECT_EQ(max_decay_time,
819 service->CreateEngagementScore(url1).last_engagement_time());
820 EXPECT_EQ(url2_last_modified,
821 service->CreateEngagementScore(url2).last_engagement_time());
822 EXPECT_EQ(max_decay_time,
823 service->CreateEngagementScore(url4).last_engagement_time());
824 EXPECT_EQ(max_decay_time, service->GetLastEngagementTime());
825 }
826
827 {
828 // Advance time by the stale period. Nothing should happen in the cleanup.
829 // Last engagement times are now relative to max_decay_time + stale period
830 base_time += service->GetStalePeriod();
831 clock->SetNow(base_time);
832 ASSERT_TRUE(service->IsLastEngagementStale());
833
834 std::map<GURL, double> score_map = service->GetScoreMap();
835 EXPECT_EQ(3u, score_map.size());
836 EXPECT_EQ(8.5, score_map[url1]);
837 EXPECT_EQ(2.0, score_map[url2]);
838 EXPECT_EQ(2.0, score_map[url4]);
839
840 EXPECT_EQ(max_decay_time + service->GetStalePeriod(),
841 service->CreateEngagementScore(url1).last_engagement_time());
842 EXPECT_EQ(url2_last_modified + service->GetStalePeriod(),
843 service->CreateEngagementScore(url2).last_engagement_time());
844 EXPECT_EQ(max_decay_time + service->GetStalePeriod(),
845 service->CreateEngagementScore(url4).last_engagement_time());
846 EXPECT_EQ(max_decay_time + service->GetStalePeriod(),
847 service->GetLastEngagementTime());
848 }
849
850 {
851 // Add points to commit the decay.
852 service->AddPoints(url1, 0.5);
853 service->AddPoints(url2, 0.5);
854 service->AddPoints(url4, 1);
855
856 std::map<GURL, double> score_map = service->GetScoreMap();
857 EXPECT_EQ(3u, score_map.size());
858 EXPECT_EQ(9.0, score_map[url1]);
859 EXPECT_EQ(2.5, score_map[url2]);
860 EXPECT_EQ(3.0, score_map[url4]);
861 EXPECT_EQ(clock->Now(),
862 service->CreateEngagementScore(url1).last_engagement_time());
863 EXPECT_EQ(clock->Now(),
864 service->CreateEngagementScore(url2).last_engagement_time());
865 EXPECT_EQ(clock->Now(),
866 service->CreateEngagementScore(url4).last_engagement_time());
867 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime());
868 }
869
870 {
871 // Advance time by a decay period after the current last engagement time.
872 // Expect url2/url4 to be decayed to zero and url1 to decay once.
873 base_time = clock->Now() + decay_period;
874 clock->SetNow(base_time);
875 ASSERT_FALSE(service->IsLastEngagementStale());
876
877 std::map<GURL, double> score_map = service->GetScoreMap();
878 EXPECT_EQ(3u, score_map.size());
879 EXPECT_EQ(4, score_map[url1]);
748 EXPECT_EQ(0, score_map[url2]); 880 EXPECT_EQ(0, score_map[url2]);
749 881 EXPECT_EQ(0, score_map[url4]);
750 service->CleanupEngagementScores(); 882
883 service->CleanupEngagementScores(false);
884 ASSERT_FALSE(service->IsLastEngagementStale());
751 885
752 score_map = service->GetScoreMap(); 886 score_map = service->GetScoreMap();
753 EXPECT_EQ(1u, score_map.size()); 887 EXPECT_EQ(1u, score_map.size());
754 EXPECT_EQ(10, score_map[url1]); 888 EXPECT_EQ(4, score_map[url1]);
755 EXPECT_EQ(0, service->GetScore(url2)); 889 EXPECT_EQ(0, service->GetScore(url2));
756 } 890 EXPECT_EQ(0, service->GetScore(url4));
757 891 EXPECT_EQ(clock->Now() - decay_period,
758 { 892 service->CreateEngagementScore(url1).last_engagement_time());
759 // Decay the other origin to zero by advancing time and expect the 893 EXPECT_EQ(clock->Now() - decay_period, service->GetLastEngagementTime());
760 // engagement score to be cleaned up. 894 }
761 clock->SetNow(GetReferenceTime() + 895
762 base::TimeDelta::FromDays( 896 {
763 3 * SiteEngagementScore::GetDecayPeriodInDays())); 897 // Add points to commit the decay.
898 service->AddPoints(url1, 0.5);
899
900 std::map<GURL, double> score_map = service->GetScoreMap();
901 EXPECT_EQ(1u, score_map.size());
902 EXPECT_EQ(4.5, score_map[url1]);
903 EXPECT_EQ(clock->Now(),
904 service->CreateEngagementScore(url1).last_engagement_time());
905 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime());
906 }
907
908 {
909 // Another decay period will decay url1 to zero.
910 clock->SetNow(clock->Now() + decay_period);
911 ASSERT_FALSE(service->IsLastEngagementStale());
764 912
765 std::map<GURL, double> score_map = service->GetScoreMap(); 913 std::map<GURL, double> score_map = service->GetScoreMap();
766 EXPECT_EQ(1u, score_map.size()); 914 EXPECT_EQ(1u, score_map.size());
767 EXPECT_EQ(0, score_map[url1]); 915 EXPECT_EQ(0, score_map[url1]);
768 916 EXPECT_EQ(clock->Now() - decay_period,
769 service->CleanupEngagementScores(); 917 service->CreateEngagementScore(url1).last_engagement_time());
918 EXPECT_EQ(clock->Now() - decay_period, service->GetLastEngagementTime());
919
920 service->CleanupEngagementScores(false);
921 ASSERT_FALSE(service->IsLastEngagementStale());
770 922
771 score_map = service->GetScoreMap(); 923 score_map = service->GetScoreMap();
772 EXPECT_EQ(0u, score_map.size()); 924 EXPECT_EQ(0u, score_map.size());
773 EXPECT_EQ(0, service->GetScore(url1)); 925 EXPECT_EQ(0, service->GetScore(url1));
926 EXPECT_EQ(clock->Now() - decay_period, service->GetLastEngagementTime());
774 } 927 }
775 } 928 }
776 929
777 TEST_F(SiteEngagementServiceTest, NavigationAccumulation) { 930 TEST_F(SiteEngagementServiceTest, NavigationAccumulation) {
778 GURL url("https://www.google.com/"); 931 GURL url("https://www.google.com/");
779 932
780 SiteEngagementService* service = SiteEngagementService::Get(profile()); 933 SiteEngagementService* service = SiteEngagementService::Get(profile());
781 ASSERT_TRUE(service); 934 ASSERT_TRUE(service);
782 935
783 // Create the helper manually since it isn't present when a tab isn't created. 936 // Create the helper manually since it isn't present when a tab isn't created.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 GURL url2("https://www.somewhereelse.com/"); 969 GURL url2("https://www.somewhereelse.com/");
817 970
818 EXPECT_FALSE(service->IsBootstrapped()); 971 EXPECT_FALSE(service->IsBootstrapped());
819 972
820 service->AddPoints(url1, 5.0); 973 service->AddPoints(url1, 5.0);
821 EXPECT_FALSE(service->IsBootstrapped()); 974 EXPECT_FALSE(service->IsBootstrapped());
822 975
823 service->AddPoints(url2, 5.0); 976 service->AddPoints(url2, 5.0);
824 EXPECT_TRUE(service->IsBootstrapped()); 977 EXPECT_TRUE(service->IsBootstrapped());
825 978
826 clock->SetNow(current_day + base::TimeDelta::FromDays(10)); 979 clock->SetNow(current_day + base::TimeDelta::FromDays(8));
827 EXPECT_FALSE(service->IsBootstrapped()); 980 EXPECT_FALSE(service->IsBootstrapped());
828 } 981 }
829 982
830 TEST_F(SiteEngagementServiceTest, CleanupOriginsOnHistoryDeletion) { 983 TEST_F(SiteEngagementServiceTest, CleanupOriginsOnHistoryDeletion) {
831 base::SimpleTestClock* clock = new base::SimpleTestClock(); 984 base::SimpleTestClock* clock = new base::SimpleTestClock();
832 std::unique_ptr<SiteEngagementService> engagement( 985 std::unique_ptr<SiteEngagementService> engagement(
833 new SiteEngagementService(profile(), base::WrapUnique(clock))); 986 new SiteEngagementService(profile(), base::WrapUnique(clock)));
834 ASSERT_TRUE(engagement.get()); 987 ASSERT_TRUE(engagement.get());
835 988
836 GURL origin1("http://www.google.com/"); 989 GURL origin1("http://www.google.com/");
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 // Trigger decay and histogram hit. 1312 // Trigger decay and histogram hit.
1160 service->AddPoints(origin1, 0.01); 1313 service->AddPoints(origin1, 0.01);
1161 histograms.ExpectUniqueSample( 1314 histograms.ExpectUniqueSample(
1162 SiteEngagementMetrics::kScoreDecayedFromHistogram, 1315 SiteEngagementMetrics::kScoreDecayedFromHistogram,
1163 SiteEngagementScore::kMaxPoints, 1); 1316 SiteEngagementScore::kMaxPoints, 1);
1164 histograms.ExpectUniqueSample( 1317 histograms.ExpectUniqueSample(
1165 SiteEngagementMetrics::kScoreDecayedToHistogram, 1318 SiteEngagementMetrics::kScoreDecayedToHistogram,
1166 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(), 1319 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(),
1167 1); 1320 1);
1168 1321
1169 // Check histograms after a few decay periods. 1322 // Check histograms after another decay period.
1170 clock->SetNow(current_day + base::TimeDelta::FromDays( 1323 clock->SetNow(current_day +
1171 kLessPeriodsThanNeededToDecayMaxScore * 1324 base::TimeDelta::FromDays(
1172 SiteEngagementScore::GetDecayPeriodInDays())); 1325 2 * SiteEngagementScore::GetDecayPeriodInDays()));
1173 // Trigger decay and histogram hit. 1326 // Trigger decay and histogram hit.
1174 service->AddPoints(origin1, 0.01); 1327 service->AddPoints(origin1, 0.01);
1175 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, 1328 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram,
1176 2); 1329 2);
1177 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1330 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1178 2); 1331 2);
1179 1332
1180 // Check decay to zero. 1333 // Check decay to zero. Start at the 3rd decay period (we have had two
1181 clock->SetNow(current_day + base::TimeDelta::FromDays( 1334 // already). This will be 40 decays in total.
1182 kMorePeriodsThanNeededToDecayMaxScore * 1335 for (int i = 3; i <= kMorePeriodsThanNeededToDecayMaxScore; ++i) {
1183 SiteEngagementScore::GetDecayPeriodInDays())); 1336 clock->SetNow(current_day +
1184 // Trigger decay and histogram hit. 1337 base::TimeDelta::FromDays(
1185 service->AddPoints(origin1, 0.01); 1338 i * SiteEngagementScore::GetDecayPeriodInDays()));
1339 // Trigger decay and histogram hit.
1340 service->AddPoints(origin1, 0.01);
1341 }
1186 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, 1342 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram,
1187 3); 1343 kMorePeriodsThanNeededToDecayMaxScore);
1188 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1344 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1189 3); 1345 kMorePeriodsThanNeededToDecayMaxScore);
1346 // It should have taken (20 - 3) = 17 of the 38 decays to get to zero, since
1347 // we started from 95. Expect the remaining 21 decays to be to bucket 0 (and
1348 // hence 20 from bucket 0).
1349 histograms.ExpectBucketCount(
1350 SiteEngagementMetrics::kScoreDecayedFromHistogram, 0, 20);
1190 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1351 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1191 0, 1); 1352 0, 21);
1192 // Trigger decay and histogram hit for origin2, checking an independent decay. 1353 // Trigger decay and histogram hit for origin2, checking an independent decay.
1193 service->AddPoints(origin2, 0.01); 1354 service->AddPoints(origin2, 0.01);
1194 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, 1355 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram,
1195 4); 1356 kMorePeriodsThanNeededToDecayMaxScore + 1);
1196 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1357 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1197 4); 1358 kMorePeriodsThanNeededToDecayMaxScore + 1);
1198 histograms.ExpectBucketCount( 1359 histograms.ExpectBucketCount(
1199 SiteEngagementMetrics::kScoreDecayedFromHistogram, 0, 1); 1360 SiteEngagementMetrics::kScoreDecayedFromHistogram, 0, 21);
1200 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1361 histograms.ExpectBucketCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1201 0, 2); 1362 0, 22);
1202 1363
1203 // Add more points and ensure no more samples are present. 1364 // Add more points and ensure no more samples are present.
1204 service->AddPoints(origin1, 0.01); 1365 service->AddPoints(origin1, 0.01);
1205 service->AddPoints(origin2, 0.01); 1366 service->AddPoints(origin2, 0.01);
1206 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, 1367 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram,
1207 4); 1368 kMorePeriodsThanNeededToDecayMaxScore + 1);
1208 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1369 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1209 4); 1370 kMorePeriodsThanNeededToDecayMaxScore + 1);
1210 } 1371 }
1372
1373 TEST_F(SiteEngagementServiceTest, LastEngagementTime) {
1374 // The last engagement time should start off null in prefs and in the service.
1375 base::Time last_engagement_time = base::Time::FromInternalValue(
1376 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime));
1377
1378 ASSERT_TRUE(last_engagement_time.is_null());
1379
1380 base::SimpleTestClock* clock = new base::SimpleTestClock();
1381 std::unique_ptr<SiteEngagementService> service(
1382 new SiteEngagementService(profile(), base::WrapUnique(clock)));
1383
1384 ASSERT_TRUE(service->GetLastEngagementTime().is_null());
1385
1386 base::Time current_day = GetReferenceTime();
1387 clock->SetNow(current_day);
1388
1389 // Add points should set the last engagement time in the service, and persist
1390 // it to disk.
1391 GURL origin("http://www.google.com/");
1392 service->AddPoints(origin, 1);
1393
1394 last_engagement_time = base::Time::FromInternalValue(
1395 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime));
1396
1397 EXPECT_EQ(current_day, service->GetLastEngagementTime());
1398 EXPECT_EQ(current_day, last_engagement_time);
1399
1400 // Running a cleanup and updating last engagement times should persist the
1401 // last engagement time to disk.
1402 current_day += service->GetStalePeriod();
1403 base::Time rebased_time = current_day - service->GetMaxDecayPeriod();
1404 clock->SetNow(current_day);
1405 service->CleanupEngagementScores(true);
1406
1407 last_engagement_time = base::Time::FromInternalValue(
1408 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime));
1409
1410 EXPECT_EQ(rebased_time, last_engagement_time);
1411 EXPECT_EQ(rebased_time, service->GetLastEngagementTime());
1412
1413 // Add some more points and ensure the value is persisted.
1414 base::Time later_in_day = current_day + base::TimeDelta::FromSeconds(30);
1415 clock->SetNow(later_in_day);
1416 service->AddPoints(origin, 3);
1417
1418 last_engagement_time = base::Time::FromInternalValue(
1419 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime));
1420
1421 EXPECT_EQ(later_in_day, last_engagement_time);
1422 EXPECT_EQ(later_in_day, service->GetLastEngagementTime());
1423 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.cc ('k') | chrome/browser/profiles/profile.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698