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); | |
johnme
2016/06/20 13:35:22
Nit: perhaps simpler to just pass GURL(kTestOrigin
harkness
2016/06/20 14:05:08
Putting it here allowed me to remove it from a bun
| |
73 std::unique_ptr<BackgroundBudgetService> service( | |
74 new BackgroundBudgetService(profile())); | |
johnme
2016/06/20 13:35:22
1. No need for unique_ptr here (`BackgroundBudgetS
harkness
2016/06/20 14:05:08
Yeah, I had that in one of my test cases, probably
| |
75 base::RunLoop run_loop; | |
76 service->StoreBudget(origin, budget, run_loop.QuitClosure()); | |
77 run_loop.Run(); | |
78 } | |
79 | |
80 // Budget for callbacks to set. | |
81 double budget_; | |
82 | |
55 private: | 83 private: |
56 content::TestBrowserThreadBundle thread_bundle_; | 84 content::TestBrowserThreadBundle thread_bundle_; |
57 TestingProfile profile_; | 85 TestingProfile profile_; |
58 }; | 86 }; |
59 | 87 |
60 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) { | 88 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) { |
61 const GURL origin(kTestOrigin); | 89 double budget = GetBudget(); |
johnme
2016/06/20 13:35:22
Nit: I guess you can just inline this into the EXP
harkness
2016/06/20 14:05:08
I swapped them in calls where it was only used onc
| |
62 | |
63 double budget = GetService()->GetBudget(origin); | |
64 | |
65 EXPECT_DOUBLE_EQ(budget, 0.0); | 90 EXPECT_DOUBLE_EQ(budget, 0.0); |
66 } | 91 } |
67 | 92 |
68 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) { | 93 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) { |
94 // Set a starting SES for the url but no stored budget info. | |
69 const GURL origin(kTestOrigin); | 95 const GURL origin(kTestOrigin); |
70 | |
71 // Set a starting SES for the url but no stored budget info. | |
72 SetSiteEngagementScore(origin, kTestSES); | 96 SetSiteEngagementScore(origin, kTestSES); |
73 | 97 |
74 double budget = GetService()->GetBudget(origin); | 98 double budget = GetBudget(); |
75 | |
76 EXPECT_DOUBLE_EQ(budget, kTestSES); | 99 EXPECT_DOUBLE_EQ(budget, kTestSES); |
77 } | 100 } |
78 | 101 |
79 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) { | 102 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) { |
80 const GURL origin(kTestOrigin); | 103 StoreBudget(kTestBudget); |
81 | 104 double budget = GetBudget(); |
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); | 105 EXPECT_NEAR(budget, kTestBudget, kTestBudget); |
johnme
2016/06/20 13:35:22
The 3rd parameter to EXPECT_NEAR is the allowable
harkness
2016/06/20 14:05:08
You're right, this one had a bad bound on it. The
| |
90 } | 106 } |
91 | 107 |
92 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) { | 108 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) { |
93 // Manually construct a BackgroundBudgetServie with a clock that the test | 109 // Manually construct a BackgroundBudgetServie with a clock that the test |
94 // can control so that we can fast forward in time. | 110 // can control so that we can fast forward in time. |
95 BackgroundBudgetService* service = GetService(); | 111 BackgroundBudgetService* service = GetService(); |
96 base::SimpleTestClock* clock = SetClockForTesting(); | 112 base::SimpleTestClock* clock = SetClockForTesting(); |
97 base::Time starting_time = clock->Now(); | 113 base::Time starting_time = clock->Now(); |
98 | 114 |
99 // Set initial SES and budget values. | 115 // Set initial SES and budget values. |
100 const GURL origin(kTestOrigin); | 116 const GURL origin(kTestOrigin); |
101 SetSiteEngagementScore(origin, kTestSES); | 117 SetSiteEngagementScore(origin, kTestSES); |
102 service->StoreBudget(origin, kTestBudget); | 118 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop()); |
119 service->StoreBudget(origin, kTestBudget, run_loop->QuitClosure()); | |
120 run_loop->Run(); | |
103 | 121 |
104 double budget = service->GetBudget(origin); | 122 run_loop.reset(new base::RunLoop()); |
105 EXPECT_DOUBLE_EQ(budget, kTestBudget); | 123 GetService()->GetBudget( |
johnme
2016/06/20 13:35:22
It seems like you can use the helper methods here
harkness
2016/06/20 14:05:08
Done.
| |
124 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
125 base::Unretained(this), run_loop->QuitClosure())); | |
126 run_loop->Run(); | |
127 EXPECT_DOUBLE_EQ(budget_, kTestBudget); | |
106 | 128 |
107 // Query for the budget after 1 second has passed. | 129 // Query for the budget after 1 second has passed. |
108 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); | 130 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); |
109 budget = service->GetBudget(origin); | 131 run_loop.reset(new base::RunLoop()); |
110 EXPECT_NEAR(budget, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate); | 132 GetService()->GetBudget( |
111 EXPECT_GT(budget, kTestBudget); | 133 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
134 base::Unretained(this), run_loop->QuitClosure())); | |
135 run_loop->Run(); | |
136 EXPECT_NEAR(budget_, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate); | |
137 EXPECT_GT(budget_, kTestBudget); | |
112 | 138 |
113 // Query for the budget after 1 hour has passed. | 139 // Query for the budget after 1 hour has passed. |
114 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); | 140 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); |
115 budget = service->GetBudget(origin); | 141 run_loop.reset(new base::RunLoop()); |
116 EXPECT_NEAR(budget, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate); | 142 GetService()->GetBudget( |
117 EXPECT_GT(budget, kTestBudget); | 143 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
144 base::Unretained(this), run_loop->QuitClosure())); | |
145 run_loop->Run(); | |
146 EXPECT_NEAR(budget_, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate); | |
johnme
2016/06/20 13:35:22
This is quite an elaborate calculation for the all
harkness
2016/06/20 14:05:08
Updated it to be a more bounds-based calculation.
| |
147 EXPECT_GT(budget_, kTestBudget); | |
118 | 148 |
119 // Query for the budget after 5 days have passed. The budget should be | 149 // Query for the budget after 5 days have passed. The budget should be |
120 // increasing, but not up the SES score. | 150 // increasing, but not up the SES score. |
121 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 151 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
122 budget = service->GetBudget(origin); | 152 run_loop.reset(new base::RunLoop()); |
123 EXPECT_GT(budget, kTestBudget); | 153 GetService()->GetBudget( |
124 EXPECT_LT(budget, kTestSES); | 154 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
125 double moderate_ses_budget = budget; | 155 base::Unretained(this), run_loop->QuitClosure())); |
156 run_loop->Run(); | |
157 EXPECT_GT(budget_, kTestBudget); | |
158 EXPECT_LT(budget_, kTestSES); | |
159 double moderate_ses_budget = budget_; | |
126 | 160 |
127 // Query for the budget after 10 days have passed. By this point, the budget | 161 // Query for the budget after 10 days have passed. By this point, the budget |
128 // should converge to the SES score. | 162 // should converge to the SES score. |
129 clock->SetNow(starting_time + base::TimeDelta::FromDays(10)); | 163 clock->SetNow(starting_time + base::TimeDelta::FromDays(10)); |
130 budget = service->GetBudget(origin); | 164 run_loop.reset(new base::RunLoop()); |
131 EXPECT_DOUBLE_EQ(budget, kTestSES); | 165 GetService()->GetBudget( |
166 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
167 base::Unretained(this), run_loop->QuitClosure())); | |
168 run_loop->Run(); | |
169 EXPECT_DOUBLE_EQ(budget_, kTestSES); | |
132 | 170 |
133 // Now, change the SES score to the maximum amount and reinitialize budget. | 171 // Now, change the SES score to the maximum amount and reinitialize budget. |
134 SetSiteEngagementScore(origin, kMaxSES); | 172 SetSiteEngagementScore(origin, kMaxSES); |
135 service->StoreBudget(origin, kTestBudget); | 173 run_loop.reset(new base::RunLoop()); |
174 service->StoreBudget(origin, kTestBudget, run_loop->QuitClosure()); | |
175 run_loop->Run(); | |
176 | |
136 starting_time = clock->Now(); | 177 starting_time = clock->Now(); |
137 | 178 |
138 // Query for the budget after 1 second has passed. | 179 // Query for the budget after 1 second has passed. |
139 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); | 180 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1)); |
140 budget = service->GetBudget(origin); | 181 run_loop.reset(new base::RunLoop()); |
141 EXPECT_NEAR(budget, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate); | 182 GetService()->GetBudget( |
183 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
184 base::Unretained(this), run_loop->QuitClosure())); | |
185 run_loop->Run(); | |
186 EXPECT_NEAR(budget_, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate); | |
142 | 187 |
143 // Query for the budget after 5 days have passed. Again, the budget should be | 188 // Query for the budget after 5 days have passed. Again, the budget should be |
144 // approaching the SES, but not have reached it. | 189 // approaching the SES, but not have reached it. |
145 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 190 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
146 budget = service->GetBudget(origin); | 191 run_loop.reset(new base::RunLoop()); |
147 EXPECT_GT(budget, kTestBudget); | 192 GetService()->GetBudget( |
148 EXPECT_LT(budget, kMaxSES); | 193 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
194 base::Unretained(this), run_loop->QuitClosure())); | |
195 run_loop->Run(); | |
196 EXPECT_GT(budget_, kTestBudget); | |
197 EXPECT_LT(budget_, kMaxSES); | |
149 | 198 |
150 // The budget after 5 days with max SES should be greater than the budget | 199 // The budget after 5 days with max SES should be greater than the budget |
151 // after 5 days with moderate SES. | 200 // after 5 days with moderate SES. |
152 EXPECT_GT(budget, moderate_ses_budget); | 201 EXPECT_GT(budget_, moderate_ses_budget); |
153 | 202 |
154 // Now, change the SES score to a low amount and reinitialize budget. | 203 // Now, change the SES score to a low amount and reinitialize budget. |
155 SetSiteEngagementScore(origin, kLowSES); | 204 SetSiteEngagementScore(origin, kLowSES); |
156 service->StoreBudget(origin, kTestBudget); | 205 run_loop.reset(new base::RunLoop()); |
206 service->StoreBudget(origin, kTestBudget, run_loop->QuitClosure()); | |
207 run_loop->Run(); | |
157 starting_time = clock->Now(); | 208 starting_time = clock->Now(); |
158 | 209 |
159 // Query for the budget after 5 days have passed. Again, the budget should be | 210 // 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. | 211 // approaching the SES, this time decreasing, but not have reached it. |
161 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); | 212 clock->SetNow(starting_time + base::TimeDelta::FromDays(5)); |
162 budget = service->GetBudget(origin); | 213 run_loop.reset(new base::RunLoop()); |
163 EXPECT_LT(budget, kTestBudget); | 214 GetService()->GetBudget( |
164 EXPECT_GT(budget, kLowSES); | 215 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
216 base::Unretained(this), run_loop->QuitClosure())); | |
217 run_loop->Run(); | |
218 EXPECT_LT(budget_, kTestBudget); | |
219 EXPECT_GT(budget_, kLowSES); | |
165 } | 220 } |
166 | 221 |
167 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) { | 222 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) { |
168 // Manually construct a BackgroundBudgetService with a clock that the test | 223 // Manually construct a BackgroundBudgetService with a clock that the test |
169 // can control so that we can fast forward in time. | 224 // can control so that we can fast forward in time. |
170 BackgroundBudgetService* service = GetService(); | 225 BackgroundBudgetService* service = GetService(); |
171 base::SimpleTestClock* clock = SetClockForTesting(); | 226 base::SimpleTestClock* clock = SetClockForTesting(); |
172 | 227 |
173 // Set initial SES and budget values. | 228 // Set initial SES and budget values. |
174 const GURL origin(kTestOrigin); | 229 const GURL origin(kTestOrigin); |
175 SetSiteEngagementScore(origin, kTestSES); | 230 SetSiteEngagementScore(origin, kTestSES); |
176 service->StoreBudget(origin, kTestBudget); | 231 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop()); |
177 double budget = 0.0; | 232 service->StoreBudget(origin, kTestBudget, run_loop->QuitClosure()); |
233 run_loop->Run(); | |
178 | 234 |
179 // Measure over 200 hours. In each hour a message is received, and for 1 in | 235 // Measure over 200 hours. In each hour a message is received, and for 1 in |
180 // 10, budget is consumed. | 236 // 10, budget is consumed. |
181 for (int i = 0; i < 200; i++) { | 237 for (int i = 0; i < 200; i++) { |
182 // Query for the budget after 1 hour has passed. | 238 // Query for the budget after 1 hour has passed. |
183 clock->Advance(base::TimeDelta::FromHours(1)); | 239 clock->Advance(base::TimeDelta::FromHours(1)); |
184 budget = service->GetBudget(origin); | 240 run_loop.reset(new base::RunLoop()); |
241 GetService()->GetBudget( | |
242 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
243 base::Unretained(this), run_loop->QuitClosure())); | |
244 run_loop->Run(); | |
185 | 245 |
186 if (i % 10 == 0) { | 246 if (i % 10 == 0) { |
187 double cost = BackgroundBudgetService::GetCost( | 247 double cost = BackgroundBudgetService::GetCost( |
188 BackgroundBudgetService::CostType::SILENT_PUSH); | 248 BackgroundBudgetService::CostType::SILENT_PUSH); |
189 service->StoreBudget(origin, budget - cost); | 249 run_loop.reset(new base::RunLoop()); |
250 service->StoreBudget(origin, budget_ - cost, run_loop->QuitClosure()); | |
251 run_loop->Run(); | |
190 } | 252 } |
191 } | 253 } |
192 | 254 |
193 // With a SES of 48.0, the origin will get a budget of 2.4 per day, but the | 255 // 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 | 256 // old budget will also decay. At the end, we expect the budget to be lower |
195 // than the starting budget. | 257 // than the starting budget. |
196 EXPECT_GT(budget, 0.0); | 258 EXPECT_GT(budget_, 0.0); |
197 EXPECT_LT(budget, kTestBudget); | 259 EXPECT_LT(budget_, kTestBudget); |
198 } | 260 } |
199 | 261 |
200 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { | 262 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) { |
201 const GURL origin(kTestOrigin); | 263 const GURL origin(kTestOrigin); |
202 | 264 |
203 // Set a starting SES for the url. | 265 // Set a starting SES for the url. |
204 SetSiteEngagementScore(origin, kTestSES); | 266 SetSiteEngagementScore(origin, kTestSES); |
205 | 267 |
206 // Set a badly formatted budget in the user preferences. | 268 // Set a badly formatted budget in the user preferences. |
207 DictionaryPrefUpdate update(profile()->GetPrefs(), | 269 DictionaryPrefUpdate update(profile()->GetPrefs(), |
208 prefs::kBackgroundBudgetMap); | 270 prefs::kBackgroundBudgetMap); |
209 base::DictionaryValue* update_map = update.Get(); | 271 base::DictionaryValue* update_map = update.Get(); |
210 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); | 272 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0"); |
211 | 273 |
212 // Get the budget, expect that it will return SES. | 274 // Get the budget, expect that it will return SES. |
213 double budget = GetService()->GetBudget(origin); | 275 double budget = GetBudget(); |
214 | |
215 EXPECT_DOUBLE_EQ(budget, kTestSES); | 276 EXPECT_DOUBLE_EQ(budget, kTestSES); |
216 } | 277 } |
217 | 278 |
218 TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) { | 279 TEST_F(BackgroundBudgetServiceTest, GetBudgetNegativeTime) { |
219 // Manually construct a BackgroundBudgetService with a clock that the test | 280 // Manually construct a BackgroundBudgetService with a clock that the test |
220 // can control so that we can fast forward in time. | 281 // can control so that we can fast forward in time. |
221 BackgroundBudgetService* service = GetService(); | 282 BackgroundBudgetService* service = GetService(); |
222 base::SimpleTestClock* clock = SetClockForTesting(); | 283 base::SimpleTestClock* clock = SetClockForTesting(); |
223 base::Time starting_time = clock->Now(); | 284 base::Time starting_time = clock->Now(); |
224 | 285 |
225 // Set initial SES and budget values. | 286 // Set initial SES and budget values. |
226 const GURL origin(kTestOrigin); | 287 const GURL origin(kTestOrigin); |
227 SetSiteEngagementScore(origin, kTestSES); | 288 SetSiteEngagementScore(origin, kTestSES); |
228 service->StoreBudget(origin, kTestBudget); | 289 std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop()); |
290 service->StoreBudget(origin, kTestBudget, run_loop->QuitClosure()); | |
291 run_loop->Run(); | |
229 | 292 |
230 // Move time forward an hour and get the budget. | 293 // Move time forward an hour and get the budget. |
231 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); | 294 clock->SetNow(starting_time + base::TimeDelta::FromHours(1)); |
232 double budget = service->GetBudget(origin); | 295 run_loop.reset(new base::RunLoop()); |
233 service->StoreBudget(origin, budget); | 296 GetService()->GetBudget( |
234 EXPECT_NE(kTestBudget, budget); | 297 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, |
298 base::Unretained(this), run_loop->QuitClosure())); | |
299 run_loop->Run(); | |
300 | |
301 // Store the updated budget. | |
302 run_loop.reset(new base::RunLoop()); | |
303 service->StoreBudget(origin, budget_, run_loop->QuitClosure()); | |
304 run_loop->Run(); | |
305 EXPECT_NE(kTestBudget, budget_); | |
306 double original_budget = budget_; | |
235 | 307 |
236 // Now move time backwards a day and make sure that the current | 308 // Now move time backwards a day and make sure that the current |
237 // budget matches the budget of the most foward time. | 309 // budget matches the budget of the most foward time. |
238 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); | 310 clock->SetNow(starting_time - base::TimeDelta::FromDays(1)); |
239 double back_budget = service->GetBudget(origin); | 311 run_loop.reset(new base::RunLoop()); |
240 EXPECT_NEAR(budget, back_budget, 0.01); | 312 GetService()->GetBudget( |
313 origin, base::Bind(&BackgroundBudgetServiceTest::GotBudget, | |
314 base::Unretained(this), run_loop->QuitClosure())); | |
315 run_loop->Run(); | |
316 EXPECT_NEAR(original_budget, budget_, 0.01); | |
241 } | 317 } |
OLD | NEW |