| 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/budget_service/budget_database.h" | 5 #include "chrome/browser/budget_service/budget_database.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/test/simple_test_clock.h" | 8 #include "base/test/simple_test_clock.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/budget_service/budget.pb.h" | 10 #include "chrome/browser/budget_service/budget.pb.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 db_.AddBudget(origin, amount, | 47 db_.AddBudget(origin, amount, |
| 48 base::Bind(&BudgetDatabaseTest::AddBudgetComplete, | 48 base::Bind(&BudgetDatabaseTest::AddBudgetComplete, |
| 49 base::Unretained(this), run_loop.QuitClosure())); | 49 base::Unretained(this), run_loop.QuitClosure())); |
| 50 run_loop.Run(); | 50 run_loop.Run(); |
| 51 return success_; | 51 return success_; |
| 52 } | 52 } |
| 53 | 53 |
| 54 void GetBudgetDetailsComplete( | 54 void GetBudgetDetailsComplete( |
| 55 base::Closure run_loop_closure, | 55 base::Closure run_loop_closure, |
| 56 bool success, | 56 bool success, |
| 57 double debt, | |
| 58 const BudgetDatabase::BudgetExpectation& expectation) { | 57 const BudgetDatabase::BudgetExpectation& expectation) { |
| 59 success_ = success; | 58 success_ = success; |
| 60 debt_ = debt; | |
| 61 expectation_ = expectation; | 59 expectation_ = expectation; |
| 62 run_loop_closure.Run(); | 60 run_loop_closure.Run(); |
| 63 } | 61 } |
| 64 | 62 |
| 65 // Get the full set of budget expectations for the origin. | 63 // Get the full set of budget expectations for the origin. |
| 66 void GetBudgetDetails() { | 64 void GetBudgetDetails() { |
| 67 base::RunLoop run_loop; | 65 base::RunLoop run_loop; |
| 68 db_.GetBudgetDetails( | 66 db_.GetBudgetDetails( |
| 69 GURL(kTestOrigin), | 67 GURL(kTestOrigin), |
| 70 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, | 68 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 88 bool IsCached(const GURL& origin) { return db_.IsCached(origin); } | 86 bool IsCached(const GURL& origin) { return db_.IsCached(origin); } |
| 89 | 87 |
| 90 protected: | 88 protected: |
| 91 bool success_; | 89 bool success_; |
| 92 | 90 |
| 93 private: | 91 private: |
| 94 content::TestBrowserThreadBundle thread_bundle_; | 92 content::TestBrowserThreadBundle thread_bundle_; |
| 95 std::unique_ptr<budget_service::Budget> budget_; | 93 std::unique_ptr<budget_service::Budget> budget_; |
| 96 TestingProfile profile_; | 94 TestingProfile profile_; |
| 97 BudgetDatabase db_; | 95 BudgetDatabase db_; |
| 98 double debt_ = 0; | |
| 99 BudgetDatabase::BudgetExpectation expectation_; | 96 BudgetDatabase::BudgetExpectation expectation_; |
| 100 }; | 97 }; |
| 101 | 98 |
| 102 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | 99 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { |
| 103 const GURL origin(kTestOrigin); | 100 const GURL origin(kTestOrigin); |
| 104 base::SimpleTestClock* clock = SetClockForTesting(); | 101 base::SimpleTestClock* clock = SetClockForTesting(); |
| 105 base::TimeDelta expiration( | 102 base::TimeDelta expiration( |
| 106 base::TimeDelta::FromHours(kDefaultExpirationInHours)); | 103 base::TimeDelta::FromHours(kDefaultExpirationInHours)); |
| 107 base::Time starting_time = clock->Now(); | 104 base::Time starting_time = clock->Now(); |
| 108 base::Time expiration_time = clock->Now() + expiration; | 105 base::Time expiration_time = clock->Now() + expiration; |
| 109 | 106 |
| 110 // Add two budget chunks with different expirations (default expiration and | 107 // Add two budget chunks with different expirations (default expiration and |
| 111 // default expiration + 1 day). | 108 // default expiration + 1 day). |
| 112 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); | 109 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); |
| 113 clock->Advance(base::TimeDelta::FromDays(1)); | 110 clock->Advance(base::TimeDelta::FromDays(1)); |
| 114 ASSERT_TRUE(AddBudget(origin, kDefaultBudget2)); | 111 ASSERT_TRUE(AddBudget(origin, kDefaultBudget2)); |
| 115 | 112 |
| 116 // Get the budget. | 113 // Get the budget. |
| 117 GetBudgetDetails(); | 114 GetBudgetDetails(); |
| 118 | 115 |
| 119 // Get the expectation and validate it. | 116 // Get the expectation and validate it. |
| 120 const auto& expected_value = expectation(); | 117 const auto& expected_value = expectation(); |
| 121 ASSERT_TRUE(success_); | 118 ASSERT_TRUE(success_); |
| 122 ASSERT_EQ(3U, expected_value.size()); | 119 ASSERT_EQ(3U, expected_value.size()); |
| 123 | 120 |
| 124 // Make sure that the correct data is returned. | 121 // Make sure that the correct data is returned. |
| 125 auto iter = expected_value.begin(); | 122 auto iter = expected_value.begin(); |
| 126 | 123 |
| 127 // First value should be [total_budget, now] | 124 // First value should be [total_budget, now] |
| 128 EXPECT_EQ(kDefaultBudget1 + kDefaultBudget2, iter->first); | 125 EXPECT_EQ(kDefaultBudget1 + kDefaultBudget2, iter->budget_at); |
| 129 EXPECT_EQ(clock->Now(), iter->second); | 126 EXPECT_EQ(clock->Now(), iter->time); |
| 130 | 127 |
| 131 // The next value should be the budget after the first chunk expires. | 128 // The next value should be the budget after the first chunk expires. |
| 132 iter++; | 129 iter++; |
| 133 EXPECT_EQ(kDefaultBudget2, iter->first); | 130 EXPECT_EQ(kDefaultBudget2, iter->budget_at); |
| 134 EXPECT_EQ(expiration_time, iter->second); | 131 EXPECT_EQ(expiration_time, iter->time); |
| 135 | 132 |
| 136 // The final value gives the budget of 0.0 after the second chunk expires. | 133 // The final value gives the budget of 0.0 after the second chunk expires. |
| 137 expiration_time += base::TimeDelta::FromDays(1); | 134 expiration_time += base::TimeDelta::FromDays(1); |
| 138 iter++; | 135 iter++; |
| 139 EXPECT_EQ(0, iter->first); | 136 EXPECT_EQ(0, iter->budget_at); |
| 140 EXPECT_EQ(expiration_time, iter->second); | 137 EXPECT_EQ(expiration_time, iter->time); |
| 141 | 138 |
| 142 // Advance the time until the first chunk of budget should be expired. | 139 // Advance the time until the first chunk of budget should be expired. |
| 143 clock->SetNow(starting_time + | 140 clock->SetNow(starting_time + |
| 144 base::TimeDelta::FromHours(kDefaultExpirationInHours)); | 141 base::TimeDelta::FromHours(kDefaultExpirationInHours)); |
| 145 | 142 |
| 146 // Get the new budget and check that kDefaultBudget1 has been removed. | 143 // Get the new budget and check that kDefaultBudget1 has been removed. |
| 147 GetBudgetDetails(); | 144 GetBudgetDetails(); |
| 148 iter = expectation().begin(); | 145 iter = expectation().begin(); |
| 149 ASSERT_EQ(2U, expectation().size()); | 146 ASSERT_EQ(2U, expectation().size()); |
| 150 EXPECT_EQ(kDefaultBudget2, iter->first); | 147 EXPECT_EQ(kDefaultBudget2, iter->budget_at); |
| 151 iter++; | 148 iter++; |
| 152 EXPECT_EQ(0, iter->first); | 149 EXPECT_EQ(0, iter->budget_at); |
| 153 | 150 |
| 154 // Advace the time until both chunks of budget should be expired. | 151 // Advace the time until both chunks of budget should be expired. |
| 155 clock->SetNow(starting_time + | 152 clock->SetNow(starting_time + |
| 156 base::TimeDelta::FromHours(kDefaultExpirationInHours) + | 153 base::TimeDelta::FromHours(kDefaultExpirationInHours) + |
| 157 base::TimeDelta::FromDays(1)); | 154 base::TimeDelta::FromDays(1)); |
| 158 | 155 |
| 159 GetBudgetDetails(); | 156 GetBudgetDetails(); |
| 160 iter = expectation().begin(); | 157 iter = expectation().begin(); |
| 161 ASSERT_EQ(1U, expectation().size()); | 158 ASSERT_EQ(1U, expectation().size()); |
| 162 EXPECT_EQ(0, iter->first); | 159 EXPECT_EQ(0, iter->budget_at); |
| 163 | 160 |
| 164 // Now that the entire budget has expired, check that the entry in the map | 161 // Now that the entire budget has expired, check that the entry in the map |
| 165 // has been removed. | 162 // has been removed. |
| 166 EXPECT_FALSE(IsCached(origin)); | 163 EXPECT_FALSE(IsCached(origin)); |
| 167 } | 164 } |
| OLD | NEW |