| 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/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 Loading... |
| 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 ContentSuggestionsService::RegisterProfilePrefs(pref_service_->registry()); |
| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 const std::vector<int>& numbers) { | 215 const std::vector<int>& numbers) { |
| 213 std::vector<ContentSuggestion> result; | 216 std::vector<ContentSuggestion> result; |
| 214 for (int number : numbers) { | 217 for (int number : numbers) { |
| 215 result.push_back(CreateSuggestion(category, number)); | 218 result.push_back(CreateSuggestion(category, number)); |
| 216 } | 219 } |
| 217 return result; | 220 return result; |
| 218 } | 221 } |
| 219 | 222 |
| 220 private: | 223 private: |
| 221 std::unique_ptr<ContentSuggestionsService> service_; | 224 std::unique_ptr<ContentSuggestionsService> service_; |
| 225 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 222 | 226 |
| 223 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest); | 227 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest); |
| 224 }; | 228 }; |
| 225 | 229 |
| 226 class ContentSuggestionsServiceDisabledTest | 230 class ContentSuggestionsServiceDisabledTest |
| 227 : public ContentSuggestionsServiceTest { | 231 : public ContentSuggestionsServiceTest { |
| 228 public: | 232 public: |
| 229 void SetUp() override { | 233 void SetUp() override { |
| 230 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); | 234 CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED); |
| 231 } | 235 } |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 TEST_F(ContentSuggestionsServiceTest, ShouldForwardClearHistory) { | 598 TEST_F(ContentSuggestionsServiceTest, ShouldForwardClearHistory) { |
| 595 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); | 599 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); |
| 596 MockProvider* provider = RegisterProvider(category); | 600 MockProvider* provider = RegisterProvider(category); |
| 597 base::Time begin = base::Time::FromTimeT(123), | 601 base::Time begin = base::Time::FromTimeT(123), |
| 598 end = base::Time::FromTimeT(456); | 602 end = base::Time::FromTimeT(456); |
| 599 EXPECT_CALL(*provider, ClearHistory(begin, end, _)); | 603 EXPECT_CALL(*provider, ClearHistory(begin, end, _)); |
| 600 base::Callback<bool(const GURL& url)> filter; | 604 base::Callback<bool(const GURL& url)> filter; |
| 601 service()->ClearHistory(begin, end, filter); | 605 service()->ClearHistory(begin, end, filter); |
| 602 } | 606 } |
| 603 | 607 |
| 608 TEST_F(ContentSuggestionsServiceTest, ShouldNotReturnDismissedCategories) { |
| 609 Category category = FromKnownCategory(KnownCategories::ARTICLES); |
| 610 |
| 611 // Create and register providers |
| 612 MockProvider* provider = RegisterProvider(category); |
| 613 provider->FireCategoryStatusChangedWithCurrentStatus(category); |
| 614 ASSERT_THAT(providers().count(category), Eq(1ul)); |
| 615 EXPECT_THAT(providers().at(category), Eq(provider)); |
| 616 ASSERT_THAT(service()->GetCategories(), ElementsAre(category)); |
| 617 |
| 618 // Create and register observer |
| 619 MockServiceObserver observer; |
| 620 service()->AddObserver(&observer); |
| 621 |
| 622 ASSERT_THAT(service()->GetCategoryStatus(category), |
| 623 Eq(CategoryStatus::AVAILABLE)); |
| 624 ASSERT_TRUE(service()->GetCategoryInfo(category)); |
| 625 |
| 626 service()->DismissCategory(category); |
| 627 ASSERT_THAT(service()->GetCategories(), ElementsAre(category)); |
| 628 ASSERT_THAT(service()->GetCategoryStatus(category), |
| 629 Eq(CategoryStatus::NOT_PROVIDED)); |
| 630 ASSERT_FALSE(service()->GetCategoryInfo(category)); |
| 631 ASSERT_TRUE(service()->GetSuggestionsForCategory(category).empty()); |
| 632 |
| 633 // OnCategoryStatusChanged |
| 634 EXPECT_CALL(observer, |
| 635 OnCategoryStatusChanged( |
| 636 category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); |
| 637 provider->FireCategoryStatusChanged( |
| 638 category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
| 639 Mock::VerifyAndClearExpectations(&observer); |
| 640 |
| 641 // OnNewSuggestions (empty) |
| 642 EXPECT_CALL(observer, OnNewSuggestions(category)); // TODO or not? |
| 643 provider->FireSuggestionsChanged(category, std::vector<ContentSuggestion>()); |
| 644 Mock::VerifyAndClearExpectations(&observer); |
| 645 |
| 646 ASSERT_THAT(service()->GetCategoryStatus(category), |
| 647 Eq(CategoryStatus::NOT_PROVIDED)); |
| 648 ASSERT_FALSE(service()->GetCategoryInfo(category)); |
| 649 ASSERT_TRUE(service()->GetSuggestionsForCategory(category).empty()); |
| 650 |
| 651 // OnNewSuggestions (with suggestions) |
| 652 EXPECT_CALL(observer, OnNewSuggestions(category)); |
| 653 provider->FireSuggestionsChanged(category, |
| 654 CreateSuggestions(category, {1, 2})); |
| 655 ExpectThatSuggestionsAre(category, {1, 2}); |
| 656 Mock::VerifyAndClearExpectations(&observer); |
| 657 |
| 658 ASSERT_THAT(service()->GetCategoryStatus(category), |
| 659 Eq(CategoryStatus::AVAILABLE)); |
| 660 ASSERT_TRUE(service()->GetCategoryInfo(category)); |
| 661 ASSERT_FALSE(service()->GetSuggestionsForCategory(category).empty()); |
| 662 |
| 663 EXPECT_CALL(observer, |
| 664 OnCategoryStatusChanged( |
| 665 category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED)); |
| 666 provider->FireCategoryStatusChanged( |
| 667 category, CategoryStatus::CATEGORY_EXPLICITLY_DISABLED); |
| 668 Mock::VerifyAndClearExpectations(&observer); |
| 669 } |
| 670 |
| 604 } // namespace ntp_snippets | 671 } // namespace ntp_snippets |
| OLD | NEW |