OLD | NEW |
---|---|
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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); | 166 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); |
167 user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get()); | 167 user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get()); |
168 | 168 |
169 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 169 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
170 fake_signin_manager_.get(), fake_token_service_.get(), | 170 fake_signin_manager_.get(), fake_token_service_.get(), |
171 scoped_refptr<net::TestURLRequestContextGetter>( | 171 scoped_refptr<net::TestURLRequestContextGetter>( |
172 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 172 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
173 pref_service_.get(), &category_factory_, nullptr, | 173 pref_service_.get(), &category_factory_, nullptr, |
174 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); | 174 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); |
175 | 175 |
176 snippets_fetcher_->SetCallback( | |
177 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | |
178 base::Unretained(&mock_callback_))); | |
179 snippets_fetcher_->SetTickClockForTesting( | 176 snippets_fetcher_->SetTickClockForTesting( |
180 mock_task_runner_->GetMockTickClock()); | 177 mock_task_runner_->GetMockTickClock()); |
181 // Increase initial time such that ticks are non-zero. | 178 // Increase initial time such that ticks are non-zero. |
182 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 179 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
183 } | 180 } |
184 | 181 |
182 NTPSnippetsFetcher::SnippetsAvailableCallback MakeMockCallback() { | |
183 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, | |
184 base::Unretained(&mock_callback_)); | |
185 } | |
186 | |
187 base::Optional<Category> OptionalArticlesCategory() { | |
tschumann
2016/11/03 09:20:57
we should inline this. It only obfuscates reading
| |
188 return base::Optional<Category>( | |
189 CategoryFactory().FromKnownCategory(KnownCategories::ARTICLES)); | |
190 } | |
191 | |
185 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } | 192 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } |
186 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } | 193 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } |
187 void FastForwardUntilNoTasksRemain() { | 194 void FastForwardUntilNoTasksRemain() { |
188 mock_task_runner_->FastForwardUntilNoTasksRemain(); | 195 mock_task_runner_->FastForwardUntilNoTasksRemain(); |
189 } | 196 } |
190 base::HistogramTester& histogram_tester() { return histogram_tester_; } | 197 base::HistogramTester& histogram_tester() { return histogram_tester_; } |
191 | 198 |
192 NTPSnippetsFetcher::Params test_params() { | 199 NTPSnippetsFetcher::Params test_params() { |
193 NTPSnippetsFetcher::Params result; | 200 NTPSnippetsFetcher::Params result; |
194 result.count_to_fetch = 1; | 201 result.count_to_fetch = 1; |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
501 " \"sourceCorpusInfo\" : [{" | 508 " \"sourceCorpusInfo\" : [{" |
502 " \"ampUrl\" : \"http://localhost/amp\"," | 509 " \"ampUrl\" : \"http://localhost/amp\"," |
503 " \"corpusId\" : \"http://localhost/foobar\"," | 510 " \"corpusId\" : \"http://localhost/foobar\"," |
504 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" | 511 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" |
505 " }]" | 512 " }]" |
506 " }" | 513 " }" |
507 "}]}"; | 514 "}]}"; |
508 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 515 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
509 net::URLRequestStatus::SUCCESS); | 516 net::URLRequestStatus::SUCCESS); |
510 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 517 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
511 snippets_fetcher().FetchSnippets(test_params()); | 518 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
512 FastForwardUntilNoTasksRemain(); | 519 FastForwardUntilNoTasksRemain(); |
513 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 520 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
514 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 521 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
515 EXPECT_THAT(histogram_tester().GetAllSamples( | 522 EXPECT_THAT(histogram_tester().GetAllSamples( |
516 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 523 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
517 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 524 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
518 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 525 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
519 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 526 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
520 /*count=*/1))); | 527 /*count=*/1))); |
521 } | 528 } |
(...skipping 12 matching lines...) Expand all Loading... | |
534 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," | 541 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
535 " \"attribution\" : \"Foo News\"," | 542 " \"attribution\" : \"Foo News\"," |
536 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," | 543 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
537 " \"ampUrl\" : \"http://localhost/amp\"," | 544 " \"ampUrl\" : \"http://localhost/amp\"," |
538 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 545 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
539 " }]" | 546 " }]" |
540 "}]}"; | 547 "}]}"; |
541 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 548 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
542 net::URLRequestStatus::SUCCESS); | 549 net::URLRequestStatus::SUCCESS); |
543 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 550 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
544 snippets_fetcher().FetchSnippets(test_params()); | 551 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
545 FastForwardUntilNoTasksRemain(); | 552 FastForwardUntilNoTasksRemain(); |
546 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 553 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
547 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 554 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
548 EXPECT_THAT(histogram_tester().GetAllSamples( | 555 EXPECT_THAT(histogram_tester().GetAllSamples( |
549 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 556 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
550 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 557 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
551 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 558 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
552 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 559 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
553 /*count=*/1))); | 560 /*count=*/1))); |
554 } | 561 } |
555 | 562 |
556 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { | 563 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { |
557 const std::string kJsonStr = | 564 const std::string kJsonStr = |
558 "{\"categories\" : [{" | 565 "{\"categories\" : [{" |
559 " \"id\": 1," | 566 " \"id\": 1," |
560 " \"localizedTitle\": \"Articles for You\"" | 567 " \"localizedTitle\": \"Articles for You\"" |
561 "}]}"; | 568 "}]}"; |
562 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 569 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
563 net::URLRequestStatus::SUCCESS); | 570 net::URLRequestStatus::SUCCESS); |
564 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 571 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
565 snippets_fetcher().FetchSnippets(test_params()); | 572 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
566 FastForwardUntilNoTasksRemain(); | 573 FastForwardUntilNoTasksRemain(); |
567 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 574 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
568 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 575 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
569 EXPECT_THAT(histogram_tester().GetAllSamples( | 576 EXPECT_THAT(histogram_tester().GetAllSamples( |
570 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 577 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
571 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 578 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
572 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 579 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
573 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 580 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
574 /*count=*/1))); | 581 /*count=*/1))); |
575 } | 582 } |
(...skipping 29 matching lines...) Expand all Loading... | |
605 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," | 612 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," |
606 " \"ampUrl\" : \"http://localhost/amp\"," | 613 " \"ampUrl\" : \"http://localhost/amp\"," |
607 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 614 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
608 " }]" | 615 " }]" |
609 "}]}"; | 616 "}]}"; |
610 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 617 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
611 net::URLRequestStatus::SUCCESS); | 618 net::URLRequestStatus::SUCCESS); |
612 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; | 619 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
613 EXPECT_CALL(mock_callback(), Run(_)) | 620 EXPECT_CALL(mock_callback(), Run(_)) |
614 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); | 621 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); |
615 snippets_fetcher().FetchSnippets(test_params()); | 622 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
616 FastForwardUntilNoTasksRemain(); | 623 FastForwardUntilNoTasksRemain(); |
617 | 624 |
618 ASSERT_TRUE(fetched_categories); | 625 ASSERT_TRUE(fetched_categories); |
619 ASSERT_THAT(fetched_categories->size(), Eq(2u)); | 626 ASSERT_THAT(fetched_categories->size(), Eq(2u)); |
620 for (const auto& category : *fetched_categories) { | 627 for (const auto& category : *fetched_categories) { |
621 const auto& articles = category.snippets; | 628 const auto& articles = category.snippets; |
622 switch (category.category.id()) { | 629 switch (category.category.id()) { |
623 case static_cast<int>(KnownCategories::ARTICLES): | 630 case static_cast<int>(KnownCategories::ARTICLES): |
624 ASSERT_THAT(articles.size(), Eq(1u)); | 631 ASSERT_THAT(articles.size(), Eq(1u)); |
625 EXPECT_THAT(articles[0]->best_source().url.spec(), | 632 EXPECT_THAT(articles[0]->best_source().url.spec(), |
(...skipping 12 matching lines...) Expand all Loading... | |
638 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 645 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
639 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 646 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
640 EXPECT_THAT(histogram_tester().GetAllSamples( | 647 EXPECT_THAT(histogram_tester().GetAllSamples( |
641 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 648 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
642 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 649 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
643 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 650 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
644 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 651 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
645 /*count=*/1))); | 652 /*count=*/1))); |
646 } | 653 } |
647 | 654 |
655 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ExclusiveCategoryOnly) { | |
656 const std::string kJsonStr = | |
657 "{\"categories\" : [{" | |
658 " \"id\": 1," | |
659 " \"localizedTitle\": \"Articles for You\"," | |
660 " \"suggestions\" : [{" | |
661 " \"ids\" : [\"http://localhost/foobar\"]," | |
662 " \"title\" : \"Foo Barred from Baz\"," | |
663 " \"snippet\" : \"...\"," | |
664 " \"fullPageUrl\" : \"http://localhost/foobar\"," | |
665 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," | |
666 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," | |
667 " \"attribution\" : \"Foo News\"," | |
668 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," | |
669 " \"ampUrl\" : \"http://localhost/amp\"," | |
670 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | |
671 " }]" | |
672 "}, {" | |
673 " \"id\": 2," | |
674 " \"localizedTitle\": \"Articles for Me\"," | |
675 " \"suggestions\" : [{" | |
676 " \"ids\" : [\"http://localhost/foo2\"]," | |
677 " \"title\" : \"Foo Barred from Baz\"," | |
678 " \"snippet\" : \"...\"," | |
679 " \"fullPageUrl\" : \"http://localhost/foo2\"," | |
680 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," | |
681 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," | |
682 " \"attribution\" : \"Foo News\"," | |
683 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," | |
684 " \"ampUrl\" : \"http://localhost/amp\"," | |
685 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | |
686 " }]" | |
687 "}]}"; | |
688 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | |
689 net::URLRequestStatus::SUCCESS); | |
690 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; | |
691 EXPECT_CALL(mock_callback(), Run(_)) | |
692 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); | |
693 | |
694 NTPSnippetsFetcher::Params params = test_params(); | |
695 params.exclusive_category = OptionalArticlesCategory(); | |
696 snippets_fetcher().FetchSnippets(params, MakeMockCallback()); | |
tschumann
2016/11/03 09:20:58
it would be cleaner to pass-in mock_callback(). Ot
| |
697 FastForwardUntilNoTasksRemain(); | |
698 | |
699 ASSERT_TRUE(fetched_categories); | |
700 ASSERT_THAT(fetched_categories->size(), Eq(1u)); | |
701 for (const auto& category : *fetched_categories) { | |
tschumann
2016/11/03 09:20:58
if you already enforce the size of 1, then there's
| |
702 const auto& articles = category.snippets; | |
703 switch (category.category.id()) { | |
704 case static_cast<int>(KnownCategories::ARTICLES): | |
705 ASSERT_THAT(articles.size(), Eq(1u)); | |
706 EXPECT_THAT(articles[0]->best_source().url.spec(), | |
707 Eq("http://localhost/foobar")); | |
708 break; | |
709 default: | |
710 FAIL() << "unexpected category with ID " << category.category.id(); | |
711 } | |
712 } | |
713 } | |
714 | |
648 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { | 715 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
649 const std::string kJsonStr = "{\"recos\": []}"; | 716 const std::string kJsonStr = "{\"recos\": []}"; |
650 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 717 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
651 net::URLRequestStatus::SUCCESS); | 718 net::URLRequestStatus::SUCCESS); |
652 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 719 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
653 snippets_fetcher().FetchSnippets(test_params()); | 720 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
654 FastForwardUntilNoTasksRemain(); | 721 FastForwardUntilNoTasksRemain(); |
655 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 722 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
656 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 723 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
657 EXPECT_THAT( | 724 EXPECT_THAT( |
658 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 725 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
659 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 726 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
660 EXPECT_THAT(histogram_tester().GetAllSamples( | 727 EXPECT_THAT(histogram_tester().GetAllSamples( |
661 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 728 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
662 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 729 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
663 } | 730 } |
664 | 731 |
665 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { | 732 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { |
666 net::TestURLFetcherFactory test_url_fetcher_factory; | 733 net::TestURLFetcherFactory test_url_fetcher_factory; |
667 NTPSnippetsFetcher::Params params = test_params(); | 734 NTPSnippetsFetcher::Params params = test_params(); |
668 params.hosts = {"www.somehost1.com", "www.somehost2.com"}; | 735 params.hosts = {"www.somehost1.com", "www.somehost2.com"}; |
669 params.count_to_fetch = 17; | 736 params.count_to_fetch = 17; |
670 snippets_fetcher().FetchSnippets(params); | 737 snippets_fetcher().FetchSnippets(params, MakeMockCallback()); |
671 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); | 738 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
672 ASSERT_THAT(fetcher, NotNull()); | 739 ASSERT_THAT(fetcher, NotNull()); |
673 std::unique_ptr<base::Value> value = | 740 std::unique_ptr<base::Value> value = |
674 base::JSONReader::Read(fetcher->upload_data()); | 741 base::JSONReader::Read(fetcher->upload_data()); |
675 ASSERT_TRUE(value) << " failed to parse JSON: " | 742 ASSERT_TRUE(value) << " failed to parse JSON: " |
676 << PrintToString(fetcher->upload_data()); | 743 << PrintToString(fetcher->upload_data()); |
677 const base::DictionaryValue* dict = nullptr; | 744 const base::DictionaryValue* dict = nullptr; |
678 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 745 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
679 const base::DictionaryValue* local_scoring_params = nullptr; | 746 const base::DictionaryValue* local_scoring_params = nullptr; |
680 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", | 747 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", |
681 &local_scoring_params)); | 748 &local_scoring_params)); |
682 const base::ListValue* content_selectors = nullptr; | 749 const base::ListValue* content_selectors = nullptr; |
683 ASSERT_TRUE( | 750 ASSERT_TRUE( |
684 local_scoring_params->GetList("content_selectors", &content_selectors)); | 751 local_scoring_params->GetList("content_selectors", &content_selectors)); |
685 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2))); | 752 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2))); |
686 const base::DictionaryValue* content_selector = nullptr; | 753 const base::DictionaryValue* content_selector = nullptr; |
687 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); | 754 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); |
688 std::string content_selector_value; | 755 std::string content_selector_value; |
689 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 756 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
690 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); | 757 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); |
691 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); | 758 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); |
692 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 759 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
693 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); | 760 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); |
694 } | 761 } |
695 | 762 |
696 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | 763 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
697 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, | 764 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
698 net::URLRequestStatus::FAILED); | 765 net::URLRequestStatus::FAILED); |
699 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 766 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
700 snippets_fetcher().FetchSnippets(test_params()); | 767 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
701 FastForwardUntilNoTasksRemain(); | 768 FastForwardUntilNoTasksRemain(); |
702 EXPECT_THAT(snippets_fetcher().last_status(), | 769 EXPECT_THAT(snippets_fetcher().last_status(), |
703 Eq("URLRequestStatus error -2")); | 770 Eq("URLRequestStatus error -2")); |
704 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 771 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
705 EXPECT_THAT( | 772 EXPECT_THAT( |
706 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 773 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
707 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); | 774 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); |
708 EXPECT_THAT(histogram_tester().GetAllSamples( | 775 EXPECT_THAT(histogram_tester().GetAllSamples( |
709 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 776 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
710 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); | 777 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); |
711 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 778 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
712 Not(IsEmpty())); | 779 Not(IsEmpty())); |
713 } | 780 } |
714 | 781 |
715 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | 782 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
716 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, | 783 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
717 net::URLRequestStatus::SUCCESS); | 784 net::URLRequestStatus::SUCCESS); |
718 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 785 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
719 snippets_fetcher().FetchSnippets(test_params()); | 786 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
720 FastForwardUntilNoTasksRemain(); | 787 FastForwardUntilNoTasksRemain(); |
721 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 788 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
722 EXPECT_THAT( | 789 EXPECT_THAT( |
723 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 790 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
724 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); | 791 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); |
725 EXPECT_THAT(histogram_tester().GetAllSamples( | 792 EXPECT_THAT(histogram_tester().GetAllSamples( |
726 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 793 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
727 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); | 794 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); |
728 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 795 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
729 Not(IsEmpty())); | 796 Not(IsEmpty())); |
730 } | 797 } |
731 | 798 |
732 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { | 799 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
733 const std::string kInvalidJsonStr = "{ \"recos\": []"; | 800 const std::string kInvalidJsonStr = "{ \"recos\": []"; |
734 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, | 801 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, |
735 net::URLRequestStatus::SUCCESS); | 802 net::URLRequestStatus::SUCCESS); |
736 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 803 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
737 snippets_fetcher().FetchSnippets(test_params()); | 804 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
738 FastForwardUntilNoTasksRemain(); | 805 FastForwardUntilNoTasksRemain(); |
739 EXPECT_THAT(snippets_fetcher().last_status(), | 806 EXPECT_THAT(snippets_fetcher().last_status(), |
740 StartsWith("Received invalid JSON (error ")); | 807 StartsWith("Received invalid JSON (error ")); |
741 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); | 808 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); |
742 EXPECT_THAT( | 809 EXPECT_THAT( |
743 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 810 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
744 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 811 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
745 EXPECT_THAT(histogram_tester().GetAllSamples( | 812 EXPECT_THAT(histogram_tester().GetAllSamples( |
746 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 813 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
747 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 814 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
748 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 815 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
749 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 816 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
750 /*count=*/1))); | 817 /*count=*/1))); |
751 } | 818 } |
752 | 819 |
753 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { | 820 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
754 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, | 821 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, |
755 net::URLRequestStatus::SUCCESS); | 822 net::URLRequestStatus::SUCCESS); |
756 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 823 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
757 snippets_fetcher().FetchSnippets(test_params()); | 824 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
758 FastForwardUntilNoTasksRemain(); | 825 FastForwardUntilNoTasksRemain(); |
759 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); | 826 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); |
760 EXPECT_THAT( | 827 EXPECT_THAT( |
761 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 828 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
762 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 829 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
763 EXPECT_THAT(histogram_tester().GetAllSamples( | 830 EXPECT_THAT(histogram_tester().GetAllSamples( |
764 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 831 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
765 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 832 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
766 } | 833 } |
767 | 834 |
768 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { | 835 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
769 const std::string kJsonStr = | 836 const std::string kJsonStr = |
770 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; | 837 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; |
771 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 838 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
772 net::URLRequestStatus::SUCCESS); | 839 net::URLRequestStatus::SUCCESS); |
773 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 840 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
774 snippets_fetcher().FetchSnippets(test_params()); | 841 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
775 FastForwardUntilNoTasksRemain(); | 842 FastForwardUntilNoTasksRemain(); |
776 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 843 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
777 EXPECT_THAT( | 844 EXPECT_THAT( |
778 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 845 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
779 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); | 846 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); |
780 EXPECT_THAT(histogram_tester().GetAllSamples( | 847 EXPECT_THAT(histogram_tester().GetAllSamples( |
781 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 848 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
782 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 849 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
783 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 850 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
784 Not(IsEmpty())); | 851 Not(IsEmpty())); |
785 } | 852 } |
786 | 853 |
787 // This test actually verifies that the test setup itself is sane, to prevent | 854 // This test actually verifies that the test setup itself is sane, to prevent |
788 // hard-to-reproduce test failures. | 855 // hard-to-reproduce test failures. |
789 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { | 856 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { |
790 InitFakeURLFetcherFactory(); | 857 InitFakeURLFetcherFactory(); |
791 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 858 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
792 snippets_fetcher().FetchSnippets(test_params()); | 859 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
793 FastForwardUntilNoTasksRemain(); | 860 FastForwardUntilNoTasksRemain(); |
794 } | 861 } |
795 | 862 |
796 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | 863 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
797 const std::string kJsonStr = "{ \"recos\": [] }"; | 864 const std::string kJsonStr = "{ \"recos\": [] }"; |
798 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 865 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
799 net::URLRequestStatus::SUCCESS); | 866 net::URLRequestStatus::SUCCESS); |
800 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 867 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
801 snippets_fetcher().FetchSnippets(test_params()); | 868 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
802 // Second call to FetchSnippets() overrides/cancels the previous. | 869 // Second call to FetchSnippets() overrides/cancels the previous. |
803 // Callback is expected to be called once. | 870 // Callback is expected to be called once. |
804 snippets_fetcher().FetchSnippets(test_params()); | 871 snippets_fetcher().FetchSnippets(test_params(), MakeMockCallback()); |
805 FastForwardUntilNoTasksRemain(); | 872 FastForwardUntilNoTasksRemain(); |
806 EXPECT_THAT( | 873 EXPECT_THAT( |
807 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 874 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
808 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 875 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
809 EXPECT_THAT(histogram_tester().GetAllSamples( | 876 EXPECT_THAT(histogram_tester().GetAllSamples( |
810 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 877 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
811 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 878 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
812 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 879 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
813 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 880 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
814 /*count=*/1))); | 881 /*count=*/1))); |
815 } | 882 } |
816 | 883 |
817 ::std::ostream& operator<<( | 884 ::std::ostream& operator<<( |
818 ::std::ostream& os, | 885 ::std::ostream& os, |
819 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 886 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
820 if (fetched_categories) { | 887 if (fetched_categories) { |
821 // Matchers above aren't any more precise than this, so this is sufficient | 888 // Matchers above aren't any more precise than this, so this is sufficient |
822 // for test-failure diagnostics. | 889 // for test-failure diagnostics. |
823 return os << "list with " << fetched_categories->size() << " elements"; | 890 return os << "list with " << fetched_categories->size() << " elements"; |
824 } | 891 } |
825 return os << "null"; | 892 return os << "null"; |
826 } | 893 } |
827 | 894 |
828 } // namespace ntp_snippets | 895 } // namespace ntp_snippets |
OLD | NEW |