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

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

Issue 2552813005: Use improved VariationParamsManager to hide details. (Closed)
Patch Set: Rebased, Created 4 years 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
« no previous file with comments | « components/ntp_snippets/remote/ntp_snippets_fetcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/remote/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <map> 8 #include <map>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, 269 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback,
270 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { 270 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) {
271 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 271 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
272 FROM_HERE, base::Bind(&ParseJson, json, std::move(success_callback), 272 FROM_HERE, base::Bind(&ParseJson, json, std::move(success_callback),
273 std::move(error_callback)), 273 std::move(error_callback)),
274 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); 274 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs));
275 } 275 }
276 276
277 } // namespace 277 } // namespace
278 278
279 class NTPSnippetsFetcherTest : public testing::Test { 279 class NTPSnippetsFetcherTestBase : public testing::Test {
280 public: 280 public:
281 // TODO(fhorschig): As soon as crbug.com/645447 is resolved, use 281 explicit NTPSnippetsFetcherTestBase(const GURL& gurl)
282 // variations::testing::VariationParamsManager to configure these values. 282 : default_variation_params_(
283 class RequestBuilderWithMockedFlagsForTesting 283 {{"send_top_languages", "true"}, {"send_user_class", "true"}}),
284 : public NTPSnippetsFetcher::RequestBuilder { 284 params_manager_(
285 public:
286 RequestBuilderWithMockedFlagsForTesting() : RequestBuilder() {}
287
288 private:
289 bool IsSendingUserClassEnabled() const override { return true; }
290 bool IsSendingTopLanguagesEnabled() const override { return true; }
291 };
292
293 NTPSnippetsFetcherTest()
294 : NTPSnippetsFetcherTest(GURL(kTestChromeReaderUrl),
295 std::map<std::string, std::string>()) {}
296
297 NTPSnippetsFetcherTest(const GURL& gurl,
298 const std::map<std::string, std::string>& params)
299 : params_manager_(
300 base::MakeUnique<variations::testing::VariationParamsManager>( 285 base::MakeUnique<variations::testing::VariationParamsManager>(
301 ntp_snippets::kStudyName, 286 ntp_snippets::kStudyName,
302 params)), 287 default_variation_params_,
288 std::set<std::string>{
289 ntp_snippets::kArticleSuggestionsFeature.name})),
303 mock_task_runner_(new base::TestMockTimeTaskRunner()), 290 mock_task_runner_(new base::TestMockTimeTaskRunner()),
304 mock_task_runner_handle_(mock_task_runner_), 291 mock_task_runner_handle_(mock_task_runner_),
305 signin_client_(base::MakeUnique<TestSigninClient>(nullptr)), 292 signin_client_(base::MakeUnique<TestSigninClient>(nullptr)),
306 account_tracker_(base::MakeUnique<AccountTrackerService>()), 293 account_tracker_(base::MakeUnique<AccountTrackerService>()),
307 fake_signin_manager_( 294 fake_signin_manager_(
308 base::MakeUnique<FakeSigninManagerBase>(signin_client_.get(), 295 base::MakeUnique<FakeSigninManagerBase>(signin_client_.get(),
309 account_tracker_.get())), 296 account_tracker_.get())),
310 fake_token_service_(base::MakeUnique<FakeProfileOAuth2TokenService>()), 297 fake_token_service_(base::MakeUnique<FakeProfileOAuth2TokenService>()),
311 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()), 298 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()),
312 test_url_(gurl) { 299 test_url_(gurl) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 void InitFakeURLFetcherFactory() { 341 void InitFakeURLFetcherFactory() {
355 if (fake_url_fetcher_factory_) { 342 if (fake_url_fetcher_factory_) {
356 return; 343 return;
357 } 344 }
358 // Instantiation of factory automatically sets itself as URLFetcher's 345 // Instantiation of factory automatically sets itself as URLFetcher's
359 // factory. 346 // factory.
360 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( 347 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(
361 /*default_factory=*/&failing_url_fetcher_factory_)); 348 /*default_factory=*/&failing_url_fetcher_factory_));
362 } 349 }
363 350
364 void SetFetchingPersonalizationVariation( 351 void SetDefaultVariationParam(std::string param_name, std::string value) {
365 const std::string& personalization_string) { 352 default_variation_params_[param_name] = value;
353 SetVariationParam(param_name, value);
354 }
355
356 void SetVariationParam(std::string param_name, std::string value) {
366 params_manager_.reset(); 357 params_manager_.reset();
367 std::map<std::string, std::string> params = { 358
368 {"fetching_personalization", personalization_string}}; 359 std::map<std::string, std::string> params = default_variation_params_;
360 params[param_name] = value;
361
369 params_manager_ = 362 params_manager_ =
370 base::MakeUnique<variations::testing::VariationParamsManager>( 363 base::MakeUnique<variations::testing::VariationParamsManager>(
371 ntp_snippets::kStudyName, params); 364 ntp_snippets::kStudyName, params,
365 std::set<std::string>{
366 ntp_snippets::kArticleSuggestionsFeature.name});
372 } 367 }
373 368
374 void SetVariationParametersForFeatures( 369 void SetVariationParametersForFeatures(
375 const std::map<std::string, std::string>& params, 370 const std::map<std::string, std::string>& params,
376 const std::set<std::string>& features) { 371 const std::set<std::string>& features) {
377 params_manager_.reset(); 372 params_manager_.reset();
378 params_manager_ = 373 params_manager_ =
379 base::MakeUnique<variations::testing::VariationParamsManager>( 374 base::MakeUnique<variations::testing::VariationParamsManager>(
380 ntp_snippets::kStudyName, params, features); 375 ntp_snippets::kStudyName, params, features);
381 } 376 }
(...skipping 15 matching lines...) Expand all
397 for (const auto& code : codes) { 392 for (const auto& code : codes) {
398 language_model->OnPageVisited(code); 393 language_model->OnPageVisited(code);
399 } 394 }
400 } 395 }
401 return language_model; 396 return language_model;
402 } 397 }
403 398
404 TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); } 399 TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); }
405 400
406 private: 401 private:
407 // TODO(fhorschig): Make it a simple member as soon as it resets properly. 402 std::map<std::string, std::string> default_variation_params_;
403 // TODO(fhorschig): Make it a simple member when crbug.com/672010 is resolved.
408 std::unique_ptr<variations::testing::VariationParamsManager> params_manager_; 404 std::unique_ptr<variations::testing::VariationParamsManager> params_manager_;
409 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; 405 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_;
410 base::ThreadTaskRunnerHandle mock_task_runner_handle_; 406 base::ThreadTaskRunnerHandle mock_task_runner_handle_;
411 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 407 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
412 // Initialized lazily in SetFakeResponse(). 408 // Initialized lazily in SetFakeResponse().
413 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; 409 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_;
414 std::unique_ptr<TestSigninClient> signin_client_; 410 std::unique_ptr<TestSigninClient> signin_client_;
415 std::unique_ptr<AccountTrackerService> account_tracker_; 411 std::unique_ptr<AccountTrackerService> account_tracker_;
416 std::unique_ptr<SigninManagerBase> fake_signin_manager_; 412 std::unique_ptr<SigninManagerBase> fake_signin_manager_;
417 std::unique_ptr<OAuth2TokenService> fake_token_service_; 413 std::unique_ptr<OAuth2TokenService> fake_token_service_;
418 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; 414 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_;
419 std::unique_ptr<TestingPrefServiceSimple> pref_service_; 415 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
420 std::unique_ptr<UserClassifier> user_classifier_; 416 std::unique_ptr<UserClassifier> user_classifier_;
421 CategoryFactory category_factory_; 417 CategoryFactory category_factory_;
422 MockSnippetsAvailableCallback mock_callback_; 418 MockSnippetsAvailableCallback mock_callback_;
423 const GURL test_url_; 419 const GURL test_url_;
424 base::HistogramTester histogram_tester_; 420 base::HistogramTester histogram_tester_;
425 421
426 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); 422 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTestBase);
427 }; 423 };
428 424
429 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { 425 class ChromeReaderSnippetsFetcherTest : public NTPSnippetsFetcherTestBase {
426 public:
427 ChromeReaderSnippetsFetcherTest()
428 : NTPSnippetsFetcherTestBase(GURL(kTestChromeReaderUrl)) {}
429 };
430
431 class NTPSnippetsContentSuggestionsFetcherTest
432 : public NTPSnippetsFetcherTestBase {
430 public: 433 public:
431 NTPSnippetsContentSuggestionsFetcherTest() 434 NTPSnippetsContentSuggestionsFetcherTest()
432 : NTPSnippetsFetcherTest( 435 : NTPSnippetsFetcherTestBase(GURL(kTestChromeContentSuggestionsUrl)) {
433 GURL(kTestChromeContentSuggestionsUrl), 436 SetDefaultVariationParam("content_suggestions_backend",
434 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} 437 kContentSuggestionsServer);
438 ResetSnippetsFetcher();
439 }
435 }; 440 };
436 441
437 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { 442 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestAuthenticated) {
438 NTPSnippetsFetcher::RequestBuilder builder; 443 NTPSnippetsFetcher::RequestBuilder builder;
439 NTPSnippetsFetcher::Params params; 444 NTPSnippetsFetcher::Params params;
440 params.hosts = {"chromium.org"}; 445 params.hosts = {"chromium.org"};
441 params.excluded_ids = {"1234567890"}; 446 params.excluded_ids = {"1234567890"};
442 params.count_to_fetch = 25; 447 params.count_to_fetch = 25;
443 params.interactive_request = false; 448 params.interactive_request = false;
444 builder.SetParams(params) 449 builder.SetParams(params)
445 .SetAuthentication("0BFUSGAIA", "headerstuff") 450 .SetAuthentication("0BFUSGAIA", "headerstuff")
446 .SetPersonalization(NTPSnippetsFetcher::Personalization::kPersonal) 451 .SetPersonalization(NTPSnippetsFetcher::Personalization::kPersonal)
447 .SetUserClassForTesting("ACTIVE_NTP_USER") 452 .SetUserClassForTesting("ACTIVE_NTP_USER")
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 " \"regularlyVisitedHostNames\": [" 501 " \"regularlyVisitedHostNames\": ["
497 " \"chromium.org\"" 502 " \"chromium.org\""
498 " ]," 503 " ],"
499 " \"excludedSuggestionIds\": [" 504 " \"excludedSuggestionIds\": ["
500 " \"1234567890\"" 505 " \"1234567890\""
501 " ]," 506 " ],"
502 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" 507 " \"userActivenessClass\": \"ACTIVE_NTP_USER\""
503 "}")); 508 "}"));
504 } 509 }
505 510
506 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { 511 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestUnauthenticated) {
507 NTPSnippetsFetcher::RequestBuilder builder; 512 NTPSnippetsFetcher::RequestBuilder builder;
508 NTPSnippetsFetcher::Params params = test_params(); 513 NTPSnippetsFetcher::Params params = test_params();
509 params.count_to_fetch = 10; 514 params.count_to_fetch = 10;
510 builder.SetParams(params) 515 builder.SetParams(params)
511 .SetUserClassForTesting("ACTIVE_NTP_USER") 516 .SetUserClassForTesting("ACTIVE_NTP_USER")
512 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 517 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
513 .SetFetchAPI(NTPSnippetsFetcher::CHROME_READER_API); 518 .SetFetchAPI(NTPSnippetsFetcher::CHROME_READER_API);
514 519
515 EXPECT_THAT(builder.PreviewRequestHeadersForTesting(), 520 EXPECT_THAT(builder.PreviewRequestHeadersForTesting(),
516 StrEq("Content-Type: application/json; charset=UTF-8\r\n" 521 StrEq("Content-Type: application/json; charset=UTF-8\r\n"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 555 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
551 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 556 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
552 EqualsJSON("{" 557 EqualsJSON("{"
553 " \"regularlyVisitedHostNames\": []," 558 " \"regularlyVisitedHostNames\": [],"
554 " \"priority\": \"USER_ACTION\"," 559 " \"priority\": \"USER_ACTION\","
555 " \"excludedSuggestionIds\": []," 560 " \"excludedSuggestionIds\": [],"
556 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" 561 " \"userActivenessClass\": \"ACTIVE_NTP_USER\""
557 "}")); 562 "}"));
558 } 563 }
559 564
560 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { 565 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestExcludedIds) {
561 NTPSnippetsFetcher::RequestBuilder builder; 566 NTPSnippetsFetcher::RequestBuilder builder;
562 NTPSnippetsFetcher::Params params = test_params(); 567 NTPSnippetsFetcher::Params params = test_params();
563 params.interactive_request = false; 568 params.interactive_request = false;
564 for (int i = 0; i < 200; ++i) { 569 for (int i = 0; i < 200; ++i) {
565 params.excluded_ids.insert(base::StringPrintf("%03d", i)); 570 params.excluded_ids.insert(base::StringPrintf("%03d", i));
566 } 571 }
567 builder.SetParams(params) 572 builder.SetParams(params)
568 .SetUserClassForTesting("ACTIVE_NTP_USER") 573 .SetUserClassForTesting("ACTIVE_NTP_USER")
569 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 574 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
570 .SetFetchAPI( 575 .SetFetchAPI(
(...skipping 24 matching lines...) Expand all
595 " \"085\", \"086\", \"087\", \"088\", \"089\"," 600 " \"085\", \"086\", \"087\", \"088\", \"089\","
596 " \"090\", \"091\", \"092\", \"093\", \"094\"," 601 " \"090\", \"091\", \"092\", \"093\", \"094\","
597 " \"095\", \"096\", \"097\", \"098\", \"099\"" 602 " \"095\", \"096\", \"097\", \"098\", \"099\""
598 // Truncated to 100 entries. Currently, they happen to 603 // Truncated to 100 entries. Currently, they happen to
599 // be those lexically first. 604 // be those lexically first.
600 " ]," 605 " ],"
601 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" 606 " \"userActivenessClass\": \"ACTIVE_NTP_USER\""
602 "}")); 607 "}"));
603 } 608 }
604 609
605 TEST_F(NTPSnippetsFetcherTest, BuildRequestNoUserClass) { 610 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestNoUserClass) {
606 NTPSnippetsFetcher::RequestBuilder builder; 611 NTPSnippetsFetcher::RequestBuilder builder;
607 NTPSnippetsFetcher::Params params = test_params(); 612 NTPSnippetsFetcher::Params params = test_params();
608 params.interactive_request = false; 613 params.interactive_request = false;
609 builder.SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 614 builder.SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
610 .SetParams(params) 615 .SetParams(params)
611 .SetFetchAPI( 616 .SetFetchAPI(
612 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 617 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
613 618
614 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 619 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
615 EqualsJSON("{" 620 EqualsJSON("{"
616 " \"regularlyVisitedHostNames\": []," 621 " \"regularlyVisitedHostNames\": [],"
617 " \"priority\": \"BACKGROUND_PREFETCH\"," 622 " \"priority\": \"BACKGROUND_PREFETCH\","
618 " \"excludedSuggestionIds\": []" 623 " \"excludedSuggestionIds\": []"
619 "}")); 624 "}"));
620 } 625 }
621 626
622 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { 627 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestWithTwoLanguages) {
623 RequestBuilderWithMockedFlagsForTesting builder; 628 NTPSnippetsFetcher::RequestBuilder builder;
624 std::unique_ptr<translate::LanguageModel> language_model = 629 std::unique_ptr<translate::LanguageModel> language_model =
625 MakeLanguageModel({"de", "en"}); 630 MakeLanguageModel({"de", "en"});
626 NTPSnippetsFetcher::Params params = test_params(); 631 NTPSnippetsFetcher::Params params = test_params();
627 params.language_code = "en"; 632 params.language_code = "en";
628 builder.SetParams(params) 633 builder.SetParams(params)
629 .SetLanguageModel(language_model.get()) 634 .SetLanguageModel(language_model.get())
630 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 635 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
631 .SetFetchAPI( 636 .SetFetchAPI(
632 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 637 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
633 638
634 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 639 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
635 EqualsJSON("{" 640 EqualsJSON("{"
636 " \"regularlyVisitedHostNames\": []," 641 " \"regularlyVisitedHostNames\": [],"
637 " \"priority\": \"USER_ACTION\"," 642 " \"priority\": \"USER_ACTION\","
638 " \"uiLanguage\": \"en\"," 643 " \"uiLanguage\": \"en\","
639 " \"excludedSuggestionIds\": []," 644 " \"excludedSuggestionIds\": [],"
640 " \"topLanguages\": [" 645 " \"topLanguages\": ["
641 " {" 646 " {"
642 " \"language\" : \"en\"," 647 " \"language\" : \"en\","
643 " \"frequency\" : 0.5" 648 " \"frequency\" : 0.5"
644 " }," 649 " },"
645 " {" 650 " {"
646 " \"language\" : \"de\"," 651 " \"language\" : \"de\","
647 " \"frequency\" : 0.5" 652 " \"frequency\" : 0.5"
648 " }" 653 " }"
649 " ]" 654 " ]"
650 "}")); 655 "}"));
651 } 656 }
652 657
653 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) { 658 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestWithUILanguageOnly) {
654 RequestBuilderWithMockedFlagsForTesting builder; 659 NTPSnippetsFetcher::RequestBuilder builder;
655 std::unique_ptr<translate::LanguageModel> language_model = 660 std::unique_ptr<translate::LanguageModel> language_model =
656 MakeLanguageModel({"en"}); 661 MakeLanguageModel({"en"});
657 NTPSnippetsFetcher::Params params = test_params(); 662 NTPSnippetsFetcher::Params params = test_params();
658 params.language_code = "en"; 663 params.language_code = "en";
659 builder.SetParams(params) 664 builder.SetParams(params)
660 .SetLanguageModel(language_model.get()) 665 .SetLanguageModel(language_model.get())
661 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 666 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
662 .SetFetchAPI( 667 .SetFetchAPI(
663 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 668 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
664 669
665 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 670 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
666 EqualsJSON("{" 671 EqualsJSON("{"
667 " \"regularlyVisitedHostNames\": []," 672 " \"regularlyVisitedHostNames\": [],"
668 " \"priority\": \"USER_ACTION\"," 673 " \"priority\": \"USER_ACTION\","
669 " \"uiLanguage\": \"en\"," 674 " \"uiLanguage\": \"en\","
670 " \"excludedSuggestionIds\": []," 675 " \"excludedSuggestionIds\": [],"
671 " \"topLanguages\": [{" 676 " \"topLanguages\": [{"
672 " \"language\" : \"en\"," 677 " \"language\" : \"en\","
673 " \"frequency\" : 1.0" 678 " \"frequency\" : 1.0"
674 " }]" 679 " }]"
675 "}")); 680 "}"));
676 } 681 }
677 682
678 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { 683 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldNotFetchOnCreation) {
679 // The lack of registered baked in responses would cause any fetch to fail. 684 // The lack of registered baked in responses would cause any fetch to fail.
680 FastForwardUntilNoTasksRemain(); 685 FastForwardUntilNoTasksRemain();
681 EXPECT_THAT(histogram_tester().GetAllSamples( 686 EXPECT_THAT(histogram_tester().GetAllSamples(
682 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 687 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
683 IsEmpty()); 688 IsEmpty());
684 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 689 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
685 IsEmpty()); 690 IsEmpty());
686 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty()); 691 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty());
687 } 692 }
688 693
689 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { 694 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldFetchSuccessfully) {
690 const std::string kJsonStr = 695 const std::string kJsonStr =
691 "{\"recos\": [{" 696 "{\"recos\": [{"
692 " \"contentInfo\": {" 697 " \"contentInfo\": {"
693 " \"url\" : \"http://localhost/foobar\"," 698 " \"url\" : \"http://localhost/foobar\","
694 " \"sourceCorpusInfo\" : [{" 699 " \"sourceCorpusInfo\" : [{"
695 " \"ampUrl\" : \"http://localhost/amp\"," 700 " \"ampUrl\" : \"http://localhost/amp\","
696 " \"corpusId\" : \"http://localhost/foobar\"," 701 " \"corpusId\" : \"http://localhost/foobar\","
697 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" 702 " \"publisherData\": { \"sourceName\" : \"Foo News\" }"
698 " }]" 703 " }]"
699 " }" 704 " }"
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 957
953 ASSERT_TRUE(fetched_categories); 958 ASSERT_TRUE(fetched_categories);
954 ASSERT_THAT(fetched_categories->size(), Eq(1u)); 959 ASSERT_THAT(fetched_categories->size(), Eq(1u));
955 const auto& category = (*fetched_categories)[0]; 960 const auto& category = (*fetched_categories)[0];
956 EXPECT_THAT(category.category.id(), 961 EXPECT_THAT(category.category.id(),
957 Eq(CategoryFactory().FromRemoteCategory(2).id())); 962 Eq(CategoryFactory().FromRemoteCategory(2).id()));
958 ASSERT_THAT(category.snippets.size(), Eq(1u)); 963 ASSERT_THAT(category.snippets.size(), Eq(1u));
959 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); 964 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2"));
960 } 965 }
961 966
962 TEST_F(NTPSnippetsFetcherTest, PersonalizesDependingOnVariations) { 967 TEST_F(ChromeReaderSnippetsFetcherTest, PersonalizesDependingOnVariations) {
963 // Default setting should be both personalization options. 968 // Default setting should be both personalization options.
964 EXPECT_THAT(snippets_fetcher().personalization(), 969 EXPECT_THAT(snippets_fetcher().personalization(),
965 Eq(NTPSnippetsFetcher::Personalization::kBoth)); 970 Eq(NTPSnippetsFetcher::Personalization::kBoth));
966 971
967 SetFetchingPersonalizationVariation("personal"); 972 SetVariationParam("fetching_personalization", "personal");
968 ResetSnippetsFetcher(); 973 ResetSnippetsFetcher();
969 EXPECT_THAT(snippets_fetcher().personalization(), 974 EXPECT_THAT(snippets_fetcher().personalization(),
970 Eq(NTPSnippetsFetcher::Personalization::kPersonal)); 975 Eq(NTPSnippetsFetcher::Personalization::kPersonal));
971 976
972 SetFetchingPersonalizationVariation("non_personal"); 977 SetVariationParam("fetching_personalization", "non_personal");
973 ResetSnippetsFetcher(); 978 ResetSnippetsFetcher();
974 EXPECT_THAT(snippets_fetcher().personalization(), 979 EXPECT_THAT(snippets_fetcher().personalization(),
975 Eq(NTPSnippetsFetcher::Personalization::kNonPersonal)); 980 Eq(NTPSnippetsFetcher::Personalization::kNonPersonal));
976 981
977 SetFetchingPersonalizationVariation("both"); 982 SetVariationParam("fetching_personalization", "both");
978 ResetSnippetsFetcher(); 983 ResetSnippetsFetcher();
979 EXPECT_THAT(snippets_fetcher().personalization(), 984 EXPECT_THAT(snippets_fetcher().personalization(),
980 Eq(NTPSnippetsFetcher::Personalization::kBoth)); 985 Eq(NTPSnippetsFetcher::Personalization::kBoth));
981 } 986 }
982 987
983 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { 988 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
984 const std::string kJsonStr = "{\"recos\": []}"; 989 const std::string kJsonStr = "{\"recos\": []}";
985 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 990 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
986 net::URLRequestStatus::SUCCESS); 991 net::URLRequestStatus::SUCCESS);
987 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, 992 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS,
988 IsEmptyArticleList())); 993 IsEmptyArticleList()));
989 snippets_fetcher().FetchSnippets( 994 snippets_fetcher().FetchSnippets(
990 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 995 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
991 FastForwardUntilNoTasksRemain(); 996 FastForwardUntilNoTasksRemain();
992 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 997 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
993 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 998 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
994 EXPECT_THAT( 999 EXPECT_THAT(
995 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 1000 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
996 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 1001 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
997 EXPECT_THAT(histogram_tester().GetAllSamples( 1002 EXPECT_THAT(histogram_tester().GetAllSamples(
998 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 1003 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
999 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 1004 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
1000 } 1005 }
1001 1006
1002 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { 1007 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldRestrictToHosts) {
1003 DelegateCallingTestURLFetcherFactory fetcher_factory; 1008 DelegateCallingTestURLFetcherFactory fetcher_factory;
1004 NTPSnippetsFetcher::Params params = test_params(); 1009 NTPSnippetsFetcher::Params params = test_params();
1005 params.hosts = {"www.somehost1.com", "www.somehost2.com"}; 1010 params.hosts = {"www.somehost1.com", "www.somehost2.com"};
1006 params.count_to_fetch = 17; 1011 params.count_to_fetch = 17;
1007 1012
1008 snippets_fetcher().FetchSnippets( 1013 snippets_fetcher().FetchSnippets(
1009 params, ToSnippetsAvailableCallback(&mock_callback())); 1014 params, ToSnippetsAvailableCallback(&mock_callback()));
1010 1015
1011 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher(); 1016 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher();
1012 ASSERT_THAT(fetcher, NotNull()); 1017 ASSERT_THAT(fetcher, NotNull());
(...skipping 13 matching lines...) Expand all
1026 const base::DictionaryValue* content_selector = nullptr; 1031 const base::DictionaryValue* content_selector = nullptr;
1027 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); 1032 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector));
1028 std::string content_selector_value; 1033 std::string content_selector_value;
1029 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); 1034 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
1030 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); 1035 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com"));
1031 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); 1036 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector));
1032 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); 1037 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
1033 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); 1038 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com"));
1034 } 1039 }
1035 1040
1036 TEST_F(NTPSnippetsFetcherTest, RetryOnInteractiveRequests) { 1041 TEST_F(ChromeReaderSnippetsFetcherTest, RetryOnInteractiveRequests) {
1037 DelegateCallingTestURLFetcherFactory fetcher_factory; 1042 DelegateCallingTestURLFetcherFactory fetcher_factory;
1038 NTPSnippetsFetcher::Params params = test_params(); 1043 NTPSnippetsFetcher::Params params = test_params();
1039 params.interactive_request = true; 1044 params.interactive_request = true;
1040 1045
1041 snippets_fetcher().FetchSnippets( 1046 snippets_fetcher().FetchSnippets(
1042 params, ToSnippetsAvailableCallback(&mock_callback())); 1047 params, ToSnippetsAvailableCallback(&mock_callback()));
1043 1048
1044 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher(); 1049 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher();
1045 ASSERT_THAT(fetcher, NotNull()); 1050 ASSERT_THAT(fetcher, NotNull());
1046 EXPECT_THAT(fetcher->GetMaxRetriesOn5xx(), Eq(2)); 1051 EXPECT_THAT(fetcher->GetMaxRetriesOn5xx(), Eq(2));
1047 } 1052 }
1048 1053
1049 TEST_F(NTPSnippetsFetcherTest, RetriesConfigurableOnNonInteractiveRequests) { 1054 TEST_F(ChromeReaderSnippetsFetcherTest,
1055 RetriesConfigurableOnNonInteractiveRequests) {
1050 struct ExpectationForVariationParam { 1056 struct ExpectationForVariationParam {
1051 std::string param_value; 1057 std::string param_value;
1052 int expected_value; 1058 int expected_value;
1053 std::string description; 1059 std::string description;
1054 }; 1060 };
1055 const std::vector<ExpectationForVariationParam> retry_config_expectation = { 1061 const std::vector<ExpectationForVariationParam> retry_config_expectation = {
1056 {"", 0, "Do not retry by default"}, 1062 {"", 0, "Do not retry by default"},
1057 {"0", 0, "Do not retry on param value 0"}, 1063 {"0", 0, "Do not retry on param value 0"},
1058 {"-1", 0, "Do not retry on negative param values."}, 1064 {"-1", 0, "Do not retry on negative param values."},
1059 {"4", 4, "Retry as set in param value."}}; 1065 {"4", 4, "Retry as set in param value."}};
(...skipping 10 matching lines...) Expand all
1070 snippets_fetcher().FetchSnippets( 1076 snippets_fetcher().FetchSnippets(
1071 params, ToSnippetsAvailableCallback(&mock_callback())); 1077 params, ToSnippetsAvailableCallback(&mock_callback()));
1072 1078
1073 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher(); 1079 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher();
1074 ASSERT_THAT(fetcher, NotNull()); 1080 ASSERT_THAT(fetcher, NotNull());
1075 EXPECT_THAT(fetcher->GetMaxRetriesOn5xx(), Eq(retry_config.expected_value)) 1081 EXPECT_THAT(fetcher->GetMaxRetriesOn5xx(), Eq(retry_config.expected_value))
1076 << retry_config.description; 1082 << retry_config.description;
1077 } 1083 }
1078 } 1084 }
1079 1085
1080 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 1086 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportUrlStatusError) {
1081 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, 1087 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
1082 net::URLRequestStatus::FAILED); 1088 net::URLRequestStatus::FAILED);
1083 EXPECT_CALL(mock_callback(), 1089 EXPECT_CALL(mock_callback(),
1084 Run(NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR, 1090 Run(NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR,
1085 /*snippets=*/Not(HasValue()))) 1091 /*snippets=*/Not(HasValue())))
1086 .Times(1); 1092 .Times(1);
1087 snippets_fetcher().FetchSnippets( 1093 snippets_fetcher().FetchSnippets(
1088 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 1094 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
1089 FastForwardUntilNoTasksRemain(); 1095 FastForwardUntilNoTasksRemain();
1090 EXPECT_THAT(snippets_fetcher().last_status(), 1096 EXPECT_THAT(snippets_fetcher().last_status(),
1091 Eq("URLRequestStatus error -2")); 1097 Eq("URLRequestStatus error -2"));
1092 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 1098 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
1093 EXPECT_THAT( 1099 EXPECT_THAT(
1094 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 1100 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
1095 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); 1101 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
1096 EXPECT_THAT(histogram_tester().GetAllSamples( 1102 EXPECT_THAT(histogram_tester().GetAllSamples(
1097 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 1103 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
1098 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 1104 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
1099 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 1105 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
1100 Not(IsEmpty())); 1106 Not(IsEmpty()));
1101 } 1107 }
1102 1108
1103 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 1109 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportHttpError) {
1104 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, 1110 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
1105 net::URLRequestStatus::SUCCESS); 1111 net::URLRequestStatus::SUCCESS);
1106 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::HTTP_ERROR, 1112 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::HTTP_ERROR,
1107 /*snippets=*/Not(HasValue()))) 1113 /*snippets=*/Not(HasValue())))
1108 .Times(1); 1114 .Times(1);
1109 snippets_fetcher().FetchSnippets( 1115 snippets_fetcher().FetchSnippets(
1110 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 1116 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
1111 FastForwardUntilNoTasksRemain(); 1117 FastForwardUntilNoTasksRemain();
1112 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 1118 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
1113 EXPECT_THAT( 1119 EXPECT_THAT(
1114 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 1120 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
1115 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); 1121 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
1116 EXPECT_THAT(histogram_tester().GetAllSamples( 1122 EXPECT_THAT(histogram_tester().GetAllSamples(
1117 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 1123 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
1118 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 1124 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
1119 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 1125 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
1120 Not(IsEmpty())); 1126 Not(IsEmpty()));
1121 } 1127 }
1122 1128
1123 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { 1129 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportJsonError) {
1124 const std::string kInvalidJsonStr = "{ \"recos\": []"; 1130 const std::string kInvalidJsonStr = "{ \"recos\": []";
1125 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, 1131 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK,
1126 net::URLRequestStatus::SUCCESS); 1132 net::URLRequestStatus::SUCCESS);
1127 EXPECT_CALL(mock_callback(), 1133 EXPECT_CALL(mock_callback(),
1128 Run(NTPSnippetsFetcher::FetchResult::JSON_PARSE_ERROR, 1134 Run(NTPSnippetsFetcher::FetchResult::JSON_PARSE_ERROR,
1129 /*snippets=*/Not(HasValue()))) 1135 /*snippets=*/Not(HasValue())))
1130 .Times(1); 1136 .Times(1);
1131 snippets_fetcher().FetchSnippets( 1137 snippets_fetcher().FetchSnippets(
1132 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 1138 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
1133 FastForwardUntilNoTasksRemain(); 1139 FastForwardUntilNoTasksRemain();
1134 EXPECT_THAT(snippets_fetcher().last_status(), 1140 EXPECT_THAT(snippets_fetcher().last_status(),
1135 StartsWith("Received invalid JSON (error ")); 1141 StartsWith("Received invalid JSON (error "));
1136 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); 1142 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr));
1137 EXPECT_THAT( 1143 EXPECT_THAT(
1138 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 1144 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
1139 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 1145 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
1140 EXPECT_THAT(histogram_tester().GetAllSamples( 1146 EXPECT_THAT(histogram_tester().GetAllSamples(
1141 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 1147 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
1142 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 1148 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
1143 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 1149 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
1144 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 1150 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
1145 /*count=*/1))); 1151 /*count=*/1)));
1146 } 1152 }
1147 1153
1148 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { 1154 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
1149 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, 1155 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK,
1150 net::URLRequestStatus::SUCCESS); 1156 net::URLRequestStatus::SUCCESS);
1151 EXPECT_CALL(mock_callback(), 1157 EXPECT_CALL(mock_callback(),
1152 Run(NTPSnippetsFetcher::FetchResult::JSON_PARSE_ERROR, 1158 Run(NTPSnippetsFetcher::FetchResult::JSON_PARSE_ERROR,
1153 /*snippets=*/Not(HasValue()))) 1159 /*snippets=*/Not(HasValue())))
1154 .Times(1); 1160 .Times(1);
1155 snippets_fetcher().FetchSnippets( 1161 snippets_fetcher().FetchSnippets(
1156 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 1162 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
1157 FastForwardUntilNoTasksRemain(); 1163 FastForwardUntilNoTasksRemain();
1158 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); 1164 EXPECT_THAT(snippets_fetcher().last_json(), std::string());
1159 EXPECT_THAT( 1165 EXPECT_THAT(
1160 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 1166 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
1161 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 1167 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
1162 EXPECT_THAT(histogram_tester().GetAllSamples( 1168 EXPECT_THAT(histogram_tester().GetAllSamples(
1163 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 1169 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
1164 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 1170 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
1165 } 1171 }
1166 1172
1167 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { 1173 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportInvalidListError) {
1168 const std::string kJsonStr = 1174 const std::string kJsonStr =
1169 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; 1175 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}";
1170 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 1176 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
1171 net::URLRequestStatus::SUCCESS); 1177 net::URLRequestStatus::SUCCESS);
1172 EXPECT_CALL( 1178 EXPECT_CALL(
1173 mock_callback(), 1179 mock_callback(),
1174 Run(NTPSnippetsFetcher::FetchResult::INVALID_SNIPPET_CONTENT_ERROR, 1180 Run(NTPSnippetsFetcher::FetchResult::INVALID_SNIPPET_CONTENT_ERROR,
1175 /*snippets=*/Not(HasValue()))) 1181 /*snippets=*/Not(HasValue())))
1176 .Times(1); 1182 .Times(1);
1177 snippets_fetcher().FetchSnippets( 1183 snippets_fetcher().FetchSnippets(
1178 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 1184 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
1179 FastForwardUntilNoTasksRemain(); 1185 FastForwardUntilNoTasksRemain();
1180 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 1186 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
1181 EXPECT_THAT( 1187 EXPECT_THAT(
1182 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 1188 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
1183 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); 1189 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
1184 EXPECT_THAT(histogram_tester().GetAllSamples( 1190 EXPECT_THAT(histogram_tester().GetAllSamples(
1185 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 1191 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
1186 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 1192 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
1187 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 1193 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
1188 Not(IsEmpty())); 1194 Not(IsEmpty()));
1189 } 1195 }
1190 1196
1191 // This test actually verifies that the test setup itself is sane, to prevent 1197 // This test actually verifies that the test setup itself is sane, to prevent
1192 // hard-to-reproduce test failures. 1198 // hard-to-reproduce test failures.
1193 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 1199 TEST_F(ChromeReaderSnippetsFetcherTest,
1200 ShouldReportHttpErrorForMissingBakedResponse) {
1194 InitFakeURLFetcherFactory(); 1201 InitFakeURLFetcherFactory();
1195 EXPECT_CALL(mock_callback(), 1202 EXPECT_CALL(mock_callback(),
1196 Run(NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR, 1203 Run(NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR,
1197 /*snippets=*/Not(HasValue()))) 1204 /*snippets=*/Not(HasValue())))
1198 .Times(1); 1205 .Times(1);
1199 snippets_fetcher().FetchSnippets( 1206 snippets_fetcher().FetchSnippets(
1200 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 1207 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
1201 FastForwardUntilNoTasksRemain(); 1208 FastForwardUntilNoTasksRemain();
1202 } 1209 }
1203 1210
1204 TEST_F(NTPSnippetsFetcherTest, ShouldProcessConcurrentFetches) { 1211 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldProcessConcurrentFetches) {
1205 const std::string kJsonStr = "{ \"recos\": [] }"; 1212 const std::string kJsonStr = "{ \"recos\": [] }";
1206 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, 1213 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
1207 net::URLRequestStatus::SUCCESS); 1214 net::URLRequestStatus::SUCCESS);
1208 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, 1215 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS,
1209 IsEmptyArticleList())) 1216 IsEmptyArticleList()))
1210 .Times(5); 1217 .Times(5);
1211 snippets_fetcher().FetchSnippets( 1218 snippets_fetcher().FetchSnippets(
1212 test_params(), ToSnippetsAvailableCallback(&mock_callback())); 1219 test_params(), ToSnippetsAvailableCallback(&mock_callback()));
1213 // More calls to FetchSnippets() do not interrupt the previous. 1220 // More calls to FetchSnippets() do not interrupt the previous.
1214 // Callback is expected to be called once each time. 1221 // Callback is expected to be called once each time.
(...skipping 22 matching lines...) Expand all
1237 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { 1244 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) {
1238 if (fetched_categories) { 1245 if (fetched_categories) {
1239 // Matchers above aren't any more precise than this, so this is sufficient 1246 // Matchers above aren't any more precise than this, so this is sufficient
1240 // for test-failure diagnostics. 1247 // for test-failure diagnostics.
1241 return os << "list with " << fetched_categories->size() << " elements"; 1248 return os << "list with " << fetched_categories->size() << " elements";
1242 } 1249 }
1243 return os << "null"; 1250 return os << "null";
1244 } 1251 }
1245 1252
1246 } // namespace ntp_snippets 1253 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/ntp_snippets_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698