| Index: components/previews/core/previews_black_list_unittest.cc
|
| diff --git a/components/previews/core/previews_black_list_unittest.cc b/components/previews/core/previews_black_list_unittest.cc
|
| index 6f98c122b016f94e8f3651566c760aa418e0961d..85538a0315ebc0faaa91696705b98d0cdcd5a1fd 100644
|
| --- a/components/previews/core/previews_black_list_unittest.cc
|
| +++ b/components/previews/core/previews_black_list_unittest.cc
|
| @@ -27,68 +27,82 @@
|
| 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));
|
| +void RunLoadCallback(
|
| + LoadBlackListCallback callback,
|
| + std::unique_ptr<BlackListItemMap> black_list_item_map,
|
| + std::unique_ptr<PreviewsBlackListItem> general_black_list_item) {
|
| + callback.Run(std::move(black_list_item_map),
|
| + std::move(general_black_list_item));
|
| }
|
|
|
| class TestPreviewsOptOutStore : public PreviewsOptOutStore {
|
| public:
|
| TestPreviewsOptOutStore() : clear_blacklist_count_(0) {}
|
| ~TestPreviewsOptOutStore() override {}
|
|
|
| int clear_blacklist_count() { return clear_blacklist_count_; }
|
|
|
| private:
|
| // PreviewsOptOutStore implementation:
|
| void AddPreviewNavigation(bool opt_out,
|
| const std::string& host_name,
|
| PreviewsType type,
|
| base::Time now) override {}
|
|
|
| void LoadBlackList(LoadBlackListCallback callback) override {
|
| std::unique_ptr<BlackListItemMap> black_list_item_map(
|
| new BlackListItemMap());
|
| + std::unique_ptr<PreviewsBlackListItem> general_black_list_item =
|
| + PreviewsBlackList::CreateGeneralBlackListItem();
|
| base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| FROM_HERE, base::Bind(&RunLoadCallback, callback,
|
| - base::Passed(&black_list_item_map)));
|
| + base::Passed(&black_list_item_map),
|
| + base::Passed(&general_black_list_item)));
|
| }
|
|
|
| void ClearBlackList(base::Time begin_time, base::Time end_time) override {
|
| ++clear_blacklist_count_;
|
| }
|
|
|
| int clear_blacklist_count_;
|
| };
|
|
|
| } // namespace
|
|
|
| -TEST_F(PreviewsBlackListTest, BlackListNoStore) {
|
| +TEST_F(PreviewsBlackListTest, PerHostBlackListNoStore) {
|
| // Tests the black list behavior when a null OptOutSture is passed in.
|
| const GURL url_a("http://www.url_a.com");
|
| const GURL url_b("http://www.url_b.com");
|
| - const size_t history = 4;
|
| - const int threshold = 2;
|
| + const size_t per_host_history = 4;
|
| + const int per_host_threshold = 2;
|
| + // General blacklisting should have no effect with the following params.
|
| + const size_t general_history = 1;
|
| + const int general_threshold = 2;
|
| const int duration_in_days = 365;
|
| base::FieldTrialList field_trial_list(nullptr);
|
| std::map<std::string, std::string> params;
|
| - params["stored_history_length"] = base::SizeTToString(history);
|
| - params["opt_out_threshold"] = base::IntToString(threshold);
|
| - params["black_list_duration_in_days"] = base::IntToString(duration_in_days);
|
| + params["per_host_max_stored_history_length"] =
|
| + base::SizeTToString(per_host_history);
|
| + params["general_max_stored_history_length"] =
|
| + base::SizeTToString(general_history);
|
| + params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold);
|
| + params["general_opt_out_threshold"] = base::IntToString(general_threshold);
|
| + params["per_host_black_list_duration_in_days"] =
|
| + base::IntToString(duration_in_days);
|
| ASSERT_TRUE(
|
| variations::AssociateVariationParams("ClientSidePreviews", "Enabled",
|
| params) &&
|
| base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
|
|
|
| base::SimpleTestClock* test_clock = new base::SimpleTestClock();
|
| base::Time start = test_clock->Now();
|
| test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
|
|
| std::unique_ptr<PreviewsBlackList> black_list(
|
| @@ -118,33 +132,41 @@ TEST_F(PreviewsBlackListTest, BlackListNoStore) {
|
|
|
| test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| black_list->ClearBlackList(start, test_clock->Now());
|
|
|
| EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
|
|
| variations::testing::ClearAllVariationParams();
|
| }
|
|
|
| -TEST_F(PreviewsBlackListTest, BlackListWithStore) {
|
| +TEST_F(PreviewsBlackListTest, PerHostBlackListWithStore) {
|
| // Tests the black list behavior when a non-null OptOutSture is passed in.
|
| const GURL url_a1("http://www.url_a.com/a1");
|
| const GURL url_a2("http://www.url_a.com/a2");
|
| const GURL url_b("http://www.url_b.com");
|
| - const size_t history = 4;
|
| - const int threshold = 2;
|
| + const size_t per_host_history = 4;
|
| + const int per_host_threshold = 2;
|
| + // General blacklisting should have no effect with the following params.
|
| + const size_t general_history = 1;
|
| + const int general_threshold = 2;
|
| const int duration_in_days = 365;
|
| base::FieldTrialList field_trial_list(nullptr);
|
| std::map<std::string, std::string> params;
|
| - params["stored_history_length"] = base::SizeTToString(history);
|
| - params["opt_out_threshold"] = base::IntToString(threshold);
|
| - params["black_list_duration_in_days"] = base::IntToString(duration_in_days);
|
| + params["per_host_max_stored_history_length"] =
|
| + base::SizeTToString(per_host_history);
|
| + params["general_max_stored_history_length"] =
|
| + base::SizeTToString(general_history);
|
| + params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold);
|
| + params["general_opt_out_threshold"] = base::IntToString(general_threshold);
|
| + params["per_host_black_list_duration_in_days"] =
|
| + base::IntToString(duration_in_days);
|
| ASSERT_TRUE(
|
| variations::AssociateVariationParams("ClientSidePreviews", "Enabled",
|
| params) &&
|
| base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
|
|
|
| base::MessageLoop loop;
|
|
|
| base::SimpleTestClock* test_clock = new base::SimpleTestClock();
|
| base::Time start = test_clock->Now();
|
| test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| @@ -198,29 +220,183 @@ TEST_F(PreviewsBlackListTest, BlackListWithStore) {
|
| base::RunLoop().RunUntilIdle();
|
| EXPECT_EQ(1, opt_out_store->clear_blacklist_count());
|
|
|
| EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE));
|
| EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE));
|
| EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
|
|
| variations::testing::ClearAllVariationParams();
|
| }
|
|
|
| +TEST_F(PreviewsBlackListTest, GeneralBlackListNoStore) {
|
| + // Tests the black list behavior when a null OptOutSture is passed in.
|
| + const GURL url_a("http://www.url_a.com");
|
| + const GURL url_b("http://www.url_b.com");
|
| + const GURL url_c("http://www.url_c.com");
|
| + const GURL url_d("http://www.url_d.com");
|
| + // Per host blacklisting should have no effect with the following params.
|
| + const size_t per_host_history = 1;
|
| + const int per_host_threshold = 2;
|
| + const size_t general_history = 4;
|
| + const int general_threshold = 4;
|
| + const int duration_in_days = 365;
|
| + base::FieldTrialList field_trial_list(nullptr);
|
| + std::map<std::string, std::string> params;
|
| + params["per_host_max_stored_history_length"] =
|
| + base::SizeTToString(per_host_history);
|
| + params["general_max_stored_history_length"] =
|
| + base::SizeTToString(general_history);
|
| + params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold);
|
| + params["general_opt_out_threshold"] = base::IntToString(general_threshold);
|
| + params["per_host_black_list_duration_in_days"] =
|
| + base::IntToString(duration_in_days);
|
| + ASSERT_TRUE(
|
| + variations::AssociateVariationParams("ClientSidePreviews", "Enabled",
|
| + params) &&
|
| + base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
|
| +
|
| + base::SimpleTestClock* test_clock = new base::SimpleTestClock();
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| +
|
| + std::unique_ptr<PreviewsBlackList> black_list(
|
| + new PreviewsBlackList(nullptr, base::WrapUnique(test_clock)));
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_c, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_d, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_d, false, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE));
|
| +
|
| + variations::testing::ClearAllVariationParams();
|
| +}
|
| +
|
| +TEST_F(PreviewsBlackListTest, GeneralBlackListWithStore) {
|
| + // Tests the black list behavior when a non-null OptOutSture is passed in.
|
| + const GURL url_a("http://www.url_a.com");
|
| + const GURL url_b("http://www.url_b.com");
|
| + const GURL url_c("http://www.url_c.com");
|
| + const GURL url_d("http://www.url_d.com");
|
| + // Per host blacklisting should have no effect with the following params.
|
| + const size_t per_host_history = 1;
|
| + const int per_host_threshold = 2;
|
| + const size_t general_history = 4;
|
| + const int general_threshold = 4;
|
| + const int duration_in_days = 365;
|
| + base::FieldTrialList field_trial_list(nullptr);
|
| + std::map<std::string, std::string> params;
|
| + params["per_host_max_stored_history_length"] =
|
| + base::SizeTToString(per_host_history);
|
| + params["general_max_stored_history_length"] =
|
| + base::SizeTToString(general_history);
|
| + params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold);
|
| + params["general_opt_out_threshold"] = base::IntToString(general_threshold);
|
| + params["per_host_black_list_duration_in_days"] =
|
| + base::IntToString(duration_in_days);
|
| + ASSERT_TRUE(
|
| + variations::AssociateVariationParams("ClientSidePreviews", "Enabled",
|
| + params) &&
|
| + base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
|
| +
|
| + base::MessageLoop loop;
|
| +
|
| + base::SimpleTestClock* test_clock = new base::SimpleTestClock();
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| +
|
| + TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore();
|
| +
|
| + std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList(
|
| + base::WrapUnique(opt_out_store), base::WrapUnique(test_clock)));
|
| +
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_c, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_d, true, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE));
|
| + EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE));
|
| +
|
| + black_list->AddPreviewNavigation(url_d, false, PreviewsType::OFFLINE);
|
| + test_clock->Advance(base::TimeDelta::FromSeconds(1));
|
| +
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE));
|
| + EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE));
|
| +
|
| + variations::testing::ClearAllVariationParams();
|
| +}
|
| +
|
| TEST_F(PreviewsBlackListTest, QueueBehavior) {
|
| // Tests the black list asynchronous queue behavior. Methods called while
|
| // loading are queued and should run in the order they were queued.
|
| const GURL url("http://www.url.com");
|
| const GURL url2("http://www.url2.com");
|
| + // General blacklisting should have no effect with the following params.
|
| + const size_t general_history = 1;
|
| + const int general_threshold = 2;
|
| const int duration_in_days = 365;
|
| base::FieldTrialList field_trial_list(nullptr);
|
| std::map<std::string, std::string> params;
|
| - params["black_list_duration_in_days"] = base::IntToString(duration_in_days);
|
| + params["per_host_black_list_duration_in_days"] =
|
| + base::IntToString(duration_in_days);
|
| + params["general_max_stored_history_length"] =
|
| + base::SizeTToString(general_history);
|
| + params["general_opt_out_threshold"] = base::IntToString(general_threshold);
|
| ASSERT_TRUE(
|
| variations::AssociateVariationParams("ClientSidePreviews", "Enabled",
|
| params) &&
|
| base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
|
|
|
| base::MessageLoop loop;
|
|
|
| std::vector<bool> test_opt_out{true, false};
|
|
|
| for (auto opt_out : test_opt_out) {
|
| @@ -266,28 +442,35 @@ TEST_F(PreviewsBlackListTest, MaxHosts) {
|
| // hosts.
|
| const GURL url_a("http://www.url_a.com");
|
| const GURL url_b("http://www.url_b.com");
|
| const GURL url_c("http://www.url_c.com");
|
| const GURL url_d("http://www.url_d.com");
|
| const GURL url_e("http://www.url_e.com");
|
| const size_t stored_history_length = 1;
|
| const int opt_out_threshold = 1;
|
| const int black_list_duration_in_days = 365;
|
| const size_t max_hosts_in_blacklist = 2;
|
| + // General blacklisting should have no effect with the following params.
|
| + const size_t general_history = 1;
|
| + const int general_threshold = 2;
|
| base::FieldTrialList field_trial_list(nullptr);
|
| std::map<std::string, std::string> params;
|
| - params["stored_history_length"] = base::SizeTToString(stored_history_length);
|
| - params["opt_out_threshold"] = base::IntToString(opt_out_threshold);
|
| - params["black_list_duration_in_days"] =
|
| + params["per_host_stored_history_length"] =
|
| + base::SizeTToString(stored_history_length);
|
| + params["per_host_opt_out_threshold"] = base::IntToString(opt_out_threshold);
|
| + params["per_host_black_list_duration_in_days"] =
|
| base::IntToString(black_list_duration_in_days);
|
| params["max_hosts_in_blacklist"] =
|
| base::SizeTToString(max_hosts_in_blacklist);
|
| + params["general_max_stored_history_length"] =
|
| + base::SizeTToString(general_history);
|
| + params["general_opt_out_threshold"] = base::IntToString(general_threshold);
|
| ASSERT_TRUE(
|
| variations::AssociateVariationParams("ClientSidePreviews", "Enabled",
|
| params) &&
|
| base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"));
|
|
|
| base::MessageLoop loop;
|
|
|
| base::SimpleTestClock* test_clock = new base::SimpleTestClock();
|
|
|
| std::unique_ptr<PreviewsBlackList> black_list(
|
|
|