File: site_engagement_score_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (470 lines) | stat: -rw-r--r-- 18,059 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/site_engagement/content/site_engagement_score.h"

#include <optional>
#include <utility>

#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/site_engagement/core/mojom/site_engagement_details.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace site_engagement {

namespace {

const int kLessAccumulationsThanNeededToMaxDailyEngagement = 2;
const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40;
const int kMoreAccumulationsThanNeededToMaxTotalEngagement = 200;
const int kLessDaysThanNeededToMaxTotalEngagement = 4;
const int kMoreDaysThanNeededToMaxTotalEngagement = 40;
const int kLessPeriodsThanNeededToDecayMaxScore = 2;
const int kMorePeriodsThanNeededToDecayMaxScore = 40;
const double kMaxRoundingDeviation = 0.0001;

base::Time GetReferenceTime() {
  static constexpr base::Time::Exploded kReferenceTime = {.year = 2015,
                                                          .month = 1,
                                                          .day_of_week = 5,
                                                          .day_of_month = 30,
                                                          .hour = 11};
  base::Time out_time;
  EXPECT_TRUE(base::Time::FromLocalExploded(kReferenceTime, &out_time));
  return out_time;
}

}  // namespace

class SiteEngagementScoreTest : public testing::Test {
 public:
  SiteEngagementScoreTest() : score_(&test_clock_, GURL(), nullptr) {}

  void SetUp() override {
    // Disable the first engagement bonus for tests.
    SiteEngagementScore::SetParamValuesForTesting();
  }

 protected:
  void VerifyScore(const SiteEngagementScore& score,
                   double expected_raw_score,
                   double expected_points_added_today,
                   base::Time expected_last_engagement_time) {
    EXPECT_EQ(expected_raw_score, score.raw_score_);
    EXPECT_EQ(expected_points_added_today, score.points_added_today_);
    EXPECT_EQ(expected_last_engagement_time, score.last_engagement_time_);
  }

  void UpdateScore(SiteEngagementScore* score,
                   double raw_score,
                   double points_added_today,
                   base::Time last_engagement_time) {
    score->raw_score_ = raw_score;
    score->points_added_today_ = points_added_today;
    score->last_engagement_time_ = last_engagement_time;
  }

  void TestScoreInitializesAndUpdates(
      base::Value::Dict score_dict,
      double expected_raw_score,
      double expected_points_added_today,
      base::Time expected_last_engagement_time) {
    base::Value::Dict copy(score_dict.Clone());

    SiteEngagementScore initial_score(&test_clock_, GURL(),
                                      std::move(score_dict));
    VerifyScore(initial_score, expected_raw_score, expected_points_added_today,
                expected_last_engagement_time);

    // Updating the score dict should return false, as the score shouldn't
    // have changed at this point.
    EXPECT_FALSE(initial_score.UpdateScoreDict(copy));

    // Update the score to new values and verify it updates the score dict
    // correctly.
    base::Time different_day = GetReferenceTime() + base::Days(1);
    UpdateScore(&initial_score, 5, 10, different_day);
    EXPECT_TRUE(initial_score.UpdateScoreDict(copy));
    SiteEngagementScore updated_score(&test_clock_, GURL(), std::move(copy));
    VerifyScore(updated_score, 5, 10, different_day);
  }

  void SetParamValue(SiteEngagementScore::Variation variation, double value) {
    SiteEngagementScore::GetParamValues()[variation].second = value;
  }

  base::SimpleTestClock test_clock_;
  SiteEngagementScore score_;
};

// Accumulate score many times on the same day. Ensure each time the score goes
// up, but not more than the maximum per day.
TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) {
  base::Time reference_time = GetReferenceTime();

  test_clock_.SetNow(reference_time);
  for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
    score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
    EXPECT_EQ(std::min(SiteEngagementScore::GetMaxPointsPerDay(),
                       (i + 1) * SiteEngagementScore::GetNavigationPoints()),
              score_.GetTotalScore());
  }

  EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotalScore());
}

