OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_score.h" | 5 #include "chrome/browser/engagement/site_engagement_score.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
12 #include "chrome/browser/engagement/site_engagement_service.h" | 13 #include "chrome/browser/engagement/site_engagement_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
14 | 19 |
15 namespace { | 20 namespace { |
16 | 21 |
17 const int kLessAccumulationsThanNeededToMaxDailyEngagement = 2; | 22 const int kLessAccumulationsThanNeededToMaxDailyEngagement = 2; |
18 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; | 23 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40; |
19 const int kMoreAccumulationsThanNeededToMaxTotalEngagement = 200; | 24 const int kMoreAccumulationsThanNeededToMaxTotalEngagement = 200; |
20 const int kLessDaysThanNeededToMaxTotalEngagement = 4; | 25 const int kLessDaysThanNeededToMaxTotalEngagement = 4; |
21 const int kMoreDaysThanNeededToMaxTotalEngagement = 40; | 26 const int kMoreDaysThanNeededToMaxTotalEngagement = 40; |
22 const int kLessPeriodsThanNeededToDecayMaxScore = 2; | 27 const int kLessPeriodsThanNeededToDecayMaxScore = 2; |
(...skipping 12 matching lines...) Expand all Loading... |
35 exploded_reference_time.millisecond = 0; | 40 exploded_reference_time.millisecond = 0; |
36 | 41 |
37 base::Time out_time; | 42 base::Time out_time; |
38 EXPECT_TRUE( | 43 EXPECT_TRUE( |
39 base::Time::FromLocalExploded(exploded_reference_time, &out_time)); | 44 base::Time::FromLocalExploded(exploded_reference_time, &out_time)); |
40 return out_time; | 45 return out_time; |
41 } | 46 } |
42 | 47 |
43 } // namespace | 48 } // namespace |
44 | 49 |
45 class SiteEngagementScoreTest : public testing::Test { | 50 class SiteEngagementScoreTest : public ChromeRenderViewHostTestHarness { |
46 public: | 51 public: |
47 SiteEngagementScoreTest() : score_(&test_clock_, GURL(), nullptr) {} | 52 SiteEngagementScoreTest() : score_(&test_clock_, GURL(), nullptr) {} |
48 | 53 |
49 void SetUp() override { | 54 void SetUp() override { |
50 testing::Test::SetUp(); | 55 ChromeRenderViewHostTestHarness::SetUp(); |
51 // Disable the first engagement bonus for tests. | 56 // Disable the first engagement bonus for tests. |
52 SiteEngagementScore::SetParamValuesForTesting(); | 57 SiteEngagementScore::SetParamValuesForTesting(); |
53 } | 58 } |
54 | 59 |
55 protected: | 60 protected: |
56 void VerifyScore(const SiteEngagementScore& score, | 61 void VerifyScore(const SiteEngagementScore& score, |
57 double expected_raw_score, | 62 double expected_raw_score, |
58 double expected_points_added_today, | 63 double expected_points_added_today, |
59 base::Time expected_last_engagement_time) { | 64 base::Time expected_last_engagement_time) { |
60 EXPECT_EQ(expected_raw_score, score.raw_score_); | 65 EXPECT_EQ(expected_raw_score, score.raw_score_); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // Accumulate score many times on the same day. Ensure each time the score goes | 112 // Accumulate score many times on the same day. Ensure each time the score goes |
108 // up, but not more than the maximum per day. | 113 // up, but not more than the maximum per day. |
109 TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) { | 114 TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) { |
110 base::Time reference_time = GetReferenceTime(); | 115 base::Time reference_time = GetReferenceTime(); |
111 | 116 |
112 test_clock_.SetNow(reference_time); | 117 test_clock_.SetNow(reference_time); |
113 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 118 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
114 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 119 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
115 EXPECT_EQ(std::min(SiteEngagementScore::GetMaxPointsPerDay(), | 120 EXPECT_EQ(std::min(SiteEngagementScore::GetMaxPointsPerDay(), |
116 (i + 1) * SiteEngagementScore::GetNavigationPoints()), | 121 (i + 1) * SiteEngagementScore::GetNavigationPoints()), |
117 score_.GetScore()); | 122 score_.GetTotalScore()); |
118 } | 123 } |
119 | 124 |
120 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 125 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotalScore()); |
121 } | 126 } |
122 | 127 |
123 // Accumulate on the first day to max that day's engagement, then accumulate on | 128 // Accumulate on the first day to max that day's engagement, then accumulate on |
124 // a different day. | 129 // a different day. |
125 TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) { | 130 TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) { |
126 base::Time reference_time = GetReferenceTime(); | 131 base::Time reference_time = GetReferenceTime(); |
127 base::Time later_date = reference_time + base::TimeDelta::FromDays(2); | 132 base::Time later_date = reference_time + base::TimeDelta::FromDays(2); |
128 | 133 |
129 test_clock_.SetNow(reference_time); | 134 test_clock_.SetNow(reference_time); |
130 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) | 135 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) |
131 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 136 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
132 | 137 |
133 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 138 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotalScore()); |
134 | 139 |
135 test_clock_.SetNow(later_date); | 140 test_clock_.SetNow(later_date); |
136 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 141 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
137 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 142 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
138 double day_score = | 143 double day_score = |
139 std::min(SiteEngagementScore::GetMaxPointsPerDay(), | 144 std::min(SiteEngagementScore::GetMaxPointsPerDay(), |
140 (i + 1) * SiteEngagementScore::GetNavigationPoints()); | 145 (i + 1) * SiteEngagementScore::GetNavigationPoints()); |
141 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), | 146 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), |
142 score_.GetScore()); | 147 score_.GetTotalScore()); |
143 } | 148 } |
144 | 149 |
145 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 150 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), |
| 151 score_.GetTotalScore()); |
146 } | 152 } |
147 | 153 |
148 // Accumulate score on many consecutive days and ensure the score doesn't exceed | 154 // Accumulate score on many consecutive days and ensure the score doesn't exceed |
149 // the maximum allowed. | 155 // the maximum allowed. |
150 TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) { | 156 TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) { |
151 base::Time current_day = GetReferenceTime(); | 157 base::Time current_day = GetReferenceTime(); |
152 | 158 |
153 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { | 159 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { |
154 current_day += base::TimeDelta::FromDays(1); | 160 current_day += base::TimeDelta::FromDays(1); |
155 test_clock_.SetNow(current_day); | 161 test_clock_.SetNow(current_day); |
156 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 162 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
157 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 163 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
158 | 164 |
159 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints, | 165 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints, |
160 (i + 1) * SiteEngagementScore::GetMaxPointsPerDay()), | 166 (i + 1) * SiteEngagementScore::GetMaxPointsPerDay()), |
161 score_.GetScore()); | 167 score_.GetTotalScore()); |
162 } | 168 } |
163 | 169 |
164 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 170 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore()); |
165 } | 171 } |
166 | 172 |
167 // Accumulate a little on many consecutive days and ensure the score doesn't | 173 // Accumulate a little on many consecutive days and ensure the score doesn't |
168 // exceed the maximum allowed. | 174 // exceed the maximum allowed. |
169 TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) { | 175 TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) { |
170 base::Time current_day = GetReferenceTime(); | 176 base::Time current_day = GetReferenceTime(); |
171 | 177 |
172 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) { | 178 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) { |
173 current_day += base::TimeDelta::FromDays(1); | 179 current_day += base::TimeDelta::FromDays(1); |
174 test_clock_.SetNow(current_day); | 180 test_clock_.SetNow(current_day); |
175 | 181 |
176 for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j) | 182 for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j) |
177 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 183 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
178 | 184 |
179 EXPECT_EQ( | 185 EXPECT_EQ( |
180 std::min(SiteEngagementScore::kMaxPoints, | 186 std::min(SiteEngagementScore::kMaxPoints, |
181 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement * | 187 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement * |
182 SiteEngagementScore::GetNavigationPoints()), | 188 SiteEngagementScore::GetNavigationPoints()), |
183 score_.GetScore()); | 189 score_.GetTotalScore()); |
184 } | 190 } |
185 | 191 |
186 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 192 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore()); |
187 } | 193 } |
188 | 194 |
189 // Accumulate a bit, then check the score decays properly for a range of times. | 195 // Accumulate a bit, then check the score decays properly for a range of times. |
190 TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) { | 196 TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) { |
191 base::Time current_day = GetReferenceTime(); | 197 base::Time current_day = GetReferenceTime(); |
192 | 198 |
193 // First max the score. | 199 // First max the score. |
194 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { | 200 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { |
195 current_day += base::TimeDelta::FromDays(1); | 201 current_day += base::TimeDelta::FromDays(1); |
196 test_clock_.SetNow(current_day); | 202 test_clock_.SetNow(current_day); |
197 | 203 |
198 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 204 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
199 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 205 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
200 } | 206 } |
201 | 207 |
202 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 208 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore()); |
203 | 209 |
204 // The score should not have decayed before the first decay period has | 210 // The score should not have decayed before the first decay period has |
205 // elapsed. | 211 // elapsed. |
206 test_clock_.SetNow(current_day + | 212 test_clock_.SetNow(current_day + |
207 base::TimeDelta::FromHours( | 213 base::TimeDelta::FromHours( |
208 SiteEngagementScore::GetDecayPeriodInHours() - 1)); | 214 SiteEngagementScore::GetDecayPeriodInHours() - 1)); |
209 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 215 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore()); |
210 | 216 |
211 // The score should have decayed by one chunk after one decay period has | 217 // The score should have decayed by one chunk after one decay period has |
212 // elapsed. | 218 // elapsed. |
213 test_clock_.SetNow( | 219 test_clock_.SetNow( |
214 current_day + | 220 current_day + |
215 base::TimeDelta::FromHours(SiteEngagementScore::GetDecayPeriodInHours())); | 221 base::TimeDelta::FromHours(SiteEngagementScore::GetDecayPeriodInHours())); |
216 EXPECT_EQ( | 222 EXPECT_EQ( |
217 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(), | 223 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(), |
218 score_.GetScore()); | 224 score_.GetTotalScore()); |
219 | 225 |
220 // The score should have decayed by the right number of chunks after a few | 226 // The score should have decayed by the right number of chunks after a few |
221 // decay periods have elapsed. | 227 // decay periods have elapsed. |
222 test_clock_.SetNow( | 228 test_clock_.SetNow( |
223 current_day + | 229 current_day + |
224 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * | 230 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * |
225 SiteEngagementScore::GetDecayPeriodInHours())); | 231 SiteEngagementScore::GetDecayPeriodInHours())); |
226 EXPECT_EQ(SiteEngagementScore::kMaxPoints - | 232 EXPECT_EQ(SiteEngagementScore::kMaxPoints - |
227 kLessPeriodsThanNeededToDecayMaxScore * | 233 kLessPeriodsThanNeededToDecayMaxScore * |
228 SiteEngagementScore::GetDecayPoints(), | 234 SiteEngagementScore::GetDecayPoints(), |
229 score_.GetScore()); | 235 score_.GetTotalScore()); |
230 | 236 |
231 // The score should not decay below zero. | 237 // The score should not decay below zero. |
232 test_clock_.SetNow( | 238 test_clock_.SetNow( |
233 current_day + | 239 current_day + |
234 base::TimeDelta::FromHours(kMorePeriodsThanNeededToDecayMaxScore * | 240 base::TimeDelta::FromHours(kMorePeriodsThanNeededToDecayMaxScore * |
235 SiteEngagementScore::GetDecayPeriodInHours())); | 241 SiteEngagementScore::GetDecayPeriodInHours())); |
236 EXPECT_EQ(0, score_.GetScore()); | 242 EXPECT_EQ(0, score_.GetTotalScore()); |
237 } | 243 } |
238 | 244 |
239 // Test that any expected decays are applied before adding points. | 245 // Test that any expected decays are applied before adding points. |
240 TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) { | 246 TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) { |
241 base::Time current_day = GetReferenceTime(); | 247 base::Time current_day = GetReferenceTime(); |
242 | 248 |
243 // Get the score up to something that can handle a bit of decay before | 249 // Get the score up to something that can handle a bit of decay before |
244 for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) { | 250 for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) { |
245 current_day += base::TimeDelta::FromDays(1); | 251 current_day += base::TimeDelta::FromDays(1); |
246 test_clock_.SetNow(current_day); | 252 test_clock_.SetNow(current_day); |
247 | 253 |
248 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 254 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
249 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 255 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
250 } | 256 } |
251 | 257 |
252 double initial_score = kLessDaysThanNeededToMaxTotalEngagement * | 258 double initial_score = kLessDaysThanNeededToMaxTotalEngagement * |
253 SiteEngagementScore::GetMaxPointsPerDay(); | 259 SiteEngagementScore::GetMaxPointsPerDay(); |
254 EXPECT_EQ(initial_score, score_.GetScore()); | 260 EXPECT_EQ(initial_score, score_.GetTotalScore()); |
255 | 261 |
256 // Go forward a few decay periods. | 262 // Go forward a few decay periods. |
257 test_clock_.SetNow( | 263 test_clock_.SetNow( |
258 current_day + | 264 current_day + |
259 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * | 265 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * |
260 SiteEngagementScore::GetDecayPeriodInHours())); | 266 SiteEngagementScore::GetDecayPeriodInHours())); |
261 | 267 |
262 double decayed_score = initial_score - | 268 double decayed_score = initial_score - |
263 kLessPeriodsThanNeededToDecayMaxScore * | 269 kLessPeriodsThanNeededToDecayMaxScore * |
264 SiteEngagementScore::GetDecayPoints(); | 270 SiteEngagementScore::GetDecayPoints(); |
265 EXPECT_EQ(decayed_score, score_.GetScore()); | 271 EXPECT_EQ(decayed_score, score_.GetTotalScore()); |
266 | 272 |
267 // Now add some points. | 273 // Now add some points. |
268 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 274 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
269 EXPECT_EQ(decayed_score + SiteEngagementScore::GetNavigationPoints(), | 275 EXPECT_EQ(decayed_score + SiteEngagementScore::GetNavigationPoints(), |
270 score_.GetScore()); | 276 score_.GetTotalScore()); |
271 } | 277 } |
272 | 278 |
273 // Test that going back in time is handled properly. | 279 // Test that going back in time is handled properly. |
274 TEST_F(SiteEngagementScoreTest, GoBackInTime) { | 280 TEST_F(SiteEngagementScoreTest, GoBackInTime) { |
275 base::Time current_day = GetReferenceTime(); | 281 base::Time current_day = GetReferenceTime(); |
276 | 282 |
277 test_clock_.SetNow(current_day); | 283 test_clock_.SetNow(current_day); |
278 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) | 284 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) |
279 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 285 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
280 | 286 |
281 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 287 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotalScore()); |
282 | 288 |
283 // Adding to the score on an earlier date should be treated like another day, | 289 // Adding to the score on an earlier date should be treated like another day, |
284 // and should not cause any decay. | 290 // and should not cause any decay. |
285 test_clock_.SetNow(current_day - base::TimeDelta::FromDays( | 291 test_clock_.SetNow(current_day - base::TimeDelta::FromDays( |
286 kMorePeriodsThanNeededToDecayMaxScore * | 292 kMorePeriodsThanNeededToDecayMaxScore * |
287 SiteEngagementScore::GetDecayPoints())); | 293 SiteEngagementScore::GetDecayPoints())); |
288 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 294 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
289 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 295 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
290 double day_score = | 296 double day_score = |
291 std::min(SiteEngagementScore::GetMaxPointsPerDay(), | 297 std::min(SiteEngagementScore::GetMaxPointsPerDay(), |
292 (i + 1) * SiteEngagementScore::GetNavigationPoints()); | 298 (i + 1) * SiteEngagementScore::GetNavigationPoints()); |
293 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), | 299 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), |
294 score_.GetScore()); | 300 score_.GetTotalScore()); |
295 } | 301 } |
296 | 302 |
297 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 303 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), |
| 304 score_.GetTotalScore()); |
298 } | 305 } |
299 | 306 |
300 // Test that scores are read / written correctly from / to empty score | 307 // Test that scores are read / written correctly from / to empty score |
301 // dictionaries. | 308 // dictionaries. |
302 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { | 309 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { |
303 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 310 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
304 TestScoreInitializesAndUpdates(std::move(dict), 0, 0, base::Time()); | 311 TestScoreInitializesAndUpdates(std::move(dict), 0, 0, base::Time()); |
305 } | 312 } |
306 | 313 |
307 // Test that scores are read / written correctly from / to partially empty | 314 // Test that scores are read / written correctly from / to partially empty |
(...skipping 24 matching lines...) Expand all Loading... |
332 SiteEngagementScore score1(&test_clock_, GURL(), | 339 SiteEngagementScore score1(&test_clock_, GURL(), |
333 std::unique_ptr<base::DictionaryValue>()); | 340 std::unique_ptr<base::DictionaryValue>()); |
334 SiteEngagementScore score2(&test_clock_, GURL(), | 341 SiteEngagementScore score2(&test_clock_, GURL(), |
335 std::unique_ptr<base::DictionaryValue>()); | 342 std::unique_ptr<base::DictionaryValue>()); |
336 base::Time current_day = GetReferenceTime(); | 343 base::Time current_day = GetReferenceTime(); |
337 | 344 |
338 test_clock_.SetNow(current_day); | 345 test_clock_.SetNow(current_day); |
339 | 346 |
340 // The first engagement event gets the bonus. | 347 // The first engagement event gets the bonus. |
341 score1.AddPoints(0.5); | 348 score1.AddPoints(0.5); |
342 EXPECT_EQ(1.0, score1.GetScore()); | 349 EXPECT_EQ(1.0, score1.GetTotalScore()); |
343 | 350 |
344 // Subsequent events do not. | 351 // Subsequent events do not. |
345 score1.AddPoints(0.5); | 352 score1.AddPoints(0.5); |
346 EXPECT_EQ(1.5, score1.GetScore()); | 353 EXPECT_EQ(1.5, score1.GetTotalScore()); |
347 | 354 |
348 // Bonuses are awarded independently between scores. | 355 // Bonuses are awarded independently between scores. |
349 score2.AddPoints(1.0); | 356 score2.AddPoints(1.0); |
350 EXPECT_EQ(1.5, score2.GetScore()); | 357 EXPECT_EQ(1.5, score2.GetTotalScore()); |
351 score2.AddPoints(1.0); | 358 score2.AddPoints(1.0); |
352 EXPECT_EQ(2.5, score2.GetScore()); | 359 EXPECT_EQ(2.5, score2.GetTotalScore()); |
353 | 360 |
354 test_clock_.SetNow(current_day + base::TimeDelta::FromDays(1)); | 361 test_clock_.SetNow(current_day + base::TimeDelta::FromDays(1)); |
355 | 362 |
356 // The first event for the next day gets the bonus. | 363 // The first event for the next day gets the bonus. |
357 score1.AddPoints(0.5); | 364 score1.AddPoints(0.5); |
358 EXPECT_EQ(2.5, score1.GetScore()); | 365 EXPECT_EQ(2.5, score1.GetTotalScore()); |
359 | 366 |
360 // Subsequent events do not. | 367 // Subsequent events do not. |
361 score1.AddPoints(0.5); | 368 score1.AddPoints(0.5); |
362 EXPECT_EQ(3.0, score1.GetScore()); | 369 EXPECT_EQ(3.0, score1.GetTotalScore()); |
363 | 370 |
364 score2.AddPoints(1.0); | 371 score2.AddPoints(1.0); |
365 EXPECT_EQ(4.0, score2.GetScore()); | 372 EXPECT_EQ(4.0, score2.GetTotalScore()); |
366 score2.AddPoints(1.0); | 373 score2.AddPoints(1.0); |
367 EXPECT_EQ(5.0, score2.GetScore()); | 374 EXPECT_EQ(5.0, score2.GetTotalScore()); |
368 } | 375 } |
369 | 376 |
370 // Test that resetting a score has the correct properties. | 377 // Test that resetting a score has the correct properties. |
371 TEST_F(SiteEngagementScoreTest, Reset) { | 378 TEST_F(SiteEngagementScoreTest, Reset) { |
372 base::Time current_day = GetReferenceTime(); | 379 base::Time current_day = GetReferenceTime(); |
373 | 380 |
374 test_clock_.SetNow(current_day); | 381 test_clock_.SetNow(current_day); |
375 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 382 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
376 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetScore()); | 383 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetTotalScore()); |
377 | 384 |
378 current_day += base::TimeDelta::FromDays(7); | 385 current_day += base::TimeDelta::FromDays(7); |
379 test_clock_.SetNow(current_day); | 386 test_clock_.SetNow(current_day); |
380 | 387 |
381 score_.Reset(20.0, current_day); | 388 score_.Reset(20.0, current_day); |
382 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); | 389 EXPECT_DOUBLE_EQ(20.0, score_.GetTotalScore()); |
383 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 390 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
384 EXPECT_EQ(current_day, score_.last_engagement_time_); | 391 EXPECT_EQ(current_day, score_.last_engagement_time_); |
385 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); | 392 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
386 | 393 |
387 // Adding points after the reset should work as normal. | 394 // Adding points after the reset should work as normal. |
388 score_.AddPoints(5); | 395 score_.AddPoints(5); |
389 EXPECT_EQ(25.0, score_.GetScore()); | 396 EXPECT_EQ(25.0, score_.GetTotalScore()); |
390 | 397 |
391 // The decay should happen one decay period from the current time. | 398 // The decay should happen one decay period from the current time. |
392 test_clock_.SetNow(current_day + | 399 test_clock_.SetNow(current_day + |
393 base::TimeDelta::FromHours( | 400 base::TimeDelta::FromHours( |
394 SiteEngagementScore::GetDecayPeriodInHours() + 1)); | 401 SiteEngagementScore::GetDecayPeriodInHours() + 1)); |
395 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.GetScore()); | 402 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), |
| 403 score_.GetTotalScore()); |
396 | 404 |
397 // Ensure that manually setting a time works as expected. | 405 // Ensure that manually setting a time works as expected. |
398 score_.AddPoints(5); | 406 score_.AddPoints(5); |
399 test_clock_.SetNow(GetReferenceTime()); | 407 test_clock_.SetNow(GetReferenceTime()); |
400 base::Time now = test_clock_.Now(); | 408 base::Time now = test_clock_.Now(); |
401 score_.Reset(10.0, now); | 409 score_.Reset(10.0, now); |
402 | 410 |
403 EXPECT_DOUBLE_EQ(10.0, score_.GetScore()); | 411 EXPECT_DOUBLE_EQ(10.0, score_.GetTotalScore()); |
404 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 412 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
405 EXPECT_EQ(now, score_.last_engagement_time_); | 413 EXPECT_EQ(now, score_.last_engagement_time_); |
406 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); | 414 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
407 | 415 |
408 base::Time old_now = test_clock_.Now(); | 416 base::Time old_now = test_clock_.Now(); |
409 | 417 |
410 score_.set_last_shortcut_launch_time(test_clock_.Now()); | 418 score_.set_last_shortcut_launch_time(test_clock_.Now()); |
411 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3)); | 419 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3)); |
412 now = test_clock_.Now(); | 420 now = test_clock_.Now(); |
413 score_.Reset(15.0, now); | 421 score_.Reset(15.0, now); |
414 | 422 |
415 // 5 bonus from the last shortcut launch. | 423 // 5 bonus from the last shortcut launch. |
416 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); | 424 EXPECT_DOUBLE_EQ(20.0, score_.GetTotalScore()); |
417 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 425 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
418 EXPECT_EQ(now, score_.last_engagement_time_); | 426 EXPECT_EQ(now, score_.last_engagement_time_); |
419 EXPECT_EQ(old_now, score_.last_shortcut_launch_time_); | 427 EXPECT_EQ(old_now, score_.last_shortcut_launch_time_); |
420 } | 428 } |
421 | 429 |
422 // Test proportional decay. | 430 // Test proportional decay. |
423 TEST_F(SiteEngagementScoreTest, ProportionalDecay) { | 431 TEST_F(SiteEngagementScoreTest, ProportionalDecay) { |
424 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); | 432 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); |
425 SetParamValue(SiteEngagementScore::DECAY_POINTS, 0); | 433 SetParamValue(SiteEngagementScore::DECAY_POINTS, 0); |
426 SetParamValue(SiteEngagementScore::MAX_POINTS_PER_DAY, 20); | 434 SetParamValue(SiteEngagementScore::MAX_POINTS_PER_DAY, 20); |
427 base::Time current_day = GetReferenceTime(); | 435 base::Time current_day = GetReferenceTime(); |
428 test_clock_.SetNow(current_day); | 436 test_clock_.SetNow(current_day); |
429 | 437 |
430 // Single decay period, expect the score to be halved once. | 438 // Single decay period, expect the score to be halved once. |
431 score_.AddPoints(2.0); | 439 score_.AddPoints(2.0); |
432 current_day += base::TimeDelta::FromDays(7); | 440 current_day += base::TimeDelta::FromDays(7); |
433 test_clock_.SetNow(current_day); | 441 test_clock_.SetNow(current_day); |
434 EXPECT_DOUBLE_EQ(1.0, score_.GetScore()); | 442 EXPECT_DOUBLE_EQ(1.0, score_.GetTotalScore()); |
435 | 443 |
436 // 3 decay periods, expect the score to be halved 3 times. | 444 // 3 decay periods, expect the score to be halved 3 times. |
437 score_.AddPoints(15.0); | 445 score_.AddPoints(15.0); |
438 current_day += base::TimeDelta::FromDays(21); | 446 current_day += base::TimeDelta::FromDays(21); |
439 test_clock_.SetNow(current_day); | 447 test_clock_.SetNow(current_day); |
440 EXPECT_DOUBLE_EQ(2.0, score_.GetScore()); | 448 EXPECT_DOUBLE_EQ(2.0, score_.GetTotalScore()); |
441 | 449 |
442 // Ensure point removal happens after proportional decay. | 450 // Ensure point removal happens after proportional decay. |
443 score_.AddPoints(4.0); | 451 score_.AddPoints(4.0); |
444 EXPECT_DOUBLE_EQ(6.0, score_.GetScore()); | 452 EXPECT_DOUBLE_EQ(6.0, score_.GetTotalScore()); |
445 SetParamValue(SiteEngagementScore::DECAY_POINTS, 2.0); | 453 SetParamValue(SiteEngagementScore::DECAY_POINTS, 2.0); |
446 current_day += base::TimeDelta::FromDays(7); | 454 current_day += base::TimeDelta::FromDays(7); |
447 test_clock_.SetNow(current_day); | 455 test_clock_.SetNow(current_day); |
448 EXPECT_NEAR(1.0, score_.GetScore(), kMaxRoundingDeviation); | 456 EXPECT_NEAR(1.0, score_.GetTotalScore(), kMaxRoundingDeviation); |
449 } | 457 } |
| 458 |
| 459 // Verify that GetDetails fills out all fields correctly. |
| 460 TEST_F(SiteEngagementScoreTest, GetDetails) { |
| 461 // Advance the clock, otherwise Now() is the same as the null Time value. |
| 462 test_clock_.Advance(base::TimeDelta::FromDays(365)); |
| 463 |
| 464 GURL url("http://www.google.com/"); |
| 465 |
| 466 // Replace |score_| with one with an actual URL, and with a settings map. |
| 467 HostContentSettingsMap* settings_map = |
| 468 HostContentSettingsMapFactory::GetForProfile(profile()); |
| 469 score_ = SiteEngagementScore(&test_clock_, url, settings_map); |
| 470 |
| 471 // Initially all component scores should be zero. |
| 472 mojom::SiteEngagementDetails details = score_.GetDetails(); |
| 473 EXPECT_DOUBLE_EQ(0.0, details.total_score); |
| 474 EXPECT_DOUBLE_EQ(0.0, details.installed_bonus); |
| 475 EXPECT_DOUBLE_EQ(0.0, details.notifications_bonus); |
| 476 EXPECT_DOUBLE_EQ(0.0, details.base_score); |
| 477 EXPECT_EQ(url, details.origin); |
| 478 |
| 479 // Add notifications permission and verify that impacts the correct bonus, |
| 480 // and the total. |
| 481 settings_map->SetContentSettingDefaultScope( |
| 482 url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 483 CONTENT_SETTING_ALLOW); |
| 484 details = score_.GetDetails(); |
| 485 EXPECT_DOUBLE_EQ(details.notifications_bonus, details.total_score); |
| 486 EXPECT_DOUBLE_EQ(0.0, details.installed_bonus); |
| 487 EXPECT_LT(0.0, details.notifications_bonus); |
| 488 EXPECT_DOUBLE_EQ(0.0, details.base_score); |
| 489 |
| 490 // Simulate the app having been launched. |
| 491 score_.set_last_shortcut_launch_time(test_clock_.Now()); |
| 492 details = score_.GetDetails(); |
| 493 EXPECT_DOUBLE_EQ(details.installed_bonus + details.notifications_bonus, |
| 494 details.total_score); |
| 495 EXPECT_LT(0.0, details.installed_bonus); |
| 496 EXPECT_LT(0.0, details.notifications_bonus); |
| 497 EXPECT_DOUBLE_EQ(0.0, details.base_score); |
| 498 } |
OLD | NEW |