| 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/offline_pages/recent_tab_suggestions_provider.
h" | 5 #include "components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.
h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/guid.h" | |
| 13 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 16 #include "components/ntp_snippets/category.h" | 14 #include "components/ntp_snippets/category.h" |
| 17 #include "components/ntp_snippets/category_factory.h" | 15 #include "components/ntp_snippets/category_factory.h" |
| 18 #include "components/ntp_snippets/content_suggestions_provider.h" | 16 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 19 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" | 17 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
| 18 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" |
| 20 #include "components/offline_pages/client_namespace_constants.h" | 19 #include "components/offline_pages/client_namespace_constants.h" |
| 21 #include "components/offline_pages/offline_page_item.h" | 20 #include "components/offline_pages/offline_page_item.h" |
| 22 #include "components/offline_pages/stub_offline_page_model.h" | 21 #include "components/offline_pages/stub_offline_page_model.h" |
| 23 #include "components/prefs/testing_pref_service.h" | 22 #include "components/prefs/testing_pref_service.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 25 |
| 26 using ntp_snippets::test::CaptureDismissedSuggestions; |
| 27 using ntp_snippets::test::FakeOfflinePageModel; |
| 27 using offline_pages::ClientId; | 28 using offline_pages::ClientId; |
| 28 using offline_pages::MultipleOfflinePageItemCallback; | 29 using offline_pages::MultipleOfflinePageItemCallback; |
| 29 using offline_pages::OfflinePageItem; | 30 using offline_pages::OfflinePageItem; |
| 30 using offline_pages::StubOfflinePageModel; | 31 using offline_pages::StubOfflinePageModel; |
| 31 using testing::_; | 32 using testing::_; |
| 32 using testing::IsEmpty; | 33 using testing::IsEmpty; |
| 33 using testing::Mock; | 34 using testing::Mock; |
| 34 using testing::Property; | 35 using testing::Property; |
| 35 using testing::SizeIs; | 36 using testing::SizeIs; |
| 36 | 37 |
| 37 namespace ntp_snippets { | 38 namespace ntp_snippets { |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 OfflinePageItem CreateDummyRecentTab(int id) { | 42 OfflinePageItem CreateDummyRecentTab(int id) { |
| 42 std::string strid = base::IntToString(id); | 43 return test::CreateDummyOfflinePageItem(id, offline_pages::kLastNNamespace); |
| 43 return OfflinePageItem( | |
| 44 GURL("http://dummy.com/" + strid), id, | |
| 45 ClientId(offline_pages::kLastNNamespace, base::GenerateGUID()), | |
| 46 base::FilePath::FromUTF8Unsafe("some/folder/test" + strid + ".mhtml"), 0, | |
| 47 base::Time::Now()); | |
| 48 } | 44 } |
| 49 | 45 |
| 50 std::vector<OfflinePageItem> CreateDummyRecentTabs( | 46 std::vector<OfflinePageItem> CreateDummyRecentTabs( |
| 51 const std::vector<int>& ids) { | 47 const std::vector<int>& ids) { |
| 52 std::vector<OfflinePageItem> result; | 48 std::vector<OfflinePageItem> result; |
| 53 for (int id : ids) { | 49 for (int id : ids) { |
| 54 result.push_back(CreateDummyRecentTab(id)); | 50 result.push_back(CreateDummyRecentTab(id)); |
| 55 } | 51 } |
| 56 return result; | 52 return result; |
| 57 } | 53 } |
| 58 | 54 |
| 59 OfflinePageItem CreateDummyRecentTab(int id, base::Time time) { | 55 OfflinePageItem CreateDummyRecentTab(int id, base::Time time) { |
| 60 OfflinePageItem item = CreateDummyRecentTab(id); | 56 OfflinePageItem item = CreateDummyRecentTab(id); |
| 61 item.last_access_time = time; | 57 item.last_access_time = time; |
| 62 return item; | 58 return item; |
| 63 } | 59 } |
| 64 | 60 |
| 65 void CaptureDismissedSuggestions( | |
| 66 std::vector<ContentSuggestion>* captured_suggestions, | |
| 67 std::vector<ContentSuggestion> dismissed_suggestions) { | |
| 68 std::move(dismissed_suggestions.begin(), dismissed_suggestions.end(), | |
| 69 std::back_inserter(*captured_suggestions)); | |
| 70 } | |
| 71 | |
| 72 } // namespace | 61 } // namespace |
| 73 | 62 |
| 74 // This model is needed only when a provider is expected to call |GetAllPages|. | |
| 75 // In other cases, keeping this model empty ensures that provider listens to | |
| 76 // proxy notifications without calling |GetAllPages|. | |
| 77 class FakeOfflinePageModel : public StubOfflinePageModel { | |
| 78 public: | |
| 79 FakeOfflinePageModel() {} | |
| 80 | |
| 81 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { | |
| 82 callback.Run(items_); | |
| 83 } | |
| 84 | |
| 85 const std::vector<OfflinePageItem>& items() { return items_; } | |
| 86 std::vector<OfflinePageItem>* mutable_items() { return &items_; } | |
| 87 | |
| 88 private: | |
| 89 std::vector<OfflinePageItem> items_; | |
| 90 }; | |
| 91 | |
| 92 class RecentTabSuggestionsProviderTest : public testing::Test { | 63 class RecentTabSuggestionsProviderTest : public testing::Test { |
| 93 public: | 64 public: |
| 94 RecentTabSuggestionsProviderTest() | 65 RecentTabSuggestionsProviderTest() |
| 95 : pref_service_(new TestingPrefServiceSimple()) { | 66 : pref_service_(new TestingPrefServiceSimple()) { |
| 96 RecentTabSuggestionsProvider::RegisterProfilePrefs( | 67 RecentTabSuggestionsProvider::RegisterProfilePrefs( |
| 97 pref_service()->registry()); | 68 pref_service()->registry()); |
| 98 | 69 |
| 99 scoped_refptr<OfflinePageProxy> proxy(new OfflinePageProxy(&model_)); | 70 scoped_refptr<OfflinePageProxy> proxy(new OfflinePageProxy(&model_)); |
| 100 provider_.reset(new RecentTabSuggestionsProvider( | 71 provider_.reset(new RecentTabSuggestionsProvider( |
| 101 &observer_, &category_factory_, proxy, pref_service())); | 72 &observer_, &category_factory_, proxy, pref_service())); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 EXPECT_THAT(ReadDismissedIDsFromPrefs(), SizeIs(2)); | 234 EXPECT_THAT(ReadDismissedIDsFromPrefs(), SizeIs(2)); |
| 264 | 235 |
| 265 FireOfflinePageModelChanged(CreateDummyRecentTabs({2})); | 236 FireOfflinePageModelChanged(CreateDummyRecentTabs({2})); |
| 266 EXPECT_THAT(ReadDismissedIDsFromPrefs(), SizeIs(1)); | 237 EXPECT_THAT(ReadDismissedIDsFromPrefs(), SizeIs(1)); |
| 267 | 238 |
| 268 FireOfflinePageModelChanged(std::vector<OfflinePageItem>()); | 239 FireOfflinePageModelChanged(std::vector<OfflinePageItem>()); |
| 269 EXPECT_THAT(ReadDismissedIDsFromPrefs(), IsEmpty()); | 240 EXPECT_THAT(ReadDismissedIDsFromPrefs(), IsEmpty()); |
| 270 } | 241 } |
| 271 | 242 |
| 272 } // namespace ntp_snippets | 243 } // namespace ntp_snippets |
| OLD | NEW |