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

Side by Side 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: Created 4 years, 3 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_snippet.h" 5 #include "components/ntp_snippets/ntp_snippet.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace ntp_snippets { 12 namespace ntp_snippets {
13 namespace { 13 namespace {
14 14
15 std::unique_ptr<NTPSnippet> SnippetFromContentSuggestionJSON(
16 const std::string& json) {
17 auto json_value = base::JSONReader::Read(json);
18 base::DictionaryValue* json_dict;
19 if (!json_value->GetAsDictionary(&json_dict)) {
tschumann 2016/08/30 06:57:21 as you don't expect this to ever fail, you can als
Marc Treib 2016/08/30 09:29:28 I find it a bit nicer to ASSERT_*, so the test fai
sfiera 2016/08/30 13:16:43 My feeling is that ASSERTing is better but often n
tschumann 2016/08/30 14:01:39 Ok. My motivation is that the test verifies the sy
20 return nullptr;
21 }
22 return NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict);
23 }
24
15 TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) { 25 TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
16 const std::string kJsonStr = 26 const std::string kJsonStr =
17 "{" 27 "{"
18 " \"ids\" : [\"http://localhost/foobar\"]," 28 " \"ids\" : [\"http://localhost/foobar\"],"
19 " \"title\" : \"Foo Barred from Baz\"," 29 " \"title\" : \"Foo Barred from Baz\","
20 " \"snippet\" : \"...\"," 30 " \"snippet\" : \"...\","
21 " \"fullPageUrl\" : \"http://localhost/foobar\"," 31 " \"fullPageUrl\" : \"http://localhost/foobar\","
22 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," 32 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
23 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," 33 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
24 " \"attribution\" : \"Foo News\"," 34 " \"attribution\" : \"Foo News\","
25 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 35 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
26 " \"ampUrl\" : \"http://localhost/amp\"," 36 " \"ampUrl\" : \"http://localhost/amp\","
27 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 37 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
28 "}"; 38 "}";
29 auto json_value = base::JSONReader::Read(kJsonStr); 39 auto snippet = SnippetFromContentSuggestionJSON(kJsonStr);
30 base::DictionaryValue* json_dict;
31 ASSERT_TRUE(json_value->GetAsDictionary(&json_dict));
32
33 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict);
34 ASSERT_THAT(snippet, testing::NotNull()); 40 ASSERT_THAT(snippet, testing::NotNull());
35 41
36 EXPECT_EQ(snippet->id(), "http://localhost/foobar"); 42 EXPECT_EQ(snippet->id(), "http://localhost/foobar");
37 EXPECT_EQ(snippet->title(), "Foo Barred from Baz"); 43 EXPECT_EQ(snippet->title(), "Foo Barred from Baz");
38 EXPECT_EQ(snippet->snippet(), "..."); 44 EXPECT_EQ(snippet->snippet(), "...");
39 EXPECT_EQ(snippet->salient_image_url(), GURL("http://localhost/foobar.jpg")); 45 EXPECT_EQ(snippet->salient_image_url(), GURL("http://localhost/foobar.jpg"));
40 auto unix_publish_date = snippet->publish_date() - base::Time::UnixEpoch(); 46 auto unix_publish_date = snippet->publish_date() - base::Time::UnixEpoch();
41 auto expiry_duration = snippet->expiry_date() - snippet->publish_date(); 47 auto expiry_duration = snippet->expiry_date() - snippet->publish_date();
42 EXPECT_FLOAT_EQ(unix_publish_date.InSecondsF(), 1467284497.000000f); 48 EXPECT_FLOAT_EQ(unix_publish_date.InSecondsF(), 1467284497.000000f);
43 EXPECT_FLOAT_EQ(expiry_duration.InSecondsF(), 86400.000000f); 49 EXPECT_FLOAT_EQ(expiry_duration.InSecondsF(), 86400.000000f);
44 50
45 EXPECT_EQ(snippet->best_source().publisher_name, "Foo News"); 51 EXPECT_EQ(snippet->best_source().publisher_name, "Foo News");
46 EXPECT_EQ(snippet->best_source().url, GURL("http://localhost/foobar")); 52 EXPECT_EQ(snippet->best_source().url, GURL("http://localhost/foobar"));
47 EXPECT_EQ(snippet->best_source().amp_url, GURL("http://localhost/amp")); 53 EXPECT_EQ(snippet->best_source().amp_url, GURL("http://localhost/amp"));
48 } 54 }
49 55
56 std::unique_ptr<NTPSnippet> SnippetFromChromeReaderDict(
57 std::unique_ptr<base::DictionaryValue> dict) {
58 if (!dict) {
tschumann 2016/08/30 06:57:20 i don't think you need to handle this case. None o
59 return nullptr;
60 }
61 return NTPSnippet::CreateFromChromeReaderDictionary(*dict);
62 }
63
64 std::unique_ptr<base::DictionaryValue> SnippetWithTwoSources() {
65 std::string kJsonStr =
Marc Treib 2016/08/30 09:29:28 nit: If you use kConstantNaming, you should also m
sfiera 2016/08/30 13:16:43 Done.
66 "{\n"
67 " \"contentInfo\": {\n"
68 " \"url\": \"http://url.com\",\n"
69 " \"title\": \"Source 1 Title\",\n"
70 " \"snippet\": \"Source 1 Snippet\",\n"
71 " \"thumbnailUrl\": \"http://url.com/thumbnail\",\n"
72 " \"creationTimestampSec\": 1234567890,\n"
73 " \"expiryTimestampSec\": 2345678901,\n"
74 " \"sourceCorpusInfo\": [{\n"
75 " \"corpusId\": \"http://source1.com\",\n"
76 " \"publisherData\": {\n"
77 " \"sourceName\": \"Source 1\"\n"
78 " },\n"
79 " \"ampUrl\": \"http://source1.amp.com\"\n"
80 " }, {\n"
81 " \"corpusId\": \"http://source2.com\",\n"
82 " \"publisherData\": {\n"
83 " \"sourceName\": \"Source 2\"\n"
84 " },\n"
85 " \"ampUrl\": \"http://source2.amp.com\"\n"
86 " }]\n"
87 " },\n"
88 " \"score\": 5.0\n"
89 "}\n";
90
91 auto json_value = base::JSONReader::Read(kJsonStr);
92 base::DictionaryValue* json_dict;
93 if (!json_value->GetAsDictionary(&json_dict)) {
tschumann 2016/08/30 06:57:20 CHECK()?
94 return nullptr;
95 }
96 return json_dict->CreateDeepCopy();
97 }
98
99 TEST(NTPSnippetTest, TestMultipleSources) {
100 auto snippet = SnippetFromChromeReaderDict(SnippetWithTwoSources());
101 ASSERT_THAT(snippet, testing::NotNull());
102
103 // Expect the first source to be chosen
Marc Treib 2016/08/30 09:29:29 Why? AFAICT, they're equivalent. Do we really want
sfiera 2016/08/30 13:16:43 I don't know; this was moved verbatim from the oth
Marc Treib 2016/08/30 13:43:38 Hm, so if we can't figure out why we'd expect this
sfiera 2016/08/30 16:33:09 I've loosened the tests; there are things we care
104 EXPECT_EQ(snippet->sources().size(), 2u);
105 EXPECT_EQ(snippet->id(), "http://url.com");
106 EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
107 EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
108 EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source1.amp.com"));
109 }
110
111 TEST(NTPSnippetTest, TestMultipleIncompleteSources1) {
112 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name
113 // Source 2 should win since we favor publisher name over amp url
114 auto dict = SnippetWithTwoSources();
115 base::ListValue* sources;
116 ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
117 base::DictionaryValue* source;
118 ASSERT_TRUE(sources->GetDictionary(0, &source));
119 source->Remove("publisherData.sourceName", nullptr);
120 ASSERT_TRUE(sources->GetDictionary(1, &source));
121 source->Remove("ampUrl", nullptr);
122
123 auto snippet = SnippetFromChromeReaderDict(std::move(dict));
124 ASSERT_THAT(snippet, testing::NotNull());
125
126 EXPECT_EQ(snippet->sources().size(), 2u);
127 EXPECT_EQ(snippet->id(), "http://url.com");
128 EXPECT_EQ(snippet->best_source().url, GURL("http://source2.com"));
129 EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 2"));
130 EXPECT_EQ(snippet->best_source().amp_url, GURL());
131 }
132
133 TEST(NTPSnippetTest, TestMultipleIncompleteSources2) {
134 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name
135 // Source 1 should win in this case since we prefer publisher name to AMP url
136 auto dict = SnippetWithTwoSources();
137 base::ListValue* sources;
138 ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
139 base::DictionaryValue* source;
140 ASSERT_TRUE(sources->GetDictionary(0, &source));
141 source->Remove("ampUrl", nullptr);
142 ASSERT_TRUE(sources->GetDictionary(1, &source));
143 source->Remove("publisherData.sourceName", nullptr);
144
145 auto snippet = SnippetFromChromeReaderDict(std::move(dict));
146 ASSERT_THAT(snippet, testing::NotNull());
147
148 EXPECT_EQ(snippet->sources().size(), 2u);
149 EXPECT_EQ(snippet->id(), "http://url.com");
150 EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
151 EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
152 EXPECT_EQ(snippet->best_source().amp_url, GURL());
153 }
154
155 TEST(NTPSnippetTest, TestMultipleIncompleteSources3) {
156 // Set source 1 to have no AMP url and no source, and source 2 to only have
157 // amp url. There should be no snippets since we only add sources we consider
158 // complete
159 auto dict = SnippetWithTwoSources();
160 base::ListValue* sources;
161 ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
162 base::DictionaryValue* source;
163 ASSERT_TRUE(sources->GetDictionary(0, &source));
164 source->Remove("publisherData.sourceName", nullptr);
165 source->Remove("ampUrl", nullptr);
166 ASSERT_TRUE(sources->GetDictionary(1, &source));
167 source->Remove("publisherData.sourceName", nullptr);
168
169 auto snippet = SnippetFromChromeReaderDict(std::move(dict));
170 ASSERT_THAT(snippet, testing::NotNull());
171 ASSERT_FALSE(snippet->is_complete());
172 }
173
174 std::unique_ptr<base::DictionaryValue> SnippetWithThreeSources() {
175 std::string kJsonStr =
176 "{\n"
177 " \"contentInfo\": {\n"
178 " \"url\": \"http://url.com\",\n"
179 " \"title\": \"Source 1 Title\",\n"
180 " \"snippet\": \"Source 1 Snippet\",\n"
181 " \"thumbnailUrl\": \"http://url.com/thumbnail\",\n"
182 " \"creationTimestampSec\": 1234567890,\n"
183 " \"expiryTimestampSec\": 2345678901,\n"
184 " \"sourceCorpusInfo\": [{\n"
185 " \"corpusId\": \"http://source1.com\",\n"
186 " \"publisherData\": {\n"
187 " \"sourceName\": \"Source 1\"\n"
188 " },\n"
189 " \"ampUrl\": \"http://source1.amp.com\"\n"
190 " }, {\n"
191 " \"corpusId\": \"http://source2.com\",\n"
192 " \"publisherData\": {\n"
193 " \"sourceName\": \"Source 2\"\n"
194 " },\n"
195 " \"ampUrl\": \"http://source2.amp.com\"\n"
196 " }, {\n"
197 " \"corpusId\": \"http://source3.com\",\n"
198 " \"publisherData\": {\n"
199 " \"sourceName\": \"Source 3\"\n"
200 " },\n"
201 " \"ampUrl\": \"http://source3.amp.com\"\n"
202 " }]\n"
203 " },\n"
204 " \"score\": 5.0\n"
205 "}\n";
206
207 auto json_value = base::JSONReader::Read(kJsonStr);
208 base::DictionaryValue* json_dict;
209 if (!json_value->GetAsDictionary(&json_dict)) {
tschumann 2016/08/30 06:57:21 CHECK()?
210 return nullptr;
211 }
212 return json_dict->CreateDeepCopy();
213 }
214
215 TEST(NTPSnippetTest, TestMultipleCompleteSources1) {
216 // Test 2 complete sources, we should choose the first complete source
217 auto dict = SnippetWithThreeSources();
218 base::ListValue* sources;
219 ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
220 base::DictionaryValue* source;
221 ASSERT_TRUE(sources->GetDictionary(1, &source));
222 source->Remove("publisherData.sourceName", nullptr);
223
224 auto snippet = SnippetFromChromeReaderDict(std::move(dict));
225 ASSERT_THAT(snippet, testing::NotNull());
226
227 EXPECT_EQ(snippet->sources().size(), 3u);
228 EXPECT_EQ(snippet->id(), "http://url.com");
229 EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
230 EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
231 EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source1.amp.com"));
232 }
233
234 TEST(NTPSnippetTest, TestMultipleCompleteSources2) {
235 // Test 2 complete sources, we should choose the first complete source
236 auto dict = SnippetWithThreeSources();
237 base::ListValue* sources;
238 ASSERT_TRUE(dict->GetList("contentInfo.sourceCorpusInfo", &sources));
239 base::DictionaryValue* source;
240 ASSERT_TRUE(sources->GetDictionary(0, &source));
241 source->Remove("publisherData.sourceName", nullptr);
242
243 auto snippet = SnippetFromChromeReaderDict(std::move(dict));
244 ASSERT_THAT(snippet, testing::NotNull());
245
246 EXPECT_EQ(snippet->sources().size(), 3u);
247 EXPECT_EQ(snippet->id(), "http://url.com");
248 EXPECT_EQ(snippet->best_source().url, GURL("http://source2.com"));
249 EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 2"));
250 EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source2.amp.com"));
251 }
252
253 TEST(NTPSnippetTest, TestMultipleCompleteSources3) {
254 // Test 3 complete sources, we should choose the first complete source
255 auto dict = SnippetWithThreeSources();
256 auto snippet = SnippetFromChromeReaderDict(std::move(dict));
257 ASSERT_THAT(snippet, testing::NotNull());
258
259 EXPECT_EQ(snippet->sources().size(), 3u);
260 EXPECT_EQ(snippet->id(), "http://url.com");
261 EXPECT_EQ(snippet->best_source().url, GURL("http://source1.com"));
262 EXPECT_EQ(snippet->best_source().publisher_name, std::string("Source 1"));
263 EXPECT_EQ(snippet->best_source().amp_url, GURL("http://source1.amp.com"));
264 }
265
50 } // namespace 266 } // namespace
51 } // namespace ntp_snippets 267 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698