| 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 "components/offline_pages/content/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 "components/offline_pages/core/client_namespace_constants.h" | 10 #include "components/offline_pages/core/client_namespace_constants.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { | 25 ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { |
| 26 auto category = | 26 auto category = |
| 27 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | 27 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| 28 return ContentSuggestion(category, test_url.spec(), test_url); | 28 return ContentSuggestion(category, test_url.spec(), test_url); |
| 29 } | 29 } |
| 30 | 30 |
| 31 class TestingPrefetchDispatcher : public PrefetchDispatcher { | 31 class TestingPrefetchDispatcher : public PrefetchDispatcher { |
| 32 public: | 32 public: |
| 33 TestingPrefetchDispatcher() = default; | 33 TestingPrefetchDispatcher() = default; |
| 34 | 34 |
| 35 void SetService(PrefetchService* service) override{}; |
| 36 |
| 35 void AddCandidatePrefetchURLs( | 37 void AddCandidatePrefetchURLs( |
| 36 const std::vector<PrefetchURL>& suggested_urls) override { | 38 const std::string& name_space, |
| 37 latest_prefetch_urls = suggested_urls; | 39 const std::vector<PrefetchURL>& prefetch_urls) override { |
| 40 latest_name_space = name_space; |
| 41 latest_prefetch_urls = prefetch_urls; |
| 38 new_suggestions_count++; | 42 new_suggestions_count++; |
| 39 } | 43 } |
| 40 | 44 |
| 41 void RemoveAllUnprocessedPrefetchURLs( | 45 void RemoveAllUnprocessedPrefetchURLs( |
| 42 const std::string& name_space) override { | 46 const std::string& name_space) override { |
| 43 DCHECK_EQ(name_space, kSuggestedArticlesNamespace); | 47 DCHECK_EQ(kSuggestedArticlesNamespace, name_space); |
| 44 latest_prefetch_urls.clear(); | 48 latest_prefetch_urls.clear(); |
| 45 remove_all_suggestions_count++; | 49 remove_all_suggestions_count++; |
| 46 } | 50 } |
| 47 | 51 |
| 48 void RemovePrefetchURLsByClientId(const ClientId& client_id) override { | 52 void RemovePrefetchURLsByClientId(const std::string& name_space, |
| 49 DCHECK_EQ(client_id.name_space, kSuggestedArticlesNamespace); | 53 const std::string& client_id) override { |
| 54 DCHECK_EQ(kSuggestedArticlesNamespace, name_space); |
| 50 remove_by_client_id_count++; | 55 remove_by_client_id_count++; |
| 51 last_removed_client_id = base::MakeUnique<ClientId>(client_id); | 56 last_removed_name_space = name_space; |
| 57 last_removed_client_id = client_id; |
| 52 } | 58 } |
| 53 | 59 |
| 54 void BeginBackgroundTask( | 60 void BeginBackgroundTask( |
| 55 std::unique_ptr<ScopedBackgroundTask> task) override {} | 61 std::unique_ptr<ScopedBackgroundTask> task) override {} |
| 56 void StopBackgroundTask(ScopedBackgroundTask* task) override {} | 62 void StopBackgroundTask(ScopedBackgroundTask* task) override {} |
| 57 | 63 |
| 64 std::string latest_name_space; |
| 58 std::vector<PrefetchURL> latest_prefetch_urls; | 65 std::vector<PrefetchURL> latest_prefetch_urls; |
| 59 std::unique_ptr<ClientId> last_removed_client_id; | 66 std::string last_removed_name_space; |
| 67 std::string last_removed_client_id; |
| 60 | 68 |
| 61 int new_suggestions_count = 0; | 69 int new_suggestions_count = 0; |
| 62 int remove_all_suggestions_count = 0; | 70 int remove_all_suggestions_count = 0; |
| 63 int remove_by_client_id_count = 0; | 71 int remove_by_client_id_count = 0; |
| 64 }; | 72 }; |
| 65 | 73 |
| 66 class TestingPrefetchService : public PrefetchService { | 74 class TestingPrefetchService : public PrefetchService { |
| 67 public: | 75 public: |
| 68 TestingPrefetchService() = default; | 76 TestingPrefetchService() = default; |
| 69 | 77 |
| 70 PrefetchDispatcher* GetDispatcher() override { return &dispatcher; }; | 78 PrefetchDispatcher* GetDispatcher() override { return &dispatcher; }; |
| 79 PrefetchStore* GetStore() override { |
| 80 CHECK(false); |
| 81 return nullptr; |
| 82 }; |
| 71 | 83 |
| 72 TestingPrefetchDispatcher dispatcher; | 84 TestingPrefetchDispatcher dispatcher; |
| 73 }; | 85 }; |
| 74 | 86 |
| 75 class TestDelegate : public SuggestedArticlesObserver::Delegate { | 87 class TestDelegate : public SuggestedArticlesObserver::Delegate { |
| 76 public: | 88 public: |
| 77 TestDelegate() = default; | 89 TestDelegate() = default; |
| 78 ~TestDelegate() override = default; | 90 ~TestDelegate() override = default; |
| 79 | 91 |
| 80 const std::vector<ContentSuggestion>& GetSuggestions( | 92 const std::vector<ContentSuggestion>& GetSuggestions( |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 test_delegate()->suggestions.push_back( | 173 test_delegate()->suggestions.push_back( |
| 162 ContentSuggestionFromTestURL(test_url_1)); | 174 ContentSuggestionFromTestURL(test_url_1)); |
| 163 | 175 |
| 164 observer()->OnCategoryStatusChanged(category, | 176 observer()->OnCategoryStatusChanged(category, |
| 165 ntp_snippets::CategoryStatus::AVAILABLE); | 177 ntp_snippets::CategoryStatus::AVAILABLE); |
| 166 observer()->OnNewSuggestions(category); | 178 observer()->OnNewSuggestions(category); |
| 167 EXPECT_EQ(1, test_prefetch_dispatcher()->new_suggestions_count); | 179 EXPECT_EQ(1, test_prefetch_dispatcher()->new_suggestions_count); |
| 168 EXPECT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); | 180 EXPECT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); |
| 169 EXPECT_EQ(test_url_1, | 181 EXPECT_EQ(test_url_1, |
| 170 test_prefetch_dispatcher()->latest_prefetch_urls[0].url); | 182 test_prefetch_dispatcher()->latest_prefetch_urls[0].url); |
| 171 EXPECT_EQ( | 183 EXPECT_EQ(kSuggestedArticlesNamespace, |
| 172 kSuggestedArticlesNamespace, | 184 test_prefetch_dispatcher()->latest_name_space); |
| 173 test_prefetch_dispatcher()->latest_prefetch_urls[0].client_id.name_space); | |
| 174 } | 185 } |
| 175 | 186 |
| 176 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { | 187 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { |
| 177 const GURL test_url_1("https://www.example.com/1"); | 188 const GURL test_url_1("https://www.example.com/1"); |
| 178 const GURL test_url_2("https://www.example.com/2"); | 189 const GURL test_url_2("https://www.example.com/2"); |
| 179 test_delegate()->suggestions.push_back( | 190 test_delegate()->suggestions.push_back( |
| 180 ContentSuggestionFromTestURL(test_url_1)); | 191 ContentSuggestionFromTestURL(test_url_1)); |
| 181 test_delegate()->suggestions.push_back( | 192 test_delegate()->suggestions.push_back( |
| 182 ContentSuggestionFromTestURL(test_url_2)); | 193 ContentSuggestionFromTestURL(test_url_2)); |
| 183 | 194 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 201 ContentSuggestionFromTestURL(test_url_1)); | 212 ContentSuggestionFromTestURL(test_url_1)); |
| 202 observer()->OnCategoryStatusChanged(category, | 213 observer()->OnCategoryStatusChanged(category, |
| 203 ntp_snippets::CategoryStatus::AVAILABLE); | 214 ntp_snippets::CategoryStatus::AVAILABLE); |
| 204 observer()->OnNewSuggestions(category); | 215 observer()->OnNewSuggestions(category); |
| 205 ASSERT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); | 216 ASSERT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); |
| 206 | 217 |
| 207 observer()->OnSuggestionInvalidated( | 218 observer()->OnSuggestionInvalidated( |
| 208 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); | 219 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); |
| 209 | 220 |
| 210 EXPECT_EQ(1, test_prefetch_dispatcher()->remove_by_client_id_count); | 221 EXPECT_EQ(1, test_prefetch_dispatcher()->remove_by_client_id_count); |
| 211 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), | 222 EXPECT_EQ(test_url_1.spec(), |
| 212 *test_prefetch_dispatcher()->last_removed_client_id); | 223 test_prefetch_dispatcher()->last_removed_client_id); |
| 224 EXPECT_EQ(kSuggestedArticlesNamespace, |
| 225 test_prefetch_dispatcher()->last_removed_name_space); |
| 213 } | 226 } |
| 214 | 227 |
| 215 } // namespace offline_pages | 228 } // namespace offline_pages |
| OLD | NEW |