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

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

Powered by Google App Engine
This is Rietveld 408576698