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" | 22 #include "url/gurl.h" |
23 #include "url/origin.h" | 23 #include "url/origin.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const double kDefaultExpirationInHours = 240; | 27 const double kDefaultExpirationInHours = 96; |
28 const double kDefaultEngagement = 30.0; | 28 const double kDefaultEngagement = 30.0; |
29 | 29 |
30 const char kTestOrigin[] = "https://example.com"; | 30 const char kTestOrigin[] = "https://example.com"; |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 class BudgetDatabaseTest : public ::testing::Test { | 34 class BudgetDatabaseTest : public ::testing::Test { |
35 public: | 35 public: |
36 BudgetDatabaseTest() | 36 BudgetDatabaseTest() |
37 : success_(false), | 37 : success_(false), |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 ASSERT_EQ(3U, prediction_.size()); | 236 ASSERT_EQ(3U, prediction_.size()); |
237 ASSERT_EQ(budget, prediction_[0]->budget_at); | 237 ASSERT_EQ(budget, prediction_[0]->budget_at); |
238 } | 238 } |
239 | 239 |
240 TEST_F(BudgetDatabaseTest, CheckBackgroundBudgetHistogram) { | 240 TEST_F(BudgetDatabaseTest, CheckBackgroundBudgetHistogram) { |
241 base::SimpleTestClock* clock = SetClockForTesting(); | 241 base::SimpleTestClock* clock = SetClockForTesting(); |
242 | 242 |
243 // Set the default site engagement. | 243 // Set the default site engagement. |
244 SetSiteEngagementScore(kDefaultEngagement); | 244 SetSiteEngagementScore(kDefaultEngagement); |
245 | 245 |
246 // Initialize the budget with some interesting chunks: 30 budget, 3 budget, | 246 // Initialize the budget with some interesting chunks: 30 budget (full |
247 // 0 budget, and then after the first two expire, another 30 budget. | 247 // engagement), 15 budget (half of the engagement), 0 budget (less than an |
| 248 // hour), and then after the first two expire, another 30 budget. |
248 GetBudgetDetails(); | 249 GetBudgetDetails(); |
249 clock->Advance(base::TimeDelta::FromDays(2)); | 250 clock->Advance(base::TimeDelta::FromHours(kDefaultExpirationInHours / 2)); |
250 GetBudgetDetails(); | 251 GetBudgetDetails(); |
251 clock->Advance(base::TimeDelta::FromMinutes(59)); | 252 clock->Advance(base::TimeDelta::FromMinutes(59)); |
252 GetBudgetDetails(); | 253 GetBudgetDetails(); |
253 clock->Advance(base::TimeDelta::FromDays(11)); | 254 clock->Advance(base::TimeDelta::FromHours(kDefaultExpirationInHours + 1)); |
254 GetBudgetDetails(); | 255 GetBudgetDetails(); |
255 | 256 |
256 // The BackgroundBudget UMA is recorded when budget is added to the origin. | 257 // The BackgroundBudget UMA is recorded when budget is added to the origin. |
257 // This can happen a maximum of once per hour so there should be two entries. | 258 // This can happen a maximum of once per hour so there should be two entries. |
258 std::vector<base::Bucket> buckets = | 259 std::vector<base::Bucket> buckets = |
259 GetHistogramTester()->GetAllSamples("PushMessaging.BackgroundBudget"); | 260 GetHistogramTester()->GetAllSamples("PushMessaging.BackgroundBudget"); |
260 ASSERT_EQ(2U, buckets.size()); | 261 ASSERT_EQ(2U, buckets.size()); |
261 // First bucket is for 30 budget, which should have 2 entries. | 262 // First bucket is for full engagement, which should have 2 entries. |
262 EXPECT_EQ(30, buckets[0].min); | 263 EXPECT_EQ(kDefaultEngagement, buckets[0].min); |
263 EXPECT_EQ(2, buckets[0].count); | 264 EXPECT_EQ(2, buckets[0].count); |
264 // Second bucket is for 36 budget, which should have 1 entry. | 265 // Second bucket is for 1.5 * engagement, which should have 1 entry. |
265 EXPECT_EQ(36, buckets[1].min); | 266 EXPECT_EQ(kDefaultEngagement * 1.5, buckets[1].min); |
266 EXPECT_EQ(1, buckets[1].count); | 267 EXPECT_EQ(1, buckets[1].count); |
267 } | 268 } |
268 | 269 |
269 TEST_F(BudgetDatabaseTest, CheckEngagementHistograms) { | 270 TEST_F(BudgetDatabaseTest, CheckEngagementHistograms) { |
270 base::SimpleTestClock* clock = SetClockForTesting(); | 271 base::SimpleTestClock* clock = SetClockForTesting(); |
271 | 272 |
272 // Set the engagement to twice the cost of an action. | 273 // Set the engagement to twice the cost of an action. |
273 double cost = 2; | 274 double cost = 2; |
274 double engagement = cost * 2; | 275 double engagement = cost * 2; |
275 SetSiteEngagementScore(engagement); | 276 SetSiteEngagementScore(engagement); |
(...skipping 29 matching lines...) Expand all Loading... |
305 | 306 |
306 std::vector<base::Bucket> low_budget_buckets = | 307 std::vector<base::Bucket> low_budget_buckets = |
307 GetHistogramTester()->GetAllSamples( | 308 GetHistogramTester()->GetAllSamples( |
308 "PushMessaging.SESForLowBudgetOrigin"); | 309 "PushMessaging.SESForLowBudgetOrigin"); |
309 ASSERT_EQ(2U, low_budget_buckets.size()); | 310 ASSERT_EQ(2U, low_budget_buckets.size()); |
310 EXPECT_EQ(engagement, low_budget_buckets[0].min); | 311 EXPECT_EQ(engagement, low_budget_buckets[0].min); |
311 EXPECT_EQ(1, low_budget_buckets[0].count); | 312 EXPECT_EQ(1, low_budget_buckets[0].count); |
312 EXPECT_EQ(engagement * 2, low_budget_buckets[1].min); | 313 EXPECT_EQ(engagement * 2, low_budget_buckets[1].min); |
313 EXPECT_EQ(1, low_budget_buckets[1].count); | 314 EXPECT_EQ(1, low_budget_buckets[1].count); |
314 } | 315 } |
OLD | NEW |