// Accumulate on the first day to max that day's engagement, then accumulate on
// a different day.
TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) {
  base::Time reference_time = GetReferenceTime();
  base::Time later_date = reference_time + base::Days(2);

  test_clock_.SetNow(reference_time);
  for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i)
    score_.AddPoints(SiteEngagementScore::GetNavigationPoints());

  EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotalScore());

  test_clock_.SetNow(later_date);
  for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
    score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
    double day_score =
        std::min(SiteEngagementScore::GetMaxPointsPerDay(),
                 (i + 1) * SiteEngagementScore::GetNavigationPoints());
    EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(),
              score_.GetTotalScore());
  }

  EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(),
            score_.GetTotalScore());
}

// Accumulate score on many consecutive days and ensure the score doesn't exceed
// the maximum allowed.
TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) {
  base::Time current_day = GetReferenceTime();

  for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) {
    current_day += base::Days(1);
    test_clock_.SetNow(current_day);
    for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
      score_.AddPoints(SiteEngagementScore::GetNavigationPoints());

    EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints,
                       (i + 1) * SiteEngagementScore::GetMaxPointsPerDay()),
              score_.GetTotalScore());
  }

  EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore());
}

// Accumulate a little on many consecutive days and ensure the score doesn't
// exceed the maximum allowed.
TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) {
  base::Time current_day = GetReferenceTime();

  for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) {
    current_day += base::Days(1);
    test_clock_.SetNow(current_day);

    for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j)
      score_.AddPoints(SiteEngagementScore::GetNavigationPoints());

    EXPECT_EQ(
        std::min(SiteEngagementScore::kMaxPoints,
                 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement *
                     SiteEngagementScore::GetNavigationPoints()),
        score_.GetTotalScore());
  }

  EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore());
}

// Accumulate a bit, then check the score decays properly for a range of times.
TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) {
  base::Time current_day = GetReferenceTime();

  // First max the score.
  for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) {
    current_day += base::Days(1);
    test_clock_.SetNow(current_day);

    for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
      score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
  }

  EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore());

  // The score should not have decayed before the first decay period has
  // elapsed.
  test_clock_.SetNow(
      current_day +
      base::Hours(SiteEngagementScore::GetDecayPeriodInHours() - 1));
  EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotalScore());

  // The score should have decayed by one chunk after one decay period has
  // elapsed.
  test_clock_.SetNow(current_day +
                     base::Hours(SiteEngagementScore::GetDecayPeriodInHours()));
  EXPECT_EQ(
      SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(),
      score_.GetTotalScore());

  // The score should have decayed by the right number of chunks after a few
  // decay periods have elapsed.
  test_clock_.SetNow(current_day +
                     base::Hours(kLessPeriodsThanNeededToDecayMaxScore *
                                 SiteEngagementScore::GetDecayPeriodInHours()));
  EXPECT_EQ(SiteEngagementScore::kMaxPoints -
                kLessPeriodsThanNeededToDecayMaxScore *
                    SiteEngagementScore::GetDecayPoints(),
            score_.GetTotalScore());

  // The score should not decay below zero.
  test_clock_.SetNow(current_day +
                     base::Hours(kMorePeriodsThanNeededToDecayMaxScore *
                                 SiteEngagementScore::GetDecayPeriodInHours()));
  EXPECT_EQ(0, score_.GetTotalScore());
}

// Test that any expected decays are applied before adding points.
TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) {
  base::Time current_day = GetReferenceTime();

  // Get the score up to something that can handle a bit of decay before
  for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) {
    current_day += base::Days(1);
    test_clock_.SetNow(current_day);

    for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
      score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
  }

  double initial_score = kLessDaysThanNeededToMaxTotalEngagement *
                         SiteEngagementScore::GetMaxPointsPerDay();
  EXPECT_EQ(initial_score, score_.GetTotalScore());

  // Go forward a few decay periods.
  test_clock_.SetNow(current_day +
                     base::Hours(kLessPeriodsThanNeededToDecayMaxScore *
                                 SiteEngagementScore::GetDecayPeriodInHours()));

  double decayed_score =
      initial_score - kLessPeriodsThanNeededToDecayMaxScore *
                          SiteEngagementScore::GetDecayPoints();
  EXPECT_EQ(decayed_score, score_.GetTotalScore());

  // Now add some points.
  score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
  EXPECT_EQ(decayed_score + SiteEngagementScore::GetNavigationPoints(),
            score_.GetTotalScore());
}

