| 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" |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 std::move(snippets_fetcher), /*image_fetcher=*/nullptr, | 327 std::move(snippets_fetcher), /*image_fetcher=*/nullptr, |
| 328 /*image_fetcher=*/nullptr, base::MakeUnique<NTPSnippetsDatabase>( | 328 /*image_fetcher=*/nullptr, base::MakeUnique<NTPSnippetsDatabase>( |
| 329 database_dir_.path(), task_runner), | 329 database_dir_.path(), task_runner), |
| 330 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), | 330 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), |
| 331 mock_sync_service()))); | 331 mock_sync_service()))); |
| 332 | 332 |
| 333 if (enabled) | 333 if (enabled) |
| 334 WaitForDBLoad(service_.get()); | 334 WaitForDBLoad(service_.get()); |
| 335 } | 335 } |
| 336 | 336 |
| 337 std::string MakeUniqueID(const std::string& within_category_id) { |
| 338 return NTPSnippetsService::MakeUniqueID( |
| 339 ContentSuggestionsCategory::ARTICLES, within_category_id); |
| 340 } |
| 341 |
| 337 protected: | 342 protected: |
| 338 const GURL& test_url() { return test_url_; } | 343 const GURL& test_url() { return test_url_; } |
| 339 NTPSnippetsService* service() { return service_.get(); } | 344 NTPSnippetsService* service() { return service_.get(); } |
| 340 MockScheduler& mock_scheduler() { return scheduler_; } | 345 MockScheduler& mock_scheduler() { return scheduler_; } |
| 341 | 346 |
| 342 // Provide the json to be returned by the fake fetcher. | 347 // Provide the json to be returned by the fake fetcher. |
| 343 void SetUpFetchResponse(const std::string& json) { | 348 void SetUpFetchResponse(const std::string& json) { |
| 344 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, | 349 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, |
| 345 net::URLRequestStatus::SUCCESS); | 350 net::URLRequestStatus::SUCCESS); |
| 346 } | 351 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 publishers.push_back(std::string("Source 1")); | 497 publishers.push_back(std::string("Source 1")); |
| 493 amp_urls.push_back(std::string()); | 498 amp_urls.push_back(std::string()); |
| 494 std::string json_str( | 499 std::string json_str( |
| 495 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 500 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 496 | 501 |
| 497 LoadFromJSONString(json_str); | 502 LoadFromJSONString(json_str); |
| 498 | 503 |
| 499 ASSERT_THAT(service()->snippets(), SizeIs(1)); | 504 ASSERT_THAT(service()->snippets(), SizeIs(1)); |
| 500 | 505 |
| 501 // Discarding a non-existent snippet shouldn't do anything. | 506 // Discarding a non-existent snippet shouldn't do anything. |
| 502 service()->DiscardSuggestion("http://othersite.com"); | 507 service()->DiscardSuggestion(MakeUniqueID("http://othersite.com")); |
| 503 EXPECT_THAT(service()->snippets(), SizeIs(1)); | 508 EXPECT_THAT(service()->snippets(), SizeIs(1)); |
| 504 | 509 |
| 505 // Discard the snippet. | 510 // Discard the snippet. |
| 506 service()->DiscardSuggestion(kSnippetUrl); | 511 service()->DiscardSuggestion(MakeUniqueID(kSnippetUrl)); |
| 507 EXPECT_THAT(service()->snippets(), IsEmpty()); | 512 EXPECT_THAT(service()->snippets(), IsEmpty()); |
| 508 | 513 |
| 509 // Make sure that fetching the same snippet again does not re-add it. | 514 // Make sure that fetching the same snippet again does not re-add it. |
| 510 LoadFromJSONString(json_str); | 515 LoadFromJSONString(json_str); |
| 511 EXPECT_THAT(service()->snippets(), IsEmpty()); | 516 EXPECT_THAT(service()->snippets(), IsEmpty()); |
| 512 | 517 |
| 513 // The snippet should stay discarded even after re-creating the service. | 518 // The snippet should stay discarded even after re-creating the service. |
| 514 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 519 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 515 CreateSnippetsService(/*enabled=*/true); | 520 CreateSnippetsService(/*enabled=*/true); |
| 516 LoadFromJSONString(json_str); | 521 LoadFromJSONString(json_str); |
| 517 EXPECT_THAT(service()->snippets(), IsEmpty()); | 522 EXPECT_THAT(service()->snippets(), IsEmpty()); |
| 518 | 523 |
| 519 // The snippet can be added again after clearing discarded snippets. | 524 // The snippet can be added again after clearing discarded snippets. |
| 520 service()->ClearDiscardedSuggestionsForDebugging(); | 525 service()->ClearDiscardedSuggestionsForDebugging(); |
| 521 EXPECT_THAT(service()->snippets(), IsEmpty()); | 526 EXPECT_THAT(service()->snippets(), IsEmpty()); |
| 522 LoadFromJSONString(json_str); | 527 LoadFromJSONString(json_str); |
| 523 EXPECT_THAT(service()->snippets(), SizeIs(1)); | 528 EXPECT_THAT(service()->snippets(), SizeIs(1)); |
| 524 } | 529 } |
| 525 | 530 |
| 526 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { | 531 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { |
| 527 LoadFromJSONString(GetTestJson({GetSnippet()})); | 532 LoadFromJSONString(GetTestJson({GetSnippet()})); |
| 528 | 533 |
| 529 service()->DiscardSuggestion(kSnippetUrl); | 534 service()->DiscardSuggestion(MakeUniqueID(kSnippetUrl)); |
| 530 const NTPSnippet::PtrVector& snippets = service()->discarded_snippets(); | 535 const NTPSnippet::PtrVector& snippets = service()->discarded_snippets(); |
| 531 EXPECT_EQ(1u, snippets.size()); | 536 EXPECT_EQ(1u, snippets.size()); |
| 532 for (auto& snippet : snippets) { | 537 for (auto& snippet : snippets) { |
| 533 EXPECT_EQ(kSnippetUrl, snippet->id()); | 538 EXPECT_EQ(kSnippetUrl, snippet->id()); |
| 534 } | 539 } |
| 535 | 540 |
| 536 // There should be no discarded snippet after clearing the list. | 541 // There should be no discarded snippet after clearing the list. |
| 537 service()->ClearDiscardedSuggestionsForDebugging(); | 542 service()->ClearDiscardedSuggestionsForDebugging(); |
| 538 EXPECT_EQ(0u, service()->discarded_snippets().size()); | 543 EXPECT_EQ(0u, service()->discarded_snippets().size()); |
| 539 } | 544 } |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 805 ElementsAre(base::Bucket(/*min=*/0, /*count=*/2), | 810 ElementsAre(base::Bucket(/*min=*/0, /*count=*/2), |
| 806 base::Bucket(/*min=*/1, /*count=*/2))); | 811 base::Bucket(/*min=*/1, /*count=*/2))); |
| 807 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | 812 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), |
| 808 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | 813 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
| 809 base::Bucket(/*min=*/1, /*count=*/2))); | 814 base::Bucket(/*min=*/1, /*count=*/2))); |
| 810 EXPECT_THAT( | 815 EXPECT_THAT( |
| 811 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 816 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
| 812 IsEmpty()); | 817 IsEmpty()); |
| 813 // Discarding a snippet should decrease the list size. This will only be | 818 // Discarding a snippet should decrease the list size. This will only be |
| 814 // logged after the next fetch. | 819 // logged after the next fetch. |
| 815 service()->DiscardSuggestion(kSnippetUrl); | 820 service()->DiscardSuggestion(MakeUniqueID(kSnippetUrl)); |
| 816 LoadFromJSONString(GetTestJson({GetSnippet()})); | 821 LoadFromJSONString(GetTestJson({GetSnippet()})); |
| 817 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), | 822 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticles"), |
| 818 ElementsAre(base::Bucket(/*min=*/0, /*count=*/3), | 823 ElementsAre(base::Bucket(/*min=*/0, /*count=*/3), |
| 819 base::Bucket(/*min=*/1, /*count=*/2))); | 824 base::Bucket(/*min=*/1, /*count=*/2))); |
| 820 // Discarded snippets shouldn't influence NumArticlesFetched. | 825 // Discarded snippets shouldn't influence NumArticlesFetched. |
| 821 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | 826 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), |
| 822 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | 827 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
| 823 base::Bucket(/*min=*/1, /*count=*/3))); | 828 base::Bucket(/*min=*/1, /*count=*/3))); |
| 824 EXPECT_THAT( | 829 EXPECT_THAT( |
| 825 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 830 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
| (...skipping 18 matching lines...) Expand all Loading... |
| 844 const std::vector<std::string> amp_urls = { | 849 const std::vector<std::string> amp_urls = { |
| 845 "http://mashable-amphtml.googleusercontent.com/1", | 850 "http://mashable-amphtml.googleusercontent.com/1", |
| 846 "http://t2.gstatic.com/images?q=tbn:3", | 851 "http://t2.gstatic.com/images?q=tbn:3", |
| 847 "http://t2.gstatic.com/images?q=tbn:3"}; | 852 "http://t2.gstatic.com/images?q=tbn:3"}; |
| 848 | 853 |
| 849 // Add the snippet from the mashable domain. | 854 // Add the snippet from the mashable domain. |
| 850 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 855 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
| 851 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); | 856 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); |
| 852 ASSERT_THAT(service()->snippets(), SizeIs(1)); | 857 ASSERT_THAT(service()->snippets(), SizeIs(1)); |
| 853 // Discard the snippet via the mashable source corpus ID. | 858 // Discard the snippet via the mashable source corpus ID. |
| 854 service()->DiscardSuggestion(source_urls[0]); | 859 service()->DiscardSuggestion(MakeUniqueID(source_urls[0])); |
| 855 EXPECT_THAT(service()->snippets(), IsEmpty()); | 860 EXPECT_THAT(service()->snippets(), IsEmpty()); |
| 856 | 861 |
| 857 // The same article from the AOL domain should now be detected as discarded. | 862 // The same article from the AOL domain should now be detected as discarded. |
| 858 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 863 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
| 859 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); | 864 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); |
| 860 ASSERT_THAT(service()->snippets(), IsEmpty()); | 865 ASSERT_THAT(service()->snippets(), IsEmpty()); |
| 861 } | 866 } |
| 862 | 867 |
| 863 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) { | 868 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) { |
| 864 MockServiceObserver mock_observer; | 869 MockServiceObserver mock_observer; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 880 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 885 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 881 service()->UpdateStateForStatus(DisabledReason::NONE); | 886 service()->UpdateStateForStatus(DisabledReason::NONE); |
| 882 base::RunLoop().RunUntilIdle(); | 887 base::RunLoop().RunUntilIdle(); |
| 883 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 888 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
| 884 EXPECT_FALSE(service()->snippets().empty()); | 889 EXPECT_FALSE(service()->snippets().empty()); |
| 885 | 890 |
| 886 service()->RemoveObserver(&mock_observer); | 891 service()->RemoveObserver(&mock_observer); |
| 887 } | 892 } |
| 888 | 893 |
| 889 } // namespace ntp_snippets | 894 } // namespace ntp_snippets |
| OLD | NEW |