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/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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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"; | 93 const char kTestJsonDefaultCategoryTitle[] = "Some title"; |
94 | 94 |
| 95 const int kUnknownRemoteCategoryId = 1234; |
| 96 |
95 base::Time GetDefaultCreationTime() { | 97 base::Time GetDefaultCreationTime() { |
96 base::Time out_time; | 98 base::Time out_time; |
97 EXPECT_TRUE(base::Time::FromUTCExploded(kDefaultCreationTime, &out_time)); | 99 EXPECT_TRUE(base::Time::FromUTCExploded(kDefaultCreationTime, &out_time)); |
98 return out_time; | 100 return out_time; |
99 } | 101 } |
100 | 102 |
101 base::Time GetDefaultExpirationTime() { | 103 base::Time GetDefaultExpirationTime() { |
102 return base::Time::Now() + base::TimeDelta::FromHours(1); | 104 return base::Time::Now() + base::TimeDelta::FromHours(1); |
103 } | 105 } |
104 | 106 |
(...skipping 13 matching lines...) Expand all Loading... |
118 | 120 |
119 std::string GetTestJson(const std::vector<std::string>& snippets) { | 121 std::string GetTestJson(const std::vector<std::string>& snippets) { |
120 return GetTestJson(snippets, kTestJsonDefaultCategoryTitle); | 122 return GetTestJson(snippets, kTestJsonDefaultCategoryTitle); |
121 } | 123 } |
122 | 124 |
123 std::string GetTestJsonWithoutTitle(const std::vector<std::string>& snippets) { | 125 std::string GetTestJsonWithoutTitle(const std::vector<std::string>& snippets) { |
124 return GetTestJson(snippets, std::string()); | 126 return GetTestJson(snippets, std::string()); |
125 } | 127 } |
126 | 128 |
127 std::string GetMultiCategoryJson(const std::vector<std::string>& articles, | 129 std::string GetMultiCategoryJson(const std::vector<std::string>& articles, |
128 const std::vector<std::string>& others) { | 130 const std::vector<std::string>& others, |
| 131 int other_id = 2) { |
129 return base::StringPrintf( | 132 return base::StringPrintf( |
130 "{\n" | 133 "{\n" |
131 " \"categories\": [{\n" | 134 " \"categories\": [{\n" |
132 " \"id\": 1,\n" | 135 " \"id\": 1,\n" |
133 " \"localizedTitle\": \"Articles for You\",\n" | 136 " \"localizedTitle\": \"Articles for You\",\n" |
134 " \"suggestions\": [%s]\n" | 137 " \"suggestions\": [%s]\n" |
135 " }, {\n" | 138 " }, {\n" |
136 " \"id\": 2,\n" | 139 " \"id\": %i,\n" |
137 " \"localizedTitle\": \"Other Things\",\n" | 140 " \"localizedTitle\": \"Other Things\",\n" |
138 " \"suggestions\": [%s]\n" | 141 " \"suggestions\": [%s]\n" |
139 " }]\n" | 142 " }]\n" |
140 "}\n", | 143 "}\n", |
141 base::JoinString(articles, ", ").c_str(), | 144 base::JoinString(articles, ", ").c_str(), other_id, |
142 base::JoinString(others, ", ").c_str()); | 145 base::JoinString(others, ", ").c_str()); |
143 } | 146 } |
144 | 147 |
145 std::string FormatTime(const base::Time& t) { | 148 std::string FormatTime(const base::Time& t) { |
146 base::Time::Exploded x; | 149 base::Time::Exploded x; |
147 t.UTCExplode(&x); | 150 t.UTCExplode(&x); |
148 return base::StringPrintf("%04d-%02d-%02dT%02d:%02d:%02dZ", x.year, x.month, | 151 return base::StringPrintf("%04d-%02d-%02dT%02d:%02d:%02dZ", x.year, x.month, |
149 x.day_of_month, x.hour, x.minute, x.second); | 152 x.day_of_month, x.hour, x.minute, x.second); |
150 } | 153 } |
151 | 154 |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 Category articles_category() { | 471 Category articles_category() { |
469 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); | 472 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); |
470 } | 473 } |
471 | 474 |
472 ContentSuggestion::ID MakeOtherID(const std::string& id_within_category) { | 475 ContentSuggestion::ID MakeOtherID(const std::string& id_within_category) { |
473 return ContentSuggestion::ID(other_category(), id_within_category); | 476 return ContentSuggestion::ID(other_category(), id_within_category); |
474 } | 477 } |
475 | 478 |
476 Category other_category() { return category_factory_.FromRemoteCategory(2); } | 479 Category other_category() { return category_factory_.FromRemoteCategory(2); } |
477 | 480 |
| 481 Category unknown_category() { |
| 482 return category_factory_.FromRemoteCategory(kUnknownRemoteCategoryId); |
| 483 } |
| 484 |
478 protected: | 485 protected: |
479 const GURL& test_url() { return test_url_; } | 486 const GURL& test_url() { return test_url_; } |
480 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } | 487 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } |
481 MockScheduler& mock_scheduler() { return scheduler_; } | 488 MockScheduler& mock_scheduler() { return scheduler_; } |
482 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } | 489 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } |
483 FakeImageDecoder* image_decoder() { return image_decoder_; } | 490 FakeImageDecoder* image_decoder() { return image_decoder_; } |
484 | 491 |
485 // Provide the json to be returned by the fake fetcher. | 492 // Provide the json to be returned by the fake fetcher. |
486 void SetUpFetchResponse(const std::string& json) { | 493 void SetUpFetchResponse(const std::string& json) { |
487 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 494 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 EXPECT_EQ(MakeOtherID(std::string(kSnippetUrl) + "/1"), suggestion.id()); | 738 EXPECT_EQ(MakeOtherID(std::string(kSnippetUrl) + "/1"), suggestion.id()); |
732 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); | 739 EXPECT_EQ(kSnippetTitle, base::UTF16ToUTF8(suggestion.title())); |
733 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); | 740 EXPECT_EQ(kSnippetText, base::UTF16ToUTF8(suggestion.snippet_text())); |
734 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); | 741 EXPECT_EQ(GetDefaultCreationTime(), suggestion.publish_date()); |
735 EXPECT_EQ(kSnippetPublisherName, | 742 EXPECT_EQ(kSnippetPublisherName, |
736 base::UTF16ToUTF8(suggestion.publisher_name())); | 743 base::UTF16ToUTF8(suggestion.publisher_name())); |
737 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); | 744 EXPECT_EQ(GURL(kSnippetAmpUrl), suggestion.amp_url()); |
738 } | 745 } |
739 } | 746 } |
740 | 747 |
| 748 TEST_F(NTPSnippetsServiceTest, PersistCategoryInfos) { |
| 749 auto service = MakeSnippetsService(); |
| 750 |
| 751 LoadFromJSONString(service.get(), |
| 752 GetMultiCategoryJson({GetSnippetN(0)}, {GetSnippetN(1)}, |
| 753 kUnknownRemoteCategoryId)); |
| 754 |
| 755 ASSERT_NE(observer().StatusForCategory(articles_category()), |
| 756 CategoryStatus::NOT_PROVIDED); |
| 757 ASSERT_NE(observer().StatusForCategory(unknown_category()), |
| 758 CategoryStatus::NOT_PROVIDED); |
| 759 |
| 760 CategoryInfo info_articles_before = |
| 761 service->GetCategoryInfo(articles_category()); |
| 762 CategoryInfo info_unknown_before = |
| 763 service->GetCategoryInfo(unknown_category()); |
| 764 |
| 765 // Recreate the service to simulate a Chrome restart. |
| 766 ResetSnippetsService(&service); |
| 767 |
| 768 // The categories should have been restored. |
| 769 ASSERT_NE(observer().StatusForCategory(articles_category()), |
| 770 CategoryStatus::NOT_PROVIDED); |
| 771 ASSERT_NE(observer().StatusForCategory(unknown_category()), |
| 772 CategoryStatus::NOT_PROVIDED); |
| 773 |
| 774 CategoryInfo info_articles_after = |
| 775 service->GetCategoryInfo(articles_category()); |
| 776 CategoryInfo info_unknown_after = |
| 777 service->GetCategoryInfo(unknown_category()); |
| 778 |
| 779 EXPECT_EQ(info_articles_before.title(), info_articles_after.title()); |
| 780 EXPECT_EQ(info_unknown_before.title(), info_unknown_after.title()); |
| 781 } |
| 782 |
741 TEST_F(NTPSnippetsServiceTest, Clear) { | 783 TEST_F(NTPSnippetsServiceTest, Clear) { |
742 auto service = MakeSnippetsService(); | 784 auto service = MakeSnippetsService(); |
743 | 785 |
744 std::string json_str(GetTestJson({GetSnippet()})); | 786 std::string json_str(GetTestJson({GetSnippet()})); |
745 | 787 |
746 LoadFromJSONString(service.get(), json_str); | 788 LoadFromJSONString(service.get(), json_str); |
747 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 789 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
748 | 790 |
749 service->ClearCachedSuggestions(articles_category()); | 791 service->ClearCachedSuggestions(articles_category()); |
750 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 792 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 base::StringPrintf("http://localhost/snippet-id-%d", i))); | 1272 base::StringPrintf("http://localhost/snippet-id-%d", i))); |
1231 } | 1273 } |
1232 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | 1274 LoadFromJSONString(service.get(), GetTestJson(suggestions)); |
1233 // TODO(tschumann): We should probably trim out any additional results and | 1275 // TODO(tschumann): We should probably trim out any additional results and |
1234 // only serve the MaxSnippetCount items. | 1276 // only serve the MaxSnippetCount items. |
1235 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1277 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
1236 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | 1278 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); |
1237 } | 1279 } |
1238 | 1280 |
1239 } // namespace ntp_snippets | 1281 } // namespace ntp_snippets |
OLD | NEW |