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

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: Integrated code review comments and refactored service constructors. 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"
10 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
13 16
14 namespace { 17 namespace {
15 18
16 const char kTestOrigin[] = "https://example.com"; 19 const char kTestOrigin[] = "https://example.com";
17 const char kTestData[] = "1111101111"; 20 const double kTestBudget = 10.0;
21 const double kTestSES = 24.0;
22 const double kLowSES = 1.0;
23 const double kMaxSES = 100.0;
24 // Mirrors definition in BackgroundBudgetService, this is 10 days of seconds.
25 const double kSecondsToAccumulate = 864000.0;
18 26
19 } // namespace 27 } // namespace
20 28
21 class BackgroundBudgetServiceTest : public testing::Test { 29 class BackgroundBudgetServiceTest : public testing::Test {
22 public: 30 public:
23 BackgroundBudgetServiceTest() {} 31 BackgroundBudgetServiceTest() : clock_(new base::SimpleTestClock()) {}
24 ~BackgroundBudgetServiceTest() override {} 32 ~BackgroundBudgetServiceTest() override {}
25 33
26 BackgroundBudgetService* service() { 34 BackgroundBudgetService* GetService() {
27 return BackgroundBudgetServiceFactory::GetForProfile(&profile_); 35 return BackgroundBudgetServiceFactory::GetForProfile(&profile_);
28 } 36 }
29 37
38 void SetSiteEngagementScore(const GURL& url, double score) {
39 SiteEngagementService* service = SiteEngagementService::Get(&profile_);
40 service->ResetScoreForURL(url, score);
41 }
42
43 Profile* profile() { return &profile_; }
44
45 base::SimpleTestClock* SetClockForTesting() {
46 BackgroundBudgetServiceFactory::GetForProfile(&profile_)->setClock(
47 base::WrapUnique(clock_));
48 return clock_;
49 }
50
30 private: 51 private:
31 content::TestBrowserThreadBundle thread_bundle_; 52 content::TestBrowserThreadBundle thread_bundle_;
32 TestingProfile profile_; 53 TestingProfile profile_;
54 base::SimpleTestClock* clock_;
33 }; 55 };
34 56
35 TEST_F(BackgroundBudgetServiceTest, GetBudgetFailure) { 57 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) {
36 const GURL origin(kTestOrigin); 58 const GURL origin(kTestOrigin);
37 59
38 std::string budget_string = service()->GetBudget(origin); 60 double budget = GetService()->GetBudget(origin);
39 61
40 EXPECT_EQ(std::string(), budget_string); 62 EXPECT_DOUBLE_EQ(budget, 0.0);
41 } 63 }
42 64
43 TEST_F(BackgroundBudgetServiceTest, GetBudgetSuccess) { 65 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) {
44 const GURL origin(kTestOrigin); 66 const GURL origin(kTestOrigin);
45 67
46 service()->StoreBudget(origin, kTestData); 68 // Set a starting SES for the url but no stored budget info.
69 SetSiteEngagementScore(origin, kTestSES);
47 70
48 std::string budget_string = service()->GetBudget(origin); 71 double budget = GetService()->GetBudget(origin);
49 72
50 EXPECT_EQ(kTestData, budget_string); 73 EXPECT_DOUBLE_EQ(budget, kTestSES);
51 } 74 }
75
76 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) {
77 const GURL origin(kTestOrigin);
78
79 std::unique_ptr<BackgroundBudgetService> service(
80 new BackgroundBudgetService(profile()));
81
82 service->StoreBudget(origin, kTestBudget);
83
84 double budget = service->GetBudget(origin);
85
86 EXPECT_NEAR(budget, kTestBudget, kTestBudget);
87 }
88
89 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) {
90 // Manually construct a BackgroundBudgetServie with a clock that the test
91 // can control so that we can fast forward in time.
92 BackgroundBudgetService* service = GetService();
93 base::SimpleTestClock* clock = SetClockForTesting();
94 base::Time starting_time = clock->Now();
95
96 // Set initial SES and budget values.
97 const GURL origin(kTestOrigin);
98 SetSiteEngagementScore(origin, kTestSES);
99 service->StoreBudget(origin, kTestBudget);
100
101 double budget = service->GetBudget(origin);
102 EXPECT_DOUBLE_EQ(budget, kTestBudget);
103
104 // Query for the budget after 1 second has passed.
105 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1));
106 budget = service->GetBudget(origin);
107 EXPECT_NEAR(budget, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate);
108 EXPECT_GT(budget, kTestBudget);
109
110 // Query for the budget after 1 hour has passed.
111 clock->SetNow(starting_time + base::TimeDelta::FromHours(1));
112 budget = service->GetBudget(origin);
113 EXPECT_NEAR(budget, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate);
114 EXPECT_GT(budget, kTestBudget);
115
116 // Query for the budget after 5 days have passed. The budget should be
117 // increasing, but not up the SES score.
118 clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
119 budget = service->GetBudget(origin);
120 EXPECT_GT(budget, kTestBudget);
121 EXPECT_LT(budget, kTestSES);
122 double moderate_ses_budget = budget;
123
124 // Query for the budget after 10 days have passed. By this point, the budget
125 // should converge to the SES score.
126 clock->SetNow(starting_time + base::TimeDelta::FromDays(10));
127 budget = service->GetBudget(origin);
128 EXPECT_DOUBLE_EQ(budget, kTestSES);
129
130 // Now, change the SES score to the maximum amount and reinitialize budget.
131 SetSiteEngagementScore(origin, kMaxSES);
132 service->StoreBudget(origin, kTestBudget);
133 starting_time = clock->Now();
134
135 // Query for the budget after 1 second has passed.
136 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1));
137 budget = service->GetBudget(origin);
138 EXPECT_NEAR(budget, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate);
139
140 // Query for the budget after 5 days have passed. Again, the budget should be
141 // approaching the SES, but not have reached it.
142 clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
143 budget = service->GetBudget(origin);
144 EXPECT_GT(budget, kTestBudget);
145 EXPECT_LT(budget, kMaxSES);
146
147 // The budget after 5 days with max SES should be greater than the budget
148 // after 5 days with moderate SES.
149 EXPECT_GT(budget, moderate_ses_budget);
150
151 // Now, change the SES score to a low amount and reinitialize budget.
152 SetSiteEngagementScore(origin, kLowSES);
153 service->StoreBudget(origin, kTestBudget);
154 starting_time = clock->Now();
155
156 // Query for the budget after 5 days have passed. Again, the budget should be
157 // approaching the SES, this time decreasing, but not have reached it.
158 clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
159 budget = service->GetBudget(origin);
160 EXPECT_LT(budget, kTestBudget);
161 EXPECT_GT(budget, kLowSES);
162 }
163
164 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) {
165 // Manually construct a BackgroundBudgetService with a clock that the test
166 // can control so that we can fast forward in time.
167 BackgroundBudgetService* service = GetService();
168 base::SimpleTestClock* clock = SetClockForTesting();
169
170 // Set initial SES and budget values.
171 const GURL origin(kTestOrigin);
172 SetSiteEngagementScore(origin, kTestSES);
173 service->StoreBudget(origin, kTestBudget);
174 double budget = 0.0;
175
176 // Measure over 200 hours. In each hour a message is received, and for 1 in
177 // 10, budget is consumed.
178 for (int i = 0; i < 200; i++) {
179 // Query for the budget after 1 hour has passed.
180 clock->Advance(base::TimeDelta::FromHours(1));
181 budget = service->GetBudget(origin);
182
183 if (i % 10 == 0) {
184 service->StoreBudget(origin, budget - 1.0);
185 }
186 }
187
188 // With a SES of 24.0, the origin will get a budget of 2.4 per day, but the
189 // old budget will also decay. At the end, we expect the budget to be lower
190 // than the starting budget.
191 EXPECT_GT(budget, 0.0);
192 EXPECT_LT(budget, kTestBudget);
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698