| 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 <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "components/ntp_snippets/category_info.h" | 17 #include "components/ntp_snippets/category_info.h" |
| 18 #include "components/ntp_snippets/category_status.h" | 18 #include "components/ntp_snippets/category_status.h" |
| 19 #include "components/ntp_snippets/content_suggestion.h" | 19 #include "components/ntp_snippets/content_suggestion.h" |
| 20 #include "components/ntp_snippets/content_suggestions_provider.h" | 20 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 24 | 24 |
| 25 using testing::ByRef; | |
| 26 using testing::Const; | |
| 27 using testing::ElementsAre; | 25 using testing::ElementsAre; |
| 28 using testing::Eq; | 26 using testing::Eq; |
| 29 using testing::InvokeWithoutArgs; | 27 using testing::InvokeWithoutArgs; |
| 30 using testing::IsEmpty; | 28 using testing::IsEmpty; |
| 31 using testing::IsNull; | |
| 32 using testing::Mock; | 29 using testing::Mock; |
| 33 using testing::NotNull; | |
| 34 using testing::Property; | 30 using testing::Property; |
| 35 using testing::_; | 31 using testing::_; |
| 36 | 32 |
| 37 namespace ntp_snippets { | 33 namespace ntp_snippets { |
| 38 | 34 |
| 39 namespace { | 35 namespace { |
| 40 | 36 |
| 41 // Returns a suggestion instance for testing. | 37 // Returns a suggestion instance for testing. |
| 42 ContentSuggestion CreateSuggestion(int number) { | 38 ContentSuggestion CreateSuggestion(int number) { |
| 43 return ContentSuggestion( | 39 return ContentSuggestion( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 64 } | 60 } |
| 65 | 61 |
| 66 void SetProvidedCategories(const std::vector<Category>& provided_categories) { | 62 void SetProvidedCategories(const std::vector<Category>& provided_categories) { |
| 67 statuses_.clear(); | 63 statuses_.clear(); |
| 68 provided_categories_ = provided_categories; | 64 provided_categories_ = provided_categories; |
| 69 for (Category category : provided_categories) { | 65 for (Category category : provided_categories) { |
| 70 statuses_[category.id()] = CategoryStatus::AVAILABLE; | 66 statuses_[category.id()] = CategoryStatus::AVAILABLE; |
| 71 } | 67 } |
| 72 } | 68 } |
| 73 | 69 |
| 74 CategoryStatus GetCategoryStatus(Category category) { | 70 CategoryStatus GetCategoryStatus(Category category) override { |
| 75 return statuses_[category.id()]; | 71 return statuses_[category.id()]; |
| 76 } | 72 } |
| 77 | 73 |
| 78 CategoryInfo GetCategoryInfo(Category category) { | 74 CategoryInfo GetCategoryInfo(Category category) override { |
| 79 return CategoryInfo(base::ASCIIToUTF16("Section title"), | 75 return CategoryInfo(base::ASCIIToUTF16("Section title"), |
| 80 ContentSuggestionsCardLayout::FULL_CARD, true, true); | 76 ContentSuggestionsCardLayout::FULL_CARD, true, true); |
| 81 } | 77 } |
| 82 | 78 |
| 83 void FireSuggestionsChanged(Category category, | 79 void FireSuggestionsChanged(Category category, |
| 84 const std::vector<int>& numbers) { | 80 const std::vector<int>& numbers) { |
| 85 observer()->OnNewSuggestions(this, category, CreateSuggestions(numbers)); | 81 observer()->OnNewSuggestions(this, category, CreateSuggestions(numbers)); |
| 86 } | 82 } |
| 87 | 83 |
| 88 void FireCategoryStatusChanged(Category category, CategoryStatus new_status) { | 84 void FireCategoryStatusChanged(Category category, CategoryStatus new_status) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 114 void(const std::string& suggestion_id, | 110 void(const std::string& suggestion_id, |
| 115 const ImageFetchedCallback& callback)); | 111 const ImageFetchedCallback& callback)); |
| 116 | 112 |
| 117 private: | 113 private: |
| 118 std::vector<Category> provided_categories_; | 114 std::vector<Category> provided_categories_; |
| 119 std::map<int, CategoryStatus> statuses_; | 115 std::map<int, CategoryStatus> statuses_; |
| 120 }; | 116 }; |
| 121 | 117 |
| 122 class MockServiceObserver : public ContentSuggestionsService::Observer { | 118 class MockServiceObserver : public ContentSuggestionsService::Observer { |
| 123 public: | 119 public: |
| 120 MockServiceObserver() = default; |
| 121 ~MockServiceObserver() override = default; |
| 122 |
| 124 MOCK_METHOD1(OnNewSuggestions, void(Category category)); | 123 MOCK_METHOD1(OnNewSuggestions, void(Category category)); |
| 125 MOCK_METHOD2(OnCategoryStatusChanged, | 124 MOCK_METHOD2(OnCategoryStatusChanged, |
| 126 void(Category changed_category, CategoryStatus new_status)); | 125 void(Category changed_category, CategoryStatus new_status)); |
| 127 MOCK_METHOD2(OnSuggestionInvalidated, | 126 MOCK_METHOD2(OnSuggestionInvalidated, |
| 128 void(Category category, const std::string& suggestion_id)); | 127 void(Category category, const std::string& suggestion_id)); |
| 129 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); | 128 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); |
| 130 ~MockServiceObserver() override {} | 129 |
| 130 private: |
| 131 DISALLOW_COPY_AND_ASSIGN(MockServiceObserver); |
| 131 }; | 132 }; |
| 132 | 133 |
| 133 } // namespace | 134 } // namespace |
| 134 | 135 |
| 135 class ContentSuggestionsServiceTest : public testing::Test { | 136 class ContentSuggestionsServiceTest : public testing::Test { |
| 136 public: | 137 public: |
| 137 ContentSuggestionsServiceTest() {} | 138 ContentSuggestionsServiceTest() {} |
| 138 | 139 |
| 139 void SetUp() override { | 140 void SetUp() override { |
| 140 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); | 141 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); | 576 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); |
| 576 MockProvider* provider = RegisterProvider(category); | 577 MockProvider* provider = RegisterProvider(category); |
| 577 base::Time begin = base::Time::FromTimeT(123), | 578 base::Time begin = base::Time::FromTimeT(123), |
| 578 end = base::Time::FromTimeT(456); | 579 end = base::Time::FromTimeT(456); |
| 579 EXPECT_CALL(*provider, ClearHistory(begin, end, _)); | 580 EXPECT_CALL(*provider, ClearHistory(begin, end, _)); |
| 580 base::Callback<bool(const GURL& url)> filter; | 581 base::Callback<bool(const GURL& url)> filter; |
| 581 service()->ClearHistory(begin, end, filter); | 582 service()->ClearHistory(begin, end, filter); |
| 582 } | 583 } |
| 583 | 584 |
| 584 } // namespace ntp_snippets | 585 } // namespace ntp_snippets |
| OLD | NEW |