| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return expectation_; | 77 return expectation_; |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Setup a test clock so that the tests can control time. | 80 // Setup a test clock so that the tests can control time. |
| 81 base::SimpleTestClock* SetClockForTesting() { | 81 base::SimpleTestClock* SetClockForTesting() { |
| 82 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 82 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 83 db_.SetClockForTesting(base::WrapUnique(clock)); | 83 db_.SetClockForTesting(base::WrapUnique(clock)); |
| 84 return clock; | 84 return clock; |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Query the database to check if the origin is in the cache. |
| 88 bool IsCached(const GURL& origin) { return db_.IsCached(origin); } |
| 89 |
| 87 protected: | 90 protected: |
| 88 bool success_; | 91 bool success_; |
| 89 | 92 |
| 90 private: | 93 private: |
| 91 content::TestBrowserThreadBundle thread_bundle_; | 94 content::TestBrowserThreadBundle thread_bundle_; |
| 92 std::unique_ptr<budget_service::Budget> budget_; | 95 std::unique_ptr<budget_service::Budget> budget_; |
| 93 TestingProfile profile_; | 96 TestingProfile profile_; |
| 94 BudgetDatabase db_; | 97 BudgetDatabase db_; |
| 95 double debt_ = 0; | 98 double debt_ = 0; |
| 96 BudgetDatabase::BudgetExpectation expectation_; | 99 BudgetDatabase::BudgetExpectation expectation_; |
| 97 }; | 100 }; |
| 98 | 101 |
| 99 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | 102 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { |
| 100 const GURL origin(kTestOrigin); | 103 const GURL origin(kTestOrigin); |
| 101 base::SimpleTestClock* clock = SetClockForTesting(); | 104 base::SimpleTestClock* clock = SetClockForTesting(); |
| 102 base::TimeDelta expiration( | 105 base::TimeDelta expiration( |
| 103 base::TimeDelta::FromHours(kDefaultExpirationInHours)); | 106 base::TimeDelta::FromHours(kDefaultExpirationInHours)); |
| 107 base::Time starting_time = clock->Now(); |
| 104 base::Time expiration_time = clock->Now() + expiration; | 108 base::Time expiration_time = clock->Now() + expiration; |
| 105 | 109 |
| 106 // Add two budget chunks with different expirations (default expiration and | 110 // Add two budget chunks with different expirations (default expiration and |
| 107 // default expiration + 1 day). | 111 // default expiration + 1 day). |
| 108 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); | 112 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); |
| 109 clock->Advance(base::TimeDelta::FromDays(1)); | 113 clock->Advance(base::TimeDelta::FromDays(1)); |
| 110 ASSERT_TRUE(AddBudget(origin, kDefaultBudget2)); | 114 ASSERT_TRUE(AddBudget(origin, kDefaultBudget2)); |
| 111 | 115 |
| 112 // Get the budget. | 116 // Get the budget. |
| 113 GetBudgetDetails(); | 117 GetBudgetDetails(); |
| 114 | 118 |
| 115 // Get the expectation and validate it. | 119 // Get the expectation and validate it. |
| 116 const auto& expected_value = expectation(); | 120 const auto& expected_value = expectation(); |
| 117 ASSERT_TRUE(success_); | 121 ASSERT_TRUE(success_); |
| 118 ASSERT_EQ(3U, expected_value.size()); | 122 ASSERT_EQ(3U, expected_value.size()); |
| 119 | 123 |
| 120 // Make sure that the correct data is returned. | 124 // Make sure that the correct data is returned. |
| 121 auto iter = expected_value.begin(); | 125 auto iter = expected_value.begin(); |
| 122 | 126 |
| 123 // First value should be [total_budget, now] | 127 // First value should be [total_budget, now] |
| 124 EXPECT_EQ(kDefaultBudget1 + kDefaultBudget2, iter->first); | 128 EXPECT_EQ(kDefaultBudget1 + kDefaultBudget2, iter->first); |
| 125 // TODO(harkness): This will be "now" in the final version. For now, it's | 129 EXPECT_EQ(clock->Now(), iter->second); |
| 126 // just 0. | |
| 127 EXPECT_EQ(0, iter->second); | |
| 128 | 130 |
| 129 // The next value should be the budget after the first chunk expires. | 131 // The next value should be the budget after the first chunk expires. |
| 130 iter++; | 132 iter++; |
| 131 EXPECT_EQ(kDefaultBudget2, iter->first); | 133 EXPECT_EQ(kDefaultBudget2, iter->first); |
| 132 EXPECT_EQ(expiration_time.ToInternalValue(), iter->second); | 134 EXPECT_EQ(expiration_time, iter->second); |
| 133 | 135 |
| 134 // The final value gives the budget of 0.0 after the second chunk expires. | 136 // The final value gives the budget of 0.0 after the second chunk expires. |
| 135 expiration_time += base::TimeDelta::FromDays(1); | 137 expiration_time += base::TimeDelta::FromDays(1); |
| 136 iter++; | 138 iter++; |
| 137 EXPECT_EQ(0, iter->first); | 139 EXPECT_EQ(0, iter->first); |
| 138 EXPECT_EQ(expiration_time.ToInternalValue(), iter->second); | 140 EXPECT_EQ(expiration_time, iter->second); |
| 141 |
| 142 // Advance the time until the first chunk of budget should be expired. |
| 143 clock->SetNow(starting_time + |
| 144 base::TimeDelta::FromHours(kDefaultExpirationInHours)); |
| 145 |
| 146 // Get the new budget and check that kDefaultBudget1 has been removed. |
| 147 GetBudgetDetails(); |
| 148 iter = expectation().begin(); |
| 149 ASSERT_EQ(2U, expectation().size()); |
| 150 EXPECT_EQ(kDefaultBudget2, iter->first); |
| 151 iter++; |
| 152 EXPECT_EQ(0, iter->first); |
| 153 |
| 154 // Advace the time until both chunks of budget should be expired. |
| 155 clock->SetNow(starting_time + |
| 156 base::TimeDelta::FromHours(kDefaultExpirationInHours) + |
| 157 base::TimeDelta::FromDays(1)); |
| 158 |
| 159 GetBudgetDetails(); |
| 160 iter = expectation().begin(); |
| 161 ASSERT_EQ(1U, expectation().size()); |
| 162 EXPECT_EQ(0, iter->first); |
| 163 |
| 164 // Now that the entire budget has expired, check that the entry in the map |
| 165 // has been removed. |
| 166 EXPECT_FALSE(IsCached(origin)); |
| 139 } | 167 } |
| OLD | NEW |