Chromium Code Reviews| Index: chrome/browser/android/offline_pages/suggestions_observer_unittest.cc |
| diff --git a/chrome/browser/android/offline_pages/suggestions_observer_unittest.cc b/chrome/browser/android/offline_pages/suggestions_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..763b0f3f6723f24708b262fd9150738d132ef261 |
| --- /dev/null |
| +++ b/chrome/browser/android/offline_pages/suggestions_observer_unittest.cc |
| @@ -0,0 +1,196 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/offline_pages/suggestions_observer.h" |
| + |
| +#include "base/run_loop.h" |
| +#include "base/test/test_simple_task_runner.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "chrome/test/base/testing_profile.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_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +using ntp_snippets::Category; |
| +using ntp_snippets::ContentSuggestion; |
| + |
| +namespace offline_pages { |
| + |
| +namespace { |
| + |
| +ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { |
| + auto category = |
| + Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| + return ContentSuggestion(category, test_url.spec(), test_url); |
| +} |
| + |
| +class TestingPrefetchService : public PrefetchService { |
| + public: |
| + TestingPrefetchService() = default; |
| + |
| + void OnNewURLsToPrefetch( |
| + const std::vector<PrefetchURL>& suggested_urls) override { |
| + latest_prefetch_urls = suggested_urls; |
| + new_suggestions_count++; |
| + } |
| + |
| + void RemoveAllUnprocessedPrefetchURLs( |
| + const std::string& name_space) override { |
| + DCHECK_EQ(name_space, kSuggestedArticlesNamespace); |
| + latest_prefetch_urls.clear(); |
| + remove_all_suggestions_count++; |
| + } |
| + |
| + void RemovePrefetchURLsByClientId(const ClientId& client_id) override { |
| + DCHECK_EQ(client_id.name_space, kSuggestedArticlesNamespace); |
| + remove_by_client_id_count++; |
| + last_removed_client_id = base::MakeUnique<ClientId>(client_id); |
| + } |
| + |
| + std::vector<PrefetchURL> latest_prefetch_urls; |
| + std::unique_ptr<ClientId> last_removed_client_id; |
| + |
| + int new_suggestions_count = 0; |
| + int remove_all_suggestions_count = 0; |
| + int remove_by_client_id_count = 0; |
| +}; |
| + |
| +class TestDelegate : public SuggestionsObserver::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 OfflinePageSuggestionsObserverTest : public testing::Test { |
| + public: |
| + OfflinePageSuggestionsObserverTest() = default; |
| + |
| + void SetUp() override { |
| + observer_ = base::MakeUnique<SuggestionsObserver>(&profile_, category, |
| + MakeDelegate()); |
| + } |
| + |
| + virtual std::unique_ptr<SuggestionsObserver::Delegate> MakeDelegate() { |
| + auto delegate_ptr = base::MakeUnique<TestDelegate>(); |
| + test_delegate_ = delegate_ptr.get(); |
| + return std::move(delegate_ptr); |
| + } |
| + |
| + SuggestionsObserver* observer() { return observer_.get(); } |
| + |
| + TestDelegate* test_delegate() { return test_delegate_; } |
| + TestingPrefetchService* test_prefetch_service() { |
| + return &(test_delegate()->prefetch_service); |
| + } |
| + |
| + protected: |
| + Category category = |
| + Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + TestingProfile profile_; |
| + |
| + private: |
| + std::unique_ptr<SuggestionsObserver> observer_; |
| + TestDelegate* test_delegate_; |
| +}; |
| + |
| +TEST_F(OfflinePageSuggestionsObserverTest, 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(OfflinePageSuggestionsObserverTest, |
| + ForwardsSuggestionsToPrefetchService) { |
| + const GURL test_url_1("https://www.example.com/1"); |
| + test_delegate()->suggestions.push_back( |
| + ContentSuggestionFromTestURL(test_url_1)); |
| + |
| + observer()->OnCategoryStatusChanged(category, |
| + ntp_snippets::CategoryStatus::AVAILABLE); |
| + observer()->OnNewSuggestions(category); |
| + EXPECT_EQ(1, test_prefetch_service()->new_suggestions_count); |
| + EXPECT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); |
| + EXPECT_EQ(test_url_1, test_prefetch_service()->latest_prefetch_urls[0].url); |
| + EXPECT_EQ(test_url_1, test_prefetch_service()->latest_prefetch_urls[0].url); |
|
carlosk
2017/04/18 00:36:09
Duplicate line.
dewittj
2017/04/18 18:20:25
Done.
|
| + EXPECT_EQ( |
| + kSuggestedArticlesNamespace, |
| + test_prefetch_service()->latest_prefetch_urls[0].client_id.name_space); |
| +} |
| + |
| +TEST_F(OfflinePageSuggestionsObserverTest, 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( |
| + ContentSuggestionFromTestURL(test_url_1)); |
| + test_delegate()->suggestions.push_back( |
| + ContentSuggestionFromTestURL(test_url_2)); |
| + |
| + observer()->OnCategoryStatusChanged(category, |
| + ntp_snippets::CategoryStatus::AVAILABLE); |
| + observer()->OnNewSuggestions(category); |
| + ASSERT_EQ(2U, test_prefetch_service()->latest_prefetch_urls.size()); |
| + |
| + observer()->OnCategoryStatusChanged( |
| + category, ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
| + EXPECT_EQ(1, test_prefetch_service()->remove_all_suggestions_count); |
| + observer()->OnCategoryStatusChanged( |
| + category, |
| + ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED); |
| + EXPECT_EQ(2, test_prefetch_service()->remove_all_suggestions_count); |
| +} |
| + |
| +TEST_F(OfflinePageSuggestionsObserverTest, RemovesClientIdOnInvalidated) { |
| + const GURL test_url_1("https://www.example.com/1"); |
| + test_delegate()->suggestions.push_back( |
| + ContentSuggestionFromTestURL(test_url_1)); |
| + observer()->OnCategoryStatusChanged(category, |
| + ntp_snippets::CategoryStatus::AVAILABLE); |
| + observer()->OnNewSuggestions(category); |
| + ASSERT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); |
| + |
| + observer()->OnSuggestionInvalidated( |
| + ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); |
| + |
| + EXPECT_EQ(1, test_prefetch_service()->remove_by_client_id_count); |
| + EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), |
| + *test_prefetch_service()->last_removed_client_id); |
| +} |
| + |
| +} // namespace offline_pages |