| 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 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 TEST_F(SiteEngagementScoreTest, Reset) { | 424 TEST_F(SiteEngagementScoreTest, Reset) { |
| 425 base::Time current_day = GetReferenceTime(); | 425 base::Time current_day = GetReferenceTime(); |
| 426 | 426 |
| 427 test_clock_.SetNow(current_day); | 427 test_clock_.SetNow(current_day); |
| 428 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 428 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 429 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.Score()); | 429 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.Score()); |
| 430 | 430 |
| 431 current_day += base::TimeDelta::FromDays(7); | 431 current_day += base::TimeDelta::FromDays(7); |
| 432 test_clock_.SetNow(current_day); | 432 test_clock_.SetNow(current_day); |
| 433 | 433 |
| 434 score_.Reset(20.0); | 434 score_.Reset(20.0, nullptr); |
| 435 EXPECT_DOUBLE_EQ(20.0, score_.Score()); | 435 EXPECT_DOUBLE_EQ(20.0, score_.Score()); |
| 436 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 436 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 437 EXPECT_EQ(current_day, score_.last_engagement_time_); | 437 EXPECT_EQ(current_day, score_.last_engagement_time_); |
| 438 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
| 438 | 439 |
| 439 // Adding points after the reset should work as normal. | 440 // Adding points after the reset should work as normal. |
| 440 score_.AddPoints(5); | 441 score_.AddPoints(5); |
| 441 EXPECT_EQ(25.0, score_.Score()); | 442 EXPECT_EQ(25.0, score_.Score()); |
| 442 | 443 |
| 443 // The decay should happen one decay period from | 444 // The decay should happen one decay period from the current time. |
| 444 test_clock_.SetNow(current_day + | 445 test_clock_.SetNow(current_day + |
| 445 base::TimeDelta::FromDays( | 446 base::TimeDelta::FromDays( |
| 446 SiteEngagementScore::GetDecayPeriodInDays() + 1)); | 447 SiteEngagementScore::GetDecayPeriodInDays() + 1)); |
| 447 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.Score()); | 448 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.Score()); |
| 449 |
| 450 // Ensure that manually setting a time works as expected. |
| 451 score_.AddPoints(5); |
| 452 test_clock_.SetNow(GetReferenceTime()); |
| 453 base::Time now = test_clock_.Now(); |
| 454 score_.Reset(10.0, &now); |
| 455 |
| 456 EXPECT_DOUBLE_EQ(10.0, score_.Score()); |
| 457 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 458 EXPECT_EQ(now, score_.last_engagement_time_); |
| 459 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
| 460 |
| 461 score_.set_last_shortcut_launch_time(test_clock_.Now()); |
| 462 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3)); |
| 463 now = test_clock_.Now(); |
| 464 score_.Reset(15.0, &now); |
| 465 |
| 466 // 5 bonus from the last shortcut launch. |
| 467 EXPECT_DOUBLE_EQ(20.0, score_.Score()); |
| 468 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 469 EXPECT_EQ(now, score_.last_engagement_time_); |
| 470 EXPECT_EQ(now, score_.last_shortcut_launch_time_); |
| 448 } | 471 } |
| 449 | 472 |
| 450 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { | 473 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { |
| 451 public: | 474 public: |
| 452 void SetUp() override { | 475 void SetUp() override { |
| 453 ChromeRenderViewHostTestHarness::SetUp(); | 476 ChromeRenderViewHostTestHarness::SetUp(); |
| 454 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 477 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 455 g_temp_history_dir = temp_dir_.path(); | 478 g_temp_history_dir = temp_dir_.path(); |
| 456 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 479 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
| 457 profile(), &BuildTestHistoryService); | 480 profile(), &BuildTestHistoryService); |
| (...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1372 0, 2); | 1395 0, 2); |
| 1373 | 1396 |
| 1374 // Add more points and ensure no more samples are present. | 1397 // Add more points and ensure no more samples are present. |
| 1375 service->AddPoints(origin1, 0.01); | 1398 service->AddPoints(origin1, 0.01); |
| 1376 service->AddPoints(origin2, 0.01); | 1399 service->AddPoints(origin2, 0.01); |
| 1377 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, | 1400 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, |
| 1378 4); | 1401 4); |
| 1379 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, | 1402 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, |
| 1380 4); | 1403 4); |
| 1381 } | 1404 } |
| OLD | NEW |