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

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

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

Powered by Google App Engine
This is Rietveld 408576698