| 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 <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/test/histogram_tester.h" | 18 #include "base/test/histogram_tester.h" |
| 19 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "components/image_fetcher/image_fetcher.h" | 21 #include "components/image_fetcher/image_fetcher.h" |
| 22 #include "components/ntp_snippets/ntp_snippet.h" | 22 #include "components/ntp_snippets/ntp_snippet.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 25 #include "components/ntp_snippets/switches.h" | 25 #include "components/ntp_snippets/switches.h" |
| 26 #include "components/prefs/testing_pref_service.h" | 26 #include "components/prefs/testing_pref_service.h" |
| 27 #include "components/signin/core/browser/account_tracker_service.h" |
| 28 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 29 #include "components/signin/core/browser/fake_signin_manager.h" |
| 30 #include "components/signin/core/browser/test_signin_client.h" |
| 27 #include "google_apis/google_api_keys.h" | 31 #include "google_apis/google_api_keys.h" |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | 32 #include "net/url_request/test_url_fetcher_factory.h" |
| 29 #include "net/url_request/url_request_test_util.h" | 33 #include "net/url_request/url_request_test_util.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 36 |
| 33 using testing::ElementsAre; | 37 using testing::ElementsAre; |
| 34 using testing::IsEmpty; | 38 using testing::IsEmpty; |
| 35 using testing::StartsWith; | 39 using testing::StartsWith; |
| 36 using testing::_; | 40 using testing::_; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 196 |
| 193 } // namespace | 197 } // namespace |
| 194 | 198 |
| 195 class NTPSnippetsServiceTest : public testing::Test { | 199 class NTPSnippetsServiceTest : public testing::Test { |
| 196 public: | 200 public: |
| 197 NTPSnippetsServiceTest() | 201 NTPSnippetsServiceTest() |
| 198 : fake_url_fetcher_factory_( | 202 : fake_url_fetcher_factory_( |
| 199 /*default_factory=*/&failing_url_fetcher_factory_), | 203 /*default_factory=*/&failing_url_fetcher_factory_), |
| 200 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, | 204 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, |
| 201 google_apis::GetAPIKey().c_str())), | 205 google_apis::GetAPIKey().c_str())), |
| 202 pref_service_(new TestingPrefServiceSimple()) { | 206 pref_service_(new TestingPrefServiceSimple()), |
| 207 signin_client_(new TestSigninClient(nullptr)), |
| 208 account_tracker_(new AccountTrackerService()) { |
| 203 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); | 209 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); |
| 204 // Since no SuggestionsService is injected in tests, we need to force the | 210 // Since no SuggestionsService is injected in tests, we need to force the |
| 205 // service to fetch from all hosts. | 211 // service to fetch from all hosts. |
| 206 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 212 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 207 switches::kDontRestrict); | 213 switches::kDontRestrict); |
| 208 } | 214 } |
| 209 | 215 |
| 210 ~NTPSnippetsServiceTest() override {} | 216 ~NTPSnippetsServiceTest() override {} |
| 211 | 217 |
| 212 void SetUp() override { | 218 void SetUp() override { |
| 213 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 219 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 214 CreateSnippetsService(/*enabled=*/true); | 220 CreateSnippetsService(/*enabled=*/true); |
| 215 } | 221 } |
| 216 | 222 |
| 217 void CreateSnippetsService(bool enabled) { | 223 void CreateSnippetsService(bool enabled) { |
| 218 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 224 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 219 base::ThreadTaskRunnerHandle::Get()); | 225 base::ThreadTaskRunnerHandle::Get()); |
| 220 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 226 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 221 new net::TestURLRequestContextGetter(task_runner.get()); | 227 new net::TestURLRequestContextGetter(task_runner.get()); |
| 222 | 228 |
| 223 service_.reset(new NTPSnippetsService( | 229 service_.reset(new NTPSnippetsService( |
| 224 pref_service_.get(), nullptr, task_runner, std::string("fr"), | 230 pref_service_.get(), nullptr, task_runner, std::string("fr"), |
| 225 &scheduler_, | 231 &scheduler_, |
| 226 base::WrapUnique(new NTPSnippetsFetcher( | 232 base::WrapUnique(new NTPSnippetsFetcher( |
| 233 new FakeSigninManagerBase(signin_client_.get(), |
| 234 account_tracker_.get()), |
| 235 new FakeProfileOAuth2TokenService(), |
| 227 std::move(request_context_getter), base::Bind(&ParseJson), | 236 std::move(request_context_getter), base::Bind(&ParseJson), |
| 228 /*is_stable_channel=*/true)), /*image_fetcher=*/nullptr)); | 237 /*is_stable_channel=*/true)), /*image_fetcher=*/nullptr)); |
| 229 service_->Init(enabled); | 238 service_->Init(enabled); |
| 230 } | 239 } |
| 231 | 240 |
| 232 protected: | 241 protected: |
| 233 const GURL& test_url() { return test_url_; } | 242 const GURL& test_url() { return test_url_; } |
| 234 NTPSnippetsService* service() { return service_.get(); } | 243 NTPSnippetsService* service() { return service_.get(); } |
| 235 MockScheduler& mock_scheduler() { return scheduler_; } | 244 MockScheduler& mock_scheduler() { return scheduler_; } |
| 236 | 245 |
| (...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 EXPECT_THAT( | 785 EXPECT_THAT( |
| 777 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 786 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
| 778 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 787 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| 779 // Recreating the service and loading from prefs shouldn't count as fetched | 788 // Recreating the service and loading from prefs shouldn't count as fetched |
| 780 // articles. | 789 // articles. |
| 781 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 790 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 782 CreateSnippetsService(/*enabled=*/true); | 791 CreateSnippetsService(/*enabled=*/true); |
| 783 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); | 792 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); |
| 784 } | 793 } |
| 785 } // namespace ntp_snippets | 794 } // namespace ntp_snippets |
| OLD | NEW |