Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(780)

Unified Diff: components/previews/previews_black_list_unittest.cc

Issue 2335023002: Adding a previews IO-thread blacklist (Closed)
Patch Set: tbansal comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..33484ce6bfde0bd5c0674ceb69f89aae4ca2fcab
--- /dev/null
+++ b/components/previews/previews_black_list_unittest.cc
@@ -0,0 +1,143 @@
+// 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)));
+ }
+};
+
+} // 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;
+ // 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));
+
+ 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;
+ // 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));
+
+ variations::testing::ClearAllVariationParams();
+}
+
+} // namespace previews

Powered by Google App Engine
This is Rietveld 408576698