| 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/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/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 20 matching lines...) Expand all Loading... |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 32 |
| 33 namespace ntp_snippets { | 33 namespace ntp_snippets { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 using testing::_; | 37 using testing::_; |
| 38 using testing::ElementsAre; | 38 using testing::ElementsAre; |
| 39 using testing::Eq; | 39 using testing::Eq; |
| 40 using testing::IsEmpty; | 40 using testing::IsEmpty; |
| 41 using testing::IsNull; | |
| 42 using testing::Not; | 41 using testing::Not; |
| 43 using testing::NotNull; | 42 using testing::NotNull; |
| 44 using testing::PrintToString; | 43 using testing::PrintToString; |
| 45 using testing::SizeIs; | |
| 46 using testing::StartsWith; | 44 using testing::StartsWith; |
| 47 using testing::WithArg; | 45 using testing::WithArg; |
| 48 | 46 |
| 49 const char kAPIKey[] = "fakeAPIkey"; | 47 const char kAPIKey[] = "fakeAPIkey"; |
| 50 const char kTestChromeReaderUrl[] = | 48 const char kTestChromeReaderUrl[] = |
| 51 "https://chromereader-pa.googleapis.com/v1/fetch?key=fakeAPIkey"; | 49 "https://chromereader-pa.googleapis.com/v1/fetch?key=fakeAPIkey"; |
| 52 const char kTestChromeContentSuggestionsUrl[] = | 50 const char kTestChromeContentSuggestionsUrl[] = |
| 53 "https://chromecontentsuggestions-pa.googleapis.com/v1/suggestions/" | 51 "https://chromecontentsuggestions-pa.googleapis.com/v1/suggestions/" |
| 54 "fetch?key=fakeAPIkey"; | 52 "fetch?key=fakeAPIkey"; |
| 55 | 53 |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 "{\"recos\": [{" | 416 "{\"recos\": [{" |
| 419 " \"contentInfo\": {" | 417 " \"contentInfo\": {" |
| 420 " \"url\" : \"http://localhost/foobar\"," | 418 " \"url\" : \"http://localhost/foobar\"," |
| 421 " \"sourceCorpusInfo\" : [{" | 419 " \"sourceCorpusInfo\" : [{" |
| 422 " \"ampUrl\" : \"http://localhost/amp\"," | 420 " \"ampUrl\" : \"http://localhost/amp\"," |
| 423 " \"corpusId\" : \"http://localhost/foobar\"," | 421 " \"corpusId\" : \"http://localhost/foobar\"," |
| 424 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" | 422 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" |
| 425 " }]" | 423 " }]" |
| 426 " }" | 424 " }" |
| 427 "}]}"; | 425 "}]}"; |
| 428 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 426 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 429 net::URLRequestStatus::SUCCESS); | 427 net::URLRequestStatus::SUCCESS); |
| 430 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 428 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| 431 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 429 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 432 test_excluded(), | 430 test_excluded(), |
| 433 /*count=*/1, | 431 /*count=*/1, |
| 434 /*interactive_request=*/true); | 432 /*interactive_request=*/true); |
| 435 FastForwardUntilNoTasksRemain(); | 433 FastForwardUntilNoTasksRemain(); |
| 436 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 434 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 437 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 435 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 438 EXPECT_THAT(histogram_tester().GetAllSamples( | 436 EXPECT_THAT(histogram_tester().GetAllSamples( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 454 " \"snippet\" : \"...\"," | 452 " \"snippet\" : \"...\"," |
| 455 " \"fullPageUrl\" : \"http://localhost/foobar\"," | 453 " \"fullPageUrl\" : \"http://localhost/foobar\"," |
| 456 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," | 454 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," |
| 457 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," | 455 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
| 458 " \"attribution\" : \"Foo News\"," | 456 " \"attribution\" : \"Foo News\"," |
| 459 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," | 457 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
| 460 " \"ampUrl\" : \"http://localhost/amp\"," | 458 " \"ampUrl\" : \"http://localhost/amp\"," |
| 461 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 459 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 462 " }]" | 460 " }]" |
| 463 "}]}"; | 461 "}]}"; |
| 464 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 462 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 465 net::URLRequestStatus::SUCCESS); | 463 net::URLRequestStatus::SUCCESS); |
| 466 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 464 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| 467 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 465 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 468 test_excluded(), | 466 test_excluded(), |
| 469 /*count=*/1, | 467 /*count=*/1, |
| 470 /*interactive_request=*/true); | 468 /*interactive_request=*/true); |
| 471 FastForwardUntilNoTasksRemain(); | 469 FastForwardUntilNoTasksRemain(); |
| 472 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 470 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 473 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 471 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 474 EXPECT_THAT(histogram_tester().GetAllSamples( | 472 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 475 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 473 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 476 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 474 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 477 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 475 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 478 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 476 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 479 /*count=*/1))); | 477 /*count=*/1))); |
| 480 } | 478 } |
| 481 | 479 |
| 482 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { | 480 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { |
| 483 const std::string kJsonStr = | 481 const std::string kJsonStr = |
| 484 "{\"categories\" : [{" | 482 "{\"categories\" : [{" |
| 485 " \"id\": 1," | 483 " \"id\": 1," |
| 486 " \"localizedTitle\": \"Articles for You\"" | 484 " \"localizedTitle\": \"Articles for You\"" |
| 487 "}]}"; | 485 "}]}"; |
| 488 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 486 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 489 net::URLRequestStatus::SUCCESS); | 487 net::URLRequestStatus::SUCCESS); |
| 490 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 488 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 491 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 489 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 492 test_excluded(), | 490 test_excluded(), |
| 493 /*count=*/1, | 491 /*count=*/1, |
| 494 /*interactive_request=*/true); | 492 /*interactive_request=*/true); |
| 495 FastForwardUntilNoTasksRemain(); | 493 FastForwardUntilNoTasksRemain(); |
| 496 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 494 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 497 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 495 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 498 EXPECT_THAT(histogram_tester().GetAllSamples( | 496 EXPECT_THAT(histogram_tester().GetAllSamples( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 529 " \"snippet\" : \"...\"," | 527 " \"snippet\" : \"...\"," |
| 530 " \"fullPageUrl\" : \"http://localhost/foo2\"," | 528 " \"fullPageUrl\" : \"http://localhost/foo2\"," |
| 531 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," | 529 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," |
| 532 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," | 530 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
| 533 " \"attribution\" : \"Foo News\"," | 531 " \"attribution\" : \"Foo News\"," |
| 534 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," | 532 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," |
| 535 " \"ampUrl\" : \"http://localhost/amp\"," | 533 " \"ampUrl\" : \"http://localhost/amp\"," |
| 536 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 534 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 537 " }]" | 535 " }]" |
| 538 "}]}"; | 536 "}]}"; |
| 539 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 537 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 540 net::URLRequestStatus::SUCCESS); | 538 net::URLRequestStatus::SUCCESS); |
| 541 NTPSnippetsFetcher::OptionalSnippets snippets; | 539 NTPSnippetsFetcher::OptionalSnippets snippets; |
| 542 EXPECT_CALL(mock_callback(), Run(_)) | 540 EXPECT_CALL(mock_callback(), Run(_)) |
| 543 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); | 541 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); |
| 544 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 542 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 545 test_excluded(), | 543 test_excluded(), |
| 546 /*count=*/1, | 544 /*count=*/1, |
| 547 /*force_request=*/true); | 545 /*interactive_request=*/true); |
| 548 FastForwardUntilNoTasksRemain(); | 546 FastForwardUntilNoTasksRemain(); |
| 549 | 547 |
| 550 ASSERT_TRUE(snippets); | 548 ASSERT_TRUE(snippets); |
| 551 ASSERT_THAT(snippets->size(), Eq(2u)); | 549 ASSERT_THAT(snippets->size(), Eq(2u)); |
| 552 for (const auto& category : *snippets) { | 550 for (const auto& category : *snippets) { |
| 553 const auto& articles = category.snippets; | 551 const auto& articles = category.snippets; |
| 554 switch (category.category.id()) { | 552 switch (category.category.id()) { |
| 555 case static_cast<int>(KnownCategories::ARTICLES): | 553 case static_cast<int>(KnownCategories::ARTICLES): |
| 556 ASSERT_THAT(articles.size(), Eq(1u)); | 554 ASSERT_THAT(articles.size(), Eq(1u)); |
| 557 EXPECT_THAT(articles[0]->best_source().url.spec(), | 555 EXPECT_THAT(articles[0]->best_source().url.spec(), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 572 EXPECT_THAT(histogram_tester().GetAllSamples( | 570 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 573 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 571 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 574 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 572 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 575 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 573 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 576 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 574 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 577 /*count=*/1))); | 575 /*count=*/1))); |
| 578 } | 576 } |
| 579 | 577 |
| 580 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { | 578 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
| 581 const std::string kJsonStr = "{\"recos\": []}"; | 579 const std::string kJsonStr = "{\"recos\": []}"; |
| 582 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 580 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 583 net::URLRequestStatus::SUCCESS); | 581 net::URLRequestStatus::SUCCESS); |
| 584 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 582 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 585 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 583 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 586 test_excluded(), | 584 test_excluded(), |
| 587 /*count=*/1, | 585 /*count=*/1, |
| 588 /*interactive_request=*/true); | 586 /*interactive_request=*/true); |
| 589 FastForwardUntilNoTasksRemain(); | 587 FastForwardUntilNoTasksRemain(); |
| 590 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 588 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 591 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 589 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 592 EXPECT_THAT( | 590 EXPECT_THAT( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); | 643 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); |
| 646 std::string content_selector_value; | 644 std::string content_selector_value; |
| 647 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 645 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
| 648 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); | 646 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); |
| 649 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); | 647 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); |
| 650 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 648 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
| 651 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); | 649 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); |
| 652 } | 650 } |
| 653 | 651 |
| 654 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | 652 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| 655 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 653 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
| 656 net::URLRequestStatus::FAILED); | 654 net::URLRequestStatus::FAILED); |
| 657 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 655 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 658 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 656 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 659 test_excluded(), | 657 test_excluded(), |
| 660 /*count=*/1, | 658 /*count=*/1, |
| 661 /*interactive_request=*/true); | 659 /*interactive_request=*/true); |
| 662 FastForwardUntilNoTasksRemain(); | 660 FastForwardUntilNoTasksRemain(); |
| 663 EXPECT_THAT(snippets_fetcher().last_status(), | 661 EXPECT_THAT(snippets_fetcher().last_status(), |
| 664 Eq("URLRequestStatus error -2")); | 662 Eq("URLRequestStatus error -2")); |
| 665 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 663 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 666 EXPECT_THAT( | 664 EXPECT_THAT( |
| 667 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 665 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 668 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); | 666 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); |
| 669 EXPECT_THAT(histogram_tester().GetAllSamples( | 667 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 670 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 668 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 671 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); | 669 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); |
| 672 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 670 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 673 Not(IsEmpty())); | 671 Not(IsEmpty())); |
| 674 } | 672 } |
| 675 | 673 |
| 676 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | 674 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
| 677 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 675 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
| 678 net::URLRequestStatus::SUCCESS); | 676 net::URLRequestStatus::SUCCESS); |
| 679 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 677 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 680 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 678 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 681 test_excluded(), | 679 test_excluded(), |
| 682 /*count=*/1, | 680 /*count=*/1, |
| 683 /*interactive_request=*/true); | 681 /*interactive_request=*/true); |
| 684 FastForwardUntilNoTasksRemain(); | 682 FastForwardUntilNoTasksRemain(); |
| 685 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 683 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 686 EXPECT_THAT( | 684 EXPECT_THAT( |
| 687 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 685 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 688 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); | 686 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); |
| 689 EXPECT_THAT(histogram_tester().GetAllSamples( | 687 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 690 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 688 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 691 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); | 689 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); |
| 692 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 690 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 693 Not(IsEmpty())); | 691 Not(IsEmpty())); |
| 694 } | 692 } |
| 695 | 693 |
| 696 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { | 694 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
| 697 const std::string kInvalidJsonStr = "{ \"recos\": []"; | 695 const std::string kInvalidJsonStr = "{ \"recos\": []"; |
| 698 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, | 696 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, |
| 699 net::URLRequestStatus::SUCCESS); | 697 net::URLRequestStatus::SUCCESS); |
| 700 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 698 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 701 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 699 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 702 test_excluded(), | 700 test_excluded(), |
| 703 /*count=*/1, | 701 /*count=*/1, |
| 704 /*interactive_request=*/true); | 702 /*interactive_request=*/true); |
| 705 FastForwardUntilNoTasksRemain(); | 703 FastForwardUntilNoTasksRemain(); |
| 706 EXPECT_THAT(snippets_fetcher().last_status(), | 704 EXPECT_THAT(snippets_fetcher().last_status(), |
| 707 StartsWith("Received invalid JSON (error ")); | 705 StartsWith("Received invalid JSON (error ")); |
| 708 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); | 706 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); |
| 709 EXPECT_THAT( | 707 EXPECT_THAT( |
| 710 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 708 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 711 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 709 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
| 712 EXPECT_THAT(histogram_tester().GetAllSamples( | 710 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 713 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 711 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 714 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 712 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 715 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 713 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 716 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 714 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 717 /*count=*/1))); | 715 /*count=*/1))); |
| 718 } | 716 } |
| 719 | 717 |
| 720 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { | 718 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
| 721 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, | 719 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, |
| 722 net::URLRequestStatus::SUCCESS); | 720 net::URLRequestStatus::SUCCESS); |
| 723 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 721 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 724 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 722 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 725 test_excluded(), | 723 test_excluded(), |
| 726 /*count=*/1, | 724 /*count=*/1, |
| 727 /*interactive_request=*/true); | 725 /*interactive_request=*/true); |
| 728 FastForwardUntilNoTasksRemain(); | 726 FastForwardUntilNoTasksRemain(); |
| 729 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); | 727 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); |
| 730 EXPECT_THAT( | 728 EXPECT_THAT( |
| 731 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 729 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 732 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 730 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
| 733 EXPECT_THAT(histogram_tester().GetAllSamples( | 731 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 734 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 732 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 735 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 733 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 736 } | 734 } |
| 737 | 735 |
| 738 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { | 736 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
| 739 const std::string kJsonStr = | 737 const std::string kJsonStr = |
| 740 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; | 738 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; |
| 741 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 739 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 742 net::URLRequestStatus::SUCCESS); | 740 net::URLRequestStatus::SUCCESS); |
| 743 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 741 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 744 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 742 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 745 test_excluded(), | 743 test_excluded(), |
| 746 /*count=*/1, | 744 /*count=*/1, |
| 747 /*interactive_request=*/true); | 745 /*interactive_request=*/true); |
| 748 FastForwardUntilNoTasksRemain(); | 746 FastForwardUntilNoTasksRemain(); |
| 749 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 747 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 750 EXPECT_THAT( | 748 EXPECT_THAT( |
| 751 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 749 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 764 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 762 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 765 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 763 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 766 test_excluded(), | 764 test_excluded(), |
| 767 /*count=*/1, | 765 /*count=*/1, |
| 768 /*interactive_request=*/true); | 766 /*interactive_request=*/true); |
| 769 FastForwardUntilNoTasksRemain(); | 767 FastForwardUntilNoTasksRemain(); |
| 770 } | 768 } |
| 771 | 769 |
| 772 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | 770 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
| 773 const std::string kJsonStr = "{ \"recos\": [] }"; | 771 const std::string kJsonStr = "{ \"recos\": [] }"; |
| 774 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 772 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 775 net::URLRequestStatus::SUCCESS); | 773 net::URLRequestStatus::SUCCESS); |
| 776 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 774 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 777 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 775 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 778 test_excluded(), | 776 test_excluded(), |
| 779 /*count=*/1, | 777 /*count=*/1, |
| 780 /*interactive_request=*/true); | 778 /*interactive_request=*/true); |
| 781 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. | 779 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. |
| 782 // Callback is expected to be called once. | 780 // Callback is expected to be called once. |
| 783 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 781 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 784 test_excluded(), | 782 test_excluded(), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 796 /*count=*/1))); | 794 /*count=*/1))); |
| 797 } | 795 } |
| 798 | 796 |
| 799 ::std::ostream& operator<<( | 797 ::std::ostream& operator<<( |
| 800 ::std::ostream& os, | 798 ::std::ostream& os, |
| 801 const NTPSnippetsFetcher::OptionalSnippets& snippets) { | 799 const NTPSnippetsFetcher::OptionalSnippets& snippets) { |
| 802 if (snippets) { | 800 if (snippets) { |
| 803 // Matchers above aren't any more precise than this, so this is sufficient | 801 // Matchers above aren't any more precise than this, so this is sufficient |
| 804 // for test-failure diagnostics. | 802 // for test-failure diagnostics. |
| 805 return os << "list with " << snippets->size() << " elements"; | 803 return os << "list with " << snippets->size() << " elements"; |
| 806 } else { | |
| 807 return os << "null"; | |
| 808 } | 804 } |
| 805 return os << "null"; |
| 809 } | 806 } |
| 810 | 807 |
| 811 } // namespace ntp_snippets | 808 } // namespace ntp_snippets |
| OLD | NEW |