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/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
11 #include "chrome/browser/budget_service/background_budget_service.h" | 11 #include "chrome/browser/budget_service/budget_manager.h" |
12 #include "chrome/browser/budget_service/background_budget_service_factory.h" | 12 #include "chrome/browser/budget_service/budget_manager_factory.h" |
13 #include "chrome/browser/engagement/site_engagement_service.h" | 13 #include "chrome/browser/engagement/site_engagement_service.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
16 #include "components/prefs/pref_service.h" | 16 #include "components/prefs/pref_service.h" |
17 #include "components/prefs/scoped_user_pref_update.h" | 17 #include "components/prefs/scoped_user_pref_update.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const char kTestOrigin[] = "https://example.com"; | 23 const char kTestOrigin[] = "https://example.com"; |
24 const double kTestBudget = 10.0; | 24 const double kTestBudget = 10.0; |
25 const double kTestSES = 48.0; | 25 const double kTestSES = 48.0; |
26 const double kLowSES = 1.0; | 26 const double kLowSES = 1.0; |
27 const double kMaxSES = 100.0; | 27 const double kMaxSES = 100.0; |
28 // Mirrors definition in BackgroundBudgetService, this is 10 days of seconds. | 28 // Mirrors definition in BudgetManager, this is 10 days of seconds. |
29 const double kSecondsToAccumulate = 864000.0; | 29 const double kSecondsToAccumulate = 864000.0; |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 class BackgroundBudgetServiceTest : public testing::Test { | 33 class BudgetManagerTest : public testing::Test { |
34 public: | 34 public: |
35 BackgroundBudgetServiceTest() : budget_(0.0) {} | 35 BudgetManagerTest() : budget_(0.0) {} |
36 ~BackgroundBudgetServiceTest() override {} | 36 ~BudgetManagerTest() override {} |
37 | 37 |
38 BackgroundBudgetService* GetService() { | 38 BudgetManager* GetService() { |
Peter Beverloo
2016/08/12 12:39:46
nit: GetManager()
harkness
2016/08/15 08:05:41
Done.
| |
39 return BackgroundBudgetServiceFactory::GetForProfile(&profile_); | 39 return BudgetManagerFactory::GetForProfile(&profile_); |
40 } | 40 } |
41 | 41 |
42 void SetSiteEngagementScore(const GURL& url, double score) { | 42 void SetSiteEngagementScore(const GURL& url, double score) { |
43 SiteEngagementService* service = SiteEngagementService::Get(&profile_); | 43 SiteEngagementService* service = SiteEngagementService::Get(&profile_); |
44 service->ResetScoreForURL(url, score); | 44 service->ResetScoreForURL(url, score); |
45 } | 45 } |
46 | 46 |
47 Profile* profile() { return &profile_; } | 47 Profile* profile() { return &profile_; } |
48 | 48 |
49 base::SimpleTestClock* SetClockForTesting() { | 49 base::SimpleTestClock* SetClockForTesting() { |
50 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 50 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
51 BackgroundBudgetServiceFactory::GetForProfile(&profile_) | 51 BudgetManagerFactory::GetForProfile(&profile_)->SetClockForTesting( |
52 ->SetClockForTesting(base::WrapUnique(clock)); | 52 base::WrapUnique(clock)); |
53 return clock; | 53 return clock; |
54 } | 54 } |
55 | 55 |
56 double GetBudget() { | 56 double GetBudget() { |
57 const GURL origin(kTestOrigin); | 57 const GURL origin(kTestOrigin); |
58 base::RunLoop run_loop; | 58 base::RunLoop run_loop; |
59 GetService()->GetBudget( | 59 GetService()->GetBudget( |
60 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | 60 origin, base::Bind(&BudgetManagerTest::GotBudget, |
61 base::Unretained(this), run_loop.QuitClosure())); | 61 base::Unretained(this), run_loop.QuitClosure())); |
62 run_loop.Run(); | 62 run_loop.Run(); |
63 return budget_; | 63 return budget_; |
64 } | 64 } |
65 | 65 |
66 void GotBudget(base::Closure run_loop_closure, double budget) { | 66 void GotBudget(base::Closure run_loop_closure, double budget) { |
67 budget_ = budget; | 67 budget_ = budget; |
68 run_loop_closure.Run(); | 68 run_loop_closure.Run(); |
69 } | 69 } |
70 | 70 |
71 void StoreBudget(double budget) { | 71 void StoreBudget(double budget) { |
72 const GURL origin(kTestOrigin); | 72 const GURL origin(kTestOrigin); |
73 base::RunLoop run_loop; | 73 base::RunLoop run_loop; |
74 GetService()->StoreBudget(origin, budget, run_loop.QuitClosure()); | 74 GetService()->StoreBudget(origin, budget, run_loop.QuitClosure()); |
75 run_loop.Run(); | 75 run_loop.Run(); |
76 } | 76 } |
77 | 77 |
78 // Budget for callbacks to set. | 78 // Budget for callbacks to set. |
79 double budget_; | 79 double budget_; |
80 | 80 |
81 private: | 81 private: |
82 content::TestBrowserThreadBundle thread_bundle_; | 82 content::TestBrowserThreadBundle thread_bundle_; |
83 TestingProfile profile_; | 83 TestingProfile profile_; |
84 }; | 84 }; |
85 | 85 |
86 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) { | 86 TEST_F(BudgetManagerTest, GetBudgetNoBudgetOrSES) { |
87 EXPECT_DOUBLE_EQ(GetBudget(), 0.0); | 87 EXPECT_DOUBLE_EQ(GetBudget(), 0.0); |
88 } | 88 } |
89 | 89 |
90 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) { | 90 TEST_F(BudgetManagerTest, GetBudgetNoBudgetSESExists) { |
91 // Set a starting SES for the url but no stored budget info. | 91 // Set a starting SES for the url but no stored budget info. |
92 const GURL origin(kTestOrigin); | 92 const GURL origin(kTestOrigin); |
93 SetSiteEngagementScore(origin, kTestSES); | 93 SetSiteEngagementScore(origin, kTestSES); |
94 | 94 |
95 EXPECT_DOUBLE_EQ(GetBudget(), kTestSES); | 95 EXPECT_DOUBLE_EQ(GetBudget(), kTestSES); |
96 } | 96 } |
97 | 97 |
98 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) { | 98 TEST_F(BudgetManagerTest, GetBudgetNoElapsedTime) { |
99 StoreBudget(kTestBudget); | 99 StoreBudget(kTestBudget); |
100 EXPECT_DOUBLE_EQ(GetBudget(), kTestBudget); | 100 EXPECT_DOUBLE_EQ(GetBudget(), kTestBudget); |
101 } | 101 } |
102 | 102 |
103 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) { | 103 TEST_F(BudgetManagerTest, GetBudgetElapsedTime) { |
104 // Manually construct a BackgroundBudgetServie with a clock that the test | 104 // Manually construct a BudgetManager with a clock that the test |
105 // can control so that we can fast forward in time. | 105 // can control so that we can fast forward in time. |
106 base::SimpleTestClock* clock = SetClockForTesting(); | 106 base::SimpleTestClock* clock = SetClockForTesting(); |
107 base::Time starting_time = clock->Now(); | 107 base::Time starting_time = clock->Now(); |
108 | 108 |
109 // Set initial SES and budget values. | 109 // Set initial SES and budget values. |
110 const GURL origin(kTestOrigin); | 110 const GURL origin(kTestOrigin); |
111 SetSiteEngagementScore(origin, kTestSES); | 111 SetSiteEngagementScore(origin, kTestSES); |
112 StoreBudget(kTestBudget); | 112 StoreBudget(kTestBudget); |
113 | 113 |
114 double budget = GetBudget(); | 114 double budget = GetBudget(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 starting_time = clock->Now(); | 167 starting_time = clock->Now(); |
168 | 168 |
169 // Query for the budget after 5 days have passed. Again, the budget should be | 169 // Query for the budget after 5 days have passed. Again, the budget should be |
170 // approaching the SES, this time decreasing, but not have reached it. | 170 // approaching the SES, this time decreasing, but not have reached it. |
171 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 171 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
172 budget = GetBudget(); | 172 budget = GetBudget(); |
173 EXPECT_LT(budget, kTestBudget); | 173 EXPECT_LT(budget, kTestBudget); |
174 EXPECT_GT(budget, kLowSES); | 174 EXPECT_GT(budget, kLowSES); |
175 } | 175 } |
176 | 176 |
177 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) { | 177 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { |
178 // Manually construct a BackgroundBudgetService with a clock that the test | 178 // Manually construct a BudgetManager with a clock that the test |
179 // can control so that we can fast forward in time. | 179 // can control so that we can fast forward in time. |
180 base::SimpleTestClock* clock = SetClockForTesting(); | 180 base::SimpleTestClock* clock = SetClockForTesting(); |
181 | 181 |
182 // Set initial SES and budget values. | 182 // Set initial SES and budget values. |
183 const GURL origin(kTestOrigin); | 183 const GURL origin(kTestOrigin); |
184 SetSiteEngagementScore(origin, kTestSES); | 184 SetSiteEngagementScore(origin, kTestSES); |
185 StoreBudget(kTestBudget); | 185 StoreBudget(kTestBudget); |
186 double budget = 0.0; | 186 double budget = 0.0; |
187 | 187 |
188 // Measure over 200 hours. In each hour a message is received, and for 1 in | 188 // Measure over 200 hours. In each hour a message is received, and for 1 in |
189 // 10, budget is consumed. | 189 // 10, budget is consumed. |
190 for (int i = 0; i < 200; i++) { | 190 for (int i = 0; i < 200; i++) { |
191 // Query for the budget after 1 hour has passed. | 191 // Query for the budget after 1 hour has passed. |
192 clock->Advance(base::TimeDelta::FromHours(1)); | 192 clock->Advance(base::TimeDelta::FromHours(1)); |
193 budget = GetBudget(); | 193 budget = GetBudget(); |
194 | 194 |
195 if (i % 10 == 0) { | 195 if (i % 10 == 0) { |
196 double cost = BackgroundBudgetService::GetCost( | 196 double cost = |
197 BackgroundBudgetService::CostType::SILENT_PUSH); | 197 BudgetManager::GetCost(BudgetManager::CostType::SILENT_PUSH); |
198 StoreBudget(budget - cost); | 198 StoreBudget(budget - cost); |
199 } | 199 } |
200 } | 200 } |
201 | 201 |
202 // With a SES of 48.0, the origin will get a budget of 2.4 per day, but the | 202 // With a SES of 48.0, the origin will get a budget of 2.4 per day, but the |
203 // old budget will also decay. At the end, we expect the budget to be lower | 203 // old budget will also decay. At the end, we expect the budget to be lower |
204 // than the starting budget. | 204 // than the starting budget. |
205 EXPECT_GT(budget, 0.0); | 205 EXPECT_GT(budget, 0.0); |
206 EXPECT_LT(budget, kTestBudget); | 206 EXPECT_LT(budget, kTestBudget); |
207 } | 207 } |
208 | 208 |
209 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { | 209 TEST_F(BudgetManagerTest, GetBudgetInvalidBudget) { |
210 const GURL origin(kTestOrigin); | 210 const GURL origin(kTestOrigin); |
211 | 211 |
212 // Set a starting SES for the url. | 212 // Set a starting SES for the url. |
213 SetSiteEngagementScore(origin, kTestSES); | 213 SetSiteEngagementScore(origin, kTestSES); |
214 | 214 |
215 // Set a badly formatted budget in the user preferences. | 215 // Set a badly formatted budget in the user preferences. |
216 DictionaryPrefUpdate update(profile()->GetPrefs(), | 216 DictionaryPrefUpdate update(profile()->GetPrefs(), |
217 prefs::kBackgroundBudgetMap); | 217 prefs::kBackgroundBudgetMap); |
218 base::DictionaryValue* update_map = update.Get(); | 218 base::DictionaryValue* update_map = update.Get(); |
219 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); | 219 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); |
220 | 220 |
221 // Get the budget, expect that it will return SES. | 221 // Get the budget, expect that it will return SES. |
222 EXPECT_DOUBLE_EQ(GetBudget(), kTestSES); | 222 EXPECT_DOUBLE_EQ(GetBudget(), kTestSES); |
223 } | 223 } |
224 | 224 |
225 TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) { | 225 TEST_F(BudgetManagerTest, GetBudgetNegativeTime) { |
226 // Manually construct a BackgroundBudgetService with a clock that the test | 226 // Manually construct a BudgetManager with a clock that the test |
227 // can control so that we can fast forward in time. | 227 // can control so that we can fast forward in time. |
228 base::SimpleTestClock* clock = SetClockForTesting(); | 228 base::SimpleTestClock* clock = SetClockForTesting(); |
229 base::Time starting_time = clock->Now(); | 229 base::Time starting_time = clock->Now(); |
230 | 230 |
231 // Set initial SES and budget values. | 231 // Set initial SES and budget values. |
232 const GURL origin(kTestOrigin); | 232 const GURL origin(kTestOrigin); |
233 SetSiteEngagementScore(origin, kTestSES); | 233 SetSiteEngagementScore(origin, kTestSES); |
234 StoreBudget(kTestBudget); | 234 StoreBudget(kTestBudget); |
235 | 235 |
236 // Move time forward an hour and get the budget. | 236 // Move time forward an hour and get the budget. |
237 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); | 237 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); |
238 double original_budget = GetBudget(); | 238 double original_budget = GetBudget(); |
239 | 239 |
240 // Store the updated budget. | 240 // Store the updated budget. |
241 StoreBudget(original_budget); | 241 StoreBudget(original_budget); |
242 EXPECT_NE(kTestBudget, original_budget); | 242 EXPECT_NE(kTestBudget, original_budget); |
243 | 243 |
244 // Now move time backwards a day and make sure that the current | 244 // Now move time backwards a day and make sure that the current |
245 // budget matches the budget of the most foward time. | 245 // budget matches the budget of the most foward time. |
246 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); | 246 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); |
247 EXPECT_NEAR(original_budget, GetBudget(), 0.01); | 247 EXPECT_NEAR(original_budget, GetBudget(), 0.01); |
248 } | 248 } |
OLD | NEW |