| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/remote_suggestions_provider.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/test/histogram_tester.h" | 22 #include "base/test/histogram_tester.h" |
| 23 #include "base/test/simple_test_tick_clock.h" | 23 #include "base/test/simple_test_tick_clock.h" |
| 24 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
| 25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 26 #include "components/image_fetcher/image_decoder.h" | 26 #include "components/image_fetcher/image_decoder.h" |
| 27 #include "components/image_fetcher/image_fetcher.h" | 27 #include "components/image_fetcher/image_fetcher.h" |
| 28 #include "components/image_fetcher/image_fetcher_delegate.h" | 28 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 29 #include "components/ntp_snippets/category_factory.h" | 29 #include "components/ntp_snippets/category.h" |
| 30 #include "components/ntp_snippets/category_info.h" | 30 #include "components/ntp_snippets/category_info.h" |
| 31 #include "components/ntp_snippets/ntp_snippets_constants.h" | 31 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 32 #include "components/ntp_snippets/pref_names.h" | 32 #include "components/ntp_snippets/pref_names.h" |
| 33 #include "components/ntp_snippets/remote/ntp_snippet.h" | 33 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 34 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 34 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 35 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" | 35 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" |
| 36 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 36 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
| 37 #include "components/ntp_snippets/remote/test_utils.h" | 37 #include "components/ntp_snippets/remote/test_utils.h" |
| 38 #include "components/ntp_snippets/section_rankers/default_constant_section_ranke
r.h" |
| 39 #include "components/ntp_snippets/section_rankers/section_ranker.h" |
| 38 #include "components/ntp_snippets/user_classifier.h" | 40 #include "components/ntp_snippets/user_classifier.h" |
| 39 #include "components/prefs/testing_pref_service.h" | 41 #include "components/prefs/testing_pref_service.h" |
| 40 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 42 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 41 #include "components/signin/core/browser/fake_signin_manager.h" | 43 #include "components/signin/core/browser/fake_signin_manager.h" |
| 42 #include "components/variations/variations_params_manager.h" | 44 #include "components/variations/variations_params_manager.h" |
| 43 #include "net/url_request/test_url_fetcher_factory.h" | 45 #include "net/url_request/test_url_fetcher_factory.h" |
| 44 #include "net/url_request/url_request_test_util.h" | 46 #include "net/url_request/url_request_test_util.h" |
| 45 #include "testing/gmock/include/gmock/gmock.h" | 47 #include "testing/gmock/include/gmock/gmock.h" |
| 46 #include "testing/gtest/include/gtest/gtest.h" | 48 #include "testing/gtest/include/gtest/gtest.h" |
| 47 #include "ui/gfx/image/image.h" | 49 #include "ui/gfx/image/image.h" |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 class RemoteSuggestionsProviderTest : public ::testing::Test { | 396 class RemoteSuggestionsProviderTest : public ::testing::Test { |
| 395 public: | 397 public: |
| 396 RemoteSuggestionsProviderTest() | 398 RemoteSuggestionsProviderTest() |
| 397 : params_manager_(ntp_snippets::kStudyName, | 399 : params_manager_(ntp_snippets::kStudyName, |
| 398 {{"content_suggestions_backend", | 400 {{"content_suggestions_backend", |
| 399 kTestContentSuggestionsServerEndpoint}, | 401 kTestContentSuggestionsServerEndpoint}, |
| 400 {"fetching_personalization", "non_personal"}}), | 402 {"fetching_personalization", "non_personal"}}), |
| 401 fake_url_fetcher_factory_( | 403 fake_url_fetcher_factory_( |
| 402 /*default_factory=*/&failing_url_fetcher_factory_), | 404 /*default_factory=*/&failing_url_fetcher_factory_), |
| 403 test_url_(kTestContentSuggestionsServerWithAPIKey), | 405 test_url_(kTestContentSuggestionsServerWithAPIKey), |
| 406 section_ranker_(base::MakeUnique<DefaultConstantSectionRanker>()), |
| 404 user_classifier_(/*pref_service=*/nullptr), | 407 user_classifier_(/*pref_service=*/nullptr), |
| 405 image_fetcher_(nullptr), | 408 image_fetcher_(nullptr), |
| 406 image_decoder_(nullptr) { | 409 image_decoder_(nullptr) { |
| 407 RemoteSuggestionsProvider::RegisterProfilePrefs( | 410 RemoteSuggestionsProvider::RegisterProfilePrefs( |
| 408 utils_.pref_service()->registry()); | 411 utils_.pref_service()->registry()); |
| 409 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | 412 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 410 | 413 |
| 411 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); | 414 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); |
| 412 } | 415 } |
| 413 | 416 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 429 MakeSnippetsServiceWithoutInitialization() { | 432 MakeSnippetsServiceWithoutInitialization() { |
| 430 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 433 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 431 base::ThreadTaskRunnerHandle::Get()); | 434 base::ThreadTaskRunnerHandle::Get()); |
| 432 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 435 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 433 new net::TestURLRequestContextGetter(task_runner.get()); | 436 new net::TestURLRequestContextGetter(task_runner.get()); |
| 434 | 437 |
| 435 utils_.ResetSigninManager(); | 438 utils_.ResetSigninManager(); |
| 436 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = | 439 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = |
| 437 base::MakeUnique<NTPSnippetsFetcher>( | 440 base::MakeUnique<NTPSnippetsFetcher>( |
| 438 utils_.fake_signin_manager(), fake_token_service_.get(), | 441 utils_.fake_signin_manager(), fake_token_service_.get(), |
| 439 std::move(request_context_getter), utils_.pref_service(), | 442 std::move(request_context_getter), utils_.pref_service(), nullptr, |
| 440 &category_factory_, nullptr, base::Bind(&ParseJson), kAPIKey, | 443 base::Bind(&ParseJson), kAPIKey, &user_classifier_); |
| 441 &user_classifier_); | |
| 442 | 444 |
| 443 utils_.fake_signin_manager()->SignIn("foo@bar.com"); | 445 utils_.fake_signin_manager()->SignIn("foo@bar.com"); |
| 444 | 446 |
| 445 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); | 447 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); |
| 446 | 448 |
| 447 image_fetcher_ = image_fetcher.get(); | 449 image_fetcher_ = image_fetcher.get(); |
| 448 EXPECT_CALL(*image_fetcher, SetImageFetcherDelegate(_)); | 450 EXPECT_CALL(*image_fetcher, SetImageFetcherDelegate(_)); |
| 449 auto image_decoder = base::MakeUnique<FakeImageDecoder>(); | 451 auto image_decoder = base::MakeUnique<FakeImageDecoder>(); |
| 450 image_decoder_ = image_decoder.get(); | 452 image_decoder_ = image_decoder.get(); |
| 451 EXPECT_FALSE(observer_); | 453 EXPECT_FALSE(observer_); |
| 452 observer_ = base::MakeUnique<FakeContentSuggestionsProviderObserver>(); | 454 observer_ = base::MakeUnique<FakeContentSuggestionsProviderObserver>(); |
| 453 return base::MakeUnique<RemoteSuggestionsProvider>( | 455 return base::MakeUnique<RemoteSuggestionsProvider>( |
| 454 observer_.get(), &category_factory_, utils_.pref_service(), "fr", | 456 observer_.get(), utils_.pref_service(), "fr", section_ranker_.get(), |
| 455 &user_classifier_, &scheduler_, std::move(snippets_fetcher), | 457 &user_classifier_, &scheduler_, std::move(snippets_fetcher), |
| 456 std::move(image_fetcher), std::move(image_decoder), | 458 std::move(image_fetcher), std::move(image_decoder), |
| 457 base::MakeUnique<RemoteSuggestionsDatabase>(database_dir_.GetPath(), | 459 base::MakeUnique<RemoteSuggestionsDatabase>(database_dir_.GetPath(), |
| 458 task_runner), | 460 task_runner), |
| 459 base::MakeUnique<RemoteSuggestionsStatusService>( | 461 base::MakeUnique<RemoteSuggestionsStatusService>( |
| 460 utils_.fake_signin_manager(), utils_.pref_service())); | 462 utils_.fake_signin_manager(), utils_.pref_service())); |
| 461 } | 463 } |
| 462 | 464 |
| 463 void WaitForSnippetsServiceInitialization(RemoteSuggestionsProvider* service, | 465 void WaitForSnippetsServiceInitialization(RemoteSuggestionsProvider* service, |
| 464 bool set_empty_response) { | 466 bool set_empty_response) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 480 service->reset(); | 482 service->reset(); |
| 481 observer_.reset(); | 483 observer_.reset(); |
| 482 *service = MakeSnippetsService(); | 484 *service = MakeSnippetsService(); |
| 483 } | 485 } |
| 484 | 486 |
| 485 ContentSuggestion::ID MakeArticleID(const std::string& id_within_category) { | 487 ContentSuggestion::ID MakeArticleID(const std::string& id_within_category) { |
| 486 return ContentSuggestion::ID(articles_category(), id_within_category); | 488 return ContentSuggestion::ID(articles_category(), id_within_category); |
| 487 } | 489 } |
| 488 | 490 |
| 489 Category articles_category() { | 491 Category articles_category() { |
| 490 return category_factory_.FromKnownCategory(KnownCategories::ARTICLES); | 492 return Category::FromKnownCategory(KnownCategories::ARTICLES); |
| 491 } | 493 } |
| 492 | 494 |
| 493 ContentSuggestion::ID MakeOtherID(const std::string& id_within_category) { | 495 ContentSuggestion::ID MakeOtherID(const std::string& id_within_category) { |
| 494 return ContentSuggestion::ID(other_category(), id_within_category); | 496 return ContentSuggestion::ID(other_category(), id_within_category); |
| 495 } | 497 } |
| 496 | 498 |
| 497 Category other_category() { return category_factory_.FromRemoteCategory(2); } | 499 Category other_category() { return Category::FromRemoteCategory(2); } |
| 498 | 500 |
| 499 Category unknown_category() { | 501 Category unknown_category() { |
| 500 return category_factory_.FromRemoteCategory(kUnknownRemoteCategoryId); | 502 return Category::FromRemoteCategory(kUnknownRemoteCategoryId); |
| 501 } | 503 } |
| 502 | 504 |
| 503 protected: | 505 protected: |
| 504 const GURL& test_url() { return test_url_; } | 506 const GURL& test_url() { return test_url_; } |
| 505 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } | 507 FakeContentSuggestionsProviderObserver& observer() { return *observer_; } |
| 506 MockScheduler& mock_scheduler() { return scheduler_; } | 508 MockScheduler& mock_scheduler() { return scheduler_; } |
| 507 // TODO(tschumann): Make this a strict-mock. We want to avoid unneccesary | 509 // TODO(tschumann): Make this a strict-mock. We want to avoid unneccesary |
| 508 // network requests. | 510 // network requests. |
| 509 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } | 511 NiceMock<MockImageFetcher>* image_fetcher() { return image_fetcher_; } |
| 510 FakeImageDecoder* image_decoder() { return image_decoder_; } | 512 FakeImageDecoder* image_decoder() { return image_decoder_; } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 535 | 537 |
| 536 private: | 538 private: |
| 537 variations::testing::VariationParamsManager params_manager_; | 539 variations::testing::VariationParamsManager params_manager_; |
| 538 test::RemoteSuggestionsTestUtils utils_; | 540 test::RemoteSuggestionsTestUtils utils_; |
| 539 base::MessageLoop message_loop_; | 541 base::MessageLoop message_loop_; |
| 540 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 542 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 541 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 543 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
| 542 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 544 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
| 543 const GURL test_url_; | 545 const GURL test_url_; |
| 544 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 546 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 547 std::unique_ptr<SectionRanker> section_ranker_; |
| 545 UserClassifier user_classifier_; | 548 UserClassifier user_classifier_; |
| 546 NiceMock<MockScheduler> scheduler_; | 549 NiceMock<MockScheduler> scheduler_; |
| 547 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; | 550 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; |
| 548 CategoryFactory category_factory_; | |
| 549 NiceMock<MockImageFetcher>* image_fetcher_; | 551 NiceMock<MockImageFetcher>* image_fetcher_; |
| 550 FakeImageDecoder* image_decoder_; | 552 FakeImageDecoder* image_decoder_; |
| 551 | 553 |
| 552 base::ScopedTempDir database_dir_; | 554 base::ScopedTempDir database_dir_; |
| 553 | 555 |
| 554 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderTest); | 556 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderTest); |
| 555 }; | 557 }; |
| 556 | 558 |
| 557 TEST_F(RemoteSuggestionsProviderTest, ScheduleOnStart) { | 559 TEST_F(RemoteSuggestionsProviderTest, ScheduleOnStart) { |
| 558 // We should get two |Schedule| calls: The first when initialization | 560 // We should get two |Schedule| calls: The first when initialization |
| (...skipping 1062 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1621 service->FetchSnippetsInTheBackground(); | 1623 service->FetchSnippetsInTheBackground(); |
| 1622 base::RunLoop().RunUntilIdle(); | 1624 base::RunLoop().RunUntilIdle(); |
| 1623 EXPECT_EQ( | 1625 EXPECT_EQ( |
| 1624 simple_test_tick_clock_ptr->NowTicks().ToInternalValue(), | 1626 simple_test_tick_clock_ptr->NowTicks().ToInternalValue(), |
| 1625 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | 1627 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); |
| 1626 // TODO(markusheintz): Add a test that simulates a browser restart once the | 1628 // TODO(markusheintz): Add a test that simulates a browser restart once the |
| 1627 // scheduler refactoring is done (crbug.com/672434). | 1629 // scheduler refactoring is done (crbug.com/672434). |
| 1628 } | 1630 } |
| 1629 | 1631 |
| 1630 } // namespace ntp_snippets | 1632 } // namespace ntp_snippets |
| OLD | NEW |