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