| 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/sessions/foreign_sessions_suggestions_provider
.h" | 5 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider
.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "components/ntp_snippets/category.h" | 13 #include "components/ntp_snippets/category.h" |
| 14 #include "components/ntp_snippets/category_factory.h" | |
| 15 #include "components/ntp_snippets/content_suggestions_provider.h" | 14 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 16 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" | 15 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
| 17 #include "components/prefs/testing_pref_service.h" | 16 #include "components/prefs/testing_pref_service.h" |
| 18 #include "components/sessions/core/serialized_navigation_entry.h" | 17 #include "components/sessions/core/serialized_navigation_entry.h" |
| 19 #include "components/sessions/core/serialized_navigation_entry_test_helper.h" | 18 #include "components/sessions/core/serialized_navigation_entry_test_helper.h" |
| 20 #include "components/sessions/core/session_types.h" | 19 #include "components/sessions/core/session_types.h" |
| 21 #include "components/sync_sessions/synced_session.h" | 20 #include "components/sync_sessions/synced_session.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 23 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 fake_foreign_sessions_provider = | 109 fake_foreign_sessions_provider = |
| 111 base::MakeUnique<FakeForeignSessionsProvider>(); | 110 base::MakeUnique<FakeForeignSessionsProvider>(); |
| 112 fake_foreign_sessions_provider_ = fake_foreign_sessions_provider.get(); | 111 fake_foreign_sessions_provider_ = fake_foreign_sessions_provider.get(); |
| 113 | 112 |
| 114 // During the provider's construction the following mock calls occur. | 113 // During the provider's construction the following mock calls occur. |
| 115 EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty())); | 114 EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty())); |
| 116 EXPECT_CALL(*observer(), OnCategoryStatusChanged( | 115 EXPECT_CALL(*observer(), OnCategoryStatusChanged( |
| 117 _, category(), CategoryStatus::AVAILABLE)); | 116 _, category(), CategoryStatus::AVAILABLE)); |
| 118 | 117 |
| 119 provider_ = base::MakeUnique<ForeignSessionsSuggestionsProvider>( | 118 provider_ = base::MakeUnique<ForeignSessionsSuggestionsProvider>( |
| 120 &observer_, &category_factory_, | 119 &observer_, std::move(fake_foreign_sessions_provider), &pref_service_); |
| 121 std::move(fake_foreign_sessions_provider), &pref_service_); | |
| 122 } | 120 } |
| 123 | 121 |
| 124 protected: | 122 protected: |
| 125 SyncedSession* GetOrCreateSession(int session_id) { | 123 SyncedSession* GetOrCreateSession(int session_id) { |
| 126 if (sessions_map_.find(session_id) == sessions_map_.end()) { | 124 if (sessions_map_.find(session_id) == sessions_map_.end()) { |
| 127 std::string id_as_string = base::IntToString(session_id); | 125 std::string id_as_string = base::IntToString(session_id); |
| 128 std::unique_ptr<SyncedSession> owned_session = | 126 std::unique_ptr<SyncedSession> owned_session = |
| 129 base::MakeUnique<SyncedSession>(); | 127 base::MakeUnique<SyncedSession>(); |
| 130 owned_session->session_tag = id_as_string; | 128 owned_session->session_tag = id_as_string; |
| 131 owned_session->session_name = id_as_string; | 129 owned_session->session_name = id_as_string; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 150 } | 148 } |
| 151 fake_foreign_sessions_provider_->SetAllForeignSessions(std::move(sessions)); | 149 fake_foreign_sessions_provider_->SetAllForeignSessions(std::move(sessions)); |
| 152 } | 150 } |
| 153 | 151 |
| 154 void Dismiss(const std::string& url) { | 152 void Dismiss(const std::string& url) { |
| 155 // The url of a given suggestion is used as the |id_within_category|. | 153 // The url of a given suggestion is used as the |id_within_category|. |
| 156 provider_->DismissSuggestion(ContentSuggestion::ID(category(), url)); | 154 provider_->DismissSuggestion(ContentSuggestion::ID(category(), url)); |
| 157 } | 155 } |
| 158 | 156 |
| 159 Category category() { | 157 Category category() { |
| 160 return category_factory_.FromKnownCategory(KnownCategories::FOREIGN_TABS); | 158 return Category::FromKnownCategory(KnownCategories::FOREIGN_TABS); |
| 161 } | 159 } |
| 162 | 160 |
| 163 MockContentSuggestionsProviderObserver* observer() { return &observer_; } | 161 MockContentSuggestionsProviderObserver* observer() { return &observer_; } |
| 164 | 162 |
| 165 private: | 163 private: |
| 166 FakeForeignSessionsProvider* fake_foreign_sessions_provider_; | 164 FakeForeignSessionsProvider* fake_foreign_sessions_provider_; |
| 167 MockContentSuggestionsProviderObserver observer_; | 165 MockContentSuggestionsProviderObserver observer_; |
| 168 CategoryFactory category_factory_; | |
| 169 TestingPrefServiceSimple pref_service_; | 166 TestingPrefServiceSimple pref_service_; |
| 170 std::unique_ptr<ForeignSessionsSuggestionsProvider> provider_; | 167 std::unique_ptr<ForeignSessionsSuggestionsProvider> provider_; |
| 171 std::map<int, std::unique_ptr<SyncedSession>> sessions_map_; | 168 std::map<int, std::unique_ptr<SyncedSession>> sessions_map_; |
| 172 | 169 |
| 173 DISALLOW_COPY_AND_ASSIGN(ForeignSessionsSuggestionsProviderTest); | 170 DISALLOW_COPY_AND_ASSIGN(ForeignSessionsSuggestionsProviderTest); |
| 174 }; | 171 }; |
| 175 | 172 |
| 176 TEST_F(ForeignSessionsSuggestionsProviderTest, Empty) { | 173 TEST_F(ForeignSessionsSuggestionsProviderTest, Empty) { |
| 177 // When no sessions data is added, expect no suggestions. | 174 // When no sessions data is added, expect no suggestions. |
| 178 EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty())); | 175 EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty())); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 EXPECT_CALL(*observer(), | 366 EXPECT_CALL(*observer(), |
| 370 OnNewSuggestions( | 367 OnNewSuggestions( |
| 371 _, category(), | 368 _, category(), |
| 372 ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)), | 369 ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)), |
| 373 Property(&ContentSuggestion::url, GURL(kUrl2))))); | 370 Property(&ContentSuggestion::url, GURL(kUrl2))))); |
| 374 AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2)); | 371 AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2)); |
| 375 TriggerOnChange(); | 372 TriggerOnChange(); |
| 376 } | 373 } |
| 377 | 374 |
| 378 } // namespace ntp_snippets | 375 } // namespace ntp_snippets |
| OLD | NEW |