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(NTPSnippetsService* service, |
| 507 const std::string& json) { |
| 508 SetUpFetchResponse(json); |
| 509 service->FetchMore(); |
| 510 base::RunLoop().RunUntilIdle(); |
| 511 } |
| 512 |
506 private: | 513 private: |
507 variations::testing::VariationParamsManager params_manager_; | 514 variations::testing::VariationParamsManager params_manager_; |
508 test::NTPSnippetsTestUtils utils_; | 515 test::NTPSnippetsTestUtils utils_; |
509 base::MessageLoop message_loop_; | 516 base::MessageLoop message_loop_; |
510 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 517 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
511 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 518 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
512 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 519 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
513 const GURL test_url_; | 520 const GURL test_url_; |
514 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 521 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
515 UserClassifier user_classifier_; | 522 UserClassifier user_classifier_; |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 832 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
826 ElementsAre(IdEq(first))); | 833 ElementsAre(IdEq(first))); |
827 | 834 |
828 std::string second("http://second"); | 835 std::string second("http://second"); |
829 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); | 836 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(second)})); |
830 // The snippets loaded last replace all that was loaded previously. | 837 // The snippets loaded last replace all that was loaded previously. |
831 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 838 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
832 ElementsAre(IdEq(second))); | 839 ElementsAre(IdEq(second))); |
833 } | 840 } |
834 | 841 |
| 842 TEST_F(NTPSnippetsServiceTest, LoadsAdditionalSnippets) { |
| 843 auto service = MakeSnippetsService(); |
| 844 |
| 845 std::string first("http://first"); |
| 846 LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)})); |
| 847 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 848 ElementsAre(IdEq(first))); |
| 849 |
| 850 std::string second("http://second"); |
| 851 LoadMoreFromJSONString(service.get(), |
| 852 GetTestJson({GetSnippetWithUrl(second)})); |
| 853 // The snippets loaded last are added to the previously loaded. |
| 854 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
| 855 ElementsAre(IdEq(first), IdEq(second))); |
| 856 } |
| 857 |
835 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 858 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
836 auto service = MakeSnippetsService(); | 859 auto service = MakeSnippetsService(); |
837 | 860 |
838 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 861 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
839 EXPECT_THAT(service->snippets_fetcher()->last_status(), | 862 EXPECT_THAT(service->snippets_fetcher()->last_status(), |
840 StartsWith("Received invalid JSON")); | 863 StartsWith("Received invalid JSON")); |
841 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 864 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
842 } | 865 } |
843 | 866 |
844 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 867 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))); | 1319 base::StringPrintf("http://localhost/snippet-id-%d", i))); |
1297 } | 1320 } |
1298 LoadFromJSONString(service.get(), GetTestJson(suggestions)); | 1321 LoadFromJSONString(service.get(), GetTestJson(suggestions)); |
1299 // TODO(tschumann): We should probably trim out any additional results and | 1322 // TODO(tschumann): We should probably trim out any additional results and |
1300 // only serve the MaxSnippetCount items. | 1323 // only serve the MaxSnippetCount items. |
1301 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), | 1324 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), |
1302 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); | 1325 SizeIs(service->GetMaxSnippetCountForTesting() + 1)); |
1303 } | 1326 } |
1304 | 1327 |
1305 } // namespace ntp_snippets | 1328 } // namespace ntp_snippets |
OLD | NEW |