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

Side by Side Diff: components/previews/core/previews_black_list_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698