| Index: chrome/browser/budget_service/budget_database_unittest.cc
|
| diff --git a/chrome/browser/budget_service/budget_database_unittest.cc b/chrome/browser/budget_service/budget_database_unittest.cc
|
| index f2b67e057f8dd283af87951dfca868706d89f958..7c7d9db4b1265ab9a742abd6fb5163b205384b49 100644
|
| --- a/chrome/browser/budget_service/budget_database_unittest.cc
|
| +++ b/chrome/browser/budget_service/budget_database_unittest.cc
|
| @@ -14,11 +14,12 @@
|
| #include "components/leveldb_proto/proto_database_impl.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #include "content/public/test/test_browser_thread_bundle.h"
|
| +#include "mojo/public/cpp/bindings/array.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace {
|
|
|
| -const double kDefaultExpirationInHours = 72;
|
| +const double kDefaultExpirationInHours = 240;
|
| const double kDefaultEngagement = 30.0;
|
|
|
| const char kTestOrigin[] = "https://example.com";
|
| @@ -50,11 +51,10 @@ class BudgetDatabaseTest : public ::testing::Test {
|
|
|
| void GetBudgetDetailsComplete(
|
| base::Closure run_loop_closure,
|
| - bool success,
|
| - const BudgetDatabase::BudgetPrediction& prediction) {
|
| - success_ = success;
|
| - // Convert BudgetPrediction to a vector for random access to check values.
|
| - prediction_.assign(prediction.begin(), prediction.end());
|
| + mojo::Array<blink::mojom::BudgetStatePtr> predictions) {
|
| + // TODO(harkness) Check the status enum when it's added.
|
| + success_ = true;
|
| + prediction_.Swap(&predictions);
|
| run_loop_closure.Run();
|
| }
|
|
|
| @@ -84,7 +84,7 @@ class BudgetDatabaseTest : public ::testing::Test {
|
|
|
| protected:
|
| bool success_;
|
| - std::vector<BudgetDatabase::BudgetStatus> prediction_;
|
| + mojo::Array<blink::mojom::BudgetStatePtr> prediction_;
|
|
|
| private:
|
| content::TestBrowserThreadBundle thread_bundle_;
|
| @@ -93,6 +93,14 @@ class BudgetDatabaseTest : public ::testing::Test {
|
| BudgetDatabase db_;
|
| };
|
|
|
| +TEST_F(BudgetDatabaseTest, GetBudgetNoBudgetOrSES) {
|
| + const GURL origin(kTestOrigin);
|
| + GetBudgetDetails();
|
| + ASSERT_TRUE(success_);
|
| + ASSERT_EQ(2U, prediction_.size());
|
| + EXPECT_EQ(0, prediction_[0]->budget_at);
|
| +}
|
| +
|
| TEST_F(BudgetDatabaseTest, AddEngagementBudgetTest) {
|
| const GURL origin(kTestOrigin);
|
| base::SimpleTestClock* clock = SetClockForTesting();
|
| @@ -106,33 +114,36 @@ TEST_F(BudgetDatabaseTest, AddEngagementBudgetTest) {
|
| GetBudgetDetails();
|
| ASSERT_TRUE(success_);
|
| ASSERT_EQ(2U, prediction_.size());
|
| - ASSERT_EQ(kDefaultEngagement, prediction_[0].budget_at);
|
| - ASSERT_EQ(0, prediction_[1].budget_at);
|
| - ASSERT_EQ(expiration_time, prediction_[1].time);
|
| + ASSERT_EQ(kDefaultEngagement, prediction_[0]->budget_at);
|
| + ASSERT_EQ(0, prediction_[1]->budget_at);
|
| + ASSERT_EQ(expiration_time.ToDoubleT(), prediction_[1]->time);
|
|
|
| // Advance time 1 day and add more engagement budget.
|
| clock->Advance(base::TimeDelta::FromDays(1));
|
| GetBudgetDetails();
|
|
|
| - // The budget should now have 1 full share plus 1/3 share.
|
| + // The budget should now have 1 full share plus 1 daily budget.
|
| ASSERT_TRUE(success_);
|
| ASSERT_EQ(3U, prediction_.size());
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 4 / 3, prediction_[0].budget_at);
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3, prediction_[1].budget_at);
|
| - ASSERT_EQ(expiration_time, prediction_[1].time);
|
| - ASSERT_EQ(0, prediction_[2].budget_at);
|
| - ASSERT_EQ(expiration_time + base::TimeDelta::FromDays(1),
|
| - prediction_[2].time);
|
| + double daily_budget = kDefaultEngagement * 24 / kDefaultExpirationInHours;
|
| + ASSERT_DOUBLE_EQ(kDefaultEngagement + daily_budget,
|
| + prediction_[0]->budget_at);
|
| + ASSERT_DOUBLE_EQ(daily_budget, prediction_[1]->budget_at);
|
| + ASSERT_EQ(expiration_time.ToDoubleT(), prediction_[1]->time);
|
| + ASSERT_EQ(0, prediction_[2]->budget_at);
|
| + ASSERT_EQ((expiration_time + base::TimeDelta::FromDays(1)).ToDoubleT(),
|
| + prediction_[2]->time);
|
|
|
| // Advance time by 59 minutes and check that no engagement budget is added
|
| // since budget should only be added for > 1 hour increments.
|
| clock->Advance(base::TimeDelta::FromMinutes(59));
|
| + GetBudgetDetails();
|
|
|
| // The budget should be the same as before the attempted add.
|
| - GetBudgetDetails();
|
| ASSERT_TRUE(success_);
|
| ASSERT_EQ(3U, prediction_.size());
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 4 / 3, prediction_[0].budget_at);
|
| + ASSERT_DOUBLE_EQ(kDefaultEngagement + daily_budget,
|
| + prediction_[0]->budget_at);
|
| }
|
|
|
| TEST_F(BudgetDatabaseTest, SpendBudgetTest) {
|
| @@ -156,29 +167,61 @@ TEST_F(BudgetDatabaseTest, SpendBudgetTest) {
|
| // There should still be three chunks of budget of size kDefaultEngagement-1,
|
| // kDefaultEngagement, and kDefaultEngagement.
|
| ASSERT_EQ(4U, prediction_.size());
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 5 / 3 - 1, prediction_[0].budget_at);
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 2 / 3, prediction_[1].budget_at);
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3, prediction_[2].budget_at);
|
| - ASSERT_DOUBLE_EQ(0, prediction_[3].budget_at);
|
| + double daily_budget = kDefaultEngagement * 24 / kDefaultExpirationInHours;
|
| + ASSERT_DOUBLE_EQ(kDefaultEngagement + 2 * daily_budget - 1,
|
| + prediction_[0]->budget_at);
|
| + ASSERT_DOUBLE_EQ(daily_budget * 2, prediction_[1]->budget_at);
|
| + ASSERT_DOUBLE_EQ(daily_budget, prediction_[2]->budget_at);
|
| + ASSERT_DOUBLE_EQ(0, prediction_[3]->budget_at);
|
|
|
| // Now spend enough that it will use up the rest of the first chunk and all of
|
| // the second chunk, but not all of the third chunk.
|
| - ASSERT_TRUE(SpendBudget(origin, kDefaultEngagement * 4 / 3));
|
| + ASSERT_TRUE(SpendBudget(origin, kDefaultEngagement + daily_budget));
|
| GetBudgetDetails();
|
| ASSERT_EQ(2U, prediction_.size());
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3 - 1,
|
| - prediction_.begin()->budget_at);
|
| + ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at);
|
|
|
| // Validate that the code returns false if SpendBudget tries to spend more
|
| // budget than the origin has.
|
| EXPECT_FALSE(SpendBudget(origin, kDefaultEngagement));
|
| GetBudgetDetails();
|
| ASSERT_EQ(2U, prediction_.size());
|
| - ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3 - 1,
|
| - prediction_.begin()->budget_at);
|
| + ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at);
|
|
|
| // Advance time until the last remaining chunk should be expired, then query
|
| // for the full engagement worth of budget.
|
| - clock->Advance(base::TimeDelta::FromDays(6));
|
| + clock->Advance(base::TimeDelta::FromHours(kDefaultExpirationInHours + 1));
|
| EXPECT_TRUE(SpendBudget(origin, kDefaultEngagement));
|
| }
|
| +
|
| +TEST_F(BudgetDatabaseTest, GetBudgetNegativeTime) {
|
| + const GURL origin(kTestOrigin);
|
| + base::SimpleTestClock* clock = SetClockForTesting();
|
| +
|
| + // Set the default site engagement.
|
| + SetSiteEngagementScore(origin, kDefaultEngagement);
|
| +
|
| + // Initialize the budget with two chunks.
|
| + GetBudgetDetails();
|
| + clock->Advance(base::TimeDelta::FromDays(1));
|
| + GetBudgetDetails();
|
| +
|
| + // Save off the budget total.
|
| + ASSERT_EQ(3U, prediction_.size());
|
| + double budget = prediction_[0]->budget_at;
|
| +
|
| + // Move the clock backwards in time to before the budget awards.
|
| + clock->SetNow(clock->Now() - base::TimeDelta::FromDays(5));
|
| +
|
| + // Make sure the budget is the same.
|
| + GetBudgetDetails();
|
| + ASSERT_EQ(3U, prediction_.size());
|
| + ASSERT_EQ(budget, prediction_[0]->budget_at);
|
| +
|
| + // Now move the clock back to the original time and check that no extra budget
|
| + // is awarded.
|
| + clock->SetNow(clock->Now() + base::TimeDelta::FromDays(5));
|
| + GetBudgetDetails();
|
| + ASSERT_EQ(3U, prediction_.size());
|
| + ASSERT_EQ(budget, prediction_[0]->budget_at);
|
| +}
|
|
|