| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/offline_pages/content/suggested_articles_observer.h" | 5 #include "chrome/browser/offline_pages/prefetch/suggested_articles_observer.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/test/base/testing_profile.h" |
| 10 #include "components/offline_pages/core/client_namespace_constants.h" | 11 #include "components/offline_pages/core/client_namespace_constants.h" |
| 11 #include "components/offline_pages/core/stub_offline_page_model.h" | 12 #include "components/offline_pages/core/stub_offline_page_model.h" |
| 12 #include "content/public/test/test_browser_context.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 using ntp_snippets::Category; | 17 using ntp_snippets::Category; |
| 17 using ntp_snippets::ContentSuggestion; | 18 using ntp_snippets::ContentSuggestion; |
| 18 | 19 |
| 20 namespace gcm { |
| 21 class GCMDriver; |
| 22 } |
| 23 |
| 19 namespace offline_pages { | 24 namespace offline_pages { |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { | 28 ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { |
| 24 auto category = | 29 auto category = |
| 25 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | 30 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 26 return ContentSuggestion(category, test_url.spec(), test_url); | 31 return ContentSuggestion(category, test_url.spec(), test_url); |
| 27 } | 32 } |
| 28 | 33 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 }; | 92 }; |
| 88 | 93 |
| 89 } // namespace | 94 } // namespace |
| 90 | 95 |
| 91 class OfflinePageSuggestedArticlesObserverTest : public testing::Test { | 96 class OfflinePageSuggestedArticlesObserverTest : public testing::Test { |
| 92 public: | 97 public: |
| 93 OfflinePageSuggestedArticlesObserverTest() = default; | 98 OfflinePageSuggestedArticlesObserverTest() = default; |
| 94 | 99 |
| 95 void SetUp() override { | 100 void SetUp() override { |
| 96 observer_ = | 101 observer_ = |
| 97 base::MakeUnique<SuggestedArticlesObserver>(&context_, MakeDelegate()); | 102 base::MakeUnique<SuggestedArticlesObserver>(&profile_, MakeDelegate()); |
| 98 } | 103 } |
| 99 | 104 |
| 100 virtual std::unique_ptr<SuggestedArticlesObserver::Delegate> MakeDelegate() { | 105 virtual std::unique_ptr<SuggestedArticlesObserver::Delegate> MakeDelegate() { |
| 101 auto delegate_ptr = base::MakeUnique<TestDelegate>(); | 106 auto delegate_ptr = base::MakeUnique<TestDelegate>(); |
| 102 test_delegate_ = delegate_ptr.get(); | 107 test_delegate_ = delegate_ptr.get(); |
| 103 return std::move(delegate_ptr); | 108 return std::move(delegate_ptr); |
| 104 } | 109 } |
| 105 | 110 |
| 106 SuggestedArticlesObserver* observer() { return observer_.get(); } | 111 SuggestedArticlesObserver* observer() { return observer_.get(); } |
| 107 | 112 |
| 108 TestDelegate* test_delegate() { return test_delegate_; } | 113 TestDelegate* test_delegate() { return test_delegate_; } |
| 109 TestingPrefetchService* test_prefetch_service() { | 114 TestingPrefetchService* test_prefetch_service() { |
| 110 return &(test_delegate()->prefetch_service); | 115 return &(test_delegate()->prefetch_service); |
| 111 } | 116 } |
| 112 | 117 |
| 113 protected: | 118 protected: |
| 114 Category category = | 119 Category category = |
| 115 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | 120 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 116 content::TestBrowserContext context_; | 121 |
| 122 content::TestBrowserThreadBundle thread_bundle_; |
| 123 TestingProfile profile_; |
| 117 | 124 |
| 118 private: | 125 private: |
| 119 std::unique_ptr<SuggestedArticlesObserver> observer_; | 126 std::unique_ptr<SuggestedArticlesObserver> observer_; |
| 120 TestDelegate* test_delegate_; | 127 TestDelegate* test_delegate_; |
| 121 }; | 128 }; |
| 122 | 129 |
| 123 TEST_F(OfflinePageSuggestedArticlesObserverTest, | 130 TEST_F(OfflinePageSuggestedArticlesObserverTest, |
| 124 CallsDelegateOnNewSuggestions) { | 131 CallsDelegateOnNewSuggestions) { |
| 125 // We should not do anything if the category is not loaded. | 132 // We should not do anything if the category is not loaded. |
| 126 observer()->OnNewSuggestions(category); | 133 observer()->OnNewSuggestions(category); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 196 |
| 190 observer()->OnSuggestionInvalidated( | 197 observer()->OnSuggestionInvalidated( |
| 191 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); | 198 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); |
| 192 | 199 |
| 193 EXPECT_EQ(1, test_prefetch_service()->remove_by_client_id_count); | 200 EXPECT_EQ(1, test_prefetch_service()->remove_by_client_id_count); |
| 194 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), | 201 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), |
| 195 *test_prefetch_service()->last_removed_client_id); | 202 *test_prefetch_service()->last_removed_client_id); |
| 196 } | 203 } |
| 197 | 204 |
| 198 } // namespace offline_pages | 205 } // namespace offline_pages |
| OLD | NEW |