| Index: components/previews/previews_black_list_unittest.cc
|
| diff --git a/components/previews/previews_black_list_unittest.cc b/components/previews/previews_black_list_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..868f7484608aed7c9f3d36f35cdf7022dee73176
|
| --- /dev/null
|
| +++ b/components/previews/previews_black_list_unittest.cc
|
| @@ -0,0 +1,167 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "components/previews/previews_black_list.h"
|
| +
|
| +#include <memory>
|
| +#include <string>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/metrics/field_trial.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/time/time.h"
|
| +#include "components/previews/previews_black_list_item.h"
|
| +#include "components/previews/previews_experiments.h"
|
| +#include "components/previews/previews_opt_out_store.h"
|
| +#include "components/variations/variations_associated_data.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +using PreviewsBlackListTest = testing::Test;
|
| +
|
| +} // namespace
|
| +
|
| +namespace previews {
|
| +
|
| +namespace {
|
| +
|
| +void RunLoadCallback(LoadBlackListCallback callback,
|
| + std::unique_ptr<BlackListItemMap> black_list_item_map) {
|
| + callback.Run(std::move(black_list_item_map));
|
| +}
|
| +
|
| +class TestPreviewsOptOutStore : public PreviewsOptOutStore {
|
| + public:
|
| + TestPreviewsOptOutStore() {}
|
| + ~TestPreviewsOptOutStore() override {}
|
| +
|
| + private:
|
| + // PreviewsOptOutStore implementation:
|
| + void AddPreviewNavigation(bool opt_out,
|
| + const std::string& host_name,
|
| + PreviewsType type,
|
| + const base::Time& now) override {}
|
| + void LoadBlackList(LoadBlackListCallback callback) override {
|
| + std::unique_ptr<BlackListItemMap> black_list_item_map(
|
| + new BlackListItemMap());
|
| + base::MessageLoop::current()->task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&RunLoadCallback, callback,
|
| + base::Passed(&black_list_item_map)));
|
| + }
|
| + void ClearBlackList(const base::Time& begin_time,
|
| + const base::Time& end_time,
|
| + LoadBlackListCallback callback) override {
|
| + std::unique_ptr<BlackListItemMap> black_list_item_map(
|
| + new BlackListItemMap());
|
| + base::MessageLoop::current()->task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&RunLoadCallback, callback,
|
| + base::Passed(&black_list_item_map)));
|
| + }
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +TEST_F(PreviewsBlackListTest, BlackListNoStore) {
|
| + const std::string host_a = "host_a.com";
|
| + const std::string host_b = "host_b.com";
|
| + const int history = 4;
|
| + const int threshold = 2;
|
| + base::Time start = base::Time::Now();
|
| + // Hopefully, this test runs within a calendar year.
|
| + const int duration_in_seconds = 1 * 365 * 24 * 60 * 60;
|
| + base::FieldTrialList field_trial_list(nullptr);
|
| + ASSERT_TRUE(
|
| + EnableBlackListParamsForTesting(history, threshold, duration_in_seconds));
|
| + std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList(nullptr));
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE);
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE);
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE);
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->ClearBlackList(start, base::Time::Now());
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| + variations::testing::ClearAllVariationParams();
|
| +}
|
| +
|
| +TEST_F(PreviewsBlackListTest, BlackListWithStore) {
|
| + const std::string host_a = "host_a.com";
|
| + const std::string host_b = "host_b.com";
|
| + const int history = 4;
|
| + const int threshold = 2;
|
| + base::Time start = base::Time::Now();
|
| + // Hopefully, this test runs within a calendar year.
|
| + const int duration_in_seconds = 1 * 365 * 24 * 60 * 60;
|
| + base::FieldTrialList field_trial_list(nullptr);
|
| + ASSERT_TRUE(
|
| + EnableBlackListParamsForTesting(history, threshold, duration_in_seconds));
|
| +
|
| + base::MessageLoop loop;
|
| +
|
| + std::unique_ptr<PreviewsBlackList> black_list(
|
| + new PreviewsBlackList(base::MakeUnique<TestPreviewsOptOutStore>()));
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_a, true, PreviewsType::OFFLINE);
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_b, true, PreviewsType::OFFLINE);
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE);
|
| + black_list->AddPreviewNavigation(host_b, false, PreviewsType::OFFLINE);
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + black_list->ClearBlackList(start, base::Time::Now());
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(host_b));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_a));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(host_b));
|
| + variations::testing::ClearAllVariationParams();
|
| +}
|
| +
|
| +} // namespace previews
|
|
|