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

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc

Issue 2421463002: FetchMore functionality backend (Closed)
Patch Set: Introduced callback, removed strategy. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); 167 UserClassifier::RegisterProfilePrefs(pref_service_->registry());
168 user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get()); 168 user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get());
169 169
170 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( 170 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>(
171 fake_signin_manager_.get(), fake_token_service_.get(), 171 fake_signin_manager_.get(), fake_token_service_.get(),
172 scoped_refptr<net::TestURLRequestContextGetter>( 172 scoped_refptr<net::TestURLRequestContextGetter>(
173 new net::TestURLRequestContextGetter(mock_task_runner_.get())), 173 new net::TestURLRequestContextGetter(mock_task_runner_.get())),
174 pref_service_.get(), &category_factory_, nullptr, 174 pref_service_.get(), &category_factory_, nullptr,
175 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); 175 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get());
176 176
177 snippets_fetcher_->SetCallback(
178 base::Bind(&MockSnippetsAvailableCallback::WrappedRun,
179 base::Unretained(&mock_callback_)));
180 snippets_fetcher_->SetTickClockForTesting( 177 snippets_fetcher_->SetTickClockForTesting(
181 mock_task_runner_->GetMockTickClock()); 178 mock_task_runner_->GetMockTickClock());
182 test_excluded_.insert("1234567890"); 179 test_excluded_.insert("1234567890");
183 // Increase initial time such that ticks are non-zero. 180 // Increase initial time such that ticks are non-zero.
184 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); 181 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234));
185 } 182 }
186 183
184 NTPSnippetsFetcher::SnippetsAvailableCallback MakeMockCallback() {
185 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun,
186 base::Unretained(&mock_callback_));
187 }
188
189 base::Optional<Category> NoExclusiveCategory() {
190 return base::Optional<Category>();
191 }
192
193 base::Optional<Category> OptionalArticlesCategory() {
194 return base::Optional<Category>(
195 CategoryFactory().FromKnownCategory(KnownCategories::ARTICLES));
196 }
197
187 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } 198 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; }
188 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } 199 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
189 void FastForwardUntilNoTasksRemain() { 200 void FastForwardUntilNoTasksRemain() {
190 mock_task_runner_->FastForwardUntilNoTasksRemain(); 201 mock_task_runner_->FastForwardUntilNoTasksRemain();
191 } 202 }
192 const std::string& test_lang() const { return test_lang_; } 203 const std::string& test_lang() const { return test_lang_; }
193 const GURL& test_url() { return test_url_; } 204 const GURL& test_url() { return test_url_; }
194 const std::set<std::string>& test_hosts() const { return test_hosts_; } 205 const std::set<std::string>& test_hosts() const { return test_hosts_; }
195 const std::set<std::string>& test_excluded() const { return test_excluded_; } 206 const std::set<std::string>& test_excluded() const { return test_excluded_; }
196 base::HistogramTester& histogram_tester() { return histogram_tester_; } 207 base::HistogramTester& histogram_tester() { return histogram_tester_; }
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 " \"sourceCorpusInfo\" : [{" 526 " \"sourceCorpusInfo\" : [{"
516 " \"ampUrl\" : \"http://localhost/amp\"," 527 " \"ampUrl\" : \"http://localhost/amp\","
517 " \"corpusId\" : \"http://localhost/foobar\"," 528 " \"corpusId\" : \"http://localhost/foobar\","
518 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" 529 " \"publisherData\": { \"sourceName\" : \"Foo News\" }"
519 " }]" 530 " }]"
520 " }" 531 " }"
521 "}]}"; 532 "}]}";
522 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 533 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
523 net::URLRequestStatus::SUCCESS); 534 net::URLRequestStatus::SUCCESS);
524 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); 535 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar")));
525 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 536 snippets_fetcher().FetchSnippetsFromHosts(
526 test_excluded(), 537 test_hosts(), test_lang(), test_excluded(),
527 /*count=*/1, 538 /*count=*/1,
528 /*interactive_request=*/true); 539 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
529 FastForwardUntilNoTasksRemain(); 540 FastForwardUntilNoTasksRemain();
530 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 541 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
531 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 542 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
532 EXPECT_THAT(histogram_tester().GetAllSamples( 543 EXPECT_THAT(histogram_tester().GetAllSamples(
533 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 544 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
534 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 545 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
535 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 546 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
536 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 547 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
537 /*count=*/1))); 548 /*count=*/1)));
538 } 549 }
(...skipping 12 matching lines...) Expand all
551 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," 562 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
552 " \"attribution\" : \"Foo News\"," 563 " \"attribution\" : \"Foo News\","
553 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 564 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
554 " \"ampUrl\" : \"http://localhost/amp\"," 565 " \"ampUrl\" : \"http://localhost/amp\","
555 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 566 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
556 " }]" 567 " }]"
557 "}]}"; 568 "}]}";
558 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 569 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
559 net::URLRequestStatus::SUCCESS); 570 net::URLRequestStatus::SUCCESS);
560 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); 571 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar")));
561 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 572 snippets_fetcher().FetchSnippetsFromHosts(
562 test_excluded(), 573 test_hosts(), test_lang(), test_excluded(),
563 /*count=*/1, 574 /*count=*/1,
564 /*interactive_request=*/true); 575 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
565 FastForwardUntilNoTasksRemain(); 576 FastForwardUntilNoTasksRemain();
566 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 577 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
567 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 578 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
568 EXPECT_THAT(histogram_tester().GetAllSamples( 579 EXPECT_THAT(histogram_tester().GetAllSamples(
569 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 580 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
570 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 581 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
571 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 582 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
572 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 583 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
573 /*count=*/1))); 584 /*count=*/1)));
574 } 585 }
575 586
576 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { 587 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) {
577 const std::string kJsonStr = 588 const std::string kJsonStr =
578 "{\"categories\" : [{" 589 "{\"categories\" : [{"
579 " \"id\": 1," 590 " \"id\": 1,"
580 " \"localizedTitle\": \"Articles for You\"" 591 " \"localizedTitle\": \"Articles for You\""
581 "}]}"; 592 "}]}";
582 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 593 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
583 net::URLRequestStatus::SUCCESS); 594 net::URLRequestStatus::SUCCESS);
584 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); 595 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
585 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 596 snippets_fetcher().FetchSnippetsFromHosts(
586 test_excluded(), 597 test_hosts(), test_lang(), test_excluded(),
587 /*count=*/1, 598 /*count=*/1,
588 /*interactive_request=*/true); 599 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
589 FastForwardUntilNoTasksRemain(); 600 FastForwardUntilNoTasksRemain();
590 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 601 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
591 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 602 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
592 EXPECT_THAT(histogram_tester().GetAllSamples( 603 EXPECT_THAT(histogram_tester().GetAllSamples(
593 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 604 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
594 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 605 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
595 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 606 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
596 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 607 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
597 /*count=*/1))); 608 /*count=*/1)));
598 } 609 }
(...skipping 29 matching lines...) Expand all
628 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," 639 " \"imageUrl\" : \"http://localhost/foo2.jpg\","
629 " \"ampUrl\" : \"http://localhost/amp\"," 640 " \"ampUrl\" : \"http://localhost/amp\","
630 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 641 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
631 " }]" 642 " }]"
632 "}]}"; 643 "}]}";
633 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 644 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
634 net::URLRequestStatus::SUCCESS); 645 net::URLRequestStatus::SUCCESS);
635 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; 646 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories;
636 EXPECT_CALL(mock_callback(), Run(_)) 647 EXPECT_CALL(mock_callback(), Run(_))
637 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); 648 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories)));
638 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 649 snippets_fetcher().FetchSnippetsFromHosts(
639 test_excluded(), 650 test_hosts(), test_lang(), test_excluded(),
640 /*count=*/1, 651 /*count=*/1,
641 /*interactive_request=*/true); 652 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
642 FastForwardUntilNoTasksRemain(); 653 FastForwardUntilNoTasksRemain();
643 654
644 ASSERT_TRUE(fetched_categories); 655 ASSERT_TRUE(fetched_categories);
645 ASSERT_THAT(fetched_categories->size(), Eq(2u)); 656 ASSERT_THAT(fetched_categories->size(), Eq(2u));
646 for (const auto& category : *fetched_categories) { 657 for (const auto& category : *fetched_categories) {
647 const auto& articles = category.snippets; 658 const auto& articles = category.snippets;
648 switch (category.category.id()) { 659 switch (category.category.id()) {
649 case static_cast<int>(KnownCategories::ARTICLES): 660 case static_cast<int>(KnownCategories::ARTICLES):
650 ASSERT_THAT(articles.size(), Eq(1u)); 661 ASSERT_THAT(articles.size(), Eq(1u));
651 EXPECT_THAT(articles[0]->best_source().url.spec(), 662 EXPECT_THAT(articles[0]->best_source().url.spec(),
(...skipping 12 matching lines...) Expand all
664 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 675 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
665 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 676 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
666 EXPECT_THAT(histogram_tester().GetAllSamples( 677 EXPECT_THAT(histogram_tester().GetAllSamples(
667 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 678 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
668 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 679 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
669 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 680 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
670 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 681 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
671 /*count=*/1))); 682 /*count=*/1)));
672 } 683 }
673 684
685 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ExclusiveCategoryOnly) {
686 const std::string kJsonStr =
687 "{\"categories\" : [{"
688 " \"id\": 1,"
689 " \"localizedTitle\": \"Articles for You\","
690 " \"suggestions\" : [{"
691 " \"ids\" : [\"http://localhost/foobar\"],"
692 " \"title\" : \"Foo Barred from Baz\","
693 " \"snippet\" : \"...\","
694 " \"fullPageUrl\" : \"http://localhost/foobar\","
695 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
696 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
697 " \"attribution\" : \"Foo News\","
698 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
699 " \"ampUrl\" : \"http://localhost/amp\","
700 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
701 " }]"
702 "}, {"
703 " \"id\": 2,"
704 " \"localizedTitle\": \"Articles for Me\","
705 " \"suggestions\" : [{"
706 " \"ids\" : [\"http://localhost/foo2\"],"
707 " \"title\" : \"Foo Barred from Baz\","
708 " \"snippet\" : \"...\","
709 " \"fullPageUrl\" : \"http://localhost/foo2\","
710 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
711 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
712 " \"attribution\" : \"Foo News\","
713 " \"imageUrl\" : \"http://localhost/foo2.jpg\","
714 " \"ampUrl\" : \"http://localhost/amp\","
715 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
716 " }]"
717 "}]}";
718 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
719 net::URLRequestStatus::SUCCESS);
720 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories;
721 EXPECT_CALL(mock_callback(), Run(_))
722 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories)));
723 snippets_fetcher().FetchSnippetsFromHosts(
724 test_hosts(), test_lang(), test_excluded(),
725 /*count=*/1,
726 /*interactive_request=*/true, MakeMockCallback(),
727 OptionalArticlesCategory());
728 FastForwardUntilNoTasksRemain();
729
730 ASSERT_TRUE(fetched_categories);
731 ASSERT_THAT(fetched_categories->size(), Eq(1u));
732 for (const auto& category : *fetched_categories) {
733 const auto& articles = category.snippets;
734 switch (category.category.id()) {
735 case static_cast<int>(KnownCategories::ARTICLES):
Marc Treib 2016/10/20 16:51:40 Use CategoryFactory::IsKnownCategory(category.id()
Marc Treib 2016/10/28 14:49:49 Done.
736 ASSERT_THAT(articles.size(), Eq(1u));
737 EXPECT_THAT(articles[0]->best_source().url.spec(),
738 Eq("http://localhost/foobar"));
739 break;
740 default:
741 FAIL() << "unexpected category with ID " << category.category.id();
742 }
743 }
744 }
745
674 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { 746 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
675 const std::string kJsonStr = "{\"recos\": []}"; 747 const std::string kJsonStr = "{\"recos\": []}";
676 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 748 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
677 net::URLRequestStatus::SUCCESS); 749 net::URLRequestStatus::SUCCESS);
678 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); 750 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
679 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 751 snippets_fetcher().FetchSnippetsFromHosts(
680 test_excluded(), 752 test_hosts(), test_lang(), test_excluded(),
681 /*count=*/1, 753 /*count=*/1,
682 /*interactive_request=*/true); 754 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
683 FastForwardUntilNoTasksRemain(); 755 FastForwardUntilNoTasksRemain();
684 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 756 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
685 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 757 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
686 EXPECT_THAT( 758 EXPECT_THAT(
687 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 759 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
688 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 760 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
689 EXPECT_THAT(histogram_tester().GetAllSamples( 761 EXPECT_THAT(histogram_tester().GetAllSamples(
690 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 762 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
691 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 763 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
692 } 764 }
693 765
694 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { 766 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) {
695 net::TestURLFetcherFactory test_url_fetcher_factory; 767 net::TestURLFetcherFactory test_url_fetcher_factory;
696 snippets_fetcher().FetchSnippetsFromHosts( 768 snippets_fetcher().FetchSnippetsFromHosts(
697 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(), 769 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(),
698 /*count=*/17, 770 /*count=*/17,
699 /*interactive_request=*/true); 771 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
700 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); 772 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0);
701 ASSERT_THAT(fetcher, NotNull()); 773 ASSERT_THAT(fetcher, NotNull());
702 std::unique_ptr<base::Value> value = 774 std::unique_ptr<base::Value> value =
703 base::JSONReader::Read(fetcher->upload_data()); 775 base::JSONReader::Read(fetcher->upload_data());
704 ASSERT_TRUE(value) << " failed to parse JSON: " 776 ASSERT_TRUE(value) << " failed to parse JSON: "
705 << PrintToString(fetcher->upload_data()); 777 << PrintToString(fetcher->upload_data());
706 const base::DictionaryValue* dict = nullptr; 778 const base::DictionaryValue* dict = nullptr;
707 ASSERT_TRUE(value->GetAsDictionary(&dict)); 779 ASSERT_TRUE(value->GetAsDictionary(&dict));
708 const base::DictionaryValue* local_scoring_params = nullptr; 780 const base::DictionaryValue* local_scoring_params = nullptr;
709 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", 781 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params",
710 &local_scoring_params)); 782 &local_scoring_params));
711 const base::ListValue* content_selectors = nullptr; 783 const base::ListValue* content_selectors = nullptr;
712 ASSERT_TRUE( 784 ASSERT_TRUE(
713 local_scoring_params->GetList("content_selectors", &content_selectors)); 785 local_scoring_params->GetList("content_selectors", &content_selectors));
714 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2))); 786 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2)));
715 const base::DictionaryValue* content_selector = nullptr; 787 const base::DictionaryValue* content_selector = nullptr;
716 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); 788 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector));
717 std::string content_selector_value; 789 std::string content_selector_value;
718 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); 790 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
719 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); 791 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com"));
720 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); 792 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector));
721 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); 793 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
722 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); 794 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com"));
723 } 795 }
724 796
725 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 797 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
726 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, 798 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
727 net::URLRequestStatus::FAILED); 799 net::URLRequestStatus::FAILED);
728 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 800 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
729 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 801 snippets_fetcher().FetchSnippetsFromHosts(
730 test_excluded(), 802 test_hosts(), test_lang(), test_excluded(),
731 /*count=*/1, 803 /*count=*/1,
732 /*interactive_request=*/true); 804 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
733 FastForwardUntilNoTasksRemain(); 805 FastForwardUntilNoTasksRemain();
734 EXPECT_THAT(snippets_fetcher().last_status(), 806 EXPECT_THAT(snippets_fetcher().last_status(),
735 Eq("URLRequestStatus error -2")); 807 Eq("URLRequestStatus error -2"));
736 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 808 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
737 EXPECT_THAT( 809 EXPECT_THAT(
738 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 810 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
739 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); 811 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
740 EXPECT_THAT(histogram_tester().GetAllSamples( 812 EXPECT_THAT(histogram_tester().GetAllSamples(
741 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 813 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
742 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 814 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
743 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 815 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
744 Not(IsEmpty())); 816 Not(IsEmpty()));
745 } 817 }
746 818
747 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 819 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
748 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, 820 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
749 net::URLRequestStatus::SUCCESS); 821 net::URLRequestStatus::SUCCESS);
750 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 822 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
751 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 823 snippets_fetcher().FetchSnippetsFromHosts(
752 test_excluded(), 824 test_hosts(), test_lang(), test_excluded(),
753 /*count=*/1, 825 /*count=*/1,
754 /*interactive_request=*/true); 826 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
755 FastForwardUntilNoTasksRemain(); 827 FastForwardUntilNoTasksRemain();
756 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 828 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
757 EXPECT_THAT( 829 EXPECT_THAT(
758 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 830 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
759 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); 831 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
760 EXPECT_THAT(histogram_tester().GetAllSamples( 832 EXPECT_THAT(histogram_tester().GetAllSamples(
761 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 833 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
762 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 834 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
763 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 835 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
764 Not(IsEmpty())); 836 Not(IsEmpty()));
765 } 837 }
766 838
767 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { 839 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) {
768 const std::string kInvalidJsonStr = "{ \"recos\": []"; 840 const std::string kInvalidJsonStr = "{ \"recos\": []";
769 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, 841 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK,
770 net::URLRequestStatus::SUCCESS); 842 net::URLRequestStatus::SUCCESS);
771 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 843 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
772 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 844 snippets_fetcher().FetchSnippetsFromHosts(
773 test_excluded(), 845 test_hosts(), test_lang(), test_excluded(),
774 /*count=*/1, 846 /*count=*/1,
775 /*interactive_request=*/true); 847 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
776 FastForwardUntilNoTasksRemain(); 848 FastForwardUntilNoTasksRemain();
777 EXPECT_THAT(snippets_fetcher().last_status(), 849 EXPECT_THAT(snippets_fetcher().last_status(),
778 StartsWith("Received invalid JSON (error ")); 850 StartsWith("Received invalid JSON (error "));
779 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); 851 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr));
780 EXPECT_THAT( 852 EXPECT_THAT(
781 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 853 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
782 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 854 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
783 EXPECT_THAT(histogram_tester().GetAllSamples( 855 EXPECT_THAT(histogram_tester().GetAllSamples(
784 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 856 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
785 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 857 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
786 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 858 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
787 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 859 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
788 /*count=*/1))); 860 /*count=*/1)));
789 } 861 }
790 862
791 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { 863 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
792 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, 864 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK,
793 net::URLRequestStatus::SUCCESS); 865 net::URLRequestStatus::SUCCESS);
794 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 866 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
795 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 867 snippets_fetcher().FetchSnippetsFromHosts(
796 test_excluded(), 868 test_hosts(), test_lang(), test_excluded(),
797 /*count=*/1, 869 /*count=*/1,
798 /*interactive_request=*/true); 870 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
799 FastForwardUntilNoTasksRemain(); 871 FastForwardUntilNoTasksRemain();
800 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); 872 EXPECT_THAT(snippets_fetcher().last_json(), std::string());
801 EXPECT_THAT( 873 EXPECT_THAT(
802 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 874 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
803 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 875 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
804 EXPECT_THAT(histogram_tester().GetAllSamples( 876 EXPECT_THAT(histogram_tester().GetAllSamples(
805 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 877 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
806 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 878 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
807 } 879 }
808 880
809 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { 881 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) {
810 const std::string kJsonStr = 882 const std::string kJsonStr =
811 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; 883 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}";
812 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 884 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
813 net::URLRequestStatus::SUCCESS); 885 net::URLRequestStatus::SUCCESS);
814 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 886 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
815 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 887 snippets_fetcher().FetchSnippetsFromHosts(
816 test_excluded(), 888 test_hosts(), test_lang(), test_excluded(),
817 /*count=*/1, 889 /*count=*/1,
818 /*interactive_request=*/true); 890 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
819 FastForwardUntilNoTasksRemain(); 891 FastForwardUntilNoTasksRemain();
820 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 892 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
821 EXPECT_THAT( 893 EXPECT_THAT(
822 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 894 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
823 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); 895 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
824 EXPECT_THAT(histogram_tester().GetAllSamples( 896 EXPECT_THAT(histogram_tester().GetAllSamples(
825 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 897 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
826 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 898 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
827 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 899 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
828 Not(IsEmpty())); 900 Not(IsEmpty()));
829 } 901 }
830 902
831 // This test actually verifies that the test setup itself is sane, to prevent 903 // This test actually verifies that the test setup itself is sane, to prevent
832 // hard-to-reproduce test failures. 904 // hard-to-reproduce test failures.
833 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 905 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
834 InitFakeURLFetcherFactory(); 906 InitFakeURLFetcherFactory();
835 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 907 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
836 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 908 snippets_fetcher().FetchSnippetsFromHosts(
837 test_excluded(), 909 test_hosts(), test_lang(), test_excluded(),
838 /*count=*/1, 910 /*count=*/1,
839 /*interactive_request=*/true); 911 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
840 FastForwardUntilNoTasksRemain(); 912 FastForwardUntilNoTasksRemain();
841 } 913 }
842 914
843 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 915 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
844 const std::string kJsonStr = "{ \"recos\": [] }"; 916 const std::string kJsonStr = "{ \"recos\": [] }";
845 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 917 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
846 net::URLRequestStatus::SUCCESS); 918 net::URLRequestStatus::SUCCESS);
847 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); 919 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
848 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 920 snippets_fetcher().FetchSnippetsFromHosts(
849 test_excluded(), 921 test_hosts(), test_lang(), test_excluded(),
850 /*count=*/1, 922 /*count=*/1,
851 /*interactive_request=*/true); 923 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
852 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. 924 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous.
853 // Callback is expected to be called once. 925 // Callback is expected to be called once.
854 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 926 snippets_fetcher().FetchSnippetsFromHosts(
855 test_excluded(), 927 test_hosts(), test_lang(), test_excluded(),
856 /*count=*/1, 928 /*count=*/1,
857 /*interactive_request=*/true); 929 /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory());
858 FastForwardUntilNoTasksRemain(); 930 FastForwardUntilNoTasksRemain();
859 EXPECT_THAT( 931 EXPECT_THAT(
860 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 932 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
861 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 933 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
862 EXPECT_THAT(histogram_tester().GetAllSamples( 934 EXPECT_THAT(histogram_tester().GetAllSamples(
863 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 935 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
864 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 936 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
865 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 937 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
866 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 938 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
867 /*count=*/1))); 939 /*count=*/1)));
868 } 940 }
869 941
870 ::std::ostream& operator<<( 942 ::std::ostream& operator<<(
871 ::std::ostream& os, 943 ::std::ostream& os,
872 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { 944 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) {
873 if (fetched_categories) { 945 if (fetched_categories) {
874 // Matchers above aren't any more precise than this, so this is sufficient 946 // Matchers above aren't any more precise than this, so this is sufficient
875 // for test-failure diagnostics. 947 // for test-failure diagnostics.
876 return os << "list with " << fetched_categories->size() << " elements"; 948 return os << "list with " << fetched_categories->size() << " elements";
877 } 949 }
878 return os << "null"; 950 return os << "null";
879 } 951 }
880 952
881 } // namespace ntp_snippets 953 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698