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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 | 80 |
81 void FireSuggestionsChanged(Category category, std::vector<int> numbers) { | 81 void FireSuggestionsChanged(Category category, std::vector<int> numbers) { |
82 observer()->OnNewSuggestions(this, category, CreateSuggestions(numbers)); | 82 observer()->OnNewSuggestions(this, category, CreateSuggestions(numbers)); |
83 } | 83 } |
84 | 84 |
85 void FireCategoryStatusChanged(Category category, CategoryStatus new_status) { | 85 void FireCategoryStatusChanged(Category category, CategoryStatus new_status) { |
86 statuses_[category.id()] = new_status; | 86 statuses_[category.id()] = new_status; |
87 observer()->OnCategoryStatusChanged(this, category, new_status); | 87 observer()->OnCategoryStatusChanged(this, category, new_status); |
88 } | 88 } |
89 | 89 |
90 void FireSuggestionInvalidated(Category category, | |
91 const std::string& suggestion_id) { | |
92 observer()->OnSuggestionInvalidated(this, category, suggestion_id); | |
93 } | |
94 | |
90 MOCK_METHOD1(ClearCachedSuggestionsForDebugging, void(Category category)); | 95 MOCK_METHOD1(ClearCachedSuggestionsForDebugging, void(Category category)); |
91 MOCK_METHOD1(GetDismissedSuggestionsForDebugging, | 96 MOCK_METHOD1(GetDismissedSuggestionsForDebugging, |
92 std::vector<ContentSuggestion>(Category category)); | 97 std::vector<ContentSuggestion>(Category category)); |
93 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category category)); | 98 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category category)); |
94 MOCK_METHOD1(DismissSuggestion, void(const std::string& suggestion_id)); | 99 MOCK_METHOD1(DismissSuggestion, void(const std::string& suggestion_id)); |
95 MOCK_METHOD2(FetchSuggestionImage, | 100 MOCK_METHOD2(FetchSuggestionImage, |
96 void(const std::string& suggestion_id, | 101 void(const std::string& suggestion_id, |
97 const ImageFetchedCallback& callback)); | 102 const ImageFetchedCallback& callback)); |
98 | 103 |
99 private: | 104 private: |
100 std::vector<Category> provided_categories_; | 105 std::vector<Category> provided_categories_; |
101 std::map<int, CategoryStatus> statuses_; | 106 std::map<int, CategoryStatus> statuses_; |
102 }; | 107 }; |
103 | 108 |
104 class MockServiceObserver : public ContentSuggestionsService::Observer { | 109 class MockServiceObserver : public ContentSuggestionsService::Observer { |
105 public: | 110 public: |
106 MOCK_METHOD1(OnNewSuggestions, void(Category category)); | 111 MOCK_METHOD1(OnNewSuggestions, void(Category category)); |
107 MOCK_METHOD2(OnCategoryStatusChanged, | 112 MOCK_METHOD2(OnCategoryStatusChanged, |
108 void(Category changed_category, CategoryStatus new_status)); | 113 void(Category changed_category, CategoryStatus new_status)); |
114 MOCK_METHOD2(OnSuggestionInvalidated, | |
115 void(Category category, const std::string& suggestion_id)); | |
109 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); | 116 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); |
110 ~MockServiceObserver() override {} | 117 ~MockServiceObserver() override {} |
111 }; | 118 }; |
112 | 119 |
113 } // namespace | 120 } // namespace |
114 | 121 |
115 class ContentSuggestionsServiceTest : public testing::Test { | 122 class ContentSuggestionsServiceTest : public testing::Test { |
116 public: | 123 public: |
117 ContentSuggestionsServiceTest() {} | 124 ContentSuggestionsServiceTest() {} |
118 | 125 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 MockProvider* provider2 = MakeProvider(offline_pages_category); | 301 MockProvider* provider2 = MakeProvider(offline_pages_category); |
295 | 302 |
296 provider2->FireSuggestionsChanged(offline_pages_category, {11}); | 303 provider2->FireSuggestionsChanged(offline_pages_category, {11}); |
297 std::string suggestion_id = CreateSuggestion(11).id(); | 304 std::string suggestion_id = CreateSuggestion(11).id(); |
298 | 305 |
299 EXPECT_CALL(*provider1, DismissSuggestion(_)).Times(0); | 306 EXPECT_CALL(*provider1, DismissSuggestion(_)).Times(0); |
300 EXPECT_CALL(*provider2, DismissSuggestion(suggestion_id)).Times(1); | 307 EXPECT_CALL(*provider2, DismissSuggestion(suggestion_id)).Times(1); |
301 service()->DismissSuggestion(suggestion_id); | 308 service()->DismissSuggestion(suggestion_id); |
302 } | 309 } |
303 | 310 |
311 TEST_F(ContentSuggestionsServiceTest, ShouldRedirectSuggestionInvalidated) { | |
312 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | |
313 | |
314 MockProvider* provider = MakeProvider(articles_category); | |
315 MockServiceObserver observer; | |
316 service()->AddObserver(&observer); | |
317 | |
318 provider->FireSuggestionsChanged(articles_category, {11, 12, 13}); | |
319 ExpectThatSuggestionsAre(articles_category, {11, 12, 13}); | |
320 std::string suggestion_id = CreateSuggestion(12).id(); | |
321 | |
322 EXPECT_CALL(observer, | |
323 OnSuggestionInvalidated(articles_category, suggestion_id)) | |
324 .Times(1); | |
Marc Treib
2016/08/16 09:03:54
As I've recently learned, ".Times(1)" is a no-op,
Philipp Keck
2016/08/16 10:54:40
Done (for the entire file).
| |
325 provider->FireSuggestionInvalidated(articles_category, suggestion_id); | |
326 ExpectThatSuggestionsAre(articles_category, {11, 13}); | |
Marc Treib
2016/08/16 09:03:54
Add a case with a suggestion that isn't known to t
Philipp Keck
2016/08/16 10:54:40
Done.
| |
327 service()->RemoveObserver(&observer); | |
328 } | |
329 | |
304 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { | 330 TEST_F(ContentSuggestionsServiceTest, ShouldForwardSuggestions) { |
305 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 331 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
306 Category offline_pages_category = | 332 Category offline_pages_category = |
307 FromKnownCategory(KnownCategories::BOOKMARKS); | 333 FromKnownCategory(KnownCategories::BOOKMARKS); |
308 | 334 |
309 // Create and register providers | 335 // Create and register providers |
310 MockProvider* provider1 = MakeProvider(articles_category); | 336 MockProvider* provider1 = MakeProvider(articles_category); |
311 MockProvider* provider2 = MakeProvider(offline_pages_category); | 337 MockProvider* provider2 = MakeProvider(offline_pages_category); |
312 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); | 338 EXPECT_THAT(providers().at(articles_category), Eq(provider1)); |
313 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); | 339 EXPECT_THAT(providers().at(offline_pages_category), Eq(provider2)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 Mock::VerifyAndClearExpectations(&observer); | 385 Mock::VerifyAndClearExpectations(&observer); |
360 | 386 |
361 // Shutdown the service | 387 // Shutdown the service |
362 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); | 388 EXPECT_CALL(observer, ContentSuggestionsServiceShutdown()); |
363 service()->Shutdown(); | 389 service()->Shutdown(); |
364 service()->RemoveObserver(&observer); | 390 service()->RemoveObserver(&observer); |
365 // The service will receive two Shutdown() calls. | 391 // The service will receive two Shutdown() calls. |
366 } | 392 } |
367 | 393 |
368 } // namespace ntp_snippets | 394 } // namespace ntp_snippets |
OLD | NEW |