| 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" |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 return provider_.get(); | 257 return provider_.get(); |
| 258 } | 258 } |
| 259 | 259 |
| 260 void DestroyProvider() { provider_.reset(); } | 260 void DestroyProvider() { provider_.reset(); } |
| 261 | 261 |
| 262 Category downloads_category() { | 262 Category downloads_category() { |
| 263 return category_factory_.FromKnownCategory( | 263 return category_factory_.FromKnownCategory( |
| 264 ntp_snippets::KnownCategories::DOWNLOADS); | 264 ntp_snippets::KnownCategories::DOWNLOADS); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void FireOfflinePageModelChanged() { | 267 void AddOfflinePage(const offline_pages::OfflinePageItem& added_page) { |
| 268 DCHECK(provider_); | 268 DCHECK(provider_); |
| 269 provider_->OfflinePageModelChanged(&offline_pages_model_); | 269 offline_pages_model_.mutable_items()->push_back(added_page); |
| 270 provider_->OfflinePageAdded(&offline_pages_model_, added_page); |
| 270 } | 271 } |
| 271 | 272 |
| 272 void FireOfflinePageDeleted(const OfflinePageItem& item) { | 273 void FireOfflinePageDeleted(const OfflinePageItem& item) { |
| 273 DCHECK(provider_); | 274 DCHECK(provider_); |
| 274 provider_->OfflinePageDeleted(item.offline_id, item.client_id); | 275 provider_->OfflinePageDeleted(item.offline_id, item.client_id); |
| 275 } | 276 } |
| 276 | 277 |
| 277 void FireDownloadCreated(DownloadItem* item) { | 278 void FireDownloadCreated(DownloadItem* item) { |
| 278 DCHECK(provider_); | 279 DCHECK(provider_); |
| 279 downloads_manager_.NotifyDownloadCreated(item); | 280 downloads_manager_.NotifyDownloadCreated(item); |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 | 506 |
| 506 provider()->DismissSuggestion( | 507 provider()->DismissSuggestion( |
| 507 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 508 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 508 provider()->DismissSuggestion( | 509 provider()->DismissSuggestion( |
| 509 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 510 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 510 | 511 |
| 511 EXPECT_CALL( | 512 EXPECT_CALL( |
| 512 *observer(), | 513 *observer(), |
| 513 OnNewSuggestions(_, downloads_category(), | 514 OnNewSuggestions(_, downloads_category(), |
| 514 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 515 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 516 HasUrl("http://dummy.com/3"), |
| 515 HasUrl("http://download.com/2")))); | 517 HasUrl("http://download.com/2")))); |
| 516 FireOfflinePageModelChanged(); | 518 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 517 } | 519 } |
| 518 | 520 |
| 519 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { | 521 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { |
| 520 IgnoreOnCategoryStatusChangedToAvailable(); | 522 IgnoreOnCategoryStatusChangedToAvailable(); |
| 521 | 523 |
| 522 EXPECT_CALL(*observer(), | 524 EXPECT_CALL(*observer(), |
| 523 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) | 525 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) |
| 524 .Times(2); | 526 .Times(2); |
| 525 EXPECT_CALL( | 527 EXPECT_CALL( |
| 526 *observer(), | 528 *observer(), |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 599 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 598 FireDownloadsCreated(downloads_manager()->items()); | 600 FireDownloadsCreated(downloads_manager()->items()); |
| 599 | 601 |
| 600 provider()->DismissSuggestion( | 602 provider()->DismissSuggestion( |
| 601 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 603 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 602 | 604 |
| 603 EXPECT_CALL( | 605 EXPECT_CALL( |
| 604 *observer(), | 606 *observer(), |
| 605 OnNewSuggestions(_, downloads_category(), | 607 OnNewSuggestions(_, downloads_category(), |
| 606 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 608 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 609 HasUrl("http://dummy.com/3"), |
| 607 HasUrl("http://download.com/1"), | 610 HasUrl("http://download.com/1"), |
| 608 HasUrl("http://download.com/2")))); | 611 HasUrl("http://download.com/2")))); |
| 609 FireOfflinePageModelChanged(); | 612 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 610 } | 613 } |
| 611 | 614 |
| 612 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { | 615 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { |
| 613 IgnoreOnCategoryStatusChangedToAvailable(); | 616 IgnoreOnCategoryStatusChangedToAvailable(); |
| 614 | 617 |
| 615 EXPECT_CALL(*observer(), | 618 EXPECT_CALL(*observer(), |
| 616 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) | 619 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) |
| 617 .Times(5); | 620 .Times(5); |
| 618 EXPECT_CALL( | 621 EXPECT_CALL( |
| 619 *observer(), | 622 *observer(), |
| 620 OnNewSuggestions(_, downloads_category(), | 623 OnNewSuggestions(_, downloads_category(), |
| 621 UnorderedElementsAre(HasUrl("http://download.com/1"), | 624 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 622 HasUrl("http://download.com/2"), | 625 HasUrl("http://download.com/2"), |
| 623 HasUrl("http://download.com/3"), | 626 HasUrl("http://download.com/3"), |
| 624 HasUrl("http://download.com/4"), | 627 HasUrl("http://download.com/4"), |
| 625 HasUrl("http://download.com/5")))); | 628 HasUrl("http://download.com/5")))); |
| 626 CreateProvider(); | 629 CreateProvider(); |
| 627 // Currently the provider stores five items in its internal cache, so six | 630 // Currently the provider stores five items in its internal cache, so six |
| 628 // items are needed to check whether all downloads are fetched on dismissal. | 631 // items are needed to check whether all downloads are fetched on dismissal. |
| 629 *(downloads_manager()->mutable_items()) = | 632 *(downloads_manager()->mutable_items()) = |
| 630 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6}); | 633 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6}); |
| 631 FireDownloadsCreated(downloads_manager()->items()); | 634 FireDownloadsCreated(downloads_manager()->items()); |
| 632 | 635 |
| 633 provider()->DismissSuggestion( | 636 provider()->DismissSuggestion( |
| 634 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 637 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 638 provider()->DismissSuggestion( |
| 639 GetDummySuggestionId(2, /*is_offline_page=*/false)); |
| 635 | 640 |
| 636 // The provider is not notified about the 6th item, however, it must report | 641 // The provider is not notified about the 6th item, however, it must report |
| 637 // it now. | 642 // it now. |
| 638 EXPECT_CALL( | 643 EXPECT_CALL( |
| 639 *observer(), | 644 *observer(), |
| 640 OnNewSuggestions(_, downloads_category(), | 645 OnNewSuggestions(_, downloads_category(), |
| 641 UnorderedElementsAre(HasUrl("http://download.com/2"), | 646 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 642 HasUrl("http://download.com/3"), | 647 HasUrl("http://download.com/3"), |
| 643 HasUrl("http://download.com/4"), | 648 HasUrl("http://download.com/4"), |
| 644 HasUrl("http://download.com/5"), | 649 HasUrl("http://download.com/5"), |
| 645 HasUrl("http://download.com/6")))); | 650 HasUrl("http://download.com/6")))); |
| 646 FireOfflinePageModelChanged(); | 651 AddOfflinePage(CreateDummyOfflinePage(1)); |
| 647 } | 652 } |
| 648 | 653 |
| 649 TEST_F(DownloadSuggestionsProviderTest, | 654 TEST_F(DownloadSuggestionsProviderTest, |
| 650 ShouldInvalidateWhenUnderlyingItemDeleted) { | 655 ShouldInvalidateWhenUnderlyingItemDeleted) { |
| 651 IgnoreOnCategoryStatusChangedToAvailable(); | 656 IgnoreOnCategoryStatusChangedToAvailable(); |
| 652 | 657 |
| 653 EXPECT_CALL(*observer(), | 658 EXPECT_CALL(*observer(), |
| 654 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(3ul)))); | 659 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(3ul)))); |
| 655 EXPECT_CALL( | 660 EXPECT_CALL( |
| 656 *observer(), | 661 *observer(), |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 HasUrl("http://download.com/100")))); | 727 HasUrl("http://download.com/100")))); |
| 723 FireDownloadCreated(removed_item.get()); | 728 FireDownloadCreated(removed_item.get()); |
| 724 | 729 |
| 725 // |OnDownloadDestroyed| notification is called in |DownloadItem|'s | 730 // |OnDownloadDestroyed| notification is called in |DownloadItem|'s |
| 726 // destructor. | 731 // destructor. |
| 727 removed_item.reset(); | 732 removed_item.reset(); |
| 728 | 733 |
| 729 EXPECT_CALL( | 734 EXPECT_CALL( |
| 730 *observer(), | 735 *observer(), |
| 731 OnNewSuggestions(_, downloads_category(), | 736 OnNewSuggestions(_, downloads_category(), |
| 732 UnorderedElementsAre(HasUrl("http://download.com/1"), | 737 UnorderedElementsAre(HasUrl("http://dummy.com/6"), |
| 738 HasUrl("http://download.com/1"), |
| 733 HasUrl("http://download.com/2"), | 739 HasUrl("http://download.com/2"), |
| 734 HasUrl("http://download.com/3"), | 740 HasUrl("http://download.com/3"), |
| 735 HasUrl("http://download.com/4"), | 741 HasUrl("http://download.com/4")))); |
| 736 HasUrl("http://download.com/5")))); | 742 AddOfflinePage(CreateDummyOfflinePage(6)); |
| 737 FireOfflinePageModelChanged(); | |
| 738 } | 743 } |
| 739 | 744 |
| 740 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneOfflinePagesDismissedIDs) { | 745 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneOfflinePagesDismissedIDs) { |
| 741 IgnoreOnCategoryStatusChangedToAvailable(); | 746 IgnoreOnCategoryStatusChangedToAvailable(); |
| 742 IgnoreOnSuggestionInvalidated(); | 747 IgnoreOnSuggestionInvalidated(); |
| 743 | 748 |
| 744 *(offline_pages_model()->mutable_items()) = | 749 auto offline_pages = CreateDummyOfflinePages({1, 2, 3}); |
| 745 CreateDummyOfflinePages({1, 2, 3}); | 750 |
| 751 *(offline_pages_model()->mutable_items()) = offline_pages; |
| 746 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 752 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 747 UnorderedElementsAre( | 753 UnorderedElementsAre( |
| 748 HasUrl("http://dummy.com/1"), | 754 HasUrl("http://dummy.com/1"), |
| 749 HasUrl("http://dummy.com/2"), | 755 HasUrl("http://dummy.com/2"), |
| 750 HasUrl("http://dummy.com/3")))); | 756 HasUrl("http://dummy.com/3")))); |
| 751 CreateProvider(); | 757 CreateProvider(); |
| 752 | 758 |
| 753 provider()->DismissSuggestion( | 759 provider()->DismissSuggestion( |
| 754 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 760 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 755 provider()->DismissSuggestion( | 761 provider()->DismissSuggestion( |
| 756 GetDummySuggestionId(2, /*is_offline_page=*/true)); | 762 GetDummySuggestionId(2, /*is_offline_page=*/true)); |
| 757 provider()->DismissSuggestion( | 763 provider()->DismissSuggestion( |
| 758 GetDummySuggestionId(3, /*is_offline_page=*/true)); | 764 GetDummySuggestionId(3, /*is_offline_page=*/true)); |
| 759 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); | 765 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); |
| 760 | 766 |
| 761 // Prune on getting all offline pages. Note that the first suggestion is not | 767 // Note that the first suggestion is not removed from |offline_pages_model| |
| 762 // removed from |offline_pages_model| storage, because otherwise | 768 // storage, because otherwise |GetDismissedSuggestions| cannot return it. |
| 763 // |GetDismissedSuggestions| cannot return it. | 769 FireOfflinePageDeleted(offline_pages[0]); |
| 764 EXPECT_CALL(*observer(), | |
| 765 OnNewSuggestions(_, downloads_category(), IsEmpty())); | |
| 766 | |
| 767 // TODO(vitaliii): Add the first suggestion back, so that | |
| 768 // GetDismissedSuggestions can return it. | |
| 769 *(offline_pages_model()->mutable_items()) = | |
| 770 CreateDummyOfflinePages({2, 3}); | |
| 771 FireOfflinePageModelChanged(); | |
| 772 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); | 770 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); |
| 773 | 771 |
| 774 // Prune when offline page is deleted. | 772 // Prune when offline page is deleted. |
| 775 FireOfflinePageDeleted(offline_pages_model()->items()[1]); | 773 FireOfflinePageDeleted(offline_pages_model()->items()[1]); |
| 776 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); | 774 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); |
| 777 } | 775 } |
| 778 | 776 |
| 779 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { | 777 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { |
| 780 IgnoreOnCategoryStatusChangedToAvailable(); | 778 IgnoreOnCategoryStatusChangedToAvailable(); |
| 781 IgnoreOnSuggestionInvalidated(); | 779 IgnoreOnSuggestionInvalidated(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 817 CreateProvider(); | 815 CreateProvider(); |
| 818 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 816 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 819 FireDownloadsCreated(downloads_manager()->items()); | 817 FireDownloadsCreated(downloads_manager()->items()); |
| 820 | 818 |
| 821 EXPECT_CALL(*observer(), | 819 EXPECT_CALL(*observer(), |
| 822 OnSuggestionInvalidated( | 820 OnSuggestionInvalidated( |
| 823 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); | 821 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); |
| 824 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); | 822 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); |
| 825 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); | 823 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); |
| 826 } | 824 } |
| OLD | NEW |