// Test that going back in time is handled properly.
TEST_F(SiteEngagementScoreTest, GoBackInTime) {
  base::Time current_day = GetReferenceTime();

  test_clock_.SetNow(current_day);
  for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i)
    score_.AddPoints(SiteEngagementScore::GetNavigationPoints());

  EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotalScore());

  // Adding to the score on an earlier date should be treated like another day,
  // and should not cause any decay.
  test_clock_.SetNow(current_day -
                     base::Days(kMorePeriodsThanNeededToDecayMaxScore *
                                SiteEngagementScore::GetDecayPoints()));
  for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
    score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
    double day_score =
        std::min(SiteEngagementScore::GetMaxPointsPerDay(),
                 (i + 1) * SiteEngagementScore::GetNavigationPoints());
    EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(),
              score_.GetTotalScore());
  }

  EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(),
            score_.GetTotalScore());
}

// Test that scores are read / written correctly from / to empty score
// dictionaries.
TEST_F(SiteEngagementScoreTest, EmptyDictionary) {
  TestScoreInitializesAndUpdates(base::Value::Dict(), 0, 0, base::Time());
}

// Test that scores are read / written correctly from / to partially empty
// score dictionaries.
TEST_F(SiteEngagementScoreTest, PartiallyEmptyDictionary) {
  base::Value::Dict dict;
  dict.Set(SiteEngagementScore::kPointsAddedTodayKey, 2.);

  TestScoreInitializesAndUpdates(std::move(dict), 0, 2, base::Time());
}

// Test that scores are read / written correctly from / to populated score
// dictionaries.
TEST_F(SiteEngagementScoreTest, PopulatedDictionary) {
  base::Value::Dict dict;
  dict.Set(SiteEngagementScore::kRawScoreKey, 1.);
  dict.Set(SiteEngagementScore::kPointsAddedTodayKey, 2.);
  dict.Set(SiteEngagementScore::kLastEngagementTimeKey,
           static_cast<double>(GetReferenceTime().ToInternalValue()));

  TestScoreInitializesAndUpdates(std::move(dict), 1, 2, GetReferenceTime());
}

// Ensure bonus engagement is awarded for the first engagement of a day.
TEST_F(SiteEngagementScoreTest, FirstDailyEngagementBonus) {
  SetParamValue(SiteEngagementScore::FIRST_DAILY_ENGAGEMENT, 0.5);

  SiteEngagementScore score1(&test_clock_, GURL(),
                             /*score_dict=*/std::nullopt);
  SiteEngagementScore score2(&test_clock_, GURL(),
                             /*score_dict=*/std::nullopt);
  base::Time current_day = GetReferenceTime();

  test_clock_.SetNow(current_day);

  // The first engagement event gets the bonus.
  score1.AddPoints(0.5);
  EXPECT_EQ(1.0, score1.GetTotalScore());

  // Subsequent events do not.
  score1.AddPoints(0.5);
  EXPECT_EQ(1.5, score1.GetTotalScore());

  // Bonuses are awarded independently between scores.
  score2.AddPoints(1.0);
  EXPECT_EQ(1.5, score2.GetTotalScore());
  score2.AddPoints(1.0);
  EXPECT_EQ(2.5, score2.GetTotalScore());

  test_clock_.SetNow(current_day + base::Days(1));

  // The first event for the next day gets the bonus.
  score1.AddPoints(0.5);
  EXPECT_EQ(2.5, score1.GetTotalScore());

  // Subsequent events do not.
  score1.AddPoints(0.5);
  EXPECT_EQ(3.0, score1.GetTotalScore());

  score2.AddPoints(1.0);
  EXPECT_EQ(4.0, score2.GetTotalScore());
  score2.AddPoints(1.0);
  EXPECT_EQ(5.0, score2.GetTotalScore());
}

