| 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 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 " \"corpusId\" : \"http://localhost/foobar\"," | 372 " \"corpusId\" : \"http://localhost/foobar\"," |
| 373 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" | 373 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" |
| 374 " }]" | 374 " }]" |
| 375 " }" | 375 " }" |
| 376 "}]}"; | 376 "}]}"; |
| 377 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 377 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 378 net::URLRequestStatus::SUCCESS); | 378 net::URLRequestStatus::SUCCESS); |
| 379 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 379 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| 380 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 380 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 381 /*count=*/1, | 381 /*count=*/1, |
| 382 /*force_request=*/true); | 382 /*interactive_request=*/true); |
| 383 FastForwardUntilNoTasksRemain(); | 383 FastForwardUntilNoTasksRemain(); |
| 384 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 384 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 385 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 385 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 386 EXPECT_THAT(histogram_tester().GetAllSamples( | 386 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 387 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 387 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 388 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 388 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 389 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 389 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 390 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 390 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 391 /*count=*/1))); | 391 /*count=*/1))); |
| 392 } | 392 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 407 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," | 407 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
| 408 " \"ampUrl\" : \"http://localhost/amp\"," | 408 " \"ampUrl\" : \"http://localhost/amp\"," |
| 409 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 409 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 410 " }]" | 410 " }]" |
| 411 "}]}"; | 411 "}]}"; |
| 412 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 412 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 413 net::URLRequestStatus::SUCCESS); | 413 net::URLRequestStatus::SUCCESS); |
| 414 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 414 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| 415 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 415 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 416 /*count=*/1, | 416 /*count=*/1, |
| 417 /*force_request=*/true); | 417 /*interactive_request=*/true); |
| 418 FastForwardUntilNoTasksRemain(); | 418 FastForwardUntilNoTasksRemain(); |
| 419 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 419 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 420 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 420 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 421 EXPECT_THAT(histogram_tester().GetAllSamples( | 421 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 422 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 422 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 423 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 423 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 424 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 424 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 425 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 425 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 426 /*count=*/1))); | 426 /*count=*/1))); |
| 427 } | 427 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 /*count=*/1))); | 499 /*count=*/1))); |
| 500 } | 500 } |
| 501 | 501 |
| 502 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { | 502 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
| 503 const std::string kJsonStr = "{\"recos\": []}"; | 503 const std::string kJsonStr = "{\"recos\": []}"; |
| 504 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 504 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 505 net::URLRequestStatus::SUCCESS); | 505 net::URLRequestStatus::SUCCESS); |
| 506 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 506 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 507 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 507 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 508 /*count=*/1, | 508 /*count=*/1, |
| 509 /*force_request=*/true); | 509 /*interactive_request=*/true); |
| 510 FastForwardUntilNoTasksRemain(); | 510 FastForwardUntilNoTasksRemain(); |
| 511 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 511 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 512 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 512 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 513 EXPECT_THAT( | 513 EXPECT_THAT( |
| 514 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 514 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 515 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 515 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| 516 EXPECT_THAT(histogram_tester().GetAllSamples( | 516 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 517 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 517 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 518 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 518 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 519 } | 519 } |
| 520 | 520 |
| 521 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { | 521 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { |
| 522 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 522 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 523 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), | 523 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), |
| 524 /*language_code=*/"en-US", | 524 /*language_code=*/"en-US", |
| 525 /*count=*/1, | 525 /*count=*/1, |
| 526 /*force_request=*/true); | 526 /*interactive_request=*/true); |
| 527 FastForwardUntilNoTasksRemain(); | 527 FastForwardUntilNoTasksRemain(); |
| 528 EXPECT_THAT(snippets_fetcher().last_status(), | 528 EXPECT_THAT(snippets_fetcher().last_status(), |
| 529 Eq("Cannot fetch for empty hosts list.")); | 529 Eq("Cannot fetch for empty hosts list.")); |
| 530 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 530 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 531 EXPECT_THAT( | 531 EXPECT_THAT( |
| 532 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 532 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 533 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 533 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| 534 // This particular error gets triggered prior to fetching, so no fetch time | 534 // This particular error gets triggered prior to fetching, so no fetch time |
| 535 // or response should get recorded. | 535 // or response should get recorded. |
| 536 EXPECT_THAT(histogram_tester().GetAllSamples( | 536 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 537 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 537 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 538 IsEmpty()); | 538 IsEmpty()); |
| 539 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 539 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 540 IsEmpty()); | 540 IsEmpty()); |
| 541 } | 541 } |
| 542 | 542 |
| 543 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { | 543 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { |
| 544 net::TestURLFetcherFactory test_url_fetcher_factory; | 544 net::TestURLFetcherFactory test_url_fetcher_factory; |
| 545 snippets_fetcher().FetchSnippetsFromHosts( | 545 snippets_fetcher().FetchSnippetsFromHosts( |
| 546 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17, | 546 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17, |
| 547 /*force_request=*/true); | 547 /*interactive_request=*/true); |
| 548 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); | 548 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
| 549 ASSERT_THAT(fetcher, NotNull()); | 549 ASSERT_THAT(fetcher, NotNull()); |
| 550 std::unique_ptr<base::Value> value = | 550 std::unique_ptr<base::Value> value = |
| 551 base::JSONReader::Read(fetcher->upload_data()); | 551 base::JSONReader::Read(fetcher->upload_data()); |
| 552 ASSERT_TRUE(value) << " failed to parse JSON: " | 552 ASSERT_TRUE(value) << " failed to parse JSON: " |
| 553 << PrintToString(fetcher->upload_data()); | 553 << PrintToString(fetcher->upload_data()); |
| 554 const base::DictionaryValue* dict = nullptr; | 554 const base::DictionaryValue* dict = nullptr; |
| 555 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 555 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 556 const base::DictionaryValue* local_scoring_params = nullptr; | 556 const base::DictionaryValue* local_scoring_params = nullptr; |
| 557 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", | 557 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 569 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 569 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
| 570 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); | 570 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); |
| 571 } | 571 } |
| 572 | 572 |
| 573 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | 573 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| 574 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 574 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| 575 net::URLRequestStatus::FAILED); | 575 net::URLRequestStatus::FAILED); |
| 576 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 576 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 577 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 577 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 578 /*count=*/1, | 578 /*count=*/1, |
| 579 /*force_request=*/true); | 579 /*interactive_request=*/true); |
| 580 FastForwardUntilNoTasksRemain(); | 580 FastForwardUntilNoTasksRemain(); |
| 581 EXPECT_THAT(snippets_fetcher().last_status(), | 581 EXPECT_THAT(snippets_fetcher().last_status(), |
| 582 Eq("URLRequestStatus error -2")); | 582 Eq("URLRequestStatus error -2")); |
| 583 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 583 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 584 EXPECT_THAT( | 584 EXPECT_THAT( |
| 585 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 585 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 586 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); | 586 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); |
| 587 EXPECT_THAT(histogram_tester().GetAllSamples( | 587 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 588 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 588 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 589 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); | 589 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); |
| 590 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 590 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 591 Not(IsEmpty())); | 591 Not(IsEmpty())); |
| 592 } | 592 } |
| 593 | 593 |
| 594 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | 594 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
| 595 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 595 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| 596 net::URLRequestStatus::SUCCESS); | 596 net::URLRequestStatus::SUCCESS); |
| 597 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 597 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 598 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 598 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 599 /*count=*/1, | 599 /*count=*/1, |
| 600 /*force_request=*/true); | 600 /*interactive_request=*/true); |
| 601 FastForwardUntilNoTasksRemain(); | 601 FastForwardUntilNoTasksRemain(); |
| 602 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 602 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 603 EXPECT_THAT( | 603 EXPECT_THAT( |
| 604 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 604 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 605 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); | 605 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); |
| 606 EXPECT_THAT(histogram_tester().GetAllSamples( | 606 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 607 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 607 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 608 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); | 608 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); |
| 609 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 609 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 610 Not(IsEmpty())); | 610 Not(IsEmpty())); |
| 611 } | 611 } |
| 612 | 612 |
| 613 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { | 613 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
| 614 const std::string kInvalidJsonStr = "{ \"recos\": []"; | 614 const std::string kInvalidJsonStr = "{ \"recos\": []"; |
| 615 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, | 615 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, |
| 616 net::URLRequestStatus::SUCCESS); | 616 net::URLRequestStatus::SUCCESS); |
| 617 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 617 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 618 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 618 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 619 /*count=*/1, | 619 /*count=*/1, |
| 620 /*force_request=*/true); | 620 /*interactive_request=*/true); |
| 621 FastForwardUntilNoTasksRemain(); | 621 FastForwardUntilNoTasksRemain(); |
| 622 EXPECT_THAT(snippets_fetcher().last_status(), | 622 EXPECT_THAT(snippets_fetcher().last_status(), |
| 623 StartsWith("Received invalid JSON (error ")); | 623 StartsWith("Received invalid JSON (error ")); |
| 624 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); | 624 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); |
| 625 EXPECT_THAT( | 625 EXPECT_THAT( |
| 626 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 626 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 627 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 627 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
| 628 EXPECT_THAT(histogram_tester().GetAllSamples( | 628 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 629 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 629 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 630 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 630 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 631 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 631 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 632 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 632 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 633 /*count=*/1))); | 633 /*count=*/1))); |
| 634 } | 634 } |
| 635 | 635 |
| 636 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { | 636 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
| 637 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, | 637 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, |
| 638 net::URLRequestStatus::SUCCESS); | 638 net::URLRequestStatus::SUCCESS); |
| 639 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 639 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 640 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 640 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 641 /*count=*/1, | 641 /*count=*/1, |
| 642 /*force_request=*/true); | 642 /*interactive_request=*/true); |
| 643 FastForwardUntilNoTasksRemain(); | 643 FastForwardUntilNoTasksRemain(); |
| 644 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); | 644 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); |
| 645 EXPECT_THAT( | 645 EXPECT_THAT( |
| 646 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 646 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 647 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 647 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
| 648 EXPECT_THAT(histogram_tester().GetAllSamples( | 648 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 649 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 649 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 650 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 650 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 651 } | 651 } |
| 652 | 652 |
| 653 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { | 653 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
| 654 const std::string kJsonStr = | 654 const std::string kJsonStr = |
| 655 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; | 655 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; |
| 656 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 656 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 657 net::URLRequestStatus::SUCCESS); | 657 net::URLRequestStatus::SUCCESS); |
| 658 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 658 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 659 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 659 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 660 /*count=*/1, | 660 /*count=*/1, |
| 661 /*force_request=*/true); | 661 /*interactive_request=*/true); |
| 662 FastForwardUntilNoTasksRemain(); | 662 FastForwardUntilNoTasksRemain(); |
| 663 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 663 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 664 EXPECT_THAT( | 664 EXPECT_THAT( |
| 665 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 665 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 666 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); | 666 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); |
| 667 EXPECT_THAT(histogram_tester().GetAllSamples( | 667 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 668 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 668 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 669 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 669 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 670 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 670 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 671 Not(IsEmpty())); | 671 Not(IsEmpty())); |
| 672 } | 672 } |
| 673 | 673 |
| 674 // This test actually verifies that the test setup itself is sane, to prevent | 674 // This test actually verifies that the test setup itself is sane, to prevent |
| 675 // hard-to-reproduce test failures. | 675 // hard-to-reproduce test failures. |
| 676 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { | 676 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { |
| 677 InitFakeURLFetcherFactory(); | 677 InitFakeURLFetcherFactory(); |
| 678 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 678 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 679 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 679 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 680 /*count=*/1, | 680 /*count=*/1, |
| 681 /*force_request=*/true); | 681 /*interactive_request=*/true); |
| 682 FastForwardUntilNoTasksRemain(); | 682 FastForwardUntilNoTasksRemain(); |
| 683 } | 683 } |
| 684 | 684 |
| 685 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | 685 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
| 686 const std::string kJsonStr = "{ \"recos\": [] }"; | 686 const std::string kJsonStr = "{ \"recos\": [] }"; |
| 687 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 687 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 688 net::URLRequestStatus::SUCCESS); | 688 net::URLRequestStatus::SUCCESS); |
| 689 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 689 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 690 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 690 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 691 /*count=*/1, | 691 /*count=*/1, |
| 692 /*force_request=*/true); | 692 /*interactive_request=*/true); |
| 693 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. | 693 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. |
| 694 // Callback is expected to be called once. | 694 // Callback is expected to be called once. |
| 695 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 695 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 696 /*count=*/1, | 696 /*count=*/1, |
| 697 /*force_request=*/true); | 697 /*interactive_request=*/true); |
| 698 FastForwardUntilNoTasksRemain(); | 698 FastForwardUntilNoTasksRemain(); |
| 699 EXPECT_THAT( | 699 EXPECT_THAT( |
| 700 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 700 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 701 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 701 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| 702 EXPECT_THAT(histogram_tester().GetAllSamples( | 702 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 703 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 703 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 704 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 704 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 705 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 705 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 706 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 706 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 707 /*count=*/1))); | 707 /*count=*/1))); |
| 708 } | 708 } |
| 709 | 709 |
| 710 ::std::ostream& operator<<( | 710 ::std::ostream& operator<<( |
| 711 ::std::ostream& os, | 711 ::std::ostream& os, |
| 712 const NTPSnippetsFetcher::OptionalSnippets& snippets) { | 712 const NTPSnippetsFetcher::OptionalSnippets& snippets) { |
| 713 if (snippets) { | 713 if (snippets) { |
| 714 // Matchers above aren't any more precise than this, so this is sufficient | 714 // Matchers above aren't any more precise than this, so this is sufficient |
| 715 // for test-failure diagnostics. | 715 // for test-failure diagnostics. |
| 716 return os << "list with " << snippets->size() << " elements"; | 716 return os << "list with " << snippets->size() << " elements"; |
| 717 } else { | 717 } else { |
| 718 return os << "null"; | 718 return os << "null"; |
| 719 } | 719 } |
| 720 } | 720 } |
| 721 | 721 |
| 722 } // namespace ntp_snippets | 722 } // namespace ntp_snippets |
| OLD | NEW |