Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(413)

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher_unittest.cc

Issue 2227973002: Add request throttler to thumbnail fetching for articles on mobile NTP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tim's comments #2 Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 " \"corpusId\" : \"http://localhost/foobar\"," 360 " \"corpusId\" : \"http://localhost/foobar\","
361 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" 361 " \"publisherData\": { \"sourceName\" : \"Foo News\" }"
362 " }]" 362 " }]"
363 " }" 363 " }"
364 "}]}"; 364 "}]}";
365 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 365 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
366 net::URLRequestStatus::SUCCESS); 366 net::URLRequestStatus::SUCCESS);
367 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1); 367 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1);
368 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 368 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
369 /*count=*/1, 369 /*count=*/1,
370 /*force_request=*/true); 370 /*interactive_request=*/true);
371 FastForwardUntilNoTasksRemain(); 371 FastForwardUntilNoTasksRemain();
372 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 372 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
373 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 373 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
374 EXPECT_THAT(histogram_tester().GetAllSamples( 374 EXPECT_THAT(histogram_tester().GetAllSamples(
375 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 375 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
376 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 376 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
377 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 377 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
378 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 378 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
379 /*count=*/1))); 379 /*count=*/1)));
380 } 380 }
(...skipping 14 matching lines...) Expand all
395 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 395 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
396 " \"ampUrl\" : \"http://localhost/amp\"," 396 " \"ampUrl\" : \"http://localhost/amp\","
397 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 397 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
398 " }]" 398 " }]"
399 "}]}"; 399 "}]}";
400 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 400 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
401 net::URLRequestStatus::SUCCESS); 401 net::URLRequestStatus::SUCCESS);
402 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1); 402 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(1))).Times(1);
403 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 403 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
404 /*count=*/1, 404 /*count=*/1,
405 /*force_request=*/true); 405 /*interactive_request=*/true);
406 FastForwardUntilNoTasksRemain(); 406 FastForwardUntilNoTasksRemain();
407 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 407 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
408 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 408 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
409 EXPECT_THAT(histogram_tester().GetAllSamples( 409 EXPECT_THAT(histogram_tester().GetAllSamples(
410 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 410 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
411 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 411 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
412 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 412 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
413 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 413 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
414 /*count=*/1))); 414 /*count=*/1)));
415 } 415 }
416 416
417 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { 417 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
418 const std::string kJsonStr = "{\"recos\": []}"; 418 const std::string kJsonStr = "{\"recos\": []}";
419 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 419 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
420 net::URLRequestStatus::SUCCESS); 420 net::URLRequestStatus::SUCCESS);
421 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1); 421 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1);
422 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 422 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
423 /*count=*/1, 423 /*count=*/1,
424 /*force_request=*/true); 424 /*interactive_request=*/true);
425 FastForwardUntilNoTasksRemain(); 425 FastForwardUntilNoTasksRemain();
426 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 426 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
427 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 427 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
428 EXPECT_THAT( 428 EXPECT_THAT(
429 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 429 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
430 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 430 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
431 EXPECT_THAT(histogram_tester().GetAllSamples( 431 EXPECT_THAT(histogram_tester().GetAllSamples(
432 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 432 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
433 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 433 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
434 } 434 }
435 435
436 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { 436 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) {
437 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 437 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
438 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), 438 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(),
439 /*language_code=*/"en-US", 439 /*language_code=*/"en-US",
440 /*count=*/1, 440 /*count=*/1,
441 /*force_request=*/true); 441 /*interactive_request=*/true);
442 FastForwardUntilNoTasksRemain(); 442 FastForwardUntilNoTasksRemain();
443 EXPECT_THAT(snippets_fetcher().last_status(), 443 EXPECT_THAT(snippets_fetcher().last_status(),
444 Eq("Cannot fetch for empty hosts list.")); 444 Eq("Cannot fetch for empty hosts list."));
445 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 445 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
446 EXPECT_THAT( 446 EXPECT_THAT(
447 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 447 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
448 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 448 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
449 // This particular error gets triggered prior to fetching, so no fetch time 449 // This particular error gets triggered prior to fetching, so no fetch time
450 // or response should get recorded. 450 // or response should get recorded.
451 EXPECT_THAT(histogram_tester().GetAllSamples( 451 EXPECT_THAT(histogram_tester().GetAllSamples(
452 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 452 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
453 IsEmpty()); 453 IsEmpty());
454 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 454 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
455 IsEmpty()); 455 IsEmpty());
456 } 456 }
457 457
458 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { 458 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) {
459 net::TestURLFetcherFactory test_url_fetcher_factory; 459 net::TestURLFetcherFactory test_url_fetcher_factory;
460 snippets_fetcher().FetchSnippetsFromHosts( 460 snippets_fetcher().FetchSnippetsFromHosts(
461 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17, 461 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17,
462 /*force_request=*/true); 462 /*interactive_request=*/true);
463 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); 463 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0);
464 ASSERT_THAT(fetcher, NotNull()); 464 ASSERT_THAT(fetcher, NotNull());
465 std::unique_ptr<base::Value> value = 465 std::unique_ptr<base::Value> value =
466 base::JSONReader::Read(fetcher->upload_data()); 466 base::JSONReader::Read(fetcher->upload_data());
467 ASSERT_TRUE(value) << " failed to parse JSON: " 467 ASSERT_TRUE(value) << " failed to parse JSON: "
468 << PrintToString(fetcher->upload_data()); 468 << PrintToString(fetcher->upload_data());
469 const base::DictionaryValue* dict = nullptr; 469 const base::DictionaryValue* dict = nullptr;
470 ASSERT_TRUE(value->GetAsDictionary(&dict)); 470 ASSERT_TRUE(value->GetAsDictionary(&dict));
471 const base::DictionaryValue* local_scoring_params = nullptr; 471 const base::DictionaryValue* local_scoring_params = nullptr;
472 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", 472 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params",
(...skipping 11 matching lines...) Expand all
484 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); 484 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
485 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); 485 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com"));
486 } 486 }
487 487
488 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 488 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
489 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 489 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
490 net::URLRequestStatus::FAILED); 490 net::URLRequestStatus::FAILED);
491 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 491 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
492 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 492 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
493 /*count=*/1, 493 /*count=*/1,
494 /*force_request=*/true); 494 /*interactive_request=*/true);
495 FastForwardUntilNoTasksRemain(); 495 FastForwardUntilNoTasksRemain();
496 EXPECT_THAT(snippets_fetcher().last_status(), 496 EXPECT_THAT(snippets_fetcher().last_status(),
497 Eq("URLRequestStatus error -2")); 497 Eq("URLRequestStatus error -2"));
498 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 498 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
499 EXPECT_THAT( 499 EXPECT_THAT(
500 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 500 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
501 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); 501 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
502 EXPECT_THAT(histogram_tester().GetAllSamples( 502 EXPECT_THAT(histogram_tester().GetAllSamples(
503 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 503 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
504 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 504 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
505 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 505 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
506 Not(IsEmpty())); 506 Not(IsEmpty()));
507 } 507 }
508 508
509 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 509 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
510 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 510 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
511 net::URLRequestStatus::SUCCESS); 511 net::URLRequestStatus::SUCCESS);
512 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 512 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
513 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 513 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
514 /*count=*/1, 514 /*count=*/1,
515 /*force_request=*/true); 515 /*interactive_request=*/true);
516 FastForwardUntilNoTasksRemain(); 516 FastForwardUntilNoTasksRemain();
517 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 517 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
518 EXPECT_THAT( 518 EXPECT_THAT(
519 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 519 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
520 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); 520 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
521 EXPECT_THAT(histogram_tester().GetAllSamples( 521 EXPECT_THAT(histogram_tester().GetAllSamples(
522 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 522 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
523 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 523 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
524 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 524 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
525 Not(IsEmpty())); 525 Not(IsEmpty()));
526 } 526 }
527 527
528 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { 528 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) {
529 const std::string kInvalidJsonStr = "{ \"recos\": []"; 529 const std::string kInvalidJsonStr = "{ \"recos\": []";
530 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, 530 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK,
531 net::URLRequestStatus::SUCCESS); 531 net::URLRequestStatus::SUCCESS);
532 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 532 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
533 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 533 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
534 /*count=*/1, 534 /*count=*/1,
535 /*force_request=*/true); 535 /*interactive_request=*/true);
536 FastForwardUntilNoTasksRemain(); 536 FastForwardUntilNoTasksRemain();
537 EXPECT_THAT(snippets_fetcher().last_status(), 537 EXPECT_THAT(snippets_fetcher().last_status(),
538 StartsWith("Received invalid JSON (error ")); 538 StartsWith("Received invalid JSON (error "));
539 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); 539 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr));
540 EXPECT_THAT( 540 EXPECT_THAT(
541 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 541 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
542 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 542 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
543 EXPECT_THAT(histogram_tester().GetAllSamples( 543 EXPECT_THAT(histogram_tester().GetAllSamples(
544 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 544 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
545 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 545 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
546 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 546 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
547 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 547 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
548 /*count=*/1))); 548 /*count=*/1)));
549 } 549 }
550 550
551 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { 551 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
552 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, 552 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK,
553 net::URLRequestStatus::SUCCESS); 553 net::URLRequestStatus::SUCCESS);
554 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 554 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
555 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 555 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
556 /*count=*/1, 556 /*count=*/1,
557 /*force_request=*/true); 557 /*interactive_request=*/true);
558 FastForwardUntilNoTasksRemain(); 558 FastForwardUntilNoTasksRemain();
559 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); 559 EXPECT_THAT(snippets_fetcher().last_json(), std::string());
560 EXPECT_THAT( 560 EXPECT_THAT(
561 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 561 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
562 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 562 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
563 EXPECT_THAT(histogram_tester().GetAllSamples( 563 EXPECT_THAT(histogram_tester().GetAllSamples(
564 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 564 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
565 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 565 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
566 } 566 }
567 567
568 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { 568 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) {
569 const std::string kJsonStr = 569 const std::string kJsonStr =
570 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; 570 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}";
571 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 571 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
572 net::URLRequestStatus::SUCCESS); 572 net::URLRequestStatus::SUCCESS);
573 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 573 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
574 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 574 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
575 /*count=*/1, 575 /*count=*/1,
576 /*force_request=*/true); 576 /*interactive_request=*/true);
577 FastForwardUntilNoTasksRemain(); 577 FastForwardUntilNoTasksRemain();
578 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 578 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
579 EXPECT_THAT( 579 EXPECT_THAT(
580 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 580 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
581 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); 581 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
582 EXPECT_THAT(histogram_tester().GetAllSamples( 582 EXPECT_THAT(histogram_tester().GetAllSamples(
583 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 583 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
584 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 584 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
585 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 585 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
586 Not(IsEmpty())); 586 Not(IsEmpty()));
587 } 587 }
588 588
589 // This test actually verifies that the test setup itself is sane, to prevent 589 // This test actually verifies that the test setup itself is sane, to prevent
590 // hard-to-reproduce test failures. 590 // hard-to-reproduce test failures.
591 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 591 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
592 InitFakeURLFetcherFactory(); 592 InitFakeURLFetcherFactory();
593 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 593 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
594 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 594 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
595 /*count=*/1, 595 /*count=*/1,
596 /*force_request=*/true); 596 /*interactive_request=*/true);
597 FastForwardUntilNoTasksRemain(); 597 FastForwardUntilNoTasksRemain();
598 } 598 }
599 599
600 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 600 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
601 const std::string kJsonStr = "{ \"recos\": [] }"; 601 const std::string kJsonStr = "{ \"recos\": [] }";
602 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 602 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
603 net::URLRequestStatus::SUCCESS); 603 net::URLRequestStatus::SUCCESS);
604 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1); 604 EXPECT_CALL(mock_callback(), Run(/*snippets=*/PointeeSizeIs(0))).Times(1);
605 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 605 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
606 /*count=*/1, 606 /*count=*/1,
607 /*force_request=*/true); 607 /*interactive_request=*/true);
608 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. 608 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous.
609 // Callback is expected to be called once. 609 // Callback is expected to be called once.
610 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 610 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
611 /*count=*/1, 611 /*count=*/1,
612 /*force_request=*/true); 612 /*interactive_request=*/true);
613 FastForwardUntilNoTasksRemain(); 613 FastForwardUntilNoTasksRemain();
614 EXPECT_THAT( 614 EXPECT_THAT(
615 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 615 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
616 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 616 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
617 EXPECT_THAT(histogram_tester().GetAllSamples( 617 EXPECT_THAT(histogram_tester().GetAllSamples(
618 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 618 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
619 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 619 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
620 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 620 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
621 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 621 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
622 /*count=*/1))); 622 /*count=*/1)));
623 } 623 }
624 624
625 ::std::ostream& operator<<( 625 ::std::ostream& operator<<(
626 ::std::ostream& os, 626 ::std::ostream& os,
627 const NTPSnippetsFetcher::OptionalSnippets& snippets) { 627 const NTPSnippetsFetcher::OptionalSnippets& snippets) {
628 if (snippets) { 628 if (snippets) {
629 // Matchers above aren't any more precise than this, so this is sufficient 629 // Matchers above aren't any more precise than this, so this is sufficient
630 // for test-failure diagnostics. 630 // for test-failure diagnostics.
631 return os << "list with " << snippets->size() << " elements"; 631 return os << "list with " << snippets->size() << " elements";
632 } else { 632 } else {
633 return os << "null"; 633 return os << "null";
634 } 634 }
635 } 635 }
636 636
637 } // namespace ntp_snippets 637 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698