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

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

Issue 1975723002: Reduce the site engagement service public interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Push messaging now uses engagement Created 4 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/engagement/site_engagement_score.h"
6
7 #include <utility>
8
9 #include "base/macros.h"
10 #include "base/test/simple_test_clock.h"
11 #include "base/values.h"
12 #include "chrome/browser/engagement/site_engagement_service.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 const int kLessAccumulationsThanNeededToMaxDailyEngagement = 2;
18 const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40;
19 const int kMoreAccumulationsThanNeededToMaxTotalEngagement = 200;
20 const int kLessDaysThanNeededToMaxTotalEngagement = 4;
21 const int kMoreDaysThanNeededToMaxTotalEngagement = 40;
22 const int kLessPeriodsThanNeededToDecayMaxScore = 2;
23 const int kMorePeriodsThanNeededToDecayMaxScore = 40;
24
25 base::Time GetReferenceTime() {
26 base::Time::Exploded exploded_reference_time;
27 exploded_reference_time.year = 2015;
28 exploded_reference_time.month = 1;
29 exploded_reference_time.day_of_month = 30;
30 exploded_reference_time.day_of_week = 5;
31 exploded_reference_time.hour = 11;
32 exploded_reference_time.minute = 0;
33 exploded_reference_time.second = 0;
34 exploded_reference_time.millisecond = 0;
35
36 return base::Time::FromLocalExploded(exploded_reference_time);
37 }
38
39 } // namespace
40
41 class SiteEngagementScoreTest : public testing::Test {
42 public:
43 SiteEngagementScoreTest() : score_(&test_clock_) {}
44
45 void SetUp() override {
46 testing::Test::SetUp();
47 // Disable the first engagement bonus for tests.
48 SiteEngagementScore::SetParamValuesForTesting();
49 }
50
51 protected:
52 void VerifyScore(const SiteEngagementScore& score,
53 double expected_raw_score,
54 double expected_points_added_today,
55 base::Time expected_last_engagement_time) {
56 EXPECT_EQ(expected_raw_score, score.raw_score_);
57 EXPECT_EQ(expected_points_added_today, score.points_added_today_);
58 EXPECT_EQ(expected_last_engagement_time, score.last_engagement_time_);
59 }
60
61 void UpdateScore(SiteEngagementScore* score,
62 double raw_score,
63 double points_added_today,
64 base::Time last_engagement_time) {
65 score->raw_score_ = raw_score;
66 score->points_added_today_ = points_added_today;
67 score->last_engagement_time_ = last_engagement_time;
68 }
69
70 void TestScoreInitializesAndUpdates(
71 base::DictionaryValue* score_dict,
72 double expected_raw_score,
73 double expected_points_added_today,
74 base::Time expected_last_engagement_time) {
75 SiteEngagementScore initial_score(&test_clock_, *score_dict);
76 VerifyScore(initial_score, expected_raw_score, expected_points_added_today,
77 expected_last_engagement_time);
78
79 // Updating the score dict should return false, as the score shouldn't
80 // have changed at this point.
81 EXPECT_FALSE(initial_score.UpdateScoreDict(score_dict));
82
83 // Update the score to new values and verify it updates the score dict
84 // correctly.
85 base::Time different_day =
86 GetReferenceTime() + base::TimeDelta::FromDays(1);
87 UpdateScore(&initial_score, 5, 10, different_day);
88 EXPECT_TRUE(initial_score.UpdateScoreDict(score_dict));
89 SiteEngagementScore updated_score(&test_clock_, *score_dict);
90 VerifyScore(updated_score, 5, 10, different_day);
91 }
92
93 void SetFirstDailyEngagementPointsForTesting(double points) {
94 SiteEngagementScore::param_values
95 [SiteEngagementScore::FIRST_DAILY_ENGAGEMENT] = points;
96 }
97
98 base::SimpleTestClock test_clock_;
99 SiteEngagementScore score_;
100 };
101
102 // Accumulate score many times on the same day. Ensure each time the score goes
103 // up, but not more than the maximum per day.
104 TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) {
105 base::Time reference_time = GetReferenceTime();
106
107 test_clock_.SetNow(reference_time);
108 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
109 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
110 EXPECT_EQ(std::min(SiteEngagementScore::GetMaxPointsPerDay(),
111 (i + 1) * SiteEngagementScore::GetNavigationPoints()),
112 score_.GetScore());
113 }
114
115 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore());
116 }
117
118 // Accumulate on the first day to max that day's engagement, then accumulate on
119 // a different day.
120 TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) {
121 base::Time reference_time = GetReferenceTime();
122 base::Time later_date = reference_time + base::TimeDelta::FromDays(2);
123
124 test_clock_.SetNow(reference_time);
125 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i)
126 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
127
128 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore());
129
130 test_clock_.SetNow(later_date);
131 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
132 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
133 double day_score =
134 std::min(SiteEngagementScore::GetMaxPointsPerDay(),
135 (i + 1) * SiteEngagementScore::GetNavigationPoints());
136 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(),
137 score_.GetScore());
138 }
139
140 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore());
141 }
142
143 // Accumulate score on many consecutive days and ensure the score doesn't exceed
144 // the maximum allowed.
145 TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) {
146 base::Time current_day = GetReferenceTime();
147
148 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) {
149 current_day += base::TimeDelta::FromDays(1);
150 test_clock_.SetNow(current_day);
151 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
152 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
153
154 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints,
155 (i + 1) * SiteEngagementScore::GetMaxPointsPerDay()),
156 score_.GetScore());
157 }
158
159 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore());
160 }
161
162 // Accumulate a little on many consecutive days and ensure the score doesn't
163 // exceed the maximum allowed.
164 TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) {
165 base::Time current_day = GetReferenceTime();
166
167 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) {
168 current_day += base::TimeDelta::FromDays(1);
169 test_clock_.SetNow(current_day);
170
171 for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j)
172 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
173
174 EXPECT_EQ(
175 std::min(SiteEngagementScore::kMaxPoints,
176 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement *
177 SiteEngagementScore::GetNavigationPoints()),
178 score_.GetScore());
179 }
180
181 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore());
182 }
183
184 // Accumulate a bit, then check the score decays properly for a range of times.
185 TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) {
186 base::Time current_day = GetReferenceTime();
187
188 // First max the score.
189 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) {
190 current_day += base::TimeDelta::FromDays(1);
191 test_clock_.SetNow(current_day);
192
193 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
194 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
195 }
196
197 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore());
198
199 // The score should not have decayed before the first decay period has
200 // elapsed.
201 test_clock_.SetNow(current_day +
202 base::TimeDelta::FromDays(
203 SiteEngagementScore::GetDecayPeriodInDays() - 1));
204 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore());
205
206 // The score should have decayed by one chunk after one decay period has
207 // elapsed.
208 test_clock_.SetNow(
209 current_day +
210 base::TimeDelta::FromDays(SiteEngagementScore::GetDecayPeriodInDays()));
211 EXPECT_EQ(
212 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(),
213 score_.GetScore());
214
215 // The score should have decayed by the right number of chunks after a few
216 // decay periods have elapsed.
217 test_clock_.SetNow(
218 current_day +
219 base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore *
220 SiteEngagementScore::GetDecayPeriodInDays()));
221 EXPECT_EQ(SiteEngagementScore::kMaxPoints -
222 kLessPeriodsThanNeededToDecayMaxScore *
223 SiteEngagementScore::GetDecayPoints(),
224 score_.GetScore());
225
226 // The score should not decay below zero.
227 test_clock_.SetNow(
228 current_day +
229 base::TimeDelta::FromDays(kMorePeriodsThanNeededToDecayMaxScore *
230 SiteEngagementScore::GetDecayPeriodInDays()));
231 EXPECT_EQ(0, score_.GetScore());
232 }
233
234 // Test that any expected decays are applied before adding points.
235 TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) {
236 base::Time current_day = GetReferenceTime();
237
238 // Get the score up to something that can handle a bit of decay before
239 for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) {
240 current_day += base::TimeDelta::FromDays(1);
241 test_clock_.SetNow(current_day);
242
243 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
244 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
245 }
246
247 double initial_score = kLessDaysThanNeededToMaxTotalEngagement *
248 SiteEngagementScore::GetMaxPointsPerDay();
249 EXPECT_EQ(initial_score, score_.GetScore());
250
251 // Go forward a few decay periods.
252 test_clock_.SetNow(
253 current_day +
254 base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore *
255 SiteEngagementScore::GetDecayPeriodInDays()));
256
257 double decayed_score = initial_score -
258 kLessPeriodsThanNeededToDecayMaxScore *
259 SiteEngagementScore::GetDecayPoints();
260 EXPECT_EQ(decayed_score, score_.GetScore());
261
262 // Now add some points.
263 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
264 EXPECT_EQ(decayed_score + SiteEngagementScore::GetNavigationPoints(),
265 score_.GetScore());
266 }
267
268 // Test that going back in time is handled properly.
269 TEST_F(SiteEngagementScoreTest, GoBackInTime) {
270 base::Time current_day = GetReferenceTime();
271
272 test_clock_.SetNow(current_day);
273 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i)
274 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
275
276 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore());
277
278 // Adding to the score on an earlier date should be treated like another day,
279 // and should not cause any decay.
280 test_clock_.SetNow(current_day - base::TimeDelta::FromDays(
281 kMorePeriodsThanNeededToDecayMaxScore *
282 SiteEngagementScore::GetDecayPoints()));
283 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
284 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
285 double day_score =
286 std::min(SiteEngagementScore::GetMaxPointsPerDay(),
287 (i + 1) * SiteEngagementScore::GetNavigationPoints());
288 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(),
289 score_.GetScore());
290 }
291
292 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore());
293 }
294
295 // Test that scores are read / written correctly from / to empty score
296 // dictionaries.
297 TEST_F(SiteEngagementScoreTest, EmptyDictionary) {
298 base::DictionaryValue dict;
299 TestScoreInitializesAndUpdates(&dict, 0, 0, base::Time());
300 }
301
302 // Test that scores are read / written correctly from / to partially empty
303 // score dictionaries.
304 TEST_F(SiteEngagementScoreTest, PartiallyEmptyDictionary) {
305 base::DictionaryValue dict;
306 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2);
307
308 TestScoreInitializesAndUpdates(&dict, 0, 2, base::Time());
309 }
310
311 // Test that scores are read / written correctly from / to populated score
312 // dictionaries.
313 TEST_F(SiteEngagementScoreTest, PopulatedDictionary) {
314 base::DictionaryValue dict;
315 dict.SetDouble(SiteEngagementScore::kRawScoreKey, 1);
316 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2);
317 dict.SetDouble(SiteEngagementScore::kLastEngagementTimeKey,
318 GetReferenceTime().ToInternalValue());
319
320 TestScoreInitializesAndUpdates(&dict, 1, 2, GetReferenceTime());
321 }
322
323 // Ensure bonus engagement is awarded for the first engagement of a day.
324 TEST_F(SiteEngagementScoreTest, FirstDailyEngagementBonus) {
325 SetFirstDailyEngagementPointsForTesting(0.5);
326
327 SiteEngagementScore score1(&test_clock_);
328 SiteEngagementScore score2(&test_clock_);
329 base::Time current_day = GetReferenceTime();
330
331 test_clock_.SetNow(current_day);
332
333 // The first engagement event gets the bonus.
334 score1.AddPoints(0.5);
335 EXPECT_EQ(1.0, score1.GetScore());
336
337 // Subsequent events do not.
338 score1.AddPoints(0.5);
339 EXPECT_EQ(1.5, score1.GetScore());
340
341 // Bonuses are awarded independently between scores.
342 score2.AddPoints(1.0);
343 EXPECT_EQ(1.5, score2.GetScore());
344 score2.AddPoints(1.0);
345 EXPECT_EQ(2.5, score2.GetScore());
346
347 test_clock_.SetNow(current_day + base::TimeDelta::FromDays(1));
348
349 // The first event for the next day gets the bonus.
350 score1.AddPoints(0.5);
351 EXPECT_EQ(2.5, score1.GetScore());
352
353 // Subsequent events do not.
354 score1.AddPoints(0.5);
355 EXPECT_EQ(3.0, score1.GetScore());
356
357 score2.AddPoints(1.0);
358 EXPECT_EQ(4.0, score2.GetScore());
359 score2.AddPoints(1.0);
360 EXPECT_EQ(5.0, score2.GetScore());
361 }
362
363 // Test that resetting a score has the correct properties.
364 TEST_F(SiteEngagementScoreTest, Reset) {
365 base::Time current_day = GetReferenceTime();
366
367 test_clock_.SetNow(current_day);
368 score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
369 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetScore());
370
371 current_day += base::TimeDelta::FromDays(7);
372 test_clock_.SetNow(current_day);
373
374 score_.Reset(20.0, nullptr);
375 EXPECT_DOUBLE_EQ(20.0, score_.GetScore());
376 EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
377 EXPECT_EQ(current_day, score_.last_engagement_time_);
378 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());
379
380 // Adding points after the reset should work as normal.
381 score_.AddPoints(5);
382 EXPECT_EQ(25.0, score_.GetScore());
383
384 // The decay should happen one decay period from the current time.
385 test_clock_.SetNow(current_day +
386 base::TimeDelta::FromDays(
387 SiteEngagementScore::GetDecayPeriodInDays() + 1));
388 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.GetScore());
389
390 // Ensure that manually setting a time works as expected.
391 score_.AddPoints(5);
392 test_clock_.SetNow(GetReferenceTime());
393 base::Time now = test_clock_.Now();
394 score_.Reset(10.0, &now);
395
396 EXPECT_DOUBLE_EQ(10.0, score_.GetScore());
397 EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
398 EXPECT_EQ(now, score_.last_engagement_time_);
399 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());
400
401 score_.set_last_shortcut_launch_time(test_clock_.Now());
402 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3));
403 now = test_clock_.Now();
404 score_.Reset(15.0, &now);
405
406 // 5 bonus from the last shortcut launch.
407 EXPECT_DOUBLE_EQ(20.0, score_.GetScore());
408 EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
409 EXPECT_EQ(now, score_.last_engagement_time_);
410 EXPECT_EQ(now, score_.last_shortcut_launch_time_);
411 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_score.cc ('k') | chrome/browser/engagement/site_engagement_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698