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

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 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() {}
24 ~BackgroundBudgetServiceTest() override {} 32 ~BackgroundBudgetServiceTest() override {}
25 33
26 BackgroundBudgetService* service() { 34 BackgroundBudgetService* service() {
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
30 private: 45 private:
31 content::TestBrowserThreadBundle thread_bundle_; 46 content::TestBrowserThreadBundle thread_bundle_;
32 TestingProfile profile_; 47 TestingProfile profile_;
33 }; 48 };
34 49
35 TEST_F(BackgroundBudgetServiceTest, GetBudgetFailure) { 50 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetOrSES) {
36 const GURL origin(kTestOrigin); 51 const GURL origin(kTestOrigin);
37 52
38 std::string budget_string = service()->GetBudget(origin); 53 double budget = service()->GetBudget(origin);
39 54
40 EXPECT_EQ(std::string(), budget_string); 55 EXPECT_DOUBLE_EQ(budget, 0.0);
41 } 56 }
42 57
43 TEST_F(BackgroundBudgetServiceTest, GetBudgetSuccess) { 58 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoBudgetSESExists) {
44 const GURL origin(kTestOrigin); 59 const GURL origin(kTestOrigin);
45 60
46 service()->StoreBudget(origin, kTestData); 61 // Set a starting SES for the url but no stored budget info.
62 SetSiteEngagementScore(origin, kTestSES);
47 63
48 std::string budget_string = service()->GetBudget(origin); 64 double budget = service()->GetBudget(origin);
49 65
50 EXPECT_EQ(kTestData, budget_string); 66 EXPECT_DOUBLE_EQ(budget, kTestSES);
51 } 67 }
68
69 TEST_F(BackgroundBudgetServiceTest, GetBudgetNoElapsedTime) {
70 const GURL origin(kTestOrigin);
71
72 // Manually construct a BackgroundBudgetServie with a clock that the test
Peter Beverloo 2016/05/04 15:41:52 nit: s/BackgroundBudgetServie/BackgroundBudgetServ
harkness 2016/05/09 15:33:27 Done.
73 // can control so that we can fast forward in time.
74 base::SimpleTestClock* clock = new base::SimpleTestClock();
75 std::unique_ptr<BackgroundBudgetService> service(
76 new BackgroundBudgetService(profile(), base::WrapUnique(clock)));
77
78 service->StoreBudget(origin, kTestBudget);
79
80 double budget = service->GetBudget(origin);
81
82 EXPECT_NEAR(budget, kTestBudget, kTestBudget);
83 }
84
85 TEST_F(BackgroundBudgetServiceTest, GetBudgetElapsedTime) {
86 // Manually construct a BackgroundBudgetServie with a clock that the test
87 // can control so that we can fast forward in time.
88 base::SimpleTestClock* clock = new base::SimpleTestClock();
89 base::Time starting_time = clock->Now();
90 std::unique_ptr<BackgroundBudgetService> service(
91 new BackgroundBudgetService(profile(), base::WrapUnique(clock)));
92
93 // Set initial SES and budget values.
94 const GURL origin(kTestOrigin);
95 SetSiteEngagementScore(origin, kTestSES);
96 service->StoreBudget(origin, kTestBudget);
97
98 double budget = service->GetBudget(origin);
99 EXPECT_DOUBLE_EQ(budget, kTestBudget);
100
101 // Query for the budget after 1 second has passed.
102 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1));
103 budget = service->GetBudget(origin);
104 EXPECT_NEAR(budget, kTestBudget, kTestSES * 1.0 / kSecondsToAccumulate);
Peter Beverloo 2016/05/04 15:41:52 Heh, you're using EXPECT_NEAR as sort of a EXPECT_
harkness 2016/05/09 15:33:27 I added extra GT checks to avoid that case.
105
106 // Query for the budget after 1 hour has passed.
107 clock->SetNow(starting_time + base::TimeDelta::FromHours(1));
108 budget = service->GetBudget(origin);
109 EXPECT_NEAR(budget, kTestBudget, kTestSES * 3600.0 / kSecondsToAccumulate);
110
111 // Query for the budget after 5 days have passed. The budget should be
112 // increasing, but not up the SES score.
113 clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
114 budget = service->GetBudget(origin);
115 EXPECT_GT(budget, kTestBudget);
116 EXPECT_LT(budget, kTestSES);
117 double moderate_ses_budget = budget;
118
119 // Query for the budget after 11 days have passed. By this point, the budget
120 // should converge to the SES score.
121 clock->SetNow(starting_time + base::TimeDelta::FromDays(11));
Peter Beverloo 2016/05/04 15:41:52 Why 11 days instead of 10? (kSecondsToAccumulate)
harkness 2016/05/09 15:33:27 Originally, I was using Advance() instead of SetNo
122 budget = service->GetBudget(origin);
123 EXPECT_DOUBLE_EQ(budget, kTestSES);
124
125 // Now, change the SES score to the maximum amount and reinitialize budget.
126 SetSiteEngagementScore(origin, kMaxSES);
127 service->StoreBudget(origin, kTestBudget);
128 starting_time = clock->Now();
129
130 // Query for the budget after 1 second has passed.
131 clock->SetNow(starting_time + base::TimeDelta::FromSeconds(1));
132 budget = service->GetBudget(origin);
133 EXPECT_NEAR(budget, kTestBudget, kMaxSES * 1.0 / kSecondsToAccumulate);
134
135 // Query for the budget after 5 days have passed. Again, the budget should be
136 // approaching the SES, but not have reached it.
137 clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
138 budget = service->GetBudget(origin);
139 EXPECT_GT(budget, kTestBudget);
140 EXPECT_LT(budget, kMaxSES);
141
142 // The budget after 5 days with max SES should be greater than the budget
143 // after 5 days with moderate SES.
144 EXPECT_GT(budget, moderate_ses_budget);
145
146 // Now, change the SES score to a low amount and reinitialize budget.
147 SetSiteEngagementScore(origin, kLowSES);
148 service->StoreBudget(origin, kTestBudget);
149 starting_time = clock->Now();
150
151 // Query for the budget after 5 days have passed. Again, the budget should be
152 // approaching the SES, this time decreasing, but not have reached it.
153 clock->SetNow(starting_time + base::TimeDelta::FromDays(5));
154 budget = service->GetBudget(origin);
155 EXPECT_LT(budget, kTestBudget);
156 EXPECT_GT(budget, kLowSES);
157 }
158
159 TEST_F(BackgroundBudgetServiceTest, GetBudgetConsumedOverTime) {
160 // Manually construct a BackgroundBudgetServie with a clock that the test
161 // can control so that we can fast forward in time.
162 base::SimpleTestClock* clock = new base::SimpleTestClock();
163 std::unique_ptr<BackgroundBudgetService> service(
164 new BackgroundBudgetService(profile(), base::WrapUnique(clock)));
165
166 // Set initial SES and budget values.
167 const GURL origin(kTestOrigin);
168 SetSiteEngagementScore(origin, kTestSES);
169 service->StoreBudget(origin, kTestBudget);
170 double budget = 0.0;
171
172 // Measure over 200 hours. In each hour a message is received, and for 1 in
173 // 10, budget is consumed.
174 for (int i = 0; i < 200; i++) {
175 // Query for the budget after 1 hour has passed.
176 clock->Advance(base::TimeDelta::FromHours(1));
177 budget = service->GetBudget(origin);
178
179 if (i % 10 == 0) {
180 service->StoreBudget(origin, budget - 1.0);
181 }
182 }
183
184 // With a SES of 24.0, the origin will get a budget of 2.4 per day, but the
185 // old budget will also decay. At the end, we expect the budget to be lower
186 // than the starting budget.
Peter Beverloo 2016/05/04 15:41:52 We should definitely measure the site's budget for
harkness 2016/05/09 15:33:27 Do you want to measure for every message, or only
Peter Beverloo 2016/05/09 17:12:06 Since it's all synchronous, I think we can just qu
187 EXPECT_GT(budget, 0.0);
188 EXPECT_LT(budget, kTestBudget);
189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698