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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 // Increase initial time such that ticks are non-zero. | 314 // Increase initial time such that ticks are non-zero. |
315 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 315 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
316 ResetSnippetsFetcher(); | 316 ResetSnippetsFetcher(); |
317 } | 317 } |
318 | 318 |
319 void ResetSnippetsFetcher() { | 319 void ResetSnippetsFetcher() { |
320 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 320 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
321 fake_signin_manager_.get(), fake_token_service_.get(), | 321 fake_signin_manager_.get(), fake_token_service_.get(), |
322 scoped_refptr<net::TestURLRequestContextGetter>( | 322 scoped_refptr<net::TestURLRequestContextGetter>( |
323 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 323 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
324 pref_service_.get(), &category_factory_, nullptr, | 324 pref_service_.get(), nullptr, base::Bind(&ParseJsonDelayed), kAPIKey, |
325 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); | 325 user_classifier_.get()); |
326 | 326 |
327 snippets_fetcher_->SetTickClockForTesting( | 327 snippets_fetcher_->SetTickClockForTesting( |
328 mock_task_runner_->GetMockTickClock()); | 328 mock_task_runner_->GetMockTickClock()); |
329 } | 329 } |
330 | 330 |
331 NTPSnippetsFetcher::SnippetsAvailableCallback ToSnippetsAvailableCallback( | 331 NTPSnippetsFetcher::SnippetsAvailableCallback ToSnippetsAvailableCallback( |
332 MockSnippetsAvailableCallback* callback) { | 332 MockSnippetsAvailableCallback* callback) { |
333 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, | 333 return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, |
334 base::Unretained(callback)); | 334 base::Unretained(callback)); |
335 } | 335 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 417 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
418 // Initialized lazily in SetFakeResponse(). | 418 // Initialized lazily in SetFakeResponse(). |
419 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 419 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
420 std::unique_ptr<TestSigninClient> signin_client_; | 420 std::unique_ptr<TestSigninClient> signin_client_; |
421 std::unique_ptr<AccountTrackerService> account_tracker_; | 421 std::unique_ptr<AccountTrackerService> account_tracker_; |
422 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 422 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
423 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 423 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
424 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 424 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
425 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 425 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
426 std::unique_ptr<UserClassifier> user_classifier_; | 426 std::unique_ptr<UserClassifier> user_classifier_; |
427 CategoryFactory category_factory_; | |
428 MockSnippetsAvailableCallback mock_callback_; | 427 MockSnippetsAvailableCallback mock_callback_; |
429 const GURL test_url_; | 428 const GURL test_url_; |
430 base::HistogramTester histogram_tester_; | 429 base::HistogramTester histogram_tester_; |
431 | 430 |
432 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTestBase); | 431 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTestBase); |
433 }; | 432 }; |
434 | 433 |
435 class ChromeReaderSnippetsFetcherTest : public NTPSnippetsFetcherTestBase { | 434 class ChromeReaderSnippetsFetcherTest : public NTPSnippetsFetcherTestBase { |
436 public: | 435 public: |
437 ChromeReaderSnippetsFetcherTest() | 436 ChromeReaderSnippetsFetcherTest() |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 FastForwardUntilNoTasksRemain(); | 818 FastForwardUntilNoTasksRemain(); |
820 | 819 |
821 ASSERT_TRUE(fetched_categories); | 820 ASSERT_TRUE(fetched_categories); |
822 ASSERT_THAT(fetched_categories->size(), Eq(2u)); | 821 ASSERT_THAT(fetched_categories->size(), Eq(2u)); |
823 for (const auto& category : *fetched_categories) { | 822 for (const auto& category : *fetched_categories) { |
824 const auto& articles = category.snippets; | 823 const auto& articles = category.snippets; |
825 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { | 824 if (category.category.IsKnownCategory(KnownCategories::ARTICLES)) { |
826 ASSERT_THAT(articles.size(), Eq(1u)); | 825 ASSERT_THAT(articles.size(), Eq(1u)); |
827 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); | 826 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foobar")); |
828 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); | 827 EXPECT_THAT(category.info, IsCategoryInfoForArticles()); |
829 } else if (category.category == CategoryFactory().FromRemoteCategory(2)) { | 828 } else if (category.category == Category::FromRemoteCategory(2)) { |
830 ASSERT_THAT(articles.size(), Eq(1u)); | 829 ASSERT_THAT(articles.size(), Eq(1u)); |
831 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); | 830 EXPECT_THAT(articles[0]->url().spec(), Eq("http://localhost/foo2")); |
832 EXPECT_THAT(category.info.has_more_action(), Eq(true)); | 831 EXPECT_THAT(category.info.has_more_action(), Eq(true)); |
833 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); | 832 EXPECT_THAT(category.info.has_reload_action(), Eq(true)); |
834 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); | 833 EXPECT_THAT(category.info.has_view_all_action(), Eq(false)); |
835 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); | 834 EXPECT_THAT(category.info.show_if_empty(), Eq(false)); |
836 } else { | 835 } else { |
837 FAIL() << "unknown category ID " << category.category.id(); | 836 FAIL() << "unknown category ID " << category.category.id(); |
838 } | 837 } |
839 } | 838 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
935 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 934 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
936 " }]" | 935 " }]" |
937 "}]}"; | 936 "}]}"; |
938 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 937 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
939 net::URLRequestStatus::SUCCESS); | 938 net::URLRequestStatus::SUCCESS); |
940 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; | 939 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
941 EXPECT_CALL(mock_callback(), Run(IsSuccess(), _)) | 940 EXPECT_CALL(mock_callback(), Run(IsSuccess(), _)) |
942 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); | 941 .WillOnce(MoveArgument1PointeeTo(&fetched_categories)); |
943 | 942 |
944 NTPSnippetsFetcher::Params params = test_params(); | 943 NTPSnippetsFetcher::Params params = test_params(); |
945 params.exclusive_category = base::Optional<Category>( | 944 params.exclusive_category = |
946 CategoryFactory().FromRemoteCategory(2)); | 945 base::Optional<Category>(Category::FromRemoteCategory(2)); |
947 snippets_fetcher().FetchSnippets( | 946 snippets_fetcher().FetchSnippets( |
948 params, ToSnippetsAvailableCallback(&mock_callback())); | 947 params, ToSnippetsAvailableCallback(&mock_callback())); |
949 FastForwardUntilNoTasksRemain(); | 948 FastForwardUntilNoTasksRemain(); |
950 | 949 |
951 ASSERT_TRUE(fetched_categories); | 950 ASSERT_TRUE(fetched_categories); |
952 ASSERT_THAT(fetched_categories->size(), Eq(1u)); | 951 ASSERT_THAT(fetched_categories->size(), Eq(1u)); |
953 const auto& category = (*fetched_categories)[0]; | 952 const auto& category = (*fetched_categories)[0]; |
954 EXPECT_THAT(category.category.id(), | 953 EXPECT_THAT(category.category.id(), Eq(Category::FromRemoteCategory(2).id())); |
955 Eq(CategoryFactory().FromRemoteCategory(2).id())); | |
956 ASSERT_THAT(category.snippets.size(), Eq(1u)); | 954 ASSERT_THAT(category.snippets.size(), Eq(1u)); |
957 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); | 955 EXPECT_THAT(category.snippets[0]->url().spec(), Eq("http://localhost/foo2")); |
958 } | 956 } |
959 | 957 |
960 TEST_F(ChromeReaderSnippetsFetcherTest, PersonalizesDependingOnVariations) { | 958 TEST_F(ChromeReaderSnippetsFetcherTest, PersonalizesDependingOnVariations) { |
961 // Default setting should be both personalization options. | 959 // Default setting should be both personalization options. |
962 EXPECT_THAT(snippets_fetcher().personalization(), | 960 EXPECT_THAT(snippets_fetcher().personalization(), |
963 Eq(NTPSnippetsFetcher::Personalization::kBoth)); | 961 Eq(NTPSnippetsFetcher::Personalization::kBoth)); |
964 | 962 |
965 SetVariationParam("fetching_personalization", "personal"); | 963 SetVariationParam("fetching_personalization", "personal"); |
(...skipping 228 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 |