Index: components/offline_pages/core/prefetch/suggested_articles_observer_unittest.cc |
diff --git a/components/offline_pages/content/suggested_articles_observer_unittest.cc b/components/offline_pages/core/prefetch/suggested_articles_observer_unittest.cc |
similarity index 67% |
rename from components/offline_pages/content/suggested_articles_observer_unittest.cc |
rename to components/offline_pages/core/prefetch/suggested_articles_observer_unittest.cc |
index 2a988ddf3673abb16806b75846de6321a0650e82..0cf7051c75450478ce6231770e1132db809b8246 100644 |
--- a/components/offline_pages/content/suggested_articles_observer_unittest.cc |
+++ b/components/offline_pages/core/prefetch/suggested_articles_observer_unittest.cc |
@@ -2,14 +2,13 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "components/offline_pages/content/suggested_articles_observer.h" |
+#include "components/offline_pages/core/prefetch/suggested_articles_observer.h" |
#include "base/run_loop.h" |
#include "base/test/test_simple_task_runner.h" |
#include "base/threading/thread_task_runner_handle.h" |
#include "components/offline_pages/core/client_namespace_constants.h" |
#include "components/offline_pages/core/stub_offline_page_model.h" |
-#include "content/public/test/test_browser_context.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "url/gurl.h" |
@@ -30,6 +29,10 @@ class TestingPrefetchService : public PrefetchService { |
public: |
TestingPrefetchService() = default; |
+ void ObserveContentSuggestionsService( |
+ ntp_snippets::ContentSuggestionsService* content_suggestions_service) |
+ override {} |
+ |
void AddCandidatePrefetchURLs( |
const std::vector<PrefetchURL>& suggested_urls) override { |
latest_prefetch_urls = suggested_urls; |
@@ -61,31 +64,6 @@ class TestingPrefetchService : public PrefetchService { |
int remove_by_client_id_count = 0; |
}; |
-class TestDelegate : public SuggestedArticlesObserver::Delegate { |
- public: |
- TestDelegate() = default; |
- ~TestDelegate() override = default; |
- |
- const std::vector<ContentSuggestion>& GetSuggestions( |
- const Category& category) override { |
- get_suggestions_count++; |
- return suggestions; |
- } |
- |
- PrefetchService* GetPrefetchService( |
- content::BrowserContext* context) override { |
- return &prefetch_service; |
- } |
- |
- TestingPrefetchService prefetch_service; |
- |
- // Public for test manipulation. |
- std::vector<ContentSuggestion> suggestions; |
- |
- // Signals that delegate was called. |
- int get_suggestions_count = 0; |
-}; |
- |
} // namespace |
class OfflinePageSuggestedArticlesObserverTest : public testing::Test { |
@@ -93,56 +71,27 @@ class OfflinePageSuggestedArticlesObserverTest : public testing::Test { |
OfflinePageSuggestedArticlesObserverTest() = default; |
void SetUp() override { |
- observer_ = |
- base::MakeUnique<SuggestedArticlesObserver>(&context_, MakeDelegate()); |
- } |
- |
- virtual std::unique_ptr<SuggestedArticlesObserver::Delegate> MakeDelegate() { |
- auto delegate_ptr = base::MakeUnique<TestDelegate>(); |
- test_delegate_ = delegate_ptr.get(); |
- return std::move(delegate_ptr); |
+ observer_ = base::MakeUnique<SuggestedArticlesObserver>( |
+ nullptr, test_prefetch_service()); |
} |
SuggestedArticlesObserver* observer() { return observer_.get(); } |
- TestDelegate* test_delegate() { return test_delegate_; } |
- TestingPrefetchService* test_prefetch_service() { |
- return &(test_delegate()->prefetch_service); |
- } |
+ TestingPrefetchService* test_prefetch_service() { return &prefetch_service_; } |
protected: |
Category category = |
Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
- content::TestBrowserContext context_; |
private: |
std::unique_ptr<SuggestedArticlesObserver> observer_; |
- TestDelegate* test_delegate_; |
+ TestingPrefetchService prefetch_service_; |
}; |
TEST_F(OfflinePageSuggestedArticlesObserverTest, |
- CallsDelegateOnNewSuggestions) { |
- // We should not do anything if the category is not loaded. |
- observer()->OnNewSuggestions(category); |
- EXPECT_EQ(0, test_delegate()->get_suggestions_count); |
- EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); |
- |
- // Once the category becomes available, new suggestions should cause us to ask |
- // the delegate for suggestion URLs. |
- observer()->OnCategoryStatusChanged(category, |
- ntp_snippets::CategoryStatus::AVAILABLE); |
- observer()->OnNewSuggestions(category); |
- EXPECT_EQ(1, test_delegate()->get_suggestions_count); |
- |
- // We expect that no pages were forwarded to the prefetch service since no |
- // pages were prepopulated. |
- EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); |
-} |
- |
-TEST_F(OfflinePageSuggestedArticlesObserverTest, |
ForwardsSuggestionsToPrefetchService) { |
const GURL test_url_1("https://www.example.com/1"); |
- test_delegate()->suggestions.push_back( |
+ observer()->GetTestingArticles()->push_back( |
ContentSuggestionFromTestURL(test_url_1)); |
observer()->OnCategoryStatusChanged(category, |
@@ -159,9 +108,9 @@ TEST_F(OfflinePageSuggestedArticlesObserverTest, |
TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { |
const GURL test_url_1("https://www.example.com/1"); |
const GURL test_url_2("https://www.example.com/2"); |
- test_delegate()->suggestions.push_back( |
+ observer()->GetTestingArticles()->push_back( |
ContentSuggestionFromTestURL(test_url_1)); |
- test_delegate()->suggestions.push_back( |
+ observer()->GetTestingArticles()->push_back( |
ContentSuggestionFromTestURL(test_url_2)); |
observer()->OnCategoryStatusChanged(category, |
@@ -180,7 +129,7 @@ TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesAllOnBadStatus) { |
TEST_F(OfflinePageSuggestedArticlesObserverTest, RemovesClientIdOnInvalidated) { |
const GURL test_url_1("https://www.example.com/1"); |
- test_delegate()->suggestions.push_back( |
+ observer()->GetTestingArticles()->push_back( |
ContentSuggestionFromTestURL(test_url_1)); |
observer()->OnCategoryStatusChanged(category, |
ntp_snippets::CategoryStatus::AVAILABLE); |