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 "components/offline_pages/content/suggested_articles_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 "components/offline_pages/core/client_namespace_constants.h" |
| 11 #include "components/offline_pages/core/stub_offline_page_model.h" |
| 12 #include "content/public/test/test_browser_context.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 using ntp_snippets::Category; |
| 17 using ntp_snippets::ContentSuggestion; |
| 18 |
| 19 namespace offline_pages { |
| 20 |
| 21 namespace { |
| 22 |
| 23 ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { |
| 24 auto category = |
| 25 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 26 return ContentSuggestion(category, test_url.spec(), test_url); |
| 27 } |
| 28 |
| 29 class TestingPrefetchService : public PrefetchService { |
| 30 public: |
| 31 TestingPrefetchService() = default; |
| 32 |
| 33 void AddCandidatePrefetchURLs( |
| 34 const std::vector<PrefetchURL>& suggested_urls) override { |
| 35 latest_prefetch_urls = suggested_urls; |
| 36 new_suggestions_count++; |
| 37 } |
| 38 |
| 39 void RemoveAllUnprocessedPrefetchURLs( |
| 40 const std::string& name_space) override { |
| 41 DCHECK_EQ(name_space, kSuggestedArticlesNamespace); |
| 42 latest_prefetch_urls.clear(); |
| 43 remove_all_suggestions_count++; |
| 44 } |
| 45 |
| 46 void RemovePrefetchURLsByClientId(const ClientId& client_id) override { |
| 47 DCHECK_EQ(client_id.name_space, kSuggestedArticlesNamespace); |
| 48 remove_by_client_id_count++; |
| 49 last_removed_client_id = base::MakeUnique<ClientId>(client_id); |
| 50 } |
| 51 |
| 52 std::vector<PrefetchURL> latest_prefetch_urls; |
| 53 std::unique_ptr<ClientId> last_removed_client_id; |
| 54 |
| 55 int new_suggestions_count = 0; |
| 56 int remove_all_suggestions_count = 0; |
| 57 int remove_by_client_id_count = 0; |
| 58 }; |
| 59 |
| 60 class TestDelegate : public SuggestedArticlesObserver::Delegate { |
| 61 public: |
| 62 TestDelegate() = default; |
| 63 ~TestDelegate() override = default; |
| 64 |
| 65 const std::vector<ContentSuggestion>& GetSuggestions( |
| 66 const Category& category) override { |
| 67 get_suggestions_count++; |
| 68 return suggestions; |
| 69 } |
| 70 |
| 71 PrefetchService* GetPrefetchService( |
| 72 content::BrowserContext* context) override { |
| 73 return &prefetch_service; |
| 74 } |
| 75 |
| 76 TestingPrefetchService prefetch_service; |
| 77 |
| 78 // Public for test manipulation. |
| 79 std::vector<ContentSuggestion> suggestions; |
| 80 |
| 81 // Signals that delegate was called. |
| 82 int get_suggestions_count = 0; |
| 83 }; |
| 84 |
| 85 } // namespace |
| 86 |
| 87 class OfflinePageSuggestedArticlesObserverTest : public testing::Test { |
| 88 public: |
| 89 OfflinePageSuggestedArticlesObserverTest() = default; |
| 90 |
| 91 void SetUp() override { |
| 92 observer_ = |
| 93 base::MakeUnique<SuggestedArticlesObserver>(&context_, MakeDelegate()); |
| 94 } |
| 95 |
| 96 virtual std::unique_ptr<SuggestedArticlesObserver::Delegate> MakeDelegate() { |
| 97 auto delegate_ptr = base::MakeUnique<TestDelegate>(); |
| 98 test_delegate_ = delegate_ptr.get(); |
| 99 return std::move(delegate_ptr); |
| 100 } |
| 101 |
| 102 SuggestedArticlesObserver* observer() { return observer_.get(); } |
| 103 |
| 104 TestDelegate* test_delegate() { return test_delegate_; } |
| 105 TestingPrefetchService* test_prefetch_service() { |
| 106 return &(test_delegate()->prefetch_service); |
| 107 } |
| 108 |
| 109 protected: |
| 110 Category category = |
| 111 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 112 content::TestBrowserContext context_; |
| 113 |
| 114 private: |
| 115 std::unique_ptr<SuggestedArticlesObserver> observer_; |
| 116 TestDelegate* test_delegate_; |
| 117 }; |
| 118 |
| 119 TEST_F(OfflinePageSuggestedArticlesObserverTest, |
| 120 CallsDelegateOnNewSuggestions) { |
| 121 // We should not do anything if the category is not loaded. |
| 122 observer()->OnNewSuggestions(category); |
| 123 EXPECT_EQ(0, test_delegate()->get_suggestions_count); |
| 124 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); |
| 125 |
| 126 // Once the category becomes available, new suggestions should cause us to ask |
| 127 // the delegate for suggestion URLs. |
| 128 observer()->OnCategoryStatusChanged(category, |
| 129 ntp_snippets::CategoryStatus::AVAILABLE); |
| 130 observer()->OnNewSuggestions(category); |
| 131 EXPECT_EQ(1, test_delegate()->get_suggestions_count); |
| 132 |
| 133 // We expect that no pages were forwarded to the prefetch service since no |
| 134 // pages were prepopulated. |
| 135 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); |
| 136 } |
| 137 |
| 138 TEST_F(OfflinePageSuggestedArticlesObserverTest, |
| 139 ForwardsSuggestionsToPrefetchService) { |
| 140 const GURL test_url_1("https://www.example.com/1"); |
| 141 test_delegate()->suggestions.push_back( |
| 142 ContentSuggestionFromTestURL(test_url_1)); |
| 143 |
| 144 observer()->OnCategoryStatusChanged(category, |
| 145 ntp_snippets::CategoryStatus::AVAILABLE); |
| 146 observer()->OnNewSuggestions(category); |
| 147 EXPECT_EQ(1, test_prefetch_service()->new_suggestions_count); |
| 148 EXPECT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); |
| 149 EXPECT_EQ(test_url_1, test_prefetch_service()->latest_prefetch_urls[0].url); |
| 150 EXPECT_EQ( |
| 151 kSuggestedArticlesNamespace, |
| 152 test_prefetch_service()->latest_prefetch_urls[0].client_id.name_space); |
| 153 } |
| 154 |
| 155 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { |
| 156 const GURL test_url_1("https://www.example.com/1"); |
| 157 const GURL test_url_2("https://www.example.com/2"); |
| 158 test_delegate()->suggestions.push_back( |
| 159 ContentSuggestionFromTestURL(test_url_1)); |
| 160 test_delegate()->suggestions.push_back( |
| 161 ContentSuggestionFromTestURL(test_url_2)); |
| 162 |
| 163 observer()->OnCategoryStatusChanged(category, |
| 164 ntp_snippets::CategoryStatus::AVAILABLE); |
| 165 observer()->OnNewSuggestions(category); |
| 166 ASSERT_EQ(2U, test_prefetch_service()->latest_prefetch_urls.size()); |
| 167 |
| 168 observer()->OnCategoryStatusChanged( |
| 169 category, ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
| 170 EXPECT_EQ(1, test_prefetch_service()->remove_all_suggestions_count); |
| 171 observer()->OnCategoryStatusChanged( |
| 172 category, |
| 173 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED); |
| 174 EXPECT_EQ(2, test_prefetch_service()->remove_all_suggestions_count); |
| 175 } |
| 176 |
| 177 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesClientIdOnInvalidated) { |
| 178 const GURL test_url_1("https://www.example.com/1"); |
| 179 test_delegate()->suggestions.push_back( |
| 180 ContentSuggestionFromTestURL(test_url_1)); |
| 181 observer()->OnCategoryStatusChanged(category, |
| 182 ntp_snippets::CategoryStatus::AVAILABLE); |
| 183 observer()->OnNewSuggestions(category); |
| 184 ASSERT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); |
| 185 |
| 186 observer()->OnSuggestionInvalidated( |
| 187 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); |
| 188 |
| 189 EXPECT_EQ(1, test_prefetch_service()->remove_by_client_id_count); |
| 190 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), |
| 191 *test_prefetch_service()->last_removed_client_id); |
| 192 } |
| 193 |
| 194 } // namespace offline_pages |
OLD | NEW |