Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(795)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 2167063003: Rename discard to dismiss for NTP snippets and content suggestions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bridgeumbiegen
Patch Set: Fix unit test Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 LoadFromJSONString(GetTestJson({GetSnippet()})); 484 LoadFromJSONString(GetTestJson({GetSnippet()}));
485 ASSERT_THAT(service()->snippets(), SizeIs(1)); 485 ASSERT_THAT(service()->snippets(), SizeIs(1));
486 486
487 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); 487 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()}));
488 EXPECT_EQ("Invalid / empty list.", 488 EXPECT_EQ("Invalid / empty list.",
489 service()->snippets_fetcher()->last_status()); 489 service()->snippets_fetcher()->last_status());
490 // This should not have changed the existing snippets. 490 // This should not have changed the existing snippets.
491 EXPECT_THAT(service()->snippets(), SizeIs(1)); 491 EXPECT_THAT(service()->snippets(), SizeIs(1));
492 } 492 }
493 493
494 TEST_F(NTPSnippetsServiceTest, Discard) { 494 TEST_F(NTPSnippetsServiceTest, Dismiss) {
495 std::vector<std::string> source_urls, publishers, amp_urls; 495 std::vector<std::string> source_urls, publishers, amp_urls;
496 source_urls.push_back(std::string("http://site.com")); 496 source_urls.push_back(std::string("http://site.com"));
497 publishers.push_back(std::string("Source 1")); 497 publishers.push_back(std::string("Source 1"));
498 amp_urls.push_back(std::string()); 498 amp_urls.push_back(std::string());
499 std::string json_str( 499 std::string json_str(
500 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); 500 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
501 501
502 LoadFromJSONString(json_str); 502 LoadFromJSONString(json_str);
503 503
504 ASSERT_THAT(service()->snippets(), SizeIs(1)); 504 ASSERT_THAT(service()->snippets(), SizeIs(1));
505 505
506 // Discarding a non-existent snippet shouldn't do anything. 506 // Dismissing a non-existent snippet shouldn't do anything.
507 service()->DiscardSuggestion(MakeUniqueID("http://othersite.com")); 507 service()->DismissSuggestion(MakeUniqueID("http://othersite.com"));
508 EXPECT_THAT(service()->snippets(), SizeIs(1)); 508 EXPECT_THAT(service()->snippets(), SizeIs(1));
509 509
510 // Discard the snippet. 510 // Dismiss the snippet.
511 service()->DiscardSuggestion(MakeUniqueID(kSnippetUrl)); 511 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl));
512 EXPECT_THAT(service()->snippets(), IsEmpty()); 512 EXPECT_THAT(service()->snippets(), IsEmpty());
513 513
514 // Make sure that fetching the same snippet again does not re-add it. 514 // Make sure that fetching the same snippet again does not re-add it.
515 LoadFromJSONString(json_str); 515 LoadFromJSONString(json_str);
516 EXPECT_THAT(service()->snippets(), IsEmpty()); 516 EXPECT_THAT(service()->snippets(), IsEmpty());
517 517
518 // The snippet should stay discarded even after re-creating the service. 518 // The snippet should stay dismissed even after re-creating the service.
519 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 519 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
520 CreateSnippetsService(/*enabled=*/true); 520 CreateSnippetsService(/*enabled=*/true);
521 LoadFromJSONString(json_str); 521 LoadFromJSONString(json_str);
522 EXPECT_THAT(service()->snippets(), IsEmpty()); 522 EXPECT_THAT(service()->snippets(), IsEmpty());
523 523
524 // The snippet can be added again after clearing discarded snippets. 524 // The snippet can be added again after clearing dismissed snippets.
525 service()->ClearDiscardedSuggestionsForDebugging(); 525 service()->ClearDismissedSuggestionsForDebugging();
526 EXPECT_THAT(service()->snippets(), IsEmpty()); 526 EXPECT_THAT(service()->snippets(), IsEmpty());
527 LoadFromJSONString(json_str); 527 LoadFromJSONString(json_str);
528 EXPECT_THAT(service()->snippets(), SizeIs(1)); 528 EXPECT_THAT(service()->snippets(), SizeIs(1));
529 } 529 }
530 530
531 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { 531 TEST_F(NTPSnippetsServiceTest, GetDismissed) {
532 LoadFromJSONString(GetTestJson({GetSnippet()})); 532 LoadFromJSONString(GetTestJson({GetSnippet()}));
533 533
534 service()->DiscardSuggestion(MakeUniqueID(kSnippetUrl)); 534 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl));
535 const NTPSnippet::PtrVector& snippets = service()->discarded_snippets(); 535 const NTPSnippet::PtrVector& snippets = service()->dismissed_snippets();
536 EXPECT_EQ(1u, snippets.size()); 536 EXPECT_EQ(1u, snippets.size());
537 for (auto& snippet : snippets) { 537 for (auto& snippet : snippets) {
538 EXPECT_EQ(kSnippetUrl, snippet->id()); 538 EXPECT_EQ(kSnippetUrl, snippet->id());
539 } 539 }
540 540
541 // There should be no discarded snippet after clearing the list. 541 // There should be no dismissed snippet after clearing the list.
542 service()->ClearDiscardedSuggestionsForDebugging(); 542 service()->ClearDismissedSuggestionsForDebugging();
543 EXPECT_EQ(0u, service()->discarded_snippets().size()); 543 EXPECT_EQ(0u, service()->dismissed_snippets().size());
544 } 544 }
545 545
546 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { 546 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) {
547 std::string json_str(GetTestJson({GetSnippetWithTimes( 547 std::string json_str(GetTestJson({GetSnippetWithTimes(
548 "aaa1448459205", 548 "aaa1448459205",
549 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); 549 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))}));
550 550
551 LoadFromJSONString(json_str); 551 LoadFromJSONString(json_str);
552 ASSERT_THAT(service()->snippets(), SizeIs(1)); 552 ASSERT_THAT(service()->snippets(), SizeIs(1));
553 const NTPSnippet& snippet = *service()->snippets().front(); 553 const NTPSnippet& snippet = *service()->snippets().front();
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 LoadFromJSONString(GetTestJson({GetSnippet()})); 808 LoadFromJSONString(GetTestJson({GetSnippet()}));
809 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), 809 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"),
810 ElementsAre(base::Bucket(/*min=*/0, /*count=*/2), 810 ElementsAre(base::Bucket(/*min=*/0, /*count=*/2),
811 base::Bucket(/*min=*/1, /*count=*/2))); 811 base::Bucket(/*min=*/1, /*count=*/2)));
812 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), 812 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"),
813 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), 813 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
814 base::Bucket(/*min=*/1, /*count=*/2))); 814 base::Bucket(/*min=*/1, /*count=*/2)));
815 EXPECT_THAT( 815 EXPECT_THAT(
816 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), 816 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"),
817 IsEmpty()); 817 IsEmpty());
818 // Discarding a snippet should decrease the list size. This will only be 818 // Dismissing a snippet should decrease the list size. This will only be
819 // logged after the next fetch. 819 // logged after the next fetch.
820 service()->DiscardSuggestion(MakeUniqueID(kSnippetUrl)); 820 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl));
821 LoadFromJSONString(GetTestJson({GetSnippet()})); 821 LoadFromJSONString(GetTestJson({GetSnippet()}));
822 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), 822 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"),
823 ElementsAre(base::Bucket(/*min=*/0, /*count=*/3), 823 ElementsAre(base::Bucket(/*min=*/0, /*count=*/3),
824 base::Bucket(/*min=*/1, /*count=*/2))); 824 base::Bucket(/*min=*/1, /*count=*/2)));
825 // Discarded snippets shouldn't influence NumArticlesFetched. 825 // Dismissed snippets shouldn't influence NumArticlesFetched.
826 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), 826 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"),
827 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), 827 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
828 base::Bucket(/*min=*/1, /*count=*/3))); 828 base::Bucket(/*min=*/1, /*count=*/3)));
829 EXPECT_THAT( 829 EXPECT_THAT(
830 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), 830 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"),
831 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 831 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
832 // Recreating the service and loading from prefs shouldn't count as fetched 832 // Recreating the service and loading from prefs shouldn't count as fetched
833 // articles. 833 // articles.
834 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 834 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
835 CreateSnippetsService(/*enabled=*/true); 835 CreateSnippetsService(/*enabled=*/true);
836 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); 836 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4);
837 } 837 }
838 838
839 TEST_F(NTPSnippetsServiceTest, DiscardShouldRespectAllKnownUrls) { 839 TEST_F(NTPSnippetsServiceTest, DismissShouldRespectAllKnownUrls) {
840 const std::string creation = 840 const std::string creation =
841 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()); 841 NTPSnippet::TimeToJsonString(GetDefaultCreationTime());
842 const std::string expiry = 842 const std::string expiry =
843 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()); 843 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime());
844 const std::vector<std::string> source_urls = { 844 const std::vector<std::string> source_urls = {
845 "http://mashable.com/2016/05/11/stolen", 845 "http://mashable.com/2016/05/11/stolen",
846 "http://www.aol.com/article/2016/05/stolen-doggie", 846 "http://www.aol.com/article/2016/05/stolen-doggie",
847 "http://mashable.com/2016/05/11/stolen?utm_cid=1"}; 847 "http://mashable.com/2016/05/11/stolen?utm_cid=1"};
848 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"}; 848 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"};
849 const std::vector<std::string> amp_urls = { 849 const std::vector<std::string> amp_urls = {
850 "http://mashable-amphtml.googleusercontent.com/1", 850 "http://mashable-amphtml.googleusercontent.com/1",
851 "http://t2.gstatic.com/images?q=tbn:3", 851 "http://t2.gstatic.com/images?q=tbn:3",
852 "http://t2.gstatic.com/images?q=tbn:3"}; 852 "http://t2.gstatic.com/images?q=tbn:3"};
853 853
854 // Add the snippet from the mashable domain. 854 // Add the snippet from the mashable domain.
855 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( 855 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources(
856 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); 856 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)}));
857 ASSERT_THAT(service()->snippets(), SizeIs(1)); 857 ASSERT_THAT(service()->snippets(), SizeIs(1));
858 // Discard the snippet via the mashable source corpus ID. 858 // Dismiss the snippet via the mashable source corpus ID.
859 service()->DiscardSuggestion(MakeUniqueID(source_urls[0])); 859 service()->DismissSuggestion(MakeUniqueID(source_urls[0]));
860 EXPECT_THAT(service()->snippets(), IsEmpty()); 860 EXPECT_THAT(service()->snippets(), IsEmpty());
861 861
862 // The same article from the AOL domain should now be detected as discarded. 862 // The same article from the AOL domain should now be detected as dismissed.
863 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( 863 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources(
864 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); 864 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)}));
865 ASSERT_THAT(service()->snippets(), IsEmpty()); 865 ASSERT_THAT(service()->snippets(), IsEmpty());
866 } 866 }
867 867
868 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) { 868 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) {
869 MockServiceObserver mock_observer; 869 MockServiceObserver mock_observer;
870 service()->AddObserver(&mock_observer); 870 service()->AddObserver(&mock_observer);
871 871
872 // Simulate user signed out 872 // Simulate user signed out
(...skipping 12 matching lines...) Expand all
885 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 885 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
886 service()->OnDisabledReasonChanged(DisabledReason::NONE); 886 service()->OnDisabledReasonChanged(DisabledReason::NONE);
887 base::RunLoop().RunUntilIdle(); 887 base::RunLoop().RunUntilIdle();
888 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); 888 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_);
889 EXPECT_FALSE(service()->snippets().empty()); 889 EXPECT_FALSE(service()->snippets().empty());
890 890
891 service()->RemoveObserver(&mock_observer); 891 service()->RemoveObserver(&mock_observer);
892 } 892 }
893 893
894 } // namespace ntp_snippets 894 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698