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