| 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/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "components/ntp_snippets/ntp_snippets_constants.h" | 30 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 31 #include "components/ntp_snippets/ntp_snippets_database.h" | 31 #include "components/ntp_snippets/ntp_snippets_database.h" |
| 32 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 32 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 33 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 33 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 34 #include "components/ntp_snippets/ntp_snippets_test_utils.h" | 34 #include "components/ntp_snippets/ntp_snippets_test_utils.h" |
| 35 #include "components/ntp_snippets/switches.h" | 35 #include "components/ntp_snippets/switches.h" |
| 36 #include "components/prefs/testing_pref_service.h" | 36 #include "components/prefs/testing_pref_service.h" |
| 37 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 37 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 38 #include "components/signin/core/browser/fake_signin_manager.h" | 38 #include "components/signin/core/browser/fake_signin_manager.h" |
| 39 #include "components/variations/variations_associated_data.h" | 39 #include "components/variations/variations_associated_data.h" |
| 40 #include "google_apis/google_api_keys.h" | |
| 41 #include "net/url_request/test_url_fetcher_factory.h" | 40 #include "net/url_request/test_url_fetcher_factory.h" |
| 42 #include "net/url_request/url_request_test_util.h" | 41 #include "net/url_request/url_request_test_util.h" |
| 43 #include "testing/gmock/include/gmock/gmock.h" | 42 #include "testing/gmock/include/gmock/gmock.h" |
| 44 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
| 45 #include "ui/gfx/image/image.h" | 44 #include "ui/gfx/image/image.h" |
| 46 #include "ui/gfx/image/image_unittest_util.h" | 45 #include "ui/gfx/image/image_unittest_util.h" |
| 47 | 46 |
| 48 using image_fetcher::ImageFetcher; | 47 using image_fetcher::ImageFetcher; |
| 49 using image_fetcher::ImageFetcherDelegate; | 48 using image_fetcher::ImageFetcherDelegate; |
| 50 using testing::ElementsAre; | 49 using testing::ElementsAre; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 67 namespace { | 66 namespace { |
| 68 | 67 |
| 69 MATCHER_P(IdEq, value, "") { | 68 MATCHER_P(IdEq, value, "") { |
| 70 return arg->id() == value; | 69 return arg->id() == value; |
| 71 } | 70 } |
| 72 | 71 |
| 73 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; | 72 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; |
| 74 const char kTestContentSuggestionsServerEndpoint[] = | 73 const char kTestContentSuggestionsServerEndpoint[] = |
| 75 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | 74 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" |
| 76 "suggestions/fetch"; | 75 "suggestions/fetch"; |
| 77 const char kTestContentSuggestionsServerFormat[] = | 76 const char kAPIKey[] = "fakeAPIkey"; |
| 77 const char kTestContentSuggestionsServerWithAPIKey[] = |
| 78 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | 78 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" |
| 79 "suggestions/fetch?key=%s"; | 79 "suggestions/fetch?key=fakeAPIkey"; |
| 80 | 80 |
| 81 const char kSnippetUrl[] = "http://localhost/foobar"; | 81 const char kSnippetUrl[] = "http://localhost/foobar"; |
| 82 const char kSnippetTitle[] = "Title"; | 82 const char kSnippetTitle[] = "Title"; |
| 83 const char kSnippetText[] = "Snippet"; | 83 const char kSnippetText[] = "Snippet"; |
| 84 const char kSnippetSalientImage[] = "http://localhost/salient_image"; | 84 const char kSnippetSalientImage[] = "http://localhost/salient_image"; |
| 85 const char kSnippetPublisherName[] = "Foo News"; | 85 const char kSnippetPublisherName[] = "Foo News"; |
| 86 const char kSnippetAmpUrl[] = "http://localhost/amp"; | 86 const char kSnippetAmpUrl[] = "http://localhost/amp"; |
| 87 | 87 |
| 88 const char kSnippetUrl2[] = "http://foo.com/bar"; | 88 const char kSnippetUrl2[] = "http://foo.com/bar"; |
| 89 | 89 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 } // namespace | 308 } // namespace |
| 309 | 309 |
| 310 class NTPSnippetsServiceTest : public ::testing::Test { | 310 class NTPSnippetsServiceTest : public ::testing::Test { |
| 311 public: | 311 public: |
| 312 NTPSnippetsServiceTest() | 312 NTPSnippetsServiceTest() |
| 313 : params_manager_(ntp_snippets::kStudyName, | 313 : params_manager_(ntp_snippets::kStudyName, |
| 314 {{"content_suggestions_backend", | 314 {{"content_suggestions_backend", |
| 315 kTestContentSuggestionsServerEndpoint}}), | 315 kTestContentSuggestionsServerEndpoint}}), |
| 316 fake_url_fetcher_factory_( | 316 fake_url_fetcher_factory_( |
| 317 /*default_factory=*/&failing_url_fetcher_factory_), | 317 /*default_factory=*/&failing_url_fetcher_factory_), |
| 318 test_url_(base::StringPrintf(kTestContentSuggestionsServerFormat, | 318 test_url_(kTestContentSuggestionsServerWithAPIKey), |
| 319 google_apis::GetAPIKey().c_str())), | |
| 320 | 319 |
| 321 observer_(base::MakeUnique<FakeContentSuggestionsProviderObserver>()) { | 320 observer_(base::MakeUnique<FakeContentSuggestionsProviderObserver>()) { |
| 322 NTPSnippetsService::RegisterProfilePrefs(utils_.pref_service()->registry()); | 321 NTPSnippetsService::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 323 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | 322 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 324 | 323 |
| 325 // Since no SuggestionsService is injected in tests, we need to force the | 324 // Since no SuggestionsService is injected in tests, we need to force the |
| 326 // service to fetch from all hosts. | 325 // service to fetch from all hosts. |
| 327 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 326 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 328 switches::kDontRestrict); | 327 switches::kDontRestrict); |
| 329 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); | 328 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 343 base::ThreadTaskRunnerHandle::Get()); | 342 base::ThreadTaskRunnerHandle::Get()); |
| 344 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 343 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 345 new net::TestURLRequestContextGetter(task_runner.get()); | 344 new net::TestURLRequestContextGetter(task_runner.get()); |
| 346 | 345 |
| 347 utils_.ResetSigninManager(); | 346 utils_.ResetSigninManager(); |
| 348 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = | 347 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = |
| 349 base::MakeUnique<NTPSnippetsFetcher>( | 348 base::MakeUnique<NTPSnippetsFetcher>( |
| 350 utils_.fake_signin_manager(), fake_token_service_.get(), | 349 utils_.fake_signin_manager(), fake_token_service_.get(), |
| 351 std::move(request_context_getter), utils_.pref_service(), | 350 std::move(request_context_getter), utils_.pref_service(), |
| 352 &category_factory_, base::Bind(&ParseJson), | 351 &category_factory_, base::Bind(&ParseJson), |
| 353 /*is_stable_channel=*/true); | 352 kAPIKey); |
| 354 | 353 |
| 355 utils_.fake_signin_manager()->SignIn("foo@bar.com"); | 354 utils_.fake_signin_manager()->SignIn("foo@bar.com"); |
| 356 snippets_fetcher->SetPersonalizationForTesting( | 355 snippets_fetcher->SetPersonalizationForTesting( |
| 357 NTPSnippetsFetcher::Personalization::kNonPersonal); | 356 NTPSnippetsFetcher::Personalization::kNonPersonal); |
| 358 | 357 |
| 359 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); | 358 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); |
| 360 image_fetcher_ = image_fetcher.get(); | 359 image_fetcher_ = image_fetcher.get(); |
| 361 | 360 |
| 362 // Add an initial fetch response, as the service tries to fetch when there | 361 // Add an initial fetch response, as the service tries to fetch when there |
| 363 // is nothing in the DB. | 362 // is nothing in the DB. |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 service->FetchSuggestionImage( | 858 service->FetchSuggestionImage( |
| 860 MakeUniqueID(*service, kSnippetUrl2), | 859 MakeUniqueID(*service, kSnippetUrl2), |
| 861 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, | 860 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, |
| 862 base::Unretained(&image_fetched))); | 861 base::Unretained(&image_fetched))); |
| 863 | 862 |
| 864 base::RunLoop().RunUntilIdle(); | 863 base::RunLoop().RunUntilIdle(); |
| 865 EXPECT_TRUE(image.IsEmpty()); | 864 EXPECT_TRUE(image.IsEmpty()); |
| 866 } | 865 } |
| 867 | 866 |
| 868 } // namespace ntp_snippets | 867 } // namespace ntp_snippets |
| OLD | NEW |