Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/offline_pages/suggestions_observer.h" | |
| 6 | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/test/test_simple_task_runner.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "chrome/test/base/testing_profile.h" | |
| 11 #include "components/offline_pages/core/client_namespace_constants.h" | |
| 12 #include "components/offline_pages/core/stub_offline_page_model.h" | |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "url/gurl.h" | |
| 16 | |
| 17 using ntp_snippets::Category; | |
| 18 using ntp_snippets::ContentSuggestion; | |
| 19 | |
| 20 namespace offline_pages { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 ContentSuggestion ContentSuggestionFromTestURL(const GURL& test_url) { | |
| 25 auto category = | |
| 26 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | |
| 27 return ContentSuggestion(category, test_url.spec(), test_url); | |
| 28 } | |
| 29 | |
| 30 class TestingPrefetchService : public PrefetchService { | |
| 31 public: | |
| 32 TestingPrefetchService() = default; | |
| 33 | |
| 34 void OnNewURLsToPrefetch( | |
| 35 const std::vector<PrefetchURL>& suggested_urls) override { | |
| 36 latest_prefetch_urls = suggested_urls; | |
| 37 new_suggestions_count++; | |
| 38 } | |
| 39 | |
| 40 void RemoveAllUnprocessedPrefetchURLs( | |
| 41 const std::string& name_space) override { | |
| 42 DCHECK_EQ(name_space, kSuggestedArticlesNamespace); | |
| 43 latest_prefetch_urls.clear(); | |
| 44 remove_all_suggestions_count++; | |
| 45 } | |
| 46 | |
| 47 void RemovePrefetchURLsByClientId(const ClientId& client_id) override { | |
| 48 DCHECK_EQ(client_id.name_space, kSuggestedArticlesNamespace); | |
| 49 remove_by_client_id_count++; | |
| 50 last_removed_client_id = base::MakeUnique<ClientId>(client_id); | |
| 51 } | |
| 52 | |
| 53 std::vector<PrefetchURL> latest_prefetch_urls; | |
| 54 std::unique_ptr<ClientId> last_removed_client_id; | |
| 55 | |
| 56 int new_suggestions_count = 0; | |
| 57 int remove_all_suggestions_count = 0; | |
| 58 int remove_by_client_id_count = 0; | |
| 59 }; | |
| 60 | |
| 61 class TestDelegate : public SuggestionsObserver::Delegate { | |
| 62 public: | |
| 63 TestDelegate() = default; | |
| 64 ~TestDelegate() override = default; | |
| 65 | |
| 66 const std::vector<ContentSuggestion>& GetSuggestions( | |
| 67 const Category& category) override { | |
| 68 get_suggestions_count++; | |
| 69 return suggestions; | |
| 70 } | |
| 71 | |
| 72 PrefetchService* GetPrefetchService( | |
| 73 content::BrowserContext* context) override { | |
| 74 return &prefetch_service; | |
| 75 } | |
| 76 | |
| 77 TestingPrefetchService prefetch_service; | |
| 78 | |
| 79 // Public for test manipulation. | |
| 80 std::vector<ContentSuggestion> suggestions; | |
| 81 | |
| 82 // Signals that delegate was called. | |
| 83 int get_suggestions_count = 0; | |
| 84 }; | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 class OfflinePageSuggestionsObserverTest : public testing::Test { | |
| 89 public: | |
| 90 OfflinePageSuggestionsObserverTest() = default; | |
| 91 | |
| 92 void SetUp() override { | |
| 93 observer_ = base::MakeUnique<SuggestionsObserver>(&profile_, category, | |
| 94 MakeDelegate()); | |
| 95 } | |
| 96 | |
| 97 virtual std::unique_ptr<SuggestionsObserver::Delegate> MakeDelegate() { | |
| 98 auto delegate_ptr = base::MakeUnique<TestDelegate>(); | |
| 99 test_delegate_ = delegate_ptr.get(); | |
| 100 return std::move(delegate_ptr); | |
| 101 } | |
| 102 | |
| 103 SuggestionsObserver* observer() { return observer_.get(); } | |
| 104 | |
| 105 TestDelegate* test_delegate() { return test_delegate_; } | |
| 106 TestingPrefetchService* test_prefetch_service() { | |
| 107 return &(test_delegate()->prefetch_service); | |
| 108 } | |
| 109 | |
| 110 protected: | |
| 111 Category category = | |
| 112 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | |
| 113 content::TestBrowserThreadBundle thread_bundle_; | |
| 114 TestingProfile profile_; | |
| 115 | |
| 116 private: | |
| 117 std::unique_ptr<SuggestionsObserver> observer_; | |
| 118 TestDelegate* test_delegate_; | |
| 119 }; | |
| 120 | |
| 121 TEST_F(OfflinePageSuggestionsObserverTest, CallsDelegateOnNewSuggestions) { | |
| 122 // We should not do anything if the category is not loaded. | |
| 123 observer()->OnNewSuggestions(category); | |
| 124 EXPECT_EQ(0, test_delegate()->get_suggestions_count); | |
| 125 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); | |
| 126 | |
| 127 // Once the category becomes available, new suggestions should cause us to ask | |
| 128 // the delegate for suggestion URLs. | |
| 129 observer()->OnCategoryStatusChanged(category, | |
| 130 ntp_snippets::CategoryStatus::AVAILABLE); | |
| 131 observer()->OnNewSuggestions(category); | |
| 132 EXPECT_EQ(1, test_delegate()->get_suggestions_count); | |
| 133 | |
| 134 // We expect that no pages were forwarded to the prefetch service since no | |
| 135 // pages were prepopulated. | |
| 136 EXPECT_EQ(0, test_prefetch_service()->new_suggestions_count); | |
| 137 } | |
| 138 | |
| 139 TEST_F(OfflinePageSuggestionsObserverTest, | |
| 140 ForwardsSuggestionsToPrefetchService) { | |
| 141 const GURL test_url_1("https://www.example.com/1"); | |
| 142 test_delegate()->suggestions.push_back( | |
| 143 ContentSuggestionFromTestURL(test_url_1)); | |
| 144 | |
| 145 observer()->OnCategoryStatusChanged(category, | |
| 146 ntp_snippets::CategoryStatus::AVAILABLE); | |
| 147 observer()->OnNewSuggestions(category); | |
| 148 EXPECT_EQ(1, test_prefetch_service()->new_suggestions_count); | |
| 149 EXPECT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); | |
| 150 EXPECT_EQ(test_url_1, test_prefetch_service()->latest_prefetch_urls[0].url); | |
| 151 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.
| |
| 152 EXPECT_EQ( | |
| 153 kSuggestedArticlesNamespace, | |
| 154 test_prefetch_service()->latest_prefetch_urls[0].client_id.name_space); | |
| 155 } | |
| 156 | |
| 157 TEST_F(OfflinePageSuggestionsObserverTest, RemovesAllOnBadStatus) { | |
| 158 const GURL test_url_1("https://www.example.com/1"); | |
| 159 const GURL test_url_2("https://www.example.com/2"); | |
| 160 test_delegate()->suggestions.push_back( | |
| 161 ContentSuggestionFromTestURL(test_url_1)); | |
| 162 test_delegate()->suggestions.push_back( | |
| 163 ContentSuggestionFromTestURL(test_url_2)); | |
| 164 | |
| 165 observer()->OnCategoryStatusChanged(category, | |
| 166 ntp_snippets::CategoryStatus::AVAILABLE); | |
| 167 observer()->OnNewSuggestions(category); | |
| 168 ASSERT_EQ(2U, test_prefetch_service()->latest_prefetch_urls.size()); | |
| 169 | |
| 170 observer()->OnCategoryStatusChanged( | |
| 171 category, ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); | |
| 172 EXPECT_EQ(1, test_prefetch_service()->remove_all_suggestions_count); | |
| 173 observer()->OnCategoryStatusChanged( | |
| 174 category, | |
| 175 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED); | |
| 176 EXPECT_EQ(2, test_prefetch_service()->remove_all_suggestions_count); | |
| 177 } | |
| 178 | |
| 179 TEST_F(OfflinePageSuggestionsObserverTest, RemovesClientIdOnInvalidated) { | |
| 180 const GURL test_url_1("https://www.example.com/1"); | |
| 181 test_delegate()->suggestions.push_back( | |
| 182 ContentSuggestionFromTestURL(test_url_1)); | |
| 183 observer()->OnCategoryStatusChanged(category, | |
| 184 ntp_snippets::CategoryStatus::AVAILABLE); | |
| 185 observer()->OnNewSuggestions(category); | |
| 186 ASSERT_EQ(1U, test_prefetch_service()->latest_prefetch_urls.size()); | |
| 187 | |
| 188 observer()->OnSuggestionInvalidated( | |
| 189 ntp_snippets::ContentSuggestion::ID(category, test_url_1.spec())); | |
| 190 | |
| 191 EXPECT_EQ(1, test_prefetch_service()->remove_by_client_id_count); | |
| 192 EXPECT_EQ(ClientId(kSuggestedArticlesNamespace, test_url_1.spec()), | |
| 193 *test_prefetch_service()->last_removed_client_id); | |
| 194 } | |
| 195 | |
| 196 } // namespace offline_pages | |
| OLD | NEW |