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

Unified Diff: components/ntp_snippets/ntp_snippet_unittest.cc

Issue 2285133004: Overhaul ntp_snippets_service_unittest.cc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: No brackets. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/ntp_snippet_unittest.cc
diff --git a/components/ntp_snippets/ntp_snippet_unittest.cc b/components/ntp_snippets/ntp_snippet_unittest.cc
index 93304b63f9649c318717050acf00b0590d7848d8..825a6f29b1619e5f074bc99ae7be25d90ba466ae 100644
--- a/components/ntp_snippets/ntp_snippet_unittest.cc
+++ b/components/ntp_snippets/ntp_snippet_unittest.cc
@@ -12,6 +12,16 @@
namespace ntp_snippets {
namespace {
+std::unique_ptr<NTPSnippet> SnippetFromContentSuggestionJSON(
+ const std::string& json) {
+ auto json_value = base::JSONReader::Read(json);
+ base::DictionaryValue* json_dict;
+ if (!json_value->GetAsDictionary(&json_dict)) {
+ return nullptr;
+ }
+ return NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict);
+}
+
TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
const std::string kJsonStr =
"{"
@@ -26,11 +36,7 @@ TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
" \"ampUrl\" : \"http://localhost/amp\","
" \"faviconUrl\" : \"http://localhost/favicon.ico\" "
"}";
- auto json_value = base::JSONReader::Read(kJsonStr);
- base::DictionaryValue* json_dict;
- ASSERT_TRUE(json_value->GetAsDictionary(&json_dict));
-
- auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict);
+ auto snippet = SnippetFromContentSuggestionJSON(kJsonStr);
ASSERT_THAT(snippet, testing::NotNull());
EXPECT_EQ(snippet->id(), "http://localhost/foobar");
@@ -47,5 +53,215 @@ TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
EXPECT_EQ(snippet->best_source().amp_url, GURL("http://localhost/amp"));
}
+std::unique_ptr<NTPSnippet> SnippetFromChromeReaderDict(
+ std::unique_ptr<base::DictionaryValue> dict) {
+ if (!dict) {
+ return nullptr;
+ }
+ return NTPSnippet::CreateFromChromeReaderDictionary(*dict);
+}
+
+std::unique_ptr<base::DictionaryValue> SnippetWithTwoSources() {
+ const std::string kJsonStr =
+ "{\n"
+ " \"contentInfo\": {\n"
+ " \"url\": \"http://url.com\",\n"
+ " \"title\": \"Source 1 Title\",\n"
+ " \"snippet\": \"Source 1 Snippet\",\n"
+ " \"thumbnailUrl\": \"http://url.com/thumbnail\",\n"
+ " \"creationTimestampSec\": 1234567890,\n"
+ " \"expiryTimestampSec\": 2345678901,\n"
+ " \"sourceCorpusInfo\": [{\n"
+ " \"corpusId\": \"http://source1.com\",\n"
+ " \"publisherData\": {\n"
+ " \"sourceName\": \"Source 1\"\n"
+ " },\n"
+ " \"ampUrl\": \"http://source1.amp.com\"\n"
+ " }, {\n"
+ " \"corpusId\": \"http://source2.com\",\n"
+ " \"publisherData\": {\n"
+ " \"sourceName\": \"Source 2\"\n"
+ " },\n"
+ " \"ampUrl\": \"http://source2.amp.com\"\n"
+ " }]\n"
+ " },\n"
+ " \"score\": 5.0\n"
+ "}\n";
+
+ auto json_value = base::JSONReader::Read(kJsonStr);
+ base::DictionaryValue* json_dict;
+ if (!json_value->GetAsDictionary(&json_dict)) {
+ return nullptr;
+ }
+ return json_dict->CreateDeepCopy();
+}
+
+TEST(NTPSnippetTest, TestMultipleSources) {
+ auto snippet = SnippetFromChromeReaderDict(SnippetWithTwoSources());
+ ASSERT_THAT(snippet, testing::NotNull());
+
+ // Expect the first source to be chosen.
+ EXPECT_EQ(snippet->sources().size(), 2u);
+ EXPECT_EQ(snippet->id(), "http://url.com");
+ 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"));
+}
+
+TEST(NTPSnippetTest, TestMultipleIncompleteSources1) {
+ // Set Source 2 to have no AMP url, and Source 1 to have no publisher name
+ // Source 2 should win since we favor publisher name over amp url
+ auto dict = SnippetWithTwoSources();
+ base::ListValue* sources;
+ ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
+ base::DictionaryValue* source;
+ ASSERT_TRUE(sources->GetDictionary(0, &source));
+ source->Remove("publisherData.sourceName", nullptr);
+ ASSERT_TRUE(sources->GetDictionary(1, &source));
+ source->Remove("ampUrl", nullptr);
+
+ auto snippet = SnippetFromChromeReaderDict(std::move(dict));
+ ASSERT_THAT(snippet, testing::NotNull());
+
+ EXPECT_EQ(snippet->sources().size(), 2u);
+ EXPECT_EQ(snippet->id(), "http://url.com");
+ 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());
+}
+
+TEST(NTPSnippetTest, TestMultipleIncompleteSources2) {
+ // Set Source 1 to have no AMP url, and Source 2 to have no publisher name
+ // Source 1 should win in this case since we prefer publisher name to AMP url
+ auto dict = SnippetWithTwoSources();
+ base::ListValue* sources;
+ ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
+ base::DictionaryValue* source;
+ ASSERT_TRUE(sources->GetDictionary(0, &source));
+ source->Remove("ampUrl", nullptr);
+ ASSERT_TRUE(sources->GetDictionary(1, &source));
+ source->Remove("publisherData.sourceName", nullptr);
+
+ auto snippet = SnippetFromChromeReaderDict(std::move(dict));
+ ASSERT_THAT(snippet, testing::NotNull());
+
+ EXPECT_EQ(snippet->sources().size(), 2u);
+ EXPECT_EQ(snippet->id(), "http://url.com");
+ 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());
+}
+
+TEST(NTPSnippetTest, TestMultipleIncompleteSources3) {
+ // Set source 1 to have no AMP url and no source, and source 2 to only have
+ // amp url. There should be no snippets since we only add sources we consider
+ // complete
+ auto dict = SnippetWithTwoSources();
+ base::ListValue* sources;
+ ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
+ base::DictionaryValue* source;
+ ASSERT_TRUE(sources->GetDictionary(0, &source));
+ source->Remove("publisherData.sourceName", nullptr);
+ source->Remove("ampUrl", nullptr);
+ ASSERT_TRUE(sources->GetDictionary(1, &source));
+ source->Remove("publisherData.sourceName", nullptr);
+
+ auto snippet = SnippetFromChromeReaderDict(std::move(dict));
+ ASSERT_THAT(snippet, testing::NotNull());
+ ASSERT_FALSE(snippet->is_complete());
+}
+
+std::unique_ptr<base::DictionaryValue> SnippetWithThreeSources() {
+ const std::string kJsonStr =
+ "{\n"
+ " \"contentInfo\": {\n"
+ " \"url\": \"http://url.com\",\n"
+ " \"title\": \"Source 1 Title\",\n"
+ " \"snippet\": \"Source 1 Snippet\",\n"
+ " \"thumbnailUrl\": \"http://url.com/thumbnail\",\n"
+ " \"creationTimestampSec\": 1234567890,\n"
+ " \"expiryTimestampSec\": 2345678901,\n"
+ " \"sourceCorpusInfo\": [{\n"
+ " \"corpusId\": \"http://source1.com\",\n"
+ " \"publisherData\": {\n"
+ " \"sourceName\": \"Source 1\"\n"
+ " },\n"
+ " \"ampUrl\": \"http://source1.amp.com\"\n"
+ " }, {\n"
+ " \"corpusId\": \"http://source2.com\",\n"
+ " \"publisherData\": {\n"
+ " \"sourceName\": \"Source 2\"\n"
+ " },\n"
+ " \"ampUrl\": \"http://source2.amp.com\"\n"
+ " }, {\n"
+ " \"corpusId\": \"http://source3.com\",\n"
+ " \"publisherData\": {\n"
+ " \"sourceName\": \"Source 3\"\n"
+ " },\n"
+ " \"ampUrl\": \"http://source3.amp.com\"\n"
+ " }]\n"
+ " },\n"
+ " \"score\": 5.0\n"
+ "}\n";
+
+ auto json_value = base::JSONReader::Read(kJsonStr);
+ base::DictionaryValue* json_dict;
+ if (!json_value->GetAsDictionary(&json_dict)) {
+ return nullptr;
+ }
+ return json_dict->CreateDeepCopy();
+}
+
+TEST(NTPSnippetTest, TestMultipleCompleteSources1) {
+ // Test 2 complete sources, we should choose the first complete source
+ auto dict = SnippetWithThreeSources();
+ base::ListValue* sources;
+ ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
+ base::DictionaryValue* source;
+ ASSERT_TRUE(sources->GetDictionary(1, &source));
+ source->Remove("publisherData.sourceName", nullptr);
+
+ auto snippet = SnippetFromChromeReaderDict(std::move(dict));
+ ASSERT_THAT(snippet, testing::NotNull());
+
+ EXPECT_EQ(snippet->sources().size(), 3u);
+ EXPECT_EQ(snippet->id(), "http://url.com");
+ 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"));
+}
+
+TEST(NTPSnippetTest, TestMultipleCompleteSources2) {
+ // Test 2 complete sources, we should choose the first complete source
+ auto dict = SnippetWithThreeSources();
+ base::ListValue* sources;
+ ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
+ base::DictionaryValue* source;
+ ASSERT_TRUE(sources->GetDictionary(0, &source));
+ source->Remove("publisherData.sourceName", nullptr);
+
+ auto snippet = SnippetFromChromeReaderDict(std::move(dict));
+ ASSERT_THAT(snippet, testing::NotNull());
+
+ EXPECT_EQ(snippet->sources().size(), 3u);
+ EXPECT_EQ(snippet->id(), "http://url.com");
+ 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"));
+}
+
+TEST(NTPSnippetTest, TestMultipleCompleteSources3) {
+ // Test 3 complete sources, we should choose the first complete source
+ auto dict = SnippetWithThreeSources();
+ auto snippet = SnippetFromChromeReaderDict(std::move(dict));
+ ASSERT_THAT(snippet, testing::NotNull());
+
+ EXPECT_EQ(snippet->sources().size(), 3u);
+ EXPECT_EQ(snippet->id(), "http://url.com");
+ 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"));
+}
+
} // namespace
} // namespace ntp_snippets
« no previous file with comments | « no previous file | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698