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

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

Issue 2406573002: 📰 Persist category dismissals (Closed)
Patch Set: 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());
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 */)); 211 }
212
213 void ResetService() {
214 service_->Shutdown();
215 service_.reset();
216 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED);
204 } 217 }
205 218
206 ContentSuggestionsService* service() { return service_.get(); } 219 ContentSuggestionsService* service() { return service_.get(); }
207 220
208 // Returns a suggestion instance for testing. 221 // Returns a suggestion instance for testing.
209 ContentSuggestion CreateSuggestion(Category category, int number) { 222 ContentSuggestion CreateSuggestion(Category category, int number) {
210 return ContentSuggestion( 223 return ContentSuggestion(
211 category, base::IntToString(number), 224 category, base::IntToString(number),
212 GURL("http://testsuggestion/" + base::IntToString(number))); 225 GURL("http://testsuggestion/" + base::IntToString(number)));
213 } 226 }
214 227
215 std::vector<ContentSuggestion> CreateSuggestions( 228 std::vector<ContentSuggestion> CreateSuggestions(
216 Category category, 229 Category category,
217 const std::vector<int>& numbers) { 230 const std::vector<int>& numbers) {
218 std::vector<ContentSuggestion> result; 231 std::vector<ContentSuggestion> result;
219 for (int number : numbers) { 232 for (int number : numbers) {
220 result.push_back(CreateSuggestion(category, number)); 233 result.push_back(CreateSuggestion(category, number));
221 } 234 }
222 return result; 235 return result;
223 } 236 }
224 237
225 private: 238 private:
226 std::unique_ptr<ContentSuggestionsService> service_; 239 std::unique_ptr<ContentSuggestionsService> service_;
240 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
227 241
228 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest); 242 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest);
229 }; 243 };
230 244
231 class ContentSuggestionsServiceDisabledTest 245 class ContentSuggestionsServiceDisabledTest
232 : public ContentSuggestionsServiceTest { 246 : public ContentSuggestionsServiceTest {
233 public: 247 public:
234 void SetUp() override { 248 void SetUp() override {
249 RegisterPrefs();
235 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); 250 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED);
236 } 251 }
237 }; 252 };
238 253
239 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) { 254 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) {
240 EXPECT_THAT(service()->state(), 255 EXPECT_THAT(service()->state(),
241 Eq(ContentSuggestionsService::State::ENABLED)); 256 Eq(ContentSuggestionsService::State::ENABLED));
242 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); 257 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES);
243 Category offline_pages_category = 258 Category offline_pages_category =
244 FromKnownCategory(KnownCategories::DOWNLOADS); 259 FromKnownCategory(KnownCategories::DOWNLOADS);
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 service()->RestoreDismissedCategories(); 650 service()->RestoreDismissedCategories();
636 651
637 EXPECT_THAT(service()->GetCategories(), ElementsAre(category)); 652 EXPECT_THAT(service()->GetCategories(), ElementsAre(category));
638 EXPECT_THAT(service()->GetCategoryStatus(category), 653 EXPECT_THAT(service()->GetCategoryStatus(category),
639 Eq(CategoryStatus::AVAILABLE)); 654 Eq(CategoryStatus::AVAILABLE));
640 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty()); 655 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty());
641 EXPECT_THAT(providers().count(category), Eq(1ul)); 656 EXPECT_THAT(providers().count(category), Eq(1ul));
642 EXPECT_THAT(dismissed_providers(), IsEmpty()); 657 EXPECT_THAT(dismissed_providers(), IsEmpty());
643 } 658 }
644 659
660 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissedCategories) {
661 // Create and register provider
662 Category category1 = service()->category_factory()->FromIDValue(1);
663 Category category2 = service()->category_factory()->FromIDValue(2);
664
665 // Setup and verify initial state
666 MockProvider* provider = RegisterProvider({category1, category2});
667 provider->FireCategoryStatusChangedWithCurrentStatus(category1);
668 provider->FireCategoryStatusChangedWithCurrentStatus(category2);
669
670 ASSERT_THAT(service()->GetCategoryStatus(category1),
671 Eq(CategoryStatus::AVAILABLE));
672 ASSERT_THAT(service()->GetCategoryStatus(category2),
673 Eq(CategoryStatus::AVAILABLE));
674
675 // Dismiss all the categories. None should be provided now.
676 service()->DismissCategory(category1);
677 service()->DismissCategory(category2);
678
679 ASSERT_THAT(service()->GetCategoryStatus(category1),
680 Eq(CategoryStatus::NOT_PROVIDED));
681 ASSERT_THAT(service()->GetCategoryStatus(category2),
682 Eq(CategoryStatus::NOT_PROVIDED));
683
684 // Receiving a status change notification should not change anything.
685 provider->FireCategoryStatusChanged(category1, CategoryStatus::AVAILABLE);
686
687 EXPECT_THAT(service()->GetCategoryStatus(category1),
688 Eq(CategoryStatus::NOT_PROVIDED));
689 EXPECT_THAT(service()->GetCategoryStatus(category2),
690 Eq(CategoryStatus::NOT_PROVIDED));
691
692 // Receiving a notification without suggestions should not change anything.
693 provider->FireSuggestionsChanged(category1, std::vector<ContentSuggestion>());
694
695 EXPECT_THAT(service()->GetCategoryStatus(category1),
696 Eq(CategoryStatus::NOT_PROVIDED));
697 EXPECT_THAT(service()->GetCategoryStatus(category2),
698 Eq(CategoryStatus::NOT_PROVIDED));
699
700 // Receiving suggestions should make the notified category available.
701 provider->FireSuggestionsChanged(category1,
702 CreateSuggestions(category1, {1, 2}));
703
704 EXPECT_THAT(service()->GetCategoryStatus(category1),
705 Eq(CategoryStatus::AVAILABLE));
706 EXPECT_THAT(service()->GetCategoryStatus(category2),
707 Eq(CategoryStatus::NOT_PROVIDED));
708 }
709
710 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissalsFromPrefs) {
711 // Register a category with one suggestion.
712 Category category = FromKnownCategory(KnownCategories::ARTICLES);
713 MockProvider* provider = RegisterProvider(category);
714 provider->FireCategoryStatusChangedWithCurrentStatus(category);
715
716 // For a regular initialisation, the category is not disabled.
Marc Treib 2016/10/18 07:44:31 s/disabled/dismissed/
dgn 2016/10/18 09:27:18 Done.
717 ASSERT_FALSE(service()->IsCategoryDismissed(category));
718
719 // Dismiss the category.
720 service()->DismissCategory(category);
721 ASSERT_TRUE(service()->IsCategoryDismissed(category));
722
723 // Simulate a Chrome restart. The category should still be dismissed.
724 ResetService();
725 EXPECT_TRUE(service()->IsCategoryDismissed(category));
726
727 // Ensure that the provider registered at initialisation is used after
728 // restoration.
729 provider = RegisterProvider(category);
730 provider->FireCategoryStatusChangedWithCurrentStatus(category);
731 EXPECT_TRUE(service()->IsCategoryDismissed(category));
732
733 service()->RestoreDismissedCategories();
734 EXPECT_FALSE(service()->IsCategoryDismissed(category));
735 EXPECT_THAT(providers().find(category)->second, Eq(provider));
736 }
737
645 } // namespace ntp_snippets 738 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/content_suggestions_service.cc ('k') | components/ntp_snippets/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698