| 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()), |
| 209 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), |
| 210 account_tracker_.get())), |
| 211 fake_token_service_(new FakeProfileOAuth2TokenService()) { |
| 203 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); | 212 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); |
| 204 // Since no SuggestionsService is injected in tests, we need to force the | 213 // Since no SuggestionsService is injected in tests, we need to force the |
| 205 // service to fetch from all hosts. | 214 // service to fetch from all hosts. |
| 206 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 215 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 207 switches::kDontRestrict); | 216 switches::kDontRestrict); |
| 208 } | 217 } |
| 209 | 218 |
| 210 ~NTPSnippetsServiceTest() override {} | 219 ~NTPSnippetsServiceTest() override {} |
| 211 | 220 |
| 212 void SetUp() override { | 221 void SetUp() override { |
| 213 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 222 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 214 CreateSnippetsService(/*enabled=*/true); | 223 CreateSnippetsService(/*enabled=*/true); |
| 215 } | 224 } |
| 216 | 225 |
| 217 void CreateSnippetsService(bool enabled) { | 226 void CreateSnippetsService(bool enabled) { |
| 218 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 227 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 219 base::ThreadTaskRunnerHandle::Get()); | 228 base::ThreadTaskRunnerHandle::Get()); |
| 220 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 229 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 221 new net::TestURLRequestContextGetter(task_runner.get()); | 230 new net::TestURLRequestContextGetter(task_runner.get()); |
| 222 | 231 |
| 223 service_.reset(new NTPSnippetsService( | 232 service_.reset(new NTPSnippetsService( |
| 224 pref_service_.get(), nullptr, task_runner, std::string("fr"), | 233 pref_service_.get(), nullptr, task_runner, std::string("fr"), |
| 225 &scheduler_, | 234 &scheduler_, |
| 226 base::WrapUnique(new NTPSnippetsFetcher( | 235 base::WrapUnique(new NTPSnippetsFetcher( |
| 236 fake_signin_manager_.get(), fake_token_service_.get(), |
| 227 std::move(request_context_getter), base::Bind(&ParseJson), | 237 std::move(request_context_getter), base::Bind(&ParseJson), |
| 228 /*is_stable_channel=*/true)), /*image_fetcher=*/nullptr)); | 238 /*is_stable_channel=*/true)), |
| 239 /*image_fetcher=*/nullptr)); |
| 229 service_->Init(enabled); | 240 service_->Init(enabled); |
| 230 } | 241 } |
| 231 | 242 |
| 232 protected: | 243 protected: |
| 233 const GURL& test_url() { return test_url_; } | 244 const GURL& test_url() { return test_url_; } |
| 234 NTPSnippetsService* service() { return service_.get(); } | 245 NTPSnippetsService* service() { return service_.get(); } |
| 235 MockScheduler& mock_scheduler() { return scheduler_; } | 246 MockScheduler& mock_scheduler() { return scheduler_; } |
| 236 | 247 |
| 237 void LoadFromJSONString(const std::string& json) { | 248 void LoadFromJSONString(const std::string& json) { |
| 238 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 249 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
| 239 net::URLRequestStatus::SUCCESS); | 250 net::URLRequestStatus::SUCCESS); |
| 240 service_->FetchSnippets(); | 251 service_->FetchSnippets(); |
| 241 message_loop_.RunUntilIdle(); | 252 message_loop_.RunUntilIdle(); |
| 242 } | 253 } |
| 243 | 254 |
| 244 private: | 255 private: |
| 245 base::MessageLoop message_loop_; | 256 base::MessageLoop message_loop_; |
| 246 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 257 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 247 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 258 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
| 248 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 259 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
| 249 const GURL test_url_; | 260 const GURL test_url_; |
| 250 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 261 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 262 std::unique_ptr<TestSigninClient> signin_client_; |
| 263 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 251 std::unique_ptr<NTPSnippetsService> service_; | 264 std::unique_ptr<NTPSnippetsService> service_; |
| 265 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 266 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 252 MockScheduler scheduler_; | 267 MockScheduler scheduler_; |
| 253 | 268 |
| 254 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 269 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
| 255 }; | 270 }; |
| 256 | 271 |
| 257 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { | 272 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { |
| 258 public: | 273 public: |
| 259 void SetUp() override { | 274 void SetUp() override { |
| 260 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); | 275 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); |
| 261 CreateSnippetsService(/*enabled=*/false); | 276 CreateSnippetsService(/*enabled=*/false); |
| (...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 EXPECT_THAT( | 791 EXPECT_THAT( |
| 777 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 792 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
| 778 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 793 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| 779 // Recreating the service and loading from prefs shouldn't count as fetched | 794 // Recreating the service and loading from prefs shouldn't count as fetched |
| 780 // articles. | 795 // articles. |
| 781 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 796 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 782 CreateSnippetsService(/*enabled=*/true); | 797 CreateSnippetsService(/*enabled=*/true); |
| 783 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); | 798 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); |
| 784 } | 799 } |
| 785 } // namespace ntp_snippets | 800 } // namespace ntp_snippets |
| OLD | NEW |