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

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

Issue 2406573002: 📰 Persist category dismissals (Closed)
Patch Set: address comments Created 4 years, 2 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 <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "components/ntp_snippets/category_info.h" 18 #include "components/ntp_snippets/category_info.h"
19 #include "components/ntp_snippets/category_status.h" 19 #include "components/ntp_snippets/category_status.h"
20 #include "components/ntp_snippets/content_suggestion.h" 20 #include "components/ntp_snippets/content_suggestion.h"
21 #include "components/ntp_snippets/content_suggestions_provider.h" 21 #include "components/ntp_snippets/content_suggestions_provider.h"
22 #include "components/prefs/testing_pref_service.h"
22 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/gfx/image/image.h" 25 #include "ui/gfx/image/image.h"
25 26
26 using testing::ElementsAre; 27 using testing::ElementsAre;
27 using testing::Eq; 28 using testing::Eq;
28 using testing::InvokeWithoutArgs; 29 using testing::InvokeWithoutArgs;
29 using testing::IsEmpty; 30 using testing::IsEmpty;
30 using testing::Mock; 31 using testing::Mock;
31 using testing::Property; 32 using testing::Property;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); 115 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void());
115 116
116 private: 117 private:
117 DISALLOW_COPY_AND_ASSIGN(MockServiceObserver); 118 DISALLOW_COPY_AND_ASSIGN(MockServiceObserver);
118 }; 119 };
119 120
120 } // namespace 121 } // namespace
121 122
122 class ContentSuggestionsServiceTest : public testing::Test { 123 class ContentSuggestionsServiceTest : public testing::Test {
123 public: 124 public:
124 ContentSuggestionsServiceTest() {} 125 ContentSuggestionsServiceTest()
126 : pref_service_(new TestingPrefServiceSimple()) {}
125 127
126 void SetUp() override { 128 void SetUp() override {
129 RegisterPrefs();
127 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); 130 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED);
128 } 131 }
129 132
130 void TearDown() override { 133 void TearDown() override {
131 service_->Shutdown(); 134 service_->Shutdown();
132 service_.reset(); 135 service_.reset();
133 } 136 }
134 137
135 // Verifies that exactly the suggestions with the given |numbers| are 138 // Verifies that exactly the suggestions with the given |numbers| are
136 // returned by the service for the given |category|. 139 // returned by the service for the given |category|.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 std::unique_ptr<MockProvider> provider = base::MakeUnique<MockProvider>( 191 std::unique_ptr<MockProvider> provider = base::MakeUnique<MockProvider>(
189 service(), category_factory(), provided_categories); 192 service(), category_factory(), provided_categories);
190 MockProvider* result = provider.get(); 193 MockProvider* result = provider.get();
191 service()->RegisterProvider(std::move(provider)); 194 service()->RegisterProvider(std::move(provider));
192 return result; 195 return result;
193 } 196 }
194 197
195 MOCK_METHOD1(OnImageFetched, void(const gfx::Image&)); 198 MOCK_METHOD1(OnImageFetched, void(const gfx::Image&));
196 199
197 protected: 200 protected:
201 void RegisterPrefs() {
202 ContentSuggestionsService::RegisterProfilePrefs(pref_service_->registry());
203 UserClassifier::RegisterProfilePrefs(pref_service_->registry());
Marc Treib 2016/10/17 08:44:46 Is this required now? If so, nit: #include it!
Marc Treib 2016/10/18 07:44:31 Still open
dgn 2016/10/18 09:27:18 Oops, done. Because we now provide a non null pref
204 }
205
198 void CreateContentSuggestionsService( 206 void CreateContentSuggestionsService(
199 ContentSuggestionsService::State enabled) { 207 ContentSuggestionsService::State enabled) {
200 ASSERT_FALSE(service_); 208 ASSERT_FALSE(service_);
201 service_.reset(new ContentSuggestionsService(enabled, 209 service_.reset(new ContentSuggestionsService(
202 nullptr /* history_service */, 210 enabled, /*history_service=*/nullptr, pref_service_.get()));
203 nullptr /* pref_service */));
204 } 211 }
205 212
206 ContentSuggestionsService* service() { return service_.get(); } 213 ContentSuggestionsService* service() { return service_.get(); }
207 214
208 // Returns a suggestion instance for testing. 215 // Returns a suggestion instance for testing.
209 ContentSuggestion CreateSuggestion(Category category, int number) { 216 ContentSuggestion CreateSuggestion(Category category, int number) {
210 return ContentSuggestion( 217 return ContentSuggestion(
211 category, base::IntToString(number), 218 category, base::IntToString(number),
212 GURL("http://testsuggestion/" + base::IntToString(number))); 219 GURL("http://testsuggestion/" + base::IntToString(number)));
213 } 220 }
214 221
215 std::vector<ContentSuggestion> CreateSuggestions( 222 std::vector<ContentSuggestion> CreateSuggestions(
216 Category category, 223 Category category,
217 const std::vector<int>& numbers) { 224 const std::vector<int>& numbers) {
218 std::vector<ContentSuggestion> result; 225 std::vector<ContentSuggestion> result;
219 for (int number : numbers) { 226 for (int number : numbers) {
220 result.push_back(CreateSuggestion(category, number)); 227 result.push_back(CreateSuggestion(category, number));
221 } 228 }
222 return result; 229 return result;
223 } 230 }
224 231
225 private: 232 private:
226 std::unique_ptr<ContentSuggestionsService> service_; 233 std::unique_ptr<ContentSuggestionsService> service_;
234 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
227 235
228 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest); 236 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest);
229 }; 237 };
230 238
231 class ContentSuggestionsServiceDisabledTest 239 class ContentSuggestionsServiceDisabledTest
232 : public ContentSuggestionsServiceTest { 240 : public ContentSuggestionsServiceTest {
233 public: 241 public:
234 void SetUp() override { 242 void SetUp() override {
243 RegisterPrefs();
235 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); 244 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED);
236 } 245 }
237 }; 246 };
238 247
239 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) { 248 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) {
240 EXPECT_THAT(service()->state(), 249 EXPECT_THAT(service()->state(),
241 Eq(ContentSuggestionsService::State::ENABLED)); 250 Eq(ContentSuggestionsService::State::ENABLED));
242 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 251 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES);
243 Category offline_pages_category = 252 Category offline_pages_category =
244 FromKnownCategory(KnownCategories::DOWNLOADS); 253 FromKnownCategory(KnownCategories::DOWNLOADS);
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 service()->RestoreDismissedCategories(); 644 service()->RestoreDismissedCategories();
636 645
637 EXPECT_THAT(service()->GetCategories(), ElementsAre(category)); 646 EXPECT_THAT(service()->GetCategories(), ElementsAre(category));
638 EXPECT_THAT(service()->GetCategoryStatus(category), 647 EXPECT_THAT(service()->GetCategoryStatus(category),
639 Eq(CategoryStatus::AVAILABLE)); 648 Eq(CategoryStatus::AVAILABLE));
640 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty()); 649 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty());
641 EXPECT_THAT(providers().count(category), Eq(1ul)); 650 EXPECT_THAT(providers().count(category), Eq(1ul));
642 EXPECT_THAT(dismissed_providers(), IsEmpty()); 651 EXPECT_THAT(dismissed_providers(), IsEmpty());
643 } 652 }
644 653
654 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissedCategories) {
655 // Create and register provider
Marc Treib 2016/10/17 08:44:46 nitty nit: Comment should end in "."
dgn 2016/10/18 09:27:18 Done.
656 Category category1 = service()->category_factory()->FromIDValue(1);
657 Category category2 = service()->category_factory()->FromIDValue(2);
658
659 // Setup and verify initial state
660 MockProvider* provider = RegisterProvider({category1, category2});
661 provider->FireCategoryStatusChangedWithCurrentStatus(category1);
662 provider->FireCategoryStatusChangedWithCurrentStatus(category2);
663
664 ASSERT_THAT(service()->GetCategoryStatus(category1),
665 Eq(CategoryStatus::AVAILABLE));
666 ASSERT_THAT(service()->GetCategoryStatus(category2),
667 Eq(CategoryStatus::AVAILABLE));
668
669 // Dismiss all the categories. None should be provided now.
670 service()->DismissCategory(category1);
671 service()->DismissCategory(category2);
672
673 ASSERT_THAT(service()->GetCategoryStatus(category1),
674 Eq(CategoryStatus::NOT_PROVIDED));
675 ASSERT_THAT(service()->GetCategoryStatus(category2),
676 Eq(CategoryStatus::NOT_PROVIDED));
677
678 // Receiving a status change notification should not change anything.
679 provider->FireCategoryStatusChanged(category1, CategoryStatus::AVAILABLE);
680
681 EXPECT_THAT(service()->GetCategoryStatus(category1),
682 Eq(CategoryStatus::NOT_PROVIDED));
683 EXPECT_THAT(service()->GetCategoryStatus(category2),
684 Eq(CategoryStatus::NOT_PROVIDED));
685
686 // Receiving a notification without suggestions should not change anything.
687 provider->FireSuggestionsChanged(category1, std::vector<ContentSuggestion>());
688
689 EXPECT_THAT(service()->GetCategoryStatus(category1),
690 Eq(CategoryStatus::NOT_PROVIDED));
691 EXPECT_THAT(service()->GetCategoryStatus(category2),
692 Eq(CategoryStatus::NOT_PROVIDED));
693
694 // Receiving suggestions should make the notified category available.
695 provider->FireSuggestionsChanged(category1,
696 CreateSuggestions(category1, {1, 2}));
697
698 EXPECT_THAT(service()->GetCategoryStatus(category1),
699 Eq(CategoryStatus::AVAILABLE));
700 EXPECT_THAT(service()->GetCategoryStatus(category2),
701 Eq(CategoryStatus::NOT_PROVIDED));
702 }
703
645 } // namespace ntp_snippets 704 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698