Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_provider_unittest.cc

Issue 2558393004: Refactoring: Moves image fetching and caching into CachedImageFetcher (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/remote_suggestions_provider.h" 5 #include "components/ntp_snippets/remote/remote_suggestions_provider.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 912 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 Status status, std::vector<ContentSuggestion> suggestions) { 923 Status status, std::vector<ContentSuggestion> suggestions) {
924 EXPECT_THAT(suggestions, SizeIs(1)); 924 EXPECT_THAT(suggestions, SizeIs(1));
925 EXPECT_THAT(suggestions[0].id().id_within_category(), Eq("http://second")); 925 EXPECT_THAT(suggestions[0].id().id_within_category(), Eq("http://second"));
926 }); 926 });
927 LoadMoreFromJSONString(service.get(), articles_category(), 927 LoadMoreFromJSONString(service.get(), articles_category(),
928 GetTestJson({GetSnippetWithUrl("http://second")}), 928 GetTestJson({GetSnippetWithUrl("http://second")}),
929 /*known_ids=*/std::set<std::string>(), 929 /*known_ids=*/std::set<std::string>(),
930 expect_only_second_suggestion_received); 930 expect_only_second_suggestion_received);
931 931
932 // Verify we can resolve the image of the new snippets. 932 // Verify we can resolve the image of the new snippets.
933 ServeImageCallback cb = base::Bind(&ServeOneByOneImage, service.get()); 933 ServeImageCallback cb =
934 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
934 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 935 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _))
935 .Times(2) 936 .Times(2)
936 .WillRepeatedly(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); 937 .WillRepeatedly(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run)));
937 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 938 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
938 gfx::Image image = FetchImage(service.get(), MakeArticleID("http://first")); 939 gfx::Image image = FetchImage(service.get(), MakeArticleID("http://first"));
939 EXPECT_FALSE(image.IsEmpty()); 940 EXPECT_FALSE(image.IsEmpty());
940 EXPECT_EQ(1, image.Width()); 941 EXPECT_EQ(1, image.Width());
941 942
942 image = FetchImage(service.get(), MakeArticleID("http://second")); 943 image = FetchImage(service.get(), MakeArticleID("http://second"));
943 EXPECT_FALSE(image.IsEmpty()); 944 EXPECT_FALSE(image.IsEmpty());
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 TEST_F(RemoteSuggestionsProviderTest, Dismiss) { 1160 TEST_F(RemoteSuggestionsProviderTest, Dismiss) {
1160 auto service = MakeSnippetsService(); 1161 auto service = MakeSnippetsService();
1161 1162
1162 std::string json_str( 1163 std::string json_str(
1163 GetTestJson({GetSnippetWithSources("http://site.com", "Source 1", "")})); 1164 GetTestJson({GetSnippetWithSources("http://site.com", "Source 1", "")}));
1164 1165
1165 LoadFromJSONString(service.get(), json_str); 1166 LoadFromJSONString(service.get(), json_str);
1166 1167
1167 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); 1168 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
1168 // Load the image to store it in the database. 1169 // Load the image to store it in the database.
1169 ServeImageCallback cb = base::Bind(&ServeOneByOneImage, service.get()); 1170 ServeImageCallback cb =
1171 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1170 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1172 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _))
1171 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); 1173 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run)));
1172 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1174 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1173 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); 1175 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl));
1174 EXPECT_FALSE(image.IsEmpty()); 1176 EXPECT_FALSE(image.IsEmpty());
1175 EXPECT_EQ(1, image.Width()); 1177 EXPECT_EQ(1, image.Width());
1176 1178
1177 // Dismissing a non-existent snippet shouldn't do anything. 1179 // Dismissing a non-existent snippet shouldn't do anything.
1178 service->DismissSuggestion(MakeArticleID("http://othersite.com")); 1180 service->DismissSuggestion(MakeArticleID("http://othersite.com"));
1179 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); 1181 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1256 1258
1257 TEST_F(RemoteSuggestionsProviderTest, RemoveExpiredDismissedContent) { 1259 TEST_F(RemoteSuggestionsProviderTest, RemoveExpiredDismissedContent) {
1258 auto service = MakeSnippetsService(); 1260 auto service = MakeSnippetsService();
1259 1261
1260 std::string json_str1(GetTestJson({GetExpiredSnippet()})); 1262 std::string json_str1(GetTestJson({GetExpiredSnippet()}));
1261 // Load it. 1263 // Load it.
1262 LoadFromJSONString(service.get(), json_str1); 1264 LoadFromJSONString(service.get(), json_str1);
1263 // Load the image to store it in the database. 1265 // Load the image to store it in the database.
1264 // TODO(tschumann): Introduce some abstraction to nicely work with image 1266 // TODO(tschumann): Introduce some abstraction to nicely work with image
1265 // fetching expectations. 1267 // fetching expectations.
1266 ServeImageCallback cb = base::Bind(&ServeOneByOneImage, service.get()); 1268 ServeImageCallback cb =
1269 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1267 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1270 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _))
1268 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); 1271 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run)));
1269 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1272 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1270 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); 1273 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl));
1271 EXPECT_FALSE(image.IsEmpty()); 1274 EXPECT_FALSE(image.IsEmpty());
1272 EXPECT_EQ(1, image.Width()); 1275 EXPECT_EQ(1, image.Width());
1273 1276
1274 // Dismiss the suggestion 1277 // Dismiss the suggestion
1275 service->DismissSuggestion( 1278 service->DismissSuggestion(
1276 ContentSuggestion::ID(articles_category(), kSnippetUrl)); 1279 ContentSuggestion::ID(articles_category(), kSnippetUrl));
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
1468 EXPECT_FALSE(service->GetSnippetsForTesting(articles_category()).empty()); 1471 EXPECT_FALSE(service->GetSnippetsForTesting(articles_category()).empty());
1469 } 1472 }
1470 1473
1471 TEST_F(RemoteSuggestionsProviderTest, ImageReturnedWithTheSameId) { 1474 TEST_F(RemoteSuggestionsProviderTest, ImageReturnedWithTheSameId) {
1472 auto service = MakeSnippetsService(); 1475 auto service = MakeSnippetsService();
1473 1476
1474 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); 1477 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()}));
1475 1478
1476 gfx::Image image; 1479 gfx::Image image;
1477 MockFunction<void(const gfx::Image&)> image_fetched; 1480 MockFunction<void(const gfx::Image&)> image_fetched;
1478 ServeImageCallback cb = base::Bind(&ServeOneByOneImage, service.get()); 1481 ServeImageCallback cb =
1482 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1479 { 1483 {
1480 InSequence s; 1484 InSequence s;
1481 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1485 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _))
1482 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); 1486 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run)));
1483 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); 1487 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image));
1484 } 1488 }
1485 1489
1486 service->FetchSuggestionImage( 1490 service->FetchSuggestionImage(
1487 MakeArticleID(kSnippetUrl), 1491 MakeArticleID(kSnippetUrl),
1488 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, 1492 base::Bind(&MockFunction<void(const gfx::Image&)>::Call,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1550 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, 1554 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT,
1551 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); 1555 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN);
1552 base::RunLoop().RunUntilIdle(); 1556 base::RunLoop().RunUntilIdle();
1553 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2)); 1557 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(2));
1554 } 1558 }
1555 1559
1556 TEST_F(RemoteSuggestionsProviderTest, ShouldClearOrphanedImagesOnRestart) { 1560 TEST_F(RemoteSuggestionsProviderTest, ShouldClearOrphanedImagesOnRestart) {
1557 auto service = MakeSnippetsService(); 1561 auto service = MakeSnippetsService();
1558 1562
1559 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); 1563 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()}));
1560 ServeImageCallback cb = base::Bind(&ServeOneByOneImage, service.get()); 1564 ServeImageCallback cb =
1565 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1561 1566
1562 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1567 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _))
1563 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run))); 1568 .WillOnce(WithArgs<0, 2>(Invoke(&cb, &ServeImageCallback::Run)));
1564 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1569 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1565 1570
1566 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl)); 1571 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSnippetUrl));
1567 EXPECT_EQ(1, image.Width()); 1572 EXPECT_EQ(1, image.Width());
1568 EXPECT_FALSE(image.IsEmpty()); 1573 EXPECT_FALSE(image.IsEmpty());
1569 1574
1570 // Send new suggestion which don't include the snippet referencing the image. 1575 // Send new suggestion which don't include the snippet referencing the image.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1621 service->FetchSnippetsInTheBackground(); 1626 service->FetchSnippetsInTheBackground();
1622 base::RunLoop().RunUntilIdle(); 1627 base::RunLoop().RunUntilIdle();
1623 EXPECT_EQ( 1628 EXPECT_EQ(
1624 simple_test_tick_clock_ptr->NowTicks().ToInternalValue(), 1629 simple_test_tick_clock_ptr->NowTicks().ToInternalValue(),
1625 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); 1630 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime));
1626 // TODO(markusheintz): Add a test that simulates a browser restart once the 1631 // TODO(markusheintz): Add a test that simulates a browser restart once the
1627 // scheduler refactoring is done (crbug.com/672434). 1632 // scheduler refactoring is done (crbug.com/672434).
1628 } 1633 }
1629 1634
1630 } // namespace ntp_snippets 1635 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698