OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/previews/previews_black_list.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "base/time/time.h" |
| 17 #include "components/previews/previews_black_list_item.h" |
| 18 #include "components/previews/previews_experiments.h" |
| 19 #include "components/previews/previews_opt_out_store.h" |
| 20 #include "components/variations/variations_associated_data.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 using PreviewsBlackListTest = testing::Test; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 namespace previews { |
| 31 |
| 32 namespace { |
| 33 |
| 34 void RunLoadCallback(LoadBlackListCallback callback, |
| 35 std::unique_ptr<BlackListItemMap> black_list_item_map) { |
| 36 callback.Run(std::move(black_list_item_map)); |
| 37 } |
| 38 |
| 39 class TestPreviewsOptOutStore : public PreviewsOptOutStore { |
| 40 public: |
| 41 TestPreviewsOptOutStore() {} |
| 42 ~TestPreviewsOptOutStore() override {} |
| 43 |
| 44 private: |
| 45 // PreviewsOptOutStore implementation: |
| 46 void AddPreviewNavigation(bool opt_out, |
| 47 const std::string& host_name, |
| 48 PreviewsType type, |
| 49 const base::Time& now) override {} |
| 50 void LoadBlackList(LoadBlackListCallback callback) override { |
| 51 std::unique_ptr<BlackListItemMap> black_list_item_map( |
| 52 new BlackListItemMap()); |
| 53 base::MessageLoop::current()->task_runner()->PostTask( |
| 54 FROM_HERE, base::Bind(&RunLoadCallback, callback, |
| 55 base::Passed(&black_list_item_map))); |
| 56 } |
| 57 }; |
| 58 |
| 59 } // namespace |
| 60 |
| 61 TEST_F(PreviewsBlackListTest, BlackListNoStore) { |
| 62 const GURL url_a("http://www.url_a.com"); |
| 63 const GURL url_b("http://www.url_b.com"); |
| 64 const size_t history = 4; |
| 65 const int threshold = 2; |
| 66 // Hopefully, this test runs within a calendar year. |
| 67 const int duration_in_seconds = 1 * 365 * 24 * 60 * 60; |
| 68 base::FieldTrialList field_trial_list(nullptr); |
| 69 ASSERT_TRUE( |
| 70 EnableBlackListParamsForTesting(history, threshold, duration_in_seconds)); |
| 71 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList(nullptr)); |
| 72 |
| 73 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 74 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 75 |
| 76 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
| 77 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
| 78 |
| 79 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 80 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 81 |
| 82 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
| 83 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
| 84 |
| 85 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 86 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 87 |
| 88 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
| 89 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
| 90 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
| 91 |
| 92 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
| 93 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 94 |
| 95 variations::testing::ClearAllVariationParams(); |
| 96 } |
| 97 |
| 98 TEST_F(PreviewsBlackListTest, BlackListWithStore) { |
| 99 const GURL url_a1("http://www.url_a.com/a1"); |
| 100 const GURL url_a2("http://www.url_a.com/a2"); |
| 101 const GURL url_b("http://www.url_b.com"); |
| 102 const size_t history = 4; |
| 103 const int threshold = 2; |
| 104 // Hopefully, this test runs within a calendar year. |
| 105 const int duration_in_seconds = 1 * 365 * 24 * 60 * 60; |
| 106 base::FieldTrialList field_trial_list(nullptr); |
| 107 ASSERT_TRUE( |
| 108 EnableBlackListParamsForTesting(history, threshold, duration_in_seconds)); |
| 109 |
| 110 base::MessageLoop loop; |
| 111 |
| 112 std::unique_ptr<PreviewsBlackList> black_list( |
| 113 new PreviewsBlackList(base::MakeUnique<TestPreviewsOptOutStore>())); |
| 114 |
| 115 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
| 116 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 117 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 118 |
| 119 base::RunLoop().RunUntilIdle(); |
| 120 |
| 121 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
| 122 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 123 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 124 |
| 125 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
| 126 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
| 127 |
| 128 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
| 129 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 130 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 131 |
| 132 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
| 133 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
| 134 |
| 135 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
| 136 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 137 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 138 |
| 139 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
| 140 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
| 141 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
| 142 |
| 143 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
| 144 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
| 145 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
| 146 |
| 147 variations::testing::ClearAllVariationParams(); |
| 148 } |
| 149 |
| 150 } // namespace previews |
OLD | NEW |