| 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 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 FastForwardUntilNoTasksRemain(); | 825 FastForwardUntilNoTasksRemain(); |
| 827 | 826 |
| 828 ASSERT_TRUE(fetched_categories); | 827 ASSERT_TRUE(fetched_categories); |
| 829 ASSERT_THAT(fetched_categories->size(), Eq(2u)); | 828 ASSERT_THAT(fetched_categories->size(), Eq(2u)); |
| 830 for (const auto& category : *fetched_categories) { | 829 for (const auto& category : *fetched_categories) { |
| 831 const auto& articles = category.snippets; | 830 const auto& articles = category.snippets; |
| 832 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { | 831 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { |
| 833 ASSERT_THAT(articles.size(), Eq(1u)); | 832 ASSERT_THAT(articles.size(), Eq(1u)); |
| 834 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); | 833 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); |
| 835 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); | 834 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); |
| 836 } else if (category.category == CategoryFactory().FromRemoteCategory(2)) { | 835 } else if (category.category == Category::FromRemoteCategory(2)) { |
| 837 ASSERT_THAT(articles.size(), Eq(1u)); | 836 ASSERT_THAT(articles.size(), Eq(1u)); |
| 838 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); | 837 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); |
| 839 EXPECT_THAT(category.info.has_more_action(), Eq(true)); | 838 EXPECT_THAT(category.info.has_more_action(), Eq(true)); |
| 840 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); | 839 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); |
| 841 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); | 840 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); |
| 842 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); | 841 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); |
| 843 } else { | 842 } else { |
| 844 FAIL() << "unknown category ID " << category.category.id(); | 843 FAIL() << "unknown category ID " << category.category.id(); |
| 845 } | 844 } |
| 846 } | 845 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 941 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 943 " }]" | 942 " }]" |
| 944 "}]}"; | 943 "}]}"; |
| 945 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 944 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| 946 net::URLRequestStatus::SUCCESS); | 945 net::URLRequestStatus::SUCCESS); |
| 947 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; | 946 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
| 948 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, _)) | 947 EXPECT_CALL(mock_callback(), Run(NTPSnippetsFetcher::FetchResult::SUCCESS, _)) |
| 949 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); | 948 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); |
| 950 | 949 |
| 951 NTPSnippetsFetcher::Params params = test_params(); | 950 NTPSnippetsFetcher::Params params = test_params(); |
| 952 params.exclusive_category = base::Optional<Category>( | 951 params.exclusive_category = |
| 953 CategoryFactory().FromRemoteCategory(2)); | 952 base::Optional<Category>(Category::FromRemoteCategory(2)); |
| 954 snippets_fetcher().FetchSnippets( | 953 snippets_fetcher().FetchSnippets( |
| 955 params, ToSnippetsAvailableCallback(&mock_callback())); | 954 params, ToSnippetsAvailableCallback(&mock_callback())); |
| 956 FastForwardUntilNoTasksRemain(); | 955 FastForwardUntilNoTasksRemain(); |
| 957 | 956 |
| 958 ASSERT_TRUE(fetched_categories); | 957 ASSERT_TRUE(fetched_categories); |
| 959 ASSERT_THAT(fetched_categories->size(), Eq(1u)); | 958 ASSERT_THAT(fetched_categories->size(), Eq(1u)); |
| 960 const auto& category = (*fetched_categories)[0]; | 959 const auto& category = (*fetched_categories)[0]; |
| 961 EXPECT_THAT(category.category.id(), | 960 EXPECT_THAT(category.category.id(), Eq(Category::FromRemoteCategory(2).id())); |
| 962 Eq(CategoryFactory().FromRemoteCategory(2).id())); | |
| 963 ASSERT_THAT(category.snippets.size(), Eq(1u)); | 961 ASSERT_THAT(category.snippets.size(), Eq(1u)); |
| 964 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); | 962 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); |
| 965 } | 963 } |
| 966 | 964 |
| 967 TEST_F(ChromeReaderSnippetsFetcherTest, PersonalizesDependingOnVariations) { | 965 TEST_F(ChromeReaderSnippetsFetcherTest, PersonalizesDependingOnVariations) { |
| 968 // Default setting should be both personalization options. | 966 // Default setting should be both personalization options. |
| 969 EXPECT_THAT(snippets_fetcher().personalization(), | 967 EXPECT_THAT(snippets_fetcher().personalization(), |
| 970 Eq(NTPSnippetsFetcher::Personalization::kBoth)); | 968 Eq(NTPSnippetsFetcher::Personalization::kBoth)); |
| 971 | 969 |
| 972 SetVariationParam("fetching_personalization", "personal"); | 970 SetVariationParam("fetching_personalization", "personal"); |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1244 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 1242 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
| 1245 if (fetched_categories) { | 1243 if (fetched_categories) { |
| 1246 // Matchers above aren't any more precise than this, so this is sufficient | 1244 // Matchers above aren't any more precise than this, so this is sufficient |
| 1247 // for test-failure diagnostics. | 1245 // for test-failure diagnostics. |
| 1248 return os << "list with " << fetched_categories->size() << " elements"; | 1246 return os << "list with " << fetched_categories->size() << " elements"; |
| 1249 } | 1247 } |
| 1250 return os << "null"; | 1248 return os << "null"; |
| 1251 } | 1249 } |
| 1252 | 1250 |
| 1253 } // namespace ntp_snippets | 1251 } // namespace ntp_snippets |
| OLD | NEW |