OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/offline_pages/offline_page_suggestions_observer
.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/test/base/testing_profile.h" |
| 11 #include "components/offline_pages/core/stub_offline_page_model.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace offline_pages { |
| 17 |
| 18 namespace { |
| 19 class TestDelegate : public OfflinePageSuggestionsObserver::Delegate { |
| 20 public: |
| 21 TestDelegate() = default; |
| 22 ~TestDelegate() override = default; |
| 23 |
| 24 std::vector<GURL> GetSuggestionURLs() override { |
| 25 get_suggestion_urls_count++; |
| 26 return suggestion_urls; |
| 27 } |
| 28 |
| 29 void GetPagesWithURLs( |
| 30 const std::vector<GURL>& suggestion_urls, |
| 31 const MultipleOfflinePageItemCallback& callback) override { |
| 32 get_pages_with_urls_count++; |
| 33 callback.Run(matching_pages); |
| 34 } |
| 35 |
| 36 // Public for test manipulation. |
| 37 std::vector<GURL> suggestion_urls; |
| 38 std::vector<OfflinePageItem> matching_pages; |
| 39 |
| 40 // Signals that delegate was called. |
| 41 int get_suggestion_urls_count = 0; |
| 42 int get_pages_with_urls_count = 0; |
| 43 }; |
| 44 |
| 45 } // namespace |
| 46 |
| 47 class OfflinePageSuggestionsObserverTest : public testing::Test { |
| 48 public: |
| 49 OfflinePageSuggestionsObserverTest() = default; |
| 50 |
| 51 void SetUp() override { |
| 52 observer_ = base::MakeUnique<OfflinePageSuggestionsObserver>( |
| 53 &profile_, MakeDelegate(), category); |
| 54 } |
| 55 |
| 56 virtual std::unique_ptr<OfflinePageSuggestionsObserver::Delegate> |
| 57 MakeDelegate() { |
| 58 auto delegate_ptr = base::MakeUnique<TestDelegate>(); |
| 59 test_delegate_ = delegate_ptr.get(); |
| 60 return std::move(delegate_ptr); |
| 61 } |
| 62 |
| 63 OfflinePageSuggestionsObserver* observer() { return observer_.get(); } |
| 64 |
| 65 TestDelegate* test_delegate() { return test_delegate_; } |
| 66 |
| 67 protected: |
| 68 ntp_snippets::Category category = ntp_snippets::Category::FromKnownCategory( |
| 69 ntp_snippets::KnownCategories::ARTICLES); |
| 70 content::TestBrowserThreadBundle thread_bundle_; |
| 71 TestingProfile profile_; |
| 72 |
| 73 private: |
| 74 std::unique_ptr<OfflinePageSuggestionsObserver> observer_; |
| 75 TestDelegate* test_delegate_; |
| 76 }; |
| 77 |
| 78 TEST_F(OfflinePageSuggestionsObserverTest, CallsDelegateOnNewSuggestions) { |
| 79 // We should not do anything if the category is not loaded. |
| 80 observer()->OnNewSuggestions(category); |
| 81 EXPECT_EQ(0, test_delegate()->get_suggestion_urls_count); |
| 82 EXPECT_EQ(0, test_delegate()->get_pages_with_urls_count); |
| 83 |
| 84 // Once the category becomes available, new suggestions should cause us to ask |
| 85 // the delegate for suggestion URLs. |
| 86 observer()->OnCategoryStatusChanged(category, |
| 87 ntp_snippets::CategoryStatus::AVAILABLE); |
| 88 observer()->OnNewSuggestions(category); |
| 89 EXPECT_EQ(1, test_delegate()->get_suggestion_urls_count); |
| 90 EXPECT_EQ(1, test_delegate()->get_pages_with_urls_count); |
| 91 } |
| 92 |
| 93 } // namespace offline_pages |
OLD | NEW |