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( | 506 void LoadMoreFromJSONString(NTPSnippetsService* service, |
507 NTPSnippetsService* service, | 507 const std::string& json, |
508 const std::string& json, | 508 std::set<std::string> known, |
dgn
2016/11/02 11:10:11
|known_ids| maybe? name is too unspecific as it is
fhorschig
2016/11/03 01:53:14
Done.
| |
509 NTPSnippetsService::FetchedMoreCallback callback) { | 509 NTPSnippetsService::FetchingCallback callback) { |
510 SetUpFetchResponse(json); | 510 SetUpFetchResponse(json); |
511 service->FetchMore(articles_category(), callback); | 511 service->Fetch(articles_category(), known, callback); |
512 base::RunLoop().RunUntilIdle(); | 512 base::RunLoop().RunUntilIdle(); |
513 } | 513 } |
514 | 514 |
515 private: | 515 private: |
516 variations::testing::VariationParamsManager params_manager_; | 516 variations::testing::VariationParamsManager params_manager_; |
517 test::NTPSnippetsTestUtils utils_; | 517 test::NTPSnippetsTestUtils utils_; |
518 base::MessageLoop message_loop_; | 518 base::MessageLoop message_loop_; |
519 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 519 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
520 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 520 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
521 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 521 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
845 auto service = MakeSnippetsService(); | 845 auto service = MakeSnippetsService(); |
846 | 846 |
847 std::string first("http://first"); | 847 std::string first("http://first"); |
848 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); | 848 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); |
849 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 849 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
850 ElementsAre(IdEq(first))); | 850 ElementsAre(IdEq(first))); |
851 | 851 |
852 std::string second("http://second"); | 852 std::string second("http://second"); |
853 LoadMoreFromJSONString(service.get(), | 853 LoadMoreFromJSONString(service.get(), |
854 GetTestJson({GetSnippetWithUrl(second)}), | 854 GetTestJson({GetSnippetWithUrl(second)}), |
855 std::set<std::string>(), | |
855 base::Bind([](std::vector<ContentSuggestion>) {})); | 856 base::Bind([](std::vector<ContentSuggestion>) {})); |
856 // The snippets loaded last are added to the previously loaded. | 857 // The snippets loaded last are added to the previously loaded. |
857 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 858 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
858 ElementsAre(IdEq(first), IdEq(second))); | 859 ElementsAre(IdEq(first), IdEq(second))); |
859 } | 860 } |
860 | 861 |
861 namespace { | 862 namespace { |
862 | 863 |
863 // Workaround for gMock's lack of support for movable types. | 864 // Workaround for gMock's lack of support for movable types. |
864 void SuggestionsLoaded( | 865 void SuggestionsLoaded( |
865 MockFunction<void(const std::vector<ContentSuggestion>& v)>* loaded, | 866 MockFunction<void(const std::vector<ContentSuggestion>& v)>* loaded, |
866 std::vector<ContentSuggestion> v) { | 867 std::vector<ContentSuggestion> v) { |
867 loaded->Call(v); | 868 loaded->Call(v); |
868 } | 869 } |
869 | 870 |
870 } // namespace | 871 } // namespace |
871 | 872 |
872 TEST_F(NTPSnippetsServiceTest, InvokesOnlyCallbackOnFetchingMore) { | 873 TEST_F(NTPSnippetsServiceTest, InvokesOnlyCallbackOnFetchingMore) { |
873 auto service = MakeSnippetsService(); | 874 auto service = MakeSnippetsService(); |
874 | 875 |
875 MockFunction<void(const std::vector<ContentSuggestion>&)> loaded; | 876 MockFunction<void(const std::vector<ContentSuggestion>&)> loaded; |
876 EXPECT_CALL(loaded, Call(SizeIs(1))); | 877 EXPECT_CALL(loaded, Call(SizeIs(1))); |
877 | 878 |
878 LoadMoreFromJSONString(service.get(), | 879 LoadMoreFromJSONString( |
879 GetTestJson({GetSnippetWithUrl("http://some")}), | 880 service.get(), GetTestJson({GetSnippetWithUrl("http://some")}), |
880 base::Bind(&SuggestionsLoaded, &loaded)); | 881 std::set<std::string>(), base::Bind(&SuggestionsLoaded, &loaded)); |
881 | 882 |
882 // The observer shouldn't have been triggered. | 883 // The observer shouldn't have been triggered. |
883 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), | 884 EXPECT_THAT(observer().SuggestionsForCategory(articles_category()), |
884 IsEmpty()); | 885 IsEmpty()); |
885 } | 886 } |
886 | 887 |
888 TEST_F(NTPSnippetsServiceTest, ReturnFetchRequestEmptyBeforeInit) { | |
889 auto service = MakeSnippetsServiceWithoutInitialization(); | |
890 MockFunction<void(const std::vector<ContentSuggestion>&)> loaded; | |
891 EXPECT_CALL(loaded, Call(SizeIs(0))); | |
892 service->Fetch(articles_category(), std::set<std::string>(), | |
893 base::Bind(&SuggestionsLoaded, &loaded)); | |
894 base::RunLoop().RunUntilIdle(); | |
895 } | |
896 | |
887 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 897 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
888 auto service = MakeSnippetsService(); | 898 auto service = MakeSnippetsService(); |
889 | 899 |
890 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 900 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
891 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 901 EXPECT_THAT(service->snippets_fetcher()->last_status(), |
892 StartsWith("Received invalid JSON")); | 902 StartsWith("Received invalid JSON")); |
893 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 903 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
894 } | 904 } |
895 | 905 |
896 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 906 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1348 base::StringPrintf("http://localhost/snippet-id-%d", i))); | 1358 base::StringPrintf("http://localhost/snippet-id-%d", i))); |
1349 } | 1359 } |
1350 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | 1360 LoadFromJSONString(service.get(), GetTestJson(suggestions)); |
1351 // TODO(tschumann): We should probably trim out any additional results and | 1361 // TODO(tschumann): We should probably trim out any additional results and |
1352 // only serve the MaxSnippetCount items. | 1362 // only serve the MaxSnippetCount items. |
1353 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1363 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
1354 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | 1364 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); |
1355 } | 1365 } |
1356 | 1366 |
1357 } // namespace ntp_snippets | 1367 } // namespace ntp_snippets |
OLD | NEW |