Index: chrome/browser/android/offline_pages/offline_page_suggestions_observer_unittest.cc |
diff --git a/chrome/browser/android/offline_pages/offline_page_suggestions_observer_unittest.cc b/chrome/browser/android/offline_pages/offline_page_suggestions_observer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..63baf400cca80092be4f5886d979da47d61ba5ad |
--- /dev/null |
+++ b/chrome/browser/android/offline_pages/offline_page_suggestions_observer_unittest.cc |
@@ -0,0 +1,149 @@ |
+// Copyright 2017 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 "chrome/browser/android/offline_pages/offline_page_suggestions_observer.h" |
+ |
+#include "base/run_loop.h" |
+#include "base/test/test_simple_task_runner.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "components/offline_pages/core/stub_offline_page_model.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+ |
+namespace offline_pages { |
+ |
+namespace { |
+ |
+class TestingPrefetchService : public PrefetchService { |
+ public: |
+ TestingPrefetchService() = default; |
+ |
+ void OnNewURLsToPrefetch(std::vector<GURL> suggested_urls) override { |
+ latest_url_suggestions = suggested_urls; |
+ new_suggestions_count++; |
+ } |
+ void RemoveAllUnprocessedURLsToPrefetch() override { |
+ latest_url_suggestions.clear(); |
+ remove_all_suggestions_count++; |
+ } |
+ |
+ std::vector<GURL> latest_url_suggestions; |
+ int new_suggestions_count = 0; |
+ int remove_all_suggestions_count = 0; |
+}; |
+ |
+class TestDelegate : public SuggestionsObserver::Delegate { |
+ public: |
+ TestDelegate() = default; |
+ ~TestDelegate() override = default; |
+ |
+ std::vector<GURL> GetSuggestionURLs( |
+ const ntp_snippets::Category& category) override { |
+ get_suggestion_urls_count++; |
+ return suggestion_urls; |
+ } |
+ |
+ PrefetchService* GetPrefetchService( |
+ content::BrowserContext* context) override { |
+ return &prefetch_service; |
+ } |
+ |
+ TestingPrefetchService prefetch_service; |
+ |
+ // Public for test manipulation. |
+ std::vector<GURL> suggestion_urls; |
+ |
+ // Signals that delegate was called. |
+ int get_suggestion_urls_count = 0; |
+}; |
+ |
+} // namespace |
+ |
+class OfflinePageSuggestionsObserverTest : public testing::Test { |
+ public: |
+ OfflinePageSuggestionsObserverTest() = default; |
+ |
+ void SetUp() override { |
+ observer_ = base::MakeUnique<SuggestionsObserver>(&profile_, MakeDelegate(), |
+ category); |
+ } |
+ |
+ virtual std::unique_ptr<SuggestionsObserver::Delegate> MakeDelegate() { |
+ auto delegate_ptr = base::MakeUnique<TestDelegate>(); |
+ test_delegate_ = delegate_ptr.get(); |
+ return std::move(delegate_ptr); |
+ } |
+ |
+ SuggestionsObserver* observer() { return observer_.get(); } |
+ |
+ TestDelegate* test_delegate() { return test_delegate_; } |
+ TestingPrefetchService* test_prefetch_service() { |
+ return &(test_delegate()->prefetch_service); |
+ } |
+ |
+ protected: |
+ ntp_snippets::Category category = ntp_snippets::Category::FromKnownCategory( |
+ ntp_snippets::KnownCategories::ARTICLES); |
+ content::TestBrowserThreadBundle thread_bundle_; |
+ TestingProfile profile_; |
+ |
+ private: |
+ std::unique_ptr<SuggestionsObserver> observer_; |
+ TestDelegate* test_delegate_; |
+}; |
+ |
+TEST_F(OfflinePageSuggestionsObserverTest, CallsDelegateOnNewSuggestions) { |
+ // We should not do anything if the category is not loaded. |
+ observer()->OnNewSuggestions(category); |
+ EXPECT_EQ(0, test_delegate()->get_suggestion_urls_count); |
+ EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); |
+ |
+ // Once the category becomes available, new suggestions should cause us to ask |
+ // the delegate for suggestion URLs. |
+ observer()->OnCategoryStatusChanged(category, |
+ ntp_snippets::CategoryStatus::AVAILABLE); |
+ observer()->OnNewSuggestions(category); |
+ EXPECT_EQ(1, test_delegate()->get_suggestion_urls_count); |
+ |
+ // We expect that no pages were forwarded to the prefetch service since no |
+ // pages were prepopulated. |
+ EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); |
+} |
+ |
+TEST_F(OfflinePageSuggestionsObserverTest, |
+ ForwardsSuggestionsToPrefetchService) { |
+ const GURL test_url_1("https://www.example.com/1"); |
+ test_delegate()->suggestion_urls.push_back(test_url_1); |
+ |
+ observer()->OnCategoryStatusChanged(category, |
+ ntp_snippets::CategoryStatus::AVAILABLE); |
+ observer()->OnNewSuggestions(category); |
+ EXPECT_EQ(1, test_prefetch_service()->new_suggestions_count); |
+ EXPECT_EQ(1U, test_prefetch_service()->latest_url_suggestions.size()); |
+ EXPECT_EQ(test_url_1, test_prefetch_service()->latest_url_suggestions[0]); |
+} |
+ |
+TEST_F(OfflinePageSuggestionsObserverTest, RemovesAllOnBadStatus) { |
+ const GURL test_url_1("https://www.example.com/1"); |
+ const GURL test_url_2("https://www.example.com/2"); |
+ test_delegate()->suggestion_urls.push_back(test_url_1); |
+ test_delegate()->suggestion_urls.push_back(test_url_2); |
+ |
+ observer()->OnCategoryStatusChanged(category, |
+ ntp_snippets::CategoryStatus::AVAILABLE); |
+ observer()->OnNewSuggestions(category); |
+ ASSERT_EQ(2U, test_prefetch_service()->latest_url_suggestions.size()); |
+ |
+ observer()->OnCategoryStatusChanged( |
+ category, ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
+ ASSERT_EQ(1, test_prefetch_service()->remove_all_suggestions_count); |
+ observer()->OnCategoryStatusChanged( |
+ category, |
+ ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED); |
+ ASSERT_EQ(2, test_prefetch_service()->remove_all_suggestions_count); |
+} |
+ |
+} // namespace offline_pages |