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/remote/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/remote/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 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 public: | 381 public: |
382 NTPSnippetsServiceTest() | 382 NTPSnippetsServiceTest() |
383 : params_manager_(ntp_snippets::kStudyName, | 383 : params_manager_(ntp_snippets::kStudyName, |
384 {{"content_suggestions_backend", | 384 {{"content_suggestions_backend", |
385 kTestContentSuggestionsServerEndpoint}}), | 385 kTestContentSuggestionsServerEndpoint}}), |
386 fake_url_fetcher_factory_( | 386 fake_url_fetcher_factory_( |
387 /*default_factory=*/&failing_url_fetcher_factory_), | 387 /*default_factory=*/&failing_url_fetcher_factory_), |
388 test_url_(kTestContentSuggestionsServerWithAPIKey), | 388 test_url_(kTestContentSuggestionsServerWithAPIKey), |
389 user_classifier_(/*pref_service=*/nullptr), | 389 user_classifier_(/*pref_service=*/nullptr), |
390 image_fetcher_(nullptr), | 390 image_fetcher_(nullptr), |
391 image_decoder_(nullptr) { | 391 image_decoder_(nullptr), |
| 392 caller_() { |
392 NTPSnippetsService::RegisterProfilePrefs(utils_.pref_service()->registry()); | 393 NTPSnippetsService::RegisterProfilePrefs(utils_.pref_service()->registry()); |
393 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | 394 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); |
394 | 395 |
395 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); | 396 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); |
396 } | 397 } |
397 | 398 |
398 ~NTPSnippetsServiceTest() override { | 399 ~NTPSnippetsServiceTest() override { |
399 // We need to run the message loop after deleting the database, because | 400 // We need to run the message loop after deleting the database, because |
400 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task | 401 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task |
401 // runner. Without this, we'd get reports of memory leaks. | 402 // runner. Without this, we'd get reports of memory leaks. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 net::URLRequestStatus::SUCCESS); | 497 net::URLRequestStatus::SUCCESS); |
497 } | 498 } |
498 | 499 |
499 void LoadFromJSONString(NTPSnippetsService* service, | 500 void LoadFromJSONString(NTPSnippetsService* service, |
500 const std::string& json) { | 501 const std::string& json) { |
501 SetUpFetchResponse(json); | 502 SetUpFetchResponse(json); |
502 service->FetchSnippets(true); | 503 service->FetchSnippets(true); |
503 base::RunLoop().RunUntilIdle(); | 504 base::RunLoop().RunUntilIdle(); |
504 } | 505 } |
505 | 506 |
| 507 void LoadMoreFromJSONString( |
| 508 NTPSnippetsService* service, |
| 509 const std::string& json, |
| 510 const NTPSnippetsService::FetchedMoreCallback& callback) { |
| 511 SetUpFetchResponse(json); |
| 512 service->FetchMore(articles_category(), callback); |
| 513 base::RunLoop().RunUntilIdle(); |
| 514 } |
| 515 |
| 516 NTPSnippetsService::FetchedMoreCallback MakeDummyCallback() { |
| 517 return base::Bind(&MockCaller::EmptyCallback, |
| 518 base::Unretained(&this->caller_)); |
| 519 } |
| 520 |
| 521 NTPSnippetsService::FetchedMoreCallback MakeMustCallCallback() { |
| 522 EXPECT_CALL(this->caller_, MustCallback()).Times(1); |
| 523 return base::Bind(&MockCaller::CheckCallback, |
| 524 base::Unretained(&this->caller_)); |
| 525 } |
| 526 |
506 private: | 527 private: |
| 528 class MockCaller { |
| 529 public: |
| 530 void EmptyCallback(std::vector<ContentSuggestion>) {} |
| 531 void CheckCallback(std::vector<ContentSuggestion> v) { MustCallback(); } |
| 532 MOCK_METHOD0(MustCallback, void()); |
| 533 }; |
| 534 |
507 variations::testing::VariationParamsManager params_manager_; | 535 variations::testing::VariationParamsManager params_manager_; |
508 test::NTPSnippetsTestUtils utils_; | 536 test::NTPSnippetsTestUtils utils_; |
509 base::MessageLoop message_loop_; | 537 base::MessageLoop message_loop_; |
510 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 538 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
511 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 539 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
512 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 540 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
513 const GURL test_url_; | 541 const GURL test_url_; |
514 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 542 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
515 UserClassifier user_classifier_; | 543 UserClassifier user_classifier_; |
516 NiceMock<MockScheduler> scheduler_; | 544 NiceMock<MockScheduler> scheduler_; |
517 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; | 545 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; |
518 CategoryFactory category_factory_; | 546 CategoryFactory category_factory_; |
519 NiceMock<MockImageFetcher>* image_fetcher_; | 547 NiceMock<MockImageFetcher>* image_fetcher_; |
520 FakeImageDecoder* image_decoder_; | 548 FakeImageDecoder* image_decoder_; |
521 | 549 |
522 base::ScopedTempDir database_dir_; | 550 base::ScopedTempDir database_dir_; |
| 551 MockCaller caller_; |
523 | 552 |
524 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 553 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
525 }; | 554 }; |
526 | 555 |
527 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { | 556 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { |
528 // We should get two |Schedule| calls: The first when initialization | 557 // We should get two |Schedule| calls: The first when initialization |
529 // completes, the second one after the automatic (since the service doesn't | 558 // completes, the second one after the automatic (since the service doesn't |
530 // have any data yet) fetch finishes. | 559 // have any data yet) fetch finishes. |
531 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); | 560 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); |
532 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | 561 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 854 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
826 ElementsAre(IdEq(first))); | 855 ElementsAre(IdEq(first))); |
827 | 856 |
828 std::string second("http://second"); | 857 std::string second("http://second"); |
829 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); | 858 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); |
830 // The snippets loaded last replace all that was loaded previously. | 859 // The snippets loaded last replace all that was loaded previously. |
831 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 860 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
832 ElementsAre(IdEq(second))); | 861 ElementsAre(IdEq(second))); |
833 } | 862 } |
834 | 863 |
| 864 TEST_F(NTPSnippetsServiceTest, LoadsAdditionalSnippets) { |
| 865 auto service = MakeSnippetsService(); |
| 866 |
| 867 std::string first("http://first"); |
| 868 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); |
| 869 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 870 ElementsAre(IdEq(first))); |
| 871 |
| 872 std::string second("http://second"); |
| 873 LoadMoreFromJSONString(service.get(), |
| 874 GetTestJson({GetSnippetWithUrl(second)}), |
| 875 MakeDummyCallback()); |
| 876 // The snippets loaded last are added to the previously loaded. |
| 877 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 878 ElementsAre(IdEq(first), IdEq(second))); |
| 879 } |
| 880 |
| 881 TEST_F(NTPSnippetsServiceTest, InvokesOnlyCallbackOnFetchingMore) { |
| 882 LoadMoreFromJSONString(MakeSnippetsService().get(), |
| 883 GetTestJson({GetSnippetWithUrl("http://some")}), |
| 884 MakeMustCallCallback()); |
| 885 |
| 886 // The observer shouldn't have been triggered. |
| 887 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 888 SizeIs(0)); |
| 889 } |
| 890 |
835 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 891 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
836 auto service = MakeSnippetsService(); | 892 auto service = MakeSnippetsService(); |
837 | 893 |
838 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 894 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
839 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 895 EXPECT_THAT(service->snippets_fetcher()->last_status(), |
840 StartsWith("Received invalid JSON")); | 896 StartsWith("Received invalid JSON")); |
841 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 897 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
842 } | 898 } |
843 | 899 |
844 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 900 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1296 base::StringPrintf("http://localhost/snippet-id-%d", i))); | 1352 base::StringPrintf("http://localhost/snippet-id-%d", i))); |
1297 } | 1353 } |
1298 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | 1354 LoadFromJSONString(service.get(), GetTestJson(suggestions)); |
1299 // TODO(tschumann): We should probably trim out any additional results and | 1355 // TODO(tschumann): We should probably trim out any additional results and |
1300 // only serve the MaxSnippetCount items. | 1356 // only serve the MaxSnippetCount items. |
1301 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1357 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
1302 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | 1358 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); |
1303 } | 1359 } |
1304 | 1360 |
1305 } // namespace ntp_snippets | 1361 } // namespace ntp_snippets |
OLD | NEW |