| Index: chrome/browser/budget_service/background_budget_service_unittest.cc
|
| diff --git a/chrome/browser/budget_service/background_budget_service_unittest.cc b/chrome/browser/budget_service/background_budget_service_unittest.cc
|
| index 98ee3dffd1c31561276982627bb305b26d3d05aa..605052707b6b4018eaab43f67b46bc93b3f947f4 100644
|
| --- a/chrome/browser/budget_service/background_budget_service_unittest.cc
|
| +++ b/chrome/browser/budget_service/background_budget_service_unittest.cc
|
| @@ -6,6 +6,7 @@
|
| #include <string>
|
|
|
| #include "base/memory/ptr_util.h"
|
| +#include "base/run_loop.h"
|
| #include "base/test/simple_test_clock.h"
|
| #include "chrome/browser/budget_service/background_budget_service.h"
|
| #include "chrome/browser/budget_service/background_budget_service_factory.h"
|
| @@ -31,7 +32,7 @@ const double kSecondsToAccumulate = 864000.0;
|
|
|
| class BackgroundBudgetServiceTest : public testing::Test {
|
| public:
|
| - BackgroundBudgetServiceTest() {}
|
| + BackgroundBudgetServiceTest() : budget_(0.0) {}
|
| ~BackgroundBudgetServiceTest() override {}
|
|
|
| BackgroundBudgetService* GetService() {
|
| @@ -52,74 +53,83 @@ class BackgroundBudgetServiceTest : public testing::Test {
|
| return clock;
|
| }
|
|
|
| + double GetBudget() {
|
| + const GURL origin(kTestOrigin);
|
| + base::RunLoop run_loop;
|
| + GetService()->GetBudget(
|
| + origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget,
|
| + base::Unretained(this), run_loop.QuitClosure()));
|
| + run_loop.Run();
|
| + return budget_;
|
| + }
|
| +
|
| + void GotBudget(base::Closure run_loop_closure, double budget) {
|
| + budget_ = budget;
|
| + run_loop_closure.Run();
|
| + }
|
| +
|
| + void StoreBudget(double budget) {
|
| + const GURL origin(kTestOrigin);
|
| + base::RunLoop run_loop;
|
| + GetService()->StoreBudget(origin, budget, run_loop.QuitClosure());
|
| + run_loop.Run();
|
| + }
|
| +
|
| + // Budget for callbacks to set.
|
| + double budget_;
|
| +
|
| private:
|
| content::TestBrowserThreadBundle thread_bundle_;
|
| TestingProfile profile_;
|
| };
|
|
|
| TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) {
|
| - const GURL origin(kTestOrigin);
|
| -
|
| - double budget = GetService()->GetBudget(origin);
|
| -
|
| - EXPECT_DOUBLE_EQ(budget, 0.0);
|
| + EXPECT_DOUBLE_EQ(GetBudget(), 0.0);
|
| }
|
|
|
| TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) {
|
| - const GURL origin(kTestOrigin);
|
| -
|
| // Set a starting SES for the url but no stored budget info.
|
| + const GURL origin(kTestOrigin);
|
| SetSiteEngagementScore(origin, kTestSES);
|
|
|
| - double budget = GetService()->GetBudget(origin);
|
| -
|
| - EXPECT_DOUBLE_EQ(budget, kTestSES);
|
| + EXPECT_DOUBLE_EQ(GetBudget(), kTestSES);
|
| }
|
|
|
| TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) {
|
| - const GURL origin(kTestOrigin);
|
| -
|
| - std::unique_ptr<BackgroundBudgetService> service(
|
| - new BackgroundBudgetService(profile()));
|
| -
|
| - service->StoreBudget(origin, kTestBudget);
|
| -
|
| - double budget = service->GetBudget(origin);
|
| -
|
| - EXPECT_NEAR(budget, kTestBudget, kTestBudget);
|
| + StoreBudget(kTestBudget);
|
| + EXPECT_DOUBLE_EQ(GetBudget(), kTestBudget);
|
| }
|
|
|
| TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) {
|
| // Manually construct a BackgroundBudgetServie with a clock that the test
|
| // can control so that we can fast forward in time.
|
| - BackgroundBudgetService* service = GetService();
|
| base::SimpleTestClock* clock = SetClockForTesting();
|
| base::Time starting_time = clock->Now();
|
|
|
| // Set initial SES and budget values.
|
| const GURL origin(kTestOrigin);
|
| SetSiteEngagementScore(origin, kTestSES);
|
| - service->StoreBudget(origin, kTestBudget);
|
| + StoreBudget(kTestBudget);
|
|
|
| - double budget = service->GetBudget(origin);
|
| + double budget = GetBudget();
|
| EXPECT_DOUBLE_EQ(budget, kTestBudget);
|
|
|
| // Query for the budget after 1 second has passed.
|
| clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1));
|
| - budget = service->GetBudget(origin);
|
| - EXPECT_NEAR(budget, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate);
|
| + budget = GetBudget();
|
| + EXPECT_LT(budget, kTestBudget + kTestSES * 1.0 / kSecondsToAccumulate);
|
| EXPECT_GT(budget, kTestBudget);
|
|
|
| // Query for the budget after 1 hour has passed.
|
| clock->SetNow(starting_time + base::TimeDelta::FromHours(1));
|
| - budget = service->GetBudget(origin);
|
| - EXPECT_NEAR(budget, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate);
|
| + budget = GetBudget();
|
| + EXPECT_LT(budget, kTestBudget + kTestSES * 3600.0 / kSecondsToAccumulate);
|
| EXPECT_GT(budget, kTestBudget);
|
|
|
| // Query for the budget after 5 days have passed. The budget should be
|
| // increasing, but not up the SES score.
|
| clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
|
| - budget = service->GetBudget(origin);
|
| + budget = GetBudget();
|
| EXPECT_GT(budget, kTestBudget);
|
| EXPECT_LT(budget, kTestSES);
|
| double moderate_ses_budget = budget;
|
| @@ -127,23 +137,23 @@ TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) {
|
| // Query for the budget after 10 days have passed. By this point, the budget
|
| // should converge to the SES score.
|
| clock->SetNow(starting_time + base::TimeDelta::FromDays(10));
|
| - budget = service->GetBudget(origin);
|
| + budget = GetBudget();
|
| EXPECT_DOUBLE_EQ(budget, kTestSES);
|
|
|
| // Now, change the SES score to the maximum amount and reinitialize budget.
|
| SetSiteEngagementScore(origin, kMaxSES);
|
| - service->StoreBudget(origin, kTestBudget);
|
| + StoreBudget(kTestBudget);
|
| starting_time = clock->Now();
|
|
|
| // Query for the budget after 1 second has passed.
|
| clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1));
|
| - budget = service->GetBudget(origin);
|
| - EXPECT_NEAR(budget, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate);
|
| + budget = GetBudget();
|
| + EXPECT_LT(budget, kTestBudget + kMaxSES * 1.0 / kSecondsToAccumulate);
|
|
|
| // Query for the budget after 5 days have passed. Again, the budget should be
|
| // approaching the SES, but not have reached it.
|
| clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
|
| - budget = service->GetBudget(origin);
|
| + budget = GetBudget();
|
| EXPECT_GT(budget, kTestBudget);
|
| EXPECT_LT(budget, kMaxSES);
|
|
|
| @@ -153,13 +163,13 @@ TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) {
|
|
|
| // Now, change the SES score to a low amount and reinitialize budget.
|
| SetSiteEngagementScore(origin, kLowSES);
|
| - service->StoreBudget(origin, kTestBudget);
|
| + StoreBudget(kTestBudget);
|
| starting_time = clock->Now();
|
|
|
| // Query for the budget after 5 days have passed. Again, the budget should be
|
| // approaching the SES, this time decreasing, but not have reached it.
|
| clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
|
| - budget = service->GetBudget(origin);
|
| + budget = GetBudget();
|
| EXPECT_LT(budget, kTestBudget);
|
| EXPECT_GT(budget, kLowSES);
|
| }
|
| @@ -167,13 +177,12 @@ TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) {
|
| TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) {
|
| // Manually construct a BackgroundBudgetService with a clock that the test
|
| // can control so that we can fast forward in time.
|
| - BackgroundBudgetService* service = GetService();
|
| base::SimpleTestClock* clock = SetClockForTesting();
|
|
|
| // Set initial SES and budget values.
|
| const GURL origin(kTestOrigin);
|
| SetSiteEngagementScore(origin, kTestSES);
|
| - service->StoreBudget(origin, kTestBudget);
|
| + StoreBudget(kTestBudget);
|
| double budget = 0.0;
|
|
|
| // Measure over 200 hours. In each hour a message is received, and for 1 in
|
| @@ -181,12 +190,12 @@ TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) {
|
| for (int i = 0; i < 200; i++) {
|
| // Query for the budget after 1 hour has passed.
|
| clock->Advance(base::TimeDelta::FromHours(1));
|
| - budget = service->GetBudget(origin);
|
| + budget = GetBudget();
|
|
|
| if (i % 10 == 0) {
|
| double cost = BackgroundBudgetService::GetCost(
|
| BackgroundBudgetService::CostType::SILENT_PUSH);
|
| - service->StoreBudget(origin, budget - cost);
|
| + StoreBudget(budget - cost);
|
| }
|
| }
|
|
|
| @@ -210,32 +219,30 @@ TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) {
|
| update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0");
|
|
|
| // Get the budget, expect that it will return SES.
|
| - double budget = GetService()->GetBudget(origin);
|
| -
|
| - EXPECT_DOUBLE_EQ(budget, kTestSES);
|
| + EXPECT_DOUBLE_EQ(GetBudget(), kTestSES);
|
| }
|
|
|
| TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) {
|
| // Manually construct a BackgroundBudgetService with a clock that the test
|
| // can control so that we can fast forward in time.
|
| - BackgroundBudgetService* service = GetService();
|
| base::SimpleTestClock* clock = SetClockForTesting();
|
| base::Time starting_time = clock->Now();
|
|
|
| // Set initial SES and budget values.
|
| const GURL origin(kTestOrigin);
|
| SetSiteEngagementScore(origin, kTestSES);
|
| - service->StoreBudget(origin, kTestBudget);
|
| + StoreBudget(kTestBudget);
|
|
|
| // Move time forward an hour and get the budget.
|
| clock->SetNow(starting_time + base::TimeDelta::FromHours(1));
|
| - double budget = service->GetBudget(origin);
|
| - service->StoreBudget(origin, budget);
|
| - EXPECT_NE(kTestBudget, budget);
|
| + double original_budget = GetBudget();
|
| +
|
| + // Store the updated budget.
|
| + StoreBudget(original_budget);
|
| + EXPECT_NE(kTestBudget, original_budget);
|
|
|
| // Now move time backwards a day and make sure that the current
|
| // budget matches the budget of the most foward time.
|
| clock->SetNow(starting_time - base::TimeDelta::FromDays(1));
|
| - double back_budget = service->GetBudget(origin);
|
| - EXPECT_NEAR(budget, back_budget, 0.01);
|
| + EXPECT_NEAR(original_budget, GetBudget(), 0.01);
|
| }
|
|
|