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/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
21 #include "base/test/histogram_tester.h" | 21 #include "base/test/histogram_tester.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
24 #include "components/image_fetcher/image_decoder.h" | 24 #include "components/image_fetcher/image_decoder.h" |
25 #include "components/image_fetcher/image_fetcher.h" | 25 #include "components/image_fetcher/image_fetcher.h" |
| 26 #include "components/ntp_snippets/content_suggestions_category_factory.h" |
26 #include "components/ntp_snippets/ntp_snippet.h" | 27 #include "components/ntp_snippets/ntp_snippet.h" |
27 #include "components/ntp_snippets/ntp_snippets_database.h" | 28 #include "components/ntp_snippets/ntp_snippets_database.h" |
28 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 29 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
29 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 30 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
30 #include "components/ntp_snippets/ntp_snippets_test_utils.h" | 31 #include "components/ntp_snippets/ntp_snippets_test_utils.h" |
31 #include "components/ntp_snippets/switches.h" | 32 #include "components/ntp_snippets/switches.h" |
32 #include "components/prefs/testing_pref_service.h" | 33 #include "components/prefs/testing_pref_service.h" |
33 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 34 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
34 #include "components/signin/core/browser/fake_signin_manager.h" | 35 #include "components/signin/core/browser/fake_signin_manager.h" |
35 #include "google_apis/google_api_keys.h" | 36 #include "google_apis/google_api_keys.h" |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 319 |
319 fake_signin_manager()->SignIn("foo@bar.com"); | 320 fake_signin_manager()->SignIn("foo@bar.com"); |
320 snippets_fetcher->SetPersonalizationForTesting( | 321 snippets_fetcher->SetPersonalizationForTesting( |
321 NTPSnippetsFetcher::Personalization::kNonPersonal); | 322 NTPSnippetsFetcher::Personalization::kNonPersonal); |
322 | 323 |
323 // Add an initial fetch response, as the service tries to fetch when there | 324 // Add an initial fetch response, as the service tries to fetch when there |
324 // is nothing in the DB. | 325 // is nothing in the DB. |
325 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 326 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
326 | 327 |
327 service_.reset(new NTPSnippetsService( | 328 service_.reset(new NTPSnippetsService( |
328 enabled, pref_service(), nullptr, "fr", &scheduler_, | 329 enabled, pref_service(), nullptr, &category_factory_, "fr", &scheduler_, |
329 std::move(snippets_fetcher), /*image_fetcher=*/nullptr, | 330 std::move(snippets_fetcher), /*image_fetcher=*/nullptr, |
330 /*image_fetcher=*/nullptr, base::MakeUnique<NTPSnippetsDatabase>( | 331 /*image_fetcher=*/nullptr, base::MakeUnique<NTPSnippetsDatabase>( |
331 database_dir_.path(), task_runner), | 332 database_dir_.path(), task_runner), |
332 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), | 333 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), |
333 pref_service()))); | 334 pref_service()))); |
334 | 335 |
335 if (enabled) | 336 if (enabled) |
336 WaitForDBLoad(service_.get()); | 337 WaitForDBLoad(service_.get()); |
337 } | 338 } |
338 | 339 |
339 std::string MakeUniqueID(const std::string& within_category_id) { | 340 std::string MakeUniqueID(const std::string& within_category_id) { |
340 return NTPSnippetsService::MakeUniqueID( | 341 return service()->MakeUniqueID(category_factory_.FromKnownCategory( |
341 ContentSuggestionsCategory::ARTICLES, within_category_id); | 342 KnownSuggestionsCategories::ARTICLES), |
| 343 within_category_id); |
342 } | 344 } |
343 | 345 |
344 protected: | 346 protected: |
345 const GURL& test_url() { return test_url_; } | 347 const GURL& test_url() { return test_url_; } |
346 NTPSnippetsService* service() { return service_.get(); } | 348 NTPSnippetsService* service() { return service_.get(); } |
347 MockScheduler& mock_scheduler() { return scheduler_; } | 349 MockScheduler& mock_scheduler() { return scheduler_; } |
348 | 350 |
349 // Provide the json to be returned by the fake fetcher. | 351 // Provide the json to be returned by the fake fetcher. |
350 void SetUpFetchResponse(const std::string& json) { | 352 void SetUpFetchResponse(const std::string& json) { |
351 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 353 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
352 net::URLRequestStatus::SUCCESS); | 354 net::URLRequestStatus::SUCCESS); |
353 } | 355 } |
354 | 356 |
355 void LoadFromJSONString(const std::string& json) { | 357 void LoadFromJSONString(const std::string& json) { |
356 SetUpFetchResponse(json); | 358 SetUpFetchResponse(json); |
357 service()->FetchSnippets(true); | 359 service()->FetchSnippets(true); |
358 base::RunLoop().RunUntilIdle(); | 360 base::RunLoop().RunUntilIdle(); |
359 } | 361 } |
360 | 362 |
361 private: | 363 private: |
362 base::MessageLoop message_loop_; | 364 base::MessageLoop message_loop_; |
363 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 365 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
364 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 366 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
365 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 367 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
366 const GURL test_url_; | 368 const GURL test_url_; |
367 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 369 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
368 MockScheduler scheduler_; | 370 MockScheduler scheduler_; |
| 371 ContentSuggestionsCategoryFactory category_factory_; |
369 // Last so that the dependencies are deleted after the service. | 372 // Last so that the dependencies are deleted after the service. |
370 std::unique_ptr<NTPSnippetsService> service_; | 373 std::unique_ptr<NTPSnippetsService> service_; |
371 | 374 |
372 base::ScopedTempDir database_dir_; | 375 base::ScopedTempDir database_dir_; |
373 | 376 |
374 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 377 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
375 }; | 378 }; |
376 | 379 |
377 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { | 380 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { |
378 public: | 381 public: |
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 890 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
888 service()->OnDisabledReasonChanged(DisabledReason::NONE); | 891 service()->OnDisabledReasonChanged(DisabledReason::NONE); |
889 base::RunLoop().RunUntilIdle(); | 892 base::RunLoop().RunUntilIdle(); |
890 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 893 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
891 EXPECT_FALSE(service()->snippets().empty()); | 894 EXPECT_FALSE(service()->snippets().empty()); |
892 | 895 |
893 service()->RemoveObserver(&mock_observer); | 896 service()->RemoveObserver(&mock_observer); |
894 } | 897 } |
895 | 898 |
896 } // namespace ntp_snippets | 899 } // namespace ntp_snippets |
OLD | NEW |