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) { |
| 296 std::vector<std::string> source_urls; |
| 297 source_urls.push_back(std::string("http://first")); |
| 298 std::vector<std::string> publishers; |
| 299 publishers.push_back(std::string("Source 1")); |
| 300 std::vector<std::string> amp_urls; |
| 301 amp_urls.push_back(std::string("")); |
244 std::string json_str( | 302 std::string json_str( |
245 "{ \"recos\": [ " | 303 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
246 " { \"contentInfo\": { \"url\" : \"http://first\" }}" | 304 |
247 "]}"); | |
248 LoadFromJSONString(json_str); | 305 LoadFromJSONString(json_str); |
| 306 |
249 ASSERT_EQ(service()->size(), 1u); | 307 ASSERT_EQ(service()->size(), 1u); |
250 | 308 |
251 std::string json_str2( | 309 source_urls.clear(); |
252 "{ \"recos\": [ " | 310 source_urls.push_back(std::string("http://second")); |
253 " { \"contentInfo\": { \"url\" : \"http://second\" }}" | 311 publishers.clear(); |
254 "]}"); | 312 publishers.push_back(std::string("Source 1")); |
255 LoadFromJSONString(json_str2); | 313 amp_urls.clear(); |
| 314 amp_urls.push_back(std::string("")); |
| 315 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 316 |
| 317 LoadFromJSONString(json_str); |
256 ASSERT_EQ(service()->size(), 2u); | 318 ASSERT_EQ(service()->size(), 2u); |
257 | 319 |
258 // The snippet loaded last should be at the first position in the list now. | 320 // The snippet loaded last should be at the first position in the list now. |
259 const NTPSnippet& first_snippet = *service()->begin(); | 321 const NTPSnippet& first_snippet = *service()->begin(); |
260 EXPECT_EQ(first_snippet.url(), GURL("http://second")); | 322 EXPECT_EQ(first_snippet.url(), GURL("http://second")); |
261 } | 323 } |
262 | 324 |
263 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { | 325 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { |
264 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); | 326 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); |
265 int snippets_per_load = max_snippet_count / 2 + 1; | 327 int snippets_per_load = max_snippet_count / 2 + 1; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { | 369 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { |
308 LoadFromJSONString(GetTestJson()); | 370 LoadFromJSONString(GetTestJson()); |
309 ASSERT_EQ(service()->size(), 1u); | 371 ASSERT_EQ(service()->size(), 1u); |
310 | 372 |
311 LoadFromJSONString(GetIncompleteJson()); | 373 LoadFromJSONString(GetIncompleteJson()); |
312 // This should not have changed the existing snippets. | 374 // This should not have changed the existing snippets. |
313 EXPECT_EQ(service()->size(), 1u); | 375 EXPECT_EQ(service()->size(), 1u); |
314 } | 376 } |
315 | 377 |
316 TEST_F(NTPSnippetsServiceTest, Discard) { | 378 TEST_F(NTPSnippetsServiceTest, Discard) { |
| 379 std::vector<std::string> source_urls; |
| 380 source_urls.push_back(std::string("http://site.com")); |
| 381 std::vector<std::string> publishers; |
| 382 publishers.push_back(std::string("Source 1")); |
| 383 std::vector<std::string> amp_urls; |
| 384 amp_urls.push_back(std::string("")); |
317 std::string json_str( | 385 std::string json_str( |
318 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); | 386 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 387 |
319 LoadFromJSONString(json_str); | 388 LoadFromJSONString(json_str); |
320 | 389 |
321 ASSERT_EQ(1u, service()->size()); | 390 ASSERT_EQ(1u, service()->size()); |
322 | 391 |
323 // Discarding a non-existent snippet shouldn't do anything. | 392 // Discarding a non-existent snippet shouldn't do anything. |
324 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); | 393 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); |
325 EXPECT_EQ(1u, service()->size()); | 394 EXPECT_EQ(1u, service()->size()); |
326 | 395 |
327 // Discard the snippet. | 396 // Discard the snippet. |
328 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); | 397 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); |
329 EXPECT_EQ(0u, service()->size()); | 398 EXPECT_EQ(0u, service()->size()); |
330 | 399 |
331 // Make sure that fetching the same snippet again does not re-add it. | 400 // Make sure that fetching the same snippet again does not re-add it. |
332 LoadFromJSONString(json_str); | 401 LoadFromJSONString(json_str); |
333 EXPECT_EQ(0u, service()->size()); | 402 EXPECT_EQ(0u, service()->size()); |
334 | 403 |
335 // The snippet should stay discarded even after re-creating the service. | 404 // The snippet should stay discarded even after re-creating the service. |
336 CreateSnippetsService(); | 405 CreateSnippetsService(); |
337 LoadFromJSONString(json_str); | 406 LoadFromJSONString(json_str); |
338 EXPECT_EQ(0u, service()->size()); | 407 EXPECT_EQ(0u, service()->size()); |
339 | 408 |
340 // The snippet can be added again after clearing discarded snippets. | 409 // The snippet can be added again after clearing discarded snippets. |
341 service()->ClearDiscardedSnippets(); | 410 service()->ClearDiscardedSnippets(); |
342 EXPECT_EQ(0u, service()->size()); | 411 EXPECT_EQ(0u, service()->size()); |
343 LoadFromJSONString(json_str); | 412 LoadFromJSONString(json_str); |
344 EXPECT_EQ(1u, service()->size()); | 413 EXPECT_EQ(1u, service()->size()); |
345 } | 414 } |
346 | 415 |
347 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { | 416 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { |
348 std::string json_str( | 417 LoadFromJSONString(GetTestJson()); |
349 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); | |
350 LoadFromJSONString(json_str); | |
351 | 418 |
352 // For the test, we need the snippet to get discarded. | 419 // For the test, we need the snippet to get discarded. |
353 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); | 420 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar"))); |
354 const NTPSnippetsService::NTPSnippetStorage& snippets = | 421 const NTPSnippetsService::NTPSnippetStorage& snippets = |
355 service()->discarded_snippets(); | 422 service()->discarded_snippets(); |
356 EXPECT_EQ(1u, snippets.size()); | 423 EXPECT_EQ(1u, snippets.size()); |
357 for (auto& snippet : snippets) { | 424 for (auto& snippet : snippets) { |
358 EXPECT_EQ(GURL("http://site.com"), snippet->url()); | 425 EXPECT_EQ(GURL("http://localhost/foobar"), snippet->url()); |
359 } | 426 } |
360 | 427 |
361 // There should be no discarded snippet after clearing the list. | 428 // There should be no discarded snippet after clearing the list. |
362 service()->ClearDiscardedSnippets(); | 429 service()->ClearDiscardedSnippets(); |
363 EXPECT_EQ(0u, service()->discarded_snippets().size()); | 430 EXPECT_EQ(0u, service()->discarded_snippets().size()); |
364 } | 431 } |
365 | 432 |
366 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { | 433 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { |
367 std::string json_str(GetTestJson("aaa1448459205")); | 434 std::string json_str(GetTestJson("aaa1448459205")); |
368 | 435 |
(...skipping 10 matching lines...) Expand all Loading... |
379 } | 446 } |
380 } | 447 } |
381 | 448 |
382 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 449 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
383 std::string json_str(GetTestExpiredJson()); | 450 std::string json_str(GetTestExpiredJson()); |
384 | 451 |
385 LoadFromJSONString(json_str); | 452 LoadFromJSONString(json_str); |
386 EXPECT_EQ(service()->size(), 0u); | 453 EXPECT_EQ(service()->size(), 0u); |
387 } | 454 } |
388 | 455 |
| 456 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { |
| 457 std::vector<std::string> source_urls; |
| 458 source_urls.push_back(std::string("http://source1.com")); |
| 459 std::vector<std::string> publishers; |
| 460 publishers.push_back(std::string("Source 1")); |
| 461 std::vector<std::string> amp_urls; |
| 462 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 463 std::string json_str( |
| 464 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 465 |
| 466 LoadFromJSONString(json_str); |
| 467 EXPECT_EQ(service()->size(), 1u); |
| 468 |
| 469 for (auto& snippet : *service()) { |
| 470 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); |
| 471 EXPECT_EQ(snippet.sources().size(), 1u); |
| 472 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 473 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 474 } |
| 475 } |
| 476 |
| 477 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { |
| 478 std::vector<std::string> source_urls; |
| 479 source_urls.push_back(std::string("aaaa")); |
| 480 std::vector<std::string> publishers; |
| 481 publishers.push_back(std::string("Source 1")); |
| 482 std::vector<std::string> amp_urls; |
| 483 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 484 std::string json_str( |
| 485 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 486 |
| 487 LoadFromJSONString(json_str); |
| 488 // If we're running a release build, we only add snippets that have a well- |
| 489 // formed source url |
| 490 #if !DCHECK_IS_ON() |
| 491 EXPECT_EQ(service()->size(), 0u); |
| 492 #else |
| 493 EXPECT_EQ(service()->size(), 1u); |
| 494 |
| 495 for (auto& snippet : *service()) { |
| 496 EXPECT_EQ(snippet.sources().size(), 0u); |
| 497 } |
| 498 #endif |
| 499 } |
| 500 |
| 501 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { |
| 502 std::vector<std::string> source_urls; |
| 503 source_urls.push_back(std::string("http://source1.com")); |
| 504 std::vector<std::string> publishers; |
| 505 publishers.push_back(std::string("")); |
| 506 std::vector<std::string> amp_urls; |
| 507 amp_urls.push_back(std::string("")); |
| 508 std::string json_str( |
| 509 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 510 |
| 511 LoadFromJSONString(json_str); |
| 512 // If we're running a release build, we only add snippets have a source url |
| 513 // and publisher |
| 514 #if !DCHECK_IS_ON() |
| 515 EXPECT_EQ(service()->size(), 0u); |
| 516 #else |
| 517 EXPECT_EQ(service()->size(), 1u); |
| 518 |
| 519 for (auto& snippet : *service()) { |
| 520 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); |
| 521 EXPECT_EQ(snippet.best_source().publisher_name, std::string()); |
| 522 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 523 EXPECT_EQ(snippet.sources().size(), 1u); |
| 524 } |
| 525 #endif |
| 526 } |
| 527 |
| 528 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { |
| 529 std::vector<std::string> source_urls; |
| 530 source_urls.push_back(std::string("http://source1.com")); |
| 531 source_urls.push_back(std::string("http://source2.com")); |
| 532 std::vector<std::string> publishers; |
| 533 publishers.push_back(std::string("Source 1")); |
| 534 publishers.push_back(std::string("Source 2")); |
| 535 std::vector<std::string> amp_urls; |
| 536 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 537 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 538 std::string json_str( |
| 539 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 540 |
| 541 LoadFromJSONString(json_str); |
| 542 EXPECT_EQ(service()->size(), 1u); |
| 543 |
| 544 // Expect the first source to be chosen |
| 545 for (auto& snippet : *service()) { |
| 546 EXPECT_EQ(snippet.sources().size(), 2u); |
| 547 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); |
| 548 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 549 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 550 } |
| 551 } |
| 552 |
| 553 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { |
| 554 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name |
| 555 // Source 2 should win since we favor publisher name over amp url |
| 556 std::vector<std::string> source_urls; |
| 557 source_urls.push_back(std::string("http://source1.com")); |
| 558 source_urls.push_back(std::string("http://source2.com")); |
| 559 std::vector<std::string> publishers; |
| 560 publishers.push_back(std::string("")); |
| 561 publishers.push_back(std::string("Source 2")); |
| 562 std::vector<std::string> amp_urls; |
| 563 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 564 amp_urls.push_back(std::string("")); |
| 565 std::string json_str( |
| 566 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 567 |
| 568 LoadFromJSONString(json_str); |
| 569 EXPECT_EQ(service()->size(), 1u); |
| 570 |
| 571 for (auto& snippet : *service()) { |
| 572 EXPECT_EQ(snippet.sources().size(), 2u); |
| 573 EXPECT_EQ(snippet.url(), GURL("http://source2.com")); |
| 574 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 575 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 576 } |
| 577 |
| 578 service()->ClearSnippets(); |
| 579 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name |
| 580 // Source 1 should win in this case since we prefer publisher name to AMP url |
| 581 source_urls.clear(); |
| 582 source_urls.push_back(std::string("http://source1.com")); |
| 583 source_urls.push_back(std::string("http://source2.com")); |
| 584 publishers.clear(); |
| 585 publishers.push_back(std::string("Source 1")); |
| 586 publishers.push_back(std::string("")); |
| 587 amp_urls.clear(); |
| 588 amp_urls.push_back(std::string("")); |
| 589 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 590 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 591 |
| 592 LoadFromJSONString(json_str); |
| 593 EXPECT_EQ(service()->size(), 1u); |
| 594 |
| 595 for (auto& snippet : *service()) { |
| 596 EXPECT_EQ(snippet.sources().size(), 2u); |
| 597 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); |
| 598 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 599 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 600 } |
| 601 |
| 602 service()->ClearSnippets(); |
| 603 // Set source 1 to have no AMP url and no source. Source 1 should win since |
| 604 // all sources without publisher name are ranked equally |
| 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 #if !DCHECK_IS_ON() |
| 618 EXPECT_EQ(service()->size(), 0u); |
| 619 #else |
| 620 EXPECT_EQ(service()->size(), 1u); |
| 621 for (auto& snippet : *service()) { |
| 622 EXPECT_EQ(snippet.sources().size(), 2u); |
| 623 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); |
| 624 EXPECT_EQ(snippet.best_source().publisher_name, std::string()); |
| 625 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 626 } |
| 627 #endif |
| 628 } |
| 629 |
| 630 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { |
| 631 // Test 2 complete sources, we should choose the first complete source |
| 632 std::vector<std::string> source_urls; |
| 633 source_urls.push_back(std::string("http://source1.com")); |
| 634 source_urls.push_back(std::string("http://source2.com")); |
| 635 source_urls.push_back(std::string("http://source3.com")); |
| 636 std::vector<std::string> publishers; |
| 637 publishers.push_back(std::string("Source 1")); |
| 638 publishers.push_back(std::string("")); |
| 639 publishers.push_back(std::string("Source 3")); |
| 640 std::vector<std::string> amp_urls; |
| 641 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 642 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 643 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 644 std::string json_str( |
| 645 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 646 |
| 647 LoadFromJSONString(json_str); |
| 648 EXPECT_EQ(service()->size(), 1u); |
| 649 |
| 650 for (auto& snippet : *service()) { |
| 651 EXPECT_EQ(snippet.sources().size(), 3u); |
| 652 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); |
| 653 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 654 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 655 } |
| 656 |
| 657 // Test 2 complete sources, we should choose the first complete source |
| 658 service()->ClearSnippets(); |
| 659 source_urls.clear(); |
| 660 source_urls.push_back(std::string("http://source1.com")); |
| 661 source_urls.push_back(std::string("http://source2.com")); |
| 662 source_urls.push_back(std::string("http://source3.com")); |
| 663 publishers.clear(); |
| 664 publishers.push_back(std::string("")); |
| 665 publishers.push_back(std::string("Source 2")); |
| 666 publishers.push_back(std::string("Source 3")); |
| 667 amp_urls.clear(); |
| 668 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 669 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 670 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 671 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 672 |
| 673 LoadFromJSONString(json_str); |
| 674 EXPECT_EQ(service()->size(), 1u); |
| 675 |
| 676 for (auto& snippet : *service()) { |
| 677 EXPECT_EQ(snippet.sources().size(), 3u); |
| 678 EXPECT_EQ(snippet.url(), GURL("http://source2.com")); |
| 679 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 680 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
| 681 } |
| 682 |
| 683 // Test 3 complete sources, we should choose the first complete source |
| 684 service()->ClearSnippets(); |
| 685 source_urls.clear(); |
| 686 source_urls.push_back(std::string("http://source1.com")); |
| 687 source_urls.push_back(std::string("http://source2.com")); |
| 688 source_urls.push_back(std::string("http://source3.com")); |
| 689 publishers.clear(); |
| 690 publishers.push_back(std::string("Source 1")); |
| 691 publishers.push_back(std::string("Source 2")); |
| 692 publishers.push_back(std::string("Source 3")); |
| 693 amp_urls.clear(); |
| 694 amp_urls.push_back(std::string("")); |
| 695 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 696 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 697 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); |
| 698 |
| 699 LoadFromJSONString(json_str); |
| 700 EXPECT_EQ(service()->size(), 1u); |
| 701 |
| 702 for (auto& snippet : *service()) { |
| 703 EXPECT_EQ(snippet.sources().size(), 3u); |
| 704 EXPECT_EQ(snippet.url(), GURL("http://source2.com")); |
| 705 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 706 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
| 707 } |
| 708 } |
389 } // namespace ntp_snippets | 709 } // namespace ntp_snippets |
OLD | NEW |