| 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" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 exploded_reference_time.second = 0; | 33 exploded_reference_time.second = 0; |
| 34 exploded_reference_time.millisecond = 0; | 34 exploded_reference_time.millisecond = 0; |
| 35 | 35 |
| 36 return base::Time::FromLocalExploded(exploded_reference_time); | 36 return base::Time::FromLocalExploded(exploded_reference_time); |
| 37 } | 37 } |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 class SiteEngagementScoreTest : public testing::Test { | 41 class SiteEngagementScoreTest : public testing::Test { |
| 42 public: | 42 public: |
| 43 SiteEngagementScoreTest() : score_(&test_clock_) {} | 43 SiteEngagementScoreTest() : score_(&test_clock_, nullptr) {} |
| 44 | 44 |
| 45 void SetUp() override { | 45 void SetUp() override { |
| 46 testing::Test::SetUp(); | 46 testing::Test::SetUp(); |
| 47 // Disable the first engagement bonus for tests. | 47 // Disable the first engagement bonus for tests. |
| 48 SiteEngagementScore::SetParamValuesForTesting(); | 48 SiteEngagementScore::SetParamValuesForTesting(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 protected: | 51 protected: |
| 52 void VerifyScore(const SiteEngagementScore& score, | 52 void VerifyScore(const SiteEngagementScore& score, |
| 53 double expected_raw_score, | 53 double expected_raw_score, |
| 54 double expected_points_added_today, | 54 double expected_points_added_today, |
| 55 base::Time expected_last_engagement_time) { | 55 base::Time expected_last_engagement_time) { |
| 56 EXPECT_EQ(expected_raw_score, score.raw_score_); | 56 EXPECT_EQ(expected_raw_score, score.raw_score_); |
| 57 EXPECT_EQ(expected_points_added_today, score.points_added_today_); | 57 EXPECT_EQ(expected_points_added_today, score.points_added_today_); |
| 58 EXPECT_EQ(expected_last_engagement_time, score.last_engagement_time_); | 58 EXPECT_EQ(expected_last_engagement_time, score.last_engagement_time_); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void UpdateScore(SiteEngagementScore* score, | 61 void UpdateScore(SiteEngagementScore* score, |
| 62 double raw_score, | 62 double raw_score, |
| 63 double points_added_today, | 63 double points_added_today, |
| 64 base::Time last_engagement_time) { | 64 base::Time last_engagement_time) { |
| 65 score->raw_score_ = raw_score; | 65 score->raw_score_ = raw_score; |
| 66 score->points_added_today_ = points_added_today; | 66 score->points_added_today_ = points_added_today; |
| 67 score->last_engagement_time_ = last_engagement_time; | 67 score->last_engagement_time_ = last_engagement_time; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void TestScoreInitializesAndUpdates( | 70 void TestScoreInitializesAndUpdates( |
| 71 base::DictionaryValue* score_dict, | 71 std::unique_ptr<base::DictionaryValue> score_dict, |
| 72 double expected_raw_score, | 72 double expected_raw_score, |
| 73 double expected_points_added_today, | 73 double expected_points_added_today, |
| 74 base::Time expected_last_engagement_time) { | 74 base::Time expected_last_engagement_time) { |
| 75 SiteEngagementScore initial_score(&test_clock_, *score_dict); | 75 std::unique_ptr<base::DictionaryValue> copy(score_dict->DeepCopy()); |
| 76 SiteEngagementScore initial_score(&test_clock_, std::move(score_dict)); |
| 76 VerifyScore(initial_score, expected_raw_score, expected_points_added_today, | 77 VerifyScore(initial_score, expected_raw_score, expected_points_added_today, |
| 77 expected_last_engagement_time); | 78 expected_last_engagement_time); |
| 78 | 79 |
| 79 // Updating the score dict should return false, as the score shouldn't | 80 // Updating the score dict should return false, as the score shouldn't |
| 80 // have changed at this point. | 81 // have changed at this point. |
| 81 EXPECT_FALSE(initial_score.UpdateScoreDict(score_dict)); | 82 EXPECT_FALSE(initial_score.UpdateScoreDict(copy.get())); |
| 82 | 83 |
| 83 // Update the score to new values and verify it updates the score dict | 84 // Update the score to new values and verify it updates the score dict |
| 84 // correctly. | 85 // correctly. |
| 85 base::Time different_day = | 86 base::Time different_day = |
| 86 GetReferenceTime() + base::TimeDelta::FromDays(1); | 87 GetReferenceTime() + base::TimeDelta::FromDays(1); |
| 87 UpdateScore(&initial_score, 5, 10, different_day); | 88 UpdateScore(&initial_score, 5, 10, different_day); |
| 88 EXPECT_TRUE(initial_score.UpdateScoreDict(score_dict)); | 89 EXPECT_TRUE(initial_score.UpdateScoreDict(copy.get())); |
| 89 SiteEngagementScore updated_score(&test_clock_, *score_dict); | 90 SiteEngagementScore updated_score(&test_clock_, std::move(copy)); |
| 90 VerifyScore(updated_score, 5, 10, different_day); | 91 VerifyScore(updated_score, 5, 10, different_day); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void SetFirstDailyEngagementPointsForTesting(double points) { | 94 void SetFirstDailyEngagementPointsForTesting(double points) { |
| 94 SiteEngagementScore::param_values | 95 SiteEngagementScore::param_values |
| 95 [SiteEngagementScore::FIRST_DAILY_ENGAGEMENT] = points; | 96 [SiteEngagementScore::FIRST_DAILY_ENGAGEMENT] = points; |
| 96 } | 97 } |
| 97 | 98 |
| 98 base::SimpleTestClock test_clock_; | 99 base::SimpleTestClock test_clock_; |
| 99 SiteEngagementScore score_; | 100 SiteEngagementScore score_; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), | 289 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), |
| 289 score_.GetScore()); | 290 score_.GetScore()); |
| 290 } | 291 } |
| 291 | 292 |
| 292 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 293 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); |
| 293 } | 294 } |
| 294 | 295 |
| 295 // Test that scores are read / written correctly from / to empty score | 296 // Test that scores are read / written correctly from / to empty score |
| 296 // dictionaries. | 297 // dictionaries. |
| 297 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { | 298 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { |
| 298 base::DictionaryValue dict; | 299 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 299 TestScoreInitializesAndUpdates(&dict, 0, 0, base::Time()); | 300 TestScoreInitializesAndUpdates(std::move(dict), 0, 0, base::Time()); |
| 300 } | 301 } |
| 301 | 302 |
| 302 // Test that scores are read / written correctly from / to partially empty | 303 // Test that scores are read / written correctly from / to partially empty |
| 303 // score dictionaries. | 304 // score dictionaries. |
| 304 TEST_F(SiteEngagementScoreTest, PartiallyEmptyDictionary) { | 305 TEST_F(SiteEngagementScoreTest, PartiallyEmptyDictionary) { |
| 305 base::DictionaryValue dict; | 306 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 306 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); | 307 dict->SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); |
| 307 | 308 |
| 308 TestScoreInitializesAndUpdates(&dict, 0, 2, base::Time()); | 309 TestScoreInitializesAndUpdates(std::move(dict), 0, 2, base::Time()); |
| 309 } | 310 } |
| 310 | 311 |
| 311 // Test that scores are read / written correctly from / to populated score | 312 // Test that scores are read / written correctly from / to populated score |
| 312 // dictionaries. | 313 // dictionaries. |
| 313 TEST_F(SiteEngagementScoreTest, PopulatedDictionary) { | 314 TEST_F(SiteEngagementScoreTest, PopulatedDictionary) { |
| 314 base::DictionaryValue dict; | 315 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 315 dict.SetDouble(SiteEngagementScore::kRawScoreKey, 1); | 316 dict->SetDouble(SiteEngagementScore::kRawScoreKey, 1); |
| 316 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); | 317 dict->SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); |
| 317 dict.SetDouble(SiteEngagementScore::kLastEngagementTimeKey, | 318 dict->SetDouble(SiteEngagementScore::kLastEngagementTimeKey, |
| 318 GetReferenceTime().ToInternalValue()); | 319 GetReferenceTime().ToInternalValue()); |
| 319 | 320 |
| 320 TestScoreInitializesAndUpdates(&dict, 1, 2, GetReferenceTime()); | 321 TestScoreInitializesAndUpdates(std::move(dict), 1, 2, GetReferenceTime()); |
| 321 } | 322 } |
| 322 | 323 |
| 323 // Ensure bonus engagement is awarded for the first engagement of a day. | 324 // Ensure bonus engagement is awarded for the first engagement of a day. |
| 324 TEST_F(SiteEngagementScoreTest, FirstDailyEngagementBonus) { | 325 TEST_F(SiteEngagementScoreTest, FirstDailyEngagementBonus) { |
| 325 SetFirstDailyEngagementPointsForTesting(0.5); | 326 SetFirstDailyEngagementPointsForTesting(0.5); |
| 326 | 327 |
| 327 SiteEngagementScore score1(&test_clock_); | 328 SiteEngagementScore score1(&test_clock_, |
| 328 SiteEngagementScore score2(&test_clock_); | 329 std::unique_ptr<base::DictionaryValue>()); |
| 330 SiteEngagementScore score2(&test_clock_, |
| 331 std::unique_ptr<base::DictionaryValue>()); |
| 329 base::Time current_day = GetReferenceTime(); | 332 base::Time current_day = GetReferenceTime(); |
| 330 | 333 |
| 331 test_clock_.SetNow(current_day); | 334 test_clock_.SetNow(current_day); |
| 332 | 335 |
| 333 // The first engagement event gets the bonus. | 336 // The first engagement event gets the bonus. |
| 334 score1.AddPoints(0.5); | 337 score1.AddPoints(0.5); |
| 335 EXPECT_EQ(1.0, score1.GetScore()); | 338 EXPECT_EQ(1.0, score1.GetScore()); |
| 336 | 339 |
| 337 // Subsequent events do not. | 340 // Subsequent events do not. |
| 338 score1.AddPoints(0.5); | 341 score1.AddPoints(0.5); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 364 TEST_F(SiteEngagementScoreTest, Reset) { | 367 TEST_F(SiteEngagementScoreTest, Reset) { |
| 365 base::Time current_day = GetReferenceTime(); | 368 base::Time current_day = GetReferenceTime(); |
| 366 | 369 |
| 367 test_clock_.SetNow(current_day); | 370 test_clock_.SetNow(current_day); |
| 368 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 371 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 369 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetScore()); | 372 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetScore()); |
| 370 | 373 |
| 371 current_day += base::TimeDelta::FromDays(7); | 374 current_day += base::TimeDelta::FromDays(7); |
| 372 test_clock_.SetNow(current_day); | 375 test_clock_.SetNow(current_day); |
| 373 | 376 |
| 374 score_.Reset(20.0, nullptr); | 377 score_.Reset(20.0, current_day); |
| 375 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); | 378 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); |
| 376 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 379 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 377 EXPECT_EQ(current_day, score_.last_engagement_time_); | 380 EXPECT_EQ(current_day, score_.last_engagement_time_); |
| 378 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); | 381 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
| 379 | 382 |
| 380 // Adding points after the reset should work as normal. | 383 // Adding points after the reset should work as normal. |
| 381 score_.AddPoints(5); | 384 score_.AddPoints(5); |
| 382 EXPECT_EQ(25.0, score_.GetScore()); | 385 EXPECT_EQ(25.0, score_.GetScore()); |
| 383 | 386 |
| 384 // The decay should happen one decay period from the current time. | 387 // The decay should happen one decay period from the current time. |
| 385 test_clock_.SetNow(current_day + | 388 test_clock_.SetNow(current_day + |
| 386 base::TimeDelta::FromDays( | 389 base::TimeDelta::FromDays( |
| 387 SiteEngagementScore::GetDecayPeriodInDays() + 1)); | 390 SiteEngagementScore::GetDecayPeriodInDays() + 1)); |
| 388 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.GetScore()); | 391 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.GetScore()); |
| 389 | 392 |
| 390 // Ensure that manually setting a time works as expected. | 393 // Ensure that manually setting a time works as expected. |
| 391 score_.AddPoints(5); | 394 score_.AddPoints(5); |
| 392 test_clock_.SetNow(GetReferenceTime()); | 395 test_clock_.SetNow(GetReferenceTime()); |
| 393 base::Time now = test_clock_.Now(); | 396 base::Time now = test_clock_.Now(); |
| 394 score_.Reset(10.0, &now); | 397 score_.Reset(10.0, now); |
| 395 | 398 |
| 396 EXPECT_DOUBLE_EQ(10.0, score_.GetScore()); | 399 EXPECT_DOUBLE_EQ(10.0, score_.GetScore()); |
| 397 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 400 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 398 EXPECT_EQ(now, score_.last_engagement_time_); | 401 EXPECT_EQ(now, score_.last_engagement_time_); |
| 399 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); | 402 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
| 400 | 403 |
| 404 base::Time old_now = test_clock_.Now(); |
| 405 |
| 401 score_.set_last_shortcut_launch_time(test_clock_.Now()); | 406 score_.set_last_shortcut_launch_time(test_clock_.Now()); |
| 402 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3)); | 407 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3)); |
| 403 now = test_clock_.Now(); | 408 now = test_clock_.Now(); |
| 404 score_.Reset(15.0, &now); | 409 score_.Reset(15.0, now); |
| 405 | 410 |
| 406 // 5 bonus from the last shortcut launch. | 411 // 5 bonus from the last shortcut launch. |
| 407 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); | 412 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); |
| 408 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 413 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 409 EXPECT_EQ(now, score_.last_engagement_time_); | 414 EXPECT_EQ(now, score_.last_engagement_time_); |
| 410 EXPECT_EQ(now, score_.last_shortcut_launch_time_); | 415 EXPECT_EQ(old_now, score_.last_shortcut_launch_time_); |
| 411 } | 416 } |
| OLD | NEW |