| 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 <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 public: | 273 public: |
| 274 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); | 274 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); |
| 275 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); | 275 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); |
| 276 MOCK_METHOD3( | 276 MOCK_METHOD3( |
| 277 StartOrQueueNetworkRequest, | 277 StartOrQueueNetworkRequest, |
| 278 void(const std::string&, | 278 void(const std::string&, |
| 279 const GURL&, | 279 const GURL&, |
| 280 base::Callback<void(const std::string&, const gfx::Image&)>)); | 280 base::Callback<void(const std::string&, const gfx::Image&)>)); |
| 281 }; | 281 }; |
| 282 | 282 |
| 283 class MockDismissedSuggestionsCallback |
| 284 : public ContentSuggestionsProvider::DismissedSuggestionsCallback { |
| 285 public: |
| 286 MOCK_METHOD2(MockRun, |
| 287 void(Category category, |
| 288 std::vector<ContentSuggestion>* dismissed_suggestions)); |
| 289 void Run(Category category, |
| 290 std::vector<ContentSuggestion> dismissed_suggestions) { |
| 291 MockRun(category, &dismissed_suggestions); |
| 292 } |
| 293 }; |
| 294 |
| 283 } // namespace | 295 } // namespace |
| 284 | 296 |
| 285 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { | 297 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { |
| 286 public: | 298 public: |
| 287 NTPSnippetsServiceTest() | 299 NTPSnippetsServiceTest() |
| 288 : fake_url_fetcher_factory_( | 300 : fake_url_fetcher_factory_( |
| 289 /*default_factory=*/&failing_url_fetcher_factory_), | 301 /*default_factory=*/&failing_url_fetcher_factory_), |
| 290 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, | 302 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, |
| 291 google_apis::GetAPIKey().c_str())) { | 303 google_apis::GetAPIKey().c_str())) { |
| 292 NTPSnippetsService::RegisterProfilePrefs(pref_service()->registry()); | 304 NTPSnippetsService::RegisterProfilePrefs(pref_service()->registry()); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 service()->ClearDismissedSuggestionsForDebugging(articles_category()); | 558 service()->ClearDismissedSuggestionsForDebugging(articles_category()); |
| 547 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 559 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); |
| 548 LoadFromJSONString(json_str); | 560 LoadFromJSONString(json_str); |
| 549 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 561 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
| 550 } | 562 } |
| 551 | 563 |
| 552 TEST_F(NTPSnippetsServiceTest, GetDismissed) { | 564 TEST_F(NTPSnippetsServiceTest, GetDismissed) { |
| 553 LoadFromJSONString(GetTestJson({GetSnippet()})); | 565 LoadFromJSONString(GetTestJson({GetSnippet()})); |
| 554 | 566 |
| 555 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); | 567 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); |
| 556 std::vector<ContentSuggestion> suggestions = | 568 |
| 557 service()->GetDismissedSuggestionsForDebugging(articles_category()); | 569 MockDismissedSuggestionsCallback callback; |
| 558 EXPECT_EQ(1u, suggestions.size()); | 570 |
| 559 for (auto& suggestion : suggestions) { | 571 EXPECT_CALL(callback, MockRun(_, _)) |
| 560 EXPECT_EQ(MakeUniqueID(kSnippetUrl), suggestion.id()); | 572 .WillOnce( |
| 561 } | 573 Invoke([this](Category category, |
| 574 std::vector<ContentSuggestion>* dismissed_suggestions) { |
| 575 EXPECT_EQ(1u, dismissed_suggestions->size()); |
| 576 for (auto& suggestion : *dismissed_suggestions) { |
| 577 EXPECT_EQ(MakeUniqueID(kSnippetUrl), suggestion.id()); |
| 578 } |
| 579 })); |
| 580 service()->GetDismissedSuggestionsForDebugging( |
| 581 articles_category(), |
| 582 base::Bind(&MockDismissedSuggestionsCallback::Run, |
| 583 base::Unretained(&callback), articles_category())); |
| 584 Mock::VerifyAndClearExpectations(&callback); |
| 562 | 585 |
| 563 // There should be no dismissed snippet after clearing the list. | 586 // There should be no dismissed snippet after clearing the list. |
| 587 EXPECT_CALL(callback, MockRun(_, _)) |
| 588 .WillOnce( |
| 589 Invoke([this](Category category, |
| 590 std::vector<ContentSuggestion>* dismissed_suggestions) { |
| 591 EXPECT_EQ(0u, dismissed_suggestions->size()); |
| 592 })); |
| 564 service()->ClearDismissedSuggestionsForDebugging(articles_category()); | 593 service()->ClearDismissedSuggestionsForDebugging(articles_category()); |
| 565 EXPECT_EQ(0u, service() | 594 service()->GetDismissedSuggestionsForDebugging( |
| 566 ->GetDismissedSuggestionsForDebugging(articles_category()) | 595 articles_category(), |
| 567 .size()); | 596 base::Bind(&MockDismissedSuggestionsCallback::Run, |
| 597 base::Unretained(&callback), articles_category())); |
| 568 } | 598 } |
| 569 | 599 |
| 570 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { | 600 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { |
| 571 std::string json_str(GetTestJson({GetSnippetWithTimes( | 601 std::string json_str(GetTestJson({GetSnippetWithTimes( |
| 572 "aaa1448459205", | 602 "aaa1448459205", |
| 573 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); | 603 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); |
| 574 | 604 |
| 575 LoadFromJSONString(json_str); | 605 LoadFromJSONString(json_str); |
| 576 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 606 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); |
| 577 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 607 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 946 MakeUniqueID(kSnippetUrl2), | 976 MakeUniqueID(kSnippetUrl2), |
| 947 base::Bind(&testing::MockFunction<void(const std::string&, | 977 base::Bind(&testing::MockFunction<void(const std::string&, |
| 948 const gfx::Image&)>::Call, | 978 const gfx::Image&)>::Call, |
| 949 base::Unretained(&image_fetched))); | 979 base::Unretained(&image_fetched))); |
| 950 | 980 |
| 951 base::RunLoop().RunUntilIdle(); | 981 base::RunLoop().RunUntilIdle(); |
| 952 EXPECT_TRUE(image.IsEmpty()); | 982 EXPECT_TRUE(image.IsEmpty()); |
| 953 } | 983 } |
| 954 | 984 |
| 955 } // namespace ntp_snippets | 985 } // namespace ntp_snippets |
| OLD | NEW |