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

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

Issue 2794343002: Network traffic annotation added to image_data_fetcher. (Closed)
Patch Set: Comments addressed. Created 3 years, 7 months 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_impl.h" 5 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "components/ntp_snippets/remote/remote_suggestion.h" 42 #include "components/ntp_snippets/remote/remote_suggestion.h"
43 #include "components/ntp_snippets/remote/remote_suggestions_database.h" 43 #include "components/ntp_snippets/remote/remote_suggestions_database.h"
44 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" 44 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h"
45 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" 45 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h"
46 #include "components/ntp_snippets/remote/test_utils.h" 46 #include "components/ntp_snippets/remote/test_utils.h"
47 #include "components/ntp_snippets/user_classifier.h" 47 #include "components/ntp_snippets/user_classifier.h"
48 #include "components/prefs/testing_pref_service.h" 48 #include "components/prefs/testing_pref_service.h"
49 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 49 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
50 #include "components/signin/core/browser/fake_signin_manager.h" 50 #include "components/signin/core/browser/fake_signin_manager.h"
51 #include "components/variations/variations_params_manager.h" 51 #include "components/variations/variations_params_manager.h"
52 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
52 #include "net/url_request/test_url_fetcher_factory.h" 53 #include "net/url_request/test_url_fetcher_factory.h"
53 #include "net/url_request/url_request_test_util.h" 54 #include "net/url_request/url_request_test_util.h"
54 #include "testing/gmock/include/gmock/gmock.h" 55 #include "testing/gmock/include/gmock/gmock.h"
55 #include "testing/gmock_mutant.h" 56 #include "testing/gmock_mutant.h"
56 #include "testing/gtest/include/gtest/gtest.h" 57 #include "testing/gtest/include/gtest/gtest.h"
57 #include "ui/gfx/geometry/size.h" 58 #include "ui/gfx/geometry/size.h"
58 #include "ui/gfx/image/image.h" 59 #include "ui/gfx/image/image.h"
59 #include "ui/gfx/image/image_unittest_util.h" 60 #include "ui/gfx/image/image_unittest_util.h"
60 61
61 using image_fetcher::ImageFetcher; 62 using image_fetcher::ImageFetcher;
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 } 345 }
345 }; 346 };
346 347
347 class MockImageFetcher : public ImageFetcher { 348 class MockImageFetcher : public ImageFetcher {
348 public: 349 public:
349 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); 350 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*));
350 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); 351 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName));
351 MOCK_METHOD1(SetImageDownloadLimit, 352 MOCK_METHOD1(SetImageDownloadLimit,
352 void(base::Optional<int64_t> max_download_bytes)); 353 void(base::Optional<int64_t> max_download_bytes));
353 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size&)); 354 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size&));
354 MOCK_METHOD3(StartOrQueueNetworkRequest, 355 MOCK_METHOD4(StartOrQueueNetworkRequest,
355 void(const std::string&, 356 void(const std::string&,
356 const GURL&, 357 const GURL&,
357 const ImageFetcherCallback&)); 358 const ImageFetcherCallback&,
359 const net::NetworkTrafficAnnotationTag&));
358 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*()); 360 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*());
359 }; 361 };
360 362
361 class FakeImageDecoder : public image_fetcher::ImageDecoder { 363 class FakeImageDecoder : public image_fetcher::ImageDecoder {
362 public: 364 public:
363 FakeImageDecoder() {} 365 FakeImageDecoder() {}
364 ~FakeImageDecoder() override = default; 366 ~FakeImageDecoder() override = default;
365 void DecodeImage( 367 void DecodeImage(
366 const std::string& image_data, 368 const std::string& image_data,
367 const gfx::Size& desired_image_frame_size, 369 const gfx::Size& desired_image_frame_size,
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after
995 Eq("http://second")); 997 Eq("http://second"));
996 }); 998 });
997 LoadMoreFromJSONString(service.get(), articles_category(), 999 LoadMoreFromJSONString(service.get(), articles_category(),
998 GetTestJson({GetSuggestionWithUrl("http://second")}), 1000 GetTestJson({GetSuggestionWithUrl("http://second")}),
999 /*known_ids=*/std::set<std::string>(), 1001 /*known_ids=*/std::set<std::string>(),
1000 expect_only_second_suggestion_received); 1002 expect_only_second_suggestion_received);
1001 1003
1002 // Verify we can resolve the image of the new suggestions. 1004 // Verify we can resolve the image of the new suggestions.
1003 ServeImageCallback cb = 1005 ServeImageCallback cb =
1004 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); 1006 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1005 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1007 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _, _))
1006 .Times(2) 1008 .Times(2)
1007 .WillRepeatedly(WithArgs<0, 2>(Invoke(CreateFunctor(cb)))); 1009 .WillRepeatedly(WithArgs<0, 2>(Invoke(CreateFunctor(cb))));
1008 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1010 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1009 gfx::Image image = FetchImage(service.get(), MakeArticleID("http://first")); 1011 gfx::Image image = FetchImage(service.get(), MakeArticleID("http://first"));
1010 EXPECT_FALSE(image.IsEmpty()); 1012 EXPECT_FALSE(image.IsEmpty());
1011 EXPECT_EQ(1, image.Width()); 1013 EXPECT_EQ(1, image.Width());
1012 1014
1013 image = FetchImage(service.get(), MakeArticleID("http://second")); 1015 image = FetchImage(service.get(), MakeArticleID("http://second"));
1014 EXPECT_FALSE(image.IsEmpty()); 1016 EXPECT_FALSE(image.IsEmpty());
1015 EXPECT_EQ(1, image.Width()); 1017 EXPECT_EQ(1, image.Width());
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1204 LoadFromJSONString(service.get(), 1206 LoadFromJSONString(service.get(),
1205 GetTestJson({GetSuggestionWithUrl("http://id-1"), 1207 GetTestJson({GetSuggestionWithUrl("http://id-1"),
1206 GetSuggestionWithUrl("http://id-2")})); 1208 GetSuggestionWithUrl("http://id-2")}));
1207 LoadFromJSONString(service.get(), 1209 LoadFromJSONString(service.get(),
1208 GetTestJson({GetSuggestionWithUrl("http://new-id-1"), 1210 GetTestJson({GetSuggestionWithUrl("http://new-id-1"),
1209 GetSuggestionWithUrl("http://new-id-2")})); 1211 GetSuggestionWithUrl("http://new-id-2")}));
1210 // Make sure images of both batches are available. This is to sanity check our 1212 // Make sure images of both batches are available. This is to sanity check our
1211 // assumptions for the test are right. 1213 // assumptions for the test are right.
1212 ServeImageCallback cb = 1214 ServeImageCallback cb =
1213 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); 1215 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1214 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1216 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _, _))
1215 .Times(2) 1217 .Times(2)
1216 .WillRepeatedly(WithArgs<0, 2>(Invoke(CreateFunctor(cb)))); 1218 .WillRepeatedly(WithArgs<0, 2>(Invoke(CreateFunctor(cb))));
1217 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1219 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1218 gfx::Image image = FetchImage(service.get(), MakeArticleID("http://id-1")); 1220 gfx::Image image = FetchImage(service.get(), MakeArticleID("http://id-1"));
1219 ASSERT_FALSE(image.IsEmpty()); 1221 ASSERT_FALSE(image.IsEmpty());
1220 ASSERT_EQ(1, image.Width()); 1222 ASSERT_EQ(1, image.Width());
1221 image = FetchImage(service.get(), MakeArticleID("http://new-id-1")); 1223 image = FetchImage(service.get(), MakeArticleID("http://new-id-1"));
1222 ASSERT_FALSE(image.IsEmpty()); 1224 ASSERT_FALSE(image.IsEmpty());
1223 ASSERT_EQ(1, image.Width()); 1225 ASSERT_EQ(1, image.Width());
1224 1226
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1369 std::string json_str(GetTestJson( 1371 std::string json_str(GetTestJson(
1370 {GetSuggestionWithSources("http://site.com", "Source 1", "")})); 1372 {GetSuggestionWithSources("http://site.com", "Source 1", "")}));
1371 1373
1372 LoadFromJSONString(service.get(), json_str); 1374 LoadFromJSONString(service.get(), json_str);
1373 1375
1374 ASSERT_THAT(service->GetSuggestionsForTesting(articles_category()), 1376 ASSERT_THAT(service->GetSuggestionsForTesting(articles_category()),
1375 SizeIs(1)); 1377 SizeIs(1));
1376 // Load the image to store it in the database. 1378 // Load the image to store it in the database.
1377 ServeImageCallback cb = 1379 ServeImageCallback cb =
1378 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); 1380 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1379 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1381 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _, _))
1380 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb)))); 1382 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb))));
1381 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1383 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1382 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSuggestionUrl)); 1384 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSuggestionUrl));
1383 EXPECT_FALSE(image.IsEmpty()); 1385 EXPECT_FALSE(image.IsEmpty());
1384 EXPECT_EQ(1, image.Width()); 1386 EXPECT_EQ(1, image.Width());
1385 1387
1386 // Dismissing a non-existent suggestion shouldn't do anything. 1388 // Dismissing a non-existent suggestion shouldn't do anything.
1387 service->DismissSuggestion(MakeArticleID("http://othersite.com")); 1389 service->DismissSuggestion(MakeArticleID("http://othersite.com"));
1388 EXPECT_THAT(service->GetSuggestionsForTesting(articles_category()), 1390 EXPECT_THAT(service->GetSuggestionsForTesting(articles_category()),
1389 SizeIs(1)); 1391 SizeIs(1));
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1474 auto service = MakeSuggestionsProvider(); 1476 auto service = MakeSuggestionsProvider();
1475 1477
1476 std::string json_str1(GetTestJson({GetExpiredSuggestion()})); 1478 std::string json_str1(GetTestJson({GetExpiredSuggestion()}));
1477 // Load it. 1479 // Load it.
1478 LoadFromJSONString(service.get(), json_str1); 1480 LoadFromJSONString(service.get(), json_str1);
1479 // Load the image to store it in the database. 1481 // Load the image to store it in the database.
1480 // TODO(tschumann): Introduce some abstraction to nicely work with image 1482 // TODO(tschumann): Introduce some abstraction to nicely work with image
1481 // fetching expectations. 1483 // fetching expectations.
1482 ServeImageCallback cb = 1484 ServeImageCallback cb =
1483 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); 1485 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1484 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1486 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _, _))
1485 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb)))); 1487 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb))));
1486 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1488 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1487 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSuggestionUrl)); 1489 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSuggestionUrl));
1488 EXPECT_FALSE(image.IsEmpty()); 1490 EXPECT_FALSE(image.IsEmpty());
1489 EXPECT_EQ(1, image.Width()); 1491 EXPECT_EQ(1, image.Width());
1490 1492
1491 // Dismiss the suggestion 1493 // Dismiss the suggestion
1492 service->DismissSuggestion( 1494 service->DismissSuggestion(
1493 ContentSuggestion::ID(articles_category(), kSuggestionUrl)); 1495 ContentSuggestion::ID(articles_category(), kSuggestionUrl));
1494 1496
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
1648 auto service = MakeSuggestionsProvider(); 1650 auto service = MakeSuggestionsProvider();
1649 1651
1650 LoadFromJSONString(service.get(), GetTestJson({GetSuggestion()})); 1652 LoadFromJSONString(service.get(), GetTestJson({GetSuggestion()}));
1651 1653
1652 gfx::Image image; 1654 gfx::Image image;
1653 MockFunction<void(const gfx::Image&)> image_fetched; 1655 MockFunction<void(const gfx::Image&)> image_fetched;
1654 ServeImageCallback cb = 1656 ServeImageCallback cb =
1655 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); 1657 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1656 { 1658 {
1657 InSequence s; 1659 InSequence s;
1658 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1660 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _, _))
1659 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb)))); 1661 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb))));
1660 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image)); 1662 EXPECT_CALL(image_fetched, Call(_)).WillOnce(SaveArg<0>(&image));
1661 } 1663 }
1662 1664
1663 service->FetchSuggestionImage( 1665 service->FetchSuggestionImage(
1664 MakeArticleID(kSuggestionUrl), 1666 MakeArticleID(kSuggestionUrl),
1665 base::Bind(&MockFunction<void(const gfx::Image&)>::Call, 1667 base::Bind(&MockFunction<void(const gfx::Image&)>::Call,
1666 base::Unretained(&image_fetched))); 1668 base::Unretained(&image_fetched)));
1667 base::RunLoop().RunUntilIdle(); 1669 base::RunLoop().RunUntilIdle();
1668 // Check that the image by ServeOneByOneImage is really served. 1670 // Check that the image by ServeOneByOneImage is really served.
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1755 Eq(CategoryStatus::AVAILABLE)); 1757 Eq(CategoryStatus::AVAILABLE));
1756 } 1758 }
1757 1759
1758 TEST_F(RemoteSuggestionsProviderImplTest, ShouldClearOrphanedImagesOnRestart) { 1760 TEST_F(RemoteSuggestionsProviderImplTest, ShouldClearOrphanedImagesOnRestart) {
1759 auto service = MakeSuggestionsProvider(); 1761 auto service = MakeSuggestionsProvider();
1760 1762
1761 LoadFromJSONString(service.get(), GetTestJson({GetSuggestion()})); 1763 LoadFromJSONString(service.get(), GetTestJson({GetSuggestion()}));
1762 ServeImageCallback cb = 1764 ServeImageCallback cb =
1763 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting()); 1765 base::Bind(&ServeOneByOneImage, &service->GetImageFetcherForTesting());
1764 1766
1765 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) 1767 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _, _))
1766 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb)))); 1768 .WillOnce(WithArgs<0, 2>(Invoke(CreateFunctor(cb))));
1767 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1)); 1769 image_decoder()->SetDecodedImage(gfx::test::CreateImage(1, 1));
1768 1770
1769 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSuggestionUrl)); 1771 gfx::Image image = FetchImage(service.get(), MakeArticleID(kSuggestionUrl));
1770 EXPECT_EQ(1, image.Width()); 1772 EXPECT_EQ(1, image.Width());
1771 EXPECT_FALSE(image.IsEmpty()); 1773 EXPECT_FALSE(image.IsEmpty());
1772 1774
1773 // Send new suggestion which don't include the suggestion referencing the 1775 // Send new suggestion which don't include the suggestion referencing the
1774 // image. 1776 // image.
1775 LoadFromJSONString(service.get(), 1777 LoadFromJSONString(service.get(),
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 WaitForSuggestionsProviderInitialization(service.get(), 1908 WaitForSuggestionsProviderInitialization(service.get(),
1907 /*set_empty_response=*/true); 1909 /*set_empty_response=*/true);
1908 1910
1909 // The scheduler should be notified of clearing the history. 1911 // The scheduler should be notified of clearing the history.
1910 EXPECT_CALL(*scheduler(), OnSuggestionsCleared()); 1912 EXPECT_CALL(*scheduler(), OnSuggestionsCleared());
1911 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT, 1913 service->OnStatusChanged(RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT,
1912 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN); 1914 RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN);
1913 } 1915 }
1914 1916
1915 } // namespace ntp_snippets 1917 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/remote_suggestions_provider_impl.cc ('k') | components/ntp_tiles/icon_cacher_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698