Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: components/ntp_snippets/content_suggestions_service_unittest.cc

Issue 2187233002: Add ContentSuggestionsCategoryFactory; Store categories as ints (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/ntp_snippets/content_suggestions_service.h" 5 #include "components/ntp_snippets/content_suggestions_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 std::vector<ContentSuggestion> CreateSuggestions(std::vector<int> numbers) { 42 std::vector<ContentSuggestion> CreateSuggestions(std::vector<int> numbers) {
43 std::vector<ContentSuggestion> result; 43 std::vector<ContentSuggestion> result;
44 for (int number : numbers) { 44 for (int number : numbers) {
45 result.emplace_back(CreateSuggestion(number)); 45 result.emplace_back(CreateSuggestion(number));
46 } 46 }
47 return result; 47 return result;
48 } 48 }
49 49
50 class MockProvider : public ContentSuggestionsProvider { 50 class MockProvider : public ContentSuggestionsProvider {
51 public: 51 public:
52 MockProvider(ContentSuggestionsCategory provided_category) 52 MockProvider(ContentSuggestionsCategoryFactory* category_factory,
53 ContentSuggestionsCategory provided_category)
53 : MockProvider( 54 : MockProvider(
55 category_factory,
54 std::vector<ContentSuggestionsCategory>({provided_category})){}; 56 std::vector<ContentSuggestionsCategory>({provided_category})){};
55 57
56 MockProvider(std::vector<ContentSuggestionsCategory> provided_categories) 58 MockProvider(ContentSuggestionsCategoryFactory* category_factory,
57 : ContentSuggestionsProvider(provided_categories), observer_(nullptr) { 59 std::vector<ContentSuggestionsCategory> provided_categories)
60 : ContentSuggestionsProvider(category_factory),
61 observer_(nullptr),
62 provided_categories_(provided_categories) {
58 for (ContentSuggestionsCategory category : provided_categories) { 63 for (ContentSuggestionsCategory category : provided_categories) {
59 statuses_[category] = ContentSuggestionsCategoryStatus::AVAILABLE; 64 statuses_[category.id()] = ContentSuggestionsCategoryStatus::AVAILABLE;
60 } 65 }
61 } 66 }
62 67
68 std::vector<ContentSuggestionsCategory> provided_categories() override {
69 return provided_categories_;
70 }
71
63 Observer* observer() { return observer_; } 72 Observer* observer() { return observer_; }
64 73
65 void SetObserver(Observer* observer) override { observer_ = observer; } 74 void SetObserver(Observer* observer) override { observer_ = observer; }
66 75
67 ContentSuggestionsCategoryStatus GetCategoryStatus( 76 ContentSuggestionsCategoryStatus GetCategoryStatus(
68 ContentSuggestionsCategory category) { 77 ContentSuggestionsCategory category) {
69 return statuses_[category]; 78 return statuses_[category.id()];
70 } 79 }
71 80
72 void FireSuggestionsChanged(ContentSuggestionsCategory category, 81 void FireSuggestionsChanged(ContentSuggestionsCategory category,
73 std::vector<int> numbers) { 82 std::vector<int> numbers) {
74 observer_->OnNewSuggestions(category, CreateSuggestions(numbers)); 83 observer_->OnNewSuggestions(category, CreateSuggestions(numbers));
75 } 84 }
76 85
77 void FireCategoryStatusChanged(ContentSuggestionsCategory category, 86 void FireCategoryStatusChanged(ContentSuggestionsCategory category,
78 ContentSuggestionsCategoryStatus new_status) { 87 ContentSuggestionsCategoryStatus new_status) {
79 statuses_[category] = new_status; 88 statuses_[category.id()] = new_status;
80 observer_->OnCategoryStatusChanged(category, new_status); 89 observer_->OnCategoryStatusChanged(category, new_status);
81 } 90 }
82 91
83 void FireShutdown() { 92 void FireShutdown() {
84 observer_->OnProviderShutdown(this); 93 observer_->OnProviderShutdown(this);
85 observer_ = nullptr; 94 observer_ = nullptr;
86 } 95 }
87 96
88 MOCK_METHOD0(ClearCachedSuggestionsForDebugging, void()); 97 MOCK_METHOD0(ClearCachedSuggestionsForDebugging, void());
89 MOCK_METHOD0(ClearDismissedSuggestionsForDebugging, void()); 98 MOCK_METHOD0(ClearDismissedSuggestionsForDebugging, void());
90 MOCK_METHOD1(DismissSuggestion, void(const std::string& suggestion_id)); 99 MOCK_METHOD1(DismissSuggestion, void(const std::string& suggestion_id));
91 MOCK_METHOD2(FetchSuggestionImage, 100 MOCK_METHOD2(FetchSuggestionImage,
92 void(const std::string& suggestion_id, 101 void(const std::string& suggestion_id,
93 const ImageFetchedCallback& callback)); 102 const ImageFetchedCallback& callback));
94 103
95 private: 104 private:
96 Observer* observer_; 105 Observer* observer_;
97 std::map<ContentSuggestionsCategory, ContentSuggestionsCategoryStatus> 106 std::vector<ContentSuggestionsCategory> provided_categories_;
98 statuses_; 107 std::map<int, ContentSuggestionsCategoryStatus> statuses_;
99 }; 108 };
100 109
101 class MockServiceObserver : public ContentSuggestionsService::Observer { 110 class MockServiceObserver : public ContentSuggestionsService::Observer {
102 public: 111 public:
103 MOCK_METHOD0(OnNewSuggestions, void()); 112 MOCK_METHOD0(OnNewSuggestions, void());
104 MOCK_METHOD2(OnCategoryStatusChanged, 113 MOCK_METHOD2(OnCategoryStatusChanged,
105 void(ContentSuggestionsCategory changed_category, 114 void(ContentSuggestionsCategory changed_category,
106 ContentSuggestionsCategoryStatus new_status)); 115 ContentSuggestionsCategoryStatus new_status));
107 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); 116 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void());
108 ~MockServiceObserver() override {} 117 ~MockServiceObserver() override {}
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 } else { 153 } else {
145 numbers.erase(position); 154 numbers.erase(position);
146 } 155 }
147 } 156 }
148 for (int number : numbers) { 157 for (int number : numbers) {
149 ADD_FAILURE() << "Suggestion number " << number 158 ADD_FAILURE() << "Suggestion number " << number
150 << " not present, though expected"; 159 << " not present, though expected";
151 } 160 }
152 } 161 }
153 162
154 const std::map<ContentSuggestionsCategory, ContentSuggestionsProvider*>& 163 const std::map<ContentSuggestionsCategory,
164 ContentSuggestionsProvider*,
165 ContentSuggestionsService::CompareCategoriesByID>&
155 providers() { 166 providers() {
156 return service()->providers_; 167 return service()->providers_;
157 } 168 }
158 169
170 ContentSuggestionsCategoryFactory* category_factory() {
171 return service()->category_factory();
172 }
173
174 ContentSuggestionsCategory FromKnownCategory(
175 KnownSuggestionsCategories known_category) {
176 return service()->category_factory()->FromKnownCategory(known_category);
177 }
178
159 MOCK_METHOD2(OnImageFetched, 179 MOCK_METHOD2(OnImageFetched,
160 void(const std::string& suggestion_id, const gfx::Image&)); 180 void(const std::string& suggestion_id, const gfx::Image&));
161 181
162 protected: 182 protected:
163 void CreateContentSuggestionsService( 183 void CreateContentSuggestionsService(
164 ContentSuggestionsService::State enabled) { 184 ContentSuggestionsService::State enabled) {
165 ASSERT_FALSE(service_); 185 ASSERT_FALSE(service_);
166 service_.reset(new ContentSuggestionsService(enabled)); 186 service_.reset(new ContentSuggestionsService(enabled));
167 } 187 }
168 188
169 ContentSuggestionsService* service() { return service_.get(); } 189 ContentSuggestionsService* service() { return service_.get(); }
170 190
171 private: 191 private:
172 std::unique_ptr<ContentSuggestionsService> service_; 192 std::unique_ptr<ContentSuggestionsService> service_;
173 193
174 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest); 194 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest);
175 }; 195 };
176 196
177 class ContentSuggestionsServiceDisabledTest 197 class ContentSuggestionsServiceDisabledTest
178 : public ContentSuggestionsServiceTest { 198 : public ContentSuggestionsServiceTest {
179 public: 199 public:
180 void SetUp() override { 200 void SetUp() override {
181 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); 201 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED);
182 } 202 }
183 }; 203 };
184 204
185 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProvidersAndShutdown) { 205 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProvidersAndShutdown) {
186 EXPECT_THAT(service()->state(), 206 EXPECT_THAT(service()->state(),
187 Eq(ContentSuggestionsService::State::ENABLED)); 207 Eq(ContentSuggestionsService::State::ENABLED));
188 MockProvider provider1(ContentSuggestionsCategory::ARTICLES); 208 ContentSuggestionsCategory articles_category =
189 MockProvider provider2(ContentSuggestionsCategory::OFFLINE_PAGES); 209 FromKnownCategory(KnownSuggestionsCategories::ARTICLES);
210 ContentSuggestionsCategory offline_pages_category =
211 FromKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES);
212 MockProvider provider1(category_factory(), articles_category);
213 MockProvider provider2(category_factory(), offline_pages_category);
190 ASSERT_THAT(provider1.observer(), IsNull()); 214 ASSERT_THAT(provider1.observer(), IsNull());
191 ASSERT_THAT(provider2.observer(), IsNull()); 215 ASSERT_THAT(provider2.observer(), IsNull());
192 ASSERT_THAT(providers(), IsEmpty()); 216 ASSERT_THAT(providers(), IsEmpty());
193 EXPECT_THAT(service()->GetCategories(), IsEmpty()); 217 EXPECT_THAT(service()->GetCategories(), IsEmpty());
194 EXPECT_THAT( 218 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
195 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 219 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
196 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED)); 220 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
197 EXPECT_THAT( 221 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
198 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
199 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
200 222
201 service()->RegisterProvider(&provider1); 223 service()->RegisterProvider(&provider1);
202 EXPECT_THAT(provider1.observer(), NotNull()); 224 EXPECT_THAT(provider1.observer(), NotNull());
203 EXPECT_THAT(providers().count(ContentSuggestionsCategory::OFFLINE_PAGES), 225 EXPECT_THAT(providers().count(offline_pages_category), Eq(0ul));
204 Eq(0ul)); 226 EXPECT_THAT(providers().at(articles_category), Eq(&provider1));
205 EXPECT_THAT(providers().at(ContentSuggestionsCategory::ARTICLES),
206 Eq(&provider1));
207 EXPECT_THAT(providers().size(), Eq(1ul)); 227 EXPECT_THAT(providers().size(), Eq(1ul));
208 EXPECT_THAT(service()->GetCategories(), 228 EXPECT_THAT(service()->GetCategories(), ElementsAre(articles_category));
209 ElementsAre(ContentSuggestionsCategory::ARTICLES)); 229 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
210 EXPECT_THAT( 230 Eq(ContentSuggestionsCategoryStatus::AVAILABLE));
211 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 231 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
212 Eq(ContentSuggestionsCategoryStatus::AVAILABLE)); 232 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
213 EXPECT_THAT(
214 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
215 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
216 233
217 service()->RegisterProvider(&provider2); 234 service()->RegisterProvider(&provider2);
218 EXPECT_THAT(provider1.observer(), NotNull()); 235 EXPECT_THAT(provider1.observer(), NotNull());
219 EXPECT_THAT(provider2.observer(), NotNull()); 236 EXPECT_THAT(provider2.observer(), NotNull());
220 EXPECT_THAT(providers().at(ContentSuggestionsCategory::ARTICLES), 237 EXPECT_THAT(providers().at(articles_category), Eq(&provider1));
221 Eq(&provider1)); 238 EXPECT_THAT(providers().at(offline_pages_category), Eq(&provider2));
222 EXPECT_THAT(providers().at(ContentSuggestionsCategory::OFFLINE_PAGES),
223 Eq(&provider2));
224 EXPECT_THAT(providers().size(), Eq(2ul)); 239 EXPECT_THAT(providers().size(), Eq(2ul));
225 EXPECT_THAT(service()->GetCategories(), 240 EXPECT_THAT(service()->GetCategories(),
226 ElementsAre(ContentSuggestionsCategory::ARTICLES, 241 ElementsAre(offline_pages_category, articles_category));
227 ContentSuggestionsCategory::OFFLINE_PAGES)); 242 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
228 EXPECT_THAT( 243 Eq(ContentSuggestionsCategoryStatus::AVAILABLE));
229 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 244 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
230 Eq(ContentSuggestionsCategoryStatus::AVAILABLE)); 245 Eq(ContentSuggestionsCategoryStatus::AVAILABLE));
231 EXPECT_THAT(
232 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
233 Eq(ContentSuggestionsCategoryStatus::AVAILABLE));
234 246
235 provider1.FireShutdown(); 247 provider1.FireShutdown();
236 EXPECT_THAT(providers().count(ContentSuggestionsCategory::ARTICLES), Eq(0ul)); 248 EXPECT_THAT(providers().count(articles_category), Eq(0ul));
237 EXPECT_THAT(providers().at(ContentSuggestionsCategory::OFFLINE_PAGES), 249 EXPECT_THAT(providers().at(offline_pages_category), Eq(&provider2));
238 Eq(&provider2));
239 EXPECT_THAT(providers().size(), Eq(1ul)); 250 EXPECT_THAT(providers().size(), Eq(1ul));
240 EXPECT_THAT(service()->GetCategories(), 251 EXPECT_THAT(service()->GetCategories(), ElementsAre(offline_pages_category));
241 ElementsAre(ContentSuggestionsCategory::OFFLINE_PAGES)); 252 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
242 EXPECT_THAT( 253 ContentSuggestionsCategoryStatus::NOT_PROVIDED);
243 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 254 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
244 ContentSuggestionsCategoryStatus::NOT_PROVIDED); 255 ContentSuggestionsCategoryStatus::AVAILABLE);
245 EXPECT_THAT(
246 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
247 ContentSuggestionsCategoryStatus::AVAILABLE);
248 256
249 provider2.FireShutdown(); 257 provider2.FireShutdown();
250 EXPECT_THAT(providers(), IsEmpty()); 258 EXPECT_THAT(providers(), IsEmpty());
251 EXPECT_THAT(service()->GetCategories(), IsEmpty()); 259 EXPECT_THAT(service()->GetCategories(), IsEmpty());
252 EXPECT_THAT( 260 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
253 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 261 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
254 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED)); 262 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
255 EXPECT_THAT( 263 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
256 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
257 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
258 } 264 }
259 265
260 TEST_F(ContentSuggestionsServiceDisabledTest, ShouldDoNothingWhenDisabled) { 266 TEST_F(ContentSuggestionsServiceDisabledTest, ShouldDoNothingWhenDisabled) {
267 ContentSuggestionsCategory articles_category =
268 FromKnownCategory(KnownSuggestionsCategories::ARTICLES);
269 ContentSuggestionsCategory offline_pages_category =
270 FromKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES);
261 EXPECT_THAT(service()->state(), 271 EXPECT_THAT(service()->state(),
262 Eq(ContentSuggestionsService::State::DISABLED)); 272 Eq(ContentSuggestionsService::State::DISABLED));
263 MockProvider provider1(ContentSuggestionsCategory::ARTICLES); 273 MockProvider provider1(category_factory(), articles_category);
264 service()->RegisterProvider(&provider1); 274 service()->RegisterProvider(&provider1);
265 EXPECT_THAT(providers(), IsEmpty()); 275 EXPECT_THAT(providers(), IsEmpty());
266 EXPECT_THAT( 276 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
267 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 277 Eq(ContentSuggestionsCategoryStatus::
268 Eq(ContentSuggestionsCategoryStatus:: 278 ALL_SUGGESTIONS_EXPLICITLY_DISABLED));
269 ALL_SUGGESTIONS_EXPLICITLY_DISABLED)); 279 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
270 EXPECT_THAT( 280 Eq(ContentSuggestionsCategoryStatus::
271 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES), 281 ALL_SUGGESTIONS_EXPLICITLY_DISABLED));
272 Eq(ContentSuggestionsCategoryStatus::
273 ALL_SUGGESTIONS_EXPLICITLY_DISABLED));
274 EXPECT_THAT(service()->GetCategories(), IsEmpty()); 282 EXPECT_THAT(service()->GetCategories(), IsEmpty());
275 EXPECT_THAT(service()->GetSuggestionsForCategory( 283 EXPECT_THAT(service()->GetSuggestionsForCategory(articles_category),
276 ContentSuggestionsCategory::ARTICLES),
277 IsEmpty()); 284 IsEmpty());
278 } 285 }
279 286
280 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) { 287 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) {
281 MockProvider provider1(ContentSuggestionsCategory::ARTICLES); 288 ContentSuggestionsCategory articles_category =
282 MockProvider provider2(ContentSuggestionsCategory::OFFLINE_PAGES); 289 FromKnownCategory(KnownSuggestionsCategories::ARTICLES);
290 ContentSuggestionsCategory offline_pages_category =
291 FromKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES);
292 MockProvider provider1(category_factory(), articles_category);
293 MockProvider provider2(category_factory(), offline_pages_category);
283 service()->RegisterProvider(&provider1); 294 service()->RegisterProvider(&provider1);
284 service()->RegisterProvider(&provider2); 295 service()->RegisterProvider(&provider2);
285 296
286 provider1.FireSuggestionsChanged(ContentSuggestionsCategory::ARTICLES, {1}); 297 provider1.FireSuggestionsChanged(articles_category, {1});
287 std::string suggestion_id = CreateSuggestion(1).id(); 298 std::string suggestion_id = CreateSuggestion(1).id();
288 299
289 EXPECT_CALL(provider1, FetchSuggestionImage(suggestion_id, _)).Times(1); 300 EXPECT_CALL(provider1, FetchSuggestionImage(suggestion_id, _)).Times(1);
290 EXPECT_CALL(provider2, FetchSuggestionImage(_, _)).Times(0); 301 EXPECT_CALL(provider2, FetchSuggestionImage(_, _)).Times(0);
291 service()->FetchSuggestionImage( 302 service()->FetchSuggestionImage(
292 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, 303 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched,
293 base::Unretained(this))); 304 base::Unretained(this)));
294 provider1.FireShutdown(); 305 provider1.FireShutdown();
295 provider2.FireShutdown(); 306 provider2.FireShutdown();
296 } 307 }
297 308
298 TEST_F(ContentSuggestionsServiceTest, 309 TEST_F(ContentSuggestionsServiceTest,
299 ShouldCallbackEmptyImageForUnavailableProvider) { 310 ShouldCallbackEmptyImageForUnavailableProvider) {
300 std::string suggestion_id = "TestID"; 311 std::string suggestion_id = "TestID";
301 EXPECT_CALL(*this, OnImageFetched(suggestion_id, 312 EXPECT_CALL(*this, OnImageFetched(suggestion_id,
302 Property(&gfx::Image::IsEmpty, Eq(true)))); 313 Property(&gfx::Image::IsEmpty, Eq(true))));
303 service()->FetchSuggestionImage( 314 service()->FetchSuggestionImage(
304 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched, 315 suggestion_id, base::Bind(&ContentSuggestionsServiceTest::OnImageFetched,
305 base::Unretained(this))); 316 base::Unretained(this)));
306 } 317 }
307 318
308 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectDismissSuggestion) { 319 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectDismissSuggestion) {
309 MockProvider provider1(ContentSuggestionsCategory::ARTICLES); 320 ContentSuggestionsCategory articles_category =
310 MockProvider provider2(ContentSuggestionsCategory::OFFLINE_PAGES); 321 FromKnownCategory(KnownSuggestionsCategories::ARTICLES);
322 ContentSuggestionsCategory offline_pages_category =
323 FromKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES);
324 MockProvider provider1(category_factory(), articles_category);
325 MockProvider provider2(category_factory(), offline_pages_category);
311 service()->RegisterProvider(&provider1); 326 service()->RegisterProvider(&provider1);
312 service()->RegisterProvider(&provider2); 327 service()->RegisterProvider(&provider2);
313 328
314 provider2.FireSuggestionsChanged(ContentSuggestionsCategory::OFFLINE_PAGES, 329 provider2.FireSuggestionsChanged(offline_pages_category, {11});
315 {11});
316 std::string suggestion_id = CreateSuggestion(11).id(); 330 std::string suggestion_id = CreateSuggestion(11).id();
317 331
318 EXPECT_CALL(provider1, DismissSuggestion(_)).Times(0); 332 EXPECT_CALL(provider1, DismissSuggestion(_)).Times(0);
319 EXPECT_CALL(provider2, DismissSuggestion(suggestion_id)).Times(1); 333 EXPECT_CALL(provider2, DismissSuggestion(suggestion_id)).Times(1);
320 service()->DismissSuggestion(suggestion_id); 334 service()->DismissSuggestion(suggestion_id);
321 provider1.FireShutdown(); 335 provider1.FireShutdown();
322 provider2.FireShutdown(); 336 provider2.FireShutdown();
323 } 337 }
324 338
325 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { 339 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) {
340 ContentSuggestionsCategory articles_category =
341 FromKnownCategory(KnownSuggestionsCategories::ARTICLES);
342 ContentSuggestionsCategory offline_pages_category =
343 FromKnownCategory(KnownSuggestionsCategories::OFFLINE_PAGES);
344
326 // Create and register providers 345 // Create and register providers
327 MockProvider provider1(ContentSuggestionsCategory::ARTICLES); 346 MockProvider provider1(category_factory(), articles_category);
328 MockProvider provider2(ContentSuggestionsCategory::OFFLINE_PAGES); 347 MockProvider provider2(category_factory(), offline_pages_category);
329 service()->RegisterProvider(&provider1); 348 service()->RegisterProvider(&provider1);
330 service()->RegisterProvider(&provider2); 349 service()->RegisterProvider(&provider2);
331 EXPECT_THAT(providers().at(ContentSuggestionsCategory::ARTICLES), 350 EXPECT_THAT(providers().at(articles_category), Eq(&provider1));
332 Eq(&provider1)); 351 EXPECT_THAT(providers().at(offline_pages_category), Eq(&provider2));
333 EXPECT_THAT(providers().at(ContentSuggestionsCategory::OFFLINE_PAGES),
334 Eq(&provider2));
335 352
336 // Create and register observer 353 // Create and register observer
337 MockServiceObserver observer; 354 MockServiceObserver observer;
338 service()->AddObserver(&observer); 355 service()->AddObserver(&observer);
339 356
340 // Send suggestions 1 and 2 357 // Send suggestions 1 and 2
341 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); 358 EXPECT_CALL(observer, OnNewSuggestions()).Times(1);
342 provider1.FireSuggestionsChanged(ContentSuggestionsCategory::ARTICLES, 359 provider1.FireSuggestionsChanged(articles_category, {1, 2});
343 {1, 2}); 360 ExpectThatSuggestionsAre(articles_category, {1, 2});
344 ExpectThatSuggestionsAre(ContentSuggestionsCategory::ARTICLES, {1, 2});
345 Mock::VerifyAndClearExpectations(&observer); 361 Mock::VerifyAndClearExpectations(&observer);
346 362
347 // Send them again, make sure they're not reported twice 363 // Send them again, make sure they're not reported twice
348 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); 364 EXPECT_CALL(observer, OnNewSuggestions()).Times(1);
349 provider1.FireSuggestionsChanged(ContentSuggestionsCategory::ARTICLES, 365 provider1.FireSuggestionsChanged(articles_category, {1, 2});
350 {1, 2}); 366 ExpectThatSuggestionsAre(articles_category, {1, 2});
351 ExpectThatSuggestionsAre(ContentSuggestionsCategory::ARTICLES, {1, 2}); 367 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>());
352 ExpectThatSuggestionsAre(ContentSuggestionsCategory::OFFLINE_PAGES,
353 std::vector<int>());
354 Mock::VerifyAndClearExpectations(&observer); 368 Mock::VerifyAndClearExpectations(&observer);
355 369
356 // Send suggestions 13 and 14 370 // Send suggestions 13 and 14
357 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); 371 EXPECT_CALL(observer, OnNewSuggestions()).Times(1);
358 provider2.FireSuggestionsChanged(ContentSuggestionsCategory::OFFLINE_PAGES, 372 provider2.FireSuggestionsChanged(offline_pages_category, {13, 14});
359 {13, 14}); 373 ExpectThatSuggestionsAre(articles_category, {1, 2});
360 ExpectThatSuggestionsAre(ContentSuggestionsCategory::ARTICLES, {1, 2}); 374 ExpectThatSuggestionsAre(offline_pages_category, {13, 14});
361 ExpectThatSuggestionsAre(ContentSuggestionsCategory::OFFLINE_PAGES, {13, 14});
362 Mock::VerifyAndClearExpectations(&observer); 375 Mock::VerifyAndClearExpectations(&observer);
363 376
364 // Send suggestion 1 only 377 // Send suggestion 1 only
365 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); 378 EXPECT_CALL(observer, OnNewSuggestions()).Times(1);
366 provider1.FireSuggestionsChanged(ContentSuggestionsCategory::ARTICLES, {1}); 379 provider1.FireSuggestionsChanged(articles_category, {1});
367 ExpectThatSuggestionsAre(ContentSuggestionsCategory::ARTICLES, {1}); 380 ExpectThatSuggestionsAre(articles_category, {1});
368 ExpectThatSuggestionsAre(ContentSuggestionsCategory::OFFLINE_PAGES, {13, 14}); 381 ExpectThatSuggestionsAre(offline_pages_category, {13, 14});
369 Mock::VerifyAndClearExpectations(&observer); 382 Mock::VerifyAndClearExpectations(&observer);
370 383
371 // provider2 reports OFFLINE_PAGEs as unavailable 384 // provider2 reports OFFLINE_PAGEs as unavailable
372 EXPECT_CALL( 385 EXPECT_CALL(
373 observer, 386 observer,
374 OnCategoryStatusChanged( 387 OnCategoryStatusChanged(
375 ContentSuggestionsCategory::OFFLINE_PAGES, 388 offline_pages_category,
376 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED)) 389 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED))
377 .Times(1); 390 .Times(1);
378 provider2.FireCategoryStatusChanged( 391 provider2.FireCategoryStatusChanged(
379 ContentSuggestionsCategory::OFFLINE_PAGES, 392 offline_pages_category,
380 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED); 393 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED);
394 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
395 Eq(ContentSuggestionsCategoryStatus::AVAILABLE));
381 EXPECT_THAT( 396 EXPECT_THAT(
382 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 397 service()->GetCategoryStatus(offline_pages_category),
383 Eq(ContentSuggestionsCategoryStatus::AVAILABLE));
384 EXPECT_THAT(
385 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
386 Eq(ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); 398 Eq(ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED));
387 ExpectThatSuggestionsAre(ContentSuggestionsCategory::ARTICLES, {1}); 399 ExpectThatSuggestionsAre(articles_category, {1});
388 ExpectThatSuggestionsAre(ContentSuggestionsCategory::OFFLINE_PAGES, 400 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>());
389 std::vector<int>());
390 Mock::VerifyAndClearExpectations(&observer); 401 Mock::VerifyAndClearExpectations(&observer);
391 402
392 // Let provider1 shut down 403 // Let provider1 shut down
393 EXPECT_CALL(observer, OnCategoryStatusChanged( 404 EXPECT_CALL(observer, OnCategoryStatusChanged(
394 ContentSuggestionsCategory::ARTICLES, 405 articles_category,
395 ContentSuggestionsCategoryStatus::NOT_PROVIDED)) 406 ContentSuggestionsCategoryStatus::NOT_PROVIDED))
396 .Times(1); 407 .Times(1);
397 provider1.FireShutdown(); 408 provider1.FireShutdown();
409 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
410 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
398 EXPECT_THAT( 411 EXPECT_THAT(
399 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 412 service()->GetCategoryStatus(offline_pages_category),
400 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
401 EXPECT_THAT(
402 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
403 Eq(ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); 413 Eq(ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED));
404 ExpectThatSuggestionsAre(ContentSuggestionsCategory::ARTICLES, 414 ExpectThatSuggestionsAre(articles_category, std::vector<int>());
405 std::vector<int>()); 415 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>());
406 ExpectThatSuggestionsAre(ContentSuggestionsCategory::OFFLINE_PAGES,
407 std::vector<int>());
408 Mock::VerifyAndClearExpectations(&observer); 416 Mock::VerifyAndClearExpectations(&observer);
409 417
410 // Let provider2 shut down 418 // Let provider2 shut down
411 provider2.FireShutdown(); 419 provider2.FireShutdown();
412 EXPECT_TRUE(providers().empty()); 420 EXPECT_TRUE(providers().empty());
413 EXPECT_THAT( 421 EXPECT_THAT(service()->GetCategoryStatus(articles_category),
414 service()->GetCategoryStatus(ContentSuggestionsCategory::ARTICLES), 422 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
415 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED)); 423 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category),
416 EXPECT_THAT( 424 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
417 service()->GetCategoryStatus(ContentSuggestionsCategory::OFFLINE_PAGES),
418 Eq(ContentSuggestionsCategoryStatus::NOT_PROVIDED));
419 425
420 // Shutdown the service 426 // Shutdown the service
421 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); 427 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown());
422 service()->Shutdown(); 428 service()->Shutdown();
423 service()->RemoveObserver(&observer); 429 service()->RemoveObserver(&observer);
424 // The service will receive two Shutdown() calls. 430 // The service will receive two Shutdown() calls.
425 } 431 }
426 432
433 // TODO(pke): Write a test for dynamic sections and ordering.
Marc Treib 2016/07/28 11:41:46 TODOs in the code aren't the best tool for a perso
Philipp Keck 2016/07/28 13:50:55 Removed.
434
427 } // namespace ntp_snippets 435 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698