Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: chrome/browser/budget_service/background_budget_service_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698