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

Unified Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 1985973003: Adding score field into jsons in unittests for ntp_snippets_service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: A minor fix Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/ntp_snippets_service_unittest.cc
diff --git a/components/ntp_snippets/ntp_snippets_service_unittest.cc b/components/ntp_snippets/ntp_snippets_service_unittest.cc
index dbaaed7c62e61ef409cb1adba9e8a3b3b67f7356..29e386ef81fcf3d43a7d5a8e5e1f1e3ab8124bc4 100644
--- a/components/ntp_snippets/ntp_snippets_service_unittest.cc
+++ b/components/ntp_snippets/ntp_snippets_service_unittest.cc
@@ -53,49 +53,45 @@ const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45};
const char kTestContentSnippetsServerFormat[] =
"https://chromereader-pa.googleapis.com/v1/fetch?key=%s";
+const char kSnippetUrl[] = "http://localhost/foobar";
+const char kSnippetTitle[] = "Title";
+const char kSnippetText[] = "Snippet";
+const char kSnippetSalientImage[] = "http://localhost/salient_image";
+const char kSnippetPublisherName[] = "Foo News";
+const char kSnippetAmpUrl[] = "http://localhost/amp";
+const float kSnippetScore = 5.0;
+
base::Time GetDefaultCreationTime() {
return base::Time::FromUTCExploded(kDefaultCreationTime);
}
-std::string GetTestJson(const std::string& content_creation_time_str,
- const std::string& expiry_time_str) {
- char json_str_format[] =
- "{ \"recos\": [ "
- "{ \"contentInfo\": {"
- "\"url\" : \"http://localhost/foobar\","
- "\"title\" : \"Title\","
- "\"snippet\" : \"Snippet\","
- "\"thumbnailUrl\" : \"http://localhost/salient_image\","
- "\"creationTimestampSec\" : \"%s\","
- "\"expiryTimestampSec\" : \"%s\","
- "\"sourceCorpusInfo\" : [ "
- "{\"ampUrl\" : \"http://localhost/amp\","
- "\"corpusId\" : \"http://localhost/foobar\","
- "\"publisherData\": { \"sourceName\" : \"Foo News\"}}]"
- "}}"
- "]}";
-
- return base::StringPrintf(json_str_format, content_creation_time_str.c_str(),
- expiry_time_str.c_str());
-}
-
-std::string GetTestJsonWithSources(const std::string& content_creation_time_str,
- const std::string& expiry_time_str,
- const std::vector<std::string>& source_urls,
- const std::vector<std::string>& publishers,
- const std::vector<std::string>& amp_urls) {
+base::Time GetDefaultExpirationTime() {
+ return base::Time::Now() + base::TimeDelta::FromHours(1);
+}
+
+std::string GetTestJson(const std::vector<std::string>& snippets) {
+ char json_str_format[] = "{ \"recos\": [ %s]}";
Marc Treib 2016/05/17 12:38:45 Might as well just inline this below (and remove t
jkrcal 2016/05/20 09:50:50 Done.
+ return base::StringPrintf(json_str_format,
+ base::JoinString(snippets, ", ").c_str());
+}
+
+std::string GetSnippetWithSources(const std::string& url,
Marc Treib 2016/05/17 12:38:45 GetSnippetWithTimesAndSources? If we have "With...
jkrcal 2016/05/20 09:50:50 Done.
+ const std::string& content_creation_time_str,
+ const std::string& expiry_time_str,
+ const std::vector<std::string>& source_urls,
+ const std::vector<std::string>& publishers,
+ const std::vector<std::string>& amp_urls) {
char json_str_format[] =
- "{ \"recos\": [ "
"{ \"contentInfo\": {"
- "\"url\" : \"http://localhost/foobar\","
- "\"title\" : \"Title\","
- "\"snippet\" : \"Snippet\","
- "\"thumbnailUrl\" : \"http://localhost/salient_image\","
+ "\"url\" : \"%s\","
+ "\"title\" : \"%s\","
+ "\"snippet\" : \"%s\","
+ "\"thumbnailUrl\" : \"%s\","
"\"creationTimestampSec\" : \"%s\","
"\"expiryTimestampSec\" : \"%s\","
"\"sourceCorpusInfo\" : [%s]"
- "}}"
- "]}";
+ "}, "
+ "\"score\" : %f}";
char source_corpus_info_format[] =
"{\"corpusId\": \"%s\","
@@ -116,44 +112,62 @@ std::string GetTestJsonWithSources(const std::string& content_creation_time_str,
}
// Remove the last comma
source_corpus_info_list_str.erase(source_corpus_info_list_str.size()-1, 1);
- return base::StringPrintf(json_str_format, content_creation_time_str.c_str(),
+ return base::StringPrintf(json_str_format, url.c_str(), kSnippetTitle,
+ kSnippetText, kSnippetSalientImage,
+ content_creation_time_str.c_str(),
expiry_time_str.c_str(),
- source_corpus_info_list_str.c_str());
+ source_corpus_info_list_str.c_str(), kSnippetScore);
}
-std::string GetTestJsonWithSources(const std::vector<std::string>& source_urls,
- const std::vector<std::string>& publishers,
- const std::vector<std::string>& amp_urls) {
- base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1);
- return GetTestJsonWithSources(
- NTPSnippet::TimeToJsonString(GetDefaultCreationTime()),
- NTPSnippet::TimeToJsonString(expiry_time), source_urls, publishers,
- amp_urls);
+std::string GetSnippetWithSources(const std::vector<std::string>& source_urls,
+ const std::vector<std::string>& publishers,
+ const std::vector<std::string>& amp_urls) {
+ return GetSnippetWithSources(
+ kSnippetUrl, NTPSnippet::TimeToJsonString(GetDefaultCreationTime()),
+ NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()), source_urls,
+ publishers, amp_urls);
+}
+
+std::string GetSnippet(const std::string& url,
Marc Treib 2016/05/17 12:38:45 Also here: GetSnippetWithUrlAndTimes
jkrcal 2016/05/20 09:50:50 Done.
+ const std::string& content_creation_time_str,
+ const std::string& expiry_time_str) {
+ return GetSnippetWithSources(
+ url, content_creation_time_str, expiry_time_str,
+ std::vector<std::string>({kSnippetUrl}),
Marc Treib 2016/05/17 12:38:45 Is the std::vector<std::string>() required here?
jkrcal 2016/05/20 09:50:50 You are right, not really. Though it seems that at
Marc Treib 2016/05/20 09:59:13 Makes sense - without that, you'll probably get a
+ std::vector<std::string>({kSnippetPublisherName}),
+ std::vector<std::string>({kSnippetAmpUrl}));
}
-std::string GetTestJson(const std::string& content_creation_time_str) {
- base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1);
- return GetTestJson(content_creation_time_str,
- NTPSnippet::TimeToJsonString(expiry_time));
+std::string GetSnippet() {
+ return GetSnippet(kSnippetUrl,
+ NTPSnippet::TimeToJsonString(GetDefaultCreationTime()),
+ NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()));
}
-std::string GetTestJson() {
- return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime()));
+std::string GetSnippetWithTimes(const std::string& content_creation_time_str,
+ const std::string& expiry_time_str) {
+ return GetSnippet(kSnippetUrl, content_creation_time_str, expiry_time_str);
}
-std::string GetTestExpiredJson() {
- return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime()),
- NTPSnippet::TimeToJsonString(base::Time::Now()));
+std::string GetSnippetWithUrl(const std::string& url) {
+ return GetSnippet(url, NTPSnippet::TimeToJsonString(GetDefaultCreationTime()),
+ NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()));
}
-std::string GetInvalidJson() {
- std::string json_str = GetTestJson();
+std::string GetExpiredSnippet() {
+ return GetSnippetWithTimes(
+ NTPSnippet::TimeToJsonString(GetDefaultCreationTime()),
+ NTPSnippet::TimeToJsonString(base::Time::Now()));
+}
+
+std::string GetInvalidSnippet() {
+ std::string json_str = GetSnippet();
// Make the json invalid by removing the final closing brace.
return json_str.substr(0, json_str.size() - 1);
}
-std::string GetIncompleteJson() {
- std::string json_str = GetTestJson();
+std::string GetIncompleteSnippet() {
+ std::string json_str = GetSnippet();
// Rename the "url" entry. The result is syntactically valid json that will
// fail to parse as snippets.
size_t pos = json_str.find("\"url\"");
@@ -292,24 +306,23 @@ TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) {
}
TEST_F(NTPSnippetsServiceTest, Full) {
- std::string json_str(GetTestJson());
+ std::string json_str(GetTestJson({GetSnippet()}));
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
const NTPSnippet& snippet = *service()->snippets().front();
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
- EXPECT_EQ(snippet.best_source().publisher_name, "Foo News");
- EXPECT_EQ(snippet.title(), "Title");
- EXPECT_EQ(snippet.snippet(), "Snippet");
- EXPECT_EQ(snippet.salient_image_url(),
- GURL("http://localhost/salient_image"));
+
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
+ EXPECT_EQ(snippet.title(), kSnippetTitle);
+ EXPECT_EQ(snippet.snippet(), kSnippetText);
+ EXPECT_EQ(snippet.salient_image_url(), GURL(kSnippetSalientImage));
EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date());
- EXPECT_EQ(snippet.best_source().amp_url.spec(),
- GURL("http://localhost/amp").spec());
+ EXPECT_EQ(snippet.best_source().publisher_name, kSnippetPublisherName);
+ EXPECT_EQ(snippet.best_source().amp_url.spec(), GURL(kSnippetAmpUrl).spec());
Marc Treib 2016/05/17 12:38:45 I think the ".spec()" isn't required here - GURLs
jkrcal 2016/05/20 09:50:50 Done.
}
TEST_F(NTPSnippetsServiceTest, Clear) {
- std::string json_str(GetTestJson());
+ std::string json_str(GetTestJson({GetSnippet()}));
LoadFromJSONString(json_str);
EXPECT_THAT(service()->snippets(), SizeIs(1));
@@ -319,114 +332,64 @@ TEST_F(NTPSnippetsServiceTest, Clear) {
}
TEST_F(NTPSnippetsServiceTest, InsertAtFront) {
- base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1);
- char json_str_format[] =
- "{ \"recos\": [ "
- "{ \"contentInfo\": {"
- "\"url\" : \"%s\","
- "\"title\" : \"Title\","
- "\"snippet\" : \"Snippet\","
- "\"thumbnailUrl\" : \"http://localhost/salient_image\","
- "\"creationTimestampSec\" : \"%s\","
- "\"expiryTimestampSec\" : \"%s\","
- "\"sourceCorpusInfo\" : [{\"corpusId\": \"http://first\","
- "\"publisherData\": {"
- "\"sourceName\": \"Source 1\""
- "},"
- "\"ampUrl\": \"\"}]"
- "}}"
- "]}";
- std::string json_str(base::StringPrintf(
- json_str_format, "http://first",
- NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
- NTPSnippet::TimeToJsonString(expiry_time).c_str()));
-
- LoadFromJSONString(json_str);
-
- EXPECT_THAT(service()->snippets(), ElementsAre(IdEq("http://first")));
-
- json_str = base::StringPrintf(
- json_str_format, "http://second",
- NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
- NTPSnippet::TimeToJsonString(expiry_time).c_str());
-
- LoadFromJSONString(json_str);
+ std::string first("http://first");
+ LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)}));
+ EXPECT_THAT(service()->snippets(), ElementsAre(IdEq(first)));
+ std::string second("http://second");
+ LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)}));
// The snippet loaded last should be at the first position in the list now.
- EXPECT_THAT(service()->snippets(),
- ElementsAre(IdEq("http://second"), IdEq("http://first")));
+ EXPECT_THAT(service()->snippets(), ElementsAre(IdEq(second), IdEq(first)));
}
TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) {
int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting();
int snippets_per_load = max_snippet_count / 2 + 1;
-
- base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1);
- char json_str_format[] =
- "{ \"contentInfo\": {"
- "\"url\" : \"http://localhost/%i\","
- "\"title\" : \"Title\","
- "\"snippet\" : \"Snippet\","
- "\"thumbnailUrl\" : \"http://localhost/salient_image\","
- "\"creationTimestampSec\" : \"%s\","
- "\"expiryTimestampSec\" : \"%s\","
- "\"sourceCorpusInfo\" : [{\"corpusId\": \"http://first\","
- "\"publisherData\": {"
- "\"sourceName\": \"Source 1\""
- "},"
- "\"ampUrl\": \"\"}]"
- "}}";
+ char url_format[] = "http://localhost/%i";
std::vector<std::string> snippets1;
std::vector<std::string> snippets2;
for (int i = 0; i < snippets_per_load; i++) {
- snippets1.push_back(base::StringPrintf(
- json_str_format, i,
- NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
- NTPSnippet::TimeToJsonString(expiry_time).c_str()));
- snippets2.push_back(base::StringPrintf(
- json_str_format, snippets_per_load + i,
- NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
- NTPSnippet::TimeToJsonString(expiry_time).c_str()));
+ snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i)));
+ snippets2.push_back(GetSnippetWithUrl(
+ base::StringPrintf(url_format, snippets_per_load + i)));
}
- LoadFromJSONString(
- "{ \"recos\": [ " + base::JoinString(snippets1, ", ") + "]}");
+ LoadFromJSONString(GetTestJson(snippets1));
ASSERT_THAT(service()->snippets(), SizeIs(snippets1.size()));
- LoadFromJSONString(
- "{ \"recos\": [ " + base::JoinString(snippets2, ", ") + "]}");
+ LoadFromJSONString(GetTestJson(snippets2));
EXPECT_THAT(service()->snippets(), SizeIs(max_snippet_count));
}
TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
- LoadFromJSONString(GetInvalidJson());
+ LoadFromJSONString(GetTestJson({GetInvalidSnippet()}));
EXPECT_THAT(service()->last_status(), StartsWith("Received invalid JSON"));
EXPECT_THAT(service()->snippets(), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) {
- LoadFromJSONString(GetTestJson());
+ LoadFromJSONString(GetTestJson({GetSnippet()}));
ASSERT_THAT(service()->snippets(), SizeIs(1));
ASSERT_EQ("OK", service()->last_status());
- LoadFromJSONString(GetInvalidJson());
+ LoadFromJSONString(GetTestJson({GetInvalidSnippet()}));
EXPECT_THAT(service()->last_status(), StartsWith("Received invalid JSON"));
// This should not have changed the existing snippets.
EXPECT_THAT(service()->snippets(), SizeIs(1));
}
TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) {
- LoadFromJSONString(GetIncompleteJson());
+ LoadFromJSONString(GetTestJson({GetIncompleteSnippet()}));
EXPECT_EQ("Invalid / empty list.", service()->last_status());
EXPECT_THAT(service()->snippets(), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) {
- LoadFromJSONString(GetTestJson());
+ LoadFromJSONString(GetTestJson({GetSnippet()}));
ASSERT_THAT(service()->snippets(), SizeIs(1));
- LoadFromJSONString(GetIncompleteJson());
+ LoadFromJSONString(GetTestJson({GetIncompleteSnippet()}));
EXPECT_EQ("Invalid / empty list.", service()->last_status());
// This should not have changed the existing snippets.
EXPECT_THAT(service()->snippets(), SizeIs(1));
@@ -438,7 +401,7 @@ TEST_F(NTPSnippetsServiceTest, Discard) {
publishers.push_back(std::string("Source 1"));
amp_urls.push_back(std::string());
std::string json_str(
- GetTestJsonWithSources(source_urls, publishers, amp_urls));
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
@@ -449,7 +412,7 @@ TEST_F(NTPSnippetsServiceTest, Discard) {
EXPECT_THAT(service()->snippets(), SizeIs(1));
// Discard the snippet.
- EXPECT_TRUE(service()->DiscardSnippet("http://localhost/foobar"));
+ EXPECT_TRUE(service()->DiscardSnippet(kSnippetUrl));
EXPECT_THAT(service()->snippets(), IsEmpty());
// Make sure that fetching the same snippet again does not re-add it.
@@ -470,15 +433,15 @@ TEST_F(NTPSnippetsServiceTest, Discard) {
}
TEST_F(NTPSnippetsServiceTest, GetDiscarded) {
- LoadFromJSONString(GetTestJson());
+ LoadFromJSONString(GetTestJson({GetSnippet()}));
// For the test, we need the snippet to get discarded.
- ASSERT_TRUE(service()->DiscardSnippet("http://localhost/foobar"));
+ ASSERT_TRUE(service()->DiscardSnippet(kSnippetUrl));
const NTPSnippetsService::NTPSnippetStorage& snippets =
service()->discarded_snippets();
EXPECT_EQ(1u, snippets.size());
for (auto& snippet : snippets) {
- EXPECT_EQ("http://localhost/foobar", snippet->id());
+ EXPECT_EQ(kSnippetUrl, snippet->id());
}
// There should be no discarded snippet after clearing the list.
@@ -487,19 +450,21 @@ TEST_F(NTPSnippetsServiceTest, GetDiscarded) {
}
TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) {
- std::string json_str(GetTestJson("aaa1448459205"));
+ std::string json_str(GetTestJson({GetSnippetWithTimes(
+ "aaa1448459205",
+ NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))}));
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
const NTPSnippet& snippet = *service()->snippets().front();
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
- EXPECT_EQ(snippet.title(), "Title");
- EXPECT_EQ(snippet.snippet(), "Snippet");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
+ EXPECT_EQ(snippet.title(), kSnippetTitle);
+ EXPECT_EQ(snippet.snippet(), kSnippetText);
EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date());
}
TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
- std::string json_str(GetTestExpiredJson());
+ std::string json_str(GetTestJson({GetExpiredSnippet()}));
LoadFromJSONString(json_str);
EXPECT_THAT(service()->snippets(), IsEmpty());
@@ -511,13 +476,13 @@ TEST_F(NTPSnippetsServiceTest, TestSingleSource) {
publishers.push_back(std::string("Source 1"));
amp_urls.push_back(std::string("http://source1.amp.com"));
std::string json_str(
- GetTestJsonWithSources(source_urls, publishers, amp_urls));
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
const NTPSnippet& snippet = *service()->snippets().front();
EXPECT_EQ(snippet.sources().size(), 1u);
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
@@ -529,7 +494,7 @@ TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) {
publishers.push_back(std::string("Source 1"));
amp_urls.push_back(std::string("http://source1.amp.com"));
std::string json_str(
- GetTestJsonWithSources(source_urls, publishers, amp_urls));
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
EXPECT_THAT(service()->snippets(), IsEmpty());
@@ -541,7 +506,7 @@ TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) {
publishers.push_back(std::string());
amp_urls.push_back(std::string());
std::string json_str(
- GetTestJsonWithSources(source_urls, publishers, amp_urls));
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
EXPECT_THAT(service()->snippets(), IsEmpty());
@@ -556,14 +521,14 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleSources) {
amp_urls.push_back(std::string("http://source1.amp.com"));
amp_urls.push_back(std::string("http://source2.amp.com"));
std::string json_str(
- GetTestJsonWithSources(source_urls, publishers, amp_urls));
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
const NTPSnippet& snippet = *service()->snippets().front();
// Expect the first source to be chosen
EXPECT_EQ(snippet.sources().size(), 2u);
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
@@ -580,14 +545,14 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) {
amp_urls.push_back(std::string("http://source1.amp.com"));
amp_urls.push_back(std::string());
std::string json_str(
- GetTestJsonWithSources(source_urls, publishers, amp_urls));
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
{
const NTPSnippet& snippet = *service()->snippets().front();
EXPECT_EQ(snippet.sources().size(), 2u);
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2"));
EXPECT_EQ(snippet.best_source().amp_url, GURL());
@@ -605,14 +570,15 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) {
amp_urls.clear();
amp_urls.push_back(std::string());
amp_urls.push_back(std::string("http://source2.amp.com"));
- json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
+ json_str =
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
{
const NTPSnippet& snippet = *service()->snippets().front();
EXPECT_EQ(snippet.sources().size(), 2u);
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet.best_source().amp_url, GURL());
@@ -631,7 +597,8 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) {
amp_urls.clear();
amp_urls.push_back(std::string());
amp_urls.push_back(std::string("http://source2.amp.com"));
- json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
+ json_str =
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
EXPECT_THAT(service()->snippets(), IsEmpty());
@@ -650,14 +617,14 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
amp_urls.push_back(std::string("http://source2.amp.com"));
amp_urls.push_back(std::string("http://source3.amp.com"));
std::string json_str(
- GetTestJsonWithSources(source_urls, publishers, amp_urls));
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
{
const NTPSnippet& snippet = *service()->snippets().front();
EXPECT_EQ(snippet.sources().size(), 3u);
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
@@ -677,14 +644,15 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
amp_urls.push_back(std::string("http://source1.amp.com"));
amp_urls.push_back(std::string("http://source2.amp.com"));
amp_urls.push_back(std::string("http://source3.amp.com"));
- json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
+ json_str =
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
{
const NTPSnippet& snippet = *service()->snippets().front();
EXPECT_EQ(snippet.sources().size(), 3u);
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2"));
EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com"));
@@ -704,14 +672,15 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
amp_urls.push_back(std::string());
amp_urls.push_back(std::string("http://source2.amp.com"));
amp_urls.push_back(std::string("http://source3.amp.com"));
- json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
+ json_str =
+ GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
ASSERT_THAT(service()->snippets(), SizeIs(1));
{
const NTPSnippet& snippet = *service()->snippets().front();
EXPECT_EQ(snippet.sources().size(), 3u);
- EXPECT_EQ(snippet.id(), "http://localhost/foobar");
+ EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2"));
EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com"));
@@ -720,20 +689,21 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) {
base::HistogramTester tester;
- LoadFromJSONString(GetInvalidJson());
+ LoadFromJSONString(GetTestJson({GetInvalidSnippet()}));
+
EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"),
ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
// Invalid JSON shouldn't contribute to NumArticlesFetched.
EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"),
IsEmpty());
// Valid JSON with empty list.
- LoadFromJSONString("{ \"recos\": []}");
+ LoadFromJSONString(GetTestJson({}));
EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"),
ElementsAre(base::Bucket(/*min=*/0, /*count=*/2)));
EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"),
ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
// Snippet list should be populated with size 1.
- LoadFromJSONString(GetTestJson());
+ LoadFromJSONString(GetTestJson({GetSnippet()}));
EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"),
ElementsAre(base::Bucket(/*min=*/0, /*count=*/2),
base::Bucket(/*min=*/1, /*count=*/1)));
@@ -741,7 +711,7 @@ TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) {
ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
base::Bucket(/*min=*/1, /*count=*/1)));
// Duplicate snippet shouldn't increase the list size.
- LoadFromJSONString(GetTestJson());
+ LoadFromJSONString(GetTestJson({GetSnippet()}));
EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"),
ElementsAre(base::Bucket(/*min=*/0, /*count=*/2),
base::Bucket(/*min=*/1, /*count=*/2)));
@@ -753,8 +723,8 @@ TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) {
IsEmpty());
// Discarding a snippet should decrease the list size. This will only be
// logged after the next fetch.
- EXPECT_TRUE(service()->DiscardSnippet("http://localhost/foobar"));
- LoadFromJSONString(GetTestJson());
+ EXPECT_TRUE(service()->DiscardSnippet(kSnippetUrl));
+ LoadFromJSONString(GetTestJson({GetSnippet()}));
EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"),
ElementsAre(base::Bucket(/*min=*/0, /*count=*/3),
base::Bucket(/*min=*/1, /*count=*/2)));
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698