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

Side by Side Diff: components/favicon/core/large_icon_service_unittest.cc

Issue 2794343002: Network traffic annotation added to image_data_fetcher. (Closed)
Patch Set: Android and iOS files added. 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/favicon/core/large_icon_service.h" 5 #include "components/favicon/core/large_icon_service.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 101
102 class MockImageFetcher : public image_fetcher::ImageFetcher { 102 class MockImageFetcher : public image_fetcher::ImageFetcher {
103 public: 103 public:
104 MOCK_METHOD1(SetImageFetcherDelegate, 104 MOCK_METHOD1(SetImageFetcherDelegate,
105 void(image_fetcher::ImageFetcherDelegate* delegate)); 105 void(image_fetcher::ImageFetcherDelegate* delegate));
106 MOCK_METHOD1(SetDataUseServiceName, 106 MOCK_METHOD1(SetDataUseServiceName,
107 void(image_fetcher::ImageFetcher::DataUseServiceName name)); 107 void(image_fetcher::ImageFetcher::DataUseServiceName name));
108 MOCK_METHOD1(SetImageDownloadLimit, 108 MOCK_METHOD1(SetImageDownloadLimit,
109 void(base::Optional<int64_t> max_download_bytes)); 109 void(base::Optional<int64_t> max_download_bytes));
110 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size& size)); 110 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size& size));
111 MOCK_METHOD3(StartOrQueueNetworkRequest, 111 MOCK_METHOD4(StartOrQueueNetworkRequest,
112 void(const std::string&, 112 void(const std::string&,
113 const GURL&, 113 const GURL&,
114 const ImageFetcherCallback&)); 114 const ImageFetcherCallback&,
115 const net::NetworkTrafficAnnotationTag&));
115 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*()); 116 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*());
116 }; 117 };
117 118
118 class LargeIconServiceTest : public testing::Test { 119 class LargeIconServiceTest : public testing::Test {
119 public: 120 public:
120 LargeIconServiceTest() 121 LargeIconServiceTest()
121 : mock_image_fetcher_(new NiceMock<MockImageFetcher>()), 122 : mock_image_fetcher_(new NiceMock<MockImageFetcher>()),
122 large_icon_service_(&mock_favicon_service_, 123 large_icon_service_(&mock_favicon_service_,
123 base::ThreadTaskRunnerHandle::Get(), 124 base::ThreadTaskRunnerHandle::Get(),
124 base::WrapUnique(mock_image_fetcher_)) {} 125 base::WrapUnique(mock_image_fetcher_)) {}
(...skipping 15 matching lines...) Expand all
140 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) { 141 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServer) {
141 const GURL kExpectedServerUrl( 142 const GURL kExpectedServerUrl(
142 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 143 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
143 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL" 144 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL"
144 "&url=http://www.example.com/"); 145 "&url=http://www.example.com/");
145 146
146 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); 147 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
147 148
148 base::MockCallback<base::Callback<void(bool success)>> callback; 149 base::MockCallback<base::Callback<void(bool success)>> callback;
149 EXPECT_CALL(*mock_image_fetcher_, 150 EXPECT_CALL(*mock_image_fetcher_,
150 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 151 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
151 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( 152 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
152 CreateTestSkBitmap(64, 64, kTestColor)))); 153 CreateTestSkBitmap(64, 64, kTestColor))));
153 EXPECT_CALL(mock_favicon_service_, 154 EXPECT_CALL(mock_favicon_service_,
154 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, 155 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl,
155 favicon_base::IconType::TOUCH_ICON, _, _)) 156 favicon_base::IconType::TOUCH_ICON, _, _))
156 .WillOnce(PostBoolReply(true)); 157 .WillOnce(PostBoolReply(true));
157 158
158 large_icon_service_ 159 large_icon_service_
159 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 160 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
160 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); 161 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get());
161 162
162 EXPECT_CALL(callback, Run(true)); 163 EXPECT_CALL(callback, Run(true));
163 base::RunLoop().RunUntilIdle(); 164 base::RunLoop().RunUntilIdle();
164 histogram_tester_.ExpectUniqueSample( 165 histogram_tester_.ExpectUniqueSample(
165 "Favicons.LargeIconService.DownloadedSize", 64, /*expected_count=*/1); 166 "Favicons.LargeIconService.DownloadedSize", 64, /*expected_count=*/1);
166 } 167 }
167 168
168 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithCustomUrl) { 169 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithCustomUrl) {
169 variations::testing::VariationParamsManager variation_params( 170 variations::testing::VariationParamsManager variation_params(
170 "LargeIconServiceFetching", 171 "LargeIconServiceFetching",
171 {{"request_format", "https://t0.gstatic.com/faviconV2?size=%d&url=%s"}}, 172 {{"request_format", "https://t0.gstatic.com/faviconV2?size=%d&url=%s"}},
172 {"LargeIconServiceFetching"}); 173 {"LargeIconServiceFetching"});
173 const GURL kExpectedServerUrl( 174 const GURL kExpectedServerUrl(
174 "https://t0.gstatic.com/faviconV2?size=42&url=http://www.example.com/"); 175 "https://t0.gstatic.com/faviconV2?size=42&url=http://www.example.com/");
175 176
176 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); 177 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
177 178
178 base::MockCallback<base::Callback<void(bool success)>> callback; 179 base::MockCallback<base::Callback<void(bool success)>> callback;
179 EXPECT_CALL(*mock_image_fetcher_, 180 EXPECT_CALL(*mock_image_fetcher_,
180 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 181 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
181 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( 182 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
182 CreateTestSkBitmap(64, 64, kTestColor)))); 183 CreateTestSkBitmap(64, 64, kTestColor))));
183 EXPECT_CALL(mock_favicon_service_, 184 EXPECT_CALL(mock_favicon_service_,
184 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl, 185 SetLastResortFavicons(GURL(kDummyUrl), kExpectedServerUrl,
185 favicon_base::IconType::TOUCH_ICON, _, _)) 186 favicon_base::IconType::TOUCH_ICON, _, _))
186 .WillOnce(PostBoolReply(true)); 187 .WillOnce(PostBoolReply(true));
187 188
188 large_icon_service_ 189 large_icon_service_
189 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 190 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
190 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); 191 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get());
191 192
192 EXPECT_CALL(callback, Run(true)); 193 EXPECT_CALL(callback, Run(true));
193 base::RunLoop().RunUntilIdle(); 194 base::RunLoop().RunUntilIdle();
194 } 195 }
195 196
196 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithOriginalUrl) { 197 TEST_F(LargeIconServiceTest, ShouldGetFromGoogleServerWithOriginalUrl) {
197 const GURL kExpectedServerUrl( 198 const GURL kExpectedServerUrl(
198 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 199 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
199 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL" 200 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL"
200 "&url=http://www.example.com/"); 201 "&url=http://www.example.com/");
201 const GURL kExpectedOriginalUrl("http://www.example.com/favicon.png"); 202 const GURL kExpectedOriginalUrl("http://www.example.com/favicon.png");
202 203
203 image_fetcher::RequestMetadata expected_metadata; 204 image_fetcher::RequestMetadata expected_metadata;
204 expected_metadata.content_location_header = kExpectedOriginalUrl.spec(); 205 expected_metadata.content_location_header = kExpectedOriginalUrl.spec();
205 EXPECT_CALL(*mock_image_fetcher_, 206 EXPECT_CALL(*mock_image_fetcher_,
206 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 207 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
207 .WillOnce(PostFetchReplyWithMetadata( 208 .WillOnce(PostFetchReplyWithMetadata(
208 gfx::Image::CreateFrom1xBitmap( 209 gfx::Image::CreateFrom1xBitmap(
209 CreateTestSkBitmap(64, 64, kTestColor)), 210 CreateTestSkBitmap(64, 64, kTestColor)),
210 expected_metadata)); 211 expected_metadata));
211 EXPECT_CALL(mock_favicon_service_, 212 EXPECT_CALL(mock_favicon_service_,
212 SetLastResortFavicons(GURL(kDummyUrl), kExpectedOriginalUrl, 213 SetLastResortFavicons(GURL(kDummyUrl), kExpectedOriginalUrl,
213 favicon_base::IconType::TOUCH_ICON, _, _)) 214 favicon_base::IconType::TOUCH_ICON, _, _))
214 .WillOnce(PostBoolReply(true)); 215 .WillOnce(PostBoolReply(true));
215 216
216 base::MockCallback<base::Callback<void(bool success)>> callback; 217 base::MockCallback<base::Callback<void(bool success)>> callback;
217 large_icon_service_ 218 large_icon_service_
218 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 219 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
219 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); 220 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get());
220 221
221 EXPECT_CALL(callback, Run(true)); 222 EXPECT_CALL(callback, Run(true));
222 base::RunLoop().RunUntilIdle(); 223 base::RunLoop().RunUntilIdle();
223 } 224 }
224 225
225 TEST_F(LargeIconServiceTest, ShouldTrimQueryParametersForGoogleServer) { 226 TEST_F(LargeIconServiceTest, ShouldTrimQueryParametersForGoogleServer) {
226 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); 227 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1");
227 const GURL kExpectedServerUrl( 228 const GURL kExpectedServerUrl(
228 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 229 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
229 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL" 230 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL"
230 "&url=http://www.example.com/"); 231 "&url=http://www.example.com/");
231 232
232 EXPECT_CALL(*mock_image_fetcher_, 233 EXPECT_CALL(*mock_image_fetcher_,
233 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 234 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
234 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap( 235 .WillOnce(PostFetchReply(gfx::Image::CreateFrom1xBitmap(
235 CreateTestSkBitmap(64, 64, kTestColor)))); 236 CreateTestSkBitmap(64, 64, kTestColor))));
236 // Verify that the non-trimmed page URL is used when writing to the database. 237 // Verify that the non-trimmed page URL is used when writing to the database.
237 EXPECT_CALL(mock_favicon_service_, 238 EXPECT_CALL(mock_favicon_service_,
238 SetLastResortFavicons(_, kExpectedServerUrl, _, _, _)); 239 SetLastResortFavicons(_, kExpectedServerUrl, _, _, _));
239 240
240 large_icon_service_ 241 large_icon_service_
241 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 242 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
242 GURL(kDummyUrlWithQuery), /*min_source_size_in_pixel=*/42, 243 GURL(kDummyUrlWithQuery), /*min_source_size_in_pixel=*/42,
243 base::Callback<void(bool success)>()); 244 base::Callback<void(bool success)>());
244 245
245 base::RunLoop().RunUntilIdle(); 246 base::RunLoop().RunUntilIdle();
246 } 247 }
247 248
248 TEST_F(LargeIconServiceTest, ShouldNotQueryGoogleServerIfInvalidScheme) { 249 TEST_F(LargeIconServiceTest, ShouldNotQueryGoogleServerIfInvalidScheme) {
249 const GURL kDummyFtpUrl("ftp://www.example.com"); 250 const GURL kDummyFtpUrl("ftp://www.example.com");
250 251
251 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) 252 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _, _))
252 .Times(0); 253 .Times(0);
253 254
254 base::MockCallback<base::Callback<void(bool success)>> callback; 255 base::MockCallback<base::Callback<void(bool success)>> callback;
255 256
256 large_icon_service_ 257 large_icon_service_
257 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 258 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
258 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, callback.Get()); 259 GURL(kDummyFtpUrl), /*min_source_size_in_pixel=*/42, callback.Get());
259 260
260 EXPECT_CALL(callback, Run(false)); 261 EXPECT_CALL(callback, Run(false));
261 base::RunLoop().RunUntilIdle(); 262 base::RunLoop().RunUntilIdle();
262 EXPECT_THAT(histogram_tester_.GetAllSamples( 263 EXPECT_THAT(histogram_tester_.GetAllSamples(
263 "Favicons.LargeIconService.DownloadedSize"), 264 "Favicons.LargeIconService.DownloadedSize"),
264 IsEmpty()); 265 IsEmpty());
265 } 266 }
266 267
267 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) { 268 TEST_F(LargeIconServiceTest, ShouldReportUnavailableIfFetchFromServerFails) {
268 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1"); 269 const GURL kDummyUrlWithQuery("http://www.example.com?foo=1");
269 const GURL kExpectedServerUrl( 270 const GURL kExpectedServerUrl(
270 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 271 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
271 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL" 272 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL"
272 "&url=http://www.example.com/"); 273 "&url=http://www.example.com/");
273 274
274 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) 275 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _))
275 .Times(0); 276 .Times(0);
276 277
277 base::MockCallback<base::Callback<void(bool success)>> callback; 278 base::MockCallback<base::Callback<void(bool success)>> callback;
278 EXPECT_CALL(*mock_image_fetcher_, 279 EXPECT_CALL(*mock_image_fetcher_,
279 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _)) 280 StartOrQueueNetworkRequest(_, kExpectedServerUrl, _, _))
280 .WillOnce(PostFetchReply(gfx::Image())); 281 .WillOnce(PostFetchReply(gfx::Image()));
281 EXPECT_CALL(mock_favicon_service_, 282 EXPECT_CALL(mock_favicon_service_,
282 UnableToDownloadFavicon(kExpectedServerUrl)); 283 UnableToDownloadFavicon(kExpectedServerUrl));
283 284
284 large_icon_service_ 285 large_icon_service_
285 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 286 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
286 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, callback.Get()); 287 kDummyUrlWithQuery, /*min_source_size_in_pixel=*/42, callback.Get());
287 288
288 EXPECT_CALL(callback, Run(false)); 289 EXPECT_CALL(callback, Run(false));
289 base::RunLoop().RunUntilIdle(); 290 base::RunLoop().RunUntilIdle();
290 // Verify that download failure gets recorded. 291 // Verify that download failure gets recorded.
291 histogram_tester_.ExpectUniqueSample( 292 histogram_tester_.ExpectUniqueSample(
292 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/1); 293 "Favicons.LargeIconService.DownloadedSize", 0, /*expected_count=*/1);
293 } 294 }
294 295
295 TEST_F(LargeIconServiceTest, ShouldNotGetFromGoogleServerIfUnavailable) { 296 TEST_F(LargeIconServiceTest, ShouldNotGetFromGoogleServerIfUnavailable) {
296 ON_CALL( 297 ON_CALL(
297 mock_favicon_service_, 298 mock_favicon_service_,
298 WasUnableToDownloadFavicon(GURL( 299 WasUnableToDownloadFavicon(GURL(
299 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true" 300 "https://t0.gstatic.com/faviconV2?client=chrome&drop_404_icon=true"
300 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL" 301 "&size=32&min_size=42&max_size=64&fallback_opts=TYPE,SIZE,URL"
301 "&url=http://www.example.com/"))) 302 "&url=http://www.example.com/")))
302 .WillByDefault(Return(true)); 303 .WillByDefault(Return(true));
303 304
304 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0); 305 EXPECT_CALL(mock_favicon_service_, UnableToDownloadFavicon(_)).Times(0);
305 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)) 306 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _, _))
306 .Times(0); 307 .Times(0);
307 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _)) 308 EXPECT_CALL(mock_favicon_service_, SetLastResortFavicons(_, _, _, _, _))
308 .Times(0); 309 .Times(0);
309 310
310 base::MockCallback<base::Callback<void(bool success)>> callback; 311 base::MockCallback<base::Callback<void(bool success)>> callback;
311 large_icon_service_ 312 large_icon_service_
312 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( 313 .GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
313 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get()); 314 GURL(kDummyUrl), /*min_source_size_in_pixel=*/42, callback.Get());
314 315
315 EXPECT_CALL(callback, Run(false)); 316 EXPECT_CALL(callback, Run(false));
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 473
473 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g. 474 // Every test will appear with suffix /0 (param false) and /1 (param true), e.g.
474 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image. 475 // LargeIconServiceGetterTest.FallbackSinceTooPicky/0: get image.
475 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap. 476 // LargeIconServiceGetterTest.FallbackSinceTooPicky/1: get raw bitmap.
476 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name. 477 INSTANTIATE_TEST_CASE_P(, // Empty instatiation name.
477 LargeIconServiceGetterTest, 478 LargeIconServiceGetterTest,
478 ::testing::Values(false, true)); 479 ::testing::Values(false, true));
479 480
480 } // namespace 481 } // namespace
481 } // namespace favicon 482 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698