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 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 net::URLRequestStatus::SUCCESS); | 496 net::URLRequestStatus::SUCCESS); |
497 } | 497 } |
498 | 498 |
499 void LoadFromJSONString(NTPSnippetsService* service, | 499 void LoadFromJSONString(NTPSnippetsService* service, |
500 const std::string& json) { | 500 const std::string& json) { |
501 SetUpFetchResponse(json); | 501 SetUpFetchResponse(json); |
502 service->FetchSnippets(true); | 502 service->FetchSnippets(true); |
503 base::RunLoop().RunUntilIdle(); | 503 base::RunLoop().RunUntilIdle(); |
504 } | 504 } |
505 | 505 |
| 506 void LoadMoreFromJSONString( |
| 507 NTPSnippetsService* service, |
| 508 const std::string& json, |
| 509 NTPSnippetsService::FetchedMoreCallback callback) { |
| 510 SetUpFetchResponse(json); |
| 511 service->FetchMore(articles_category(), callback); |
| 512 base::RunLoop().RunUntilIdle(); |
| 513 } |
| 514 |
506 private: | 515 private: |
507 variations::testing::VariationParamsManager params_manager_; | 516 variations::testing::VariationParamsManager params_manager_; |
508 test::NTPSnippetsTestUtils utils_; | 517 test::NTPSnippetsTestUtils utils_; |
509 base::MessageLoop message_loop_; | 518 base::MessageLoop message_loop_; |
510 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 519 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
511 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 520 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
512 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 521 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
513 const GURL test_url_; | 522 const GURL test_url_; |
514 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 523 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
515 UserClassifier user_classifier_; | 524 UserClassifier user_classifier_; |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 834 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
826 ElementsAre(IdEq(first))); | 835 ElementsAre(IdEq(first))); |
827 | 836 |
828 std::string second("http://second"); | 837 std::string second("http://second"); |
829 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); | 838 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); |
830 // The snippets loaded last replace all that was loaded previously. | 839 // The snippets loaded last replace all that was loaded previously. |
831 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 840 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
832 ElementsAre(IdEq(second))); | 841 ElementsAre(IdEq(second))); |
833 } | 842 } |
834 | 843 |
| 844 TEST_F(NTPSnippetsServiceTest, LoadsAdditionalSnippets) { |
| 845 auto service = MakeSnippetsService(); |
| 846 |
| 847 std::string first("http://first"); |
| 848 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); |
| 849 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 850 ElementsAre(IdEq(first))); |
| 851 |
| 852 std::string second("http://second"); |
| 853 LoadMoreFromJSONString(service.get(), |
| 854 GetTestJson({GetSnippetWithUrl(second)}), |
| 855 base::Bind([](std::vector<ContentSuggestion>) {})); |
| 856 // The snippets loaded last are added to the previously loaded. |
| 857 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 858 ElementsAre(IdEq(first), IdEq(second))); |
| 859 } |
| 860 |
| 861 namespace { |
| 862 |
| 863 // Workaround for gMock's lack of support for movable types. |
| 864 void SuggestionsLoaded( |
| 865 MockFunction<void(const std::vector<ContentSuggestion>& v)>* loaded, |
| 866 std::vector<ContentSuggestion> v) { |
| 867 loaded->Call(v); |
| 868 } |
| 869 |
| 870 } // namespace |
| 871 |
| 872 TEST_F(NTPSnippetsServiceTest, InvokesOnlyCallbackOnFetchingMore) { |
| 873 auto service = MakeSnippetsService(); |
| 874 |
| 875 MockFunction<void(const std::vector<ContentSuggestion>&)> loaded; |
| 876 EXPECT_CALL(loaded, Call(SizeIs(1))); |
| 877 |
| 878 LoadMoreFromJSONString(service.get(), |
| 879 GetTestJson({GetSnippetWithUrl("http://some")}), |
| 880 base::Bind(&SuggestionsLoaded, &loaded)); |
| 881 |
| 882 // The observer shouldn't have been triggered. |
| 883 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), |
| 884 IsEmpty()); |
| 885 } |
| 886 |
835 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 887 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
836 auto service = MakeSnippetsService(); | 888 auto service = MakeSnippetsService(); |
837 | 889 |
838 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 890 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
839 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 891 EXPECT_THAT(service->snippets_fetcher()->last_status(), |
840 StartsWith("Received invalid JSON")); | 892 StartsWith("Received invalid JSON")); |
841 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 893 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
842 } | 894 } |
843 | 895 |
844 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 896 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))); | 1348 base::StringPrintf("http://localhost/snippet-id-%d", i))); |
1297 } | 1349 } |
1298 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | 1350 LoadFromJSONString(service.get(), GetTestJson(suggestions)); |
1299 // TODO(tschumann): We should probably trim out any additional results and | 1351 // TODO(tschumann): We should probably trim out any additional results and |
1300 // only serve the MaxSnippetCount items. | 1352 // only serve the MaxSnippetCount items. |
1301 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1353 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
1302 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | 1354 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); |
1303 } | 1355 } |
1304 | 1356 |
1305 } // namespace ntp_snippets | 1357 } // namespace ntp_snippets |
OLD | NEW |