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 "components/previews/core/previews_black_list.h" | 5 #include "components/previews/core/previews_black_list.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/test/histogram_tester.h" |
17 #include "base/test/simple_test_clock.h" | 18 #include "base/test/simple_test_clock.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "components/previews/core/previews_black_list_item.h" | 21 #include "components/previews/core/previews_black_list_item.h" |
21 #include "components/previews/core/previews_experiments.h" | 22 #include "components/previews/core/previews_experiments.h" |
22 #include "components/previews/core/previews_opt_out_store.h" | 23 #include "components/previews/core/previews_opt_out_store.h" |
23 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
26 | 27 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 params) && | 108 params) && |
108 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 109 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
109 | 110 |
110 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 111 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
111 base::Time start = test_clock->Now(); | 112 base::Time start = test_clock->Now(); |
112 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 113 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
113 | 114 |
114 std::unique_ptr<PreviewsBlackList> black_list( | 115 std::unique_ptr<PreviewsBlackList> black_list( |
115 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | 116 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); |
116 | 117 |
117 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 118 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
118 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 119 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 120 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 121 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
119 | 122 |
120 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | 123 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
121 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 124 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
122 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | 125 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
123 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 126 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
124 | 127 |
125 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 128 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
126 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 129 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 130 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 131 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
127 | 132 |
128 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 133 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
129 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 134 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
130 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 135 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
131 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 136 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
132 | 137 |
133 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 138 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
134 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 139 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 140 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
| 141 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
135 | 142 |
136 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 143 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
137 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 144 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
138 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 145 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
139 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 146 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
140 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 147 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
141 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 148 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
142 | 149 |
143 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 150 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
144 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 151 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 152 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 153 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
145 | 154 |
146 black_list->ClearBlackList(start, test_clock->Now()); | 155 black_list->ClearBlackList(start, test_clock->Now()); |
147 | 156 |
148 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 157 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
149 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 158 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 159 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 160 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
150 | 161 |
151 variations::testing::ClearAllVariationParams(); | 162 variations::testing::ClearAllVariationParams(); |
152 } | 163 } |
153 | 164 |
154 TEST_F(PreviewsBlackListTest, PerHostBlackListWithStore) { | 165 TEST_F(PreviewsBlackListTest, PerHostBlackListWithStore) { |
155 // Tests the black list behavior when a non-null OptOutSture is passed in. | 166 // Tests the black list behavior when a non-null OptOutSture is passed in. |
156 const GURL url_a1("http://www.url_a.com/a1"); | 167 const GURL url_a1("http://www.url_a.com/a1"); |
157 const GURL url_a2("http://www.url_a.com/a2"); | 168 const GURL url_a2("http://www.url_a.com/a2"); |
158 const GURL url_b("http://www.url_b.com"); | 169 const GURL url_b("http://www.url_b.com"); |
159 const size_t per_host_history = 4; | 170 const size_t per_host_history = 4; |
(...skipping 27 matching lines...) Expand all Loading... |
187 | 198 |
188 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 199 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
189 base::Time start = test_clock->Now(); | 200 base::Time start = test_clock->Now(); |
190 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 201 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
191 | 202 |
192 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | 203 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); |
193 | 204 |
194 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | 205 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( |
195 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | 206 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); |
196 | 207 |
197 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 208 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
198 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 209 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
199 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 210 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
| 211 black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 212 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
| 213 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
200 | 214 |
201 base::RunLoop().RunUntilIdle(); | 215 base::RunLoop().RunUntilIdle(); |
202 | 216 |
203 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 217 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
204 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 218 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
205 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 219 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 220 black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 221 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 222 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
206 | 223 |
207 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); | 224 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
208 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 225 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
209 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); | 226 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
210 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 227 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
211 | 228 |
212 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 229 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
213 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 230 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
214 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 231 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
| 232 black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 233 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 234 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
215 | 235 |
216 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 236 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
217 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 237 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
218 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 238 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
219 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 239 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
220 | 240 |
221 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 241 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
222 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 242 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
223 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 243 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
| 244 black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 245 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
| 246 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
224 | 247 |
225 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 248 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
226 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 249 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
227 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 250 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
228 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 251 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
229 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 252 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
230 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 253 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
231 | 254 |
232 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 255 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
233 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 256 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
234 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 257 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
| 258 black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 259 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 260 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
235 | 261 |
236 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); | 262 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); |
237 black_list->ClearBlackList(start, base::Time::Now()); | 263 black_list->ClearBlackList(start, base::Time::Now()); |
238 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 264 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
239 | 265 |
240 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 266 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
241 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 267 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
242 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 268 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
| 269 black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 270 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
| 271 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
243 | 272 |
244 base::RunLoop().RunUntilIdle(); | 273 base::RunLoop().RunUntilIdle(); |
245 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 274 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
246 | 275 |
247 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 276 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
248 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 277 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
249 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 278 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 279 black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
| 280 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 281 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
250 | 282 |
251 variations::testing::ClearAllVariationParams(); | 283 variations::testing::ClearAllVariationParams(); |
252 } | 284 } |
253 | 285 |
254 TEST_F(PreviewsBlackListTest, HostIndifferentBlackList) { | 286 TEST_F(PreviewsBlackListTest, HostIndifferentBlackList) { |
255 // Tests the black list behavior when a null OptOutSture is passed in. | 287 // Tests the black list behavior when a null OptOutSture is passed in. |
256 const GURL urls[] = { | 288 const GURL urls[] = { |
257 GURL("http://www.url_0.com"), GURL("http://www.url_1.com"), | 289 GURL("http://www.url_0.com"), GURL("http://www.url_1.com"), |
258 GURL("http://www.url_2.com"), GURL("http://www.url_3.com"), | 290 GURL("http://www.url_2.com"), GURL("http://www.url_3.com"), |
259 }; | 291 }; |
(...skipping 22 matching lines...) Expand all Loading... |
282 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 314 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
283 params) && | 315 params) && |
284 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 316 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
285 | 317 |
286 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 318 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
287 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 319 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
288 | 320 |
289 std::unique_ptr<PreviewsBlackList> black_list( | 321 std::unique_ptr<PreviewsBlackList> black_list( |
290 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | 322 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); |
291 | 323 |
292 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); | 324 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
293 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); | 325 black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); |
294 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); | 326 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
295 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); | 327 black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); |
| 328 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 329 black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); |
| 330 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 331 black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); |
296 | 332 |
297 for (size_t i = 0; i < host_indifferent_threshold; i++) { | 333 for (size_t i = 0; i < host_indifferent_threshold; i++) { |
298 black_list->AddPreviewNavigation(urls[i], true, PreviewsType::OFFLINE); | 334 black_list->AddPreviewNavigation(urls[i], true, PreviewsType::OFFLINE); |
299 EXPECT_EQ(i != 3, | 335 EXPECT_EQ(i != 3 ? PreviewsEligibilityReason::ALLOWED |
| 336 : PreviewsEligibilityReason::USER_BLACKLISTED, |
300 black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); | 337 black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); |
301 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 338 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
302 } | 339 } |
303 | 340 |
304 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); | 341 EXPECT_EQ(PreviewsEligibilityReason::USER_BLACKLISTED, |
305 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); | 342 black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); |
306 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); | 343 EXPECT_EQ(PreviewsEligibilityReason::USER_BLACKLISTED, |
307 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); | 344 black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); |
| 345 EXPECT_EQ(PreviewsEligibilityReason::USER_BLACKLISTED, |
| 346 black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); |
| 347 EXPECT_EQ(PreviewsEligibilityReason::USER_BLACKLISTED, |
| 348 black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); |
308 | 349 |
309 black_list->AddPreviewNavigation(urls[3], false, PreviewsType::OFFLINE); | 350 black_list->AddPreviewNavigation(urls[3], false, PreviewsType::OFFLINE); |
310 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 351 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
311 | 352 |
312 // New non-opt-out entry will cause these to be allowed now. | 353 // New non-opt-out entry will cause these to be allowed now. |
313 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); | 354 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
314 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); | 355 black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); |
315 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); | 356 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
316 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); | 357 black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); |
| 358 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 359 black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); |
| 360 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 361 black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); |
317 | 362 |
318 variations::testing::ClearAllVariationParams(); | 363 variations::testing::ClearAllVariationParams(); |
319 } | 364 } |
320 | 365 |
321 TEST_F(PreviewsBlackListTest, QueueBehavior) { | 366 TEST_F(PreviewsBlackListTest, QueueBehavior) { |
322 // Tests the black list asynchronous queue behavior. Methods called while | 367 // Tests the black list asynchronous queue behavior. Methods called while |
323 // loading the opt-out store are queued and should run in the order they were | 368 // loading the opt-out store are queued and should run in the order they were |
324 // queued. | 369 // queued. |
325 const GURL url("http://www.url.com"); | 370 const GURL url("http://www.url.com"); |
326 const GURL url2("http://www.url2.com"); | 371 const GURL url2("http://www.url2.com"); |
(...skipping 25 matching lines...) Expand all Loading... |
352 | 397 |
353 for (auto opt_out : test_opt_out) { | 398 for (auto opt_out : test_opt_out) { |
354 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 399 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
355 base::Time start = test_clock->Now(); | 400 base::Time start = test_clock->Now(); |
356 | 401 |
357 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | 402 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); |
358 | 403 |
359 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | 404 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( |
360 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | 405 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); |
361 | 406 |
362 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 407 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
| 408 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
363 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 409 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
364 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 410 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
365 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 411 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
366 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 412 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
367 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 413 EXPECT_EQ(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED, |
| 414 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
368 base::RunLoop().RunUntilIdle(); | 415 base::RunLoop().RunUntilIdle(); |
369 EXPECT_EQ(!opt_out, | 416 EXPECT_EQ(opt_out ? PreviewsEligibilityReason::HOST_BLACKLISTED |
| 417 : PreviewsEligibilityReason::ALLOWED, |
370 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 418 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
371 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 419 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
372 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 420 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
373 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 421 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
374 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 422 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
375 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); | 423 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); |
376 black_list->ClearBlackList( | 424 black_list->ClearBlackList( |
377 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); | 425 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); |
378 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 426 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
379 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 427 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
380 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 428 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
381 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 429 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
382 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 430 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
383 base::RunLoop().RunUntilIdle(); | 431 base::RunLoop().RunUntilIdle(); |
384 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 432 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
385 | 433 |
386 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 434 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
387 EXPECT_EQ(!opt_out, | 435 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
| 436 EXPECT_EQ(opt_out ? PreviewsEligibilityReason::HOST_BLACKLISTED |
| 437 : PreviewsEligibilityReason::ALLOWED, |
388 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); | 438 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); |
389 } | 439 } |
390 | 440 |
391 variations::testing::ClearAllVariationParams(); | 441 variations::testing::ClearAllVariationParams(); |
392 } | 442 } |
393 | 443 |
394 TEST_F(PreviewsBlackListTest, MaxHosts) { | 444 TEST_F(PreviewsBlackListTest, MaxHosts) { |
395 // Test that the black list only stores n hosts, and it stores the correct n | 445 // Test that the black list only stores n hosts, and it stores the correct n |
396 // hosts. | 446 // hosts. |
397 const GURL url_a("http://www.url_a.com"); | 447 const GURL url_a("http://www.url_a.com"); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 | 485 |
436 std::unique_ptr<PreviewsBlackList> black_list( | 486 std::unique_ptr<PreviewsBlackList> black_list( |
437 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | 487 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); |
438 | 488 |
439 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | 489 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
440 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 490 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
441 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 491 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
442 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 492 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
443 black_list->AddPreviewNavigation(url_c, false, PreviewsType::OFFLINE); | 493 black_list->AddPreviewNavigation(url_c, false, PreviewsType::OFFLINE); |
444 // url_a should stay in the map, since it has an opt out time. | 494 // url_a should stay in the map, since it has an opt out time. |
445 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 495 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
446 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 496 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
447 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | 497 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 498 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 499 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 500 black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); |
448 | 501 |
449 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 502 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
450 black_list->AddPreviewNavigation(url_d, true, PreviewsType::OFFLINE); | 503 black_list->AddPreviewNavigation(url_d, true, PreviewsType::OFFLINE); |
451 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 504 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
452 black_list->AddPreviewNavigation(url_e, true, PreviewsType::OFFLINE); | 505 black_list->AddPreviewNavigation(url_e, true, PreviewsType::OFFLINE); |
453 // url_d and url_e should remain in the map, but url_a should be evicted. | 506 // url_d and url_e should remain in the map, but url_a should be evicted. |
454 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 507 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
455 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | 508 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
456 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_e, PreviewsType::OFFLINE)); | 509 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
| 510 black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); |
| 511 EXPECT_EQ(PreviewsEligibilityReason::HOST_BLACKLISTED, |
| 512 black_list->IsLoadedAndAllowed(url_e, PreviewsType::OFFLINE)); |
457 | 513 |
458 variations::testing::ClearAllVariationParams(); | 514 variations::testing::ClearAllVariationParams(); |
459 } | 515 } |
460 | 516 |
461 TEST_F(PreviewsBlackListTest, SingleOptOut) { | 517 TEST_F(PreviewsBlackListTest, SingleOptOut) { |
462 // Test that when a user opts out of a preview, previews won't be shown until | 518 // Test that when a user opts out of a preview, previews won't be shown until |
463 // |single_opt_out_duration| has elapsed. | 519 // |single_opt_out_duration| has elapsed. |
464 const GURL url_a("http://www.url_a.com"); | 520 const GURL url_a("http://www.url_a.com"); |
465 const GURL url_b("http://www.url_b.com"); | 521 const GURL url_b("http://www.url_b.com"); |
466 const GURL url_c("http://www.url_c.com"); | 522 const GURL url_c("http://www.url_c.com"); |
(...skipping 25 matching lines...) Expand all Loading... |
492 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 548 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
493 params) && | 549 params) && |
494 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 550 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
495 | 551 |
496 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 552 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
497 | 553 |
498 std::unique_ptr<PreviewsBlackList> black_list( | 554 std::unique_ptr<PreviewsBlackList> black_list( |
499 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | 555 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); |
500 | 556 |
501 black_list->AddPreviewNavigation(url_a, false, PreviewsType::OFFLINE); | 557 black_list->AddPreviewNavigation(url_a, false, PreviewsType::OFFLINE); |
502 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 558 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
503 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | 559 black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 560 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 561 black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); |
504 | 562 |
505 test_clock->Advance( | 563 test_clock->Advance( |
506 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); | 564 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); |
507 | 565 |
508 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 566 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
509 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 567 EXPECT_EQ(PreviewsEligibilityReason::USER_RECENTLY_OPTED_OUT, |
510 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | 568 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 569 EXPECT_EQ(PreviewsEligibilityReason::USER_RECENTLY_OPTED_OUT, |
| 570 black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); |
511 | 571 |
512 test_clock->Advance( | 572 test_clock->Advance( |
513 base::TimeDelta::FromSeconds(single_opt_out_duration - 1)); | 573 base::TimeDelta::FromSeconds(single_opt_out_duration - 1)); |
514 | 574 |
515 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 575 EXPECT_EQ(PreviewsEligibilityReason::USER_RECENTLY_OPTED_OUT, |
516 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | 576 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 577 EXPECT_EQ(PreviewsEligibilityReason::USER_RECENTLY_OPTED_OUT, |
| 578 black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); |
517 | 579 |
518 test_clock->Advance( | 580 test_clock->Advance( |
519 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); | 581 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); |
520 | 582 |
521 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 583 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
522 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | 584 black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 585 EXPECT_EQ(PreviewsEligibilityReason::ALLOWED, |
| 586 black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); |
523 | 587 |
524 variations::testing::ClearAllVariationParams(); | 588 variations::testing::ClearAllVariationParams(); |
525 } | 589 } |
526 | 590 |
| 591 TEST_F(PreviewsBlackListTest, AddPreviewUMA) { |
| 592 base::HistogramTester histogram_tester; |
| 593 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
| 594 const GURL url("http://www.url.com"); |
| 595 std::unique_ptr<PreviewsBlackList> black_list( |
| 596 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); |
| 597 |
| 598 black_list->AddPreviewNavigation(url, false, PreviewsType::OFFLINE); |
| 599 histogram_tester.ExpectUniqueSample("Previews.OptOut.UserOptedOut.Offline", 0, |
| 600 1); |
| 601 black_list->AddPreviewNavigation(url, true, PreviewsType::OFFLINE); |
| 602 histogram_tester.ExpectBucketCount("Previews.OptOut.UserOptedOut.Offline", 1, |
| 603 1); |
| 604 } |
| 605 |
527 } // namespace previews | 606 } // namespace previews |
OLD | NEW |