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