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..a07d89c01eca841f250384594c466c374a6ae9e7 |
--- /dev/null |
+++ b/chrome/browser/android/offline_pages/offline_page_suggestions_observer_unittest.cc |
@@ -0,0 +1,93 @@ |
+// 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 TestDelegate : public OfflinePageSuggestionsObserver::Delegate { |
+ public: |
+ TestDelegate() = default; |
+ ~TestDelegate() override = default; |
+ |
+ std::vector<GURL> GetSuggestionURLs() override { |
+ get_suggestion_urls_count++; |
+ return suggestion_urls; |
+ } |
+ |
+ void GetPagesWithURLs( |
+ const std::vector<GURL>& suggestion_urls, |
+ const MultipleOfflinePageItemCallback& callback) override { |
+ get_pages_with_urls_count++; |
+ callback.Run(matching_pages); |
+ } |
+ |
+ // Public for test manipulation. |
+ std::vector<GURL> suggestion_urls; |
+ std::vector<OfflinePageItem> matching_pages; |
+ |
+ // Signals that delegate was called. |
+ int get_suggestion_urls_count = 0; |
+ int get_pages_with_urls_count = 0; |
+}; |
+ |
+} // namespace |
+ |
+class OfflinePageSuggestionsObserverTest : public testing::Test { |
+ public: |
+ OfflinePageSuggestionsObserverTest() = default; |
+ |
+ void SetUp() override { |
+ observer_ = base::MakeUnique<OfflinePageSuggestionsObserver>( |
+ &profile_, MakeDelegate(), category); |
+ } |
+ |
+ virtual std::unique_ptr<OfflinePageSuggestionsObserver::Delegate> |
+ MakeDelegate() { |
+ auto delegate_ptr = base::MakeUnique<TestDelegate>(); |
+ test_delegate_ = delegate_ptr.get(); |
+ return std::move(delegate_ptr); |
+ } |
+ |
+ OfflinePageSuggestionsObserver* observer() { return observer_.get(); } |
+ |
+ TestDelegate* test_delegate() { return test_delegate_; } |
+ |
+ protected: |
+ ntp_snippets::Category category = ntp_snippets::Category::FromKnownCategory( |
+ ntp_snippets::KnownCategories::ARTICLES); |
+ content::TestBrowserThreadBundle thread_bundle_; |
+ TestingProfile profile_; |
+ |
+ private: |
+ std::unique_ptr<OfflinePageSuggestionsObserver> 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_delegate()->get_pages_with_urls_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); |
+ EXPECT_EQ(1, test_delegate()->get_pages_with_urls_count); |
+} |
+ |
+} // namespace offline_pages |