| 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" | |
| 18 #include "components/ntp_snippets/category_info.h" | 17 #include "components/ntp_snippets/category_info.h" |
| 19 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h" | 18 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h" |
| 20 #include "components/ntp_snippets/category_rankers/fake_category_ranker.h" | 19 #include "components/ntp_snippets/category_rankers/fake_category_ranker.h" |
| 21 #include "components/ntp_snippets/category_rankers/mock_category_ranker.h" | 20 #include "components/ntp_snippets/category_rankers/mock_category_ranker.h" |
| 22 #include "components/ntp_snippets/category_status.h" | 21 #include "components/ntp_snippets/category_status.h" |
| 23 #include "components/ntp_snippets/content_suggestion.h" | 22 #include "components/ntp_snippets/content_suggestion.h" |
| 24 #include "components/ntp_snippets/content_suggestions_provider.h" | 23 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 24 #include "components/ntp_snippets/mock_content_suggestions_provider.h" |
| 25 #include "components/ntp_snippets/user_classifier.h" | 25 #include "components/ntp_snippets/user_classifier.h" |
| 26 #include "components/prefs/testing_pref_service.h" | 26 #include "components/prefs/testing_pref_service.h" |
| 27 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "ui/gfx/image/image.h" | 29 #include "ui/gfx/image/image.h" |
| 30 | 30 |
| 31 using testing::_; | 31 using testing::_; |
| 32 using testing::ElementsAre; | 32 using testing::ElementsAre; |
| 33 using testing::Eq; | 33 using testing::Eq; |
| 34 using testing::InvokeWithoutArgs; | 34 using testing::InvokeWithoutArgs; |
| 35 using testing::IsEmpty; | 35 using testing::IsEmpty; |
| 36 using testing::Mock; | 36 using testing::Mock; |
| 37 using testing::Property; | 37 using testing::Property; |
| 38 using testing::Return; | 38 using testing::Return; |
| 39 using testing::StrictMock; | 39 using testing::StrictMock; |
| 40 using testing::UnorderedElementsAre; | 40 using testing::UnorderedElementsAre; |
| 41 | 41 |
| 42 namespace ntp_snippets { | 42 namespace ntp_snippets { |
| 43 | 43 |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 // TODO(treib): This is a weird combination of a mock and a fake. Fix this. | |
| 47 class MockProvider : public ContentSuggestionsProvider { | |
| 48 public: | |
| 49 MockProvider(Observer* observer, | |
| 50 const std::vector<Category>& provided_categories) | |
| 51 : ContentSuggestionsProvider(observer) { | |
| 52 SetProvidedCategories(provided_categories); | |
| 53 } | |
| 54 | |
| 55 void SetProvidedCategories(const std::vector<Category>& provided_categories) { | |
| 56 statuses_.clear(); | |
| 57 provided_categories_ = provided_categories; | |
| 58 for (Category category : provided_categories) { | |
| 59 statuses_[category.id()] = CategoryStatus::AVAILABLE; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 CategoryStatus GetCategoryStatus(Category category) override { | |
| 64 return statuses_[category.id()]; | |
| 65 } | |
| 66 | |
| 67 CategoryInfo GetCategoryInfo(Category category) override { | |
| 68 return CategoryInfo(base::ASCIIToUTF16("Section title"), | |
| 69 ContentSuggestionsCardLayout::FULL_CARD, true, false, | |
| 70 true, false, | |
| 71 base::ASCIIToUTF16("No suggestions message")); | |
| 72 } | |
| 73 | |
| 74 void FireSuggestionsChanged( | |
| 75 Category category, | |
| 76 std::vector<ContentSuggestion> suggestions) { | |
| 77 observer()->OnNewSuggestions(this, category, std::move(suggestions)); | |
| 78 } | |
| 79 | |
| 80 void FireCategoryStatusChanged(Category category, CategoryStatus new_status) { | |
| 81 statuses_[category.id()] = new_status; | |
| 82 observer()->OnCategoryStatusChanged(this, category, new_status); | |
| 83 } | |
| 84 | |
| 85 void FireCategoryStatusChangedWithCurrentStatus(Category category) { | |
| 86 observer()->OnCategoryStatusChanged(this, category, | |
| 87 statuses_[category.id()]); | |
| 88 } | |
| 89 | |
| 90 void FireSuggestionInvalidated(const ContentSuggestion::ID& suggestion_id) { | |
| 91 observer()->OnSuggestionInvalidated(this, suggestion_id); | |
| 92 } | |
| 93 | |
| 94 MOCK_METHOD3(ClearHistory, | |
| 95 void(base::Time begin, | |
| 96 base::Time end, | |
| 97 const base::Callback<bool(const GURL& url)>& filter)); | |
| 98 MOCK_METHOD3(Fetch, | |
| 99 void(const Category&, | |
| 100 const std::set<std::string>&, | |
| 101 const FetchDoneCallback&)); | |
| 102 MOCK_METHOD1(ClearCachedSuggestions, void(Category category)); | |
| 103 MOCK_METHOD2(GetDismissedSuggestionsForDebugging, | |
| 104 void(Category category, | |
| 105 const DismissedSuggestionsCallback& callback)); | |
| 106 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category category)); | |
| 107 MOCK_METHOD1(DismissSuggestion, | |
| 108 void(const ContentSuggestion::ID& suggestion_id)); | |
| 109 MOCK_METHOD2(FetchSuggestionImage, | |
| 110 void(const ContentSuggestion::ID& suggestion_id, | |
| 111 const ImageFetchedCallback& callback)); | |
| 112 | |
| 113 private: | |
| 114 std::vector<Category> provided_categories_; | |
| 115 std::map<int, CategoryStatus> statuses_; | |
| 116 }; | |
| 117 | |
| 118 class MockServiceObserver : public ContentSuggestionsService::Observer { | 46 class MockServiceObserver : public ContentSuggestionsService::Observer { |
| 119 public: | 47 public: |
| 120 MockServiceObserver() = default; | 48 MockServiceObserver() = default; |
| 121 ~MockServiceObserver() override = default; | 49 ~MockServiceObserver() override = default; |
| 122 | 50 |
| 123 MOCK_METHOD1(OnNewSuggestions, void(Category category)); | 51 MOCK_METHOD1(OnNewSuggestions, void(Category category)); |
| 124 MOCK_METHOD2(OnCategoryStatusChanged, | 52 MOCK_METHOD2(OnCategoryStatusChanged, |
| 125 void(Category changed_category, CategoryStatus new_status)); | 53 void(Category changed_category, CategoryStatus new_status)); |
| 126 MOCK_METHOD1(OnSuggestionInvalidated, | 54 MOCK_METHOD1(OnSuggestionInvalidated, |
| 127 void(const ContentSuggestion::ID& suggestion_id)); | 55 void(const ContentSuggestion::ID& suggestion_id)); |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 | 779 |
| 852 Category category = Category::FromKnownCategory(KnownCategories::BOOKMARKS); | 780 Category category = Category::FromKnownCategory(KnownCategories::BOOKMARKS); |
| 853 MockProvider* provider = MakeRegisteredMockProvider(category); | 781 MockProvider* provider = MakeRegisteredMockProvider(category); |
| 854 provider->FireCategoryStatusChangedWithCurrentStatus(category); | 782 provider->FireCategoryStatusChangedWithCurrentStatus(category); |
| 855 | 783 |
| 856 EXPECT_CALL(*raw_mock_ranker, OnCategoryDismissed(category)); | 784 EXPECT_CALL(*raw_mock_ranker, OnCategoryDismissed(category)); |
| 857 service()->DismissCategory(category); | 785 service()->DismissCategory(category); |
| 858 } | 786 } |
| 859 | 787 |
| 860 } // namespace ntp_snippets | 788 } // namespace ntp_snippets |
| OLD | NEW |