| 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 15 matching lines...) Expand all Loading... |
| 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 AddCandidatePrefetchURLs( | 35 void AddCandidatePrefetchURLs( |
| 36 const std::vector<PrefetchURL>& suggested_urls) override { | 36 const std::string& name_space, |
| 37 latest_prefetch_urls = suggested_urls; | 37 const std::vector<PrefetchURL>& prefetch_urls) override { |
| 38 latest_name_space = name_space; |
| 39 latest_prefetch_urls = prefetch_urls; |
| 38 new_suggestions_count++; | 40 new_suggestions_count++; |
| 39 } | 41 } |
| 40 | 42 |
| 41 void RemoveAllUnprocessedPrefetchURLs( | 43 void RemoveAllUnprocessedPrefetchURLs( |
| 42 const std::string& name_space) override { | 44 const std::string& name_space) override { |
| 43 DCHECK_EQ(name_space, kSuggestedArticlesNamespace); | 45 DCHECK_EQ(kSuggestedArticlesNamespace, name_space); |
| 44 latest_prefetch_urls.clear(); | 46 latest_prefetch_urls.clear(); |
| 45 remove_all_suggestions_count++; | 47 remove_all_suggestions_count++; |
| 46 } | 48 } |
| 47 | 49 |
| 48 void RemovePrefetchURLsByClientId(const ClientId& client_id) override { | 50 void RemovePrefetchURLsByClientId(const std::string& name_space, |
| 49 DCHECK_EQ(client_id.name_space, kSuggestedArticlesNamespace); | 51 const std::string& client_id) override { |
| 52 DCHECK_EQ(kSuggestedArticlesNamespace, name_space); |
| 50 remove_by_client_id_count++; | 53 remove_by_client_id_count++; |
| 51 last_removed_client_id = base::MakeUnique<ClientId>(client_id); | 54 last_removed_name_space = name_space; |
| 55 last_removed_client_id = client_id; |
| 52 } | 56 } |
| 53 | 57 |
| 54 void BeginBackgroundTask( | 58 void BeginBackgroundTask( |
| 55 std::unique_ptr<ScopedBackgroundTask> task) override {} | 59 std::unique_ptr<ScopedBackgroundTask> task) override {} |
| 56 void StopBackgroundTask(ScopedBackgroundTask* task) override {} | 60 void StopBackgroundTask(ScopedBackgroundTask* task) override {} |
| 57 | 61 |
| 62 std::string latest_name_space; |
| 58 std::vector<PrefetchURL> latest_prefetch_urls; | 63 std::vector<PrefetchURL> latest_prefetch_urls; |
| 59 std::unique_ptr<ClientId> last_removed_client_id; | 64 std::string last_removed_name_space; |
| 65 std::string last_removed_client_id; |
| 60 | 66 |
| 61 int new_suggestions_count = 0; | 67 int new_suggestions_count = 0; |
| 62 int remove_all_suggestions_count = 0; | 68 int remove_all_suggestions_count = 0; |
| 63 int remove_by_client_id_count = 0; | 69 int remove_by_client_id_count = 0; |
| 64 }; | 70 }; |
| 65 | 71 |
| 66 class TestingPrefetchService : public PrefetchService { | 72 class TestingPrefetchService : public PrefetchService { |
| 67 public: | 73 public: |
| 68 TestingPrefetchService() = default; | 74 TestingPrefetchService() = default; |
| 69 | 75 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 test_delegate()->suggestions.push_back( | 167 test_delegate()->suggestions.push_back( |
| 162 ContentSuggestionFromTestURL(test_url_1)); | 168 ContentSuggestionFromTestURL(test_url_1)); |
| 163 | 169 |
| 164 observer()->OnCategoryStatusChanged(category, | 170 observer()->OnCategoryStatusChanged(category, |
| 165 ntp_snippets::CategoryStatus::AVAILABLE); | 171 ntp_snippets::CategoryStatus::AVAILABLE); |
| 166 observer()->OnNewSuggestions(category); | 172 observer()->OnNewSuggestions(category); |
| 167 EXPECT_EQ(1, test_prefetch_dispatcher()->new_suggestions_count); | 173 EXPECT_EQ(1, test_prefetch_dispatcher()->new_suggestions_count); |
| 168 EXPECT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); | 174 EXPECT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); |
| 169 EXPECT_EQ(test_url_1, | 175 EXPECT_EQ(test_url_1, |
| 170 test_prefetch_dispatcher()->latest_prefetch_urls[0].url); | 176 test_prefetch_dispatcher()->latest_prefetch_urls[0].url); |
| 171 EXPECT_EQ( | 177 EXPECT_EQ(kSuggestedArticlesNamespace, |
| 172 kSuggestedArticlesNamespace, | 178 test_prefetch_dispatcher()->latest_name_space); |
| 173 test_prefetch_dispatcher()->latest_prefetch_urls[0].client_id.name_space); | |
| 174 } | 179 } |
| 175 | 180 |
| 176 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { | 181 TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { |
| 177 const GURL test_url_1("https://www.example.com/1"); | 182 const GURL test_url_1("https://www.example.com/1"); |
| 178 const GURL test_url_2("https://www.example.com/2"); | 183 const GURL test_url_2("https://www.example.com/2"); |
| 179 test_delegate()->suggestions.push_back( | 184 test_delegate()->suggestions.push_back( |
| 180 ContentSuggestionFromTestURL(test_url_1)); | 185 ContentSuggestionFromTestURL(test_url_1)); |
| 181 test_delegate()->suggestions.push_back( | 186 test_delegate()->suggestions.push_back( |
| 182 ContentSuggestionFromTestURL(test_url_2)); | 187 ContentSuggestionFromTestURL(test_url_2)); |
| 183 | 188 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 201 ContentSuggestionFromTestURL(test_url_1)); | 206 ContentSuggestionFromTestURL(test_url_1)); |
| 202 observer()->OnCategoryStatusChanged(category, | 207 observer()->OnCategoryStatusChanged(category, |
| 203 ntp_snippets::CategoryStatus::AVAILABLE); | 208 ntp_snippets::CategoryStatus::AVAILABLE); |
| 204 observer()->OnNewSuggestions(category); | 209 observer()->OnNewSuggestions(category); |
| 205 ASSERT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); | 210 ASSERT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); |
| 206 | 211 |
| 207 observer()->OnSuggestionInvalidated( | 212 observer()->OnSuggestionInvalidated( |
| 208 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); | 213 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); |
| 209 | 214 |
| 210 EXPECT_EQ(1, test_prefetch_dispatcher()->remove_by_client_id_count); | 215 EXPECT_EQ(1, test_prefetch_dispatcher()->remove_by_client_id_count); |
| 211 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), | 216 EXPECT_EQ(test_url_1.spec(), |
| 212 *test_prefetch_dispatcher()->last_removed_client_id); | 217 test_prefetch_dispatcher()->last_removed_client_id); |
| 218 EXPECT_EQ(kSuggestedArticlesNamespace, |
| 219 test_prefetch_dispatcher()->last_removed_name_space); |
| 213 } | 220 } |
| 214 | 221 |
| 215 } // namespace offline_pages | 222 } // namespace offline_pages |
| OLD | NEW |