| 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 "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
| 25 | 25 |
| 26 using testing::ByRef; | |
| 27 using testing::Const; | |
| 28 using testing::ElementsAre; | 26 using testing::ElementsAre; |
| 29 using testing::Eq; | 27 using testing::Eq; |
| 30 using testing::InvokeWithoutArgs; | 28 using testing::InvokeWithoutArgs; |
| 31 using testing::IsEmpty; | 29 using testing::IsEmpty; |
| 32 using testing::IsNull; | |
| 33 using testing::Mock; | 30 using testing::Mock; |
| 34 using testing::NotNull; | |
| 35 using testing::Property; | 31 using testing::Property; |
| 36 using testing::_; | 32 using testing::_; |
| 37 | 33 |
| 38 namespace ntp_snippets { | 34 namespace ntp_snippets { |
| 39 | 35 |
| 40 namespace { | 36 namespace { |
| 41 | 37 |
| 42 class MockProvider : public ContentSuggestionsProvider { | 38 class MockProvider : public ContentSuggestionsProvider { |
| 43 public: | 39 public: |
| 44 MockProvider(Observer* observer, | 40 MockProvider(Observer* observer, |
| 45 CategoryFactory* category_factory, | 41 CategoryFactory* category_factory, |
| 46 const std::vector<Category>& provided_categories) | 42 const std::vector<Category>& provided_categories) |
| 47 : ContentSuggestionsProvider(observer, category_factory) { | 43 : ContentSuggestionsProvider(observer, category_factory) { |
| 48 SetProvidedCategories(provided_categories); | 44 SetProvidedCategories(provided_categories); |
| 49 } | 45 } |
| 50 | 46 |
| 51 void SetProvidedCategories(const std::vector<Category>& provided_categories) { | 47 void SetProvidedCategories(const std::vector<Category>& provided_categories) { |
| 52 statuses_.clear(); | 48 statuses_.clear(); |
| 53 provided_categories_ = provided_categories; | 49 provided_categories_ = provided_categories; |
| 54 for (Category category : provided_categories) { | 50 for (Category category : provided_categories) { |
| 55 statuses_[category.id()] = CategoryStatus::AVAILABLE; | 51 statuses_[category.id()] = CategoryStatus::AVAILABLE; |
| 56 } | 52 } |
| 57 } | 53 } |
| 58 | 54 |
| 59 CategoryStatus GetCategoryStatus(Category category) { | 55 CategoryStatus GetCategoryStatus(Category category) override { |
| 60 return statuses_[category.id()]; | 56 return statuses_[category.id()]; |
| 61 } | 57 } |
| 62 | 58 |
| 63 CategoryInfo GetCategoryInfo(Category category) { | 59 CategoryInfo GetCategoryInfo(Category category) override { |
| 64 return CategoryInfo(base::ASCIIToUTF16("Section title"), | 60 return CategoryInfo(base::ASCIIToUTF16("Section title"), |
| 65 ContentSuggestionsCardLayout::FULL_CARD, true, true); | 61 ContentSuggestionsCardLayout::FULL_CARD, true, true); |
| 66 } | 62 } |
| 67 | 63 |
| 68 void FireSuggestionsChanged( | 64 void FireSuggestionsChanged( |
| 69 Category category, | 65 Category category, |
| 70 std::vector<ContentSuggestion> suggestions) { | 66 std::vector<ContentSuggestion> suggestions) { |
| 71 observer()->OnNewSuggestions(this, category, std::move(suggestions)); | 67 observer()->OnNewSuggestions(this, category, std::move(suggestions)); |
| 72 } | 68 } |
| 73 | 69 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 100 void(const std::string& suggestion_id, | 96 void(const std::string& suggestion_id, |
| 101 const ImageFetchedCallback& callback)); | 97 const ImageFetchedCallback& callback)); |
| 102 | 98 |
| 103 private: | 99 private: |
| 104 std::vector<Category> provided_categories_; | 100 std::vector<Category> provided_categories_; |
| 105 std::map<int, CategoryStatus> statuses_; | 101 std::map<int, CategoryStatus> statuses_; |
| 106 }; | 102 }; |
| 107 | 103 |
| 108 class MockServiceObserver : public ContentSuggestionsService::Observer { | 104 class MockServiceObserver : public ContentSuggestionsService::Observer { |
| 109 public: | 105 public: |
| 106 MockServiceObserver() = default; |
| 107 ~MockServiceObserver() override = default; |
| 108 |
| 110 MOCK_METHOD1(OnNewSuggestions, void(Category category)); | 109 MOCK_METHOD1(OnNewSuggestions, void(Category category)); |
| 111 MOCK_METHOD2(OnCategoryStatusChanged, | 110 MOCK_METHOD2(OnCategoryStatusChanged, |
| 112 void(Category changed_category, CategoryStatus new_status)); | 111 void(Category changed_category, CategoryStatus new_status)); |
| 113 MOCK_METHOD2(OnSuggestionInvalidated, | 112 MOCK_METHOD2(OnSuggestionInvalidated, |
| 114 void(Category category, const std::string& suggestion_id)); | 113 void(Category category, const std::string& suggestion_id)); |
| 115 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); | 114 MOCK_METHOD0(ContentSuggestionsServiceShutdown, void()); |
| 116 ~MockServiceObserver() override {} | 115 |
| 116 private: |
| 117 DISALLOW_COPY_AND_ASSIGN(MockServiceObserver); |
| 117 }; | 118 }; |
| 118 | 119 |
| 119 } // namespace | 120 } // namespace |
| 120 | 121 |
| 121 class ContentSuggestionsServiceTest : public testing::Test { | 122 class ContentSuggestionsServiceTest : public testing::Test { |
| 122 public: | 123 public: |
| 123 ContentSuggestionsServiceTest() {} | 124 ContentSuggestionsServiceTest() {} |
| 124 | 125 |
| 125 void SetUp() override { | 126 void SetUp() override { |
| 126 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); | 127 CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED); |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); | 598 Category category = FromKnownCategory(KnownCategories::DOWNLOADS); |
| 598 MockProvider* provider = RegisterProvider(category); | 599 MockProvider* provider = RegisterProvider(category); |
| 599 base::Time begin = base::Time::FromTimeT(123), | 600 base::Time begin = base::Time::FromTimeT(123), |
| 600 end = base::Time::FromTimeT(456); | 601 end = base::Time::FromTimeT(456); |
| 601 EXPECT_CALL(*provider, ClearHistory(begin, end, _)); | 602 EXPECT_CALL(*provider, ClearHistory(begin, end, _)); |
| 602 base::Callback<bool(const GURL& url)> filter; | 603 base::Callback<bool(const GURL& url)> filter; |
| 603 service()->ClearHistory(begin, end, filter); | 604 service()->ClearHistory(begin, end, filter); |
| 604 } | 605 } |
| 605 | 606 |
| 606 } // namespace ntp_snippets | 607 } // namespace ntp_snippets |
| OLD | NEW |