| 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 "chrome/browser/ntp_snippets/download_suggestions_provider.h" | 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "chrome/browser/ntp_snippets/fake_download_item.h" | 12 #include "chrome/browser/ntp_snippets/fake_download_item.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/mock_content_suggestions_provider_observer.h" | 14 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
| 16 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" | 15 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" |
| 17 #include "components/offline_pages/core/client_namespace_constants.h" | 16 #include "components/offline_pages/core/client_namespace_constants.h" |
| 18 #include "components/prefs/testing_pref_service.h" | 17 #include "components/prefs/testing_pref_service.h" |
| 19 #include "content/public/test/mock_download_item.h" | 18 #include "content/public/test/mock_download_item.h" |
| 20 #include "content/public/test/mock_download_manager.h" | 19 #include "content/public/test/mock_download_manager.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 21 |
| 23 using content::DownloadItem; | 22 using content::DownloadItem; |
| 24 using content::MockDownloadManager; | 23 using content::MockDownloadManager; |
| 25 using ntp_snippets::Category; | 24 using ntp_snippets::Category; |
| 26 using ntp_snippets::CategoryFactory; | |
| 27 using ntp_snippets::ContentSuggestion; | 25 using ntp_snippets::ContentSuggestion; |
| 28 using ntp_snippets::ContentSuggestionsProvider; | 26 using ntp_snippets::ContentSuggestionsProvider; |
| 29 using ntp_snippets::MockContentSuggestionsProviderObserver; | 27 using ntp_snippets::MockContentSuggestionsProviderObserver; |
| 30 using ntp_snippets::test::CaptureDismissedSuggestions; | 28 using ntp_snippets::test::CaptureDismissedSuggestions; |
| 31 using ntp_snippets::test::FakeOfflinePageModel; | 29 using ntp_snippets::test::FakeOfflinePageModel; |
| 32 using ntp_snippets::CategoryStatus; | 30 using ntp_snippets::CategoryStatus; |
| 33 using offline_pages::ClientId; | 31 using offline_pages::ClientId; |
| 34 using offline_pages::OfflinePageItem; | 32 using offline_pages::OfflinePageItem; |
| 35 using test::FakeDownloadItem; | 33 using test::FakeDownloadItem; |
| 36 using testing::_; | 34 using testing::_; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 248 |
| 251 void IgnoreOnSuggestionInvalidated() { | 249 void IgnoreOnSuggestionInvalidated() { |
| 252 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); | 250 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); |
| 253 } | 251 } |
| 254 | 252 |
| 255 DownloadSuggestionsProvider* CreateProvider(bool show_assets, | 253 DownloadSuggestionsProvider* CreateProvider(bool show_assets, |
| 256 bool show_offline_pages) { | 254 bool show_offline_pages) { |
| 257 DCHECK(!provider_); | 255 DCHECK(!provider_); |
| 258 DCHECK(show_assets || show_offline_pages); | 256 DCHECK(show_assets || show_offline_pages); |
| 259 provider_ = base::MakeUnique<DownloadSuggestionsProvider>( | 257 provider_ = base::MakeUnique<DownloadSuggestionsProvider>( |
| 260 &observer_, &category_factory_, | 258 &observer_, show_offline_pages ? &offline_pages_model_ : nullptr, |
| 261 show_offline_pages ? &offline_pages_model_ : nullptr, | |
| 262 show_assets ? &downloads_manager_ : nullptr, pref_service(), | 259 show_assets ? &downloads_manager_ : nullptr, pref_service(), |
| 263 /*download_manager_ui_enabled=*/false); | 260 /*download_manager_ui_enabled=*/false); |
| 264 return provider_.get(); | 261 return provider_.get(); |
| 265 } | 262 } |
| 266 | 263 |
| 267 void DestroyProvider() { provider_.reset(); } | 264 void DestroyProvider() { provider_.reset(); } |
| 268 | 265 |
| 269 Category downloads_category() { | 266 Category downloads_category() { |
| 270 return category_factory_.FromKnownCategory( | 267 return Category::FromKnownCategory( |
| 271 ntp_snippets::KnownCategories::DOWNLOADS); | 268 ntp_snippets::KnownCategories::DOWNLOADS); |
| 272 } | 269 } |
| 273 | 270 |
| 274 void FireOfflinePageModelLoaded() { | 271 void FireOfflinePageModelLoaded() { |
| 275 DCHECK(provider_); | 272 DCHECK(provider_); |
| 276 provider_->OfflinePageModelLoaded(&offline_pages_model_); | 273 provider_->OfflinePageModelLoaded(&offline_pages_model_); |
| 277 } | 274 } |
| 278 | 275 |
| 279 void AddOfflinePage(const offline_pages::OfflinePageItem& added_page) { | 276 void AddOfflinePage(const offline_pages::OfflinePageItem& added_page) { |
| 280 DCHECK(provider_); | 277 DCHECK(provider_); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 return &downloads_manager_; | 319 return &downloads_manager_; |
| 323 } | 320 } |
| 324 FakeOfflinePageModel* offline_pages_model() { return &offline_pages_model_; } | 321 FakeOfflinePageModel* offline_pages_model() { return &offline_pages_model_; } |
| 325 MockContentSuggestionsProviderObserver* observer() { return &observer_; } | 322 MockContentSuggestionsProviderObserver* observer() { return &observer_; } |
| 326 TestingPrefServiceSimple* pref_service() { return pref_service_.get(); } | 323 TestingPrefServiceSimple* pref_service() { return pref_service_.get(); } |
| 327 | 324 |
| 328 private: | 325 private: |
| 329 ObservedMockDownloadManager downloads_manager_; | 326 ObservedMockDownloadManager downloads_manager_; |
| 330 FakeOfflinePageModel offline_pages_model_; | 327 FakeOfflinePageModel offline_pages_model_; |
| 331 StrictMock<MockContentSuggestionsProviderObserver> observer_; | 328 StrictMock<MockContentSuggestionsProviderObserver> observer_; |
| 332 CategoryFactory category_factory_; | |
| 333 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 329 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 334 // Last so that the dependencies are deleted after the provider. | 330 // Last so that the dependencies are deleted after the provider. |
| 335 std::unique_ptr<DownloadSuggestionsProvider> provider_; | 331 std::unique_ptr<DownloadSuggestionsProvider> provider_; |
| 336 | 332 |
| 337 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProviderTest); | 333 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProviderTest); |
| 338 }; | 334 }; |
| 339 | 335 |
| 340 TEST_F(DownloadSuggestionsProviderTest, | 336 TEST_F(DownloadSuggestionsProviderTest, |
| 341 ShouldConvertOfflinePagesToSuggestions) { | 337 ShouldConvertOfflinePagesToSuggestions) { |
| 342 IgnoreOnCategoryStatusChangedToAvailable(); | 338 IgnoreOnCategoryStatusChangedToAvailable(); |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1003 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(kMaxDismissedIdCount)); | 999 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(kMaxDismissedIdCount)); |
| 1004 DestroyProvider(); | 1000 DestroyProvider(); |
| 1005 // The oldest dismissed suggestion must become undismissed now. This is a | 1001 // The oldest dismissed suggestion must become undismissed now. This is a |
| 1006 // temporary workaround and not what we want in long term. This test must be | 1002 // temporary workaround and not what we want in long term. This test must be |
| 1007 // removed once we start pruning dismissed asset downloads on startup. | 1003 // removed once we start pruning dismissed asset downloads on startup. |
| 1008 EXPECT_CALL(*observer(), | 1004 EXPECT_CALL(*observer(), |
| 1009 OnNewSuggestions(_, downloads_category(), | 1005 OnNewSuggestions(_, downloads_category(), |
| 1010 ElementsAre(HasUrl("http://download.com/0")))); | 1006 ElementsAre(HasUrl("http://download.com/0")))); |
| 1011 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 1007 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); |
| 1012 } | 1008 } |
| OLD | NEW |