Chromium Code Reviews| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/test/simple_test_clock.h" | 9 #include "base/test/simple_test_clock.h" |
| 10 #include "chrome/browser/engagement/site_engagement_service.h" | 10 #include "chrome/browser/engagement/site_engagement_service.h" |
| 11 #include "chrome/browser/push_messaging/background_budget_service.h" | 11 #include "chrome/browser/push_messaging/background_budget_service.h" |
| 12 #include "chrome/browser/push_messaging/background_budget_service_factory.h" | 12 #include "chrome/browser/push_messaging/background_budget_service_factory.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "components/prefs/pref_service.h" | 15 #include "components/prefs/pref_service.h" |
| 16 #include "components/prefs/scoped_user_pref_update.h" | 16 #include "components/prefs/scoped_user_pref_update.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const char kTestOrigin[] = "https://example.com"; | 22 const char kTestOrigin[] = "https://example.com"; |
| 23 const double kTestBudget = 10.0; | 23 const double kTestBudget = 10.0; |
| 24 const double kTestSES = 24.0; | 24 const double kTestSES = 48.0; |
| 25 const double kLowSES = 1.0; | 25 const double kLowSES = 1.0; |
| 26 const double kMaxSES = 100.0; | 26 const double kMaxSES = 100.0; |
| 27 // Mirrors definition in BackgroundBudgetService, this is 10 days of seconds. | 27 // Mirrors definition in BackgroundBudgetService, this is 10 days of seconds. |
| 28 const double kSecondsToAccumulate = 864000.0; | 28 const double kSecondsToAccumulate = 864000.0; |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 class BackgroundBudgetServiceTest : public testing::Test { | 32 class BackgroundBudgetServiceTest : public testing::Test { |
| 33 public: | 33 public: |
| 34 BackgroundBudgetServiceTest() {} | 34 BackgroundBudgetServiceTest() {} |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 double budget = 0.0; | 177 double budget = 0.0; |
| 178 | 178 |
| 179 // Measure over 200 hours. In each hour a message is received, and for 1 in | 179 // Measure over 200 hours. In each hour a message is received, and for 1 in |
| 180 // 10, budget is consumed. | 180 // 10, budget is consumed. |
| 181 for (int i = 0; i < 200; i++) { | 181 for (int i = 0; i < 200; i++) { |
| 182 // Query for the budget after 1 hour has passed. | 182 // Query for the budget after 1 hour has passed. |
| 183 clock->Advance(base::TimeDelta::FromHours(1)); | 183 clock->Advance(base::TimeDelta::FromHours(1)); |
| 184 budget = service->GetBudget(origin); | 184 budget = service->GetBudget(origin); |
| 185 | 185 |
| 186 if (i % 10 == 0) { | 186 if (i % 10 == 0) { |
| 187 service->StoreBudget(origin, budget - 1.0); | 187 double newBudget = |
|
Peter Beverloo
2016/05/17 11:04:34
nit: new_budget
harkness
2016/05/17 13:08:16
actually, refactored a bit so renamed the variable
| |
| 188 budget - BackgroundBudgetService::GetBackgroundProcessingCost(); | |
| 189 service->StoreBudget(origin, newBudget); | |
| 188 } | 190 } |
| 189 } | 191 } |
| 190 | 192 |
| 191 // With a SES of 24.0, the origin will get a budget of 2.4 per day, but the | 193 // With a SES of 48.0, the origin will get a budget of 2.4 per day, but the |
| 192 // old budget will also decay. At the end, we expect the budget to be lower | 194 // old budget will also decay. At the end, we expect the budget to be lower |
| 193 // than the starting budget. | 195 // than the starting budget. |
| 194 EXPECT_GT(budget, 0.0); | 196 EXPECT_GT(budget, 0.0); |
| 195 EXPECT_LT(budget, kTestBudget); | 197 EXPECT_LT(budget, kTestBudget); |
| 196 } | 198 } |
| 197 | 199 |
| 198 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { | 200 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { |
| 199 const GURL origin(kTestOrigin); | 201 const GURL origin(kTestOrigin); |
| 200 | 202 |
| 201 // Set a starting SES for the url. | 203 // Set a starting SES for the url. |
| 202 SetSiteEngagementScore(origin, kTestSES); | 204 SetSiteEngagementScore(origin, kTestSES); |
| 203 | 205 |
| 204 // Set a badly formatted budget in the user preferences. | 206 // Set a badly formatted budget in the user preferences. |
| 205 DictionaryPrefUpdate update(profile()->GetPrefs(), | 207 DictionaryPrefUpdate update(profile()->GetPrefs(), |
| 206 prefs::kBackgroundBudgetMap); | 208 prefs::kBackgroundBudgetMap); |
| 207 base::DictionaryValue* update_map = update.Get(); | 209 base::DictionaryValue* update_map = update.Get(); |
| 208 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); | 210 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); |
| 209 | 211 |
| 210 // Get the budget, expect that it will return SES. | 212 // Get the budget, expect that it will return SES. |
| 211 double budget = GetService()->GetBudget(origin); | 213 double budget = GetService()->GetBudget(origin); |
| 212 | 214 |
| 213 EXPECT_DOUBLE_EQ(budget, kTestSES); | 215 EXPECT_DOUBLE_EQ(budget, kTestSES); |
| 214 } | 216 } |
| OLD | NEW |