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/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
10 #include "chrome/browser/budget_service/background_budget_service.h" | 11 #include "chrome/browser/budget_service/background_budget_service.h" |
11 #include "chrome/browser/budget_service/background_budget_service_factory.h" | 12 #include "chrome/browser/budget_service/background_budget_service_factory.h" |
12 #include "chrome/browser/engagement/site_engagement_service.h" | 13 #include "chrome/browser/engagement/site_engagement_service.h" |
13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
15 #include "components/prefs/pref_service.h" | 16 #include "components/prefs/pref_service.h" |
16 #include "components/prefs/scoped_user_pref_update.h" | 17 #include "components/prefs/scoped_user_pref_update.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const char kTestOrigin[] = "https://example.com"; | 23 const char kTestOrigin[] = "https://example.com"; |
23 const double kTestBudget = 10.0; | 24 const double kTestBudget = 10.0; |
24 const double kTestSES = 48.0; | 25 const double kTestSES = 48.0; |
25 const double kLowSES = 1.0; | 26 const double kLowSES = 1.0; |
26 const double kMaxSES = 100.0; | 27 const double kMaxSES = 100.0; |
27 // Mirrors definition in BackgroundBudgetService, this is 10 days of seconds. | 28 // Mirrors definition in BackgroundBudgetService, this is 10 days of seconds. |
28 const double kSecondsToAccumulate = 864000.0; | 29 const double kSecondsToAccumulate = 864000.0; |
29 | 30 |
30 } // namespace | 31 } // namespace |
31 | 32 |
32 class BackgroundBudgetServiceTest : public testing::Test { | 33 class BackgroundBudgetServiceTest : public testing::Test { |
33 public: | 34 public: |
34 BackgroundBudgetServiceTest() {} | 35 BackgroundBudgetServiceTest() : budget_(0.0) {} |
35 ~BackgroundBudgetServiceTest() override {} | 36 ~BackgroundBudgetServiceTest() override {} |
36 | 37 |
37 BackgroundBudgetService* GetService() { | 38 BackgroundBudgetService* GetService() { |
38 return BackgroundBudgetServiceFactory::GetForProfile(&profile_); | 39 return BackgroundBudgetServiceFactory::GetForProfile(&profile_); |
39 } | 40 } |
40 | 41 |
41 void SetSiteEngagementScore(const GURL& url, double score) { | 42 void SetSiteEngagementScore(const GURL& url, double score) { |
42 SiteEngagementService* service = SiteEngagementService::Get(&profile_); | 43 SiteEngagementService* service = SiteEngagementService::Get(&profile_); |
43 service->ResetScoreForURL(url, score); | 44 service->ResetScoreForURL(url, score); |
44 } | 45 } |
45 | 46 |
46 Profile* profile() { return &profile_; } | 47 Profile* profile() { return &profile_; } |
47 | 48 |
48 base::SimpleTestClock* SetClockForTesting() { | 49 base::SimpleTestClock* SetClockForTesting() { |
49 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 50 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
50 BackgroundBudgetServiceFactory::GetForProfile(&profile_) | 51 BackgroundBudgetServiceFactory::GetForProfile(&profile_) |
51 ->SetClockForTesting(base::WrapUnique(clock)); | 52 ->SetClockForTesting(base::WrapUnique(clock)); |
52 return clock; | 53 return clock; |
53 } | 54 } |
54 | 55 |
| 56 double GetBudget() { |
| 57 const GURL origin(kTestOrigin); |
| 58 base::RunLoop run_loop; |
| 59 GetService()->GetBudget( |
| 60 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
| 61 base::Unretained(this), run_loop.QuitClosure())); |
| 62 run_loop.Run(); |
| 63 return budget_; |
| 64 } |
| 65 |
| 66 void GotBudget(base::Closure run_loop_closure, double budget) { |
| 67 budget_ = budget; |
| 68 run_loop_closure.Run(); |
| 69 } |
| 70 |
| 71 void StoreBudget(double budget) { |
| 72 const GURL origin(kTestOrigin); |
| 73 base::RunLoop run_loop; |
| 74 GetService()->StoreBudget(origin, budget, run_loop.QuitClosure()); |
| 75 run_loop.Run(); |
| 76 } |
| 77 |
| 78 // Budget for callbacks to set. |
| 79 double budget_; |
| 80 |
55 private: | 81 private: |
56 content::TestBrowserThreadBundle thread_bundle_; | 82 content::TestBrowserThreadBundle thread_bundle_; |
57 TestingProfile profile_; | 83 TestingProfile profile_; |
58 }; | 84 }; |
59 | 85 |
60 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) { | 86 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) { |
61 const GURL origin(kTestOrigin); | 87 EXPECT_DOUBLE_EQ(GetBudget(), 0.0); |
62 | |
63 double budget = GetService()->GetBudget(origin); | |
64 | |
65 EXPECT_DOUBLE_EQ(budget, 0.0); | |
66 } | 88 } |
67 | 89 |
68 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) { | 90 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) { |
| 91 // Set a starting SES for the url but no stored budget info. |
69 const GURL origin(kTestOrigin); | 92 const GURL origin(kTestOrigin); |
70 | |
71 // Set a starting SES for the url but no stored budget info. | |
72 SetSiteEngagementScore(origin, kTestSES); | 93 SetSiteEngagementScore(origin, kTestSES); |
73 | 94 |
74 double budget = GetService()->GetBudget(origin); | 95 EXPECT_DOUBLE_EQ(GetBudget(), kTestSES); |
75 | |
76 EXPECT_DOUBLE_EQ(budget, kTestSES); | |
77 } | 96 } |
78 | 97 |
79 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) { | 98 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) { |
80 const GURL origin(kTestOrigin); | 99 StoreBudget(kTestBudget); |
81 | 100 EXPECT_DOUBLE_EQ(GetBudget(), kTestBudget); |
82 std::unique_ptr<BackgroundBudgetService> service( | |
83 new BackgroundBudgetService(profile())); | |
84 | |
85 service->StoreBudget(origin, kTestBudget); | |
86 | |
87 double budget = service->GetBudget(origin); | |
88 | |
89 EXPECT_NEAR(budget, kTestBudget, kTestBudget); | |
90 } | 101 } |
91 | 102 |
92 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) { | 103 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) { |
93 // Manually construct a BackgroundBudgetServie with a clock that the test | 104 // Manually construct a BackgroundBudgetServie with a clock that the test |
94 // can control so that we can fast forward in time. | 105 // can control so that we can fast forward in time. |
95 BackgroundBudgetService* service = GetService(); | |
96 base::SimpleTestClock* clock = SetClockForTesting(); | 106 base::SimpleTestClock* clock = SetClockForTesting(); |
97 base::Time starting_time = clock->Now(); | 107 base::Time starting_time = clock->Now(); |
98 | 108 |
99 // Set initial SES and budget values. | 109 // Set initial SES and budget values. |
100 const GURL origin(kTestOrigin); | 110 const GURL origin(kTestOrigin); |
101 SetSiteEngagementScore(origin, kTestSES); | 111 SetSiteEngagementScore(origin, kTestSES); |
102 service->StoreBudget(origin, kTestBudget); | 112 StoreBudget(kTestBudget); |
103 | 113 |
104 double budget = service->GetBudget(origin); | 114 double budget = GetBudget(); |
105 EXPECT_DOUBLE_EQ(budget, kTestBudget); | 115 EXPECT_DOUBLE_EQ(budget, kTestBudget); |
106 | 116 |
107 // Query for the budget after 1 second has passed. | 117 // Query for the budget after 1 second has passed. |
108 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); | 118 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); |
109 budget = service->GetBudget(origin); | 119 budget = GetBudget(); |
110 EXPECT_NEAR(budget, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate); | 120 EXPECT_LT(budget, kTestBudget + kTestSES * 1.0 / kSecondsToAccumulate); |
111 EXPECT_GT(budget, kTestBudget); | 121 EXPECT_GT(budget, kTestBudget); |
112 | 122 |
113 // Query for the budget after 1 hour has passed. | 123 // Query for the budget after 1 hour has passed. |
114 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); | 124 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); |
115 budget = service->GetBudget(origin); | 125 budget = GetBudget(); |
116 EXPECT_NEAR(budget, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate); | 126 EXPECT_LT(budget, kTestBudget + kTestSES * 3600.0 / kSecondsToAccumulate); |
117 EXPECT_GT(budget, kTestBudget); | 127 EXPECT_GT(budget, kTestBudget); |
118 | 128 |
119 // Query for the budget after 5 days have passed. The budget should be | 129 // Query for the budget after 5 days have passed. The budget should be |
120 // increasing, but not up the SES score. | 130 // increasing, but not up the SES score. |
121 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 131 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
122 budget = service->GetBudget(origin); | 132 budget = GetBudget(); |
123 EXPECT_GT(budget, kTestBudget); | 133 EXPECT_GT(budget, kTestBudget); |
124 EXPECT_LT(budget, kTestSES); | 134 EXPECT_LT(budget, kTestSES); |
125 double moderate_ses_budget = budget; | 135 double moderate_ses_budget = budget; |
126 | 136 |
127 // Query for the budget after 10 days have passed. By this point, the budget | 137 // Query for the budget after 10 days have passed. By this point, the budget |
128 // should converge to the SES score. | 138 // should converge to the SES score. |
129 clock->SetNow(starting_time + base::TimeDelta::FromDays(10)); | 139 clock->SetNow(starting_time + base::TimeDelta::FromDays(10)); |
130 budget = service->GetBudget(origin); | 140 budget = GetBudget(); |
131 EXPECT_DOUBLE_EQ(budget, kTestSES); | 141 EXPECT_DOUBLE_EQ(budget, kTestSES); |
132 | 142 |
133 // Now, change the SES score to the maximum amount and reinitialize budget. | 143 // Now, change the SES score to the maximum amount and reinitialize budget. |
134 SetSiteEngagementScore(origin, kMaxSES); | 144 SetSiteEngagementScore(origin, kMaxSES); |
135 service->StoreBudget(origin, kTestBudget); | 145 StoreBudget(kTestBudget); |
136 starting_time = clock->Now(); | 146 starting_time = clock->Now(); |
137 | 147 |
138 // Query for the budget after 1 second has passed. | 148 // Query for the budget after 1 second has passed. |
139 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); | 149 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); |
140 budget = service->GetBudget(origin); | 150 budget = GetBudget(); |
141 EXPECT_NEAR(budget, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate); | 151 EXPECT_LT(budget, kTestBudget + kMaxSES * 1.0 / kSecondsToAccumulate); |
142 | 152 |
143 // Query for the budget after 5 days have passed. Again, the budget should be | 153 // Query for the budget after 5 days have passed. Again, the budget should be |
144 // approaching the SES, but not have reached it. | 154 // approaching the SES, but not have reached it. |
145 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 155 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
146 budget = service->GetBudget(origin); | 156 budget = GetBudget(); |
147 EXPECT_GT(budget, kTestBudget); | 157 EXPECT_GT(budget, kTestBudget); |
148 EXPECT_LT(budget, kMaxSES); | 158 EXPECT_LT(budget, kMaxSES); |
149 | 159 |
150 // The budget after 5 days with max SES should be greater than the budget | 160 // The budget after 5 days with max SES should be greater than the budget |
151 // after 5 days with moderate SES. | 161 // after 5 days with moderate SES. |
152 EXPECT_GT(budget, moderate_ses_budget); | 162 EXPECT_GT(budget, moderate_ses_budget); |
153 | 163 |
154 // Now, change the SES score to a low amount and reinitialize budget. | 164 // Now, change the SES score to a low amount and reinitialize budget. |
155 SetSiteEngagementScore(origin, kLowSES); | 165 SetSiteEngagementScore(origin, kLowSES); |
156 service->StoreBudget(origin, kTestBudget); | 166 StoreBudget(kTestBudget); |
157 starting_time = clock->Now(); | 167 starting_time = clock->Now(); |
158 | 168 |
159 // 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 |
160 // approaching the SES, this time decreasing, but not have reached it. | 170 // approaching the SES, this time decreasing, but not have reached it. |
161 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 171 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
162 budget = service->GetBudget(origin); | 172 budget = GetBudget(); |
163 EXPECT_LT(budget, kTestBudget); | 173 EXPECT_LT(budget, kTestBudget); |
164 EXPECT_GT(budget, kLowSES); | 174 EXPECT_GT(budget, kLowSES); |
165 } | 175 } |
166 | 176 |
167 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) { | 177 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) { |
168 // Manually construct a BackgroundBudgetService with a clock that the test | 178 // Manually construct a BackgroundBudgetService with a clock that the test |
169 // can control so that we can fast forward in time. | 179 // can control so that we can fast forward in time. |
170 BackgroundBudgetService* service = GetService(); | |
171 base::SimpleTestClock* clock = SetClockForTesting(); | 180 base::SimpleTestClock* clock = SetClockForTesting(); |
172 | 181 |
173 // Set initial SES and budget values. | 182 // Set initial SES and budget values. |
174 const GURL origin(kTestOrigin); | 183 const GURL origin(kTestOrigin); |
175 SetSiteEngagementScore(origin, kTestSES); | 184 SetSiteEngagementScore(origin, kTestSES); |
176 service->StoreBudget(origin, kTestBudget); | 185 StoreBudget(kTestBudget); |
177 double budget = 0.0; | 186 double budget = 0.0; |
178 | 187 |
179 // 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 |
180 // 10, budget is consumed. | 189 // 10, budget is consumed. |
181 for (int i = 0; i < 200; i++) { | 190 for (int i = 0; i < 200; i++) { |
182 // Query for the budget after 1 hour has passed. | 191 // Query for the budget after 1 hour has passed. |
183 clock->Advance(base::TimeDelta::FromHours(1)); | 192 clock->Advance(base::TimeDelta::FromHours(1)); |
184 budget = service->GetBudget(origin); | 193 budget = GetBudget(); |
185 | 194 |
186 if (i % 10 == 0) { | 195 if (i % 10 == 0) { |
187 double cost = BackgroundBudgetService::GetCost( | 196 double cost = BackgroundBudgetService::GetCost( |
188 BackgroundBudgetService::CostType::SILENT_PUSH); | 197 BackgroundBudgetService::CostType::SILENT_PUSH); |
189 service->StoreBudget(origin, budget - cost); | 198 StoreBudget(budget - cost); |
190 } | 199 } |
191 } | 200 } |
192 | 201 |
193 // 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 |
194 // 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 |
195 // than the starting budget. | 204 // than the starting budget. |
196 EXPECT_GT(budget, 0.0); | 205 EXPECT_GT(budget, 0.0); |
197 EXPECT_LT(budget, kTestBudget); | 206 EXPECT_LT(budget, kTestBudget); |
198 } | 207 } |
199 | 208 |
200 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { | 209 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { |
201 const GURL origin(kTestOrigin); | 210 const GURL origin(kTestOrigin); |
202 | 211 |
203 // Set a starting SES for the url. | 212 // Set a starting SES for the url. |
204 SetSiteEngagementScore(origin, kTestSES); | 213 SetSiteEngagementScore(origin, kTestSES); |
205 | 214 |
206 // Set a badly formatted budget in the user preferences. | 215 // Set a badly formatted budget in the user preferences. |
207 DictionaryPrefUpdate update(profile()->GetPrefs(), | 216 DictionaryPrefUpdate update(profile()->GetPrefs(), |
208 prefs::kBackgroundBudgetMap); | 217 prefs::kBackgroundBudgetMap); |
209 base::DictionaryValue* update_map = update.Get(); | 218 base::DictionaryValue* update_map = update.Get(); |
210 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); | 219 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); |
211 | 220 |
212 // Get the budget, expect that it will return SES. | 221 // Get the budget, expect that it will return SES. |
213 double budget = GetService()->GetBudget(origin); | 222 EXPECT_DOUBLE_EQ(GetBudget(), kTestSES); |
214 | |
215 EXPECT_DOUBLE_EQ(budget, kTestSES); | |
216 } | 223 } |
217 | 224 |
218 TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) { | 225 TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) { |
219 // Manually construct a BackgroundBudgetService with a clock that the test | 226 // Manually construct a BackgroundBudgetService with a clock that the test |
220 // can control so that we can fast forward in time. | 227 // can control so that we can fast forward in time. |
221 BackgroundBudgetService* service = GetService(); | |
222 base::SimpleTestClock* clock = SetClockForTesting(); | 228 base::SimpleTestClock* clock = SetClockForTesting(); |
223 base::Time starting_time = clock->Now(); | 229 base::Time starting_time = clock->Now(); |
224 | 230 |
225 // Set initial SES and budget values. | 231 // Set initial SES and budget values. |
226 const GURL origin(kTestOrigin); | 232 const GURL origin(kTestOrigin); |
227 SetSiteEngagementScore(origin, kTestSES); | 233 SetSiteEngagementScore(origin, kTestSES); |
228 service->StoreBudget(origin, kTestBudget); | 234 StoreBudget(kTestBudget); |
229 | 235 |
230 // Move time forward an hour and get the budget. | 236 // Move time forward an hour and get the budget. |
231 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); | 237 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); |
232 double budget = service->GetBudget(origin); | 238 double original_budget = GetBudget(); |
233 service->StoreBudget(origin, budget); | 239 |
234 EXPECT_NE(kTestBudget, budget); | 240 // Store the updated budget. |
| 241 StoreBudget(original_budget); |
| 242 EXPECT_NE(kTestBudget, original_budget); |
235 | 243 |
236 // 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 |
237 // budget matches the budget of the most foward time. | 245 // budget matches the budget of the most foward time. |
238 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); | 246 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); |
239 double back_budget = service->GetBudget(origin); | 247 EXPECT_NEAR(original_budget, GetBudget(), 0.01); |
240 EXPECT_NEAR(budget, back_budget, 0.01); | |
241 } | 248 } |
OLD | NEW |