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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 BudgetDatabase db_; | 94 BudgetDatabase db_; |
95 double debt_ = 0; | 95 double debt_ = 0; |
96 BudgetDatabase::BudgetExpectation expectation_; | 96 BudgetDatabase::BudgetExpectation expectation_; |
97 }; | 97 }; |
98 | 98 |
99 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { | 99 TEST_F(BudgetDatabaseTest, ReadAndWriteTest) { |
100 const GURL origin(kTestOrigin); | 100 const GURL origin(kTestOrigin); |
101 base::SimpleTestClock* clock = SetClockForTesting(); | 101 base::SimpleTestClock* clock = SetClockForTesting(); |
102 base::TimeDelta expiration( | 102 base::TimeDelta expiration( |
103 base::TimeDelta::FromHours(kDefaultExpirationInHours)); | 103 base::TimeDelta::FromHours(kDefaultExpirationInHours)); |
104 base::Time starting_time = clock->Now(); | |
104 base::Time expiration_time = clock->Now() + expiration; | 105 base::Time expiration_time = clock->Now() + expiration; |
105 | 106 |
106 // Add two budget chunks with different expirations (default expiration and | 107 // Add two budget chunks with different expirations (default expiration and |
107 // default expiration + 1 day). | 108 // default expiration + 1 day). |
108 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); | 109 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); |
109 clock->Advance(base::TimeDelta::FromDays(1)); | 110 clock->Advance(base::TimeDelta::FromDays(1)); |
110 ASSERT_TRUE(AddBudget(origin, kDefaultBudget2)); | 111 ASSERT_TRUE(AddBudget(origin, kDefaultBudget2)); |
111 | 112 |
112 // Get the budget. | 113 // Get the budget. |
113 GetBudgetDetails(); | 114 GetBudgetDetails(); |
114 | 115 |
115 // Get the expectation and validate it. | 116 // Get the expectation and validate it. |
116 const auto& expected_value = expectation(); | 117 const auto& expected_value = expectation(); |
117 ASSERT_TRUE(success_); | 118 ASSERT_TRUE(success_); |
118 ASSERT_EQ(3U, expected_value.size()); | 119 ASSERT_EQ(3U, expected_value.size()); |
119 | 120 |
120 // Make sure that the correct data is returned. | 121 // Make sure that the correct data is returned. |
121 auto iter = expected_value.begin(); | 122 auto iter = expected_value.begin(); |
122 | 123 |
123 // First value should be [total_budget, now] | 124 // First value should be [total_budget, now] |
124 EXPECT_EQ(kDefaultBudget1 + kDefaultBudget2, iter->first); | 125 EXPECT_EQ(kDefaultBudget1 + kDefaultBudget2, iter->first); |
125 // TODO(harkness): This will be "now" in the final version. For now, it's | 126 EXPECT_EQ(clock->Now().ToInternalValue(), iter->second); |
126 // just 0. | |
127 EXPECT_EQ(0, iter->second); | |
128 | 127 |
129 // 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. |
130 iter++; | 129 iter++; |
131 EXPECT_EQ(kDefaultBudget2, iter->first); | 130 EXPECT_EQ(kDefaultBudget2, iter->first); |
132 EXPECT_EQ(expiration_time.ToInternalValue(), iter->second); | 131 EXPECT_EQ(expiration_time.ToInternalValue(), iter->second); |
133 | 132 |
134 // 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. |
135 expiration_time += base::TimeDelta::FromDays(1); | 134 expiration_time += base::TimeDelta::FromDays(1); |
136 iter++; | 135 iter++; |
137 EXPECT_EQ(0, iter->first); | 136 EXPECT_EQ(0, iter->first); |
138 EXPECT_EQ(expiration_time.ToInternalValue(), iter->second); | 137 EXPECT_EQ(expiration_time.ToInternalValue(), iter->second); |
138 | |
139 // Advance the time until the first chunk of budget should be expired. | |
140 clock->SetNow(starting_time + | |
141 base::TimeDelta::FromHours(kDefaultExpirationInHours)); | |
142 | |
143 // Get the new budget and check that kDefaultBudget1 has been removed. | |
144 GetBudgetDetails(); | |
145 iter = expectation().begin(); | |
Peter Beverloo
2016/08/01 17:35:28
Please ASSERT() something before this line, if beg
harkness
2016/08/08 14:42:43
Done.
| |
146 EXPECT_EQ(kDefaultBudget2, iter->first); | |
147 iter++; | |
148 EXPECT_EQ(0, iter->first); | |
149 | |
150 // Advace the time until both chunks of budget should be expired. | |
151 clock->SetNow(starting_time + | |
152 base::TimeDelta::FromHours(kDefaultExpirationInHours) + | |
153 base::TimeDelta::FromDays(1)); | |
154 | |
155 GetBudgetDetails(); | |
156 iter = expectation().begin(); | |
157 EXPECT_EQ(0, iter->first); | |
139 } | 158 } |
OLD | NEW |