| 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/core/prefetch/suggested_articles_observer.h" | 5 #include "components/offline_pages/core/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 "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::vector<PrefetchURL>& prefetch_urls) override { |
| 37 latest_prefetch_urls = suggested_urls; | 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 ClientId& client_id) override { |
| 49 DCHECK_EQ(client_id.name_space, kSuggestedArticlesNamespace); | 51 DCHECK_EQ(kSuggestedArticlesNamespace, client_id.name_space); |
| 50 remove_by_client_id_count++; | 52 remove_by_client_id_count++; |
| 51 last_removed_client_id = base::MakeUnique<ClientId>(client_id); | 53 last_removed_client_id = client_id; |
| 52 } | 54 } |
| 53 | 55 |
| 54 void BeginBackgroundTask( | 56 void BeginBackgroundTask( |
| 55 std::unique_ptr<ScopedBackgroundTask> task) override {} | 57 std::unique_ptr<ScopedBackgroundTask> task) override {} |
| 56 void StopBackgroundTask(ScopedBackgroundTask* task) override {} | 58 void StopBackgroundTask(ScopedBackgroundTask* task) override {} |
| 57 | 59 |
| 58 std::vector<PrefetchURL> latest_prefetch_urls; | 60 std::vector<PrefetchURL> latest_prefetch_urls; |
| 59 std::unique_ptr<ClientId> last_removed_client_id; | 61 ClientId last_removed_client_id; |
| 60 | 62 |
| 61 int new_suggestions_count = 0; | 63 int new_suggestions_count = 0; |
| 62 int remove_all_suggestions_count = 0; | 64 int remove_all_suggestions_count = 0; |
| 63 int remove_by_client_id_count = 0; | 65 int remove_by_client_id_count = 0; |
| 64 }; | 66 }; |
| 65 | 67 |
| 66 class TestingPrefetchService : public PrefetchService { | 68 class TestingPrefetchService : public PrefetchService { |
| 67 public: | 69 public: |
| 68 TestingPrefetchService() = default; | 70 TestingPrefetchService() = default; |
| 69 | 71 |
| 72 OfflineMetricsCollector* GetOfflineMetricsCollector() override { |
| 73 return nullptr; |
| 74 } |
| 75 PrefetchDispatcher* GetPrefetchDispatcher() override { return &dispatcher; } |
| 76 PrefetchGCMHandler* GetPrefetchGCMHandler() override { return nullptr; } |
| 77 PrefetchStore* GetPrefetchStore() override { return nullptr; } |
| 70 void ObserveContentSuggestionsService( | 78 void ObserveContentSuggestionsService( |
| 71 ntp_snippets::ContentSuggestionsService* content_suggestions_service) | 79 ntp_snippets::ContentSuggestionsService* content_suggestions_service) |
| 72 override {} | 80 override {} |
| 73 PrefetchDispatcher* GetDispatcher() override { return &dispatcher; }; | |
| 74 OfflineMetricsCollector* GetOfflineMetricsCollector() override { | |
| 75 return nullptr; | |
| 76 } | |
| 77 PrefetchGCMHandler* GetPrefetchGCMHandler() override { return nullptr; } | |
| 78 | 81 |
| 79 TestingPrefetchDispatcher dispatcher; | 82 TestingPrefetchDispatcher dispatcher; |
| 80 }; | 83 }; |
| 84 |
| 81 } // namespace | 85 } // namespace |
| 82 | 86 |
| 83 class OfflinePageSuggestedArticlesObserverTest : public testing::Test { | 87 class OfflinePageSuggestedArticlesObserverTest : public testing::Test { |
| 84 public: | 88 public: |
| 85 OfflinePageSuggestedArticlesObserverTest() = default; | 89 OfflinePageSuggestedArticlesObserverTest() = default; |
| 86 | 90 |
| 87 void SetUp() override { | 91 void SetUp() override { |
| 88 observer_ = base::MakeUnique<SuggestedArticlesObserver>( | 92 observer_ = base::MakeUnique<SuggestedArticlesObserver>( |
| 89 nullptr, test_prefetch_service()); | 93 nullptr, test_prefetch_service()); |
| 90 } | 94 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 ContentSuggestionFromTestURL(test_url_1)); | 156 ContentSuggestionFromTestURL(test_url_1)); |
| 153 observer()->OnCategoryStatusChanged(category, | 157 observer()->OnCategoryStatusChanged(category, |
| 154 ntp_snippets::CategoryStatus::AVAILABLE); | 158 ntp_snippets::CategoryStatus::AVAILABLE); |
| 155 observer()->OnNewSuggestions(category); | 159 observer()->OnNewSuggestions(category); |
| 156 ASSERT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); | 160 ASSERT_EQ(1U, test_prefetch_dispatcher()->latest_prefetch_urls.size()); |
| 157 | 161 |
| 158 observer()->OnSuggestionInvalidated( | 162 observer()->OnSuggestionInvalidated( |
| 159 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); | 163 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); |
| 160 | 164 |
| 161 EXPECT_EQ(1, test_prefetch_dispatcher()->remove_by_client_id_count); | 165 EXPECT_EQ(1, test_prefetch_dispatcher()->remove_by_client_id_count); |
| 162 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), | 166 EXPECT_EQ(test_url_1.spec(), |
| 163 *test_prefetch_dispatcher()->last_removed_client_id); | 167 test_prefetch_dispatcher()->last_removed_client_id.id); |
| 168 EXPECT_EQ( |
| 169 kSuggestedArticlesNamespace, |
| 170 test_prefetch_dispatcher()->latest_prefetch_urls[0].client_id.name_space); |
| 164 } | 171 } |
| 165 | 172 |
| 166 } // namespace offline_pages | 173 } // namespace offline_pages |
| OLD | NEW |