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

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

Issue 2650543003: Refactor reloading tests in ImageResourceTest.cpp (Closed)
Patch Set: Rebase Created 3 years, 10 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 133 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01, 134 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 136 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00,
137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 138 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
139 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 139 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9}; 141 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9};
142 142
143 constexpr int kJpegImage2Width = 50;
144
145 const char kSvgImage[] = 143 const char kSvgImage[] =
146 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" " 144 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" "
147 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" 145 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">"
148 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" 146 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>"
149 "</svg>"; 147 "</svg>";
150 148
151 const char kSvgImage2[] = 149 const char kSvgImage2[] =
152 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" " 150 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" "
153 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" 151 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">"
154 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>" 152 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>"
155 "</svg>"; 153 "</svg>";
156 154
157 void receiveResponse(ImageResource* imageResource, 155 void receiveResponse(ImageResource* imageResource,
158 const KURL& url, 156 const KURL& url,
159 const AtomicString& mimeType, 157 const AtomicString& mimeType,
160 const char* data, 158 const char* data,
161 size_t dataSize) { 159 size_t dataSize) {
162 ResourceResponse response; 160 ResourceResponse response;
163 response.setURL(url); 161 response.setURL(url);
164 response.setHTTPStatusCode(200); 162 response.setHTTPStatusCode(200);
165 response.setMimeType(mimeType); 163 response.setMimeType(mimeType);
166 imageResource->responseReceived(response, nullptr); 164 imageResource->responseReceived(response, nullptr);
167 imageResource->appendData(data, dataSize); 165 imageResource->appendData(data, dataSize);
168 imageResource->finish(); 166 imageResource->finish();
169 } 167 }
170 168
169 void testThatReloadIsStartedAndDoReload(
Nate Chapin 2017/02/07 22:05:56 This name is a little weird: I would expect that "
hiroshige 2017/02/08 00:03:12 Nice suggestion! Done.
170 const KURL& testURL,
171 ImageResource* imageResource,
172 ImageResourceContent* content,
173 MockImageResourceObserver* observer,
174 bool alreadyNotifiedFinish,
175 WebCachePolicy policyForReload = WebCachePolicy::BypassingCache) {
176 const char* data = reinterpret_cast<const char*>(kJpegImage2);
177 const size_t dataLength = sizeof(kJpegImage2);
178 const int imageWidth = 50;
179 const int imageHeight = 50;
180
181 // Checks that |imageResource| and |content| are ready for non-placeholder
182 // reloading.
183 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
184 EXPECT_FALSE(imageResource->resourceBuffer());
185 EXPECT_FALSE(imageResource->isPlaceholder());
186 EXPECT_EQ(nullAtom,
187 imageResource->resourceRequest().httpHeaderField("range"));
188 EXPECT_EQ(
189 static_cast<int>(policyForReload),
190 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
191 EXPECT_EQ(content, imageResource->getContent());
192 EXPECT_FALSE(content->hasImage());
193
194 // Checks |observer| before reloading.
195 const int originalImageChangedCount = observer->imageChangedCount();
196 EXPECT_EQ(alreadyNotifiedFinish, observer->imageNotifyFinishedCalled());
197 const int imageWidthOnImageNotifyFinished =
198 observer->imageWidthOnImageNotifyFinished();
199
200 // Does Reload.
201 imageResource->loader()->didReceiveResponse(WrappedResourceResponse(
202 ResourceResponse(testURL, "image/jpeg", dataLength, nullAtom, String())));
203 imageResource->loader()->didReceiveData(data, dataLength);
204 imageResource->loader()->didFinishLoading(0.0, dataLength, dataLength);
205
206 // Checks |imageResource|'s status after reloading.
207 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus());
208 EXPECT_FALSE(imageResource->errorOccurred());
209 EXPECT_EQ(dataLength, imageResource->encodedSize());
210
211 // Checks |observer| after reloading that it is notified of updates/finish.
212 EXPECT_LT(originalImageChangedCount, observer->imageChangedCount());
213 EXPECT_EQ(imageWidth, observer->imageWidthOnLastImageChanged());
214 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
215 if (alreadyNotifiedFinish) {
216 EXPECT_EQ(imageWidthOnImageNotifyFinished,
217 observer->imageWidthOnImageNotifyFinished());
218 } else {
219 EXPECT_EQ(imageWidth, observer->imageWidthOnImageNotifyFinished());
220 }
221
222 // Checks |content| receives the correct image.
223 EXPECT_TRUE(content->hasImage());
224 EXPECT_FALSE(content->getImage()->isNull());
225 EXPECT_EQ(imageWidth, content->getImage()->width());
226 EXPECT_EQ(imageHeight, content->getImage()->height());
227 EXPECT_TRUE(content->getImage()->isBitmapImage());
228 }
229
171 class ImageResourceTestMockFetchContext : public FetchContext { 230 class ImageResourceTestMockFetchContext : public FetchContext {
172 public: 231 public:
173 static ImageResourceTestMockFetchContext* create() { 232 static ImageResourceTestMockFetchContext* create() {
174 return new ImageResourceTestMockFetchContext; 233 return new ImageResourceTestMockFetchContext;
175 } 234 }
176 235
177 virtual ~ImageResourceTestMockFetchContext() {} 236 virtual ~ImageResourceTestMockFetchContext() {}
178 237
179 bool allowImage(bool imagesEnabled, const KURL&) const override { 238 bool allowImage(bool imagesEnabled, const KURL&) const override {
180 return true; 239 return true;
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 // The observer should have been notified that the image load completed. 497 // The observer should have been notified that the image load completed.
439 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 498 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
440 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); 499 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
441 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 500 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
442 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); 501 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
443 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); 502 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
444 503
445 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. 504 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading.
446 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, 505 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher,
447 Resource::kReloadAlways); 506 Resource::kReloadAlways);
448 EXPECT_FALSE(imageResource->errorOccurred());
449 EXPECT_FALSE(imageResource->resourceBuffer());
450 EXPECT_FALSE(imageResource->getContent()->hasImage());
451 EXPECT_EQ(3, observer->imageChangedCount()); 507 EXPECT_EQ(3, observer->imageChangedCount());
452 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
453 508
454 imageResource->loader()->didReceiveResponse( 509 testThatReloadIsStartedAndDoReload(testURL, imageResource,
455 WrappedResourceResponse(resourceResponse), nullptr); 510 imageResource->getContent(),
456 imageResource->loader()->didReceiveData( 511 observer.get(), true);
457 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2));
458 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
459 sizeof(kJpegImage2));
460 EXPECT_FALSE(imageResource->errorOccurred());
461 ASSERT_TRUE(imageResource->getContent()->hasImage());
462 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
463 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged());
464 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
465
466 // The observer should not have been notified of completion again.
467 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
468
469 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
470 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
471 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
472 } 512 }
473 513
474 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) { 514 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) {
475 ResourceFetcher* fetcher = 515 ResourceFetcher* fetcher =
476 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 516 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
477 517
478 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest2.html"); 518 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest2.html");
479 ScopedRegisteredURL scopedRegisteredURL(testURL); 519 ScopedRegisteredURL scopedRegisteredURL(testURL);
480 520
481 ResourceRequest request = ResourceRequest(testURL); 521 ResourceRequest request = ResourceRequest(testURL);
(...skipping 15 matching lines...) Expand all
497 WrappedResourceResponse(resourceResponse)); 537 WrappedResourceResponse(resourceResponse));
498 imageResource->loader()->didReceiveData( 538 imageResource->loader()->didReceiveData(
499 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); 539 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
500 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), 540 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
501 sizeof(kJpegImage)); 541 sizeof(kJpegImage));
502 542
503 EXPECT_EQ(imageResource, fetcher->cachedResource(testURL)); 543 EXPECT_EQ(imageResource, fetcher->cachedResource(testURL));
504 544
505 fetcher->reloadLoFiImages(); 545 fetcher->reloadLoFiImages();
506 546
507 EXPECT_FALSE(imageResource->errorOccurred()); 547 testThatReloadIsStartedAndDoReload(testURL, imageResource, content,
508 EXPECT_FALSE(imageResource->resourceBuffer()); 548 observer.get(), true);
509 EXPECT_FALSE(imageResource->getContent()->hasImage());
510 EXPECT_EQ(3, observer->imageChangedCount());
511 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
512
513 imageResource->loader()->didReceiveResponse(
514 WrappedResourceResponse(resourceResponse), nullptr);
515 imageResource->loader()->didReceiveData(
516 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2));
517 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
518 sizeof(kJpegImage2));
519 EXPECT_FALSE(imageResource->errorOccurred());
520 ASSERT_TRUE(imageResource->getContent()->hasImage());
521 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
522 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged());
523 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
524
525 // The observer should not have been notified of completion again.
526 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
527
528 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
529 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
530 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
531 } 549 }
532 550
533 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { 551 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
534 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 552 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
535 ScopedRegisteredURL scopedRegisteredURL(testURL); 553 ScopedRegisteredURL scopedRegisteredURL(testURL);
536 554
537 ResourceRequest request(testURL); 555 ResourceRequest request(testURL);
538 request.setPreviewsState(WebURLRequest::ServerLoFiOn); 556 request.setPreviewsState(WebURLRequest::ServerLoFiOn);
539 FetchRequest fetchRequest(request, FetchInitiatorInfo()); 557 FetchRequest fetchRequest(request, FetchInitiatorInfo());
540 ResourceFetcher* fetcher = 558 ResourceFetcher* fetcher =
(...skipping 19 matching lines...) Expand all
560 EXPECT_EQ(1, observer->imageChangedCount()); 578 EXPECT_EQ(1, observer->imageChangedCount());
561 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); 579 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
562 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); 580 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
563 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 581 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
564 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); 582 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
565 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); 583 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
566 584
567 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. 585 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading.
568 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, 586 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher,
569 Resource::kReloadAlways); 587 Resource::kReloadAlways);
570 EXPECT_FALSE(imageResource->errorOccurred()); 588
571 EXPECT_FALSE(imageResource->resourceBuffer());
572 EXPECT_FALSE(imageResource->getContent()->hasImage());
573 EXPECT_EQ(2, observer->imageChangedCount()); 589 EXPECT_EQ(2, observer->imageChangedCount());
574 EXPECT_EQ(0, observer->imageWidthOnLastImageChanged()); 590 EXPECT_EQ(0, observer->imageWidthOnLastImageChanged());
575 // The observer should not have been notified of completion yet, since the 591 // The observer should not have been notified of completion yet, since the
576 // image 592 // image is still loading.
577 // is still loading.
578 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); 593 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
579 594
580 imageResource->loader()->didReceiveResponse( 595 testThatReloadIsStartedAndDoReload(testURL, imageResource,
581 WrappedResourceResponse(ResourceResponse( 596 imageResource->getContent(),
582 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), 597 observer.get(), false);
583 nullptr);
584 imageResource->loader()->didReceiveData(
585 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2));
586 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
587 sizeof(kJpegImage2));
588
589 EXPECT_FALSE(imageResource->errorOccurred());
590 ASSERT_TRUE(imageResource->getContent()->hasImage());
591 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
592 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged());
593 // The observer should have been notified of completion only after the reload
594 // completed.
595 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
596 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnImageNotifyFinished());
597 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
598 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
599 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
600 } 598 }
601 599
602 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { 600 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) {
603 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 601 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
604 ScopedRegisteredURL scopedRegisteredURL(testURL); 602 ScopedRegisteredURL scopedRegisteredURL(testURL);
605 603
606 ResourceFetcher* fetcher = 604 ResourceFetcher* fetcher =
607 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 605 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
608 FetchRequest request(testURL, FetchInitiatorInfo()); 606 FetchRequest request(testURL, FetchInitiatorInfo());
609 request.setAllowImagePlaceholder(); 607 request.setAllowImagePlaceholder();
(...skipping 16 matching lines...) Expand all
626 WrappedResourceResponse(response)); 624 WrappedResourceResponse(response));
627 imageResource->loader()->didReceiveData( 625 imageResource->loader()->didReceiveData(
628 reinterpret_cast<const char*>(kJpegImage), 626 reinterpret_cast<const char*>(kJpegImage),
629 kJpegImageSubrangeWithDimensionsLength); 627 kJpegImageSubrangeWithDimensionsLength);
630 imageResource->loader()->didFinishLoading( 628 imageResource->loader()->didFinishLoading(
631 0.0, kJpegImageSubrangeWithDimensionsLength, 629 0.0, kJpegImageSubrangeWithDimensionsLength,
632 kJpegImageSubrangeWithDimensionsLength); 630 kJpegImageSubrangeWithDimensionsLength);
633 631
634 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); 632 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus());
635 EXPECT_TRUE(imageResource->isPlaceholder()); 633 EXPECT_TRUE(imageResource->isPlaceholder());
634 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
636 635
637 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, 636 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher,
638 Resource::kReloadAlways); 637 Resource::kReloadAlways);
639 638
640 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); 639 testThatReloadIsStartedAndDoReload(testURL, imageResource,
641 EXPECT_FALSE(imageResource->isPlaceholder()); 640 imageResource->getContent(),
642 EXPECT_EQ(nullAtom, 641 observer.get(), true);
643 imageResource->resourceRequest().httpHeaderField("range"));
644 EXPECT_EQ(
645 static_cast<int>(WebCachePolicy::BypassingCache),
646 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
647
648 imageResource->loader()->cancel();
649 } 642 }
650 643
651 TEST(ImageResourceTest, SVGImage) { 644 TEST(ImageResourceTest, SVGImage) {
652 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); 645 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo");
653 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); 646 ImageResource* imageResource = ImageResource::create(ResourceRequest(url));
654 std::unique_ptr<MockImageResourceObserver> observer = 647 std::unique_ptr<MockImageResourceObserver> observer =
655 MockImageResourceObserver::create(imageResource->getContent()); 648 MockImageResourceObserver::create(imageResource->getContent());
656 649
657 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, 650 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage,
658 strlen(kSvgImage)); 651 strlen(kSvgImage));
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
1178 1171
1179 imageResource->loader()->didReceiveResponse( 1172 imageResource->loader()->didReceiveResponse(
1180 WrappedResourceResponse(badResponse)); 1173 WrappedResourceResponse(badResponse));
1181 1174
1182 EXPECT_EQ(0, observer->imageChangedCount()); 1175 EXPECT_EQ(0, observer->imageChangedCount());
1183 1176
1184 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData)); 1177 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData));
1185 1178
1186 // The dimensions could not be extracted, so the full original image should be 1179 // The dimensions could not be extracted, so the full original image should be
1187 // loading. 1180 // loading.
1188 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
1189 EXPECT_FALSE(imageResource->isPlaceholder());
1190 EXPECT_EQ(nullAtom,
1191 imageResource->resourceRequest().httpHeaderField("range"));
1192 EXPECT_EQ(
1193 static_cast<int>(WebCachePolicy::BypassingCache),
1194 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1195 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); 1181 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
1196 EXPECT_EQ(3, observer->imageChangedCount()); 1182 EXPECT_EQ(3, observer->imageChangedCount());
1197 1183
1198 imageResource->loader()->didReceiveResponse( 1184 testThatReloadIsStartedAndDoReload(testURL, imageResource,
1199 WrappedResourceResponse(ResourceResponse( 1185 imageResource->getContent(),
1200 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); 1186 observer.get(), false);
1201 imageResource->loader()->didReceiveData(
1202 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
1203 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1204 sizeof(kJpegImage));
1205
1206 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus());
1207 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize());
1208 EXPECT_FALSE(imageResource->isPlaceholder());
1209 EXPECT_LT(3, observer->imageChangedCount());
1210 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
1211 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1212 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
1213
1214 ASSERT_TRUE(imageResource->getContent()->hasImage());
1215 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1216 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
hiroshige 2017/02/06 23:10:34 This test used kJpegImage as the reloaded image (a
1217 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1218 } 1187 }
1219 1188
1220 TEST(ImageResourceTest, FetchAllowPlaceholderPartialContentWithoutDimensions) { 1189 TEST(ImageResourceTest, FetchAllowPlaceholderPartialContentWithoutDimensions) {
1221 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1190 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1222 ScopedRegisteredURL scopedRegisteredURL(testURL); 1191 ScopedRegisteredURL scopedRegisteredURL(testURL);
1223 1192
1224 FetchRequest request(testURL, FetchInitiatorInfo()); 1193 FetchRequest request(testURL, FetchInitiatorInfo());
1225 request.setAllowImagePlaceholder(); 1194 request.setAllowImagePlaceholder();
1226 ImageResource* imageResource = ImageResource::fetch( 1195 ImageResource* imageResource = ImageResource::fetch(
1227 request, 1196 request,
(...skipping 20 matching lines...) Expand all
1248 imageResource->loader()->didReceiveData( 1217 imageResource->loader()->didReceiveData(
1249 reinterpret_cast<const char*>(kJpegImage), 1218 reinterpret_cast<const char*>(kJpegImage),
1250 kJpegImageSubrangeWithoutDimensionsLength); 1219 kJpegImageSubrangeWithoutDimensionsLength);
1251 1220
1252 EXPECT_EQ(0, observer->imageChangedCount()); 1221 EXPECT_EQ(0, observer->imageChangedCount());
1253 1222
1254 imageResource->loader()->didFinishLoading( 1223 imageResource->loader()->didFinishLoading(
1255 0.0, kJpegImageSubrangeWithoutDimensionsLength, 1224 0.0, kJpegImageSubrangeWithoutDimensionsLength,
1256 kJpegImageSubrangeWithoutDimensionsLength); 1225 kJpegImageSubrangeWithoutDimensionsLength);
1257 1226
1258 // Decode error didn't occur but the dimensions could not be extracted,
1259 // so the full original image should be loading.
1260 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
1261 EXPECT_FALSE(imageResource->isPlaceholder());
1262 EXPECT_EQ(nullAtom,
1263 imageResource->resourceRequest().httpHeaderField("range"));
1264 EXPECT_EQ(
1265 static_cast<int>(WebCachePolicy::BypassingCache),
1266 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1267 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
1268 EXPECT_EQ(2, observer->imageChangedCount()); 1227 EXPECT_EQ(2, observer->imageChangedCount());
1269 1228
1270 imageResource->loader()->didReceiveResponse( 1229 testThatReloadIsStartedAndDoReload(testURL, imageResource,
1271 WrappedResourceResponse(ResourceResponse( 1230 imageResource->getContent(),
1272 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); 1231 observer.get(), false);
1273 imageResource->loader()->didReceiveData(
1274 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
1275 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1276 sizeof(kJpegImage));
1277
1278 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus());
1279 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize());
1280 EXPECT_FALSE(imageResource->isPlaceholder());
1281 EXPECT_LT(0, observer->imageChangedCount());
1282 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
1283 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1284 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
1285
1286 ASSERT_TRUE(imageResource->getContent()->hasImage());
1287 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1288 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
hiroshige 2017/02/06 23:10:34 ditto.
1289 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1290 } 1232 }
1291 1233
1292 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { 1234 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) {
1293 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1235 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1294 ScopedRegisteredURL scopedRegisteredURL(testURL); 1236 ScopedRegisteredURL scopedRegisteredURL(testURL);
1295 1237
1296 ResourceFetcher* fetcher = 1238 ResourceFetcher* fetcher =
1297 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1239 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1298 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1240 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
1299 placeholderRequest.setAllowImagePlaceholder(); 1241 placeholderRequest.setAllowImagePlaceholder();
1300 ImageResource* imageResource = 1242 ImageResource* imageResource =
1301 ImageResource::fetch(placeholderRequest, fetcher); 1243 ImageResource::fetch(placeholderRequest, fetcher);
1302 std::unique_ptr<MockImageResourceObserver> observer = 1244 std::unique_ptr<MockImageResourceObserver> observer =
1303 MockImageResourceObserver::create(imageResource->getContent()); 1245 MockImageResourceObserver::create(imageResource->getContent());
1304 1246
1305 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); 1247 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
1306 ImageResource* secondImageResource = 1248 ImageResource* secondImageResource =
1307 ImageResource::fetch(nonPlaceholderRequest, fetcher); 1249 ImageResource::fetch(nonPlaceholderRequest, fetcher);
1308 EXPECT_EQ(imageResource, secondImageResource); 1250 EXPECT_EQ(imageResource, secondImageResource);
1309 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
1310 EXPECT_FALSE(imageResource->isPlaceholder());
1311 EXPECT_EQ(nullAtom,
1312 imageResource->resourceRequest().httpHeaderField("range"));
1313 EXPECT_EQ(
1314 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
1315 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1316 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
1317 1251
1318 imageResource->loader()->cancel(); 1252 testThatReloadIsStartedAndDoReload(
1253 testURL, imageResource, imageResource->getContent(), observer.get(),
1254 false, WebCachePolicy::UseProtocolCachePolicy);
1319 } 1255 }
1320 1256
1321 TEST(ImageResourceTest, 1257 TEST(ImageResourceTest,
1322 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { 1258 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) {
1323 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1259 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1324 ScopedRegisteredURL scopedRegisteredURL(testURL); 1260 ScopedRegisteredURL scopedRegisteredURL(testURL);
1325 1261
1326 ResourceFetcher* fetcher = 1262 ResourceFetcher* fetcher =
1327 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1263 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1328 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1264 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
(...skipping 23 matching lines...) Expand all
1352 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, 1288 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength,
1353 imageResource->encodedSize()); 1289 imageResource->encodedSize());
1354 EXPECT_TRUE(imageResource->isPlaceholder()); 1290 EXPECT_TRUE(imageResource->isPlaceholder());
1355 EXPECT_LT(0, observer->imageChangedCount()); 1291 EXPECT_LT(0, observer->imageChangedCount());
1356 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 1292 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1357 1293
1358 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); 1294 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
1359 ImageResource* secondImageResource = 1295 ImageResource* secondImageResource =
1360 ImageResource::fetch(nonPlaceholderRequest, fetcher); 1296 ImageResource::fetch(nonPlaceholderRequest, fetcher);
1361 EXPECT_EQ(imageResource, secondImageResource); 1297 EXPECT_EQ(imageResource, secondImageResource);
1362 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
1363 EXPECT_FALSE(imageResource->isPlaceholder());
1364 EXPECT_EQ(nullAtom,
1365 imageResource->resourceRequest().httpHeaderField("range"));
1366 EXPECT_EQ(
1367 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
1368 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1369 1298
1370 imageResource->loader()->cancel(); 1299 testThatReloadIsStartedAndDoReload(
1300 testURL, imageResource, imageResource->getContent(), observer.get(), true,
1301 WebCachePolicy::UseProtocolCachePolicy);
1371 } 1302 }
1372 1303
1373 TEST(ImageResourceTest, FetchAllowPlaceholderFullResponseDecodeSuccess) { 1304 TEST(ImageResourceTest, FetchAllowPlaceholderFullResponseDecodeSuccess) {
1374 const struct { 1305 const struct {
1375 int statusCode; 1306 int statusCode;
1376 AtomicString contentRange; 1307 AtomicString contentRange;
1377 } tests[] = { 1308 } tests[] = {
1378 {200, nullAtom}, 1309 {200, nullAtom},
1379 {404, nullAtom}, 1310 {404, nullAtom},
1380 {206, buildContentRange(sizeof(kJpegImage), sizeof(kJpegImage))}, 1311 {206, buildContentRange(sizeof(kJpegImage), sizeof(kJpegImage))},
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1493 ResourceResponse response(testURL, "image/jpeg", sizeof(kBadImageData), 1424 ResourceResponse response(testURL, "image/jpeg", sizeof(kBadImageData),
1494 nullAtom, String()); 1425 nullAtom, String());
1495 response.setHTTPStatusCode(statusCode); 1426 response.setHTTPStatusCode(statusCode);
1496 imageResource->loader()->didReceiveResponse( 1427 imageResource->loader()->didReceiveResponse(
1497 WrappedResourceResponse(response)); 1428 WrappedResourceResponse(response));
1498 imageResource->loader()->didReceiveData(kBadImageData, 1429 imageResource->loader()->didReceiveData(kBadImageData,
1499 sizeof(kBadImageData)); 1430 sizeof(kBadImageData));
1500 1431
1501 // The dimensions could not be extracted, and the response code was a 4xx 1432 // The dimensions could not be extracted, and the response code was a 4xx
1502 // error, so the full original image should be loading. 1433 // error, so the full original image should be loading.
1503 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); 1434 testThatReloadIsStartedAndDoReload(testURL, imageResource,
1504 EXPECT_FALSE(imageResource->isPlaceholder()); 1435 imageResource->getContent(),
1505 EXPECT_EQ(nullAtom, 1436 observer.get(), false);
1506 imageResource->resourceRequest().httpHeaderField("range"));
1507 EXPECT_EQ(
1508 static_cast<int>(WebCachePolicy::BypassingCache),
1509 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1510 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
1511
1512 imageResource->loader()->didReceiveResponse(
1513
1514 WrappedResourceResponse(ResourceResponse(
1515 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
1516 imageResource->loader()->didReceiveData(
1517 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
1518 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1519 sizeof(kJpegImage));
1520
1521 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus());
1522 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize());
1523 EXPECT_FALSE(imageResource->isPlaceholder());
1524 EXPECT_LT(0, observer->imageChangedCount());
1525 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
1526 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1527 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
1528
1529 ASSERT_TRUE(imageResource->getContent()->hasImage());
1530 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1531 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
hiroshige 2017/02/06 23:10:34 ditto.
1532 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1533 } 1437 }
1534 } 1438 }
1535 1439
1536 TEST(ImageResourceTest, PeriodicFlushTest) { 1440 TEST(ImageResourceTest, PeriodicFlushTest) {
1537 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> 1441 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
1538 platform; 1442 platform;
1539 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1443 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1540 ScopedRegisteredURL scopedRegisteredURL(testURL); 1444 ScopedRegisteredURL scopedRegisteredURL(testURL);
1541 ResourceRequest request = ResourceRequest(testURL); 1445 ResourceRequest request = ResourceRequest(testURL);
1542 ImageResource* imageResource = ImageResource::create(request); 1446 ImageResource* imageResource = ImageResource::create(request);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1614 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 1518 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1615 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 1519 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1616 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); 1520 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
1617 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); 1521 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
1618 1522
1619 WTF::setTimeFunctionsForTesting(nullptr); 1523 WTF::setTimeFunctionsForTesting(nullptr);
1620 } 1524 }
1621 1525
1622 } // namespace 1526 } // namespace
1623 } // namespace blink 1527 } // 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