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