OLD | NEW |
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" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 private: | 108 private: |
109 content::TestBrowserThreadBundle thread_bundle_; | 109 content::TestBrowserThreadBundle thread_bundle_; |
110 TestingProfile profile_; | 110 TestingProfile profile_; |
111 base::HistogramTester histogram_tester_; | 111 base::HistogramTester histogram_tester_; |
112 url::Origin origin_; | 112 url::Origin origin_; |
113 | 113 |
114 DISALLOW_COPY_AND_ASSIGN(BudgetManagerTest); | 114 DISALLOW_COPY_AND_ASSIGN(BudgetManagerTest); |
115 }; | 115 }; |
116 | 116 |
117 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { | 117 TEST_F(BudgetManagerTest, GetBudgetConsumedOverTime) { |
118 // Set initial SES. The first time we try to spend budget, the | 118 // Set initial SES. The first time we try to spend budget, the engagement |
119 // engagement award will be granted which is 48.0. | 119 // award will be granted which is 23.04. (kTestSES * maxDaily / maxSES) |
120 SetSiteEngagementScore(kTestSES); | 120 SetSiteEngagementScore(kTestSES); |
121 const blink::mojom::BudgetOperationType type = | 121 const blink::mojom::BudgetOperationType type = |
122 blink::mojom::BudgetOperationType::SILENT_PUSH; | 122 blink::mojom::BudgetOperationType::SILENT_PUSH; |
123 | 123 |
124 // Spend for 24 silent push messages. This should consume all the original | 124 // Spend for 11 silent push messages. This should consume all the original |
125 // budget grant. | 125 // budget grant. |
126 for (int i = 0; i < 24; i++) { | 126 for (int i = 0; i < 11; i++) { |
127 ASSERT_TRUE(GetBudget()); | 127 ASSERT_TRUE(GetBudget()); |
128 ASSERT_TRUE(ReserveBudget(type)); | 128 ASSERT_TRUE(ReserveBudget(type)); |
129 } | 129 } |
130 | 130 |
131 // Try to send one final silent push. The origin should be out of budget. | 131 // Try to send one final silent push. The origin should be out of budget. |
132 ASSERT_TRUE(GetBudget()); | 132 ASSERT_TRUE(GetBudget()); |
133 ASSERT_FALSE(ReserveBudget(type)); | 133 ASSERT_FALSE(ReserveBudget(type)); |
134 | 134 |
135 // Try to consume for the 24 messages reserved. | 135 // Try to consume for the 11 messages reserved. |
136 for (int i = 0; i < 24; i++) | 136 for (int i = 0; i < 11; i++) |
137 ASSERT_TRUE(ConsumeBudget(type)); | 137 ASSERT_TRUE(ConsumeBudget(type)); |
138 | 138 |
139 // The next consume should fail, since there is no reservation or budget | 139 // The next consume should fail, since there is no reservation or budget |
140 // available. | 140 // available. |
141 ASSERT_FALSE(ConsumeBudget(type)); | 141 ASSERT_FALSE(ConsumeBudget(type)); |
142 | 142 |
143 // Check the the UMA recorded for the Reserve calls matches the operations | 143 // Check the the UMA recorded for the Reserve calls matches the operations |
144 // that were executed. | 144 // that were executed. |
145 std::vector<base::Bucket> buckets = | 145 std::vector<base::Bucket> buckets = |
146 histogram_tester()->GetAllSamples("Blink.BudgetAPI.Reserve"); | 146 histogram_tester()->GetAllSamples("Blink.BudgetAPI.Reserve"); |
147 ASSERT_EQ(2U, buckets.size()); | 147 ASSERT_EQ(2U, buckets.size()); |
148 // 1 failed reserve call. | 148 // 1 failed reserve call. |
149 EXPECT_EQ(0, buckets[0].min); | 149 EXPECT_EQ(0, buckets[0].min); |
150 EXPECT_EQ(1, buckets[0].count); | 150 EXPECT_EQ(1, buckets[0].count); |
151 // 24 successful reserve calls. | 151 // 11 successful reserve calls. |
152 EXPECT_EQ(1, buckets[1].min); | 152 EXPECT_EQ(1, buckets[1].min); |
153 EXPECT_EQ(24, buckets[1].count); | 153 EXPECT_EQ(11, buckets[1].count); |
154 | 154 |
155 // Check that the UMA recorded for the GetBudget calls matches the operations | 155 // Check that the UMA recorded for the GetBudget calls matches the operations |
156 // that were executed. | 156 // that were executed. |
157 buckets = histogram_tester()->GetAllSamples("Blink.BudgetAPI.QueryBudget"); | 157 buckets = histogram_tester()->GetAllSamples("Blink.BudgetAPI.QueryBudget"); |
158 | 158 |
159 int num_samples = 0; | 159 int num_samples = 0; |
160 for (const base::Bucket& bucket : buckets) | 160 for (const base::Bucket& bucket : buckets) |
161 num_samples += bucket.count; | 161 num_samples += bucket.count; |
162 EXPECT_EQ(25, num_samples); | 162 EXPECT_EQ(12, num_samples); |
163 } | 163 } |
164 | 164 |
165 TEST_F(BudgetManagerTest, TestInsecureOrigin) { | 165 TEST_F(BudgetManagerTest, TestInsecureOrigin) { |
166 const blink::mojom::BudgetOperationType type = | 166 const blink::mojom::BudgetOperationType type = |
167 blink::mojom::BudgetOperationType::SILENT_PUSH; | 167 blink::mojom::BudgetOperationType::SILENT_PUSH; |
168 SetOrigin(url::Origin(GURL("http://example.com"))); | 168 SetOrigin(url::Origin(GURL("http://example.com"))); |
169 SetSiteEngagementScore(kTestSES); | 169 SetSiteEngagementScore(kTestSES); |
170 | 170 |
171 // Methods on the BudgetManager should only be allowed for secure origins. | 171 // Methods on the BudgetManager should only be allowed for secure origins. |
172 ASSERT_FALSE(ReserveBudget(type)); | 172 ASSERT_FALSE(ReserveBudget(type)); |
173 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); | 173 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); |
174 ASSERT_FALSE(ConsumeBudget(type)); | 174 ASSERT_FALSE(ConsumeBudget(type)); |
175 } | 175 } |
176 | 176 |
177 TEST_F(BudgetManagerTest, TestUniqueOrigin) { | 177 TEST_F(BudgetManagerTest, TestUniqueOrigin) { |
178 const blink::mojom::BudgetOperationType type = | 178 const blink::mojom::BudgetOperationType type = |
179 blink::mojom::BudgetOperationType::SILENT_PUSH; | 179 blink::mojom::BudgetOperationType::SILENT_PUSH; |
180 SetOrigin(url::Origin(GURL("file://example.com:443/etc/passwd"))); | 180 SetOrigin(url::Origin(GURL("file://example.com:443/etc/passwd"))); |
181 | 181 |
182 // Methods on the BudgetManager should not be allowed for unique origins. | 182 // Methods on the BudgetManager should not be allowed for unique origins. |
183 ASSERT_FALSE(ReserveBudget(type)); | 183 ASSERT_FALSE(ReserveBudget(type)); |
184 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); | 184 ASSERT_EQ(blink::mojom::BudgetServiceErrorType::NOT_SUPPORTED, error_); |
185 ASSERT_FALSE(ConsumeBudget(type)); | 185 ASSERT_FALSE(ConsumeBudget(type)); |
186 } | 186 } |
OLD | NEW |