| 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 <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/ntp_snippets/user_classifier.h" |
| 23 #include "components/prefs/testing_pref_service.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 25 | 27 |
| 26 using testing::ElementsAre; | 28 using testing::ElementsAre; |
| 27 using testing::Eq; | 29 using testing::Eq; |
| 28 using testing::InvokeWithoutArgs; | 30 using testing::InvokeWithoutArgs; |
| 29 using testing::IsEmpty; | 31 using testing::IsEmpty; |
| 30 using testing::Mock; | 32 using testing::Mock; |
| 31 using testing::Property; | 33 using testing::Property; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); | 116 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); |
| 115 | 117 |
| 116 private: | 118 private: |
| 117 DISALLOW_COPY_AND_ASSIGN(MockServiceObserver); | 119 DISALLOW_COPY_AND_ASSIGN(MockServiceObserver); |
| 118 }; | 120 }; |
| 119 | 121 |
| 120 } // namespace | 122 } // namespace |
| 121 | 123 |
| 122 class ContentSuggestionsServiceTest : public testing::Test { | 124 class ContentSuggestionsServiceTest : public testing::Test { |
| 123 public: | 125 public: |
| 124 ContentSuggestionsServiceTest() {} | 126 ContentSuggestionsServiceTest() |
| 127 : pref_service_(new TestingPrefServiceSimple()) {} |
| 125 | 128 |
| 126 void SetUp() override { | 129 void SetUp() override { |
| 130 RegisterPrefs(); |
| 127 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); | 131 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); |
| 128 } | 132 } |
| 129 | 133 |
| 130 void TearDown() override { | 134 void TearDown() override { |
| 131 service_->Shutdown(); | 135 service_->Shutdown(); |
| 132 service_.reset(); | 136 service_.reset(); |
| 133 } | 137 } |
| 134 | 138 |
| 135 // Verifies that exactly the suggestions with the given |numbers| are | 139 // Verifies that exactly the suggestions with the given |numbers| are |
| 136 // returned by the service for the given |category|. | 140 // returned by the service for the given |category|. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 std::unique_ptr<MockProvider> provider = base::MakeUnique<MockProvider>( | 192 std::unique_ptr<MockProvider> provider = base::MakeUnique<MockProvider>( |
| 189 service(), category_factory(), provided_categories); | 193 service(), category_factory(), provided_categories); |
| 190 MockProvider* result = provider.get(); | 194 MockProvider* result = provider.get(); |
| 191 service()->RegisterProvider(std::move(provider)); | 195 service()->RegisterProvider(std::move(provider)); |
| 192 return result; | 196 return result; |
| 193 } | 197 } |
| 194 | 198 |
| 195 MOCK_METHOD1(OnImageFetched, void(const gfx::Image&)); | 199 MOCK_METHOD1(OnImageFetched, void(const gfx::Image&)); |
| 196 | 200 |
| 197 protected: | 201 protected: |
| 202 void RegisterPrefs() { |
| 203 ContentSuggestionsService::RegisterProfilePrefs(pref_service_->registry()); |
| 204 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); |
| 205 } |
| 206 |
| 198 void CreateContentSuggestionsService( | 207 void CreateContentSuggestionsService( |
| 199 ContentSuggestionsService::State enabled) { | 208 ContentSuggestionsService::State enabled) { |
| 200 ASSERT_FALSE(service_); | 209 ASSERT_FALSE(service_); |
| 201 service_.reset(new ContentSuggestionsService(enabled, | 210 service_.reset(new ContentSuggestionsService( |
| 202 nullptr /* history_service */, | 211 enabled, /*history_service=*/nullptr, pref_service_.get())); |
| 203 nullptr /* pref_service */)); | 212 } |
| 213 |
| 214 void ResetService() { |
| 215 service_->Shutdown(); |
| 216 service_.reset(); |
| 217 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); |
| 204 } | 218 } |
| 205 | 219 |
| 206 ContentSuggestionsService* service() { return service_.get(); } | 220 ContentSuggestionsService* service() { return service_.get(); } |
| 207 | 221 |
| 208 // Returns a suggestion instance for testing. | 222 // Returns a suggestion instance for testing. |
| 209 ContentSuggestion CreateSuggestion(Category category, int number) { | 223 ContentSuggestion CreateSuggestion(Category category, int number) { |
| 210 return ContentSuggestion( | 224 return ContentSuggestion( |
| 211 category, base::IntToString(number), | 225 category, base::IntToString(number), |
| 212 GURL("http://testsuggestion/" + base::IntToString(number))); | 226 GURL("http://testsuggestion/" + base::IntToString(number))); |
| 213 } | 227 } |
| 214 | 228 |
| 215 std::vector<ContentSuggestion> CreateSuggestions( | 229 std::vector<ContentSuggestion> CreateSuggestions( |
| 216 Category category, | 230 Category category, |
| 217 const std::vector<int>& numbers) { | 231 const std::vector<int>& numbers) { |
| 218 std::vector<ContentSuggestion> result; | 232 std::vector<ContentSuggestion> result; |
| 219 for (int number : numbers) { | 233 for (int number : numbers) { |
| 220 result.push_back(CreateSuggestion(category, number)); | 234 result.push_back(CreateSuggestion(category, number)); |
| 221 } | 235 } |
| 222 return result; | 236 return result; |
| 223 } | 237 } |
| 224 | 238 |
| 225 private: | 239 private: |
| 226 std::unique_ptr<ContentSuggestionsService> service_; | 240 std::unique_ptr<ContentSuggestionsService> service_; |
| 241 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 227 | 242 |
| 228 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest); | 243 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest); |
| 229 }; | 244 }; |
| 230 | 245 |
| 231 class ContentSuggestionsServiceDisabledTest | 246 class ContentSuggestionsServiceDisabledTest |
| 232 : public ContentSuggestionsServiceTest { | 247 : public ContentSuggestionsServiceTest { |
| 233 public: | 248 public: |
| 234 void SetUp() override { | 249 void SetUp() override { |
| 250 RegisterPrefs(); |
| 235 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); | 251 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); |
| 236 } | 252 } |
| 237 }; | 253 }; |
| 238 | 254 |
| 239 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) { | 255 TEST_F(ContentSuggestionsServiceTest, ShouldRegisterProviders) { |
| 240 EXPECT_THAT(service()->state(), | 256 EXPECT_THAT(service()->state(), |
| 241 Eq(ContentSuggestionsService::State::ENABLED)); | 257 Eq(ContentSuggestionsService::State::ENABLED)); |
| 242 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); | 258 Category articles_category = FromKnownCategory(KnownCategories::ARTICLES); |
| 243 Category offline_pages_category = | 259 Category offline_pages_category = |
| 244 FromKnownCategory(KnownCategories::DOWNLOADS); | 260 FromKnownCategory(KnownCategories::DOWNLOADS); |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 service()->RestoreDismissedCategories(); | 651 service()->RestoreDismissedCategories(); |
| 636 | 652 |
| 637 EXPECT_THAT(service()->GetCategories(), ElementsAre(category)); | 653 EXPECT_THAT(service()->GetCategories(), ElementsAre(category)); |
| 638 EXPECT_THAT(service()->GetCategoryStatus(category), | 654 EXPECT_THAT(service()->GetCategoryStatus(category), |
| 639 Eq(CategoryStatus::AVAILABLE)); | 655 Eq(CategoryStatus::AVAILABLE)); |
| 640 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty()); | 656 EXPECT_THAT(service()->GetSuggestionsForCategory(category), IsEmpty()); |
| 641 EXPECT_THAT(providers().count(category), Eq(1ul)); | 657 EXPECT_THAT(providers().count(category), Eq(1ul)); |
| 642 EXPECT_THAT(dismissed_providers(), IsEmpty()); | 658 EXPECT_THAT(dismissed_providers(), IsEmpty()); |
| 643 } | 659 } |
| 644 | 660 |
| 661 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissedCategories) { |
| 662 // Create and register provider. |
| 663 Category category1 = service()->category_factory()->FromIDValue(1); |
| 664 Category category2 = service()->category_factory()->FromIDValue(2); |
| 665 |
| 666 // Setup and verify initial state. |
| 667 MockProvider* provider = RegisterProvider({category1, category2}); |
| 668 provider->FireCategoryStatusChangedWithCurrentStatus(category1); |
| 669 provider->FireCategoryStatusChangedWithCurrentStatus(category2); |
| 670 |
| 671 ASSERT_THAT(service()->GetCategoryStatus(category1), |
| 672 Eq(CategoryStatus::AVAILABLE)); |
| 673 ASSERT_THAT(service()->GetCategoryStatus(category2), |
| 674 Eq(CategoryStatus::AVAILABLE)); |
| 675 |
| 676 // Dismiss all the categories. None should be provided now. |
| 677 service()->DismissCategory(category1); |
| 678 service()->DismissCategory(category2); |
| 679 |
| 680 ASSERT_THAT(service()->GetCategoryStatus(category1), |
| 681 Eq(CategoryStatus::NOT_PROVIDED)); |
| 682 ASSERT_THAT(service()->GetCategoryStatus(category2), |
| 683 Eq(CategoryStatus::NOT_PROVIDED)); |
| 684 |
| 685 // Receiving a status change notification should not change anything. |
| 686 provider->FireCategoryStatusChanged(category1, CategoryStatus::AVAILABLE); |
| 687 |
| 688 EXPECT_THAT(service()->GetCategoryStatus(category1), |
| 689 Eq(CategoryStatus::NOT_PROVIDED)); |
| 690 EXPECT_THAT(service()->GetCategoryStatus(category2), |
| 691 Eq(CategoryStatus::NOT_PROVIDED)); |
| 692 |
| 693 // Receiving a notification without suggestions should not change anything. |
| 694 provider->FireSuggestionsChanged(category1, std::vector<ContentSuggestion>()); |
| 695 |
| 696 EXPECT_THAT(service()->GetCategoryStatus(category1), |
| 697 Eq(CategoryStatus::NOT_PROVIDED)); |
| 698 EXPECT_THAT(service()->GetCategoryStatus(category2), |
| 699 Eq(CategoryStatus::NOT_PROVIDED)); |
| 700 |
| 701 // Receiving suggestions should make the notified category available. |
| 702 provider->FireSuggestionsChanged(category1, |
| 703 CreateSuggestions(category1, {1, 2})); |
| 704 |
| 705 EXPECT_THAT(service()->GetCategoryStatus(category1), |
| 706 Eq(CategoryStatus::AVAILABLE)); |
| 707 EXPECT_THAT(service()->GetCategoryStatus(category2), |
| 708 Eq(CategoryStatus::NOT_PROVIDED)); |
| 709 } |
| 710 |
| 711 TEST_F(ContentSuggestionsServiceTest, ShouldRestoreDismissalsFromPrefs) { |
| 712 // Register a category with one suggestion. |
| 713 Category category = FromKnownCategory(KnownCategories::ARTICLES); |
| 714 MockProvider* provider = RegisterProvider(category); |
| 715 provider->FireCategoryStatusChangedWithCurrentStatus(category); |
| 716 |
| 717 // For a regular initialisation, the category is not dismissed. |
| 718 ASSERT_FALSE(service()->IsCategoryDismissed(category)); |
| 719 |
| 720 // Dismiss the category. |
| 721 service()->DismissCategory(category); |
| 722 ASSERT_TRUE(service()->IsCategoryDismissed(category)); |
| 723 |
| 724 // Simulate a Chrome restart. The category should still be dismissed. |
| 725 ResetService(); |
| 726 EXPECT_TRUE(service()->IsCategoryDismissed(category)); |
| 727 |
| 728 // Ensure that the provider registered at initialisation is used after |
| 729 // restoration. |
| 730 provider = RegisterProvider(category); |
| 731 provider->FireCategoryStatusChangedWithCurrentStatus(category); |
| 732 EXPECT_TRUE(service()->IsCategoryDismissed(category)); |
| 733 |
| 734 service()->RestoreDismissedCategories(); |
| 735 EXPECT_FALSE(service()->IsCategoryDismissed(category)); |
| 736 EXPECT_THAT(providers().find(category)->second, Eq(provider)); |
| 737 } |
| 738 |
| 645 } // namespace ntp_snippets | 739 } // namespace ntp_snippets |
| OLD | NEW |