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