| 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/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 |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/test/test_mock_time_task_runner.h" | 15 #include "base/test/test_mock_time_task_runner.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "components/ntp_snippets/category_factory.h" | 19 #include "components/ntp_snippets/category.h" |
| 20 #include "components/ntp_snippets/features.h" | 20 #include "components/ntp_snippets/features.h" |
| 21 #include "components/ntp_snippets/ntp_snippets_constants.h" | 21 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 22 #include "components/ntp_snippets/remote/ntp_snippet.h" | 22 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 23 #include "components/ntp_snippets/user_classifier.h" | 23 #include "components/ntp_snippets/user_classifier.h" |
| 24 #include "components/prefs/testing_pref_service.h" | 24 #include "components/prefs/testing_pref_service.h" |
| 25 #include "components/signin/core/browser/account_tracker_service.h" | 25 #include "components/signin/core/browser/account_tracker_service.h" |
| 26 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 26 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 27 #include "components/signin/core/browser/fake_signin_manager.h" | 27 #include "components/signin/core/browser/fake_signin_manager.h" |
| 28 #include "components/signin/core/browser/test_signin_client.h" | 28 #include "components/signin/core/browser/test_signin_client.h" |
| 29 #include "components/variations/entropy_provider.h" | 29 #include "components/variations/entropy_provider.h" |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 // Increase initial time such that ticks are non-zero. | 317 // Increase initial time such that ticks are non-zero. |
| 318 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 318 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
| 319 ResetSnippetsFetcher(); | 319 ResetSnippetsFetcher(); |
| 320 } | 320 } |
| 321 | 321 |
| 322 void ResetSnippetsFetcher() { | 322 void ResetSnippetsFetcher() { |
| 323 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 323 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
| 324 fake_signin_manager_.get(), fake_token_service_.get(), | 324 fake_signin_manager_.get(), fake_token_service_.get(), |
| 325 scoped_refptr<net::TestURLRequestContextGetter>( | 325 scoped_refptr<net::TestURLRequestContextGetter>( |
| 326 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 326 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
| 327 pref_service_.get(), &category_factory_, nullptr, | 327 pref_service_.get(), nullptr, base::Bind(&ParseJsonDelayed), kAPIKey, |
| 328 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); | 328 user_classifier_.get()); |
| 329 | 329 |
| 330 snippets_fetcher_->SetTickClockForTesting( | 330 snippets_fetcher_->SetTickClockForTesting( |
| 331 mock_task_runner_->GetMockTickClock()); | 331 mock_task_runner_->GetMockTickClock()); |
| 332 } | 332 } |
| 333 | 333 |
| 334 NTPSnippetsFetcher::SnippetsAvailableCallback ToSnippetsAvailableCallback( | 334 NTPSnippetsFetcher::SnippetsAvailableCallback ToSnippetsAvailableCallback( |
| 335 MockSnippetsAvailableCallback* callback) { | 335 MockSnippetsAvailableCallback* callback) { |
| 336 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, | 336 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, |
| 337 base::Unretained(callback)); | 337 base::Unretained(callback)); |
| 338 } | 338 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 411 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 412 // Initialized lazily in SetFakeResponse(). | 412 // Initialized lazily in SetFakeResponse(). |
| 413 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 413 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
| 414 std::unique_ptr<TestSigninClient> signin_client_; | 414 std::unique_ptr<TestSigninClient> signin_client_; |
| 415 std::unique_ptr<AccountTrackerService> account_tracker_; | 415 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 416 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 416 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 417 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 417 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 418 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 418 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 419 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 419 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 420 std::unique_ptr<UserClassifier> user_classifier_; | 420 std::unique_ptr<UserClassifier> user_classifier_; |
| 421 CategoryFactory category_factory_; | |
| 422 MockSnippetsAvailableCallback mock_callback_; | 421 MockSnippetsAvailableCallback mock_callback_; |
| 423 const GURL test_url_; | 422 const GURL test_url_; |
| 424 base::HistogramTester histogram_tester_; | 423 base::HistogramTester histogram_tester_; |
| 425 | 424 |
| 426 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | 425 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
| 427 }; | 426 }; |
| 428 | 427 |
| 429 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { | 428 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { |
| 430 public: | 429 public: |
| 431 NTPSnippetsContentSuggestionsFetcherTest() | 430 NTPSnippetsContentSuggestionsFetcherTest() |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 FastForwardUntilNoTasksRemain(); | 820 FastForwardUntilNoTasksRemain(); |
| 822 | 821 |
| 823 ASSERT_TRUE(fetched_categories); | 822 ASSERT_TRUE(fetched_categories); |
| 824 ASSERT_THAT(fetched_categories->size(), Eq(2u)); | 823 ASSERT_THAT(fetched_categories->size(), Eq(2u)); |
| 825 for (const auto& category : *fetched_categories) { | 824 for (const auto& category : *fetched_categories) { |
| 826 const auto& articles = category.snippets; | 825 const auto& articles = category.snippets; |
| 827 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { | 826 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { |
| 828 ASSERT_THAT(articles.size(), Eq(1u)); | 827 ASSERT_THAT(articles.size(), Eq(1u)); |
| 829 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); | 828 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); |
| 830 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); | 829 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); |
| 831 } else if (category.category == CategoryFactory().FromRemoteCategory(2)) { | 830 } else if (category.category == Category::FromRemoteCategory(2)) { |
| 832 ASSERT_THAT(articles.size(), Eq(1u)); | 831 ASSERT_THAT(articles.size(), Eq(1u)); |
| 833 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); | 832 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); |
| 834 EXPECT_THAT(category.info.has_more_action(), Eq(true)); | 833 EXPECT_THAT(category.info.has_more_action(), Eq(true)); |
| 835 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); | 834 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); |
| 836 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); | 835 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); |
| 837 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); | 836 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); |
| 838 } else { | 837 } else { |
| 839 FAIL() << "unknown category ID " << category.category.id(); | 838 FAIL() << "unknown category ID " << category.category.id(); |
| 840 } | 839 } |
| 841 } | 840 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 937 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 936 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 938 " }]" | 937 " }]" |
| 939 "}]}"; | 938 "}]}"; |
| 940 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 939 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 941 net::URLRequestStatus::SUCCESS); | 940 net::URLRequestStatus::SUCCESS); |
| 942 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; | 941 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
| 943 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, _)) | 942 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, _)) |
| 944 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); | 943 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); |
| 945 | 944 |
| 946 NTPSnippetsFetcher::Params params = test_params(); | 945 NTPSnippetsFetcher::Params params = test_params(); |
| 947 params.exclusive_category = base::Optional<Category>( | 946 params.exclusive_category = |
| 948 CategoryFactory().FromRemoteCategory(2)); | 947 base::Optional<Category>(Category::FromRemoteCategory(2)); |
| 949 snippets_fetcher().FetchSnippets( | 948 snippets_fetcher().FetchSnippets( |
| 950 params, ToSnippetsAvailableCallback(&mock_callback())); | 949 params, ToSnippetsAvailableCallback(&mock_callback())); |
| 951 FastForwardUntilNoTasksRemain(); | 950 FastForwardUntilNoTasksRemain(); |
| 952 | 951 |
| 953 ASSERT_TRUE(fetched_categories); | 952 ASSERT_TRUE(fetched_categories); |
| 954 ASSERT_THAT(fetched_categories->size(), Eq(1u)); | 953 ASSERT_THAT(fetched_categories->size(), Eq(1u)); |
| 955 const auto& category = (*fetched_categories)[0]; | 954 const auto& category = (*fetched_categories)[0]; |
| 956 EXPECT_THAT(category.category.id(), | 955 EXPECT_THAT(category.category.id(), Eq(Category::FromRemoteCategory(2).id())); |
| 957 Eq(CategoryFactory().FromRemoteCategory(2).id())); | |
| 958 ASSERT_THAT(category.snippets.size(), Eq(1u)); | 956 ASSERT_THAT(category.snippets.size(), Eq(1u)); |
| 959 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); | 957 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); |
| 960 } | 958 } |
| 961 | 959 |
| 962 TEST_F(NTPSnippetsFetcherTest, PersonalizesDependingOnVariations) { | 960 TEST_F(NTPSnippetsFetcherTest, PersonalizesDependingOnVariations) { |
| 963 // Default setting should be both personalization options. | 961 // Default setting should be both personalization options. |
| 964 EXPECT_THAT(snippets_fetcher().personalization(), | 962 EXPECT_THAT(snippets_fetcher().personalization(), |
| 965 Eq(NTPSnippetsFetcher::Personalization::kBoth)); | 963 Eq(NTPSnippetsFetcher::Personalization::kBoth)); |
| 966 | 964 |
| 967 SetFetchingPersonalizationVariation("personal"); | 965 SetFetchingPersonalizationVariation("personal"); |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1237 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 1235 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
| 1238 if (fetched_categories) { | 1236 if (fetched_categories) { |
| 1239 // Matchers above aren't any more precise than this, so this is sufficient | 1237 // Matchers above aren't any more precise than this, so this is sufficient |
| 1240 // for test-failure diagnostics. | 1238 // for test-failure diagnostics. |
| 1241 return os << "list with " << fetched_categories->size() << " elements"; | 1239 return os << "list with " << fetched_categories->size() << " elements"; |
| 1242 } | 1240 } |
| 1243 return os << "null"; | 1241 return os << "null"; |
| 1244 } | 1242 } |
| 1245 | 1243 |
| 1246 } // namespace ntp_snippets | 1244 } // namespace ntp_snippets |
| OLD | NEW |