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 |
| 23 namespace { |
| 24 |
| 25 using PreviewsBlackListTest = testing::Test; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 namespace previews { |
| 30 |
| 31 namespace { |
| 32 |
| 33 void RunLoadCallback(LoadBlackListCallback callback, |
| 34 std::unique_ptr<BlackListItemMap> black_list_item_map) { |
| 35 callback.Run(std::move(black_list_item_map)); |
| 36 } |
| 37 |
| 38 class TestPreviewsOptOutStore : public PreviewsOptOutStore { |
| 39 public: |
| 40 TestPreviewsOptOutStore() {} |
| 41 ~TestPreviewsOptOutStore() override {} |
| 42 |
| 43 private: |
| 44 // PreviewsOptOutStore implementation: |
| 45 void AddPreviewNavigation(bool opt_out, |
| 46 const std::string& host_name, |
| 47 PreviewsType type, |
| 48 const base::Time& now) override {} |
| 49 void LoadBlackList(LoadBlackListCallback callback) override { |
| 50 std::unique_ptr<BlackListItemMap> black_list_item_map( |
| 51 new BlackListItemMap()); |
| 52 base::MessageLoop::current()->task_runner()->PostTask( |
| 53 FROM_HERE, base::Bind(&RunLoadCallback, callback, |
| 54 base::Passed(&black_list_item_map))); |
| 55 } |
| 56 void ClearBlackList(const base::Time& begin_time, |
| 57 const base::Time& end_time, |
| 58 LoadBlackListCallback callback) override { |
| 59 std::unique_ptr<BlackListItemMap> black_list_item_map( |
| 60 new BlackListItemMap()); |
| 61 base::MessageLoop::current()->task_runner()->PostTask( |
| 62 FROM_HERE, base::Bind(&RunLoadCallback, callback, |
| 63 base::Passed(&black_list_item_map))); |
| 64 } |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 TEST_F(PreviewsBlackListTest, BlackListNoStore) { |
| 70 const std::string host_a = "host_a.com"; |
| 71 const std::string host_b = "host_b.com"; |
| 72 const int history = 4; |
| 73 const int threshold = 2; |
| 74 base::Time start = base::Time::Now(); |
| 75 // Hopefully, this test runs within a calendar year. |
| 76 const int duration_in_seconds = 1 * 365 * 24 * 60 * 60; |
| 77 base::FieldTrialList field_trial_list(nullptr); |
| 78 ASSERT_TRUE( |
| 79 EnableBlackListParamsForTesting(history, threshold, duration_in_seconds)); |
| 80 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList(nullptr)); |
| 81 |
| 82 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a)); |
| 83 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 84 |
| 85 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 86 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 87 |
| 88 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 89 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 90 |
| 91 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 92 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 93 |
| 94 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 95 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b)); |
| 96 |
| 97 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 98 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 99 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 100 |
| 101 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 102 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 103 |
| 104 black_list->ClearBlackList(start, base::Time::Now()); |
| 105 |
| 106 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a)); |
| 107 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 108 variations::testing::ClearAllVariationParams(); |
| 109 } |
| 110 |
| 111 TEST_F(PreviewsBlackListTest, BlackListWithStore) { |
| 112 const std::string host_a = "host_a.com"; |
| 113 const std::string host_b = "host_b.com"; |
| 114 const int history = 4; |
| 115 const int threshold = 2; |
| 116 base::Time start = base::Time::Now(); |
| 117 // Hopefully, this test runs within a calendar year. |
| 118 const int duration_in_seconds = 1 * 365 * 24 * 60 * 60; |
| 119 base::FieldTrialList field_trial_list(nullptr); |
| 120 ASSERT_TRUE( |
| 121 EnableBlackListParamsForTesting(history, threshold, duration_in_seconds)); |
| 122 |
| 123 base::MessageLoop loop; |
| 124 |
| 125 std::unique_ptr<PreviewsBlackList> black_list( |
| 126 new PreviewsBlackList(base::MakeUnique<TestPreviewsOptOutStore>())); |
| 127 |
| 128 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 129 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b)); |
| 130 |
| 131 base::RunLoop().RunUntilIdle(); |
| 132 |
| 133 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a)); |
| 134 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 135 |
| 136 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 137 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 138 |
| 139 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 140 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 141 |
| 142 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 143 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 144 |
| 145 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 146 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b)); |
| 147 |
| 148 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 149 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 150 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 151 |
| 152 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 153 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 154 |
| 155 black_list->ClearBlackList(start, base::Time::Now()); |
| 156 |
| 157 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 158 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b)); |
| 159 |
| 160 base::RunLoop().RunUntilIdle(); |
| 161 |
| 162 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a)); |
| 163 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 164 variations::testing::ClearAllVariationParams(); |
| 165 } |
| 166 |
| 167 } // namespace previews |
OLD | NEW |