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

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

Issue 2355393002: New snippets now replace old snippets and do not merge (Closed)
Patch Set: Marc's comments + further changes to make unittests happy Created 4 years, 2 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
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 633
634 std::string json_str(GetTestJson({GetSnippet()})); 634 std::string json_str(GetTestJson({GetSnippet()}));
635 635
636 LoadFromJSONString(service.get(), json_str); 636 LoadFromJSONString(service.get(), json_str);
637 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); 637 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
638 638
639 service->ClearCachedSuggestions(articles_category()); 639 service->ClearCachedSuggestions(articles_category());
640 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); 640 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty());
641 } 641 }
642 642
643 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { 643 TEST_F(NTPSnippetsServiceTest, ReplaceSnippets) {
644 auto service = MakeSnippetsService(); 644 auto service = MakeSnippetsService();
645 645
646 std::string first("http://first"); 646 std::string first("http://first");
647 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); 647 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)}));
648 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), 648 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()),
649 ElementsAre(IdEq(first))); 649 ElementsAre(IdEq(first)));
650 650
651 std::string second("http://second"); 651 std::string second("http://second");
652 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); 652 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)}));
653 // The snippet loaded last should be at the first position in the list now. 653 // The snippet loaded last should be at the first position in the list now.
Marc Treib 2016/09/23 16:36:06 nit: This comment could be updated
jkrcal 2016/09/23 17:34:32 Oh, thanks, I missed it!
654 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), 654 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()),
655 ElementsAre(IdEq(second), IdEq(first))); 655 ElementsAre(IdEq(second)));
656 }
657
658 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) {
659 auto service = MakeSnippetsService();
660
661 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting();
662 int snippets_per_load = max_snippet_count / 2 + 1;
663 char url_format[] = "http://localhost/%i";
664
665 std::vector<std::string> snippets1;
666 std::vector<std::string> snippets2;
667 for (int i = 0; i < snippets_per_load; i++) {
668 snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i)));
669 snippets2.push_back(GetSnippetWithUrl(
670 base::StringPrintf(url_format, snippets_per_load + i)));
671 }
672
673 LoadFromJSONString(service.get(), GetTestJson(snippets1));
674 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()),
675 SizeIs(snippets1.size()));
676
677 LoadFromJSONString(service.get(), GetTestJson(snippets2));
678 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()),
679 SizeIs(max_snippet_count));
680 } 656 }
681 657
682 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { 658 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
683 auto service = MakeSnippetsService(); 659 auto service = MakeSnippetsService();
684 660
685 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); 661 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()}));
686 EXPECT_THAT(service->snippets_fetcher()->last_status(), 662 EXPECT_THAT(service->snippets_fetcher()->last_status(),
687 StartsWith("Received invalid JSON")); 663 StartsWith("Received invalid JSON"));
688 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); 664 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty());
689 } 665 }
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 std::string json = 774 std::string json =
799 GetSnippetWithTimes(GetDefaultCreationTime(), GetDefaultExpirationTime()); 775 GetSnippetWithTimes(GetDefaultCreationTime(), GetDefaultExpirationTime());
800 base::ReplaceFirstSubstringAfterOffset( 776 base::ReplaceFirstSubstringAfterOffset(
801 &json, 0, FormatTime(GetDefaultCreationTime()), "aaa1448459205"); 777 &json, 0, FormatTime(GetDefaultCreationTime()), "aaa1448459205");
802 std::string json_str(GetTestJson({json})); 778 std::string json_str(GetTestJson({json}));
803 779
804 LoadFromJSONString(service.get(), json_str); 780 LoadFromJSONString(service.get(), json_str);
805 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); 781 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty());
806 } 782 }
807 783
808 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { 784 TEST_F(NTPSnippetsServiceTest, RemoveExpiredDismissedContent) {
785 auto service = MakeSnippetsService();
786
787 std::string json_str1(GetTestJson({GetExpiredSnippet()}));
788 // Load it.
789 LoadFromJSONString(service.get(), json_str1);
790 // Dismiss the suggestion
791 service->DismissSuggestion(
792 service->MakeUniqueID(service->articles_category_, kSnippetUrl));
793
794 // Load a different snippet - this will clear the expired dismissed ones.
Marc Treib 2016/09/23 16:36:06 This seems to test an implementation details that
jkrcal 2016/09/23 17:34:32 I agree it is an implementation detail but this wa
795 std::string json_str2(GetTestJson({GetSnippetWithUrl(kSnippetUrl2)}));
796 LoadFromJSONString(service.get(), json_str2);
797
798 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()),
799 IsEmpty());
800 }
801
802 TEST_F(NTPSnippetsServiceTest, ExpiredContentNotRemoved) {
809 auto service = MakeSnippetsService(); 803 auto service = MakeSnippetsService();
810 804
811 std::string json_str(GetTestJson({GetExpiredSnippet()})); 805 std::string json_str(GetTestJson({GetExpiredSnippet()}));
812 806
813 LoadFromJSONString(service.get(), json_str); 807 LoadFromJSONString(service.get(), json_str);
814 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); 808 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
815 } 809 }
816 810
817 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { 811 TEST_F(NTPSnippetsServiceTest, TestSingleSource) {
818 auto service = MakeSnippetsService(); 812 auto service = MakeSnippetsService();
819 813
820 std::string json_str(GetTestJson({GetSnippetWithSources( 814 std::string json_str(GetTestJson({GetSnippetWithSources(
821 "http://source1.com", "Source 1", "http://source1.amp.com")})); 815 "http://source1.com", "Source 1", "http://source1.amp.com")}));
822 816
823 LoadFromJSONString(service.get(), json_str); 817 LoadFromJSONString(service.get(), json_str);
824 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); 818 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
1044 end = base::Time::FromTimeT(456); 1038 end = base::Time::FromTimeT(456);
1045 base::Callback<bool(const GURL& url)> filter; 1039 base::Callback<bool(const GURL& url)> filter;
1046 service->ClearHistory(begin, end, filter); 1040 service->ClearHistory(begin, end, filter);
1047 1041
1048 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); 1042 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty());
1049 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), 1043 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()),
1050 IsEmpty()); 1044 IsEmpty());
1051 } 1045 }
1052 1046
1053 } // namespace ntp_snippets 1047 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698