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/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> |
8 | 9 |
9 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
(...skipping 16 matching lines...) Expand all Loading... |
34 base::Time GetDefaultCreationTime() { | 35 base::Time GetDefaultCreationTime() { |
35 return base::Time::FromUTCExploded(kDefaultCreationTime); | 36 return base::Time::FromUTCExploded(kDefaultCreationTime); |
36 } | 37 } |
37 | 38 |
38 std::string GetTestJson(const std::string& content_creation_time_str, | 39 std::string GetTestJson(const std::string& content_creation_time_str, |
39 const std::string& expiry_time_str) { | 40 const std::string& expiry_time_str) { |
40 char json_str_format[] = | 41 char json_str_format[] = |
41 "{ \"recos\": [ " | 42 "{ \"recos\": [ " |
42 "{ \"contentInfo\": {" | 43 "{ \"contentInfo\": {" |
43 "\"url\" : \"http://localhost/foobar\"," | 44 "\"url\" : \"http://localhost/foobar\"," |
44 "\"site_title\" : \"Site Title\"," | |
45 "\"favicon_url\" : \"http://localhost/favicon\"," | |
46 "\"title\" : \"Title\"," | 45 "\"title\" : \"Title\"," |
47 "\"snippet\" : \"Snippet\"," | 46 "\"snippet\" : \"Snippet\"," |
48 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," | 47 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," |
49 "\"creationTimestampSec\" : \"%s\"," | 48 "\"creationTimestampSec\" : \"%s\"," |
50 "\"expiryTimestampSec\" : \"%s\"," | 49 "\"expiryTimestampSec\" : \"%s\"," |
51 "\"sourceCorpusInfo\" : [ " | 50 "\"sourceCorpusInfo\" : [ " |
52 "{\"ampUrl\" : \"http://localhost/amp\"}," | 51 "{\"ampUrl\" : \"http://localhost/amp\"," |
53 "{\"corpusId\" : \"id\"}]" | 52 "\"corpusId\" : \"http://localhost/foobar\"," |
| 53 "\"publisherData\": { \"sourceName\" : \"Foo News\"}}]" |
54 "}}" | 54 "}}" |
55 "]}"; | 55 "]}"; |
56 | 56 |
57 return base::StringPrintf(json_str_format, content_creation_time_str.c_str(), | 57 return base::StringPrintf(json_str_format, content_creation_time_str.c_str(), |
58 expiry_time_str.c_str()); | 58 expiry_time_str.c_str()); |
59 } | 59 } |
60 | 60 |
| 61 std::string GetTestJsonWithSources(const std::string& content_creation_time_str, |
| 62 const std::string& expiry_time_str, |
| 63 const std::vector<std::string>& source_urls, |
| 64 const std::vector<std::string>& publishers, |
| 65 const std::vector<std::string>& amp_urls) { |
| 66 char json_str_format[] = |
| 67 "{ \"recos\": [ " |
| 68 "{ \"contentInfo\": {" |
| 69 "\"url\" : \"http://localhost/foobar\"," |
| 70 "\"title\" : \"Title\"," |
| 71 "\"snippet\" : \"Snippet\"," |
| 72 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," |
| 73 "\"creationTimestampSec\" : \"%s\"," |
| 74 "\"expiryTimestampSec\" : \"%s\"," |
| 75 "\"sourceCorpusInfo\" : [%s]" |
| 76 "}}" |
| 77 "]}"; |
| 78 |
| 79 char source_corpus_info_format[] = |
| 80 "{\"corpusId\": \"%s\"," |
| 81 "\"publisherData\": {" |
| 82 "\"sourceName\": \"%s\"" |
| 83 "}," |
| 84 "\"ampUrl\": \"%s\"}"; |
| 85 |
| 86 std::string source_corpus_info_list_str; |
| 87 for (size_t i = 0; i < source_urls.size(); ++i) { |
| 88 std::string source_corpus_info_str = |
| 89 base::StringPrintf(source_corpus_info_format, |
| 90 source_urls[i].empty() ? "" : source_urls[i].c_str(), |
| 91 publishers[i].empty() ? "" : publishers[i].c_str(), |
| 92 amp_urls[i].empty() ? "" : amp_urls[i].c_str()); |
| 93 source_corpus_info_list_str.append(source_corpus_info_str); |
| 94 source_corpus_info_list_str.append(","); |
| 95 } |
| 96 // Remove the last comma |
| 97 source_corpus_info_list_str.pop_back(); |
| 98 return base::StringPrintf(json_str_format, content_creation_time_str.c_str(), |
| 99 expiry_time_str.c_str(), |
| 100 source_corpus_info_list_str.c_str()); |
| 101 } |
| 102 |
| 103 std::string GetTestJsonWithSources(const std::vector<std::string>& source_urls, |
| 104 const std::vector<std::string>& publishers, |
| 105 const std::vector<std::string>& amp_urls) { |
| 106 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); |
| 107 return GetTestJsonWithSources( |
| 108 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()), |
| 109 NTPSnippet::TimeToJsonString(expiry_time), source_urls, publishers, |
| 110 amp_urls); |
| 111 } |
| 112 |
61 std::string GetTestJson(const std::string& content_creation_time_str) { | 113 std::string GetTestJson(const std::string& content_creation_time_str) { |
62 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); | 114 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); |
63 return GetTestJson(content_creation_time_str, | 115 return GetTestJson(content_creation_time_str, |
64 NTPSnippet::TimeToJsonString(expiry_time)); | 116 NTPSnippet::TimeToJsonString(expiry_time)); |
65 } | 117 } |
66 | 118 |
67 std::string GetTestJson() { | 119 std::string GetTestJson() { |
68 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime())); | 120 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime())); |
69 } | 121 } |
70 | 122 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 TEST_F(NTPSnippetsServiceTest, Full) { | 264 TEST_F(NTPSnippetsServiceTest, Full) { |
213 std::string json_str(GetTestJson()); | 265 std::string json_str(GetTestJson()); |
214 | 266 |
215 LoadFromJSONString(json_str); | 267 LoadFromJSONString(json_str); |
216 EXPECT_EQ(service()->size(), 1u); | 268 EXPECT_EQ(service()->size(), 1u); |
217 | 269 |
218 // The same for loop without the '&' should not compile. | 270 // The same for loop without the '&' should not compile. |
219 for (auto& snippet : *service()) { | 271 for (auto& snippet : *service()) { |
220 // Snippet here is a const. | 272 // Snippet here is a const. |
221 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); | 273 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
222 EXPECT_EQ(snippet.site_title(), "Site Title"); | 274 EXPECT_EQ(snippet.best_source().publisher_name, "Foo News"); |
223 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon")); | |
224 EXPECT_EQ(snippet.title(), "Title"); | 275 EXPECT_EQ(snippet.title(), "Title"); |
225 EXPECT_EQ(snippet.snippet(), "Snippet"); | 276 EXPECT_EQ(snippet.snippet(), "Snippet"); |
226 EXPECT_EQ(snippet.salient_image_url(), | 277 EXPECT_EQ(snippet.salient_image_url(), |
227 GURL("http://localhost/salient_image")); | 278 GURL("http://localhost/salient_image")); |
228 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); | 279 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); |
229 EXPECT_EQ(snippet.amp_url(), GURL("http://localhost/amp")); | 280 EXPECT_EQ(snippet.best_source().amp_url.spec(), |
| 281 GURL("http://localhost/amp").spec()); |
230 } | 282 } |
231 } | 283 } |
232 | 284 |
233 TEST_F(NTPSnippetsServiceTest, Clear) { | 285 TEST_F(NTPSnippetsServiceTest, Clear) { |
234 std::string json_str(GetTestJson()); | 286 std::string json_str(GetTestJson()); |
235 | 287 |
236 LoadFromJSONString(json_str); | 288 LoadFromJSONString(json_str); |
237 EXPECT_EQ(service()->size(), 1u); | 289 EXPECT_EQ(service()->size(), 1u); |
238 | 290 |
239 service()->ClearSnippets(); | 291 service()->ClearSnippets(); |
240 EXPECT_EQ(service()->size(), 0u); | 292 EXPECT_EQ(service()->size(), 0u); |
241 } | 293 } |
242 | 294 |
243 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { | 295 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { |
244 std::string json_str( | 296 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); |
| 297 char json_str_format[] = |
245 "{ \"recos\": [ " | 298 "{ \"recos\": [ " |
246 " { \"contentInfo\": { \"url\" : \"http://first\" }}" | 299 "{ \"contentInfo\": {" |
247 "]}"); | 300 "\"url\" : \"%s\"," |
| 301 "\"title\" : \"Title\"," |
| 302 "\"snippet\" : \"Snippet\"," |
| 303 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," |
| 304 "\"creationTimestampSec\" : \"%s\"," |
| 305 "\"expiryTimestampSec\" : \"%s\"," |
| 306 "\"sourceCorpusInfo\" : [{\"corpusId\": \"http://first\"," |
| 307 "\"publisherData\": {" |
| 308 "\"sourceName\": \"Source 1\"" |
| 309 "}," |
| 310 "\"ampUrl\": \"\"}]" |
| 311 "}}" |
| 312 "]}"; |
| 313 std::string json_str(base::StringPrintf( |
| 314 json_str_format, "http://first", |
| 315 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), |
| 316 NTPSnippet::TimeToJsonString(expiry_time).c_str())); |
| 317 |
248 LoadFromJSONString(json_str); | 318 LoadFromJSONString(json_str); |
| 319 |
249 ASSERT_EQ(service()->size(), 1u); | 320 ASSERT_EQ(service()->size(), 1u); |
250 | 321 |
251 std::string json_str2( | 322 json_str = base::StringPrintf( |
252 "{ \"recos\": [ " | 323 json_str_format, "http://second", |
253 " { \"contentInfo\": { \"url\" : \"http://second\" }}" | 324 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), |
254 "]}"); | 325 NTPSnippet::TimeToJsonString(expiry_time).c_str()); |
255 LoadFromJSONString(json_str2); | 326 |
| 327 LoadFromJSONString(json_str); |
256 ASSERT_EQ(service()->size(), 2u); | 328 ASSERT_EQ(service()->size(), 2u); |
257 | 329 |
258 // The snippet loaded last should be at the first position in the list now. | 330 // The snippet loaded last should be at the first position in the list now. |
259 const NTPSnippet& first_snippet = *service()->begin(); | 331 const NTPSnippet& first_snippet = *service()->begin(); |
260 EXPECT_EQ(first_snippet.url(), GURL("http://second")); | 332 EXPECT_EQ(first_snippet.url(), GURL("http://second")); |
261 } | 333 } |
262 | 334 |
263 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { | 335 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { |
264 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); | 336 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); |
265 int snippets_per_load = max_snippet_count / 2 + 1; | 337 int snippets_per_load = max_snippet_count / 2 + 1; |
266 | 338 |
267 const char snippet_format[] = | 339 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); |
268 "{ \"contentInfo\": { \"url\" : \"http://localhost/%i\" }}"; | 340 char json_str_format[] = |
| 341 "{ \"contentInfo\": {" |
| 342 "\"url\" : \"http://localhost/%i\"," |
| 343 "\"title\" : \"Title\"," |
| 344 "\"snippet\" : \"Snippet\"," |
| 345 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," |
| 346 "\"creationTimestampSec\" : \"%s\"," |
| 347 "\"expiryTimestampSec\" : \"%s\"," |
| 348 "\"sourceCorpusInfo\" : [{\"corpusId\": \"http://first\"," |
| 349 "\"publisherData\": {" |
| 350 "\"sourceName\": \"Source 1\"" |
| 351 "}," |
| 352 "\"ampUrl\": \"\"}]" |
| 353 "}}"; |
| 354 |
269 std::vector<std::string> snippets1; | 355 std::vector<std::string> snippets1; |
270 std::vector<std::string> snippets2; | 356 std::vector<std::string> snippets2; |
271 for (int i = 0; i < snippets_per_load; i++) { | 357 for (int i = 0; i < snippets_per_load; i++) { |
272 snippets1.push_back(base::StringPrintf(snippet_format, i)); | 358 snippets1.push_back(base::StringPrintf( |
273 snippets2.push_back(base::StringPrintf(snippet_format, | 359 json_str_format, i, |
274 snippets_per_load + i)); | 360 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), |
| 361 NTPSnippet::TimeToJsonString(expiry_time).c_str())); |
| 362 snippets2.push_back(base::StringPrintf( |
| 363 json_str_format, snippets_per_load + i, |
| 364 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), |
| 365 NTPSnippet::TimeToJsonString(expiry_time).c_str())); |
275 } | 366 } |
276 | 367 |
277 LoadFromJSONString( | 368 LoadFromJSONString( |
278 "{ \"recos\": [ " + base::JoinString(snippets1, ", ") + "]}"); | 369 "{ \"recos\": [ " + base::JoinString(snippets1, ", ") + "]}"); |
279 ASSERT_EQ(snippets1.size(), service()->size()); | 370 ASSERT_EQ(snippets1.size(), service()->size()); |
280 | 371 |
281 LoadFromJSONString( | 372 LoadFromJSONString( |
282 "{ \"recos\": [ " + base::JoinString(snippets2, ", ") + "]}"); | 373 "{ \"recos\": [ " + base::JoinString(snippets2, ", ") + "]}"); |
283 EXPECT_EQ(max_snippet_count, (int)service()->size()); | 374 EXPECT_EQ(max_snippet_count, (int)service()->size()); |
284 } | 375 } |
(...skipping 22 matching lines...) Expand all Loading... |
307 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { | 398 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { |
308 LoadFromJSONString(GetTestJson()); | 399 LoadFromJSONString(GetTestJson()); |
309 ASSERT_EQ(service()->size(), 1u); | 400 ASSERT_EQ(service()->size(), 1u); |
310 | 401 |
311 LoadFromJSONString(GetIncompleteJson()); | 402 LoadFromJSONString(GetIncompleteJson()); |
312 // This should not have changed the existing snippets. | 403 // This should not have changed the existing snippets. |
313 EXPECT_EQ(service()->size(), 1u); | 404 EXPECT_EQ(service()->size(), 1u); |
314 } | 405 } |
315 | 406 |
316 TEST_F(NTPSnippetsServiceTest, Discard) { | 407 TEST_F(NTPSnippetsServiceTest, Discard) { |
| 408 std::vector<std::string> source_urls, publishers, amp_urls; |
| 409 source_urls.push_back(std::string("http://site.com")); |
| 410 publishers.push_back(std::string("Source 1")); |
| 411 amp_urls.push_back(std::string()); |
317 std::string json_str( | 412 std::string json_str( |
318 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); | 413 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 414 |
319 LoadFromJSONString(json_str); | 415 LoadFromJSONString(json_str); |
320 | 416 |
321 ASSERT_EQ(1u, service()->size()); | 417 ASSERT_EQ(1u, service()->size()); |
322 | 418 |
323 // Discarding a non-existent snippet shouldn't do anything. | 419 // Discarding a non-existent snippet shouldn't do anything. |
324 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); | 420 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); |
325 EXPECT_EQ(1u, service()->size()); | 421 EXPECT_EQ(1u, service()->size()); |
326 | 422 |
327 // Discard the snippet. | 423 // Discard the snippet. |
328 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); | 424 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar"))); |
329 EXPECT_EQ(0u, service()->size()); | 425 EXPECT_EQ(0u, service()->size()); |
330 | 426 |
331 // Make sure that fetching the same snippet again does not re-add it. | 427 // Make sure that fetching the same snippet again does not re-add it. |
332 LoadFromJSONString(json_str); | 428 LoadFromJSONString(json_str); |
333 EXPECT_EQ(0u, service()->size()); | 429 EXPECT_EQ(0u, service()->size()); |
334 | 430 |
335 // The snippet should stay discarded even after re-creating the service. | 431 // The snippet should stay discarded even after re-creating the service. |
336 CreateSnippetsService(); | 432 CreateSnippetsService(); |
337 LoadFromJSONString(json_str); | 433 LoadFromJSONString(json_str); |
338 EXPECT_EQ(0u, service()->size()); | 434 EXPECT_EQ(0u, service()->size()); |
339 | 435 |
340 // The snippet can be added again after clearing discarded snippets. | 436 // The snippet can be added again after clearing discarded snippets. |
341 service()->ClearDiscardedSnippets(); | 437 service()->ClearDiscardedSnippets(); |
342 EXPECT_EQ(0u, service()->size()); | 438 EXPECT_EQ(0u, service()->size()); |
343 LoadFromJSONString(json_str); | 439 LoadFromJSONString(json_str); |
344 EXPECT_EQ(1u, service()->size()); | 440 EXPECT_EQ(1u, service()->size()); |
345 } | 441 } |
346 | 442 |
347 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { | 443 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { |
348 std::string json_str( | 444 LoadFromJSONString(GetTestJson()); |
349 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); | |
350 LoadFromJSONString(json_str); | |
351 | 445 |
352 // For the test, we need the snippet to get discarded. | 446 // For the test, we need the snippet to get discarded. |
353 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); | 447 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar"))); |
354 const NTPSnippetsService::NTPSnippetStorage& snippets = | 448 const NTPSnippetsService::NTPSnippetStorage& snippets = |
355 service()->discarded_snippets(); | 449 service()->discarded_snippets(); |
356 EXPECT_EQ(1u, snippets.size()); | 450 EXPECT_EQ(1u, snippets.size()); |
357 for (auto& snippet : snippets) { | 451 for (auto& snippet : snippets) { |
358 EXPECT_EQ(GURL("http://site.com"), snippet->url()); | 452 EXPECT_EQ(GURL("http://localhost/foobar"), snippet->url()); |
359 } | 453 } |
360 | 454 |
361 // There should be no discarded snippet after clearing the list. | 455 // There should be no discarded snippet after clearing the list. |
362 service()->ClearDiscardedSnippets(); | 456 service()->ClearDiscardedSnippets(); |
363 EXPECT_EQ(0u, service()->discarded_snippets().size()); | 457 EXPECT_EQ(0u, service()->discarded_snippets().size()); |
364 } | 458 } |
365 | 459 |
366 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { | 460 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { |
367 std::string json_str(GetTestJson("aaa1448459205")); | 461 std::string json_str(GetTestJson("aaa1448459205")); |
368 | 462 |
(...skipping 10 matching lines...) Expand all Loading... |
379 } | 473 } |
380 } | 474 } |
381 | 475 |
382 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 476 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
383 std::string json_str(GetTestExpiredJson()); | 477 std::string json_str(GetTestExpiredJson()); |
384 | 478 |
385 LoadFromJSONString(json_str); | 479 LoadFromJSONString(json_str); |
386 EXPECT_EQ(service()->size(), 0u); | 480 EXPECT_EQ(service()->size(), 0u); |
387 } | 481 } |
388 | 482 |
| 483 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { |
| 484 std::vector<std::string> source_urls, publishers, amp_urls; |
| 485 source_urls.push_back(std::string("http://source1.com")); |
| 486 publishers.push_back(std::string("Source 1")); |
| 487 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 488 std::string json_str( |
| 489 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 490 |
| 491 LoadFromJSONString(json_str); |
| 492 |
| 493 EXPECT_EQ(service()->size(), 1u); |
| 494 |
| 495 for (auto& snippet : *service()) { |
| 496 EXPECT_EQ(snippet.sources().size(), 1u); |
| 497 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 498 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 499 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 500 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 501 } |
| 502 } |
| 503 |
| 504 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { |
| 505 std::vector<std::string> source_urls, publishers, amp_urls; |
| 506 source_urls.push_back(std::string("aaaa")); |
| 507 publishers.push_back(std::string("Source 1")); |
| 508 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 509 std::string json_str( |
| 510 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 511 |
| 512 LoadFromJSONString(json_str); |
| 513 EXPECT_EQ(service()->size(), 0u); |
| 514 } |
| 515 |
| 516 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { |
| 517 std::vector<std::string> source_urls, publishers, amp_urls; |
| 518 source_urls.push_back(std::string("http://source1.com")); |
| 519 publishers.push_back(std::string()); |
| 520 amp_urls.push_back(std::string()); |
| 521 std::string json_str( |
| 522 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 523 |
| 524 LoadFromJSONString(json_str); |
| 525 EXPECT_EQ(service()->size(), 0u); |
| 526 } |
| 527 |
| 528 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { |
| 529 std::vector<std::string> source_urls, publishers, amp_urls; |
| 530 source_urls.push_back(std::string("http://source1.com")); |
| 531 source_urls.push_back(std::string("http://source2.com")); |
| 532 publishers.push_back(std::string("Source 1")); |
| 533 publishers.push_back(std::string("Source 2")); |
| 534 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 535 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 536 std::string json_str( |
| 537 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 538 |
| 539 LoadFromJSONString(json_str); |
| 540 EXPECT_EQ(service()->size(), 1u); |
| 541 |
| 542 // Expect the first source to be chosen |
| 543 for (auto& snippet : *service()) { |
| 544 EXPECT_EQ(snippet.sources().size(), 2u); |
| 545 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 546 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 547 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 548 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 549 } |
| 550 } |
| 551 |
| 552 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { |
| 553 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name |
| 554 // Source 2 should win since we favor publisher name over amp url |
| 555 std::vector<std::string> source_urls, publishers, amp_urls; |
| 556 source_urls.push_back(std::string("http://source1.com")); |
| 557 source_urls.push_back(std::string("http://source2.com")); |
| 558 publishers.push_back(std::string()); |
| 559 publishers.push_back(std::string("Source 2")); |
| 560 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 561 amp_urls.push_back(std::string()); |
| 562 std::string json_str( |
| 563 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 564 |
| 565 LoadFromJSONString(json_str); |
| 566 EXPECT_EQ(service()->size(), 1u); |
| 567 |
| 568 for (auto& snippet : *service()) { |
| 569 EXPECT_EQ(snippet.sources().size(), 2u); |
| 570 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 571 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
| 572 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 573 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 574 } |
| 575 |
| 576 service()->ClearSnippets(); |
| 577 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name |
| 578 // Source 1 should win in this case since we prefer publisher name to AMP url |
| 579 source_urls.clear(); |
| 580 source_urls.push_back(std::string("http://source1.com")); |
| 581 source_urls.push_back(std::string("http://source2.com")); |
| 582 publishers.clear(); |
| 583 publishers.push_back(std::string("Source 1")); |
| 584 publishers.push_back(std::string()); |
| 585 amp_urls.clear(); |
| 586 amp_urls.push_back(std::string()); |
| 587 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 588 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 589 |
| 590 LoadFromJSONString(json_str); |
| 591 EXPECT_EQ(service()->size(), 1u); |
| 592 |
| 593 for (auto& snippet : *service()) { |
| 594 EXPECT_EQ(snippet.sources().size(), 2u); |
| 595 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 596 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 597 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 598 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 599 } |
| 600 |
| 601 service()->ClearSnippets(); |
| 602 // Set source 1 to have no AMP url and no source, and source 2 to only have |
| 603 // amp url. There should be no snippets since we only add sources we consider |
| 604 // complete |
| 605 source_urls.clear(); |
| 606 source_urls.push_back(std::string("http://source1.com")); |
| 607 source_urls.push_back(std::string("http://source2.com")); |
| 608 publishers.clear(); |
| 609 publishers.push_back(std::string()); |
| 610 publishers.push_back(std::string()); |
| 611 amp_urls.clear(); |
| 612 amp_urls.push_back(std::string()); |
| 613 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 614 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 615 |
| 616 LoadFromJSONString(json_str); |
| 617 EXPECT_EQ(service()->size(), 0u); |
| 618 } |
| 619 |
| 620 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { |
| 621 // Test 2 complete sources, we should choose the first complete source |
| 622 std::vector<std::string> source_urls, publishers, amp_urls; |
| 623 source_urls.push_back(std::string("http://source1.com")); |
| 624 source_urls.push_back(std::string("http://source2.com")); |
| 625 source_urls.push_back(std::string("http://source3.com")); |
| 626 publishers.push_back(std::string("Source 1")); |
| 627 publishers.push_back(std::string()); |
| 628 publishers.push_back(std::string("Source 3")); |
| 629 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 630 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 631 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 632 std::string json_str( |
| 633 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 634 |
| 635 LoadFromJSONString(json_str); |
| 636 EXPECT_EQ(service()->size(), 1u); |
| 637 |
| 638 for (auto& snippet : *service()) { |
| 639 EXPECT_EQ(snippet.sources().size(), 3u); |
| 640 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 641 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 642 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 643 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 644 } |
| 645 |
| 646 // Test 2 complete sources, we should choose the first complete source |
| 647 service()->ClearSnippets(); |
| 648 source_urls.clear(); |
| 649 source_urls.push_back(std::string("http://source1.com")); |
| 650 source_urls.push_back(std::string("http://source2.com")); |
| 651 source_urls.push_back(std::string("http://source3.com")); |
| 652 publishers.clear(); |
| 653 publishers.push_back(std::string()); |
| 654 publishers.push_back(std::string("Source 2")); |
| 655 publishers.push_back(std::string("Source 3")); |
| 656 amp_urls.clear(); |
| 657 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 658 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 659 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 660 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 661 |
| 662 LoadFromJSONString(json_str); |
| 663 EXPECT_EQ(service()->size(), 1u); |
| 664 |
| 665 for (auto& snippet : *service()) { |
| 666 EXPECT_EQ(snippet.sources().size(), 3u); |
| 667 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 668 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
| 669 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 670 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
| 671 } |
| 672 |
| 673 // Test 3 complete sources, we should choose the first complete source |
| 674 service()->ClearSnippets(); |
| 675 source_urls.clear(); |
| 676 source_urls.push_back(std::string("http://source1.com")); |
| 677 source_urls.push_back(std::string("http://source2.com")); |
| 678 source_urls.push_back(std::string("http://source3.com")); |
| 679 publishers.clear(); |
| 680 publishers.push_back(std::string("Source 1")); |
| 681 publishers.push_back(std::string("Source 2")); |
| 682 publishers.push_back(std::string("Source 3")); |
| 683 amp_urls.clear(); |
| 684 amp_urls.push_back(std::string()); |
| 685 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 686 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 687 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 688 |
| 689 LoadFromJSONString(json_str); |
| 690 EXPECT_EQ(service()->size(), 1u); |
| 691 |
| 692 for (auto& snippet : *service()) { |
| 693 EXPECT_EQ(snippet.sources().size(), 3u); |
| 694 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 695 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
| 696 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 697 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
| 698 } |
| 699 } |
389 } // namespace ntp_snippets | 700 } // namespace ntp_snippets |
OLD | NEW |