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 }; |
| 57 |
| 58 } // namespace |
| 59 |
| 60 TEST_F(PreviewsBlackListTest, BlackListNoStore) { |
| 61 const std::string host_a = "host_a.com"; |
| 62 const std::string host_b = "host_b.com"; |
| 63 const int history = 4; |
| 64 const int threshold = 2; |
| 65 // Hopefully, this test runs within a calendar year. |
| 66 const int duration_in_seconds = 1 * 365 * 24 * 60 * 60; |
| 67 base::FieldTrialList field_trial_list(nullptr); |
| 68 ASSERT_TRUE( |
| 69 EnableBlackListParamsForTesting(history, threshold, duration_in_seconds)); |
| 70 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList(nullptr)); |
| 71 |
| 72 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a)); |
| 73 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 74 |
| 75 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 76 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 77 |
| 78 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 79 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 80 |
| 81 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 82 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 83 |
| 84 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 85 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b)); |
| 86 |
| 87 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 88 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 89 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 90 |
| 91 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 92 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 93 |
| 94 variations::testing::ClearAllVariationParams(); |
| 95 } |
| 96 |
| 97 TEST_F(PreviewsBlackListTest, BlackListWithStore) { |
| 98 const std::string host_a = "host_a.com"; |
| 99 const std::string host_b = "host_b.com"; |
| 100 const int history = 4; |
| 101 const int threshold = 2; |
| 102 // Hopefully, this test runs within a calendar year. |
| 103 const int duration_in_seconds = 1 * 365 * 24 * 60 * 60; |
| 104 base::FieldTrialList field_trial_list(nullptr); |
| 105 ASSERT_TRUE( |
| 106 EnableBlackListParamsForTesting(history, threshold, duration_in_seconds)); |
| 107 |
| 108 base::MessageLoop loop; |
| 109 |
| 110 std::unique_ptr<PreviewsBlackList> black_list( |
| 111 new PreviewsBlackList(base::MakeUnique<TestPreviewsOptOutStore>())); |
| 112 |
| 113 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 114 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b)); |
| 115 |
| 116 base::RunLoop().RunUntilIdle(); |
| 117 |
| 118 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a)); |
| 119 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 120 |
| 121 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 122 black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE); |
| 123 |
| 124 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 125 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 126 |
| 127 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 128 black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE); |
| 129 |
| 130 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 131 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b)); |
| 132 |
| 133 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 134 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 135 black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE); |
| 136 |
| 137 EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a)); |
| 138 EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b)); |
| 139 |
| 140 variations::testing::ClearAllVariationParams(); |
| 141 } |
| 142 |
| 143 } // namespace previews |
OLD | NEW |