| 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 20 matching lines...) Expand all Loading... |
| 71 } | 70 } |
| 72 | 71 |
| 73 MATCHER_P(IsCategory, id, "") { | 72 MATCHER_P(IsCategory, id, "") { |
| 74 return arg.id() == static_cast<int>(id); | 73 return arg.id() == static_cast<int>(id); |
| 75 } | 74 } |
| 76 | 75 |
| 77 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; | 76 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; |
| 78 const char kTestContentSuggestionsServerEndpoint[] = | 77 const char kTestContentSuggestionsServerEndpoint[] = |
| 79 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | 78 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" |
| 80 "suggestions/fetch"; | 79 "suggestions/fetch"; |
| 81 const char kTestContentSuggestionsServerFormat[] = | 80 const char kAPIKey[] = "fakeAPIkey"; |
| 81 const char kTestContentSuggestionsServerWithAPIKey[] = |
| 82 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | 82 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" |
| 83 "suggestions/fetch?key=%s"; | 83 "suggestions/fetch?key=fakeAPIkey"; |
| 84 | 84 |
| 85 const char kSnippetUrl[] = "http://localhost/foobar"; | 85 const char kSnippetUrl[] = "http://localhost/foobar"; |
| 86 const char kSnippetTitle[] = "Title"; | 86 const char kSnippetTitle[] = "Title"; |
| 87 const char kSnippetText[] = "Snippet"; | 87 const char kSnippetText[] = "Snippet"; |
| 88 const char kSnippetSalientImage[] = "http://localhost/salient_image"; | 88 const char kSnippetSalientImage[] = "http://localhost/salient_image"; |
| 89 const char kSnippetPublisherName[] = "Foo News"; | 89 const char kSnippetPublisherName[] = "Foo News"; |
| 90 const char kSnippetAmpUrl[] = "http://localhost/amp"; | 90 const char kSnippetAmpUrl[] = "http://localhost/amp"; |
| 91 | 91 |
| 92 const char kSnippetUrl2[] = "http://foo.com/bar"; | 92 const char kSnippetUrl2[] = "http://foo.com/bar"; |
| 93 | 93 |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } // namespace | 341 } // namespace |
| 342 | 342 |
| 343 class NTPSnippetsServiceTest : public ::testing::Test { | 343 class NTPSnippetsServiceTest : public ::testing::Test { |
| 344 public: | 344 public: |
| 345 NTPSnippetsServiceTest() | 345 NTPSnippetsServiceTest() |
| 346 : params_manager_(ntp_snippets::kStudyName, | 346 : params_manager_(ntp_snippets::kStudyName, |
| 347 {{"content_suggestions_backend", | 347 {{"content_suggestions_backend", |
| 348 kTestContentSuggestionsServerEndpoint}}), | 348 kTestContentSuggestionsServerEndpoint}}), |
| 349 fake_url_fetcher_factory_( | 349 fake_url_fetcher_factory_( |
| 350 /*default_factory=*/&failing_url_fetcher_factory_), | 350 /*default_factory=*/&failing_url_fetcher_factory_), |
| 351 test_url_(base::StringPrintf(kTestContentSuggestionsServerFormat, | 351 test_url_(kTestContentSuggestionsServerWithAPIKey), |
| 352 google_apis::GetAPIKey().c_str())), | |
| 353 | 352 |
| 354 observer_(base::MakeUnique<FakeContentSuggestionsProviderObserver>()) { | 353 observer_(base::MakeUnique<FakeContentSuggestionsProviderObserver>()) { |
| 355 NTPSnippetsService::RegisterProfilePrefs(utils_.pref_service()->registry()); | 354 NTPSnippetsService::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 356 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | 355 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 357 | 356 |
| 358 // Since no SuggestionsService is injected in tests, we need to force the | 357 // Since no SuggestionsService is injected in tests, we need to force the |
| 359 // service to fetch from all hosts. | 358 // service to fetch from all hosts. |
| 360 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 359 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 361 switches::kDontRestrict); | 360 switches::kDontRestrict); |
| 362 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); | 361 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 375 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 374 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 376 base::ThreadTaskRunnerHandle::Get()); | 375 base::ThreadTaskRunnerHandle::Get()); |
| 377 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 376 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 378 new net::TestURLRequestContextGetter(task_runner.get()); | 377 new net::TestURLRequestContextGetter(task_runner.get()); |
| 379 | 378 |
| 380 utils_.ResetSigninManager(); | 379 utils_.ResetSigninManager(); |
| 381 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = | 380 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = |
| 382 base::MakeUnique<NTPSnippetsFetcher>( | 381 base::MakeUnique<NTPSnippetsFetcher>( |
| 383 utils_.fake_signin_manager(), fake_token_service_.get(), | 382 utils_.fake_signin_manager(), fake_token_service_.get(), |
| 384 std::move(request_context_getter), utils_.pref_service(), | 383 std::move(request_context_getter), utils_.pref_service(), |
| 385 &category_factory_, base::Bind(&ParseJson), | 384 &category_factory_, base::Bind(&ParseJson), kAPIKey); |
| 386 /*is_stable_channel=*/true); | |
| 387 | 385 |
| 388 utils_.fake_signin_manager()->SignIn("foo@bar.com"); | 386 utils_.fake_signin_manager()->SignIn("foo@bar.com"); |
| 389 snippets_fetcher->SetPersonalizationForTesting( | 387 snippets_fetcher->SetPersonalizationForTesting( |
| 390 NTPSnippetsFetcher::Personalization::kNonPersonal); | 388 NTPSnippetsFetcher::Personalization::kNonPersonal); |
| 391 | 389 |
| 392 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); | 390 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); |
| 393 image_fetcher_ = image_fetcher.get(); | 391 image_fetcher_ = image_fetcher.get(); |
| 394 | 392 |
| 395 // Add an initial fetch response, as the service tries to fetch when there | 393 // Add an initial fetch response, as the service tries to fetch when there |
| 396 // is nothing in the DB. | 394 // is nothing in the DB. |
| (...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 end = base::Time::FromTimeT(456); | 976 end = base::Time::FromTimeT(456); |
| 979 base::Callback<bool(const GURL& url)> filter; | 977 base::Callback<bool(const GURL& url)> filter; |
| 980 service->ClearHistory(begin, end, filter); | 978 service->ClearHistory(begin, end, filter); |
| 981 | 979 |
| 982 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 980 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 983 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 981 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
| 984 IsEmpty()); | 982 IsEmpty()); |
| 985 } | 983 } |
| 986 | 984 |
| 987 } // namespace ntp_snippets | 985 } // namespace ntp_snippets |
| OLD | NEW |