| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 292 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| 293 Category category, | 293 Category category, |
| 294 CategoryStatus new_status) override { | 294 CategoryStatus new_status) override { |
| 295 if (category.IsKnownCategory(KnownCategories::ARTICLES) && | 295 if (category.IsKnownCategory(KnownCategories::ARTICLES) && |
| 296 IsCategoryStatusAvailable(new_status)) { | 296 IsCategoryStatusAvailable(new_status)) { |
| 297 loaded_.Signal(); | 297 loaded_.Signal(); |
| 298 } | 298 } |
| 299 statuses_[category] = new_status; | 299 statuses_[category] = new_status; |
| 300 } | 300 } |
| 301 | 301 |
| 302 void OnSuggestionInvalidated(ContentSuggestionsProvider* provider, | 302 void OnSuggestionInvalidated( |
| 303 Category category, | 303 ContentSuggestionsProvider* provider, |
| 304 const std::string& suggestion_id) override {} | 304 const ContentSuggestion::ID& suggestion_id) override {} |
| 305 | 305 |
| 306 const std::map<Category, CategoryStatus, Category::CompareByID>& statuses() | 306 const std::map<Category, CategoryStatus, Category::CompareByID>& statuses() |
| 307 const { | 307 const { |
| 308 return statuses_; | 308 return statuses_; |
| 309 } | 309 } |
| 310 | 310 |
| 311 CategoryStatus StatusForCategory(Category category) const { | 311 CategoryStatus StatusForCategory(Category category) const { |
| 312 auto it = statuses_.find(category); | 312 auto it = statuses_.find(category); |
| 313 if (it == statuses_.end()) { | 313 if (it == statuses_.end()) { |
| 314 return CategoryStatus::NOT_PROVIDED; | 314 return CategoryStatus::NOT_PROVIDED; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 base::RunLoop().RunUntilIdle(); | 416 base::RunLoop().RunUntilIdle(); |
| 417 observer_->WaitForLoad(); | 417 observer_->WaitForLoad(); |
| 418 } | 418 } |
| 419 | 419 |
| 420 void ResetSnippetsService(std::unique_ptr<NTPSnippetsService>* service) { | 420 void ResetSnippetsService(std::unique_ptr<NTPSnippetsService>* service) { |
| 421 service->reset(); | 421 service->reset(); |
| 422 observer_.reset(); | 422 observer_.reset(); |
| 423 *service = MakeSnippetsService(); | 423 *service = MakeSnippetsService(); |
| 424 } | 424 } |
| 425 | 425 |
| 426 std::string MakeArticleID(const NTPSnippetsService& service, | 426 ContentSuggestion::ID MakeArticleID(const std::string& within_category_id) { |
| 427 const std::string& within_category_id) { | 427 return ContentSuggestion::ID(articles_category(), within_category_id); |
| 428 return service.MakeUniqueID(articles_category(), within_category_id); | |
| 429 } | 428 } |
| 430 | 429 |
| 431 Category articles_category() { | 430 Category articles_category() { |
| 432 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); | 431 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); |
| 433 } | 432 } |
| 434 | 433 |
| 435 std::string MakeOtherID(const NTPSnippetsService& service, | 434 ContentSuggestion::ID MakeOtherID(const std::string& within_category_id) { |
| 436 const std::string& within_category_id) { | 435 return ContentSuggestion::ID(other_category(), within_category_id); |
| 437 return service.MakeUniqueID(other_category(), within_category_id); | |
| 438 } | 436 } |
| 439 | 437 |
| 440 Category other_category() { return category_factory_.FromRemoteCategory(2); } | 438 Category other_category() { return category_factory_.FromRemoteCategory(2); } |
| 441 | 439 |
| 442 protected: | 440 protected: |
| 443 const GURL& test_url() { return test_url_; } | 441 const GURL& test_url() { return test_url_; } |
| 444 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } | 442 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } |
| 445 MockScheduler& mock_scheduler() { return scheduler_; } | 443 MockScheduler& mock_scheduler() { return scheduler_; } |
| 446 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } | 444 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } |
| 447 | 445 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 | 601 |
| 604 LoadFromJSONString(service.get(), json_str); | 602 LoadFromJSONString(service.get(), json_str); |
| 605 | 603 |
| 606 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | 604 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 607 SizeIs(1)); | 605 SizeIs(1)); |
| 608 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 606 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 609 | 607 |
| 610 const ContentSuggestion& suggestion = | 608 const ContentSuggestion& suggestion = |
| 611 observer().SuggestionsForCategory(articles_category()).front(); | 609 observer().SuggestionsForCategory(articles_category()).front(); |
| 612 | 610 |
| 613 EXPECT_EQ(MakeArticleID(*service, kSnippetUrl), suggestion.id()); | 611 EXPECT_EQ(MakeArticleID(kSnippetUrl), suggestion.id()); |
| 614 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | 612 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); |
| 615 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | 613 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); |
| 616 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | 614 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); |
| 617 EXPECT_EQ(kSnippetPublisherName, | 615 EXPECT_EQ(kSnippetPublisherName, |
| 618 base::UTF16ToUTF8(suggestion.publisher_name())); | 616 base::UTF16ToUTF8(suggestion.publisher_name())); |
| 619 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | 617 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); |
| 620 } | 618 } |
| 621 | 619 |
| 622 TEST_F(NTPSnippetsServiceTest, MultipleCategories) { | 620 TEST_F(NTPSnippetsServiceTest, MultipleCategories) { |
| 623 std::string json_str( | 621 std::string json_str( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 637 EXPECT_THAT(service->GetSnippetsForTesting(other_category()), SizeIs(1)); | 635 EXPECT_THAT(service->GetSnippetsForTesting(other_category()), SizeIs(1)); |
| 638 | 636 |
| 639 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), | 637 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 640 SizeIs(1)); | 638 SizeIs(1)); |
| 641 | 639 |
| 642 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); | 640 ASSERT_THAT(observer().SuggestionsForCategory(other_category()), SizeIs(1)); |
| 643 | 641 |
| 644 { | 642 { |
| 645 const ContentSuggestion& suggestion = | 643 const ContentSuggestion& suggestion = |
| 646 observer().SuggestionsForCategory(articles_category()).front(); | 644 observer().SuggestionsForCategory(articles_category()).front(); |
| 647 EXPECT_EQ(MakeArticleID(*service, std::string(kSnippetUrl) + "/0"), | 645 EXPECT_EQ(MakeArticleID(std::string(kSnippetUrl) + "/0"), suggestion.id()); |
| 648 suggestion.id()); | |
| 649 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | 646 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); |
| 650 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | 647 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); |
| 651 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | 648 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); |
| 652 EXPECT_EQ(kSnippetPublisherName, | 649 EXPECT_EQ(kSnippetPublisherName, |
| 653 base::UTF16ToUTF8(suggestion.publisher_name())); | 650 base::UTF16ToUTF8(suggestion.publisher_name())); |
| 654 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | 651 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); |
| 655 } | 652 } |
| 656 | 653 |
| 657 { | 654 { |
| 658 const ContentSuggestion& suggestion = | 655 const ContentSuggestion& suggestion = |
| 659 observer().SuggestionsForCategory(other_category()).front(); | 656 observer().SuggestionsForCategory(other_category()).front(); |
| 660 EXPECT_EQ(MakeOtherID(*service, std::string(kSnippetUrl) + "/1"), | 657 EXPECT_EQ(MakeOtherID(std::string(kSnippetUrl) + "/1"), suggestion.id()); |
| 661 suggestion.id()); | |
| 662 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | 658 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); |
| 663 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | 659 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); |
| 664 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | 660 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); |
| 665 EXPECT_EQ(kSnippetPublisherName, | 661 EXPECT_EQ(kSnippetPublisherName, |
| 666 base::UTF16ToUTF8(suggestion.publisher_name())); | 662 base::UTF16ToUTF8(suggestion.publisher_name())); |
| 667 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | 663 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); |
| 668 } | 664 } |
| 669 } | 665 } |
| 670 | 666 |
| 671 TEST_F(NTPSnippetsServiceTest, Clear) { | 667 TEST_F(NTPSnippetsServiceTest, Clear) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 auto service = MakeSnippetsService(); | 740 auto service = MakeSnippetsService(); |
| 745 | 741 |
| 746 std::string json_str( | 742 std::string json_str( |
| 747 GetTestJson({GetSnippetWithSources("http://site.com", "Source 1", "")})); | 743 GetTestJson({GetSnippetWithSources("http://site.com", "Source 1", "")})); |
| 748 | 744 |
| 749 LoadFromJSONString(service.get(), json_str); | 745 LoadFromJSONString(service.get(), json_str); |
| 750 | 746 |
| 751 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 747 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 752 | 748 |
| 753 // Dismissing a non-existent snippet shouldn't do anything. | 749 // Dismissing a non-existent snippet shouldn't do anything. |
| 754 service->DismissSuggestion(MakeArticleID(*service, "http://othersite.com")); | 750 service->DismissSuggestion(MakeArticleID("http://othersite.com")); |
| 755 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 751 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 756 | 752 |
| 757 // Dismiss the snippet. | 753 // Dismiss the snippet. |
| 758 service->DismissSuggestion(MakeArticleID(*service, kSnippetUrl)); | 754 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); |
| 759 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 755 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 760 | 756 |
| 761 // Make sure that fetching the same snippet again does not re-add it. | 757 // Make sure that fetching the same snippet again does not re-add it. |
| 762 LoadFromJSONString(service.get(), json_str); | 758 LoadFromJSONString(service.get(), json_str); |
| 763 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 759 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 764 | 760 |
| 765 // The snippet should stay dismissed even after re-creating the service. | 761 // The snippet should stay dismissed even after re-creating the service. |
| 766 ResetSnippetsService(&service); | 762 ResetSnippetsService(&service); |
| 767 LoadFromJSONString(service.get(), json_str); | 763 LoadFromJSONString(service.get(), json_str); |
| 768 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 764 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 769 | 765 |
| 770 // The snippet can be added again after clearing dismissed snippets. | 766 // The snippet can be added again after clearing dismissed snippets. |
| 771 service->ClearDismissedSuggestionsForDebugging(articles_category()); | 767 service->ClearDismissedSuggestionsForDebugging(articles_category()); |
| 772 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 768 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 773 LoadFromJSONString(service.get(), json_str); | 769 LoadFromJSONString(service.get(), json_str); |
| 774 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 770 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 775 } | 771 } |
| 776 | 772 |
| 777 TEST_F(NTPSnippetsServiceTest, GetDismissed) { | 773 TEST_F(NTPSnippetsServiceTest, GetDismissed) { |
| 778 auto service = MakeSnippetsService(); | 774 auto service = MakeSnippetsService(); |
| 779 | 775 |
| 780 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 776 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
| 781 | 777 |
| 782 service->DismissSuggestion(MakeArticleID(*service, kSnippetUrl)); | 778 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); |
| 783 | 779 |
| 784 service->GetDismissedSuggestionsForDebugging( | 780 service->GetDismissedSuggestionsForDebugging( |
| 785 articles_category(), | 781 articles_category(), |
| 786 base::Bind( | 782 base::Bind( |
| 787 [](NTPSnippetsService* service, NTPSnippetsServiceTest* test, | 783 [](NTPSnippetsService* service, NTPSnippetsServiceTest* test, |
| 788 std::vector<ContentSuggestion> dismissed_suggestions) { | 784 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 789 EXPECT_EQ(1u, dismissed_suggestions.size()); | 785 EXPECT_EQ(1u, dismissed_suggestions.size()); |
| 790 for (auto& suggestion : dismissed_suggestions) { | 786 for (auto& suggestion : dismissed_suggestions) { |
| 791 EXPECT_EQ(test->MakeArticleID(*service, kSnippetUrl), | 787 EXPECT_EQ(test->MakeArticleID(kSnippetUrl), suggestion.id()); |
| 792 suggestion.id()); | |
| 793 } | 788 } |
| 794 }, | 789 }, |
| 795 service.get(), this)); | 790 service.get(), this)); |
| 796 base::RunLoop().RunUntilIdle(); | 791 base::RunLoop().RunUntilIdle(); |
| 797 | 792 |
| 798 // There should be no dismissed snippet after clearing the list. | 793 // There should be no dismissed snippet after clearing the list. |
| 799 service->ClearDismissedSuggestionsForDebugging(articles_category()); | 794 service->ClearDismissedSuggestionsForDebugging(articles_category()); |
| 800 service->GetDismissedSuggestionsForDebugging( | 795 service->GetDismissedSuggestionsForDebugging( |
| 801 articles_category(), | 796 articles_category(), |
| 802 base::Bind( | 797 base::Bind( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 822 } | 817 } |
| 823 | 818 |
| 824 TEST_F(NTPSnippetsServiceTest, RemoveExpiredDismissedContent) { | 819 TEST_F(NTPSnippetsServiceTest, RemoveExpiredDismissedContent) { |
| 825 auto service = MakeSnippetsService(); | 820 auto service = MakeSnippetsService(); |
| 826 | 821 |
| 827 std::string json_str1(GetTestJson({GetExpiredSnippet()})); | 822 std::string json_str1(GetTestJson({GetExpiredSnippet()})); |
| 828 // Load it. | 823 // Load it. |
| 829 LoadFromJSONString(service.get(), json_str1); | 824 LoadFromJSONString(service.get(), json_str1); |
| 830 // Dismiss the suggestion | 825 // Dismiss the suggestion |
| 831 service->DismissSuggestion( | 826 service->DismissSuggestion( |
| 832 service->MakeUniqueID(service->articles_category_, kSnippetUrl)); | 827 ContentSuggestion::ID(articles_category(), kSnippetUrl)); |
| 833 | 828 |
| 834 // Load a different snippet - this will clear the expired dismissed ones. | 829 // Load a different snippet - this will clear the expired dismissed ones. |
| 835 std::string json_str2(GetTestJson({GetSnippetWithUrl(kSnippetUrl2)})); | 830 std::string json_str2(GetTestJson({GetSnippetWithUrl(kSnippetUrl2)})); |
| 836 LoadFromJSONString(service.get(), json_str2); | 831 LoadFromJSONString(service.get(), json_str2); |
| 837 | 832 |
| 838 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 833 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 839 IsEmpty()); | 834 IsEmpty()); |
| 840 } | 835 } |
| 841 | 836 |
| 842 TEST_F(NTPSnippetsServiceTest, ExpiredContentNotRemoved) { | 837 TEST_F(NTPSnippetsServiceTest, ExpiredContentNotRemoved) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 base::Bucket(/*min=*/1, /*count=*/2))); | 916 base::Bucket(/*min=*/1, /*count=*/2))); |
| 922 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | 917 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), |
| 923 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | 918 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
| 924 base::Bucket(/*min=*/1, /*count=*/2))); | 919 base::Bucket(/*min=*/1, /*count=*/2))); |
| 925 EXPECT_THAT( | 920 EXPECT_THAT( |
| 926 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 921 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
| 927 IsEmpty()); | 922 IsEmpty()); |
| 928 | 923 |
| 929 // Dismissing a snippet should decrease the list size. This will only be | 924 // Dismissing a snippet should decrease the list size. This will only be |
| 930 // logged after the next fetch. | 925 // logged after the next fetch. |
| 931 service->DismissSuggestion(MakeArticleID(*service, kSnippetUrl)); | 926 service->DismissSuggestion(MakeArticleID(kSnippetUrl)); |
| 932 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 927 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
| 933 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | 928 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), |
| 934 ElementsAre(base::Bucket(/*min=*/0, /*count=*/3), | 929 ElementsAre(base::Bucket(/*min=*/0, /*count=*/3), |
| 935 base::Bucket(/*min=*/1, /*count=*/2))); | 930 base::Bucket(/*min=*/1, /*count=*/2))); |
| 936 // Dismissed snippets shouldn't influence NumArticlesFetched. | 931 // Dismissed snippets shouldn't influence NumArticlesFetched. |
| 937 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | 932 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), |
| 938 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | 933 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
| 939 base::Bucket(/*min=*/1, /*count=*/3))); | 934 base::Bucket(/*min=*/1, /*count=*/3))); |
| 940 EXPECT_THAT( | 935 EXPECT_THAT( |
| 941 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 936 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 "http://mashable-amphtml.googleusercontent.com/1", | 970 "http://mashable-amphtml.googleusercontent.com/1", |
| 976 "http://t2.gstatic.com/images?q=tbn:3"}; | 971 "http://t2.gstatic.com/images?q=tbn:3"}; |
| 977 | 972 |
| 978 // Add the snippet from the mashable domain. | 973 // Add the snippet from the mashable domain. |
| 979 LoadFromJSONString(service.get(), | 974 LoadFromJSONString(service.get(), |
| 980 GetTestJson({GetSnippetWithUrlAndTimesAndSource( | 975 GetTestJson({GetSnippetWithUrlAndTimesAndSource( |
| 981 source_urls, source_urls[0], creation, expiry, | 976 source_urls, source_urls[0], creation, expiry, |
| 982 publishers[0], amp_urls[0])})); | 977 publishers[0], amp_urls[0])})); |
| 983 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 978 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 984 // Dismiss the snippet via the mashable source corpus ID. | 979 // Dismiss the snippet via the mashable source corpus ID. |
| 985 service->DismissSuggestion(MakeArticleID(*service, source_urls[0])); | 980 service->DismissSuggestion(MakeArticleID(source_urls[0])); |
| 986 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 981 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 987 | 982 |
| 988 // The same article from the AOL domain should now be detected as dismissed. | 983 // The same article from the AOL domain should now be detected as dismissed. |
| 989 LoadFromJSONString(service.get(), | 984 LoadFromJSONString(service.get(), |
| 990 GetTestJson({GetSnippetWithUrlAndTimesAndSource( | 985 GetTestJson({GetSnippetWithUrlAndTimesAndSource( |
| 991 source_urls, source_urls[1], creation, expiry, | 986 source_urls, source_urls[1], creation, expiry, |
| 992 publishers[1], amp_urls[1])})); | 987 publishers[1], amp_urls[1])})); |
| 993 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 988 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 994 } | 989 } |
| 995 | 990 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 gfx::Image image; | 1023 gfx::Image image; |
| 1029 MockFunction<void(const gfx::Image&)> image_fetched; | 1024 MockFunction<void(const gfx::Image&)> image_fetched; |
| 1030 { | 1025 { |
| 1031 InSequence s; | 1026 InSequence s; |
| 1032 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | 1027 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) |
| 1033 .WillOnce(WithArgs<0, 2>(Invoke(ServeOneByOneImage))); | 1028 .WillOnce(WithArgs<0, 2>(Invoke(ServeOneByOneImage))); |
| 1034 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | 1029 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); |
| 1035 } | 1030 } |
| 1036 | 1031 |
| 1037 service->FetchSuggestionImage( | 1032 service->FetchSuggestionImage( |
| 1038 MakeArticleID(*service, kSnippetUrl), | 1033 MakeArticleID(kSnippetUrl), |
| 1039 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | 1034 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, |
| 1040 base::Unretained(&image_fetched))); | 1035 base::Unretained(&image_fetched))); |
| 1041 base::RunLoop().RunUntilIdle(); | 1036 base::RunLoop().RunUntilIdle(); |
| 1042 // Check that the image by ServeOneByOneImage is really served. | 1037 // Check that the image by ServeOneByOneImage is really served. |
| 1043 EXPECT_EQ(1, image.Width()); | 1038 EXPECT_EQ(1, image.Width()); |
| 1044 } | 1039 } |
| 1045 | 1040 |
| 1046 TEST_F(NTPSnippetsServiceTest, EmptyImageReturnedForNonExistentId) { | 1041 TEST_F(NTPSnippetsServiceTest, EmptyImageReturnedForNonExistentId) { |
| 1047 auto service = MakeSnippetsService(); | 1042 auto service = MakeSnippetsService(); |
| 1048 | 1043 |
| 1049 // Create a non-empty image so that we can test the image gets updated. | 1044 // Create a non-empty image so that we can test the image gets updated. |
| 1050 gfx::Image image = gfx::test::CreateImage(1, 1); | 1045 gfx::Image image = gfx::test::CreateImage(1, 1); |
| 1051 MockFunction<void(const gfx::Image&)> image_fetched; | 1046 MockFunction<void(const gfx::Image&)> image_fetched; |
| 1052 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); | 1047 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); |
| 1053 | 1048 |
| 1054 service->FetchSuggestionImage( | 1049 service->FetchSuggestionImage( |
| 1055 MakeArticleID(*service, kSnippetUrl2), | 1050 MakeArticleID(kSnippetUrl2), |
| 1056 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | 1051 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, |
| 1057 base::Unretained(&image_fetched))); | 1052 base::Unretained(&image_fetched))); |
| 1058 | 1053 |
| 1059 base::RunLoop().RunUntilIdle(); | 1054 base::RunLoop().RunUntilIdle(); |
| 1060 EXPECT_TRUE(image.IsEmpty()); | 1055 EXPECT_TRUE(image.IsEmpty()); |
| 1061 } | 1056 } |
| 1062 | 1057 |
| 1063 TEST_F(NTPSnippetsServiceTest, ClearHistoryRemovesAllSuggestions) { | 1058 TEST_F(NTPSnippetsServiceTest, ClearHistoryRemovesAllSuggestions) { |
| 1064 auto service = MakeSnippetsService(); | 1059 auto service = MakeSnippetsService(); |
| 1065 | 1060 |
| 1066 std::string first_snippet = GetSnippetWithUrl("http://url1.com"); | 1061 std::string first_snippet = GetSnippetWithUrl("http://url1.com"); |
| 1067 std::string second_snippet = GetSnippetWithUrl("http://url2.com"); | 1062 std::string second_snippet = GetSnippetWithUrl("http://url2.com"); |
| 1068 std::string json_str = GetTestJson({first_snippet, second_snippet}); | 1063 std::string json_str = GetTestJson({first_snippet, second_snippet}); |
| 1069 LoadFromJSONString(service.get(), json_str); | 1064 LoadFromJSONString(service.get(), json_str); |
| 1070 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); | 1065 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); |
| 1071 | 1066 |
| 1072 service->DismissSuggestion(MakeArticleID(*service, "http://url1.com")); | 1067 service->DismissSuggestion(MakeArticleID("http://url1.com")); |
| 1073 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 1068 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 1074 ASSERT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 1069 ASSERT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 1075 SizeIs(1)); | 1070 SizeIs(1)); |
| 1076 | 1071 |
| 1077 base::Time begin = base::Time::FromTimeT(123), | 1072 base::Time begin = base::Time::FromTimeT(123), |
| 1078 end = base::Time::FromTimeT(456); | 1073 end = base::Time::FromTimeT(456); |
| 1079 base::Callback<bool(const GURL& url)> filter; | 1074 base::Callback<bool(const GURL& url)> filter; |
| 1080 service->ClearHistory(begin, end, filter); | 1075 service->ClearHistory(begin, end, filter); |
| 1081 | 1076 |
| 1082 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1077 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 1083 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 1078 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 1084 IsEmpty()); | 1079 IsEmpty()); |
| 1085 } | 1080 } |
| 1086 | 1081 |
| 1087 } // namespace ntp_snippets | 1082 } // namespace ntp_snippets |
| OLD | NEW |