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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 std::unique_ptr<KeyedService> BuildTestHistoryService( | 90 std::unique_ptr<KeyedService> BuildTestHistoryService( |
91 content::BrowserContext* context) { | 91 content::BrowserContext* context) { |
92 std::unique_ptr<history::HistoryService> service( | 92 std::unique_ptr<history::HistoryService> service( |
93 new history::HistoryService()); | 93 new history::HistoryService()); |
94 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); | 94 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); |
95 return std::move(service); | 95 return std::move(service); |
96 } | 96 } |
97 | 97 |
98 } // namespace | 98 } // namespace |
99 | 99 |
| 100 class ObserverTester : public SiteEngagementObserver { |
| 101 public: |
| 102 ObserverTester(SiteEngagementService* service, const GURL& url, double score) |
| 103 : SiteEngagementObserver(service), |
| 104 url_(url), |
| 105 score_(score), |
| 106 callback_called_(false), |
| 107 run_loop_() {} |
| 108 |
| 109 void OnEngagementIncreased(const SiteEngagementService* service, |
| 110 const GURL& url, |
| 111 double score) override { |
| 112 EXPECT_EQ(url_, url); |
| 113 EXPECT_EQ(score_, score); |
| 114 set_callback_called(true); |
| 115 run_loop_.Quit(); |
| 116 } |
| 117 |
| 118 void Wait() { run_loop_.Run(); } |
| 119 |
| 120 bool callback_called() { return callback_called_; } |
| 121 void set_callback_called(bool callback_called) { |
| 122 callback_called_ = callback_called; |
| 123 } |
| 124 |
| 125 private: |
| 126 GURL url_; |
| 127 double score_; |
| 128 bool callback_called_; |
| 129 base::RunLoop run_loop_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(ObserverTester); |
| 132 }; |
| 133 |
100 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { | 134 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { |
101 public: | 135 public: |
102 void SetUp() override { | 136 void SetUp() override { |
103 ChromeRenderViewHostTestHarness::SetUp(); | 137 ChromeRenderViewHostTestHarness::SetUp(); |
104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 138 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
105 g_temp_history_dir = temp_dir_.path(); | 139 g_temp_history_dir = temp_dir_.path(); |
106 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 140 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
107 profile(), &BuildTestHistoryService); | 141 profile(), &BuildTestHistoryService); |
108 SiteEngagementScore::SetParamValuesForTesting(); | 142 SiteEngagementScore::SetParamValuesForTesting(); |
109 } | 143 } |
(...skipping 866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
976 EXPECT_TRUE(service->IsEngagementAtLeast( | 1010 EXPECT_TRUE(service->IsEngagementAtLeast( |
977 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW)); | 1011 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW)); |
978 EXPECT_TRUE(service->IsEngagementAtLeast( | 1012 EXPECT_TRUE(service->IsEngagementAtLeast( |
979 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); | 1013 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); |
980 EXPECT_TRUE(service->IsEngagementAtLeast( | 1014 EXPECT_TRUE(service->IsEngagementAtLeast( |
981 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH)); | 1015 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH)); |
982 EXPECT_TRUE(service->IsEngagementAtLeast( | 1016 EXPECT_TRUE(service->IsEngagementAtLeast( |
983 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); | 1017 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); |
984 } | 1018 } |
985 | 1019 |
| 1020 TEST_F(SiteEngagementServiceTest, Observers) { |
| 1021 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| 1022 |
| 1023 GURL url_score_1("http://www.google.com/maps"); |
| 1024 GURL url_score_2("http://www.google.com/drive"); |
| 1025 GURL url_score_3("http://www.google.com/"); |
| 1026 GURL url_not_called("https://www.google.com/"); |
| 1027 |
| 1028 // Create an observer and Observe(nullptr). |
| 1029 ObserverTester tester_not_called(service, url_not_called, 1); |
| 1030 tester_not_called.Observe(nullptr); |
| 1031 |
| 1032 { |
| 1033 // Create an observer for score 1. |
| 1034 ObserverTester tester(service, url_score_1, 1); |
| 1035 service->AddPoints(url_score_1, 1); |
| 1036 tester.Wait(); |
| 1037 EXPECT_TRUE(tester.callback_called()); |
| 1038 EXPECT_FALSE(tester_not_called.callback_called()); |
| 1039 tester.Observe(nullptr); |
| 1040 } |
| 1041 |
| 1042 { |
| 1043 // Update observer for score 1.05 |
| 1044 ObserverTester tester(service, url_score_2, 1.05); |
| 1045 service->AddPoints(url_score_2, 0.05); |
| 1046 tester.Wait(); |
| 1047 EXPECT_TRUE(tester.callback_called()); |
| 1048 EXPECT_FALSE(tester_not_called.callback_called()); |
| 1049 tester.Observe(nullptr); |
| 1050 } |
| 1051 |
| 1052 // Add two observers for score 1.55. |
| 1053 { |
| 1054 ObserverTester tester_1(service, url_score_3, 1.55); |
| 1055 ObserverTester tester_2(service, url_score_3, 1.55); |
| 1056 service->AddPoints(url_score_3, 0.5); |
| 1057 tester_1.Wait(); |
| 1058 tester_2.Wait(); |
| 1059 |
| 1060 EXPECT_TRUE(tester_1.callback_called()); |
| 1061 EXPECT_TRUE(tester_2.callback_called()); |
| 1062 EXPECT_FALSE(tester_not_called.callback_called()); |
| 1063 tester_1.Observe(nullptr); |
| 1064 tester_2.Observe(nullptr); |
| 1065 } |
| 1066 } |
| 1067 |
986 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { | 1068 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { |
987 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1069 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
988 std::unique_ptr<SiteEngagementService> service( | 1070 std::unique_ptr<SiteEngagementService> service( |
989 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1071 new SiteEngagementService(profile(), base::WrapUnique(clock))); |
990 | 1072 |
991 base::Time current_day = GetReferenceTime(); | 1073 base::Time current_day = GetReferenceTime(); |
992 clock->SetNow(current_day); | 1074 clock->SetNow(current_day); |
993 base::HistogramTester histograms; | 1075 base::HistogramTester histograms; |
994 GURL origin1("http://www.google.com/"); | 1076 GURL origin1("http://www.google.com/"); |
995 GURL origin2("http://drive.google.com/"); | 1077 GURL origin2("http://drive.google.com/"); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1065 0, 2); | 1147 0, 2); |
1066 | 1148 |
1067 // Add more points and ensure no more samples are present. | 1149 // Add more points and ensure no more samples are present. |
1068 service->AddPoints(origin1, 0.01); | 1150 service->AddPoints(origin1, 0.01); |
1069 service->AddPoints(origin2, 0.01); | 1151 service->AddPoints(origin2, 0.01); |
1070 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1152 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1071 4); | 1153 4); |
1072 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1154 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1073 4); | 1155 4); |
1074 } | 1156 } |
OLD | NEW |