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" |
11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
12 #include "components/leveldb_proto/proto_database.h" | 12 #include "components/leveldb_proto/proto_database.h" |
13 #include "components/leveldb_proto/proto_database_impl.h" | 13 #include "components/leveldb_proto/proto_database_impl.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const double kDefaultBudget1 = 1.234; | 20 const double kDefaultBudget1 = 1.234; |
21 const double kDefaultBudget2 = 2.345; | 21 const double kDefaultBudget2 = 2.345; |
22 const double kDefaultExpirationInHours = 72; | 22 const double kDefaultExpirationInHours = 72; |
23 const double kDefaultEngagement = 30.0; | |
23 | 24 |
24 const char kTestOrigin[] = "https://example.com"; | 25 const char kTestOrigin[] = "https://example.com"; |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 class BudgetDatabaseTest : public ::testing::Test { | 29 class BudgetDatabaseTest : public ::testing::Test { |
29 public: | 30 public: |
30 BudgetDatabaseTest() | 31 BudgetDatabaseTest() |
31 : success_(false), | 32 : success_(false), |
32 db_(profile_.GetPath().Append(FILE_PATH_LITERAL("BudgetDabase")), | 33 db_(profile_.GetPath().Append(FILE_PATH_LITERAL("BudgetDabase")), |
33 base::ThreadTaskRunnerHandle::Get()) {} | 34 base::ThreadTaskRunnerHandle::Get()) {} |
34 | 35 |
35 // The BudgetDatabase assumes that a budget will always be queried before it | 36 // The BudgetDatabase assumes that a budget will always be queried before it |
36 // is written to. Use GetBudgetDetails() to pre-populate the cache. | 37 // is written to. Use GetBudgetDetails() to pre-populate the cache. |
37 void SetUp() override { GetBudgetDetails(); } | 38 void SetUp() override { GetBudgetDetails(); } |
38 | 39 |
39 void AddBudgetComplete(base::Closure run_loop_closure, bool success) { | 40 void WriteBudgetComplete(base::Closure run_loop_closure, bool success) { |
40 success_ = success; | 41 success_ = success; |
41 run_loop_closure.Run(); | 42 run_loop_closure.Run(); |
42 } | 43 } |
43 | 44 |
44 // Add budget to the origin. | 45 // Add budget to the origin. |
45 bool AddBudget(const GURL& origin, double amount) { | 46 bool AddBudget(const GURL& origin, double amount) { |
46 base::RunLoop run_loop; | 47 base::RunLoop run_loop; |
47 db_.AddBudget(origin, amount, | 48 db_.AddBudget(origin, amount, |
48 base::Bind(&BudgetDatabaseTest::AddBudgetComplete, | 49 base::Bind(&BudgetDatabaseTest::WriteBudgetComplete, |
49 base::Unretained(this), run_loop.QuitClosure())); | 50 base::Unretained(this), run_loop.QuitClosure())); |
50 run_loop.Run(); | 51 run_loop.Run(); |
51 return success_; | 52 return success_; |
52 } | 53 } |
53 | 54 |
55 // Add engagement based budget to the origin. | |
56 bool AddEngagementBudget(const GURL& origin, double sesScore) { | |
57 base::RunLoop run_loop; | |
58 db_.AddEngagementBudget( | |
59 origin, sesScore, | |
60 base::Bind(&BudgetDatabaseTest::WriteBudgetComplete, | |
61 base::Unretained(this), run_loop.QuitClosure())); | |
62 run_loop.Run(); | |
63 return success_; | |
64 } | |
65 | |
66 // Spend budget for the origin. | |
67 bool SpendBudget(const GURL& origin, double amount) { | |
68 base::RunLoop run_loop; | |
69 db_.SpendBudget(origin, amount, | |
70 base::Bind(&BudgetDatabaseTest::WriteBudgetComplete, | |
71 base::Unretained(this), run_loop.QuitClosure())); | |
72 run_loop.Run(); | |
73 return success_; | |
74 } | |
75 | |
54 void GetBudgetDetailsComplete( | 76 void GetBudgetDetailsComplete( |
55 base::Closure run_loop_closure, | 77 base::Closure run_loop_closure, |
56 bool success, | 78 bool success, |
57 const BudgetDatabase::BudgetExpectation& expectation) { | 79 const BudgetDatabase::BudgetExpectation& expectation) { |
58 success_ = success; | 80 success_ = success; |
59 expectation_ = expectation; | 81 expectation_ = expectation; |
60 run_loop_closure.Run(); | 82 run_loop_closure.Run(); |
61 } | 83 } |
62 | 84 |
63 // Get the full set of budget expectations for the origin. | 85 // Get the full set of budget expectations for the origin. |
64 void GetBudgetDetails() { | 86 void GetBudgetDetails() { |
65 base::RunLoop run_loop; | 87 base::RunLoop run_loop; |
66 db_.GetBudgetDetails( | 88 db_.GetBudgetDetails( |
67 GURL(kTestOrigin), | 89 GURL(kTestOrigin), |
68 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, | 90 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, |
69 base::Unretained(this), run_loop.QuitClosure())); | 91 base::Unretained(this), run_loop.QuitClosure())); |
70 run_loop.Run(); | 92 run_loop.Run(); |
71 } | 93 } |
72 | 94 |
73 Profile* profile() { return &profile_; } | 95 Profile* profile() { return &profile_; } |
74 const BudgetDatabase::BudgetExpectation& expectation() { | 96 const BudgetDatabase::BudgetExpectation& expectation() { |
johnme
2016/08/19 18:48:36
Nit: newline before please
harkness
2016/08/22 13:46:26
Done.
| |
75 return expectation_; | 97 return expectation_; |
76 } | 98 } |
77 | 99 |
78 // Setup a test clock so that the tests can control time. | 100 // Setup a test clock so that the tests can control time. |
79 base::SimpleTestClock* SetClockForTesting() { | 101 base::SimpleTestClock* SetClockForTesting() { |
80 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 102 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
81 db_.SetClockForTesting(base::WrapUnique(clock)); | 103 db_.SetClockForTesting(base::WrapUnique(clock)); |
82 return clock; | 104 return clock; |
83 } | 105 } |
84 | 106 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 | 177 |
156 GetBudgetDetails(); | 178 GetBudgetDetails(); |
157 iter = expectation().begin(); | 179 iter = expectation().begin(); |
158 ASSERT_EQ(1U, expectation().size()); | 180 ASSERT_EQ(1U, expectation().size()); |
159 EXPECT_EQ(0, iter->budget_at); | 181 EXPECT_EQ(0, iter->budget_at); |
160 | 182 |
161 // Now that the entire budget has expired, check that the entry in the map | 183 // Now that the entire budget has expired, check that the entry in the map |
162 // has been removed. | 184 // has been removed. |
163 EXPECT_FALSE(IsCached(origin)); | 185 EXPECT_FALSE(IsCached(origin)); |
164 } | 186 } |
187 | |
188 TEST_F(BudgetDatabaseTest, AddEngagementBudgetTest) { | |
189 const GURL origin(kTestOrigin); | |
190 base::SimpleTestClock* clock = SetClockForTesting(); | |
191 | |
192 // Add a chunk of budget to a non-existant origin. This should add the full | |
193 // amount of engagement. | |
194 ASSERT_TRUE(AddEngagementBudget(origin, kDefaultEngagement)); | |
195 | |
196 // Get the budget. | |
197 GetBudgetDetails(); | |
198 | |
johnme
2016/08/19 18:48:36
Nit: remove newline, since the ASSERTs below are t
harkness
2016/08/22 13:46:26
Done.
| |
199 // The budget should include a full share of the engagement. Just check the | |
200 // value of the budget state at the start of the list for the total budget. | |
201 const auto& expected_value = expectation(); | |
johnme
2016/08/19 18:48:36
Nit: Don't bother caching this reference; the comp
harkness
2016/08/22 13:46:26
Made prediction_ protected, it just makes the code
| |
202 ASSERT_TRUE(success_); | |
203 ASSERT_EQ(2U, expected_value.size()); | |
204 ASSERT_EQ(kDefaultEngagement, expectation().begin()->budget_at); | |
johnme
2016/08/19 18:48:36
s/expectation()/expected_value/ since you've cache
harkness
2016/08/22 13:46:26
Directly accesses prediction_ now.
| |
205 | |
206 // Advance time 1 day and add more engagement budget. | |
207 clock->Advance(base::TimeDelta::FromDays(1)); | |
208 ASSERT_TRUE(AddEngagementBudget(origin, kDefaultEngagement)); | |
209 GetBudgetDetails(); | |
johnme
2016/08/19 18:48:36
Add newline before getBudgetDetails()
harkness
2016/08/22 13:46:26
Done.
| |
210 | |
johnme
2016/08/19 18:48:36
Ditto remove newline
harkness
2016/08/22 13:46:26
Done.
| |
211 // The budget should now have 1 full share plus 1/3 share. | |
212 ASSERT_TRUE(success_); | |
213 ASSERT_EQ(3U, expectation().size()); | |
214 ASSERT_DOUBLE_EQ(kDefaultEngagement * 4 / 3, | |
johnme
2016/08/19 18:48:36
Might be nice to re-test the other 2 values too, t
harkness
2016/08/22 13:46:26
Good idea on converting the member to be a vector.
| |
215 expectation().begin()->budget_at); | |
216 } | |
217 | |
218 TEST_F(BudgetDatabaseTest, SpendBudgetTest) { | |
219 const GURL origin(kTestOrigin); | |
220 base::SimpleTestClock* clock = SetClockForTesting(); | |
221 base::Time starting_time = clock->Now(); | |
222 | |
223 // Intialize the budget with several chunks. | |
224 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); | |
225 clock->Advance(base::TimeDelta::FromDays(1)); | |
226 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); | |
227 clock->Advance(base::TimeDelta::FromDays(1)); | |
228 ASSERT_TRUE(AddBudget(origin, kDefaultBudget1)); | |
229 | |
230 // Reset the clock then spend an amount of budget less than kDefaultBudget. | |
231 clock->SetNow(starting_time); | |
232 ASSERT_TRUE(SpendBudget(origin, 1)); | |
233 GetBudgetDetails(); | |
234 | |
235 // There should still be three chunks of budget of size kDefaultBudget-1, | |
236 // kDefaultBudget, and kDefaultBudget. | |
237 ASSERT_EQ(4U, expectation().size()); | |
238 ASSERT_DOUBLE_EQ(kDefaultBudget1 * 3 - 1, expectation().begin()->budget_at); | |
johnme
2016/08/19 18:48:36
Ditto with checking the other values in expectatio
harkness
2016/08/22 13:46:26
Done.
| |
239 | |
240 // Now spend enough that it will use up the rest of the first chunk and all of | |
241 // the second chunk, but not all of the third chunk. | |
242 ASSERT_TRUE(SpendBudget(origin, kDefaultBudget1 * 2)); | |
243 GetBudgetDetails(); | |
244 ASSERT_EQ(2U, expectation().size()); | |
245 ASSERT_DOUBLE_EQ(kDefaultBudget1 - 1, expectation().begin()->budget_at); | |
246 | |
247 // Validate that the code returns false if SpendBudget tries to spend more | |
248 // budget than the origin has. | |
249 EXPECT_FALSE(SpendBudget(origin, kDefaultBudget1)); | |
johnme
2016/08/19 18:48:36
After this, check that the budget hasn't changed?
harkness
2016/08/22 13:46:26
Done.
| |
250 | |
251 // Advance time until the last remaining chunk should be expired, then query | |
252 // for what would be a valid amount of budget if the chunks weren't expired. | |
253 clock->Advance(base::TimeDelta::FromDays(6)); | |
254 EXPECT_FALSE(SpendBudget(origin, 0.01)); | |
255 } | |
OLD | NEW |