| 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 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // Increase initial time such that ticks are non-zero. | 304 // Increase initial time such that ticks are non-zero. |
| 305 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 305 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
| 306 ResetSnippetsFetcher(); | 306 ResetSnippetsFetcher(); |
| 307 } | 307 } |
| 308 | 308 |
| 309 void ResetSnippetsFetcher() { | 309 void ResetSnippetsFetcher() { |
| 310 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 310 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
| 311 fake_signin_manager_.get(), fake_token_service_.get(), | 311 fake_signin_manager_.get(), fake_token_service_.get(), |
| 312 scoped_refptr<net::TestURLRequestContextGetter>( | 312 scoped_refptr<net::TestURLRequestContextGetter>( |
| 313 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 313 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
| 314 pref_service_.get(), &category_factory_, nullptr, | 314 pref_service_.get(), nullptr, base::Bind(&ParseJsonDelayed), kAPIKey, |
| 315 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); | 315 user_classifier_.get()); |
| 316 | 316 |
| 317 snippets_fetcher_->SetTickClockForTesting( | 317 snippets_fetcher_->SetTickClockForTesting( |
| 318 mock_task_runner_->GetMockTickClock()); | 318 mock_task_runner_->GetMockTickClock()); |
| 319 } | 319 } |
| 320 | 320 |
| 321 NTPSnippetsFetcher::SnippetsAvailableCallback ToSnippetsAvailableCallback( | 321 NTPSnippetsFetcher::SnippetsAvailableCallback ToSnippetsAvailableCallback( |
| 322 MockSnippetsAvailableCallback* callback) { | 322 MockSnippetsAvailableCallback* callback) { |
| 323 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, | 323 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, |
| 324 base::Unretained(callback)); | 324 base::Unretained(callback)); |
| 325 } | 325 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 407 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 408 // Initialized lazily in SetFakeResponse(). | 408 // Initialized lazily in SetFakeResponse(). |
| 409 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 409 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
| 410 std::unique_ptr<TestSigninClient> signin_client_; | 410 std::unique_ptr<TestSigninClient> signin_client_; |
| 411 std::unique_ptr<AccountTrackerService> account_tracker_; | 411 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 412 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 412 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 413 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 413 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 414 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 414 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 415 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 415 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 416 std::unique_ptr<UserClassifier> user_classifier_; | 416 std::unique_ptr<UserClassifier> user_classifier_; |
| 417 CategoryFactory category_factory_; | |
| 418 MockSnippetsAvailableCallback mock_callback_; | 417 MockSnippetsAvailableCallback mock_callback_; |
| 419 const GURL test_url_; | 418 const GURL test_url_; |
| 420 base::HistogramTester histogram_tester_; | 419 base::HistogramTester histogram_tester_; |
| 421 | 420 |
| 422 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTestBase); | 421 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTestBase); |
| 423 }; | 422 }; |
| 424 | 423 |
| 425 class ChromeReaderSnippetsFetcherTest : public NTPSnippetsFetcherTestBase { | 424 class ChromeReaderSnippetsFetcherTest : public NTPSnippetsFetcherTestBase { |
| 426 public: | 425 public: |
| 427 ChromeReaderSnippetsFetcherTest() | 426 ChromeReaderSnippetsFetcherTest() |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 FastForwardUntilNoTasksRemain(); | 809 FastForwardUntilNoTasksRemain(); |
| 811 | 810 |
| 812 ASSERT_TRUE(fetched_categories); | 811 ASSERT_TRUE(fetched_categories); |
| 813 ASSERT_THAT(fetched_categories->size(), Eq(2u)); | 812 ASSERT_THAT(fetched_categories->size(), Eq(2u)); |
| 814 for (const auto& category : *fetched_categories) { | 813 for (const auto& category : *fetched_categories) { |
| 815 const auto& articles = category.snippets; | 814 const auto& articles = category.snippets; |
| 816 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { | 815 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { |
| 817 ASSERT_THAT(articles.size(), Eq(1u)); | 816 ASSERT_THAT(articles.size(), Eq(1u)); |
| 818 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); | 817 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); |
| 819 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); | 818 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); |
| 820 } else if (category.category == CategoryFactory().FromRemoteCategory(2)) { | 819 } else if (category.category == Category::FromRemoteCategory(2)) { |
| 821 ASSERT_THAT(articles.size(), Eq(1u)); | 820 ASSERT_THAT(articles.size(), Eq(1u)); |
| 822 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); | 821 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); |
| 823 EXPECT_THAT(category.info.has_more_action(), Eq(true)); | 822 EXPECT_THAT(category.info.has_more_action(), Eq(true)); |
| 824 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); | 823 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); |
| 825 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); | 824 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); |
| 826 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); | 825 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); |
| 827 } else { | 826 } else { |
| 828 FAIL() << "unknown category ID " << category.category.id(); | 827 FAIL() << "unknown category ID " << category.category.id(); |
| 829 } | 828 } |
| 830 } | 829 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 926 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 925 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 927 " }]" | 926 " }]" |
| 928 "}]}"; | 927 "}]}"; |
| 929 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 928 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 930 net::URLRequestStatus::SUCCESS); | 929 net::URLRequestStatus::SUCCESS); |
| 931 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; | 930 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
| 932 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, _)) | 931 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, _)) |
| 933 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); | 932 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); |
| 934 | 933 |
| 935 NTPSnippetsFetcher::Params params = test_params(); | 934 NTPSnippetsFetcher::Params params = test_params(); |
| 936 params.exclusive_category = base::Optional<Category>( | 935 params.exclusive_category = |
| 937 CategoryFactory().FromRemoteCategory(2)); | 936 base::Optional<Category>(Category::FromRemoteCategory(2)); |
| 938 snippets_fetcher().FetchSnippets( | 937 snippets_fetcher().FetchSnippets( |
| 939 params, ToSnippetsAvailableCallback(&mock_callback())); | 938 params, ToSnippetsAvailableCallback(&mock_callback())); |
| 940 FastForwardUntilNoTasksRemain(); | 939 FastForwardUntilNoTasksRemain(); |
| 941 | 940 |
| 942 ASSERT_TRUE(fetched_categories); | 941 ASSERT_TRUE(fetched_categories); |
| 943 ASSERT_THAT(fetched_categories->size(), Eq(1u)); | 942 ASSERT_THAT(fetched_categories->size(), Eq(1u)); |
| 944 const auto& category = (*fetched_categories)[0]; | 943 const auto& category = (*fetched_categories)[0]; |
| 945 EXPECT_THAT(category.category.id(), | 944 EXPECT_THAT(category.category.id(), Eq(Category::FromRemoteCategory(2).id())); |
| 946 Eq(CategoryFactory().FromRemoteCategory(2).id())); | |
| 947 ASSERT_THAT(category.snippets.size(), Eq(1u)); | 945 ASSERT_THAT(category.snippets.size(), Eq(1u)); |
| 948 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); | 946 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); |
| 949 } | 947 } |
| 950 | 948 |
| 951 TEST_F(ChromeReaderSnippetsFetcherTest, PersonalizesDependingOnVariations) { | 949 TEST_F(ChromeReaderSnippetsFetcherTest, PersonalizesDependingOnVariations) { |
| 952 // Default setting should be both personalization options. | 950 // Default setting should be both personalization options. |
| 953 EXPECT_THAT(snippets_fetcher().personalization(), | 951 EXPECT_THAT(snippets_fetcher().personalization(), |
| 954 Eq(NTPSnippetsFetcher::Personalization::kBoth)); | 952 Eq(NTPSnippetsFetcher::Personalization::kBoth)); |
| 955 | 953 |
| 956 SetVariationParam("fetching_personalization", "personal"); | 954 SetVariationParam("fetching_personalization", "personal"); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1194 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 1192 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
| 1195 if (fetched_categories) { | 1193 if (fetched_categories) { |
| 1196 // Matchers above aren't any more precise than this, so this is sufficient | 1194 // Matchers above aren't any more precise than this, so this is sufficient |
| 1197 // for test-failure diagnostics. | 1195 // for test-failure diagnostics. |
| 1198 return os << "list with " << fetched_categories->size() << " elements"; | 1196 return os << "list with " << fetched_categories->size() << " elements"; |
| 1199 } | 1197 } |
| 1200 return os << "null"; | 1198 return os << "null"; |
| 1201 } | 1199 } |
| 1202 | 1200 |
| 1203 } // namespace ntp_snippets | 1201 } // namespace ntp_snippets |
| OLD | NEW |