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 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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, "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>( | 333 base::MakeUnique<NTPSnippetsStatusService>( |
333 fake_signin_manager(), mock_sync_service(), pref_service()))); | 334 fake_signin_manager(), mock_sync_service(), pref_service()), |
| 335 &category_factory_)); |
334 | 336 |
335 if (enabled) | 337 if (enabled) |
336 WaitForDBLoad(service_.get()); | 338 WaitForDBLoad(service_.get()); |
337 } | 339 } |
338 | 340 |
339 std::string MakeUniqueID(const std::string& within_category_id) { | 341 std::string MakeUniqueID(const std::string& within_category_id) { |
340 return NTPSnippetsService::MakeUniqueID( | 342 return service()->MakeUniqueID(category_factory_.FromKnownCategory( |
341 ContentSuggestionsCategory::ARTICLES, within_category_id); | 343 KnownSuggestionsCategories::ARTICLES), |
| 344 within_category_id); |
342 } | 345 } |
343 | 346 |
344 protected: | 347 protected: |
345 const GURL& test_url() { return test_url_; } | 348 const GURL& test_url() { return test_url_; } |
346 NTPSnippetsService* service() { return service_.get(); } | 349 NTPSnippetsService* service() { return service_.get(); } |
347 MockScheduler& mock_scheduler() { return scheduler_; } | 350 MockScheduler& mock_scheduler() { return scheduler_; } |
348 | 351 |
349 // Provide the json to be returned by the fake fetcher. | 352 // Provide the json to be returned by the fake fetcher. |
350 void SetUpFetchResponse(const std::string& json) { | 353 void SetUpFetchResponse(const std::string& json) { |
351 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 354 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
352 net::URLRequestStatus::SUCCESS); | 355 net::URLRequestStatus::SUCCESS); |
353 } | 356 } |
354 | 357 |
355 void LoadFromJSONString(const std::string& json) { | 358 void LoadFromJSONString(const std::string& json) { |
356 SetUpFetchResponse(json); | 359 SetUpFetchResponse(json); |
357 service()->FetchSnippets(true); | 360 service()->FetchSnippets(true); |
358 base::RunLoop().RunUntilIdle(); | 361 base::RunLoop().RunUntilIdle(); |
359 } | 362 } |
360 | 363 |
361 private: | 364 private: |
362 base::MessageLoop message_loop_; | 365 base::MessageLoop message_loop_; |
363 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 366 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
364 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 367 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
365 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 368 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
366 const GURL test_url_; | 369 const GURL test_url_; |
367 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 370 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
368 MockScheduler scheduler_; | 371 MockScheduler scheduler_; |
| 372 ContentSuggestionsCategoryFactory category_factory_; |
369 // Last so that the dependencies are deleted after the service. | 373 // Last so that the dependencies are deleted after the service. |
370 std::unique_ptr<NTPSnippetsService> service_; | 374 std::unique_ptr<NTPSnippetsService> service_; |
371 | 375 |
372 base::ScopedTempDir database_dir_; | 376 base::ScopedTempDir database_dir_; |
373 | 377 |
374 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 378 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
375 }; | 379 }; |
376 | 380 |
377 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { | 381 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { |
378 public: | 382 public: |
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 891 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
888 service()->OnDisabledReasonChanged(DisabledReason::NONE); | 892 service()->OnDisabledReasonChanged(DisabledReason::NONE); |
889 base::RunLoop().RunUntilIdle(); | 893 base::RunLoop().RunUntilIdle(); |
890 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 894 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
891 EXPECT_FALSE(service()->snippets().empty()); | 895 EXPECT_FALSE(service()->snippets().empty()); |
892 | 896 |
893 service()->RemoveObserver(&mock_observer); | 897 service()->RemoveObserver(&mock_observer); |
894 } | 898 } |
895 | 899 |
896 } // namespace ntp_snippets | 900 } // namespace ntp_snippets |
OLD | NEW |