OLD | NEW |
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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 void(const std::string& suggestion_id, | 94 void(const std::string& suggestion_id, |
95 const ImageFetchedCallback& callback)); | 95 const ImageFetchedCallback& callback)); |
96 | 96 |
97 private: | 97 private: |
98 std::vector<Category> provided_categories_; | 98 std::vector<Category> provided_categories_; |
99 std::map<int, CategoryStatus> statuses_; | 99 std::map<int, CategoryStatus> statuses_; |
100 }; | 100 }; |
101 | 101 |
102 class MockServiceObserver : public ContentSuggestionsService::Observer { | 102 class MockServiceObserver : public ContentSuggestionsService::Observer { |
103 public: | 103 public: |
104 MOCK_METHOD0(OnNewSuggestions, void()); | 104 MOCK_METHOD1(OnNewSuggestions, void(Category category)); |
105 MOCK_METHOD2(OnCategoryStatusChanged, | 105 MOCK_METHOD2(OnCategoryStatusChanged, |
106 void(Category changed_category, CategoryStatus new_status)); | 106 void(Category changed_category, CategoryStatus new_status)); |
107 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); | 107 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); |
108 ~MockServiceObserver() override {} | 108 ~MockServiceObserver() override {} |
109 }; | 109 }; |
110 | 110 |
111 } // namespace | 111 } // namespace |
112 | 112 |
113 class ContentSuggestionsServiceTest : public testing::Test { | 113 class ContentSuggestionsServiceTest : public testing::Test { |
114 public: | 114 public: |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 MockProvider* provider1 = MakeProvider(articles_category); | 308 MockProvider* provider1 = MakeProvider(articles_category); |
309 MockProvider* provider2 = MakeProvider(offline_pages_category); | 309 MockProvider* provider2 = MakeProvider(offline_pages_category); |
310 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); | 310 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); |
311 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); | 311 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); |
312 | 312 |
313 // Create and register observer | 313 // Create and register observer |
314 MockServiceObserver observer; | 314 MockServiceObserver observer; |
315 service()->AddObserver(&observer); | 315 service()->AddObserver(&observer); |
316 | 316 |
317 // Send suggestions 1 and 2 | 317 // Send suggestions 1 and 2 |
318 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); | 318 EXPECT_CALL(observer, OnNewSuggestions(articles_category)).Times(1); |
319 provider1->FireSuggestionsChanged(articles_category, {1, 2}); | 319 provider1->FireSuggestionsChanged(articles_category, {1, 2}); |
320 ExpectThatSuggestionsAre(articles_category, {1, 2}); | 320 ExpectThatSuggestionsAre(articles_category, {1, 2}); |
321 Mock::VerifyAndClearExpectations(&observer); | 321 Mock::VerifyAndClearExpectations(&observer); |
322 | 322 |
323 // Send them again, make sure they're not reported twice | 323 // Send them again, make sure they're not reported twice |
324 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); | 324 EXPECT_CALL(observer, OnNewSuggestions(articles_category)).Times(1); |
325 provider1->FireSuggestionsChanged(articles_category, {1, 2}); | 325 provider1->FireSuggestionsChanged(articles_category, {1, 2}); |
326 ExpectThatSuggestionsAre(articles_category, {1, 2}); | 326 ExpectThatSuggestionsAre(articles_category, {1, 2}); |
327 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>()); | 327 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>()); |
328 Mock::VerifyAndClearExpectations(&observer); | 328 Mock::VerifyAndClearExpectations(&observer); |
329 | 329 |
330 // Send suggestions 13 and 14 | 330 // Send suggestions 13 and 14 |
331 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); | 331 EXPECT_CALL(observer, OnNewSuggestions(offline_pages_category)).Times(1); |
332 provider2->FireSuggestionsChanged(offline_pages_category, {13, 14}); | 332 provider2->FireSuggestionsChanged(offline_pages_category, {13, 14}); |
333 ExpectThatSuggestionsAre(articles_category, {1, 2}); | 333 ExpectThatSuggestionsAre(articles_category, {1, 2}); |
334 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); | 334 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); |
335 Mock::VerifyAndClearExpectations(&observer); | 335 Mock::VerifyAndClearExpectations(&observer); |
336 | 336 |
337 // Send suggestion 1 only | 337 // Send suggestion 1 only |
338 EXPECT_CALL(observer, OnNewSuggestions()).Times(1); | 338 EXPECT_CALL(observer, OnNewSuggestions(articles_category)).Times(1); |
339 provider1->FireSuggestionsChanged(articles_category, {1}); | 339 provider1->FireSuggestionsChanged(articles_category, {1}); |
340 ExpectThatSuggestionsAre(articles_category, {1}); | 340 ExpectThatSuggestionsAre(articles_category, {1}); |
341 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); | 341 ExpectThatSuggestionsAre(offline_pages_category, {13, 14}); |
342 Mock::VerifyAndClearExpectations(&observer); | 342 Mock::VerifyAndClearExpectations(&observer); |
343 | 343 |
344 // provider2 reports OFFLINE_PAGES as unavailable | 344 // provider2 reports OFFLINE_PAGES as unavailable |
345 EXPECT_CALL(observer, OnCategoryStatusChanged( | 345 EXPECT_CALL(observer, OnCategoryStatusChanged( |
346 offline_pages_category, | 346 offline_pages_category, |
347 CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)) | 347 CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)) |
348 .Times(1); | 348 .Times(1); |
349 provider2->FireCategoryStatusChanged( | 349 provider2->FireCategoryStatusChanged( |
350 offline_pages_category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); | 350 offline_pages_category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
351 EXPECT_THAT(service()->GetCategoryStatus(articles_category), | 351 EXPECT_THAT(service()->GetCategoryStatus(articles_category), |
352 Eq(CategoryStatus::AVAILABLE)); | 352 Eq(CategoryStatus::AVAILABLE)); |
353 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), | 353 EXPECT_THAT(service()->GetCategoryStatus(offline_pages_category), |
354 Eq(CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); | 354 Eq(CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); |
355 ExpectThatSuggestionsAre(articles_category, {1}); | 355 ExpectThatSuggestionsAre(articles_category, {1}); |
356 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>()); | 356 ExpectThatSuggestionsAre(offline_pages_category, std::vector<int>()); |
357 Mock::VerifyAndClearExpectations(&observer); | 357 Mock::VerifyAndClearExpectations(&observer); |
358 | 358 |
359 // Shutdown the service | 359 // Shutdown the service |
360 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); | 360 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); |
361 service()->Shutdown(); | 361 service()->Shutdown(); |
362 service()->RemoveObserver(&observer); | 362 service()->RemoveObserver(&observer); |
363 // The service will receive two Shutdown() calls. | 363 // The service will receive two Shutdown() calls. |
364 } | 364 } |
365 | 365 |
366 } // namespace ntp_snippets | 366 } // namespace ntp_snippets |
OLD | NEW |