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 |
31 using base::RunLoop; | |
Michael van Ouwerkerk
2016/06/10 15:04:05
Peter would probably ask you not to do this with t
harkness
2016/06/10 15:19:56
Done.
| |
32 | |
30 } // namespace | 33 } // namespace |
31 | 34 |
32 class BackgroundBudgetServiceTest : public testing::Test { | 35 class BackgroundBudgetServiceTest : public testing::Test { |
33 public: | 36 public: |
34 BackgroundBudgetServiceTest() {} | 37 BackgroundBudgetServiceTest() : budget_(0.0) {} |
35 ~BackgroundBudgetServiceTest() override {} | 38 ~BackgroundBudgetServiceTest() override {} |
36 | 39 |
37 BackgroundBudgetService* GetService() { | 40 BackgroundBudgetService* GetService() { |
38 return BackgroundBudgetServiceFactory::GetForProfile(&profile_); | 41 return BackgroundBudgetServiceFactory::GetForProfile(&profile_); |
39 } | 42 } |
40 | 43 |
41 void SetSiteEngagementScore(const GURL& url, double score) { | 44 void SetSiteEngagementScore(const GURL& url, double score) { |
42 SiteEngagementService* service = SiteEngagementService::Get(&profile_); | 45 SiteEngagementService* service = SiteEngagementService::Get(&profile_); |
43 service->ResetScoreForURL(url, score); | 46 service->ResetScoreForURL(url, score); |
44 } | 47 } |
45 | 48 |
46 Profile* profile() { return &profile_; } | 49 Profile* profile() { return &profile_; } |
47 | 50 |
48 base::SimpleTestClock* SetClockForTesting() { | 51 base::SimpleTestClock* SetClockForTesting() { |
49 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 52 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
50 BackgroundBudgetServiceFactory::GetForProfile(&profile_) | 53 BackgroundBudgetServiceFactory::GetForProfile(&profile_) |
51 ->SetClockForTesting(base::WrapUnique(clock)); | 54 ->SetClockForTesting(base::WrapUnique(clock)); |
52 return clock; | 55 return clock; |
53 } | 56 } |
54 | 57 |
58 void GotBudget(base::Closure run_loop_closure, double budget) { | |
59 budget_ = budget; | |
60 run_loop_closure.Run(); | |
61 } | |
62 | |
63 void StoredBudget(base::Closure run_loop_closure) { run_loop_closure.Run(); } | |
64 | |
65 // Budget for callbacks to set. | |
66 double budget_; | |
67 | |
55 private: | 68 private: |
56 content::TestBrowserThreadBundle thread_bundle_; | 69 content::TestBrowserThreadBundle thread_bundle_; |
57 TestingProfile profile_; | 70 TestingProfile profile_; |
58 }; | 71 }; |
59 | 72 |
60 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) { | 73 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) { |
61 const GURL origin(kTestOrigin); | 74 const GURL origin(kTestOrigin); |
62 | 75 |
63 double budget = GetService()->GetBudget(origin); | 76 std::unique_ptr<RunLoop> run_loop(new RunLoop()); |
77 GetService()->GetBudget( | |
78 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
79 base::Unretained(this), run_loop->QuitClosure())); | |
80 run_loop->Run(); | |
64 | 81 |
65 EXPECT_DOUBLE_EQ(budget, 0.0); | 82 EXPECT_DOUBLE_EQ(budget_, 0.0); |
66 } | 83 } |
67 | 84 |
68 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) { | 85 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) { |
69 const GURL origin(kTestOrigin); | 86 const GURL origin(kTestOrigin); |
70 | 87 |
71 // Set a starting SES for the url but no stored budget info. | 88 // Set a starting SES for the url but no stored budget info. |
72 SetSiteEngagementScore(origin, kTestSES); | 89 SetSiteEngagementScore(origin, kTestSES); |
73 | 90 |
74 double budget = GetService()->GetBudget(origin); | 91 std::unique_ptr<RunLoop> run_loop(new RunLoop()); |
92 GetService()->GetBudget( | |
93 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
94 base::Unretained(this), run_loop->QuitClosure())); | |
95 run_loop->Run(); | |
75 | 96 |
76 EXPECT_DOUBLE_EQ(budget, kTestSES); | 97 EXPECT_DOUBLE_EQ(budget_, kTestSES); |
77 } | 98 } |
78 | 99 |
79 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) { | 100 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) { |
80 const GURL origin(kTestOrigin); | 101 const GURL origin(kTestOrigin); |
81 | 102 |
82 std::unique_ptr<BackgroundBudgetService> service( | 103 std::unique_ptr<BackgroundBudgetService> service( |
83 new BackgroundBudgetService(profile())); | 104 new BackgroundBudgetService(profile())); |
84 | 105 |
85 service->StoreBudget(origin, kTestBudget); | 106 std::unique_ptr<RunLoop> run_loop(new RunLoop()); |
107 service->StoreBudget( | |
108 origin, kTestBudget, | |
109 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
110 base::Unretained(this), run_loop->QuitClosure())); | |
111 run_loop->Run(); | |
86 | 112 |
87 double budget = service->GetBudget(origin); | 113 run_loop.reset(new RunLoop()); |
114 GetService()->GetBudget( | |
115 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
116 base::Unretained(this), run_loop->QuitClosure())); | |
117 run_loop->Run(); | |
88 | 118 |
89 EXPECT_NEAR(budget, kTestBudget, kTestBudget); | 119 EXPECT_NEAR(budget_, kTestBudget, kTestBudget); |
90 } | 120 } |
91 | 121 |
92 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) { | 122 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) { |
93 // Manually construct a BackgroundBudgetServie with a clock that the test | 123 // Manually construct a BackgroundBudgetServie with a clock that the test |
94 // can control so that we can fast forward in time. | 124 // can control so that we can fast forward in time. |
95 BackgroundBudgetService* service = GetService(); | 125 BackgroundBudgetService* service = GetService(); |
96 base::SimpleTestClock* clock = SetClockForTesting(); | 126 base::SimpleTestClock* clock = SetClockForTesting(); |
97 base::Time starting_time = clock->Now(); | 127 base::Time starting_time = clock->Now(); |
98 | 128 |
99 // Set initial SES and budget values. | 129 // Set initial SES and budget values. |
100 const GURL origin(kTestOrigin); | 130 const GURL origin(kTestOrigin); |
101 SetSiteEngagementScore(origin, kTestSES); | 131 SetSiteEngagementScore(origin, kTestSES); |
102 service->StoreBudget(origin, kTestBudget); | 132 std::unique_ptr<RunLoop> run_loop(new RunLoop()); |
133 service->StoreBudget( | |
134 origin, kTestBudget, | |
135 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
136 base::Unretained(this), run_loop->QuitClosure())); | |
137 run_loop->Run(); | |
103 | 138 |
104 double budget = service->GetBudget(origin); | 139 run_loop.reset(new RunLoop()); |
105 EXPECT_DOUBLE_EQ(budget, kTestBudget); | 140 GetService()->GetBudget( |
141 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
142 base::Unretained(this), run_loop->QuitClosure())); | |
143 run_loop->Run(); | |
144 EXPECT_DOUBLE_EQ(budget_, kTestBudget); | |
106 | 145 |
107 // Query for the budget after 1 second has passed. | 146 // Query for the budget after 1 second has passed. |
108 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); | 147 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); |
109 budget = service->GetBudget(origin); | 148 run_loop.reset(new RunLoop()); |
110 EXPECT_NEAR(budget, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate); | 149 GetService()->GetBudget( |
111 EXPECT_GT(budget, kTestBudget); | 150 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
151 base::Unretained(this), run_loop->QuitClosure())); | |
152 run_loop->Run(); | |
153 EXPECT_NEAR(budget_, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate); | |
154 EXPECT_GT(budget_, kTestBudget); | |
112 | 155 |
113 // Query for the budget after 1 hour has passed. | 156 // Query for the budget after 1 hour has passed. |
114 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); | 157 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); |
115 budget = service->GetBudget(origin); | 158 run_loop.reset(new RunLoop()); |
116 EXPECT_NEAR(budget, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate); | 159 GetService()->GetBudget( |
117 EXPECT_GT(budget, kTestBudget); | 160 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
161 base::Unretained(this), run_loop->QuitClosure())); | |
162 run_loop->Run(); | |
163 EXPECT_NEAR(budget_, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate); | |
164 EXPECT_GT(budget_, kTestBudget); | |
118 | 165 |
119 // Query for the budget after 5 days have passed. The budget should be | 166 // Query for the budget after 5 days have passed. The budget should be |
120 // increasing, but not up the SES score. | 167 // increasing, but not up the SES score. |
121 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 168 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
122 budget = service->GetBudget(origin); | 169 run_loop.reset(new RunLoop()); |
123 EXPECT_GT(budget, kTestBudget); | 170 GetService()->GetBudget( |
124 EXPECT_LT(budget, kTestSES); | 171 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
125 double moderate_ses_budget = budget; | 172 base::Unretained(this), run_loop->QuitClosure())); |
173 run_loop->Run(); | |
174 EXPECT_GT(budget_, kTestBudget); | |
175 EXPECT_LT(budget_, kTestSES); | |
176 double moderate_ses_budget = budget_; | |
126 | 177 |
127 // Query for the budget after 10 days have passed. By this point, the budget | 178 // Query for the budget after 10 days have passed. By this point, the budget |
128 // should converge to the SES score. | 179 // should converge to the SES score. |
129 clock->SetNow(starting_time + base::TimeDelta::FromDays(10)); | 180 clock->SetNow(starting_time + base::TimeDelta::FromDays(10)); |
130 budget = service->GetBudget(origin); | 181 run_loop.reset(new RunLoop()); |
131 EXPECT_DOUBLE_EQ(budget, kTestSES); | 182 GetService()->GetBudget( |
183 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
184 base::Unretained(this), run_loop->QuitClosure())); | |
185 run_loop->Run(); | |
186 EXPECT_DOUBLE_EQ(budget_, kTestSES); | |
132 | 187 |
133 // Now, change the SES score to the maximum amount and reinitialize budget. | 188 // Now, change the SES score to the maximum amount and reinitialize budget. |
134 SetSiteEngagementScore(origin, kMaxSES); | 189 SetSiteEngagementScore(origin, kMaxSES); |
135 service->StoreBudget(origin, kTestBudget); | 190 run_loop.reset(new RunLoop()); |
191 service->StoreBudget( | |
192 origin, kTestBudget, | |
193 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
194 base::Unretained(this), run_loop->QuitClosure())); | |
195 run_loop->Run(); | |
196 | |
136 starting_time = clock->Now(); | 197 starting_time = clock->Now(); |
137 | 198 |
138 // Query for the budget after 1 second has passed. | 199 // Query for the budget after 1 second has passed. |
139 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); | 200 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); |
140 budget = service->GetBudget(origin); | 201 run_loop.reset(new RunLoop()); |
141 EXPECT_NEAR(budget, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate); | 202 GetService()->GetBudget( |
203 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
204 base::Unretained(this), run_loop->QuitClosure())); | |
205 run_loop->Run(); | |
206 EXPECT_NEAR(budget_, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate); | |
142 | 207 |
143 // Query for the budget after 5 days have passed. Again, the budget should be | 208 // Query for the budget after 5 days have passed. Again, the budget should be |
144 // approaching the SES, but not have reached it. | 209 // approaching the SES, but not have reached it. |
145 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 210 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
146 budget = service->GetBudget(origin); | 211 run_loop.reset(new RunLoop()); |
147 EXPECT_GT(budget, kTestBudget); | 212 GetService()->GetBudget( |
148 EXPECT_LT(budget, kMaxSES); | 213 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
214 base::Unretained(this), run_loop->QuitClosure())); | |
215 run_loop->Run(); | |
216 EXPECT_GT(budget_, kTestBudget); | |
217 EXPECT_LT(budget_, kMaxSES); | |
149 | 218 |
150 // The budget after 5 days with max SES should be greater than the budget | 219 // The budget after 5 days with max SES should be greater than the budget |
151 // after 5 days with moderate SES. | 220 // after 5 days with moderate SES. |
152 EXPECT_GT(budget, moderate_ses_budget); | 221 EXPECT_GT(budget_, moderate_ses_budget); |
153 | 222 |
154 // Now, change the SES score to a low amount and reinitialize budget. | 223 // Now, change the SES score to a low amount and reinitialize budget. |
155 SetSiteEngagementScore(origin, kLowSES); | 224 SetSiteEngagementScore(origin, kLowSES); |
156 service->StoreBudget(origin, kTestBudget); | 225 run_loop.reset(new RunLoop()); |
226 service->StoreBudget( | |
227 origin, kTestBudget, | |
228 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
229 base::Unretained(this), run_loop->QuitClosure())); | |
230 run_loop->Run(); | |
157 starting_time = clock->Now(); | 231 starting_time = clock->Now(); |
158 | 232 |
159 // Query for the budget after 5 days have passed. Again, the budget should be | 233 // 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. | 234 // approaching the SES, this time decreasing, but not have reached it. |
161 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 235 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
162 budget = service->GetBudget(origin); | 236 run_loop.reset(new RunLoop()); |
163 EXPECT_LT(budget, kTestBudget); | 237 GetService()->GetBudget( |
164 EXPECT_GT(budget, kLowSES); | 238 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
239 base::Unretained(this), run_loop->QuitClosure())); | |
240 run_loop->Run(); | |
241 EXPECT_LT(budget_, kTestBudget); | |
242 EXPECT_GT(budget_, kLowSES); | |
165 } | 243 } |
166 | 244 |
167 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) { | 245 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) { |
168 // Manually construct a BackgroundBudgetService with a clock that the test | 246 // Manually construct a BackgroundBudgetService with a clock that the test |
169 // can control so that we can fast forward in time. | 247 // can control so that we can fast forward in time. |
170 BackgroundBudgetService* service = GetService(); | 248 BackgroundBudgetService* service = GetService(); |
171 base::SimpleTestClock* clock = SetClockForTesting(); | 249 base::SimpleTestClock* clock = SetClockForTesting(); |
172 | 250 |
173 // Set initial SES and budget values. | 251 // Set initial SES and budget values. |
174 const GURL origin(kTestOrigin); | 252 const GURL origin(kTestOrigin); |
175 SetSiteEngagementScore(origin, kTestSES); | 253 SetSiteEngagementScore(origin, kTestSES); |
176 service->StoreBudget(origin, kTestBudget); | 254 std::unique_ptr<RunLoop> run_loop(new RunLoop()); |
177 double budget = 0.0; | 255 service->StoreBudget( |
256 origin, kTestBudget, | |
257 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
258 base::Unretained(this), run_loop->QuitClosure())); | |
259 run_loop->Run(); | |
178 | 260 |
179 // Measure over 200 hours. In each hour a message is received, and for 1 in | 261 // Measure over 200 hours. In each hour a message is received, and for 1 in |
180 // 10, budget is consumed. | 262 // 10, budget is consumed. |
181 for (int i = 0; i < 200; i++) { | 263 for (int i = 0; i < 200; i++) { |
182 // Query for the budget after 1 hour has passed. | 264 // Query for the budget after 1 hour has passed. |
183 clock->Advance(base::TimeDelta::FromHours(1)); | 265 clock->Advance(base::TimeDelta::FromHours(1)); |
184 budget = service->GetBudget(origin); | 266 run_loop.reset(new RunLoop()); |
267 GetService()->GetBudget( | |
268 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
269 base::Unretained(this), run_loop->QuitClosure())); | |
270 run_loop->Run(); | |
185 | 271 |
186 if (i % 10 == 0) { | 272 if (i % 10 == 0) { |
187 double cost = BackgroundBudgetService::GetCost( | 273 double cost = BackgroundBudgetService::GetCost( |
188 BackgroundBudgetService::CostType::SILENT_PUSH); | 274 BackgroundBudgetService::CostType::SILENT_PUSH); |
189 service->StoreBudget(origin, budget - cost); | 275 run_loop.reset(new RunLoop()); |
276 service->StoreBudget( | |
277 origin, budget_ - cost, | |
278 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
279 base::Unretained(this), run_loop->QuitClosure())); | |
280 run_loop->Run(); | |
190 } | 281 } |
191 } | 282 } |
192 | 283 |
193 // With a SES of 48.0, the origin will get a budget of 2.4 per day, but the | 284 // 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 | 285 // old budget will also decay. At the end, we expect the budget to be lower |
195 // than the starting budget. | 286 // than the starting budget. |
196 EXPECT_GT(budget, 0.0); | 287 EXPECT_GT(budget_, 0.0); |
197 EXPECT_LT(budget, kTestBudget); | 288 EXPECT_LT(budget_, kTestBudget); |
198 } | 289 } |
199 | 290 |
200 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { | 291 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { |
201 const GURL origin(kTestOrigin); | 292 const GURL origin(kTestOrigin); |
202 | 293 |
203 // Set a starting SES for the url. | 294 // Set a starting SES for the url. |
204 SetSiteEngagementScore(origin, kTestSES); | 295 SetSiteEngagementScore(origin, kTestSES); |
205 | 296 |
206 // Set a badly formatted budget in the user preferences. | 297 // Set a badly formatted budget in the user preferences. |
207 DictionaryPrefUpdate update(profile()->GetPrefs(), | 298 DictionaryPrefUpdate update(profile()->GetPrefs(), |
208 prefs::kBackgroundBudgetMap); | 299 prefs::kBackgroundBudgetMap); |
209 base::DictionaryValue* update_map = update.Get(); | 300 base::DictionaryValue* update_map = update.Get(); |
210 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); | 301 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); |
211 | 302 |
212 // Get the budget, expect that it will return SES. | 303 // Get the budget, expect that it will return SES. |
213 double budget = GetService()->GetBudget(origin); | 304 std::unique_ptr<RunLoop> run_loop(new RunLoop()); |
305 GetService()->GetBudget( | |
306 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
307 base::Unretained(this), run_loop->QuitClosure())); | |
308 run_loop->Run(); | |
214 | 309 |
215 EXPECT_DOUBLE_EQ(budget, kTestSES); | 310 EXPECT_DOUBLE_EQ(budget_, kTestSES); |
216 } | 311 } |
217 | 312 |
218 TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) { | 313 TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) { |
219 // Manually construct a BackgroundBudgetService with a clock that the test | 314 // Manually construct a BackgroundBudgetService with a clock that the test |
220 // can control so that we can fast forward in time. | 315 // can control so that we can fast forward in time. |
221 BackgroundBudgetService* service = GetService(); | 316 BackgroundBudgetService* service = GetService(); |
222 base::SimpleTestClock* clock = SetClockForTesting(); | 317 base::SimpleTestClock* clock = SetClockForTesting(); |
223 base::Time starting_time = clock->Now(); | 318 base::Time starting_time = clock->Now(); |
224 | 319 |
225 // Set initial SES and budget values. | 320 // Set initial SES and budget values. |
226 const GURL origin(kTestOrigin); | 321 const GURL origin(kTestOrigin); |
227 SetSiteEngagementScore(origin, kTestSES); | 322 SetSiteEngagementScore(origin, kTestSES); |
228 service->StoreBudget(origin, kTestBudget); | 323 std::unique_ptr<RunLoop> run_loop(new RunLoop()); |
324 service->StoreBudget( | |
325 origin, kTestBudget, | |
326 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
327 base::Unretained(this), run_loop->QuitClosure())); | |
328 run_loop->Run(); | |
229 | 329 |
230 // Move time forward an hour and get the budget. | 330 // Move time forward an hour and get the budget. |
231 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); | 331 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); |
232 double budget = service->GetBudget(origin); | 332 run_loop.reset(new RunLoop()); |
233 service->StoreBudget(origin, budget); | 333 GetService()->GetBudget( |
234 EXPECT_NE(kTestBudget, budget); | 334 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
335 base::Unretained(this), run_loop->QuitClosure())); | |
336 run_loop->Run(); | |
337 | |
338 // Store the updated budget. | |
339 run_loop.reset(new RunLoop()); | |
340 service->StoreBudget( | |
341 origin, budget_, | |
342 base::Bind(&BackgroundBudgetServiceTest::StoredBudget, | |
343 base::Unretained(this), run_loop->QuitClosure())); | |
344 run_loop->Run(); | |
345 EXPECT_NE(kTestBudget, budget_); | |
346 double original_budget = budget_; | |
235 | 347 |
236 // Now move time backwards a day and make sure that the current | 348 // Now move time backwards a day and make sure that the current |
237 // budget matches the budget of the most foward time. | 349 // budget matches the budget of the most foward time. |
238 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); | 350 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); |
239 double back_budget = service->GetBudget(origin); | 351 run_loop.reset(new RunLoop()); |
240 EXPECT_NEAR(budget, back_budget, 0.01); | 352 GetService()->GetBudget( |
353 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
354 base::Unretained(this), run_loop->QuitClosure())); | |
355 run_loop->Run(); | |
356 EXPECT_NEAR(original_budget, budget_, 0.01); | |
241 } | 357 } |
OLD | NEW |