// Test that resetting a score has the correct properties.
TEST_F(SiteEngagementScoreTest, Reset) {
  base::Time current_day = GetReferenceTime();

  test_clock_.SetNow(current_day);
  score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
  EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetTotalScore());

  current_day += base::Days(7);
  test_clock_.SetNow(current_day);

  score_.Reset(20.0, current_day);
  EXPECT_DOUBLE_EQ(20.0, score_.GetTotalScore());
  EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
  EXPECT_EQ(current_day, score_.last_engagement_time_);
  EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());

  // Adding points after the reset should work as normal.
  score_.AddPoints(5);
  EXPECT_EQ(25.0, score_.GetTotalScore());

  // The decay should happen one decay period from the current time.
  test_clock_.SetNow(
      current_day +
      base::Hours(SiteEngagementScore::GetDecayPeriodInHours() + 1));
  EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(),
            score_.GetTotalScore());

  // Ensure that manually setting a time works as expected.
  score_.AddPoints(5);
  test_clock_.SetNow(GetReferenceTime());
  base::Time now = test_clock_.Now();
  score_.Reset(10.0, now);

  EXPECT_DOUBLE_EQ(10.0, score_.GetTotalScore());
  EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
  EXPECT_EQ(now, score_.last_engagement_time_);
  EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());

  base::Time old_now = test_clock_.Now();

  score_.set_last_shortcut_launch_time(test_clock_.Now());
  test_clock_.SetNow(GetReferenceTime() + base::Days(3));
  now = test_clock_.Now();
  score_.Reset(15.0, now);

  // 5 bonus from the last shortcut launch.
  EXPECT_DOUBLE_EQ(20.0, score_.GetTotalScore());
  EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
  EXPECT_EQ(now, score_.last_engagement_time_);
  EXPECT_EQ(old_now, score_.last_shortcut_launch_time_);
}

// Test proportional decay.
TEST_F(SiteEngagementScoreTest, ProportionalDecay) {
  SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5);
  SetParamValue(SiteEngagementScore::DECAY_POINTS, 0);
  SetParamValue(SiteEngagementScore::MAX_POINTS_PER_DAY, 20);
  base::Time current_day = GetReferenceTime();
  test_clock_.SetNow(current_day);

  // Single decay period, expect the score to be halved once.
  score_.AddPoints(2.0);
  current_day += base::Days(7);
  test_clock_.SetNow(current_day);
  EXPECT_DOUBLE_EQ(1.0, score_.GetTotalScore());

  // 3 decay periods, expect the score to be halved 3 times.
  score_.AddPoints(15.0);
  current_day += base::Days(21);
  test_clock_.SetNow(current_day);
  EXPECT_DOUBLE_EQ(2.0, score_.GetTotalScore());

  // Ensure point removal happens after proportional decay.
  score_.AddPoints(4.0);
  EXPECT_DOUBLE_EQ(6.0, score_.GetTotalScore());
  SetParamValue(SiteEngagementScore::DECAY_POINTS, 2.0);
  current_day += base::Days(7);
  test_clock_.SetNow(current_day);
  EXPECT_NEAR(1.0, score_.GetTotalScore(), kMaxRoundingDeviation);
}

// Verify that GetDetails fills out all fields correctly.
TEST_F(SiteEngagementScoreTest, GetDetails) {
  // Advance the clock, otherwise Now() is the same as the null Time value.
  test_clock_.Advance(base::Days(365));

  GURL url("http://www.google.com/");

  // Replace |score_| with one with an actual URL.
  score_ = SiteEngagementScore(&test_clock_, url, nullptr);

  // Initially all component scores should be zero.
  mojom::SiteEngagementDetails details = score_.GetDetails();
  EXPECT_DOUBLE_EQ(0.0, details.total_score);
  EXPECT_DOUBLE_EQ(0.0, details.installed_bonus);
  EXPECT_DOUBLE_EQ(0.0, details.base_score);
  EXPECT_EQ(url, details.origin);

  // Simulate the app having been launched.
  score_.set_last_shortcut_launch_time(test_clock_.Now());
  details = score_.GetDetails();
  EXPECT_DOUBLE_EQ(details.installed_bonus, details.total_score);
  EXPECT_LT(0.0, details.installed_bonus);
  EXPECT_DOUBLE_EQ(0.0, details.base_score);
}

}  // namespace site_engagement