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

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

Issue 2393353005: [NTP Snippets] Overwrite the title of the ARTICLES if provided by the server (Closed)
Patch Set: 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/remote/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/remote/ntp_snippets_service.h" 5 #include "components/ntp_snippets/remote/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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 83
84 const char kSnippetUrl[] = "http://localhost/foobar"; 84 const char kSnippetUrl[] = "http://localhost/foobar";
85 const char kSnippetTitle[] = "Title"; 85 const char kSnippetTitle[] = "Title";
86 const char kSnippetText[] = "Snippet"; 86 const char kSnippetText[] = "Snippet";
87 const char kSnippetSalientImage[] = "http://localhost/salient_image"; 87 const char kSnippetSalientImage[] = "http://localhost/salient_image";
88 const char kSnippetPublisherName[] = "Foo News"; 88 const char kSnippetPublisherName[] = "Foo News";
89 const char kSnippetAmpUrl[] = "http://localhost/amp"; 89 const char kSnippetAmpUrl[] = "http://localhost/amp";
90 90
91 const char kSnippetUrl2[] = "http://foo.com/bar"; 91 const char kSnippetUrl2[] = "http://foo.com/bar";
92 92
93 const char kTestJsonDefaultCategoryTitle[] = "Some title";
94
93 base::Time GetDefaultCreationTime() { 95 base::Time GetDefaultCreationTime() {
94 base::Time out_time; 96 base::Time out_time;
95 EXPECT_TRUE(base::Time::FromUTCExploded(kDefaultCreationTime, &out_time)); 97 EXPECT_TRUE(base::Time::FromUTCExploded(kDefaultCreationTime, &out_time));
96 return out_time; 98 return out_time;
97 } 99 }
98 100
99 base::Time GetDefaultExpirationTime() { 101 base::Time GetDefaultExpirationTime() {
100 return base::Time::Now() + base::TimeDelta::FromHours(1); 102 return base::Time::Now() + base::TimeDelta::FromHours(1);
101 } 103 }
102 104
103 std::string GetTestJson(const std::vector<std::string>& snippets) { 105 std::string GetTestJson(const std::vector<std::string>& snippets,
106 const std::string& category_title) {
104 return base::StringPrintf( 107 return base::StringPrintf(
105 "{\n" 108 "{\n"
106 " \"categories\": [{\n" 109 " \"categories\": [{\n"
107 " \"id\": 1,\n" 110 " \"id\": 1,\n"
108 " \"localizedTitle\": \"Articles for You\",\n" 111 " \"localizedTitle\": \"%s\",\n"
109 " \"suggestions\": [%s]\n" 112 " \"suggestions\": [%s]\n"
110 " }]\n" 113 " }]\n"
111 "}\n", 114 "}\n",
115 category_title.c_str(),
112 base::JoinString(snippets, ", ").c_str()); 116 base::JoinString(snippets, ", ").c_str());
113 } 117 }
114 118
119 std::string GetTestJson(const std::vector<std::string>& snippets) {
120 return GetTestJson(snippets, kTestJsonDefaultCategoryTitle);
121 }
122
123 std::string GetTestJsonWithoutTitle(const std::vector<std::string>& snippets) {
124 return GetTestJson(snippets, std::string());
125 }
126
115 std::string GetMultiCategoryJson(const std::vector<std::string>& articles, 127 std::string GetMultiCategoryJson(const std::vector<std::string>& articles,
116 const std::vector<std::string>& others) { 128 const std::vector<std::string>& others) {
117 return base::StringPrintf( 129 return base::StringPrintf(
118 "{\n" 130 "{\n"
119 " \"categories\": [{\n" 131 " \"categories\": [{\n"
120 " \"id\": 1,\n" 132 " \"id\": 1,\n"
121 " \"localizedTitle\": \"Articles for You\",\n" 133 " \"localizedTitle\": \"Articles for You\",\n"
122 " \"suggestions\": [%s]\n" 134 " \"suggestions\": [%s]\n"
123 " }, {\n" 135 " }, {\n"
124 " \"id\": 2,\n" 136 " \"id\": 2,\n"
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 utils_.pref_service())); 442 utils_.pref_service()));
431 } 443 }
432 444
433 void WaitForSnippetsServiceInitialization(bool set_empty_response = true) { 445 void WaitForSnippetsServiceInitialization(bool set_empty_response = true) {
434 EXPECT_TRUE(observer_); 446 EXPECT_TRUE(observer_);
435 EXPECT_FALSE(observer_->Loaded()); 447 EXPECT_FALSE(observer_->Loaded());
436 448
437 // Add an initial fetch response, as the service tries to fetch when there 449 // Add an initial fetch response, as the service tries to fetch when there
438 // is nothing in the DB. 450 // is nothing in the DB.
439 if (set_empty_response) 451 if (set_empty_response)
440 SetUpFetchResponse(GetTestJson(std::vector<std::string>())); 452 SetUpFetchResponse(GetTestJsonWithoutTitle(std::vector<std::string>()));
441 453
442 base::RunLoop().RunUntilIdle(); 454 base::RunLoop().RunUntilIdle();
443 observer_->WaitForLoad(); 455 observer_->WaitForLoad();
444 } 456 }
445 457
446 void ResetSnippetsService(std::unique_ptr<NTPSnippetsService>* service) { 458 void ResetSnippetsService(std::unique_ptr<NTPSnippetsService>* service) {
447 service->reset(); 459 service->reset();
448 observer_.reset(); 460 observer_.reset();
449 *service = MakeSnippetsService(); 461 *service = MakeSnippetsService();
450 } 462 }
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 647
636 EXPECT_EQ(MakeArticleID(kSnippetUrl), suggestion.id()); 648 EXPECT_EQ(MakeArticleID(kSnippetUrl), suggestion.id());
637 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); 649 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title()));
638 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); 650 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text()));
639 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); 651 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date());
640 EXPECT_EQ(kSnippetPublisherName, 652 EXPECT_EQ(kSnippetPublisherName,
641 base::UTF16ToUTF8(suggestion.publisher_name())); 653 base::UTF16ToUTF8(suggestion.publisher_name()));
642 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); 654 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url());
643 } 655 }
644 656
657 TEST_F(NTPSnippetsServiceTest, CategoryTitle) {
658 const base::string16 response_title =
659 base::UTF8ToUTF16(kTestJsonDefaultCategoryTitle);
660
661 auto service = MakeSnippetsService();
662
663 // The articles category should be there by default, and have a title.
664 CategoryInfo info_before = service->GetCategoryInfo(articles_category());
665 ASSERT_FALSE(info_before.title().empty());
666 ASSERT_NE(info_before.title(), response_title);
667
668 std::string json_str_no_title(GetTestJsonWithoutTitle({GetSnippet()}));
669 LoadFromJSONString(service.get(), json_str_no_title);
670
671 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()),
672 SizeIs(1));
673 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
674
675 // The response didn't contain a category title. Make sure we didn't touch
676 // the existing one.
677 CategoryInfo info_no_title = service->GetCategoryInfo(articles_category());
678 EXPECT_EQ(info_before.title(), info_no_title.title());
679
680 std::string json_str_with_title(GetTestJson({GetSnippet()}));
681 LoadFromJSONString(service.get(), json_str_with_title);
682
683 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()),
684 SizeIs(1));
685 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
686
687 // This time, the response contained a title, |kTestJsonDefaultCategoryTitle|.
688 // Make sure we updated the title in the CategoryInfo.
689 CategoryInfo info_with_title = service->GetCategoryInfo(articles_category());
690 EXPECT_NE(info_before.title(), info_with_title.title());
691 EXPECT_EQ(response_title, info_with_title.title());
692 }
693
645 TEST_F(NTPSnippetsServiceTest, MultipleCategories) { 694 TEST_F(NTPSnippetsServiceTest, MultipleCategories) {
646 std::string json_str( 695 std::string json_str(
647 GetMultiCategoryJson({GetSnippetN(0)}, {GetSnippetN(1)})); 696 GetMultiCategoryJson({GetSnippetN(0)}, {GetSnippetN(1)}));
648 697
649 auto service = MakeSnippetsService(); 698 auto service = MakeSnippetsService();
650 699
651 LoadFromJSONString(service.get(), json_str); 700 LoadFromJSONString(service.get(), json_str);
652 701
653 ASSERT_THAT(observer().statuses(), 702 ASSERT_THAT(observer().statuses(),
654 Eq(std::map<Category, CategoryStatus, Category::CompareByID>{ 703 Eq(std::map<Category, CategoryStatus, Category::CompareByID>{
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 base::StringPrintf("http://localhost/snippet-id-%d", i))); 1230 base::StringPrintf("http://localhost/snippet-id-%d", i)));
1182 } 1231 }
1183 LoadFromJSONString(service.get(), GetTestJson(suggestions)); 1232 LoadFromJSONString(service.get(), GetTestJson(suggestions));
1184 // TODO(tschumann): We should probably trim out any additional results and 1233 // TODO(tschumann): We should probably trim out any additional results and
1185 // only serve the MaxSnippetCount items. 1234 // only serve the MaxSnippetCount items.
1186 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), 1235 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()),
1187 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); 1236 SizeIs(service->GetMaxSnippetCountForTesting() + 1));
1188 } 1237 }
1189 1238
1190 } // namespace ntp_snippets 1239 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/ntp_snippets_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698