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

Side by Side Diff: third_party/WebKit/Source/core/loader/resource/ImageResourceTest.cpp

Issue 2609463002: Unify ImageResource* local variable names in ImageResourceTest.cpp (Closed)
Patch Set: Created 3 years, 11 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved. 2 * Copyright (c) 2013, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 } 219 }
220 220
221 TEST(ImageResourceTest, MultipartImage) { 221 TEST(ImageResourceTest, MultipartImage) {
222 ResourceFetcher* fetcher = 222 ResourceFetcher* fetcher =
223 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 223 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
224 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 224 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
225 ScopedRegisteredURL scopedRegisteredURL(testURL); 225 ScopedRegisteredURL scopedRegisteredURL(testURL);
226 226
227 // Emulate starting a real load, but don't expect any "real" 227 // Emulate starting a real load, but don't expect any "real"
228 // WebURLLoaderClient callbacks. 228 // WebURLLoaderClient callbacks.
229 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); 229 ImageResource* imageResource =
230 cachedImage->setIdentifier(createUniqueIdentifier()); 230 ImageResource::create(ResourceRequest(testURL));
231 fetcher->startLoad(cachedImage); 231 imageResource->setIdentifier(createUniqueIdentifier());
232 fetcher->startLoad(imageResource);
232 233
233 Persistent<MockImageResourceClient> client = 234 Persistent<MockImageResourceClient> client =
234 new MockImageResourceClient(cachedImage); 235 new MockImageResourceClient(imageResource);
235 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 236 EXPECT_EQ(Resource::Pending, imageResource->getStatus());
236 237
237 // Send the multipart response. No image or data buffer is created. Note that 238 // Send the multipart response. No image or data buffer is created. Note that
238 // the response must be routed through ResourceLoader to ensure the load is 239 // the response must be routed through ResourceLoader to ensure the load is
239 // flagged as multipart. 240 // flagged as multipart.
240 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, 241 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0,
241 nullAtom, String()); 242 nullAtom, String());
242 multipartResponse.setMultipartBoundary("boundary", strlen("boundary")); 243 multipartResponse.setMultipartBoundary("boundary", strlen("boundary"));
243 cachedImage->loader()->didReceiveResponse( 244 imageResource->loader()->didReceiveResponse(
244 WrappedResourceResponse(multipartResponse), nullptr); 245 WrappedResourceResponse(multipartResponse), nullptr);
245 EXPECT_FALSE(cachedImage->resourceBuffer()); 246 EXPECT_FALSE(imageResource->resourceBuffer());
246 EXPECT_FALSE(cachedImage->getContent()->hasImage()); 247 EXPECT_FALSE(imageResource->getContent()->hasImage());
247 EXPECT_EQ(0, client->imageChangedCount()); 248 EXPECT_EQ(0, client->imageChangedCount());
248 EXPECT_FALSE(client->notifyFinishedCalled()); 249 EXPECT_FALSE(client->notifyFinishedCalled());
249 EXPECT_EQ("multipart/x-mixed-replace", cachedImage->response().mimeType()); 250 EXPECT_EQ("multipart/x-mixed-replace", imageResource->response().mimeType());
250 251
251 const char firstPart[] = 252 const char firstPart[] =
252 "--boundary\n" 253 "--boundary\n"
253 "Content-Type: image/svg+xml\n\n"; 254 "Content-Type: image/svg+xml\n\n";
254 cachedImage->appendData(firstPart, strlen(firstPart)); 255 imageResource->appendData(firstPart, strlen(firstPart));
255 // Send the response for the first real part. No image or data buffer is 256 // Send the response for the first real part. No image or data buffer is
256 // created. 257 // created.
257 EXPECT_FALSE(cachedImage->resourceBuffer()); 258 EXPECT_FALSE(imageResource->resourceBuffer());
258 EXPECT_FALSE(cachedImage->getContent()->hasImage()); 259 EXPECT_FALSE(imageResource->getContent()->hasImage());
259 EXPECT_EQ(0, client->imageChangedCount()); 260 EXPECT_EQ(0, client->imageChangedCount());
260 EXPECT_FALSE(client->notifyFinishedCalled()); 261 EXPECT_FALSE(client->notifyFinishedCalled());
261 EXPECT_EQ("image/svg+xml", cachedImage->response().mimeType()); 262 EXPECT_EQ("image/svg+xml", imageResource->response().mimeType());
262 263
263 const char secondPart[] = 264 const char secondPart[] =
264 "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect " 265 "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect "
265 "width='1' height='1' fill='green'/></svg>\n"; 266 "width='1' height='1' fill='green'/></svg>\n";
266 // The first bytes arrive. The data buffer is created, but no image is 267 // The first bytes arrive. The data buffer is created, but no image is
267 // created. 268 // created.
268 cachedImage->appendData(secondPart, strlen(secondPart)); 269 imageResource->appendData(secondPart, strlen(secondPart));
269 EXPECT_TRUE(cachedImage->resourceBuffer()); 270 EXPECT_TRUE(imageResource->resourceBuffer());
270 EXPECT_FALSE(cachedImage->getContent()->hasImage()); 271 EXPECT_FALSE(imageResource->getContent()->hasImage());
271 EXPECT_EQ(0, client->imageChangedCount()); 272 EXPECT_EQ(0, client->imageChangedCount());
272 EXPECT_FALSE(client->notifyFinishedCalled()); 273 EXPECT_FALSE(client->notifyFinishedCalled());
273 274
274 // Add a client to check an assertion error doesn't happen 275 // Add a client to check an assertion error doesn't happen
275 // (crbug.com/630983). 276 // (crbug.com/630983).
276 Persistent<MockImageResourceClient> client2 = 277 Persistent<MockImageResourceClient> client2 =
277 new MockImageResourceClient(cachedImage); 278 new MockImageResourceClient(imageResource);
278 EXPECT_EQ(0, client2->imageChangedCount()); 279 EXPECT_EQ(0, client2->imageChangedCount());
279 EXPECT_FALSE(client2->notifyFinishedCalled()); 280 EXPECT_FALSE(client2->notifyFinishedCalled());
280 281
281 const char thirdPart[] = "--boundary"; 282 const char thirdPart[] = "--boundary";
282 cachedImage->appendData(thirdPart, strlen(thirdPart)); 283 imageResource->appendData(thirdPart, strlen(thirdPart));
283 ASSERT_TRUE(cachedImage->resourceBuffer()); 284 ASSERT_TRUE(imageResource->resourceBuffer());
284 EXPECT_EQ(strlen(secondPart) - 1, cachedImage->resourceBuffer()->size()); 285 EXPECT_EQ(strlen(secondPart) - 1, imageResource->resourceBuffer()->size());
285 286
286 // This part finishes. The image is created, callbacks are sent, and the data 287 // This part finishes. The image is created, callbacks are sent, and the data
287 // buffer is cleared. 288 // buffer is cleared.
288 cachedImage->loader()->didFinishLoading(0.0, 0, 0); 289 imageResource->loader()->didFinishLoading(0.0, 0, 0);
289 EXPECT_TRUE(cachedImage->resourceBuffer()); 290 EXPECT_TRUE(imageResource->resourceBuffer());
290 EXPECT_FALSE(cachedImage->errorOccurred()); 291 EXPECT_FALSE(imageResource->errorOccurred());
291 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 292 ASSERT_TRUE(imageResource->getContent()->hasImage());
292 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 293 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
293 EXPECT_EQ(1, cachedImage->getContent()->getImage()->width()); 294 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
294 EXPECT_EQ(1, cachedImage->getContent()->getImage()->height()); 295 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
295 EXPECT_EQ(1, client->imageChangedCount()); 296 EXPECT_EQ(1, client->imageChangedCount());
296 EXPECT_TRUE(client->notifyFinishedCalled()); 297 EXPECT_TRUE(client->notifyFinishedCalled());
297 EXPECT_EQ(1, client2->imageChangedCount()); 298 EXPECT_EQ(1, client2->imageChangedCount());
298 EXPECT_TRUE(client2->notifyFinishedCalled()); 299 EXPECT_TRUE(client2->notifyFinishedCalled());
299 } 300 }
300 301
301 TEST(ImageResourceTest, CancelOnDetach) { 302 TEST(ImageResourceTest, CancelOnDetach) {
302 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 303 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
303 ScopedRegisteredURL scopedRegisteredURL(testURL); 304 ScopedRegisteredURL scopedRegisteredURL(testURL);
304 305
305 ResourceFetcher* fetcher = 306 ResourceFetcher* fetcher =
306 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 307 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
307 308
308 // Emulate starting a real load. 309 // Emulate starting a real load.
309 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); 310 ImageResource* imageResource =
310 cachedImage->setIdentifier(createUniqueIdentifier()); 311 ImageResource::create(ResourceRequest(testURL));
312 imageResource->setIdentifier(createUniqueIdentifier());
311 313
312 fetcher->startLoad(cachedImage); 314 fetcher->startLoad(imageResource);
313 memoryCache()->add(cachedImage); 315 memoryCache()->add(imageResource);
314 316
315 Persistent<MockImageResourceClient> client = 317 Persistent<MockImageResourceClient> client =
316 new MockImageResourceClient(cachedImage); 318 new MockImageResourceClient(imageResource);
317 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 319 EXPECT_EQ(Resource::Pending, imageResource->getStatus());
318 320
319 // The load should still be alive, but a timer should be started to cancel the 321 // The load should still be alive, but a timer should be started to cancel the
320 // load inside removeClient(). 322 // load inside removeClient().
321 client->removeAsClient(); 323 client->removeAsClient();
322 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 324 EXPECT_EQ(Resource::Pending, imageResource->getStatus());
323 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); 325 EXPECT_TRUE(memoryCache()->resourceForURL(testURL));
324 326
325 // Trigger the cancel timer, ensure the load was cancelled and the resource 327 // Trigger the cancel timer, ensure the load was cancelled and the resource
326 // was evicted from the cache. 328 // was evicted from the cache.
327 blink::testing::runPendingTasks(); 329 blink::testing::runPendingTasks();
328 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); 330 EXPECT_EQ(Resource::LoadError, imageResource->getStatus());
329 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); 331 EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
330 } 332 }
331 333
332 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) { 334 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) {
333 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); 335 ImageResource* imageResource = ImageResource::create(ResourceRequest());
334 cachedImage->setStatus(Resource::Pending); 336 imageResource->setStatus(Resource::Pending);
335 337
336 Persistent<MockImageResourceClient> client = 338 Persistent<MockImageResourceClient> client =
337 new MockImageResourceClient(cachedImage); 339 new MockImageResourceClient(imageResource);
338 340
339 // Send the image response. 341 // Send the image response.
340 cachedImage->responseReceived( 342 imageResource->responseReceived(
341 ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, 343 ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom,
342 String()), 344 String()),
343 nullptr); 345 nullptr);
344 346
345 cachedImage->responseReceived( 347 imageResource->responseReceived(
346 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, 348 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom,
347 String()), 349 String()),
348 nullptr); 350 nullptr);
349 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), 351 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage),
350 sizeof(kJpegImage)); 352 sizeof(kJpegImage));
351 EXPECT_NE(0u, cachedImage->encodedSizeMemoryUsageForTesting()); 353 EXPECT_NE(0u, imageResource->encodedSizeMemoryUsageForTesting());
352 cachedImage->finish(); 354 imageResource->finish();
353 EXPECT_EQ(0u, cachedImage->encodedSizeMemoryUsageForTesting()); 355 EXPECT_EQ(0u, imageResource->encodedSizeMemoryUsageForTesting());
354 EXPECT_FALSE(cachedImage->errorOccurred()); 356 EXPECT_FALSE(imageResource->errorOccurred());
355 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 357 ASSERT_TRUE(imageResource->getContent()->hasImage());
356 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 358 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
357 EXPECT_TRUE(client->notifyFinishedCalled()); 359 EXPECT_TRUE(client->notifyFinishedCalled());
358 360
359 // The prune comes when the ImageResource still has clients. The image should 361 // The prune comes when the ImageResource still has clients. The image should
360 // not be deleted. 362 // not be deleted.
361 cachedImage->prune(); 363 imageResource->prune();
362 EXPECT_TRUE(cachedImage->isAlive()); 364 EXPECT_TRUE(imageResource->isAlive());
363 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 365 ASSERT_TRUE(imageResource->getContent()->hasImage());
364 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 366 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
365 367
366 // The ImageResource no longer has clients. The decoded image data should be 368 // The ImageResource no longer has clients. The decoded image data should be
367 // deleted by prune. 369 // deleted by prune.
368 client->removeAsClient(); 370 client->removeAsClient();
369 cachedImage->prune(); 371 imageResource->prune();
370 EXPECT_FALSE(cachedImage->isAlive()); 372 EXPECT_FALSE(imageResource->isAlive());
371 EXPECT_TRUE(cachedImage->getContent()->hasImage()); 373 EXPECT_TRUE(imageResource->getContent()->hasImage());
372 // TODO(hajimehoshi): Should check cachedImage doesn't have decoded image 374 // TODO(hajimehoshi): Should check imageResource doesn't have decoded image
373 // data. 375 // data.
374 } 376 }
375 377
376 TEST(ImageResourceTest, UpdateBitmapImages) { 378 TEST(ImageResourceTest, UpdateBitmapImages) {
377 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); 379 ImageResource* imageResource = ImageResource::create(ResourceRequest());
378 cachedImage->setStatus(Resource::Pending); 380 imageResource->setStatus(Resource::Pending);
379 381
380 Persistent<MockImageResourceClient> client = 382 Persistent<MockImageResourceClient> client =
381 new MockImageResourceClient(cachedImage); 383 new MockImageResourceClient(imageResource);
382 384
383 // Send the image response. 385 // Send the image response.
384 cachedImage->responseReceived( 386 imageResource->responseReceived(
385 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, 387 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom,
386 String()), 388 String()),
387 nullptr); 389 nullptr);
388 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), 390 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage),
389 sizeof(kJpegImage)); 391 sizeof(kJpegImage));
390 cachedImage->finish(); 392 imageResource->finish();
391 EXPECT_FALSE(cachedImage->errorOccurred()); 393 EXPECT_FALSE(imageResource->errorOccurred());
392 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 394 ASSERT_TRUE(imageResource->getContent()->hasImage());
393 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 395 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
394 EXPECT_EQ(2, client->imageChangedCount()); 396 EXPECT_EQ(2, client->imageChangedCount());
395 EXPECT_TRUE(client->notifyFinishedCalled()); 397 EXPECT_TRUE(client->notifyFinishedCalled());
396 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 398 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
397 } 399 }
398 400
399 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { 401 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) {
400 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 402 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
401 ScopedRegisteredURL scopedRegisteredURL(testURL); 403 ScopedRegisteredURL scopedRegisteredURL(testURL);
402 ResourceRequest request = ResourceRequest(testURL); 404 ResourceRequest request = ResourceRequest(testURL);
403 request.setLoFiState(WebURLRequest::LoFiOn); 405 request.setLoFiState(WebURLRequest::LoFiOn);
404 ImageResource* cachedImage = ImageResource::create(request); 406 ImageResource* imageResource = ImageResource::create(request);
405 cachedImage->setStatus(Resource::Pending); 407 imageResource->setStatus(Resource::Pending);
406 408
407 Persistent<MockImageResourceClient> client = 409 Persistent<MockImageResourceClient> client =
408 new MockImageResourceClient(cachedImage); 410 new MockImageResourceClient(imageResource);
409 ResourceFetcher* fetcher = 411 ResourceFetcher* fetcher =
410 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 412 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
411 413
412 // Send the image response. 414 // Send the image response.
413 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), 415 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage),
414 nullAtom, String()); 416 nullAtom, String());
415 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", 417 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform",
416 "empty-image"); 418 "empty-image");
417 419
418 cachedImage->responseReceived(resourceResponse, nullptr); 420 imageResource->responseReceived(resourceResponse, nullptr);
419 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), 421 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage),
420 sizeof(kJpegImage)); 422 sizeof(kJpegImage));
421 cachedImage->finish(); 423 imageResource->finish();
422 EXPECT_FALSE(cachedImage->errorOccurred()); 424 EXPECT_FALSE(imageResource->errorOccurred());
423 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 425 ASSERT_TRUE(imageResource->getContent()->hasImage());
424 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 426 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
425 EXPECT_EQ(2, client->imageChangedCount()); 427 EXPECT_EQ(2, client->imageChangedCount());
426 EXPECT_EQ(1, client->imageNotifyFinishedCount()); 428 EXPECT_EQ(1, client->imageNotifyFinishedCount());
427 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 429 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
428 // The client should have been notified that the image load completed. 430 // The client should have been notified that the image load completed.
429 EXPECT_TRUE(client->notifyFinishedCalled()); 431 EXPECT_TRUE(client->notifyFinishedCalled());
430 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); 432 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
431 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); 433 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
432 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 434 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
433 EXPECT_EQ(1, cachedImage->getContent()->getImage()->width()); 435 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
434 EXPECT_EQ(1, cachedImage->getContent()->getImage()->height()); 436 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
435 437
436 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. 438 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading.
437 cachedImage->reloadIfLoFiOrPlaceholderImage(fetcher, Resource::kReloadAlways); 439 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher,
438 EXPECT_FALSE(cachedImage->errorOccurred()); 440 Resource::kReloadAlways);
439 EXPECT_FALSE(cachedImage->resourceBuffer()); 441 EXPECT_FALSE(imageResource->errorOccurred());
440 EXPECT_FALSE(cachedImage->getContent()->hasImage()); 442 EXPECT_FALSE(imageResource->resourceBuffer());
443 EXPECT_FALSE(imageResource->getContent()->hasImage());
441 EXPECT_EQ(3, client->imageChangedCount()); 444 EXPECT_EQ(3, client->imageChangedCount());
442 EXPECT_EQ(1, client->imageNotifyFinishedCount()); 445 EXPECT_EQ(1, client->imageNotifyFinishedCount());
443 446
444 cachedImage->loader()->didReceiveResponse( 447 imageResource->loader()->didReceiveResponse(
445 WrappedResourceResponse(resourceResponse), nullptr); 448 WrappedResourceResponse(resourceResponse), nullptr);
446 cachedImage->loader()->didReceiveData( 449 imageResource->loader()->didReceiveData(
447 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); 450 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2));
448 cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), 451 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
449 sizeof(kJpegImage2)); 452 sizeof(kJpegImage2));
450 EXPECT_FALSE(cachedImage->errorOccurred()); 453 EXPECT_FALSE(imageResource->errorOccurred());
451 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 454 ASSERT_TRUE(imageResource->getContent()->hasImage());
452 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 455 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
453 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); 456 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged());
454 EXPECT_TRUE(client->notifyFinishedCalled()); 457 EXPECT_TRUE(client->notifyFinishedCalled());
455 458
456 // The client should not have been notified of completion again. 459 // The client should not have been notified of completion again.
457 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); 460 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
458 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); 461 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
459 462
460 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 463 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
461 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); 464 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
462 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); 465 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
463 } 466 }
464 467
465 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { 468 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
466 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 469 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
467 ScopedRegisteredURL scopedRegisteredURL(testURL); 470 ScopedRegisteredURL scopedRegisteredURL(testURL);
468 471
469 ResourceRequest request(testURL); 472 ResourceRequest request(testURL);
470 request.setLoFiState(WebURLRequest::LoFiOn); 473 request.setLoFiState(WebURLRequest::LoFiOn);
471 FetchRequest fetchRequest(request, FetchInitiatorInfo()); 474 FetchRequest fetchRequest(request, FetchInitiatorInfo());
472 ResourceFetcher* fetcher = 475 ResourceFetcher* fetcher =
473 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 476 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
474 477
475 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); 478 ImageResource* imageResource = ImageResource::fetch(fetchRequest, fetcher);
476 Persistent<MockImageResourceClient> client = 479 Persistent<MockImageResourceClient> client =
477 new MockImageResourceClient(cachedImage); 480 new MockImageResourceClient(imageResource);
478 481
479 // Send the image response. 482 // Send the image response.
480 ResourceResponse initialResourceResponse( 483 ResourceResponse initialResourceResponse(
481 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); 484 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String());
482 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); 485 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low");
483 486
484 cachedImage->loader()->didReceiveResponse( 487 imageResource->loader()->didReceiveResponse(
485 WrappedResourceResponse(initialResourceResponse)); 488 WrappedResourceResponse(initialResourceResponse));
486 cachedImage->loader()->didReceiveData( 489 imageResource->loader()->didReceiveData(
487 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); 490 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
488 491
489 EXPECT_FALSE(cachedImage->errorOccurred()); 492 EXPECT_FALSE(imageResource->errorOccurred());
490 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 493 ASSERT_TRUE(imageResource->getContent()->hasImage());
491 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 494 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
492 EXPECT_EQ(1, client->imageChangedCount()); 495 EXPECT_EQ(1, client->imageChangedCount());
493 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 496 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
494 EXPECT_FALSE(client->notifyFinishedCalled()); 497 EXPECT_FALSE(client->notifyFinishedCalled());
495 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 498 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
496 EXPECT_EQ(1, cachedImage->getContent()->getImage()->width()); 499 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
497 EXPECT_EQ(1, cachedImage->getContent()->getImage()->height()); 500 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
498 501
499 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. 502 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading.
500 cachedImage->reloadIfLoFiOrPlaceholderImage(fetcher, Resource::kReloadAlways); 503 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher,
501 EXPECT_FALSE(cachedImage->errorOccurred()); 504 Resource::kReloadAlways);
502 EXPECT_FALSE(cachedImage->resourceBuffer()); 505 EXPECT_FALSE(imageResource->errorOccurred());
503 EXPECT_FALSE(cachedImage->getContent()->hasImage()); 506 EXPECT_FALSE(imageResource->resourceBuffer());
507 EXPECT_FALSE(imageResource->getContent()->hasImage());
504 EXPECT_EQ(2, client->imageChangedCount()); 508 EXPECT_EQ(2, client->imageChangedCount());
505 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); 509 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged());
506 // The client should not have been notified of completion yet, since the image 510 // The client should not have been notified of completion yet, since the image
507 // is still loading. 511 // is still loading.
508 EXPECT_FALSE(client->notifyFinishedCalled()); 512 EXPECT_FALSE(client->notifyFinishedCalled());
509 513
510 cachedImage->loader()->didReceiveResponse( 514 imageResource->loader()->didReceiveResponse(
511 WrappedResourceResponse(ResourceResponse( 515 WrappedResourceResponse(ResourceResponse(
512 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), 516 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())),
513 nullptr); 517 nullptr);
514 cachedImage->loader()->didReceiveData( 518 imageResource->loader()->didReceiveData(
515 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); 519 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2));
516 cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), 520 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
517 sizeof(kJpegImage2)); 521 sizeof(kJpegImage2));
518 522
519 EXPECT_FALSE(cachedImage->errorOccurred()); 523 EXPECT_FALSE(imageResource->errorOccurred());
520 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 524 ASSERT_TRUE(imageResource->getContent()->hasImage());
521 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 525 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
522 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); 526 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged());
523 // The client should have been notified of completion only after the reload 527 // The client should have been notified of completion only after the reload
524 // completed. 528 // completed.
525 EXPECT_TRUE(client->notifyFinishedCalled()); 529 EXPECT_TRUE(client->notifyFinishedCalled());
526 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished()); 530 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished());
527 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished()); 531 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished());
528 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 532 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
529 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); 533 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
530 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); 534 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
531 } 535 }
532 536
533 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { 537 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) {
534 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 538 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
535 ScopedRegisteredURL scopedRegisteredURL(testURL); 539 ScopedRegisteredURL scopedRegisteredURL(testURL);
536 540
537 ResourceFetcher* fetcher = 541 ResourceFetcher* fetcher =
538 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 542 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
539 FetchRequest request(testURL, FetchInitiatorInfo()); 543 FetchRequest request(testURL, FetchInitiatorInfo());
540 request.setAllowImagePlaceholder(); 544 request.setAllowImagePlaceholder();
541 ImageResource* image = ImageResource::fetch(request, fetcher); 545 ImageResource* imageResource = ImageResource::fetch(request, fetcher);
542 EXPECT_EQ(FetchRequest::AllowPlaceholder, 546 EXPECT_EQ(FetchRequest::AllowPlaceholder,
543 request.placeholderImageRequestType()); 547 request.placeholderImageRequestType());
544 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); 548 EXPECT_EQ("bytes=0-2047",
549 imageResource->resourceRequest().httpHeaderField("range"));
545 Persistent<MockImageResourceClient> client = 550 Persistent<MockImageResourceClient> client =
546 new MockImageResourceClient(image); 551 new MockImageResourceClient(imageResource);
547 552
548 ResourceResponse response(testURL, "image/jpeg", 553 ResourceResponse response(testURL, "image/jpeg",
549 kJpegImageSubrangeWithDimensionsLength, nullAtom, 554 kJpegImageSubrangeWithDimensionsLength, nullAtom,
550 String()); 555 String());
551 response.setHTTPStatusCode(206); 556 response.setHTTPStatusCode(206);
552 response.setHTTPHeaderField( 557 response.setHTTPHeaderField(
553 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, 558 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength,
554 sizeof(kJpegImage))); 559 sizeof(kJpegImage)));
555 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); 560 imageResource->loader()->didReceiveResponse(
556 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 561 WrappedResourceResponse(response));
557 kJpegImageSubrangeWithDimensionsLength); 562 imageResource->loader()->didReceiveData(
558 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, 563 reinterpret_cast<const char*>(kJpegImage),
559 kJpegImageSubrangeWithDimensionsLength); 564 kJpegImageSubrangeWithDimensionsLength);
565 imageResource->loader()->didFinishLoading(
566 0.0, kJpegImageSubrangeWithDimensionsLength,
567 kJpegImageSubrangeWithDimensionsLength);
560 568
561 EXPECT_EQ(Resource::Cached, image->getStatus()); 569 EXPECT_EQ(Resource::Cached, imageResource->getStatus());
562 EXPECT_TRUE(image->isPlaceholder()); 570 EXPECT_TRUE(imageResource->isPlaceholder());
563 571
564 image->reloadIfLoFiOrPlaceholderImage(fetcher, Resource::kReloadAlways); 572 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher,
573 Resource::kReloadAlways);
565 574
566 EXPECT_EQ(Resource::Pending, image->getStatus()); 575 EXPECT_EQ(Resource::Pending, imageResource->getStatus());
567 EXPECT_FALSE(image->isPlaceholder()); 576 EXPECT_FALSE(imageResource->isPlaceholder());
568 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 577 EXPECT_EQ(nullAtom,
569 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), 578 imageResource->resourceRequest().httpHeaderField("range"));
570 static_cast<int>(image->resourceRequest().getCachePolicy())); 579 EXPECT_EQ(
580 static_cast<int>(WebCachePolicy::BypassingCache),
581 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
571 582
572 image->loader()->cancel(); 583 imageResource->loader()->cancel();
573 } 584 }
574 585
575 TEST(ImageResourceTest, SVGImage) { 586 TEST(ImageResourceTest, SVGImage) {
576 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); 587 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo");
577 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); 588 ImageResource* imageResource = ImageResource::create(ResourceRequest(url));
578 Persistent<MockImageResourceClient> client = 589 Persistent<MockImageResourceClient> client =
579 new MockImageResourceClient(imageResource); 590 new MockImageResourceClient(imageResource);
580 591
581 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, 592 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage,
582 strlen(kSvgImage)); 593 strlen(kSvgImage));
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 EXPECT_TRUE(client2->notifyFinishedCalled()); 854 EXPECT_TRUE(client2->notifyFinishedCalled());
844 } 855 }
845 856
846 TEST(ImageResourceTest, CancelOnDecodeError) { 857 TEST(ImageResourceTest, CancelOnDecodeError) {
847 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 858 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
848 ScopedRegisteredURL scopedRegisteredURL(testURL); 859 ScopedRegisteredURL scopedRegisteredURL(testURL);
849 860
850 ResourceFetcher* fetcher = 861 ResourceFetcher* fetcher =
851 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 862 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
852 FetchRequest request(testURL, FetchInitiatorInfo()); 863 FetchRequest request(testURL, FetchInitiatorInfo());
853 ImageResource* cachedImage = ImageResource::fetch(request, fetcher); 864 ImageResource* imageResource = ImageResource::fetch(request, fetcher);
854 865
855 cachedImage->loader()->didReceiveResponse( 866 imageResource->loader()->didReceiveResponse(
856 WrappedResourceResponse( 867 WrappedResourceResponse(
857 ResourceResponse(testURL, "image/jpeg", 18, nullAtom, String())), 868 ResourceResponse(testURL, "image/jpeg", 18, nullAtom, String())),
858 nullptr); 869 nullptr);
859 cachedImage->loader()->didReceiveData("notactuallyanimage", 18); 870 imageResource->loader()->didReceiveData("notactuallyanimage", 18);
860 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); 871 EXPECT_EQ(Resource::DecodeError, imageResource->getStatus());
861 EXPECT_FALSE(cachedImage->isLoading()); 872 EXPECT_FALSE(imageResource->isLoading());
862 } 873 }
863 874
864 TEST(ImageResourceTest, FetchDisallowPlaceholder) { 875 TEST(ImageResourceTest, FetchDisallowPlaceholder) {
865 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 876 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
866 ScopedRegisteredURL scopedRegisteredURL(testURL); 877 ScopedRegisteredURL scopedRegisteredURL(testURL);
867 878
868 FetchRequest request(testURL, FetchInitiatorInfo()); 879 FetchRequest request(testURL, FetchInitiatorInfo());
869 ImageResource* image = ImageResource::fetch( 880 ImageResource* imageResource = ImageResource::fetch(
870 request, 881 request,
871 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 882 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
872 EXPECT_EQ(FetchRequest::DisallowPlaceholder, 883 EXPECT_EQ(FetchRequest::DisallowPlaceholder,
873 request.placeholderImageRequestType()); 884 request.placeholderImageRequestType());
874 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 885 EXPECT_EQ(nullAtom,
875 EXPECT_FALSE(image->isPlaceholder()); 886 imageResource->resourceRequest().httpHeaderField("range"));
887 EXPECT_FALSE(imageResource->isPlaceholder());
876 Persistent<MockImageResourceClient> client = 888 Persistent<MockImageResourceClient> client =
877 new MockImageResourceClient(image); 889 new MockImageResourceClient(imageResource);
878 890
879 image->loader()->didReceiveResponse(WrappedResourceResponse(ResourceResponse( 891 imageResource->loader()->didReceiveResponse(
880 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); 892 WrappedResourceResponse(ResourceResponse(
881 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 893 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
882 sizeof(kJpegImage)); 894 imageResource->loader()->didReceiveData(
883 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage), 895 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
884 sizeof(kJpegImage)); 896 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
897 sizeof(kJpegImage));
885 898
886 EXPECT_EQ(Resource::Cached, image->getStatus()); 899 EXPECT_EQ(Resource::Cached, imageResource->getStatus());
887 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); 900 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize());
888 EXPECT_FALSE(image->isPlaceholder()); 901 EXPECT_FALSE(imageResource->isPlaceholder());
889 EXPECT_LT(0, client->imageChangedCount()); 902 EXPECT_LT(0, client->imageChangedCount());
890 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 903 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
891 EXPECT_TRUE(client->notifyFinishedCalled()); 904 EXPECT_TRUE(client->notifyFinishedCalled());
892 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); 905 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
893 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); 906 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
894 907
895 ASSERT_TRUE(image->getContent()->hasImage()); 908 ASSERT_TRUE(imageResource->getContent()->hasImage());
896 EXPECT_EQ(1, image->getContent()->getImage()->width()); 909 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
897 EXPECT_EQ(1, image->getContent()->getImage()->height()); 910 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
898 EXPECT_TRUE(image->getContent()->getImage()->isBitmapImage()); 911 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
899 } 912 }
900 913
901 TEST(ImageResourceTest, FetchAllowPlaceholderDataURL) { 914 TEST(ImageResourceTest, FetchAllowPlaceholderDataURL) {
902 KURL testURL(ParsedURLString, 915 KURL testURL(ParsedURLString,
903 "data:image/jpeg;base64," + 916 "data:image/jpeg;base64," +
904 base64Encode(reinterpret_cast<const char*>(kJpegImage), 917 base64Encode(reinterpret_cast<const char*>(kJpegImage),
905 sizeof(kJpegImage))); 918 sizeof(kJpegImage)));
906 FetchRequest request(testURL, FetchInitiatorInfo()); 919 FetchRequest request(testURL, FetchInitiatorInfo());
907 request.setAllowImagePlaceholder(); 920 request.setAllowImagePlaceholder();
908 ImageResource* image = ImageResource::fetch( 921 ImageResource* imageResource = ImageResource::fetch(
909 request, 922 request,
910 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 923 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
911 EXPECT_EQ(FetchRequest::DisallowPlaceholder, 924 EXPECT_EQ(FetchRequest::DisallowPlaceholder,
912 request.placeholderImageRequestType()); 925 request.placeholderImageRequestType());
913 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 926 EXPECT_EQ(nullAtom,
914 EXPECT_FALSE(image->isPlaceholder()); 927 imageResource->resourceRequest().httpHeaderField("range"));
928 EXPECT_FALSE(imageResource->isPlaceholder());
915 } 929 }
916 930
917 TEST(ImageResourceTest, FetchAllowPlaceholderPostRequest) { 931 TEST(ImageResourceTest, FetchAllowPlaceholderPostRequest) {
918 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 932 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
919 ScopedRegisteredURL scopedRegisteredURL(testURL); 933 ScopedRegisteredURL scopedRegisteredURL(testURL);
920 ResourceRequest resourceRequest(testURL); 934 ResourceRequest resourceRequest(testURL);
921 resourceRequest.setHTTPMethod("POST"); 935 resourceRequest.setHTTPMethod("POST");
922 FetchRequest request(resourceRequest, FetchInitiatorInfo()); 936 FetchRequest request(resourceRequest, FetchInitiatorInfo());
923 request.setAllowImagePlaceholder(); 937 request.setAllowImagePlaceholder();
924 ImageResource* image = ImageResource::fetch( 938 ImageResource* imageResource = ImageResource::fetch(
925 request, 939 request,
926 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 940 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
927 EXPECT_EQ(FetchRequest::DisallowPlaceholder, 941 EXPECT_EQ(FetchRequest::DisallowPlaceholder,
928 request.placeholderImageRequestType()); 942 request.placeholderImageRequestType());
929 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 943 EXPECT_EQ(nullAtom,
930 EXPECT_FALSE(image->isPlaceholder()); 944 imageResource->resourceRequest().httpHeaderField("range"));
945 EXPECT_FALSE(imageResource->isPlaceholder());
931 946
932 image->loader()->cancel(); 947 imageResource->loader()->cancel();
933 } 948 }
934 949
935 TEST(ImageResourceTest, FetchAllowPlaceholderExistingRangeHeader) { 950 TEST(ImageResourceTest, FetchAllowPlaceholderExistingRangeHeader) {
936 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 951 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
937 ScopedRegisteredURL scopedRegisteredURL(testURL); 952 ScopedRegisteredURL scopedRegisteredURL(testURL);
938 ResourceRequest resourceRequest(testURL); 953 ResourceRequest resourceRequest(testURL);
939 resourceRequest.setHTTPHeaderField("range", "bytes=128-255"); 954 resourceRequest.setHTTPHeaderField("range", "bytes=128-255");
940 FetchRequest request(resourceRequest, FetchInitiatorInfo()); 955 FetchRequest request(resourceRequest, FetchInitiatorInfo());
941 request.setAllowImagePlaceholder(); 956 request.setAllowImagePlaceholder();
942 ImageResource* image = ImageResource::fetch( 957 ImageResource* imageResource = ImageResource::fetch(
943 request, 958 request,
944 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 959 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
945 EXPECT_EQ(FetchRequest::DisallowPlaceholder, 960 EXPECT_EQ(FetchRequest::DisallowPlaceholder,
946 request.placeholderImageRequestType()); 961 request.placeholderImageRequestType());
947 EXPECT_EQ("bytes=128-255", image->resourceRequest().httpHeaderField("range")); 962 EXPECT_EQ("bytes=128-255",
948 EXPECT_FALSE(image->isPlaceholder()); 963 imageResource->resourceRequest().httpHeaderField("range"));
964 EXPECT_FALSE(imageResource->isPlaceholder());
949 965
950 image->loader()->cancel(); 966 imageResource->loader()->cancel();
951 } 967 }
952 968
953 TEST(ImageResourceTest, FetchAllowPlaceholderSuccessful) { 969 TEST(ImageResourceTest, FetchAllowPlaceholderSuccessful) {
954 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 970 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
955 ScopedRegisteredURL scopedRegisteredURL(testURL); 971 ScopedRegisteredURL scopedRegisteredURL(testURL);
956 972
957 FetchRequest request(testURL, FetchInitiatorInfo()); 973 FetchRequest request(testURL, FetchInitiatorInfo());
958 request.setAllowImagePlaceholder(); 974 request.setAllowImagePlaceholder();
959 ImageResource* image = ImageResource::fetch( 975 ImageResource* imageResource = ImageResource::fetch(
960 request, 976 request,
961 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 977 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
962 EXPECT_EQ(FetchRequest::AllowPlaceholder, 978 EXPECT_EQ(FetchRequest::AllowPlaceholder,
963 request.placeholderImageRequestType()); 979 request.placeholderImageRequestType());
964 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); 980 EXPECT_EQ("bytes=0-2047",
965 EXPECT_TRUE(image->isPlaceholder()); 981 imageResource->resourceRequest().httpHeaderField("range"));
982 EXPECT_TRUE(imageResource->isPlaceholder());
966 Persistent<MockImageResourceClient> client = 983 Persistent<MockImageResourceClient> client =
967 new MockImageResourceClient(image); 984 new MockImageResourceClient(imageResource);
968 985
969 ResourceResponse response(testURL, "image/jpeg", 986 ResourceResponse response(testURL, "image/jpeg",
970 kJpegImageSubrangeWithDimensionsLength, nullAtom, 987 kJpegImageSubrangeWithDimensionsLength, nullAtom,
971 String()); 988 String());
972 response.setHTTPStatusCode(206); 989 response.setHTTPStatusCode(206);
973 response.setHTTPHeaderField( 990 response.setHTTPHeaderField(
974 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, 991 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength,
975 sizeof(kJpegImage))); 992 sizeof(kJpegImage)));
976 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); 993 imageResource->loader()->didReceiveResponse(
977 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 994 WrappedResourceResponse(response));
978 kJpegImageSubrangeWithDimensionsLength); 995 imageResource->loader()->didReceiveData(
979 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, 996 reinterpret_cast<const char*>(kJpegImage),
980 kJpegImageSubrangeWithDimensionsLength); 997 kJpegImageSubrangeWithDimensionsLength);
998 imageResource->loader()->didFinishLoading(
999 0.0, kJpegImageSubrangeWithDimensionsLength,
1000 kJpegImageSubrangeWithDimensionsLength);
981 1001
982 EXPECT_EQ(Resource::Cached, image->getStatus()); 1002 EXPECT_EQ(Resource::Cached, imageResource->getStatus());
983 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); 1003 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength,
984 EXPECT_TRUE(image->isPlaceholder()); 1004 imageResource->encodedSize());
1005 EXPECT_TRUE(imageResource->isPlaceholder());
985 EXPECT_LT(0, client->imageChangedCount()); 1006 EXPECT_LT(0, client->imageChangedCount());
986 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, 1007 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength,
987 client->encodedSizeOnLastImageChanged()); 1008 client->encodedSizeOnLastImageChanged());
988 EXPECT_TRUE(client->notifyFinishedCalled()); 1009 EXPECT_TRUE(client->notifyFinishedCalled());
989 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, 1010 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength,
990 client->encodedSizeOnNotifyFinished()); 1011 client->encodedSizeOnNotifyFinished());
991 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, 1012 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength,
992 client->encodedSizeOnImageNotifyFinished()); 1013 client->encodedSizeOnImageNotifyFinished());
993 1014
994 ASSERT_TRUE(image->getContent()->hasImage()); 1015 ASSERT_TRUE(imageResource->getContent()->hasImage());
995 EXPECT_EQ(1, image->getContent()->getImage()->width()); 1016 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
996 EXPECT_EQ(1, image->getContent()->getImage()->height()); 1017 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
997 EXPECT_FALSE(image->getContent()->getImage()->isBitmapImage()); 1018 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage());
998 EXPECT_FALSE(image->getContent()->getImage()->isSVGImage()); 1019 EXPECT_FALSE(imageResource->getContent()->getImage()->isSVGImage());
999 } 1020 }
1000 1021
1001 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { 1022 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) {
1002 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1023 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1003 ScopedRegisteredURL scopedRegisteredURL(testURL); 1024 ScopedRegisteredURL scopedRegisteredURL(testURL);
1004 1025
1005 FetchRequest request(testURL, FetchInitiatorInfo()); 1026 FetchRequest request(testURL, FetchInitiatorInfo());
1006 request.setAllowImagePlaceholder(); 1027 request.setAllowImagePlaceholder();
1007 ImageResource* image = ImageResource::fetch( 1028 ImageResource* imageResource = ImageResource::fetch(
1008 request, 1029 request,
1009 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 1030 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1010 EXPECT_EQ(FetchRequest::AllowPlaceholder, 1031 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1011 request.placeholderImageRequestType()); 1032 request.placeholderImageRequestType());
1012 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); 1033 EXPECT_EQ("bytes=0-2047",
1013 EXPECT_TRUE(image->isPlaceholder()); 1034 imageResource->resourceRequest().httpHeaderField("range"));
1035 EXPECT_TRUE(imageResource->isPlaceholder());
1014 Persistent<MockImageResourceClient> client = 1036 Persistent<MockImageResourceClient> client =
1015 new MockImageResourceClient(image); 1037 new MockImageResourceClient(imageResource);
1016 1038
1017 const char kBadData[] = "notanimageresponse"; 1039 const char kBadData[] = "notanimageresponse";
1018 1040
1019 image->loader()->didReceiveResponse(WrappedResourceResponse(ResourceResponse( 1041 imageResource->loader()->didReceiveResponse(
1020 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); 1042 WrappedResourceResponse(ResourceResponse(
1021 image->loader()->didReceiveData(kBadData, sizeof(kBadData)); 1043 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String())));
1044 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData));
1022 1045
1023 // The dimensions could not be extracted, so the full original image should be 1046 // The dimensions could not be extracted, so the full original image should be
1024 // loading. 1047 // loading.
1025 EXPECT_EQ(Resource::Pending, image->getStatus()); 1048 EXPECT_EQ(Resource::Pending, imageResource->getStatus());
1026 EXPECT_FALSE(image->isPlaceholder()); 1049 EXPECT_FALSE(imageResource->isPlaceholder());
1027 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1050 EXPECT_EQ(nullAtom,
1028 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), 1051 imageResource->resourceRequest().httpHeaderField("range"));
1029 static_cast<int>(image->resourceRequest().getCachePolicy())); 1052 EXPECT_EQ(
1053 static_cast<int>(WebCachePolicy::BypassingCache),
1054 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1030 EXPECT_FALSE(client->notifyFinishedCalled()); 1055 EXPECT_FALSE(client->notifyFinishedCalled());
1031 EXPECT_EQ(0, client->imageNotifyFinishedCount()); 1056 EXPECT_EQ(0, client->imageNotifyFinishedCount());
1032 1057
1033 image->loader()->didReceiveResponse(WrappedResourceResponse(ResourceResponse( 1058 imageResource->loader()->didReceiveResponse(
1034 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); 1059 WrappedResourceResponse(ResourceResponse(
1035 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 1060 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
1036 sizeof(kJpegImage)); 1061 imageResource->loader()->didReceiveData(
1037 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage), 1062 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
1038 sizeof(kJpegImage)); 1063 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1064 sizeof(kJpegImage));
1039 1065
1040 EXPECT_EQ(Resource::Cached, image->getStatus()); 1066 EXPECT_EQ(Resource::Cached, imageResource->getStatus());
1041 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); 1067 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize());
1042 EXPECT_FALSE(image->isPlaceholder()); 1068 EXPECT_FALSE(imageResource->isPlaceholder());
1043 EXPECT_LT(0, client->imageChangedCount()); 1069 EXPECT_LT(0, client->imageChangedCount());
1044 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 1070 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
1045 EXPECT_TRUE(client->notifyFinishedCalled()); 1071 EXPECT_TRUE(client->notifyFinishedCalled());
1046 EXPECT_EQ(1, client->imageNotifyFinishedCount()); 1072 EXPECT_EQ(1, client->imageNotifyFinishedCount());
1047 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); 1073 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
1048 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); 1074 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
1049 1075
1050 ASSERT_TRUE(image->getContent()->hasImage()); 1076 ASSERT_TRUE(imageResource->getContent()->hasImage());
1051 EXPECT_EQ(1, image->getContent()->getImage()->width()); 1077 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1052 EXPECT_EQ(1, image->getContent()->getImage()->height()); 1078 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
1053 EXPECT_TRUE(image->getContent()->getImage()->isBitmapImage()); 1079 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1054 } 1080 }
1055 1081
1056 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { 1082 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) {
1057 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1083 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1058 ScopedRegisteredURL scopedRegisteredURL(testURL); 1084 ScopedRegisteredURL scopedRegisteredURL(testURL);
1059 1085
1060 ResourceFetcher* fetcher = 1086 ResourceFetcher* fetcher =
1061 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1087 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1062 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1088 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
1063 placeholderRequest.setAllowImagePlaceholder(); 1089 placeholderRequest.setAllowImagePlaceholder();
1064 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); 1090 ImageResource* imageResource =
1091 ImageResource::fetch(placeholderRequest, fetcher);
1065 Persistent<MockImageResourceClient> client = 1092 Persistent<MockImageResourceClient> client =
1066 new MockImageResourceClient(image); 1093 new MockImageResourceClient(imageResource);
1067 1094
1068 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); 1095 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
1069 ImageResource* secondImage = 1096 ImageResource* secondImageResource =
1070 ImageResource::fetch(nonPlaceholderRequest, fetcher); 1097 ImageResource::fetch(nonPlaceholderRequest, fetcher);
1071 EXPECT_EQ(image, secondImage); 1098 EXPECT_EQ(imageResource, secondImageResource);
1072 EXPECT_EQ(Resource::Pending, image->getStatus()); 1099 EXPECT_EQ(Resource::Pending, imageResource->getStatus());
1073 EXPECT_FALSE(image->isPlaceholder()); 1100 EXPECT_FALSE(imageResource->isPlaceholder());
1074 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1101 EXPECT_EQ(nullAtom,
1075 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), 1102 imageResource->resourceRequest().httpHeaderField("range"));
1076 static_cast<int>(image->resourceRequest().getCachePolicy())); 1103 EXPECT_EQ(
1104 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
1105 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1077 EXPECT_FALSE(client->notifyFinishedCalled()); 1106 EXPECT_FALSE(client->notifyFinishedCalled());
1078 1107
1079 image->loader()->cancel(); 1108 imageResource->loader()->cancel();
1080 } 1109 }
1081 1110
1082 TEST(ImageResourceTest, 1111 TEST(ImageResourceTest,
1083 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { 1112 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) {
1084 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1113 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1085 ScopedRegisteredURL scopedRegisteredURL(testURL); 1114 ScopedRegisteredURL scopedRegisteredURL(testURL);
1086 1115
1087 ResourceFetcher* fetcher = 1116 ResourceFetcher* fetcher =
1088 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1117 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1089 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1118 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
1090 placeholderRequest.setAllowImagePlaceholder(); 1119 placeholderRequest.setAllowImagePlaceholder();
1091 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); 1120 ImageResource* imageResource =
1121 ImageResource::fetch(placeholderRequest, fetcher);
1092 Persistent<MockImageResourceClient> client = 1122 Persistent<MockImageResourceClient> client =
1093 new MockImageResourceClient(image); 1123 new MockImageResourceClient(imageResource);
1094 1124
1095 ResourceResponse response(testURL, "image/jpeg", 1125 ResourceResponse response(testURL, "image/jpeg",
1096 kJpegImageSubrangeWithDimensionsLength, nullAtom, 1126 kJpegImageSubrangeWithDimensionsLength, nullAtom,
1097 String()); 1127 String());
1098 response.setHTTPStatusCode(206); 1128 response.setHTTPStatusCode(206);
1099 response.setHTTPHeaderField( 1129 response.setHTTPHeaderField(
1100 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, 1130 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength,
1101 sizeof(kJpegImage))); 1131 sizeof(kJpegImage)));
1102 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); 1132 imageResource->loader()->didReceiveResponse(
1103 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 1133 WrappedResourceResponse(response));
1104 kJpegImageSubrangeWithDimensionsLength); 1134 imageResource->loader()->didReceiveData(
1105 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, 1135 reinterpret_cast<const char*>(kJpegImage),
1106 kJpegImageSubrangeWithDimensionsLength); 1136 kJpegImageSubrangeWithDimensionsLength);
1137 imageResource->loader()->didFinishLoading(
1138 0.0, kJpegImageSubrangeWithDimensionsLength,
1139 kJpegImageSubrangeWithDimensionsLength);
1107 1140
1108 EXPECT_EQ(Resource::Cached, image->getStatus()); 1141 EXPECT_EQ(Resource::Cached, imageResource->getStatus());
1109 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); 1142 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength,
1110 EXPECT_TRUE(image->isPlaceholder()); 1143 imageResource->encodedSize());
1144 EXPECT_TRUE(imageResource->isPlaceholder());
1111 EXPECT_LT(0, client->imageChangedCount()); 1145 EXPECT_LT(0, client->imageChangedCount());
1112 EXPECT_TRUE(client->notifyFinishedCalled()); 1146 EXPECT_TRUE(client->notifyFinishedCalled());
1113 1147
1114 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); 1148 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
1115 ImageResource* secondImage = 1149 ImageResource* secondImageResource =
1116 ImageResource::fetch(nonPlaceholderRequest, fetcher); 1150 ImageResource::fetch(nonPlaceholderRequest, fetcher);
1117 EXPECT_EQ(image, secondImage); 1151 EXPECT_EQ(imageResource, secondImageResource);
1118 EXPECT_EQ(Resource::Pending, image->getStatus()); 1152 EXPECT_EQ(Resource::Pending, imageResource->getStatus());
1119 EXPECT_FALSE(image->isPlaceholder()); 1153 EXPECT_FALSE(imageResource->isPlaceholder());
1120 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1154 EXPECT_EQ(nullAtom,
1121 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), 1155 imageResource->resourceRequest().httpHeaderField("range"));
1122 static_cast<int>(image->resourceRequest().getCachePolicy())); 1156 EXPECT_EQ(
1157 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
1158 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1123 1159
1124 image->loader()->cancel(); 1160 imageResource->loader()->cancel();
1125 } 1161 }
1126 1162
1127 TEST(ImageResourceTest, PeriodicFlushTest) { 1163 TEST(ImageResourceTest, PeriodicFlushTest) {
1128 TestingPlatformSupportWithMockScheduler platform; 1164 TestingPlatformSupportWithMockScheduler platform;
1129 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1165 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1130 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", 1166 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html",
1131 "text/html"); 1167 "text/html");
1132 ResourceRequest request = ResourceRequest(testURL); 1168 ResourceRequest request = ResourceRequest(testURL);
1133 ImageResource* cachedImage = ImageResource::create(request); 1169 ImageResource* imageResource = ImageResource::create(request);
1134 cachedImage->setStatus(Resource::Pending); 1170 imageResource->setStatus(Resource::Pending);
1135 1171
1136 Persistent<MockImageResourceClient> client = 1172 Persistent<MockImageResourceClient> client =
1137 new MockImageResourceClient(cachedImage); 1173 new MockImageResourceClient(imageResource);
1138 1174
1139 // Send the image response. 1175 // Send the image response.
1140 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage2), 1176 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage2),
1141 nullAtom, String()); 1177 nullAtom, String());
1142 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); 1178 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low");
1143 1179
1144 cachedImage->responseReceived(resourceResponse, nullptr); 1180 imageResource->responseReceived(resourceResponse, nullptr);
1145 1181
1146 // This is number is sufficiently large amount of bytes necessary for the 1182 // This is number is sufficiently large amount of bytes necessary for the
1147 // image to be created (since the size is known). This was determined by 1183 // image to be created (since the size is known). This was determined by
1148 // appending one byte at a time (with flushes) until the image was decoded. 1184 // appending one byte at a time (with flushes) until the image was decoded.
1149 size_t meaningfulImageSize = 280; 1185 size_t meaningfulImageSize = 280;
1150 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage2), 1186 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage2),
1151 meaningfulImageSize); 1187 meaningfulImageSize);
1152 size_t bytesSent = meaningfulImageSize; 1188 size_t bytesSent = meaningfulImageSize;
1153 1189
1154 EXPECT_FALSE(cachedImage->errorOccurred()); 1190 EXPECT_FALSE(imageResource->errorOccurred());
1155 EXPECT_TRUE(cachedImage->getContent()->hasImage()); 1191 EXPECT_TRUE(imageResource->getContent()->hasImage());
1156 EXPECT_EQ(1, client->imageChangedCount()); 1192 EXPECT_EQ(1, client->imageChangedCount());
1157 1193
1158 platform.runForPeriodSeconds(1.); 1194 platform.runForPeriodSeconds(1.);
1159 platform.advanceClockSeconds(1.); 1195 platform.advanceClockSeconds(1.);
1160 1196
1161 // Sanity check that we created an image after appending |meaningfulImageSize| 1197 // Sanity check that we created an image after appending |meaningfulImageSize|
1162 // bytes just once. 1198 // bytes just once.
1163 EXPECT_FALSE(cachedImage->errorOccurred()); 1199 EXPECT_FALSE(imageResource->errorOccurred());
1164 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 1200 ASSERT_TRUE(imageResource->getContent()->hasImage());
1165 EXPECT_EQ(1, client->imageChangedCount()); 1201 EXPECT_EQ(1, client->imageChangedCount());
1166 1202
1167 for (int flushCount = 1; flushCount <= 3; ++flushCount) { 1203 for (int flushCount = 1; flushCount <= 3; ++flushCount) {
1168 // For each of the iteration that appends data, we don't expect 1204 // For each of the iteration that appends data, we don't expect
1169 // |imageChangeCount()| to change, since the time is adjusted by 0.2001 1205 // |imageChangeCount()| to change, since the time is adjusted by 0.2001
1170 // seconds (it's greater than 0.2 to avoid double precision problems). 1206 // seconds (it's greater than 0.2 to avoid double precision problems).
1171 // After 5 appends, we breach the flush interval and the flush count 1207 // After 5 appends, we breach the flush interval and the flush count
1172 // increases. 1208 // increases.
1173 for (int i = 0; i < 5; ++i) { 1209 for (int i = 0; i < 5; ++i) {
1174 SCOPED_TRACE(i); 1210 SCOPED_TRACE(i);
1175 cachedImage->appendData( 1211 imageResource->appendData(
1176 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1); 1212 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1);
1177 1213
1178 EXPECT_FALSE(cachedImage->errorOccurred()); 1214 EXPECT_FALSE(imageResource->errorOccurred());
1179 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 1215 ASSERT_TRUE(imageResource->getContent()->hasImage());
1180 EXPECT_EQ(flushCount, client->imageChangedCount()); 1216 EXPECT_EQ(flushCount, client->imageChangedCount());
1181 1217
1182 ++bytesSent; 1218 ++bytesSent;
1183 platform.runForPeriodSeconds(0.2001); 1219 platform.runForPeriodSeconds(0.2001);
1184 } 1220 }
1185 } 1221 }
1186 1222
1187 // Increasing time by a large number only causes one extra flush. 1223 // Increasing time by a large number only causes one extra flush.
1188 platform.runForPeriodSeconds(10.); 1224 platform.runForPeriodSeconds(10.);
1189 platform.advanceClockSeconds(10.); 1225 platform.advanceClockSeconds(10.);
1190 EXPECT_FALSE(cachedImage->errorOccurred()); 1226 EXPECT_FALSE(imageResource->errorOccurred());
1191 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 1227 ASSERT_TRUE(imageResource->getContent()->hasImage());
1192 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 1228 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
1193 EXPECT_EQ(4, client->imageChangedCount()); 1229 EXPECT_EQ(4, client->imageChangedCount());
1194 1230
1195 // Append the rest of the data and finish (which causes another flush). 1231 // Append the rest of the data and finish (which causes another flush).
1196 cachedImage->appendData( 1232 imageResource->appendData(
1197 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1233 reinterpret_cast<const char*>(kJpegImage2) + bytesSent,
1198 sizeof(kJpegImage2) - bytesSent); 1234 sizeof(kJpegImage2) - bytesSent);
1199 cachedImage->finish(); 1235 imageResource->finish();
1200 1236
1201 EXPECT_FALSE(cachedImage->errorOccurred()); 1237 EXPECT_FALSE(imageResource->errorOccurred());
1202 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 1238 ASSERT_TRUE(imageResource->getContent()->hasImage());
1203 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 1239 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
1204 EXPECT_EQ(5, client->imageChangedCount()); 1240 EXPECT_EQ(5, client->imageChangedCount());
1205 EXPECT_TRUE(client->notifyFinishedCalled()); 1241 EXPECT_TRUE(client->notifyFinishedCalled());
1206 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 1242 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1207 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); 1243 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
1208 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); 1244 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
1209 1245
1210 WTF::setTimeFunctionsForTesting(nullptr); 1246 WTF::setTimeFunctionsForTesting(nullptr);
1211 } 1247 }
1212 1248
1213 } // namespace 1249 } // namespace
1214 } // namespace blink 1250 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698