| 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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 Category downloads_category() { | 269 Category downloads_category() { |
| 270 return category_factory_.FromKnownCategory( | 270 return category_factory_.FromKnownCategory( |
| 271 ntp_snippets::KnownCategories::DOWNLOADS); | 271 ntp_snippets::KnownCategories::DOWNLOADS); |
| 272 } | 272 } |
| 273 | 273 |
| 274 void FireOfflinePageModelLoaded() { | 274 void FireOfflinePageModelLoaded() { |
| 275 DCHECK(provider_); | 275 DCHECK(provider_); |
| 276 provider_->OfflinePageModelLoaded(&offline_pages_model_); | 276 provider_->OfflinePageModelLoaded(&offline_pages_model_); |
| 277 } | 277 } |
| 278 | 278 |
| 279 void FireOfflinePageModelChanged() { | 279 void AddOfflinePage(const offline_pages::OfflinePageItem& added_page) { |
| 280 DCHECK(provider_); | 280 DCHECK(provider_); |
| 281 provider_->OfflinePageModelChanged(&offline_pages_model_); | 281 offline_pages_model_.mutable_items()->push_back(added_page); |
| 282 provider_->OfflinePageAdded(&offline_pages_model_, added_page); |
| 282 } | 283 } |
| 283 | 284 |
| 284 void FireOfflinePageDeleted(const OfflinePageItem& item) { | 285 void FireOfflinePageDeleted(const OfflinePageItem& item) { |
| 285 DCHECK(provider_); | 286 DCHECK(provider_); |
| 286 provider_->OfflinePageDeleted(item.offline_id, item.client_id); | 287 provider_->OfflinePageDeleted(item.offline_id, item.client_id); |
| 287 } | 288 } |
| 288 | 289 |
| 289 void FireDownloadCreated(DownloadItem* item) { | 290 void FireDownloadCreated(DownloadItem* item) { |
| 290 DCHECK(provider_); | 291 DCHECK(provider_); |
| 291 downloads_manager_.NotifyDownloadCreated(item); | 292 downloads_manager_.NotifyDownloadCreated(item); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 | 519 |
| 519 provider()->DismissSuggestion( | 520 provider()->DismissSuggestion( |
| 520 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 521 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 521 provider()->DismissSuggestion( | 522 provider()->DismissSuggestion( |
| 522 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 523 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 523 | 524 |
| 524 EXPECT_CALL( | 525 EXPECT_CALL( |
| 525 *observer(), | 526 *observer(), |
| 526 OnNewSuggestions(_, downloads_category(), | 527 OnNewSuggestions(_, downloads_category(), |
| 527 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 528 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 529 HasUrl("http://dummy.com/3"), |
| 528 HasUrl("http://download.com/2")))); | 530 HasUrl("http://download.com/2")))); |
| 529 FireOfflinePageModelChanged(); | 531 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 530 } | 532 } |
| 531 | 533 |
| 532 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { | 534 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { |
| 533 IgnoreOnCategoryStatusChangedToAvailable(); | 535 IgnoreOnCategoryStatusChangedToAvailable(); |
| 534 | 536 |
| 535 EXPECT_CALL(*observer(), | 537 EXPECT_CALL(*observer(), |
| 536 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) | 538 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) |
| 537 .Times(2); | 539 .Times(2); |
| 538 EXPECT_CALL( | 540 EXPECT_CALL( |
| 539 *observer(), | 541 *observer(), |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 612 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 611 FireDownloadsCreated(downloads_manager()->items()); | 613 FireDownloadsCreated(downloads_manager()->items()); |
| 612 | 614 |
| 613 provider()->DismissSuggestion( | 615 provider()->DismissSuggestion( |
| 614 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 616 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 615 | 617 |
| 616 EXPECT_CALL( | 618 EXPECT_CALL( |
| 617 *observer(), | 619 *observer(), |
| 618 OnNewSuggestions(_, downloads_category(), | 620 OnNewSuggestions(_, downloads_category(), |
| 619 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 621 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 622 HasUrl("http://dummy.com/3"), |
| 620 HasUrl("http://download.com/1"), | 623 HasUrl("http://download.com/1"), |
| 621 HasUrl("http://download.com/2")))); | 624 HasUrl("http://download.com/2")))); |
| 622 FireOfflinePageModelChanged(); | 625 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 623 } | 626 } |
| 624 | 627 |
| 625 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { | 628 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { |
| 626 IgnoreOnCategoryStatusChangedToAvailable(); | 629 IgnoreOnCategoryStatusChangedToAvailable(); |
| 627 | 630 |
| 628 EXPECT_CALL(*observer(), | 631 EXPECT_CALL(*observer(), |
| 629 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) | 632 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) |
| 630 .Times(5); | 633 .Times(5); |
| 631 EXPECT_CALL( | 634 EXPECT_CALL( |
| 632 *observer(), | 635 *observer(), |
| 633 OnNewSuggestions(_, downloads_category(), | 636 OnNewSuggestions(_, downloads_category(), |
| 634 UnorderedElementsAre(HasUrl("http://download.com/1"), | 637 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 635 HasUrl("http://download.com/2"), | 638 HasUrl("http://download.com/2"), |
| 636 HasUrl("http://download.com/3"), | 639 HasUrl("http://download.com/3"), |
| 637 HasUrl("http://download.com/4"), | 640 HasUrl("http://download.com/4"), |
| 638 HasUrl("http://download.com/5")))); | 641 HasUrl("http://download.com/5")))); |
| 639 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 642 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 640 // Currently the provider stores five items in its internal cache, so six | 643 // Currently the provider stores five items in its internal cache, so six |
| 641 // items are needed to check whether all downloads are fetched on dismissal. | 644 // items are needed to check whether all downloads are fetched on dismissal. |
| 642 *(downloads_manager()->mutable_items()) = | 645 *(downloads_manager()->mutable_items()) = |
| 643 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6}); | 646 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6}); |
| 644 FireDownloadsCreated(downloads_manager()->items()); | 647 FireDownloadsCreated(downloads_manager()->items()); |
| 645 | 648 |
| 646 provider()->DismissSuggestion( | 649 provider()->DismissSuggestion( |
| 647 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 650 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 651 provider()->DismissSuggestion( |
| 652 GetDummySuggestionId(2, /*is_offline_page=*/false)); |
| 648 | 653 |
| 649 // The provider is not notified about the 6th item, however, it must report | 654 // The provider is not notified about the 6th item, however, it must report |
| 650 // it now. | 655 // it now. |
| 651 EXPECT_CALL( | 656 EXPECT_CALL( |
| 652 *observer(), | 657 *observer(), |
| 653 OnNewSuggestions(_, downloads_category(), | 658 OnNewSuggestions(_, downloads_category(), |
| 654 UnorderedElementsAre(HasUrl("http://download.com/2"), | 659 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 655 HasUrl("http://download.com/3"), | 660 HasUrl("http://download.com/3"), |
| 656 HasUrl("http://download.com/4"), | 661 HasUrl("http://download.com/4"), |
| 657 HasUrl("http://download.com/5"), | 662 HasUrl("http://download.com/5"), |
| 658 HasUrl("http://download.com/6")))); | 663 HasUrl("http://download.com/6")))); |
| 659 FireOfflinePageModelChanged(); | 664 AddOfflinePage(CreateDummyOfflinePage(1)); |
| 660 } | 665 } |
| 661 | 666 |
| 662 TEST_F(DownloadSuggestionsProviderTest, | 667 TEST_F(DownloadSuggestionsProviderTest, |
| 663 ShouldInvalidateWhenUnderlyingItemDeleted) { | 668 ShouldInvalidateWhenUnderlyingItemDeleted) { |
| 664 IgnoreOnCategoryStatusChangedToAvailable(); | 669 IgnoreOnCategoryStatusChangedToAvailable(); |
| 665 | 670 |
| 666 EXPECT_CALL(*observer(), | 671 EXPECT_CALL(*observer(), |
| 667 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(3ul)))); | 672 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(3ul)))); |
| 668 EXPECT_CALL( | 673 EXPECT_CALL( |
| 669 *observer(), | 674 *observer(), |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 HasUrl("http://download.com/100")))); | 740 HasUrl("http://download.com/100")))); |
| 736 FireDownloadCreated(removed_item.get()); | 741 FireDownloadCreated(removed_item.get()); |
| 737 | 742 |
| 738 // |OnDownloadDestroyed| notification is called in |DownloadItem|'s | 743 // |OnDownloadDestroyed| notification is called in |DownloadItem|'s |
| 739 // destructor. | 744 // destructor. |
| 740 removed_item.reset(); | 745 removed_item.reset(); |
| 741 | 746 |
| 742 EXPECT_CALL( | 747 EXPECT_CALL( |
| 743 *observer(), | 748 *observer(), |
| 744 OnNewSuggestions(_, downloads_category(), | 749 OnNewSuggestions(_, downloads_category(), |
| 745 UnorderedElementsAre(HasUrl("http://download.com/1"), | 750 UnorderedElementsAre(HasUrl("http://dummy.com/6"), |
| 751 HasUrl("http://download.com/1"), |
| 746 HasUrl("http://download.com/2"), | 752 HasUrl("http://download.com/2"), |
| 747 HasUrl("http://download.com/3"), | 753 HasUrl("http://download.com/3"), |
| 748 HasUrl("http://download.com/4"), | 754 HasUrl("http://download.com/4")))); |
| 749 HasUrl("http://download.com/5")))); | 755 AddOfflinePage(CreateDummyOfflinePage(6)); |
| 750 FireOfflinePageModelChanged(); | |
| 751 } | 756 } |
| 752 | 757 |
| 753 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneOfflinePagesDismissedIDs) { | 758 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneOfflinePagesDismissedIDs) { |
| 754 IgnoreOnCategoryStatusChangedToAvailable(); | 759 IgnoreOnCategoryStatusChangedToAvailable(); |
| 755 IgnoreOnSuggestionInvalidated(); | 760 IgnoreOnSuggestionInvalidated(); |
| 756 | 761 |
| 757 *(offline_pages_model()->mutable_items()) = | 762 auto offline_pages = CreateDummyOfflinePages({1, 2, 3}); |
| 758 CreateDummyOfflinePages({1, 2, 3}); | 763 |
| 764 *(offline_pages_model()->mutable_items()) = offline_pages; |
| 759 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 765 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 760 UnorderedElementsAre( | 766 UnorderedElementsAre( |
| 761 HasUrl("http://dummy.com/1"), | 767 HasUrl("http://dummy.com/1"), |
| 762 HasUrl("http://dummy.com/2"), | 768 HasUrl("http://dummy.com/2"), |
| 763 HasUrl("http://dummy.com/3")))); | 769 HasUrl("http://dummy.com/3")))); |
| 764 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 770 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 765 | 771 |
| 766 provider()->DismissSuggestion( | 772 provider()->DismissSuggestion( |
| 767 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 773 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 768 provider()->DismissSuggestion( | 774 provider()->DismissSuggestion( |
| 769 GetDummySuggestionId(2, /*is_offline_page=*/true)); | 775 GetDummySuggestionId(2, /*is_offline_page=*/true)); |
| 770 provider()->DismissSuggestion( | 776 provider()->DismissSuggestion( |
| 771 GetDummySuggestionId(3, /*is_offline_page=*/true)); | 777 GetDummySuggestionId(3, /*is_offline_page=*/true)); |
| 772 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); | 778 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); |
| 773 | 779 |
| 774 // Prune on getting all offline pages. | 780 // Note that the first suggestion is not removed from |offline_pages_model| |
| 775 EXPECT_CALL(*observer(), | 781 // storage, because otherwise |GetDismissedSuggestions| cannot return it. |
| 776 OnNewSuggestions(_, downloads_category(), IsEmpty())); | 782 FireOfflinePageDeleted(offline_pages[0]); |
| 777 | |
| 778 *(offline_pages_model()->mutable_items()) = | |
| 779 CreateDummyOfflinePages({2, 3}); | |
| 780 FireOfflinePageModelChanged(); | |
| 781 | |
| 782 // The first suggestion is added back to the |offline_pages_model| storage, | |
| 783 // because otherwise |GetDismissedSuggestions| cannot return it. | |
| 784 *(offline_pages_model()->mutable_items()) = | |
| 785 CreateDummyOfflinePages({1, 2, 3}); | |
| 786 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); | 783 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); |
| 787 | 784 |
| 788 // Prune when offline page is deleted. | 785 // Prune when offline page is deleted. |
| 789 FireOfflinePageDeleted(offline_pages_model()->items()[1]); | 786 FireOfflinePageDeleted(offline_pages_model()->items()[1]); |
| 790 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); | 787 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); |
| 791 } | 788 } |
| 792 | 789 |
| 793 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { | 790 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { |
| 794 IgnoreOnCategoryStatusChangedToAvailable(); | 791 IgnoreOnCategoryStatusChangedToAvailable(); |
| 795 IgnoreOnSuggestionInvalidated(); | 792 IgnoreOnSuggestionInvalidated(); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 922 IgnoreOnSuggestionInvalidated(); | 919 IgnoreOnSuggestionInvalidated(); |
| 923 | 920 |
| 924 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 921 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 925 EXPECT_CALL( | 922 EXPECT_CALL( |
| 926 *observer(), | 923 *observer(), |
| 927 OnNewSuggestions(_, downloads_category(), | 924 OnNewSuggestions(_, downloads_category(), |
| 928 UnorderedElementsAre(HasUrl("http://download.com/1"), | 925 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 929 HasUrl("http://download.com/2")))); | 926 HasUrl("http://download.com/2")))); |
| 930 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 927 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); |
| 931 } | 928 } |
| OLD | NEW |