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/browser/engagement/site_engagement_service.h" | 11 #include "chrome/browser/engagement/site_engagement_service.h" |
12 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
13 #include "components/leveldb_proto/proto_database.h" | 13 #include "components/leveldb_proto/proto_database.h" |
14 #include "components/leveldb_proto/proto_database_impl.h" | 14 #include "components/leveldb_proto/proto_database_impl.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "mojo/public/cpp/bindings/array.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const double kDefaultExpirationInHours = 72; | 22 const double kDefaultExpirationInHours = 240; |
22 const double kDefaultEngagement = 30.0; | 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), |
(...skipping 11 matching lines...) Expand all Loading... | |
43 base::RunLoop run_loop; | 44 base::RunLoop run_loop; |
44 db_.SpendBudget(origin, amount, | 45 db_.SpendBudget(origin, amount, |
45 base::Bind(&BudgetDatabaseTest::WriteBudgetComplete, | 46 base::Bind(&BudgetDatabaseTest::WriteBudgetComplete, |
46 base::Unretained(this), run_loop.QuitClosure())); | 47 base::Unretained(this), run_loop.QuitClosure())); |
47 run_loop.Run(); | 48 run_loop.Run(); |
48 return success_; | 49 return success_; |
49 } | 50 } |
50 | 51 |
51 void GetBudgetDetailsComplete( | 52 void GetBudgetDetailsComplete( |
52 base::Closure run_loop_closure, | 53 base::Closure run_loop_closure, |
53 bool success, | 54 mojo::Array<blink::mojom::BudgetStatePtr> predictions) { |
54 const BudgetDatabase::BudgetPrediction& prediction) { | 55 // TODO(harkness) Check the status enum when it's added. |
55 success_ = success; | 56 success_ = true; |
56 // Convert BudgetPrediction to a vector for random access to check values. | 57 prediction_.Swap(&predictions); |
57 prediction_.assign(prediction.begin(), prediction.end()); | |
58 run_loop_closure.Run(); | 58 run_loop_closure.Run(); |
59 } | 59 } |
60 | 60 |
61 // Get the full set of budget predictions for the origin. | 61 // Get the full set of budget predictions for the origin. |
62 void GetBudgetDetails() { | 62 void GetBudgetDetails() { |
63 base::RunLoop run_loop; | 63 base::RunLoop run_loop; |
64 db_.GetBudgetDetails( | 64 db_.GetBudgetDetails( |
65 GURL(kTestOrigin), | 65 GURL(kTestOrigin), |
66 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, | 66 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, |
67 base::Unretained(this), run_loop.QuitClosure())); | 67 base::Unretained(this), run_loop.QuitClosure())); |
68 run_loop.Run(); | 68 run_loop.Run(); |
69 } | 69 } |
70 | 70 |
71 Profile* profile() { return &profile_; } | 71 Profile* profile() { return &profile_; } |
72 | 72 |
73 // Setup a test clock so that the tests can control time. | 73 // Setup a test clock so that the tests can control time. |
74 base::SimpleTestClock* SetClockForTesting() { | 74 base::SimpleTestClock* SetClockForTesting() { |
75 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 75 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
76 db_.SetClockForTesting(base::WrapUnique(clock)); | 76 db_.SetClockForTesting(base::WrapUnique(clock)); |
77 return clock; | 77 return clock; |
78 } | 78 } |
79 | 79 |
80 void SetSiteEngagementScore(const GURL& url, double score) { | 80 void SetSiteEngagementScore(const GURL& url, double score) { |
81 SiteEngagementService* service = SiteEngagementService::Get(&profile_); | 81 SiteEngagementService* service = SiteEngagementService::Get(&profile_); |
82 service->ResetScoreForURL(url, score); | 82 service->ResetScoreForURL(url, score); |
83 } | 83 } |
84 | 84 |
85 protected: | 85 protected: |
86 bool success_; | 86 bool success_; |
87 std::vector<BudgetDatabase::BudgetStatus> prediction_; | 87 mojo::Array<blink::mojom::BudgetStatePtr> prediction_; |
88 | 88 |
89 private: | 89 private: |
90 content::TestBrowserThreadBundle thread_bundle_; | 90 content::TestBrowserThreadBundle thread_bundle_; |
91 std::unique_ptr<budget_service::Budget> budget_; | 91 std::unique_ptr<budget_service::Budget> budget_; |
92 TestingProfile profile_; | 92 TestingProfile profile_; |
93 BudgetDatabase db_; | 93 BudgetDatabase db_; |
94 }; | 94 }; |
95 | 95 |
96 TEST_F(BudgetDatabaseTest, GetBudgetNoBudgetOrSES) { | |
97 const GURL origin(kTestOrigin); | |
98 GetBudgetDetails(); | |
99 ASSERT_TRUE(success_); | |
100 ASSERT_EQ(2U, prediction_.size()); | |
101 EXPECT_EQ(0, prediction_[0]->budget_at); | |
102 } | |
103 | |
96 TEST_F(BudgetDatabaseTest, AddEngagementBudgetTest) { | 104 TEST_F(BudgetDatabaseTest, AddEngagementBudgetTest) { |
97 const GURL origin(kTestOrigin); | 105 const GURL origin(kTestOrigin); |
98 base::SimpleTestClock* clock = SetClockForTesting(); | 106 base::SimpleTestClock* clock = SetClockForTesting(); |
99 base::Time expiration_time = | 107 base::Time expiration_time = |
100 clock->Now() + base::TimeDelta::FromHours(kDefaultExpirationInHours); | 108 clock->Now() + base::TimeDelta::FromHours(kDefaultExpirationInHours); |
101 | 109 |
102 // Set the default site engagement. | 110 // Set the default site engagement. |
103 SetSiteEngagementScore(origin, kDefaultEngagement); | 111 SetSiteEngagementScore(origin, kDefaultEngagement); |
104 | 112 |
105 // The budget should include a full share of the engagement. | 113 // The budget should include a full share of the engagement. |
106 GetBudgetDetails(); | 114 GetBudgetDetails(); |
107 ASSERT_TRUE(success_); | 115 ASSERT_TRUE(success_); |
108 ASSERT_EQ(2U, prediction_.size()); | 116 ASSERT_EQ(2U, prediction_.size()); |
109 ASSERT_EQ(kDefaultEngagement, prediction_[0].budget_at); | 117 ASSERT_EQ(kDefaultEngagement, prediction_[0]->budget_at); |
110 ASSERT_EQ(0, prediction_[1].budget_at); | 118 ASSERT_EQ(0, prediction_[1]->budget_at); |
111 ASSERT_EQ(expiration_time, prediction_[1].time); | 119 ASSERT_EQ(expiration_time.ToDoubleT(), prediction_[1]->time); |
112 | 120 |
113 // Advance time 1 day and add more engagement budget. | 121 // Advance time 1 day and add more engagement budget. |
114 clock->Advance(base::TimeDelta::FromDays(1)); | 122 clock->Advance(base::TimeDelta::FromDays(1)); |
115 GetBudgetDetails(); | 123 GetBudgetDetails(); |
116 | 124 |
117 // The budget should now have 1 full share plus 1/3 share. | 125 // The budget should now have 1 full share plus 1 daily budget. |
118 ASSERT_TRUE(success_); | 126 ASSERT_TRUE(success_); |
119 ASSERT_EQ(3U, prediction_.size()); | 127 ASSERT_EQ(3U, prediction_.size()); |
120 ASSERT_DOUBLE_EQ(kDefaultEngagement * 4 / 3, prediction_[0].budget_at); | 128 double daily_budget = kDefaultEngagement * 24 / kDefaultExpirationInHours; |
121 ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3, prediction_[1].budget_at); | 129 ASSERT_DOUBLE_EQ(kDefaultEngagement + daily_budget, |
122 ASSERT_EQ(expiration_time, prediction_[1].time); | 130 prediction_[0]->budget_at); |
123 ASSERT_EQ(0, prediction_[2].budget_at); | 131 ASSERT_DOUBLE_EQ(daily_budget, prediction_[1]->budget_at); |
124 ASSERT_EQ(expiration_time + base::TimeDelta::FromDays(1), | 132 ASSERT_EQ(expiration_time.ToDoubleT(), prediction_[1]->time); |
125 prediction_[2].time); | 133 ASSERT_EQ(0, prediction_[2]->budget_at); |
134 ASSERT_EQ((expiration_time + base::TimeDelta::FromDays(1)).ToDoubleT(), | |
135 prediction_[2]->time); | |
126 | 136 |
127 // Advance time by 59 minutes and check that no engagement budget is added | 137 // Advance time by 59 minutes and check that no engagement budget is added |
128 // since budget should only be added for > 1 hour increments. | 138 // since budget should only be added for > 1 hour increments. |
129 clock->Advance(base::TimeDelta::FromMinutes(59)); | 139 clock->Advance(base::TimeDelta::FromMinutes(59)); |
140 GetBudgetDetails(); | |
130 | 141 |
131 // The budget should be the same as before the attempted add. | 142 // The budget should be the same as before the attempted add. |
132 GetBudgetDetails(); | |
133 ASSERT_TRUE(success_); | 143 ASSERT_TRUE(success_); |
134 ASSERT_EQ(3U, prediction_.size()); | 144 ASSERT_EQ(3U, prediction_.size()); |
135 ASSERT_DOUBLE_EQ(kDefaultEngagement * 4 / 3, prediction_[0].budget_at); | 145 ASSERT_DOUBLE_EQ(kDefaultEngagement + daily_budget, |
146 prediction_[0]->budget_at); | |
136 } | 147 } |
137 | 148 |
138 TEST_F(BudgetDatabaseTest, SpendBudgetTest) { | 149 TEST_F(BudgetDatabaseTest, SpendBudgetTest) { |
139 const GURL origin(kTestOrigin); | 150 const GURL origin(kTestOrigin); |
140 base::SimpleTestClock* clock = SetClockForTesting(); | 151 base::SimpleTestClock* clock = SetClockForTesting(); |
141 | 152 |
142 // Set the default site engagement. | 153 // Set the default site engagement. |
143 SetSiteEngagementScore(origin, kDefaultEngagement); | 154 SetSiteEngagementScore(origin, kDefaultEngagement); |
144 | 155 |
145 // Intialize the budget with several chunks. | 156 // Intialize the budget with several chunks. |
146 GetBudgetDetails(); | 157 GetBudgetDetails(); |
147 clock->Advance(base::TimeDelta::FromDays(1)); | 158 clock->Advance(base::TimeDelta::FromDays(1)); |
148 GetBudgetDetails(); | 159 GetBudgetDetails(); |
149 clock->Advance(base::TimeDelta::FromDays(1)); | 160 clock->Advance(base::TimeDelta::FromDays(1)); |
150 GetBudgetDetails(); | 161 GetBudgetDetails(); |
151 | 162 |
152 // Spend an amount of budget less than kDefaultEngagement. | 163 // Spend an amount of budget less than kDefaultEngagement. |
153 ASSERT_TRUE(SpendBudget(origin, 1)); | 164 ASSERT_TRUE(SpendBudget(origin, 1)); |
154 GetBudgetDetails(); | 165 GetBudgetDetails(); |
155 | 166 |
156 // There should still be three chunks of budget of size kDefaultEngagement-1, | 167 // There should still be three chunks of budget of size kDefaultEngagement-1, |
157 // kDefaultEngagement, and kDefaultEngagement. | 168 // kDefaultEngagement, and kDefaultEngagement. |
158 ASSERT_EQ(4U, prediction_.size()); | 169 ASSERT_EQ(4U, prediction_.size()); |
159 ASSERT_DOUBLE_EQ(kDefaultEngagement * 5 / 3 - 1, prediction_[0].budget_at); | 170 double daily_budget = kDefaultEngagement * 24 / kDefaultExpirationInHours; |
160 ASSERT_DOUBLE_EQ(kDefaultEngagement * 2 / 3, prediction_[1].budget_at); | 171 ASSERT_DOUBLE_EQ(kDefaultEngagement + 2 * daily_budget - 1, |
161 ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3, prediction_[2].budget_at); | 172 prediction_[0]->budget_at); |
162 ASSERT_DOUBLE_EQ(0, prediction_[3].budget_at); | 173 ASSERT_DOUBLE_EQ(daily_budget * 2, prediction_[1]->budget_at); |
174 ASSERT_DOUBLE_EQ(daily_budget, prediction_[2]->budget_at); | |
175 ASSERT_DOUBLE_EQ(0, prediction_[3]->budget_at); | |
163 | 176 |
164 // Now spend enough that it will use up the rest of the first chunk and all of | 177 // Now spend enough that it will use up the rest of the first chunk and all of |
165 // the second chunk, but not all of the third chunk. | 178 // the second chunk, but not all of the third chunk. |
166 ASSERT_TRUE(SpendBudget(origin, kDefaultEngagement * 4 / 3)); | 179 ASSERT_TRUE(SpendBudget(origin, kDefaultEngagement + daily_budget)); |
167 GetBudgetDetails(); | 180 GetBudgetDetails(); |
168 ASSERT_EQ(2U, prediction_.size()); | 181 ASSERT_EQ(2U, prediction_.size()); |
169 ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3 - 1, | 182 ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at); |
170 prediction_.begin()->budget_at); | |
171 | 183 |
172 // Validate that the code returns false if SpendBudget tries to spend more | 184 // Validate that the code returns false if SpendBudget tries to spend more |
173 // budget than the origin has. | 185 // budget than the origin has. |
174 EXPECT_FALSE(SpendBudget(origin, kDefaultEngagement)); | 186 EXPECT_FALSE(SpendBudget(origin, kDefaultEngagement)); |
175 GetBudgetDetails(); | 187 GetBudgetDetails(); |
176 ASSERT_EQ(2U, prediction_.size()); | 188 ASSERT_EQ(2U, prediction_.size()); |
177 ASSERT_DOUBLE_EQ(kDefaultEngagement * 1 / 3 - 1, | 189 ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at); |
178 prediction_.begin()->budget_at); | |
179 | 190 |
180 // Advance time until the last remaining chunk should be expired, then query | 191 // Advance time until the last remaining chunk should be expired, then query |
181 // for the full engagement worth of budget. | 192 // for the full engagement worth of budget. |
182 clock->Advance(base::TimeDelta::FromDays(6)); | 193 clock->Advance(base::TimeDelta::FromHours(kDefaultExpirationInHours + 1)); |
183 EXPECT_TRUE(SpendBudget(origin, kDefaultEngagement)); | 194 EXPECT_TRUE(SpendBudget(origin, kDefaultEngagement)); |
184 } | 195 } |
196 | |
197 TEST_F(BudgetDatabaseTest, GetBudgetNegativeTime) { | |
Peter Beverloo
2016/09/05 14:39:03
Could you lead this test's body with a brief comme
harkness
2016/09/06 13:28:39
Done.
| |
198 const GURL origin(kTestOrigin); | |
199 base::SimpleTestClock* clock = SetClockForTesting(); | |
200 | |
201 // Set the default site engagement. | |
202 SetSiteEngagementScore(origin, kDefaultEngagement); | |
203 | |
204 // Initialize the budget with two chunks. | |
205 GetBudgetDetails(); | |
206 clock->Advance(base::TimeDelta::FromDays(1)); | |
207 GetBudgetDetails(); | |
208 | |
209 // Save off the budget total. | |
210 ASSERT_EQ(3U, prediction_.size()); | |
211 double budget = prediction_[0]->budget_at; | |
212 | |
213 // Move the clock backwards in time to before the budget awards. | |
214 clock->SetNow(clock->Now() - base::TimeDelta::FromDays(5)); | |
215 | |
216 // Make sure the budget is the same. | |
217 GetBudgetDetails(); | |
218 ASSERT_EQ(3U, prediction_.size()); | |
219 ASSERT_EQ(budget, prediction_[0]->budget_at); | |
220 | |
221 // Now move the clock back to the original time and check that no extra budget | |
222 // is awarded. | |
223 clock->SetNow(clock->Now() + base::TimeDelta::FromDays(5)); | |
224 GetBudgetDetails(); | |
225 ASSERT_EQ(3U, prediction_.size()); | |
226 ASSERT_EQ(budget, prediction_[0]->budget_at); | |
227 } | |
OLD | NEW |