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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
11 #include "base/test/simple_test_clock.h" | 11 #include "base/test/simple_test_clock.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "chrome/browser/budget_service/budget.pb.h" | 13 #include "chrome/browser/budget_service/budget.pb.h" |
14 #include "chrome/browser/engagement/site_engagement_service.h" | 14 #include "chrome/browser/engagement/site_engagement_service.h" |
15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
16 #include "components/leveldb_proto/proto_database.h" | 16 #include "components/leveldb_proto/proto_database.h" |
17 #include "components/leveldb_proto/proto_database_impl.h" | 17 #include "components/leveldb_proto/proto_database_impl.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "mojo/public/cpp/bindings/array.h" | 20 #include "mojo/public/cpp/bindings/array.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" |
| 23 #include "url/origin.h" |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 const double kDefaultExpirationInHours = 240; | 27 const double kDefaultExpirationInHours = 240; |
26 const double kDefaultEngagement = 30.0; | 28 const double kDefaultEngagement = 30.0; |
27 | 29 |
28 const char kTestOrigin[] = "https://example.com"; | 30 const char kTestOrigin[] = "https://example.com"; |
29 | 31 |
30 } // namespace | 32 } // namespace |
31 | 33 |
32 class BudgetDatabaseTest : public ::testing::Test { | 34 class BudgetDatabaseTest : public ::testing::Test { |
33 public: | 35 public: |
34 BudgetDatabaseTest() | 36 BudgetDatabaseTest() |
35 : success_(false), | 37 : success_(false), |
36 db_(&profile_, | 38 db_(&profile_, |
37 profile_.GetPath().Append(FILE_PATH_LITERAL("BudgetDatabase")), | 39 profile_.GetPath().Append(FILE_PATH_LITERAL("BudgetDatabase")), |
38 base::ThreadTaskRunnerHandle::Get()) {} | 40 base::ThreadTaskRunnerHandle::Get()), |
| 41 origin_(url::Origin(GURL(kTestOrigin))) {} |
39 | 42 |
40 void WriteBudgetComplete(base::Closure run_loop_closure, bool success) { | 43 void WriteBudgetComplete(base::Closure run_loop_closure, bool success) { |
41 success_ = success; | 44 success_ = success; |
42 run_loop_closure.Run(); | 45 run_loop_closure.Run(); |
43 } | 46 } |
44 | 47 |
45 // Spend budget for the origin. | 48 // Spend budget for the origin. |
46 bool SpendBudget(const GURL& origin, double amount) { | 49 bool SpendBudget(double amount) { |
47 base::RunLoop run_loop; | 50 base::RunLoop run_loop; |
48 db_.SpendBudget(origin, amount, | 51 db_.SpendBudget(origin(), amount, |
49 base::Bind(&BudgetDatabaseTest::WriteBudgetComplete, | 52 base::Bind(&BudgetDatabaseTest::WriteBudgetComplete, |
50 base::Unretained(this), run_loop.QuitClosure())); | 53 base::Unretained(this), run_loop.QuitClosure())); |
51 run_loop.Run(); | 54 run_loop.Run(); |
52 return success_; | 55 return success_; |
53 } | 56 } |
54 | 57 |
55 void GetBudgetDetailsComplete( | 58 void GetBudgetDetailsComplete( |
56 base::Closure run_loop_closure, | 59 base::Closure run_loop_closure, |
57 blink::mojom::BudgetServiceErrorType error, | 60 blink::mojom::BudgetServiceErrorType error, |
58 mojo::Array<blink::mojom::BudgetStatePtr> predictions) { | 61 mojo::Array<blink::mojom::BudgetStatePtr> predictions) { |
59 success_ = (error == blink::mojom::BudgetServiceErrorType::NONE); | 62 success_ = (error == blink::mojom::BudgetServiceErrorType::NONE); |
60 prediction_.Swap(&predictions); | 63 prediction_.Swap(&predictions); |
61 run_loop_closure.Run(); | 64 run_loop_closure.Run(); |
62 } | 65 } |
63 | 66 |
64 // Get the full set of budget predictions for the origin. | 67 // Get the full set of budget predictions for the origin. |
65 void GetBudgetDetails() { | 68 void GetBudgetDetails() { |
66 base::RunLoop run_loop; | 69 base::RunLoop run_loop; |
67 db_.GetBudgetDetails( | 70 db_.GetBudgetDetails( |
68 GURL(kTestOrigin), | 71 origin(), base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, |
69 base::Bind(&BudgetDatabaseTest::GetBudgetDetailsComplete, | 72 base::Unretained(this), run_loop.QuitClosure())); |
70 base::Unretained(this), run_loop.QuitClosure())); | |
71 run_loop.Run(); | 73 run_loop.Run(); |
72 } | 74 } |
73 | 75 |
74 Profile* profile() { return &profile_; } | 76 Profile* profile() { return &profile_; } |
| 77 const url::Origin& origin() const { return origin_; } |
75 | 78 |
76 // Setup a test clock so that the tests can control time. | 79 // Setup a test clock so that the tests can control time. |
77 base::SimpleTestClock* SetClockForTesting() { | 80 base::SimpleTestClock* SetClockForTesting() { |
78 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 81 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
79 db_.SetClockForTesting(base::WrapUnique(clock)); | 82 db_.SetClockForTesting(base::WrapUnique(clock)); |
80 return clock; | 83 return clock; |
81 } | 84 } |
82 | 85 |
83 void SetSiteEngagementScore(const GURL& url, double score) { | 86 void SetSiteEngagementScore(double score) { |
84 SiteEngagementService* service = SiteEngagementService::Get(&profile_); | 87 SiteEngagementService* service = SiteEngagementService::Get(&profile_); |
85 service->ResetScoreForURL(url, score); | 88 service->ResetScoreForURL(GURL(kTestOrigin), score); |
86 } | 89 } |
87 | 90 |
88 protected: | 91 protected: |
89 base::HistogramTester* GetHistogramTester() { return &histogram_tester_; } | 92 base::HistogramTester* GetHistogramTester() { return &histogram_tester_; } |
90 bool success_; | 93 bool success_; |
91 mojo::Array<blink::mojom::BudgetStatePtr> prediction_; | 94 mojo::Array<blink::mojom::BudgetStatePtr> prediction_; |
92 | 95 |
93 private: | 96 private: |
94 content::TestBrowserThreadBundle thread_bundle_; | 97 content::TestBrowserThreadBundle thread_bundle_; |
95 std::unique_ptr<budget_service::Budget> budget_; | 98 std::unique_ptr<budget_service::Budget> budget_; |
96 TestingProfile profile_; | 99 TestingProfile profile_; |
97 BudgetDatabase db_; | 100 BudgetDatabase db_; |
98 base::HistogramTester histogram_tester_; | 101 base::HistogramTester histogram_tester_; |
| 102 const url::Origin origin_; |
99 }; | 103 }; |
100 | 104 |
101 TEST_F(BudgetDatabaseTest, GetBudgetNoBudgetOrSES) { | 105 TEST_F(BudgetDatabaseTest, GetBudgetNoBudgetOrSES) { |
102 const GURL origin(kTestOrigin); | |
103 GetBudgetDetails(); | 106 GetBudgetDetails(); |
104 ASSERT_TRUE(success_); | 107 ASSERT_TRUE(success_); |
105 ASSERT_EQ(2U, prediction_.size()); | 108 ASSERT_EQ(2U, prediction_.size()); |
106 EXPECT_EQ(0, prediction_[0]->budget_at); | 109 EXPECT_EQ(0, prediction_[0]->budget_at); |
107 } | 110 } |
108 | 111 |
109 TEST_F(BudgetDatabaseTest, AddEngagementBudgetTest) { | 112 TEST_F(BudgetDatabaseTest, AddEngagementBudgetTest) { |
110 const GURL origin(kTestOrigin); | |
111 base::SimpleTestClock* clock = SetClockForTesting(); | 113 base::SimpleTestClock* clock = SetClockForTesting(); |
112 base::Time expiration_time = | 114 base::Time expiration_time = |
113 clock->Now() + base::TimeDelta::FromHours(kDefaultExpirationInHours); | 115 clock->Now() + base::TimeDelta::FromHours(kDefaultExpirationInHours); |
114 | 116 |
115 // Set the default site engagement. | 117 // Set the default site engagement. |
116 SetSiteEngagementScore(origin, kDefaultEngagement); | 118 SetSiteEngagementScore(kDefaultEngagement); |
117 | 119 |
118 // The budget should include a full share of the engagement. | 120 // The budget should include a full share of the engagement. |
119 GetBudgetDetails(); | 121 GetBudgetDetails(); |
120 ASSERT_TRUE(success_); | 122 ASSERT_TRUE(success_); |
121 ASSERT_EQ(2U, prediction_.size()); | 123 ASSERT_EQ(2U, prediction_.size()); |
122 ASSERT_EQ(kDefaultEngagement, prediction_[0]->budget_at); | 124 ASSERT_EQ(kDefaultEngagement, prediction_[0]->budget_at); |
123 ASSERT_EQ(0, prediction_[1]->budget_at); | 125 ASSERT_EQ(0, prediction_[1]->budget_at); |
124 ASSERT_EQ(expiration_time.ToDoubleT(), prediction_[1]->time); | 126 ASSERT_EQ(expiration_time.ToDoubleT(), prediction_[1]->time); |
125 | 127 |
126 // Advance time 1 day and add more engagement budget. | 128 // Advance time 1 day and add more engagement budget. |
(...skipping 18 matching lines...) Expand all Loading... |
145 GetBudgetDetails(); | 147 GetBudgetDetails(); |
146 | 148 |
147 // The budget should be the same as before the attempted add. | 149 // The budget should be the same as before the attempted add. |
148 ASSERT_TRUE(success_); | 150 ASSERT_TRUE(success_); |
149 ASSERT_EQ(3U, prediction_.size()); | 151 ASSERT_EQ(3U, prediction_.size()); |
150 ASSERT_DOUBLE_EQ(kDefaultEngagement + daily_budget, | 152 ASSERT_DOUBLE_EQ(kDefaultEngagement + daily_budget, |
151 prediction_[0]->budget_at); | 153 prediction_[0]->budget_at); |
152 } | 154 } |
153 | 155 |
154 TEST_F(BudgetDatabaseTest, SpendBudgetTest) { | 156 TEST_F(BudgetDatabaseTest, SpendBudgetTest) { |
155 const GURL origin(kTestOrigin); | |
156 base::SimpleTestClock* clock = SetClockForTesting(); | 157 base::SimpleTestClock* clock = SetClockForTesting(); |
157 | 158 |
158 // Set the default site engagement. | 159 // Set the default site engagement. |
159 SetSiteEngagementScore(origin, kDefaultEngagement); | 160 SetSiteEngagementScore(kDefaultEngagement); |
160 | 161 |
161 // Intialize the budget with several chunks. | 162 // Intialize the budget with several chunks. |
162 GetBudgetDetails(); | 163 GetBudgetDetails(); |
163 clock->Advance(base::TimeDelta::FromDays(1)); | 164 clock->Advance(base::TimeDelta::FromDays(1)); |
164 GetBudgetDetails(); | 165 GetBudgetDetails(); |
165 clock->Advance(base::TimeDelta::FromDays(1)); | 166 clock->Advance(base::TimeDelta::FromDays(1)); |
166 GetBudgetDetails(); | 167 GetBudgetDetails(); |
167 | 168 |
168 // Spend an amount of budget less than kDefaultEngagement. | 169 // Spend an amount of budget less than kDefaultEngagement. |
169 ASSERT_TRUE(SpendBudget(origin, 1)); | 170 ASSERT_TRUE(SpendBudget(1)); |
170 GetBudgetDetails(); | 171 GetBudgetDetails(); |
171 | 172 |
172 // There should still be three chunks of budget of size kDefaultEngagement-1, | 173 // There should still be three chunks of budget of size kDefaultEngagement-1, |
173 // kDefaultEngagement, and kDefaultEngagement. | 174 // kDefaultEngagement, and kDefaultEngagement. |
174 ASSERT_EQ(4U, prediction_.size()); | 175 ASSERT_EQ(4U, prediction_.size()); |
175 double daily_budget = kDefaultEngagement * 24 / kDefaultExpirationInHours; | 176 double daily_budget = kDefaultEngagement * 24 / kDefaultExpirationInHours; |
176 ASSERT_DOUBLE_EQ(kDefaultEngagement + 2 * daily_budget - 1, | 177 ASSERT_DOUBLE_EQ(kDefaultEngagement + 2 * daily_budget - 1, |
177 prediction_[0]->budget_at); | 178 prediction_[0]->budget_at); |
178 ASSERT_DOUBLE_EQ(daily_budget * 2, prediction_[1]->budget_at); | 179 ASSERT_DOUBLE_EQ(daily_budget * 2, prediction_[1]->budget_at); |
179 ASSERT_DOUBLE_EQ(daily_budget, prediction_[2]->budget_at); | 180 ASSERT_DOUBLE_EQ(daily_budget, prediction_[2]->budget_at); |
180 ASSERT_DOUBLE_EQ(0, prediction_[3]->budget_at); | 181 ASSERT_DOUBLE_EQ(0, prediction_[3]->budget_at); |
181 | 182 |
182 // Now spend enough that it will use up the rest of the first chunk and all of | 183 // Now spend enough that it will use up the rest of the first chunk and all of |
183 // the second chunk, but not all of the third chunk. | 184 // the second chunk, but not all of the third chunk. |
184 ASSERT_TRUE(SpendBudget(origin, kDefaultEngagement + daily_budget)); | 185 ASSERT_TRUE(SpendBudget(kDefaultEngagement + daily_budget)); |
185 GetBudgetDetails(); | 186 GetBudgetDetails(); |
186 ASSERT_EQ(2U, prediction_.size()); | 187 ASSERT_EQ(2U, prediction_.size()); |
187 ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at); | 188 ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at); |
188 | 189 |
189 // Validate that the code returns false if SpendBudget tries to spend more | 190 // Validate that the code returns false if SpendBudget tries to spend more |
190 // budget than the origin has. | 191 // budget than the origin has. |
191 EXPECT_FALSE(SpendBudget(origin, kDefaultEngagement)); | 192 EXPECT_FALSE(SpendBudget(kDefaultEngagement)); |
192 GetBudgetDetails(); | 193 GetBudgetDetails(); |
193 ASSERT_EQ(2U, prediction_.size()); | 194 ASSERT_EQ(2U, prediction_.size()); |
194 ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at); | 195 ASSERT_DOUBLE_EQ(daily_budget - 1, prediction_[0]->budget_at); |
195 | 196 |
196 // Advance time until the last remaining chunk should be expired, then query | 197 // Advance time until the last remaining chunk should be expired, then query |
197 // for the full engagement worth of budget. | 198 // for the full engagement worth of budget. |
198 clock->Advance(base::TimeDelta::FromHours(kDefaultExpirationInHours + 1)); | 199 clock->Advance(base::TimeDelta::FromHours(kDefaultExpirationInHours + 1)); |
199 EXPECT_TRUE(SpendBudget(origin, kDefaultEngagement)); | 200 EXPECT_TRUE(SpendBudget(kDefaultEngagement)); |
200 } | 201 } |
201 | 202 |
202 // There are times when a device's clock could move backwards in time, either | 203 // There are times when a device's clock could move backwards in time, either |
203 // due to hardware issues or user actions. Test here to make sure that even if | 204 // due to hardware issues or user actions. Test here to make sure that even if |
204 // time goes backwards and then forwards again, the origin isn't granted extra | 205 // time goes backwards and then forwards again, the origin isn't granted extra |
205 // budget. | 206 // budget. |
206 TEST_F(BudgetDatabaseTest, GetBudgetNegativeTime) { | 207 TEST_F(BudgetDatabaseTest, GetBudgetNegativeTime) { |
207 const GURL origin(kTestOrigin); | |
208 base::SimpleTestClock* clock = SetClockForTesting(); | 208 base::SimpleTestClock* clock = SetClockForTesting(); |
209 | 209 |
210 // Set the default site engagement. | 210 // Set the default site engagement. |
211 SetSiteEngagementScore(origin, kDefaultEngagement); | 211 SetSiteEngagementScore(kDefaultEngagement); |
212 | 212 |
213 // Initialize the budget with two chunks. | 213 // Initialize the budget with two chunks. |
214 GetBudgetDetails(); | 214 GetBudgetDetails(); |
215 clock->Advance(base::TimeDelta::FromDays(1)); | 215 clock->Advance(base::TimeDelta::FromDays(1)); |
216 GetBudgetDetails(); | 216 GetBudgetDetails(); |
217 | 217 |
218 // Save off the budget total. | 218 // Save off the budget total. |
219 ASSERT_EQ(3U, prediction_.size()); | 219 ASSERT_EQ(3U, prediction_.size()); |
220 double budget = prediction_[0]->budget_at; | 220 double budget = prediction_[0]->budget_at; |
221 | 221 |
222 // Move the clock backwards in time to before the budget awards. | 222 // Move the clock backwards in time to before the budget awards. |
223 clock->SetNow(clock->Now() - base::TimeDelta::FromDays(5)); | 223 clock->SetNow(clock->Now() - base::TimeDelta::FromDays(5)); |
224 | 224 |
225 // Make sure the budget is the same. | 225 // Make sure the budget is the same. |
226 GetBudgetDetails(); | 226 GetBudgetDetails(); |
227 ASSERT_EQ(3U, prediction_.size()); | 227 ASSERT_EQ(3U, prediction_.size()); |
228 ASSERT_EQ(budget, prediction_[0]->budget_at); | 228 ASSERT_EQ(budget, prediction_[0]->budget_at); |
229 | 229 |
230 // Now move the clock back to the original time and check that no extra budget | 230 // Now move the clock back to the original time and check that no extra budget |
231 // is awarded. | 231 // is awarded. |
232 clock->SetNow(clock->Now() + base::TimeDelta::FromDays(5)); | 232 clock->SetNow(clock->Now() + base::TimeDelta::FromDays(5)); |
233 GetBudgetDetails(); | 233 GetBudgetDetails(); |
234 ASSERT_EQ(3U, prediction_.size()); | 234 ASSERT_EQ(3U, prediction_.size()); |
235 ASSERT_EQ(budget, prediction_[0]->budget_at); | 235 ASSERT_EQ(budget, prediction_[0]->budget_at); |
236 } | 236 } |
237 | 237 |
238 TEST_F(BudgetDatabaseTest, CheckBackgroundBudgetHistogram) { | 238 TEST_F(BudgetDatabaseTest, CheckBackgroundBudgetHistogram) { |
239 const GURL origin(kTestOrigin); | |
240 base::SimpleTestClock* clock = SetClockForTesting(); | 239 base::SimpleTestClock* clock = SetClockForTesting(); |
241 | 240 |
242 // Set the default site engagement. | 241 // Set the default site engagement. |
243 SetSiteEngagementScore(origin, kDefaultEngagement); | 242 SetSiteEngagementScore(kDefaultEngagement); |
244 | 243 |
245 // Initialize the budget with some interesting chunks: 30 budget, 3 budget, | 244 // Initialize the budget with some interesting chunks: 30 budget, 3 budget, |
246 // 0 budget, and then after the first two expire, another 30 budget. | 245 // 0 budget, and then after the first two expire, another 30 budget. |
247 GetBudgetDetails(); | 246 GetBudgetDetails(); |
248 clock->Advance(base::TimeDelta::FromDays(2)); | 247 clock->Advance(base::TimeDelta::FromDays(2)); |
249 GetBudgetDetails(); | 248 GetBudgetDetails(); |
250 clock->Advance(base::TimeDelta::FromMinutes(59)); | 249 clock->Advance(base::TimeDelta::FromMinutes(59)); |
251 GetBudgetDetails(); | 250 GetBudgetDetails(); |
252 clock->Advance(base::TimeDelta::FromDays(11)); | 251 clock->Advance(base::TimeDelta::FromDays(11)); |
253 GetBudgetDetails(); | 252 GetBudgetDetails(); |
254 | 253 |
255 // The BackgroundBudget UMA is recorded when budget is added to the origin. | 254 // The BackgroundBudget UMA is recorded when budget is added to the origin. |
256 // This can happen a maximum of once per hour so there should be two entries. | 255 // This can happen a maximum of once per hour so there should be two entries. |
257 std::vector<base::Bucket> buckets = | 256 std::vector<base::Bucket> buckets = |
258 GetHistogramTester()->GetAllSamples("PushMessaging.BackgroundBudget"); | 257 GetHistogramTester()->GetAllSamples("PushMessaging.BackgroundBudget"); |
259 ASSERT_EQ(2U, buckets.size()); | 258 ASSERT_EQ(2U, buckets.size()); |
260 // First bucket is for 30 budget, which should have 2 entries. | 259 // First bucket is for 30 budget, which should have 2 entries. |
261 EXPECT_EQ(30, buckets[0].min); | 260 EXPECT_EQ(30, buckets[0].min); |
262 EXPECT_EQ(2, buckets[0].count); | 261 EXPECT_EQ(2, buckets[0].count); |
263 // Second bucket is for 36 budget, which should have 1 entry. | 262 // Second bucket is for 36 budget, which should have 1 entry. |
264 EXPECT_EQ(36, buckets[1].min); | 263 EXPECT_EQ(36, buckets[1].min); |
265 EXPECT_EQ(1, buckets[1].count); | 264 EXPECT_EQ(1, buckets[1].count); |
266 } | 265 } |
267 | 266 |
268 TEST_F(BudgetDatabaseTest, CheckEngagementHistograms) { | 267 TEST_F(BudgetDatabaseTest, CheckEngagementHistograms) { |
269 const GURL origin(kTestOrigin); | |
270 base::SimpleTestClock* clock = SetClockForTesting(); | 268 base::SimpleTestClock* clock = SetClockForTesting(); |
271 | 269 |
272 // Set the engagement to twice the cost of an action. | 270 // Set the engagement to twice the cost of an action. |
273 double cost = 2; | 271 double cost = 2; |
274 double engagement = cost * 2; | 272 double engagement = cost * 2; |
275 SetSiteEngagementScore(origin, engagement); | 273 SetSiteEngagementScore(engagement); |
276 | 274 |
277 // Get the budget, which will award a chunk of budget equal to engagement. | 275 // Get the budget, which will award a chunk of budget equal to engagement. |
278 GetBudgetDetails(); | 276 GetBudgetDetails(); |
279 | 277 |
280 // Now spend the budget to trigger the UMA recording the SES score. The first | 278 // Now spend the budget to trigger the UMA recording the SES score. The first |
281 // call shouldn't write any UMA. The second should write a lowSES entry, and | 279 // call shouldn't write any UMA. The second should write a lowSES entry, and |
282 // the third should write a noSES entry. | 280 // the third should write a noSES entry. |
283 ASSERT_TRUE(SpendBudget(origin, cost)); | 281 ASSERT_TRUE(SpendBudget(cost)); |
284 ASSERT_TRUE(SpendBudget(origin, cost)); | 282 ASSERT_TRUE(SpendBudget(cost)); |
285 ASSERT_FALSE(SpendBudget(origin, cost)); | 283 ASSERT_FALSE(SpendBudget(cost)); |
286 | 284 |
287 // Advance the clock by 12 days (to guarantee a full new engagement grant) | 285 // Advance the clock by 12 days (to guarantee a full new engagement grant) |
288 // then change the SES score to get a different UMA entry, then spend the | 286 // then change the SES score to get a different UMA entry, then spend the |
289 // budget again. | 287 // budget again. |
290 clock->Advance(base::TimeDelta::FromDays(12)); | 288 clock->Advance(base::TimeDelta::FromDays(12)); |
291 GetBudgetDetails(); | 289 GetBudgetDetails(); |
292 SetSiteEngagementScore(origin, engagement * 2); | 290 SetSiteEngagementScore(engagement * 2); |
293 ASSERT_TRUE(SpendBudget(origin, cost)); | 291 ASSERT_TRUE(SpendBudget(cost)); |
294 ASSERT_TRUE(SpendBudget(origin, cost)); | 292 ASSERT_TRUE(SpendBudget(cost)); |
295 ASSERT_FALSE(SpendBudget(origin, cost)); | 293 ASSERT_FALSE(SpendBudget(cost)); |
296 | 294 |
297 // Now check the UMA. Both UMA should have 2 buckets with 1 entry each. | 295 // Now check the UMA. Both UMA should have 2 buckets with 1 entry each. |
298 std::vector<base::Bucket> no_budget_buckets = | 296 std::vector<base::Bucket> no_budget_buckets = |
299 GetHistogramTester()->GetAllSamples("PushMessaging.SESForNoBudgetOrigin"); | 297 GetHistogramTester()->GetAllSamples("PushMessaging.SESForNoBudgetOrigin"); |
300 ASSERT_EQ(2U, no_budget_buckets.size()); | 298 ASSERT_EQ(2U, no_budget_buckets.size()); |
301 EXPECT_EQ(engagement, no_budget_buckets[0].min); | 299 EXPECT_EQ(engagement, no_budget_buckets[0].min); |
302 EXPECT_EQ(1, no_budget_buckets[0].count); | 300 EXPECT_EQ(1, no_budget_buckets[0].count); |
303 EXPECT_EQ(engagement * 2, no_budget_buckets[1].min); | 301 EXPECT_EQ(engagement * 2, no_budget_buckets[1].min); |
304 EXPECT_EQ(1, no_budget_buckets[1].count); | 302 EXPECT_EQ(1, no_budget_buckets[1].count); |
305 | 303 |
306 std::vector<base::Bucket> low_budget_buckets = | 304 std::vector<base::Bucket> low_budget_buckets = |
307 GetHistogramTester()->GetAllSamples( | 305 GetHistogramTester()->GetAllSamples( |
308 "PushMessaging.SESForLowBudgetOrigin"); | 306 "PushMessaging.SESForLowBudgetOrigin"); |
309 ASSERT_EQ(2U, low_budget_buckets.size()); | 307 ASSERT_EQ(2U, low_budget_buckets.size()); |
310 EXPECT_EQ(engagement, low_budget_buckets[0].min); | 308 EXPECT_EQ(engagement, low_budget_buckets[0].min); |
311 EXPECT_EQ(1, low_budget_buckets[0].count); | 309 EXPECT_EQ(1, low_budget_buckets[0].count); |
312 EXPECT_EQ(engagement * 2, low_budget_buckets[1].min); | 310 EXPECT_EQ(engagement * 2, low_budget_buckets[1].min); |
313 EXPECT_EQ(1, low_budget_buckets[1].count); | 311 EXPECT_EQ(1, low_budget_buckets[1].count); |
314 } | 312 } |
OLD | NEW |