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 <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 using testing::_; | 57 using testing::_; |
58 | 58 |
59 namespace ntp_snippets { | 59 namespace ntp_snippets { |
60 | 60 |
61 namespace { | 61 namespace { |
62 | 62 |
63 MATCHER_P(IdEq, value, "") { | 63 MATCHER_P(IdEq, value, "") { |
64 return arg->id() == value; | 64 return arg->id() == value; |
65 } | 65 } |
66 | 66 |
67 MATCHER_P(IsCategory, id, "") { | |
68 return arg.id() == static_cast<int>(id); | |
69 } | |
70 | |
71 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; | 67 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; |
72 const char kTestContentSnippetsServerFormat[] = | 68 const char kTestContentSnippetsServerFormat[] = |
73 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; | 69 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; |
74 | 70 |
75 const char kSnippetUrl[] = "http://localhost/foobar"; | 71 const char kSnippetUrl[] = "http://localhost/foobar"; |
76 const char kSnippetTitle[] = "Title"; | 72 const char kSnippetTitle[] = "Title"; |
77 const char kSnippetText[] = "Snippet"; | 73 const char kSnippetText[] = "Snippet"; |
78 const char kSnippetSalientImage[] = "http://localhost/salient_image"; | 74 const char kSnippetSalientImage[] = "http://localhost/salient_image"; |
79 const char kSnippetPublisherName[] = "Foo News"; | 75 const char kSnippetPublisherName[] = "Foo News"; |
80 const char kSnippetAmpUrl[] = "http://localhost/amp"; | 76 const char kSnippetAmpUrl[] = "http://localhost/amp"; |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 | 427 |
432 // When we have no snippets are all, loading the service initiates a fetch. | 428 // When we have no snippets are all, loading the service initiates a fetch. |
433 base::RunLoop().RunUntilIdle(); | 429 base::RunLoop().RunUntilIdle(); |
434 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); | 430 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); |
435 } | 431 } |
436 | 432 |
437 TEST_F(NTPSnippetsServiceTest, Full) { | 433 TEST_F(NTPSnippetsServiceTest, Full) { |
438 std::string json_str(GetTestJson({GetSnippet()})); | 434 std::string json_str(GetTestJson({GetSnippet()})); |
439 | 435 |
440 LoadFromJSONString(json_str); | 436 LoadFromJSONString(json_str); |
441 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 437 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
442 const NTPSnippet& snippet = | 438 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
443 *service()->GetSnippetsForTesting(articles_category()).front(); | |
444 | 439 |
445 EXPECT_EQ(snippet.id(), kSnippetUrl); | 440 EXPECT_EQ(snippet.id(), kSnippetUrl); |
446 EXPECT_EQ(snippet.title(), kSnippetTitle); | 441 EXPECT_EQ(snippet.title(), kSnippetTitle); |
447 EXPECT_EQ(snippet.snippet(), kSnippetText); | 442 EXPECT_EQ(snippet.snippet(), kSnippetText); |
448 EXPECT_EQ(snippet.salient_image_url(), GURL(kSnippetSalientImage)); | 443 EXPECT_EQ(snippet.salient_image_url(), GURL(kSnippetSalientImage)); |
449 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); | 444 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); |
450 EXPECT_EQ(snippet.best_source().publisher_name, kSnippetPublisherName); | 445 EXPECT_EQ(snippet.best_source().publisher_name, kSnippetPublisherName); |
451 EXPECT_EQ(snippet.best_source().amp_url, GURL(kSnippetAmpUrl)); | 446 EXPECT_EQ(snippet.best_source().amp_url, GURL(kSnippetAmpUrl)); |
452 } | 447 } |
453 | 448 |
454 TEST_F(NTPSnippetsServiceTest, Clear) { | 449 TEST_F(NTPSnippetsServiceTest, Clear) { |
455 std::string json_str(GetTestJson({GetSnippet()})); | 450 std::string json_str(GetTestJson({GetSnippet()})); |
456 | 451 |
457 LoadFromJSONString(json_str); | 452 LoadFromJSONString(json_str); |
458 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 453 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
459 | 454 |
460 service()->ClearCachedSuggestions(articles_category()); | 455 service()->ClearCachedSuggestions(articles_category()); |
461 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 456 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
462 } | 457 } |
463 | 458 |
464 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { | 459 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { |
465 std::string first("http://first"); | 460 std::string first("http://first"); |
466 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)})); | 461 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)})); |
467 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), | 462 EXPECT_THAT(service()->GetSnippetsForTesting(), ElementsAre(IdEq(first))); |
468 ElementsAre(IdEq(first))); | |
469 | 463 |
470 std::string second("http://second"); | 464 std::string second("http://second"); |
471 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)})); | 465 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)})); |
472 // The snippet loaded last should be at the first position in the list now. | 466 // The snippet loaded last should be at the first position in the list now. |
473 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), | 467 EXPECT_THAT(service()->GetSnippetsForTesting(), |
474 ElementsAre(IdEq(second), IdEq(first))); | 468 ElementsAre(IdEq(second), IdEq(first))); |
475 } | 469 } |
476 | 470 |
477 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { | 471 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { |
478 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); | 472 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); |
479 int snippets_per_load = max_snippet_count / 2 + 1; | 473 int snippets_per_load = max_snippet_count / 2 + 1; |
480 char url_format[] = "http://localhost/%i"; | 474 char url_format[] = "http://localhost/%i"; |
481 | 475 |
482 std::vector<std::string> snippets1; | 476 std::vector<std::string> snippets1; |
483 std::vector<std::string> snippets2; | 477 std::vector<std::string> snippets2; |
484 for (int i = 0; i < snippets_per_load; i++) { | 478 for (int i = 0; i < snippets_per_load; i++) { |
485 snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i))); | 479 snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i))); |
486 snippets2.push_back(GetSnippetWithUrl( | 480 snippets2.push_back(GetSnippetWithUrl( |
487 base::StringPrintf(url_format, snippets_per_load + i))); | 481 base::StringPrintf(url_format, snippets_per_load + i))); |
488 } | 482 } |
489 | 483 |
490 LoadFromJSONString(GetTestJson(snippets1)); | 484 LoadFromJSONString(GetTestJson(snippets1)); |
491 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), | 485 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(snippets1.size())); |
492 SizeIs(snippets1.size())); | |
493 | 486 |
494 LoadFromJSONString(GetTestJson(snippets2)); | 487 LoadFromJSONString(GetTestJson(snippets2)); |
495 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), | 488 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(max_snippet_count)); |
496 SizeIs(max_snippet_count)); | |
497 } | 489 } |
498 | 490 |
499 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 491 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
500 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); | 492 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); |
501 EXPECT_THAT(service()->snippets_fetcher()->last_status(), | 493 EXPECT_THAT(service()->snippets_fetcher()->last_status(), |
502 StartsWith("Received invalid JSON")); | 494 StartsWith("Received invalid JSON")); |
503 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 495 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
504 } | 496 } |
505 | 497 |
506 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 498 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { |
507 LoadFromJSONString(GetTestJson({GetSnippet()})); | 499 LoadFromJSONString(GetTestJson({GetSnippet()})); |
508 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 500 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
509 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); | 501 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); |
510 | 502 |
511 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); | 503 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); |
512 EXPECT_THAT(service()->snippets_fetcher()->last_status(), | 504 EXPECT_THAT(service()->snippets_fetcher()->last_status(), |
513 StartsWith("Received invalid JSON")); | 505 StartsWith("Received invalid JSON")); |
514 // This should not have changed the existing snippets. | 506 // This should not have changed the existing snippets. |
515 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 507 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
516 } | 508 } |
517 | 509 |
518 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) { | 510 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) { |
519 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); | 511 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); |
520 EXPECT_EQ("Invalid / empty list.", | 512 EXPECT_EQ("Invalid / empty list.", |
521 service()->snippets_fetcher()->last_status()); | 513 service()->snippets_fetcher()->last_status()); |
522 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 514 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
523 } | 515 } |
524 | 516 |
525 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { | 517 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { |
526 LoadFromJSONString(GetTestJson({GetSnippet()})); | 518 LoadFromJSONString(GetTestJson({GetSnippet()})); |
527 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 519 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
528 | 520 |
529 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); | 521 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); |
530 EXPECT_EQ("Invalid / empty list.", | 522 EXPECT_EQ("Invalid / empty list.", |
531 service()->snippets_fetcher()->last_status()); | 523 service()->snippets_fetcher()->last_status()); |
532 // This should not have changed the existing snippets. | 524 // This should not have changed the existing snippets. |
533 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 525 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
534 } | 526 } |
535 | 527 |
536 TEST_F(NTPSnippetsServiceTest, Dismiss) { | 528 TEST_F(NTPSnippetsServiceTest, Dismiss) { |
537 std::vector<std::string> source_urls, publishers, amp_urls; | 529 std::vector<std::string> source_urls, publishers, amp_urls; |
538 source_urls.push_back(std::string("http://site.com")); | 530 source_urls.push_back(std::string("http://site.com")); |
539 publishers.push_back(std::string("Source 1")); | 531 publishers.push_back(std::string("Source 1")); |
540 amp_urls.push_back(std::string()); | 532 amp_urls.push_back(std::string()); |
541 std::string json_str( | 533 std::string json_str( |
542 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 534 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
543 | 535 |
544 LoadFromJSONString(json_str); | 536 LoadFromJSONString(json_str); |
545 | 537 |
546 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 538 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
547 | 539 |
548 // Dismissing a non-existent snippet shouldn't do anything. | 540 // Dismissing a non-existent snippet shouldn't do anything. |
549 service()->DismissSuggestion(MakeUniqueID("http://othersite.com")); | 541 service()->DismissSuggestion(MakeUniqueID("http://othersite.com")); |
550 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 542 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
551 | 543 |
552 // Dismiss the snippet. | 544 // Dismiss the snippet. |
553 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); | 545 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); |
554 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 546 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
555 | 547 |
556 // Make sure that fetching the same snippet again does not re-add it. | 548 // Make sure that fetching the same snippet again does not re-add it. |
557 LoadFromJSONString(json_str); | 549 LoadFromJSONString(json_str); |
558 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 550 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
559 | 551 |
560 // The snippet should stay dismissed even after re-creating the service. | 552 // The snippet should stay dismissed even after re-creating the service. |
561 RecreateSnippetsService(); | 553 RecreateSnippetsService(); |
562 LoadFromJSONString(json_str); | 554 LoadFromJSONString(json_str); |
563 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 555 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
564 | 556 |
565 // The snippet can be added again after clearing dismissed snippets. | 557 // The snippet can be added again after clearing dismissed snippets. |
566 service()->ClearDismissedSuggestionsForDebugging(articles_category()); | 558 service()->ClearDismissedSuggestionsForDebugging(articles_category()); |
567 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 559 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
568 LoadFromJSONString(json_str); | 560 LoadFromJSONString(json_str); |
569 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 561 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
570 } | 562 } |
571 | 563 |
572 TEST_F(NTPSnippetsServiceTest, GetDismissed) { | 564 TEST_F(NTPSnippetsServiceTest, GetDismissed) { |
573 LoadFromJSONString(GetTestJson({GetSnippet()})); | 565 LoadFromJSONString(GetTestJson({GetSnippet()})); |
574 | 566 |
575 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); | 567 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); |
576 | 568 |
577 MockDismissedSuggestionsCallback callback; | 569 MockDismissedSuggestionsCallback callback; |
578 | 570 |
579 EXPECT_CALL(callback, MockRun(_, _)) | 571 EXPECT_CALL(callback, MockRun(_, _)) |
(...skipping 24 matching lines...) Expand all Loading... |
604 base::Bind(&MockDismissedSuggestionsCallback::Run, | 596 base::Bind(&MockDismissedSuggestionsCallback::Run, |
605 base::Unretained(&callback), articles_category())); | 597 base::Unretained(&callback), articles_category())); |
606 } | 598 } |
607 | 599 |
608 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { | 600 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { |
609 std::string json_str(GetTestJson({GetSnippetWithTimes( | 601 std::string json_str(GetTestJson({GetSnippetWithTimes( |
610 "aaa1448459205", | 602 "aaa1448459205", |
611 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); | 603 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); |
612 | 604 |
613 LoadFromJSONString(json_str); | 605 LoadFromJSONString(json_str); |
614 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 606 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
615 const NTPSnippet& snippet = | 607 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
616 *service()->GetSnippetsForTesting(articles_category()).front(); | |
617 EXPECT_EQ(snippet.id(), kSnippetUrl); | 608 EXPECT_EQ(snippet.id(), kSnippetUrl); |
618 EXPECT_EQ(snippet.title(), kSnippetTitle); | 609 EXPECT_EQ(snippet.title(), kSnippetTitle); |
619 EXPECT_EQ(snippet.snippet(), kSnippetText); | 610 EXPECT_EQ(snippet.snippet(), kSnippetText); |
620 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); | 611 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); |
621 } | 612 } |
622 | 613 |
623 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 614 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
624 std::string json_str(GetTestJson({GetExpiredSnippet()})); | 615 std::string json_str(GetTestJson({GetExpiredSnippet()})); |
625 | 616 |
626 LoadFromJSONString(json_str); | 617 LoadFromJSONString(json_str); |
627 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 618 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
628 } | 619 } |
629 | 620 |
630 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { | 621 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { |
631 std::vector<std::string> source_urls, publishers, amp_urls; | 622 std::vector<std::string> source_urls, publishers, amp_urls; |
632 source_urls.push_back(std::string("http://source1.com")); | 623 source_urls.push_back(std::string("http://source1.com")); |
633 publishers.push_back(std::string("Source 1")); | 624 publishers.push_back(std::string("Source 1")); |
634 amp_urls.push_back(std::string("http://source1.amp.com")); | 625 amp_urls.push_back(std::string("http://source1.amp.com")); |
635 std::string json_str( | 626 std::string json_str( |
636 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 627 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
637 | 628 |
638 LoadFromJSONString(json_str); | 629 LoadFromJSONString(json_str); |
639 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 630 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
640 const NTPSnippet& snippet = | 631 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
641 *service()->GetSnippetsForTesting(articles_category()).front(); | |
642 EXPECT_EQ(snippet.sources().size(), 1u); | 632 EXPECT_EQ(snippet.sources().size(), 1u); |
643 EXPECT_EQ(snippet.id(), kSnippetUrl); | 633 EXPECT_EQ(snippet.id(), kSnippetUrl); |
644 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 634 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
645 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 635 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
646 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 636 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
647 } | 637 } |
648 | 638 |
649 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { | 639 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { |
650 std::vector<std::string> source_urls, publishers, amp_urls; | 640 std::vector<std::string> source_urls, publishers, amp_urls; |
651 source_urls.push_back(std::string("aaaa")); | 641 source_urls.push_back(std::string("aaaa")); |
652 publishers.push_back(std::string("Source 1")); | 642 publishers.push_back(std::string("Source 1")); |
653 amp_urls.push_back(std::string("http://source1.amp.com")); | 643 amp_urls.push_back(std::string("http://source1.amp.com")); |
654 std::string json_str( | 644 std::string json_str( |
655 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 645 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
656 | 646 |
657 LoadFromJSONString(json_str); | 647 LoadFromJSONString(json_str); |
658 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 648 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
659 } | 649 } |
660 | 650 |
661 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { | 651 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { |
662 std::vector<std::string> source_urls, publishers, amp_urls; | 652 std::vector<std::string> source_urls, publishers, amp_urls; |
663 source_urls.push_back(std::string("http://source1.com")); | 653 source_urls.push_back(std::string("http://source1.com")); |
664 publishers.push_back(std::string()); | 654 publishers.push_back(std::string()); |
665 amp_urls.push_back(std::string()); | 655 amp_urls.push_back(std::string()); |
666 std::string json_str( | 656 std::string json_str( |
667 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 657 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
668 | 658 |
669 LoadFromJSONString(json_str); | 659 LoadFromJSONString(json_str); |
670 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 660 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
671 } | 661 } |
672 | 662 |
673 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { | 663 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { |
674 std::vector<std::string> source_urls, publishers, amp_urls; | 664 std::vector<std::string> source_urls, publishers, amp_urls; |
675 source_urls.push_back(std::string("http://source1.com")); | 665 source_urls.push_back(std::string("http://source1.com")); |
676 source_urls.push_back(std::string("http://source2.com")); | 666 source_urls.push_back(std::string("http://source2.com")); |
677 publishers.push_back(std::string("Source 1")); | 667 publishers.push_back(std::string("Source 1")); |
678 publishers.push_back(std::string("Source 2")); | 668 publishers.push_back(std::string("Source 2")); |
679 amp_urls.push_back(std::string("http://source1.amp.com")); | 669 amp_urls.push_back(std::string("http://source1.amp.com")); |
680 amp_urls.push_back(std::string("http://source2.amp.com")); | 670 amp_urls.push_back(std::string("http://source2.amp.com")); |
681 std::string json_str( | 671 std::string json_str( |
682 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 672 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
683 | 673 |
684 LoadFromJSONString(json_str); | 674 LoadFromJSONString(json_str); |
685 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 675 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
686 const NTPSnippet& snippet = | 676 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
687 *service()->GetSnippetsForTesting(articles_category()).front(); | |
688 // Expect the first source to be chosen | 677 // Expect the first source to be chosen |
689 EXPECT_EQ(snippet.sources().size(), 2u); | 678 EXPECT_EQ(snippet.sources().size(), 2u); |
690 EXPECT_EQ(snippet.id(), kSnippetUrl); | 679 EXPECT_EQ(snippet.id(), kSnippetUrl); |
691 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 680 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
692 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 681 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
693 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 682 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
694 } | 683 } |
695 | 684 |
696 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { | 685 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { |
697 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name | 686 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name |
698 // Source 2 should win since we favor publisher name over amp url | 687 // Source 2 should win since we favor publisher name over amp url |
699 std::vector<std::string> source_urls, publishers, amp_urls; | 688 std::vector<std::string> source_urls, publishers, amp_urls; |
700 source_urls.push_back(std::string("http://source1.com")); | 689 source_urls.push_back(std::string("http://source1.com")); |
701 source_urls.push_back(std::string("http://source2.com")); | 690 source_urls.push_back(std::string("http://source2.com")); |
702 publishers.push_back(std::string()); | 691 publishers.push_back(std::string()); |
703 publishers.push_back(std::string("Source 2")); | 692 publishers.push_back(std::string("Source 2")); |
704 amp_urls.push_back(std::string("http://source1.amp.com")); | 693 amp_urls.push_back(std::string("http://source1.amp.com")); |
705 amp_urls.push_back(std::string()); | 694 amp_urls.push_back(std::string()); |
706 std::string json_str( | 695 std::string json_str( |
707 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 696 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
708 | 697 |
709 LoadFromJSONString(json_str); | 698 LoadFromJSONString(json_str); |
710 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 699 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
711 { | 700 { |
712 const NTPSnippet& snippet = | 701 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
713 *service()->GetSnippetsForTesting(articles_category()).front(); | |
714 EXPECT_EQ(snippet.sources().size(), 2u); | 702 EXPECT_EQ(snippet.sources().size(), 2u); |
715 EXPECT_EQ(snippet.id(), kSnippetUrl); | 703 EXPECT_EQ(snippet.id(), kSnippetUrl); |
716 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 704 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
717 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 705 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
718 EXPECT_EQ(snippet.best_source().amp_url, GURL()); | 706 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
719 } | 707 } |
720 | 708 |
721 service()->ClearCachedSuggestions(articles_category()); | 709 service()->ClearCachedSuggestions(articles_category()); |
722 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name | 710 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name |
723 // Source 1 should win in this case since we prefer publisher name to AMP url | 711 // Source 1 should win in this case since we prefer publisher name to AMP url |
724 source_urls.clear(); | 712 source_urls.clear(); |
725 source_urls.push_back(std::string("http://source1.com")); | 713 source_urls.push_back(std::string("http://source1.com")); |
726 source_urls.push_back(std::string("http://source2.com")); | 714 source_urls.push_back(std::string("http://source2.com")); |
727 publishers.clear(); | 715 publishers.clear(); |
728 publishers.push_back(std::string("Source 1")); | 716 publishers.push_back(std::string("Source 1")); |
729 publishers.push_back(std::string()); | 717 publishers.push_back(std::string()); |
730 amp_urls.clear(); | 718 amp_urls.clear(); |
731 amp_urls.push_back(std::string()); | 719 amp_urls.push_back(std::string()); |
732 amp_urls.push_back(std::string("http://source2.amp.com")); | 720 amp_urls.push_back(std::string("http://source2.amp.com")); |
733 json_str = | 721 json_str = |
734 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 722 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
735 | 723 |
736 LoadFromJSONString(json_str); | 724 LoadFromJSONString(json_str); |
737 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 725 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
738 { | 726 { |
739 const NTPSnippet& snippet = | 727 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
740 *service()->GetSnippetsForTesting(articles_category()).front(); | |
741 EXPECT_EQ(snippet.sources().size(), 2u); | 728 EXPECT_EQ(snippet.sources().size(), 2u); |
742 EXPECT_EQ(snippet.id(), kSnippetUrl); | 729 EXPECT_EQ(snippet.id(), kSnippetUrl); |
743 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 730 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
744 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 731 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
745 EXPECT_EQ(snippet.best_source().amp_url, GURL()); | 732 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
746 } | 733 } |
747 | 734 |
748 service()->ClearCachedSuggestions(articles_category()); | 735 service()->ClearCachedSuggestions(articles_category()); |
749 // Set source 1 to have no AMP url and no source, and source 2 to only have | 736 // Set source 1 to have no AMP url and no source, and source 2 to only have |
750 // amp url. There should be no snippets since we only add sources we consider | 737 // amp url. There should be no snippets since we only add sources we consider |
751 // complete | 738 // complete |
752 source_urls.clear(); | 739 source_urls.clear(); |
753 source_urls.push_back(std::string("http://source1.com")); | 740 source_urls.push_back(std::string("http://source1.com")); |
754 source_urls.push_back(std::string("http://source2.com")); | 741 source_urls.push_back(std::string("http://source2.com")); |
755 publishers.clear(); | 742 publishers.clear(); |
756 publishers.push_back(std::string()); | 743 publishers.push_back(std::string()); |
757 publishers.push_back(std::string()); | 744 publishers.push_back(std::string()); |
758 amp_urls.clear(); | 745 amp_urls.clear(); |
759 amp_urls.push_back(std::string()); | 746 amp_urls.push_back(std::string()); |
760 amp_urls.push_back(std::string("http://source2.amp.com")); | 747 amp_urls.push_back(std::string("http://source2.amp.com")); |
761 json_str = | 748 json_str = |
762 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 749 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
763 | 750 |
764 LoadFromJSONString(json_str); | 751 LoadFromJSONString(json_str); |
765 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 752 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
766 } | 753 } |
767 | 754 |
768 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { | 755 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { |
769 // Test 2 complete sources, we should choose the first complete source | 756 // Test 2 complete sources, we should choose the first complete source |
770 std::vector<std::string> source_urls, publishers, amp_urls; | 757 std::vector<std::string> source_urls, publishers, amp_urls; |
771 source_urls.push_back(std::string("http://source1.com")); | 758 source_urls.push_back(std::string("http://source1.com")); |
772 source_urls.push_back(std::string("http://source2.com")); | 759 source_urls.push_back(std::string("http://source2.com")); |
773 source_urls.push_back(std::string("http://source3.com")); | 760 source_urls.push_back(std::string("http://source3.com")); |
774 publishers.push_back(std::string("Source 1")); | 761 publishers.push_back(std::string("Source 1")); |
775 publishers.push_back(std::string()); | 762 publishers.push_back(std::string()); |
776 publishers.push_back(std::string("Source 3")); | 763 publishers.push_back(std::string("Source 3")); |
777 amp_urls.push_back(std::string("http://source1.amp.com")); | 764 amp_urls.push_back(std::string("http://source1.amp.com")); |
778 amp_urls.push_back(std::string("http://source2.amp.com")); | 765 amp_urls.push_back(std::string("http://source2.amp.com")); |
779 amp_urls.push_back(std::string("http://source3.amp.com")); | 766 amp_urls.push_back(std::string("http://source3.amp.com")); |
780 std::string json_str( | 767 std::string json_str( |
781 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 768 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
782 | 769 |
783 LoadFromJSONString(json_str); | 770 LoadFromJSONString(json_str); |
784 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 771 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
785 { | 772 { |
786 const NTPSnippet& snippet = | 773 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
787 *service()->GetSnippetsForTesting(articles_category()).front(); | |
788 EXPECT_EQ(snippet.sources().size(), 3u); | 774 EXPECT_EQ(snippet.sources().size(), 3u); |
789 EXPECT_EQ(snippet.id(), kSnippetUrl); | 775 EXPECT_EQ(snippet.id(), kSnippetUrl); |
790 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 776 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
791 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 777 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
792 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 778 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
793 } | 779 } |
794 | 780 |
795 // Test 2 complete sources, we should choose the first complete source | 781 // Test 2 complete sources, we should choose the first complete source |
796 service()->ClearCachedSuggestions(articles_category()); | 782 service()->ClearCachedSuggestions(articles_category()); |
797 source_urls.clear(); | 783 source_urls.clear(); |
798 source_urls.push_back(std::string("http://source1.com")); | 784 source_urls.push_back(std::string("http://source1.com")); |
799 source_urls.push_back(std::string("http://source2.com")); | 785 source_urls.push_back(std::string("http://source2.com")); |
800 source_urls.push_back(std::string("http://source3.com")); | 786 source_urls.push_back(std::string("http://source3.com")); |
801 publishers.clear(); | 787 publishers.clear(); |
802 publishers.push_back(std::string()); | 788 publishers.push_back(std::string()); |
803 publishers.push_back(std::string("Source 2")); | 789 publishers.push_back(std::string("Source 2")); |
804 publishers.push_back(std::string("Source 3")); | 790 publishers.push_back(std::string("Source 3")); |
805 amp_urls.clear(); | 791 amp_urls.clear(); |
806 amp_urls.push_back(std::string("http://source1.amp.com")); | 792 amp_urls.push_back(std::string("http://source1.amp.com")); |
807 amp_urls.push_back(std::string("http://source2.amp.com")); | 793 amp_urls.push_back(std::string("http://source2.amp.com")); |
808 amp_urls.push_back(std::string("http://source3.amp.com")); | 794 amp_urls.push_back(std::string("http://source3.amp.com")); |
809 json_str = | 795 json_str = |
810 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 796 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
811 | 797 |
812 LoadFromJSONString(json_str); | 798 LoadFromJSONString(json_str); |
813 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 799 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
814 { | 800 { |
815 const NTPSnippet& snippet = | 801 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
816 *service()->GetSnippetsForTesting(articles_category()).front(); | |
817 EXPECT_EQ(snippet.sources().size(), 3u); | 802 EXPECT_EQ(snippet.sources().size(), 3u); |
818 EXPECT_EQ(snippet.id(), kSnippetUrl); | 803 EXPECT_EQ(snippet.id(), kSnippetUrl); |
819 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 804 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
820 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 805 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
821 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); | 806 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
822 } | 807 } |
823 | 808 |
824 // Test 3 complete sources, we should choose the first complete source | 809 // Test 3 complete sources, we should choose the first complete source |
825 service()->ClearCachedSuggestions(articles_category()); | 810 service()->ClearCachedSuggestions(articles_category()); |
826 source_urls.clear(); | 811 source_urls.clear(); |
827 source_urls.push_back(std::string("http://source1.com")); | 812 source_urls.push_back(std::string("http://source1.com")); |
828 source_urls.push_back(std::string("http://source2.com")); | 813 source_urls.push_back(std::string("http://source2.com")); |
829 source_urls.push_back(std::string("http://source3.com")); | 814 source_urls.push_back(std::string("http://source3.com")); |
830 publishers.clear(); | 815 publishers.clear(); |
831 publishers.push_back(std::string("Source 1")); | 816 publishers.push_back(std::string("Source 1")); |
832 publishers.push_back(std::string("Source 2")); | 817 publishers.push_back(std::string("Source 2")); |
833 publishers.push_back(std::string("Source 3")); | 818 publishers.push_back(std::string("Source 3")); |
834 amp_urls.clear(); | 819 amp_urls.clear(); |
835 amp_urls.push_back(std::string()); | 820 amp_urls.push_back(std::string()); |
836 amp_urls.push_back(std::string("http://source2.amp.com")); | 821 amp_urls.push_back(std::string("http://source2.amp.com")); |
837 amp_urls.push_back(std::string("http://source3.amp.com")); | 822 amp_urls.push_back(std::string("http://source3.amp.com")); |
838 json_str = | 823 json_str = |
839 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 824 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
840 | 825 |
841 LoadFromJSONString(json_str); | 826 LoadFromJSONString(json_str); |
842 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 827 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
843 { | 828 { |
844 const NTPSnippet& snippet = | 829 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
845 *service()->GetSnippetsForTesting(articles_category()).front(); | |
846 EXPECT_EQ(snippet.sources().size(), 3u); | 830 EXPECT_EQ(snippet.sources().size(), 3u); |
847 EXPECT_EQ(snippet.id(), kSnippetUrl); | 831 EXPECT_EQ(snippet.id(), kSnippetUrl); |
848 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 832 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
849 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 833 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
850 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); | 834 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
851 } | 835 } |
852 } | 836 } |
853 | 837 |
854 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) { | 838 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) { |
855 base::HistogramTester tester; | 839 base::HistogramTester tester; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 "http://mashable.com/2016/05/11/stolen?utm_cid=1"}; | 900 "http://mashable.com/2016/05/11/stolen?utm_cid=1"}; |
917 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"}; | 901 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"}; |
918 const std::vector<std::string> amp_urls = { | 902 const std::vector<std::string> amp_urls = { |
919 "http://mashable-amphtml.googleusercontent.com/1", | 903 "http://mashable-amphtml.googleusercontent.com/1", |
920 "http://t2.gstatic.com/images?q=tbn:3", | 904 "http://t2.gstatic.com/images?q=tbn:3", |
921 "http://t2.gstatic.com/images?q=tbn:3"}; | 905 "http://t2.gstatic.com/images?q=tbn:3"}; |
922 | 906 |
923 // Add the snippet from the mashable domain. | 907 // Add the snippet from the mashable domain. |
924 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 908 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
925 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); | 909 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); |
926 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); | 910 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
927 // Dismiss the snippet via the mashable source corpus ID. | 911 // Dismiss the snippet via the mashable source corpus ID. |
928 service()->DismissSuggestion(MakeUniqueID(source_urls[0])); | 912 service()->DismissSuggestion(MakeUniqueID(source_urls[0])); |
929 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 913 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
930 | 914 |
931 // The same article from the AOL domain should now be detected as dismissed. | 915 // The same article from the AOL domain should now be detected as dismissed. |
932 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 916 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
933 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); | 917 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); |
934 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); | 918 ASSERT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
935 } | 919 } |
936 | 920 |
937 TEST_F(NTPSnippetsServiceTest, StatusChanges) { | 921 TEST_F(NTPSnippetsServiceTest, StatusChanges) { |
938 // Simulate user signed out | 922 // Simulate user signed out |
939 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 923 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
940 EXPECT_CALL(observer(), | 924 EXPECT_CALL(observer(), |
941 OnCategoryStatusChanged(_, _, CategoryStatus::SIGNED_OUT)); | 925 OnCategoryStatusChanged(_, _, CategoryStatus::SIGNED_OUT)); |
942 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT); | 926 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT); |
943 base::RunLoop().RunUntilIdle(); | 927 base::RunLoop().RunUntilIdle(); |
944 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); | 928 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); |
945 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), | 929 EXPECT_THAT(service()->GetSnippetsForTesting(), |
946 IsEmpty()); // No fetch should be made. | 930 IsEmpty()); // No fetch should be made. |
947 | 931 |
948 // Simulate user sign in. The service should be ready again and load snippets. | 932 // Simulate user sign in. The service should be ready again and load snippets. |
949 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 933 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
950 EXPECT_CALL(observer(), | 934 EXPECT_CALL(observer(), |
951 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); | 935 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); |
952 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 936 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
953 service()->OnDisabledReasonChanged(DisabledReason::NONE); | 937 service()->OnDisabledReasonChanged(DisabledReason::NONE); |
954 EXPECT_CALL(observer(), | 938 EXPECT_CALL(observer(), |
955 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); | 939 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); |
956 base::RunLoop().RunUntilIdle(); | 940 base::RunLoop().RunUntilIdle(); |
957 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 941 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
958 EXPECT_FALSE(service()->GetSnippetsForTesting(articles_category()).empty()); | 942 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); |
959 } | 943 } |
960 | 944 |
961 TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) { | 945 TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) { |
962 LoadFromJSONString(GetTestJson({GetSnippet()})); | 946 LoadFromJSONString(GetTestJson({GetSnippet()})); |
963 | 947 |
964 gfx::Image image; | 948 gfx::Image image; |
965 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | 949 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) |
966 .WillOnce(testing::WithArgs<0, 2>(Invoke(ServeOneByOneImage))); | 950 .WillOnce(testing::WithArgs<0, 2>(Invoke(ServeOneByOneImage))); |
967 testing::MockFunction<void(const std::string&, const gfx::Image&)> | 951 testing::MockFunction<void(const std::string&, const gfx::Image&)> |
968 image_fetched; | 952 image_fetched; |
(...skipping 23 matching lines...) Expand all Loading... |
992 MakeUniqueID(kSnippetUrl2), | 976 MakeUniqueID(kSnippetUrl2), |
993 base::Bind(&testing::MockFunction<void(const std::string&, | 977 base::Bind(&testing::MockFunction<void(const std::string&, |
994 const gfx::Image&)>::Call, | 978 const gfx::Image&)>::Call, |
995 base::Unretained(&image_fetched))); | 979 base::Unretained(&image_fetched))); |
996 | 980 |
997 base::RunLoop().RunUntilIdle(); | 981 base::RunLoop().RunUntilIdle(); |
998 EXPECT_TRUE(image.IsEmpty()); | 982 EXPECT_TRUE(image.IsEmpty()); |
999 } | 983 } |
1000 | 984 |
1001 } // namespace ntp_snippets | 985 } // namespace ntp_snippets |
OLD | NEW |