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

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

Issue 1887623002: Replace the 1 in 10 grace period with an accumulating budget based on SES. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Using Remove* instead of Set* Created 4 years, 7 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"
9 #include "base/test/simple_test_clock.h"
10 #include "chrome/browser/engagement/site_engagement_service.h"
8 #include "chrome/browser/push_messaging/background_budget_service.h" 11 #include "chrome/browser/push_messaging/background_budget_service.h"
9 #include "chrome/browser/push_messaging/background_budget_service_factory.h" 12 #include "chrome/browser/push_messaging/background_budget_service_factory.h"
13 #include "chrome/common/pref_names.h"
10 #include "chrome/test/base/testing_profile.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"
11 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
13 19
14 namespace { 20 namespace {
15 21
16 const char kTestOrigin[] = "https://example.com"; 22 const char kTestOrigin[] = "https://example.com";
17 const char kTestData[] = "1111101111"; 23 const double kTestBudget = 10.0;
24 const double kTestSES = 24.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;
18 29
19 } // namespace 30 } // namespace
20 31
21 class BackgroundBudgetServiceTest : public testing::Test { 32 class BackgroundBudgetServiceTest : public testing::Test {
22 public: 33 public:
23 BackgroundBudgetServiceTest() {} 34 BackgroundBudgetServiceTest() {}
24 ~BackgroundBudgetServiceTest() override {} 35 ~BackgroundBudgetServiceTest() override {}
25 36
26 BackgroundBudgetService* service() { 37 BackgroundBudgetService* GetService() {
27 return BackgroundBudgetServiceFactory::GetForProfile(&profile_); 38 return BackgroundBudgetServiceFactory::GetForProfile(&profile_);
28 } 39 }
29 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
30 private: 55 private:
31 content::TestBrowserThreadBundle thread_bundle_; 56 content::TestBrowserThreadBundle thread_bundle_;
32 TestingProfile profile_; 57 TestingProfile profile_;
33 }; 58 };
34 59
35 TEST_F(BackgroundBudgetServiceTest, GetBudgetFailure) { 60 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) {
36 const GURL origin(kTestOrigin); 61 const GURL origin(kTestOrigin);
37 62
38 std::string budget_string = service()->GetBudget(origin); 63 double budget = GetService()->GetBudget(origin);
39 64
40 EXPECT_EQ(std::string(), budget_string); 65 EXPECT_DOUBLE_EQ(budget, 0.0);
41 } 66 }
42 67
43 TEST_F(BackgroundBudgetServiceTest, GetBudgetSuccess) { 68 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) {
44 const GURL origin(kTestOrigin); 69 const GURL origin(kTestOrigin);
45 70
46 service()->StoreBudget(origin, kTestData); 71 // Set a starting SES for the url but no stored budget info.
72 SetSiteEngagementScore(origin, kTestSES);
47 73
48 std::string budget_string = service()->GetBudget(origin); 74 double budget = GetService()->GetBudget(origin);
49 75
50 EXPECT_EQ(kTestData, budget_string); 76 EXPECT_DOUBLE_EQ(budget, kTestSES);
51 } 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 service->StoreBudget(origin, budget - 1.0);
188 }
189 }
190
191 // With a SES of 24.0, the origin will get a budget of 2.4 per day, but the
192 // old budget will also decay. At the end, we expect the budget to be lower
193 // than the starting budget.
194 EXPECT_GT(budget, 0.0);
195 EXPECT_LT(budget, kTestBudget);
196 }
197
198 TEST_F(BackgroundBudgetServiceTest, GetBudgetInvalidBudget) {
199 const GURL origin(kTestOrigin);
200
201 // Set a starting SES for the url.
202 SetSiteEngagementScore(origin, kTestSES);
203
204 // Set a badly formatted budget in the user preferences.
205 DictionaryPrefUpdate update(profile()->GetPrefs(),
206 prefs::kBackgroundBudgetMap);
207 base::DictionaryValue* update_map = update.Get();
208 update_map->SetStringWithoutPathExpansion(origin.spec(), "20#2.0");
209
210 // Get the budget, expect that it will return SES.
211 double budget = GetService()->GetBudget(origin);
212
213 EXPECT_DOUBLE_EQ(budget, kTestSES);
214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698