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 void UpdateExpectations(const GURL& url, double score) { | |
calamity
2016/05/24 08:09:33
Unused.
dominickn
2016/05/25 07:21:32
Done.
| |
126 url_ = url; | |
127 score_ = score; | |
128 set_callback_called(false); | |
129 } | |
130 | |
131 private: | |
132 GURL url_; | |
133 double score_; | |
134 bool callback_called_; | |
135 base::RunLoop run_loop_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(ObserverTester); | |
138 }; | |
139 | |
100 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { | 140 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { |
101 public: | 141 public: |
102 void SetUp() override { | 142 void SetUp() override { |
103 ChromeRenderViewHostTestHarness::SetUp(); | 143 ChromeRenderViewHostTestHarness::SetUp(); |
104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 144 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
105 g_temp_history_dir = temp_dir_.path(); | 145 g_temp_history_dir = temp_dir_.path(); |
106 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 146 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
107 profile(), &BuildTestHistoryService); | 147 profile(), &BuildTestHistoryService); |
108 SiteEngagementScore::SetParamValuesForTesting(); | 148 SiteEngagementScore::SetParamValuesForTesting(); |
109 } | 149 } |
(...skipping 866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
976 EXPECT_TRUE(service->IsEngagementAtLeast( | 1016 EXPECT_TRUE(service->IsEngagementAtLeast( |
977 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW)); | 1017 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW)); |
978 EXPECT_TRUE(service->IsEngagementAtLeast( | 1018 EXPECT_TRUE(service->IsEngagementAtLeast( |
979 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); | 1019 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); |
980 EXPECT_TRUE(service->IsEngagementAtLeast( | 1020 EXPECT_TRUE(service->IsEngagementAtLeast( |
981 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH)); | 1021 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH)); |
982 EXPECT_TRUE(service->IsEngagementAtLeast( | 1022 EXPECT_TRUE(service->IsEngagementAtLeast( |
983 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); | 1023 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); |
984 } | 1024 } |
985 | 1025 |
1026 TEST_F(SiteEngagementServiceTest, Observers) { | |
1027 SiteEngagementService* service = SiteEngagementService::Get(profile()); | |
1028 | |
1029 GURL url_score_1("http://www.google.com/maps"); | |
1030 GURL url_score_2("http://www.google.com/drive"); | |
1031 GURL url_score_3("http://www.google.com/"); | |
1032 GURL url_not_called("https://www.google.com/"); | |
1033 | |
1034 // Create an observer and Observe(nullptr). | |
1035 ObserverTester tester_not_called(service, url_not_called, 1); | |
1036 tester_not_called.Observe(nullptr); | |
1037 | |
1038 { | |
1039 // Create an observer for score 1. | |
1040 ObserverTester tester(service, url_score_1, 1); | |
1041 service->AddPoints(url_score_1, 1); | |
1042 tester.Wait(); | |
1043 EXPECT_TRUE(tester.callback_called()); | |
1044 EXPECT_FALSE(tester_not_called.callback_called()); | |
1045 tester.Observe(nullptr); | |
1046 } | |
1047 | |
1048 { | |
1049 // Update observer for score 1.05 | |
1050 ObserverTester tester(service, url_score_2, 1.05); | |
1051 service->AddPoints(url_score_2, 0.05); | |
1052 tester.Wait(); | |
1053 EXPECT_TRUE(tester.callback_called()); | |
1054 EXPECT_FALSE(tester_not_called.callback_called()); | |
1055 tester.Observe(nullptr); | |
1056 } | |
1057 | |
1058 // Add two observers for score 1.55. | |
1059 { | |
1060 ObserverTester tester_1(service, url_score_3, 1.55); | |
1061 ObserverTester tester_2(service, url_score_3, 1.55); | |
1062 service->AddPoints(url_score_3, 0.5); | |
1063 tester_1.Wait(); | |
1064 tester_2.Wait(); | |
1065 | |
1066 EXPECT_TRUE(tester_1.callback_called()); | |
1067 EXPECT_TRUE(tester_2.callback_called()); | |
1068 EXPECT_FALSE(tester_not_called.callback_called()); | |
1069 tester_1.Observe(nullptr); | |
1070 tester_2.Observe(nullptr); | |
1071 } | |
1072 } | |
1073 | |
986 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { | 1074 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { |
987 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 1075 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
988 std::unique_ptr<SiteEngagementService> service( | 1076 std::unique_ptr<SiteEngagementService> service( |
989 new SiteEngagementService(profile(), base::WrapUnique(clock))); | 1077 new SiteEngagementService(profile(), base::WrapUnique(clock))); |
990 | 1078 |
991 base::Time current_day = GetReferenceTime(); | 1079 base::Time current_day = GetReferenceTime(); |
992 clock->SetNow(current_day); | 1080 clock->SetNow(current_day); |
993 base::HistogramTester histograms; | 1081 base::HistogramTester histograms; |
994 GURL origin1("http://www.google.com/"); | 1082 GURL origin1("http://www.google.com/"); |
995 GURL origin2("http://drive.google.com/"); | 1083 GURL origin2("http://drive.google.com/"); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1065 0, 2); | 1153 0, 2); |
1066 | 1154 |
1067 // Add more points and ensure no more samples are present. | 1155 // Add more points and ensure no more samples are present. |
1068 service->AddPoints(origin1, 0.01); | 1156 service->AddPoints(origin1, 0.01); |
1069 service->AddPoints(origin2, 0.01); | 1157 service->AddPoints(origin2, 0.01); |
1070 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1158 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
1071 4); | 1159 4); |
1072 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1160 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
1073 4); | 1161 4); |
1074 } | 1162 } |
OLD | NEW |