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