summaryrefslogtreecommitdiff
path: root/chromium/third_party/WebKit/Source/platform/graphics/BitmapImageTest.cpp
blob: 89c576f91103195b0d5fc6a09b70176de9408c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
 * Copyright (c) 2013, Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "platform/graphics/BitmapImage.h"

#include "base/test/simple_test_tick_clock.h"
#include "platform/SharedBuffer.h"
#include "platform/graphics/BitmapImageMetrics.h"
#include "platform/graphics/DeferredImageDecoder.h"
#include "platform/graphics/ImageObserver.h"
#include "platform/graphics/test/MockImageDecoder.h"
#include "platform/scheduler/test/fake_web_task_runner.h"
#include "platform/testing/HistogramTester.h"
#include "platform/testing/RuntimeEnabledFeaturesTestHelpers.h"
#include "platform/testing/TestingPlatformSupportWithMockScheduler.h"
#include "platform/testing/UnitTestHelpers.h"
#include "platform/wtf/StdLibExtras.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkImage.h"

namespace blink {

class BitmapImageTest : public ::testing::Test {
 public:
  class FakeImageObserver : public GarbageCollectedFinalized<FakeImageObserver>,
                            public ImageObserver {
    USING_GARBAGE_COLLECTED_MIXIN(FakeImageObserver);

   public:
    FakeImageObserver()
        : last_decoded_size_(0), last_decoded_size_changed_delta_(0) {}

    virtual void DecodedSizeChangedTo(const Image*, size_t new_size) {
      last_decoded_size_changed_delta_ =
          SafeCast<int>(new_size) - SafeCast<int>(last_decoded_size_);
      last_decoded_size_ = new_size;
    }
    bool ShouldPauseAnimation(const Image*) override { return false; }
    void AnimationAdvanced(const Image*) override {
      animation_advanced_ = true;
    }
    void AsyncLoadCompleted(const Image*) override { NOTREACHED(); }

    virtual void ChangedInRect(const Image*, const IntRect&) {}

    size_t last_decoded_size_;
    int last_decoded_size_changed_delta_;
    bool animation_advanced_ = false;
  };

  static scoped_refptr<SharedBuffer> ReadFile(const char* file_name) {
    String file_path = testing::BlinkRootDir();
    file_path.append(file_name);
    return testing::ReadFromFile(file_path);
  }

  // Accessors to BitmapImage's protected methods.
  void DestroyDecodedData() { image_->DestroyDecodedData(); }
  size_t FrameCount() { return image_->FrameCount(); }
  void FrameAtIndex(size_t index) { image_->FrameAtIndex(index); }
  void SetCurrentFrame(size_t frame) { image_->current_frame_index_ = frame; }
  size_t FrameDecodedSize(size_t frame) {
    return image_->frames_[frame].frame_bytes_;
  }
  size_t DecodedFramesCount() const { return image_->frames_.size(); }
  void StartAnimation() { image_->StartAnimation(); }

  void SetFirstFrameNotComplete() { image_->frames_[0].is_complete_ = false; }

  void LoadImage(const char* file_name, bool load_all_frames = true) {
    scoped_refptr<SharedBuffer> image_data = ReadFile(file_name);
    ASSERT_TRUE(image_data.get());

    image_->SetData(image_data, true);
    EXPECT_EQ(0u, DecodedSize());

    size_t frame_count = image_->FrameCount();
    if (load_all_frames) {
      for (size_t i = 0; i < frame_count; ++i)
        FrameAtIndex(i);
    }
  }

  size_t DecodedSize() {
    // In the context of this test, the following loop will give the correct
    // result, but only because the test forces all frames to be decoded in
    // loadImage() above. There is no general guarantee that frameDecodedSize()
    // is up to date. Because of how multi frame images (like GIF) work,
    // requesting one frame to be decoded may require other previous frames to
    // be decoded as well. In those cases frameDecodedSize() wouldn't return the
    // correct thing for the previous frame because the decoded size wouldn't
    // have propagated upwards to the BitmapImage frame cache.
    size_t size = 0;
    for (size_t i = 0; i < DecodedFramesCount(); ++i)
      size += FrameDecodedSize(i);
    return size;
  }

  void AdvanceAnimation() { image_->AdvanceAnimation(nullptr); }

  int RepetitionCount() { return image_->RepetitionCount(); }

  int AnimationFinished() { return image_->animation_finished_; }

  scoped_refptr<Image> ImageForDefaultFrame() {
    return image_->ImageForDefaultFrame();
  }

  int LastDecodedSizeChange() {
    return image_observer_->last_decoded_size_changed_delta_;
  }

  scoped_refptr<SharedBuffer> Data() { return image_->Data(); }

 protected:
  void SetUp() override {
    image_observer_ = new FakeImageObserver;
    image_ = BitmapImage::Create(image_observer_.Get());
  }

  Persistent<FakeImageObserver> image_observer_;
  scoped_refptr<BitmapImage> image_;
  ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
      platform_;
};

TEST_F(BitmapImageTest, destroyDecodedData) {
  LoadImage("/LayoutTests/images/resources/animated-10color.gif");
  size_t total_size = DecodedSize();
  EXPECT_GT(total_size, 0u);
  DestroyDecodedData();
  EXPECT_EQ(-static_cast<int>(total_size), LastDecodedSizeChange());
  EXPECT_EQ(0u, DecodedSize());
}

TEST_F(BitmapImageTest, maybeAnimated) {
  LoadImage("/LayoutTests/images/resources/gif-loop-count.gif");
  for (size_t i = 0; i < FrameCount(); ++i) {
    EXPECT_TRUE(image_->MaybeAnimated());
    AdvanceAnimation();
  }
  EXPECT_FALSE(image_->MaybeAnimated());
}

TEST_F(BitmapImageTest, animationRepetitions) {
  LoadImage("/LayoutTests/images/resources/full2loop.gif");
  int expected_repetition_count = 2;
  EXPECT_EQ(expected_repetition_count, RepetitionCount());

  // We actually loop once more than stored repetition count.
  for (int repeat = 0; repeat < expected_repetition_count + 1; ++repeat) {
    for (size_t i = 0; i < FrameCount(); ++i) {
      EXPECT_FALSE(AnimationFinished());
      AdvanceAnimation();
    }
  }
  EXPECT_TRUE(AnimationFinished());
}

TEST_F(BitmapImageTest, isAllDataReceived) {
  scoped_refptr<SharedBuffer> image_data =
      ReadFile("/LayoutTests/images/resources/green.jpg");
  ASSERT_TRUE(image_data.get());

  scoped_refptr<BitmapImage> image = BitmapImage::Create();
  EXPECT_FALSE(image->IsAllDataReceived());

  image->SetData(image_data, false);
  EXPECT_FALSE(image->IsAllDataReceived());

  image->SetData(image_data, true);
  EXPECT_TRUE(image->IsAllDataReceived());

  image->SetData(SharedBuffer::Create("data", sizeof("data")), false);
  EXPECT_FALSE(image->IsAllDataReceived());

  image->SetData(image_data, true);
  EXPECT_TRUE(image->IsAllDataReceived());
}

TEST_F(BitmapImageTest, noColorProfile) {
  LoadImage("/LayoutTests/images/resources/green.jpg");
  EXPECT_EQ(1u, DecodedFramesCount());
  EXPECT_EQ(1024u, DecodedSize());
  EXPECT_FALSE(image_->HasColorProfile());
}

TEST_F(BitmapImageTest, jpegHasColorProfile) {
  LoadImage("/LayoutTests/images/resources/icc-v2-gbr.jpg");
  EXPECT_EQ(1u, DecodedFramesCount());
  EXPECT_EQ(227700u, DecodedSize());
  EXPECT_TRUE(image_->HasColorProfile());
}

TEST_F(BitmapImageTest, pngHasColorProfile) {
  LoadImage(
      "/LayoutTests/images/resources/"
      "palatted-color-png-gamma-one-color-profile.png");
  EXPECT_EQ(1u, DecodedFramesCount());
  EXPECT_EQ(65536u, DecodedSize());
  EXPECT_TRUE(image_->HasColorProfile());
}

TEST_F(BitmapImageTest, webpHasColorProfile) {
  LoadImage("/LayoutTests/images/resources/webp-color-profile-lossy.webp");
  EXPECT_EQ(1u, DecodedFramesCount());
  EXPECT_EQ(2560000u, DecodedSize());
  EXPECT_TRUE(image_->HasColorProfile());
}

TEST_F(BitmapImageTest, icoHasWrongFrameDimensions) {
  LoadImage("/LayoutTests/images/resources/wrong-frame-dimensions.ico");
  // This call would cause crash without fix for 408026
  ImageForDefaultFrame();
}

TEST_F(BitmapImageTest, correctDecodedDataSize) {
  // Requesting any one frame shouldn't result in decoding any other frames.
  LoadImage("/LayoutTests/images/resources/anim_none.gif", false);
  FrameAtIndex(1);
  int frame_size =
      static_cast<int>(image_->Size().Area() * sizeof(ImageFrame::PixelData));
  EXPECT_EQ(frame_size, LastDecodedSizeChange());
}

TEST_F(BitmapImageTest, recachingFrameAfterDataChanged) {
  LoadImage("/LayoutTests/images/resources/green.jpg");
  SetFirstFrameNotComplete();
  EXPECT_GT(LastDecodedSizeChange(), 0);
  image_observer_->last_decoded_size_changed_delta_ = 0;

  // Calling dataChanged causes the cache to flush, but doesn't affect the
  // source's decoded frames. It shouldn't affect decoded size.
  image_->DataChanged(true);
  EXPECT_EQ(0, LastDecodedSizeChange());
  // Recaching the first frame also shouldn't affect decoded size.
  image_->PaintImageForCurrentFrame();
  EXPECT_EQ(0, LastDecodedSizeChange());
}

TEST_F(BitmapImageTest, ConstantImageIdForPartiallyLoadedImages) {
  scoped_refptr<SharedBuffer> image_data =
      ReadFile("/LayoutTests/images/resources/green.jpg");
  ASSERT_TRUE(image_data.get());

  // Create a new buffer to partially supply the data.
  scoped_refptr<SharedBuffer> partial_buffer = SharedBuffer::Create();
  partial_buffer->Append(image_data->Data(), image_data->size() - 4);

  // First partial load. Repeated calls for a PaintImage should have the same
  // image until the data changes or the decoded data is destroyed.
  ASSERT_EQ(image_->SetData(partial_buffer, false), Image::kSizeAvailable);
  auto image1 = image_->PaintImageForCurrentFrame();
  auto image2 = image_->PaintImageForCurrentFrame();
  EXPECT_EQ(image1, image2);
  auto sk_image1 = image1.GetSkImage();
  auto sk_image2 = image2.GetSkImage();
  EXPECT_EQ(sk_image1->uniqueID(), sk_image2->uniqueID());

  // Frame keys should be the same for these PaintImages.
  EXPECT_EQ(image1.GetKeyForFrame(image1.frame_index()),
            image2.GetKeyForFrame(image2.frame_index()));
  EXPECT_EQ(image1.frame_index(), 0u);
  EXPECT_EQ(image2.frame_index(), 0u);

  // Destroy the decoded data. This generates a new id since we don't cache
  // image ids for partial decodes.
  DestroyDecodedData();
  auto image3 = image_->PaintImageForCurrentFrame();
  auto sk_image3 = image3.GetSkImage();
  EXPECT_NE(sk_image1, sk_image3);
  EXPECT_NE(sk_image1->uniqueID(), sk_image3->uniqueID());

  // Since the cached generator is discarded on destroying the cached decode,
  // the new content id is generated resulting in an updated frame key.
  EXPECT_NE(image1.GetKeyForFrame(image1.frame_index()),
            image3.GetKeyForFrame(image3.frame_index()));
  EXPECT_EQ(image3.frame_index(), 0u);

  // Load complete. This should generate a new image id.
  image_->SetData(image_data, true);
  auto complete_image = image_->PaintImageForCurrentFrame();
  auto complete_sk_image = complete_image.GetSkImage();
  EXPECT_NE(sk_image3, complete_sk_image);
  EXPECT_NE(sk_image3->uniqueID(), complete_sk_image->uniqueID());
  EXPECT_NE(complete_image.GetKeyForFrame(complete_image.frame_index()),
            image3.GetKeyForFrame(image3.frame_index()));
  EXPECT_EQ(complete_image.frame_index(), 0u);

  // Destroy the decoded data and re-create the PaintImage. The frame key
  // remains constant but the SkImage id will change since we don't cache skia
  // uniqueIDs.
  DestroyDecodedData();
  auto new_complete_image = image_->PaintImageForCurrentFrame();
  auto new_complete_sk_image = new_complete_image.GetSkImage();
  EXPECT_NE(new_complete_sk_image, complete_sk_image);
  EXPECT_EQ(new_complete_image.GetKeyForFrame(new_complete_image.frame_index()),
            complete_image.GetKeyForFrame(complete_image.frame_index()));
  EXPECT_EQ(new_complete_image.frame_index(), 0u);
}

TEST_F(BitmapImageTest, ImageForDefaultFrame_MultiFrame) {
  LoadImage("/LayoutTests/images/resources/anim_none.gif", false);

  // Multi-frame images create new StaticBitmapImages for each call.
  auto default_image1 = image_->ImageForDefaultFrame();
  auto default_image2 = image_->ImageForDefaultFrame();
  EXPECT_NE(default_image1, default_image2);

  // But the PaintImage should be the same.
  auto paint_image1 = default_image1->PaintImageForCurrentFrame();
  auto paint_image2 = default_image2->PaintImageForCurrentFrame();
  EXPECT_EQ(paint_image1, paint_image2);
  EXPECT_EQ(paint_image1.GetSkImage()->uniqueID(),
            paint_image2.GetSkImage()->uniqueID());
}

TEST_F(BitmapImageTest, ImageForDefaultFrame_SingleFrame) {
  LoadImage("/LayoutTests/images/resources/green.jpg");

  // Default frame images for single-frame cases is the image itself.
  EXPECT_EQ(image_->ImageForDefaultFrame(), image_);
}

class BitmapImageTestWithMockDecoder : public BitmapImageTest,
                                       public MockImageDecoderClient {
 public:
  void SetUp() override {
    BitmapImageTest::SetUp();
    image_->SetTickClockForTesting(&clock_);

    auto decoder = MockImageDecoder::Create(this);
    decoder->SetSize(10, 10);
    image_->SetDecoderForTesting(
        DeferredImageDecoder::CreateForTesting(std::move(decoder)));
  }

  void DecoderBeingDestroyed() override {}
  void DecodeRequested() override {}
  ImageFrame::Status GetStatus(size_t index) override {
    if (index < frame_count_ - 1 || last_frame_complete_)
      return ImageFrame::Status::kFrameComplete;
    return ImageFrame::Status::kFramePartial;
  }
  size_t FrameCount() override { return frame_count_; }
  int RepetitionCount() const override { return repetition_count_; }
  TimeDelta FrameDuration() const override { return duration_; }

  void SetClock(int seconds) {
    clock_.SetNowTicks(base::TimeTicks() + TimeDelta::FromSeconds(seconds));
  }

 protected:
  TimeDelta duration_;
  int repetition_count_;
  size_t frame_count_;
  bool last_frame_complete_;

  base::SimpleTestTickClock clock_;
};

TEST_F(BitmapImageTestWithMockDecoder, ImageMetadataTracking) {
  // For a zero duration, we should make it non-zero when creating a PaintImage.
  repetition_count_ = kAnimationLoopOnce;
  frame_count_ = 4u;
  last_frame_complete_ = false;
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), false);

  PaintImage image = image_->PaintImageForCurrentFrame();
  ASSERT_TRUE(image);
  EXPECT_EQ(image.FrameCount(), frame_count_);
  EXPECT_EQ(image.completion_state(),
            PaintImage::CompletionState::PARTIALLY_DONE);
  EXPECT_EQ(image.repetition_count(), repetition_count_);
  for (size_t i = 0; i < image.GetFrameMetadata().size(); ++i) {
    const auto& data = image.GetFrameMetadata()[i];
    EXPECT_EQ(data.duration, base::TimeDelta::FromMilliseconds(100));
    if (i == frame_count_ - 1 && !last_frame_complete_)
      EXPECT_FALSE(data.complete);
    else
      EXPECT_TRUE(data.complete);
  }

  // Now the load is finished.
  duration_ = TimeDelta::FromSeconds(1);
  repetition_count_ = kAnimationLoopInfinite;
  frame_count_ = 6u;
  last_frame_complete_ = true;
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), true);

  image = image_->PaintImageForCurrentFrame();
  ASSERT_TRUE(image);
  EXPECT_EQ(image.FrameCount(), frame_count_);
  EXPECT_EQ(image.completion_state(), PaintImage::CompletionState::DONE);
  EXPECT_EQ(image.repetition_count(), repetition_count_);
  for (size_t i = 0; i < image.GetFrameMetadata().size(); ++i) {
    const auto& data = image.GetFrameMetadata()[i];
    if (i < 4u)
      EXPECT_EQ(data.duration, base::TimeDelta::FromMilliseconds(100));
    else
      EXPECT_EQ(data.duration, base::TimeDelta::FromSeconds(1));
    EXPECT_TRUE(data.complete);
  }
};

TEST_F(BitmapImageTestWithMockDecoder, AnimationPolicyOverride) {
  repetition_count_ = kAnimationLoopInfinite;
  frame_count_ = 4u;
  last_frame_complete_ = true;
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), false);

  PaintImage image = image_->PaintImageForCurrentFrame();
  EXPECT_EQ(image.repetition_count(), repetition_count_);

  // Only one loop allowed.
  image_->SetAnimationPolicy(kImageAnimationPolicyAnimateOnce);
  image = image_->PaintImageForCurrentFrame();
  EXPECT_EQ(image.repetition_count(), kAnimationLoopOnce);

  // No animation allowed.
  image_->SetAnimationPolicy(kImageAnimationPolicyNoAnimation);
  image = image_->PaintImageForCurrentFrame();
  EXPECT_EQ(image.repetition_count(), kAnimationNone);

  // Default policy.
  image_->SetAnimationPolicy(kImageAnimationPolicyAllowed);
  image = image_->PaintImageForCurrentFrame();
  EXPECT_EQ(image.repetition_count(), repetition_count_);
}

TEST_F(BitmapImageTestWithMockDecoder, DontAdvanceToIncompleteFrame) {
  ScopedCompositorImageAnimationsForTest compositor_image_animations(false);

  repetition_count_ = kAnimationLoopOnce;
  frame_count_ = 3u;
  last_frame_complete_ = false;
  duration_ = TimeDelta::FromSeconds(10);
  SetClock(10);

  // Last frame of the image is incomplete for a completely loaded image. We
  // still won't advance it.
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), true);

  scoped_refptr<scheduler::FakeWebTaskRunner> task_runner =
      base::MakeRefCounted<scheduler::FakeWebTaskRunner>();
  image_->SetTaskRunnerForTesting(task_runner);
  task_runner->SetTime(10);

  // Start the animation at 10s. This should schedule a timer for the next frame
  // after 10 seconds.
  StartAnimation();
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 0u);

  // Run the timer task, image advanced to the second frame.
  SetClock(20);
  task_runner->AdvanceTimeAndRun(10);
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 1u);

  // Go past the desired time for the next frame, call StartAnimation. The frame
  // still isn't advanced because its incomplete.
  SetClock(45);
  StartAnimation();
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 1u);
}

TEST_F(BitmapImageTestWithMockDecoder, FrameSkipTracking) {
  ScopedCompositorImageAnimationsForTest compositor_image_animations(false);

  repetition_count_ = kAnimationLoopOnce;
  frame_count_ = 7u;
  last_frame_complete_ = false;
  duration_ = TimeDelta::FromSeconds(10);
  SetClock(10);

  // Start with an image that is incomplete, and the last frame is not fully
  // received.
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), false);

  scoped_refptr<scheduler::FakeWebTaskRunner> task_runner =
      base::MakeRefCounted<scheduler::FakeWebTaskRunner>();
  image_->SetTaskRunnerForTesting(task_runner);
  task_runner->SetTime(10);

  // Start the animation at 10s. This should schedule a timer for the next frame
  // after 10 seconds.
  EXPECT_FALSE(image_observer_->animation_advanced_);
  StartAnimation();
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 0u);

  // No frames skipped since we just started the animation.
  EXPECT_EQ(image_->last_num_frames_skipped_for_testing().value(), 0u);

  // Advance the time to 15s. The frame is still at 0u because the posted task
  // should run at 20s.
  image_observer_->animation_advanced_ = false;
  task_runner->AdvanceTimeAndRun(5);
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 0u);
  EXPECT_FALSE(image_observer_->animation_advanced_);

  // Advance the time to 20s. The task runs and advances the animation forward
  // to 1u.
  task_runner->AdvanceTimeAndRun(5);
  EXPECT_TRUE(image_observer_->animation_advanced_);

  // If we were to paint now, we should use the second frame.
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 1u);

  // Now assume painting the next frame was delayed to 41 seconds. We should
  // first draw with the |current_frame_index_| and then call StartAnimation to
  // advance the frame.
  // Since the animation started at 10s, and each frame has a duration of 10s,
  // we should see the fourth frame at 41 seconds.
  SetClock(41);
  task_runner->SetTime(41);
  StartAnimation();

  // Since we just skipped frames while advancing this animation, we should see
  // a notification to dirty immediately.
  image_observer_->animation_advanced_ = false;
  task_runner->AdvanceTimeAndRun(0);
  EXPECT_TRUE(image_observer_->animation_advanced_);

  // On the next draw, we should see the fourth frame.
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 3u);
  EXPECT_EQ(image_->last_num_frames_skipped_for_testing().value(), 1u);

  // At 70s, we would want to display the last frame and would skip 2 frames.
  // But because its incomplete, we advanced to the sixth frame and only skipped
  // 1 frame.
  SetClock(71);
  task_runner->SetTime(71);
  StartAnimation();
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 5u);
  EXPECT_EQ(image_->last_num_frames_skipped_for_testing().value(), 1u);

  // We should still see a notification to move to the sixth frame.
  image_observer_->animation_advanced_ = false;
  task_runner->AdvanceTimeAndRun(0);
  EXPECT_TRUE(image_observer_->animation_advanced_);

  // Run any pending tasks and try to animate again. Can't advance the animation
  // because the last frame is not complete.
  task_runner->AdvanceTimeAndRun(0);
  StartAnimation();
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 5u);
  EXPECT_FALSE(image_->last_num_frames_skipped_for_testing().has_value());

  // Finish the load and kick the animation again. It finishes during catch up.
  // But no frame skipped because we just advanced to the last frame.
  last_frame_complete_ = true;
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), true);

  StartAnimation();
  EXPECT_EQ(image_->PaintImageForCurrentFrame().frame_index(), 6u);
  EXPECT_EQ(image_->last_num_frames_skipped_for_testing().value(), 0u);

  // Finishing the animation always notifies observers async.
  image_observer_->animation_advanced_ = false;
  task_runner->AdvanceTimeAndRun(0);
  EXPECT_TRUE(image_observer_->animation_advanced_);
}

TEST_F(BitmapImageTestWithMockDecoder, ResetAnimation) {
  ScopedCompositorImageAnimationsForTest compositor_image_animations(false);

  repetition_count_ = kAnimationLoopInfinite;
  frame_count_ = 4u;
  last_frame_complete_ = true;
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), false);

  PaintImage image = image_->PaintImageForCurrentFrame();
  image_->ResetAnimation();
  PaintImage image2 = image_->PaintImageForCurrentFrame();
  EXPECT_GT(image2.reset_animation_sequence_id(),
            image.reset_animation_sequence_id());
}

TEST_F(BitmapImageTestWithMockDecoder, PaintImageForStaticBitmapImage) {
  ScopedCompositorImageAnimationsForTest compositor_image_animations(false);

  repetition_count_ = kAnimationLoopInfinite;
  frame_count_ = 5;
  last_frame_complete_ = true;
  image_->SetData(SharedBuffer::Create("data", sizeof("data")), false);

  // PaintImage for the original image is animated.
  EXPECT_TRUE(image_->PaintImageForCurrentFrame().ShouldAnimate());

  // But the StaticBitmapImage is not.
  EXPECT_FALSE(image_->ImageForDefaultFrame()
                   ->PaintImageForCurrentFrame()
                   .ShouldAnimate());
}

template <typename HistogramEnumType>
struct HistogramTestParams {
  const char* filename;
  HistogramEnumType type;
};

template <typename HistogramEnumType>
class BitmapHistogramTest : public BitmapImageTest,
                            public ::testing::WithParamInterface<
                                HistogramTestParams<HistogramEnumType>> {
 protected:
  void RunTest(const char* histogram_name) {
    HistogramTester histogram_tester;
    LoadImage(this->GetParam().filename);
    histogram_tester.ExpectUniqueSample(histogram_name, this->GetParam().type,
                                        1);
  }
};

using DecodedImageTypeHistogramTest =
    BitmapHistogramTest<BitmapImageMetrics::DecodedImageType>;

TEST_P(DecodedImageTypeHistogramTest, ImageType) {
  RunTest("Blink.DecodedImageType");
}

const DecodedImageTypeHistogramTest::ParamType
    kDecodedImageTypeHistogramTestparams[] = {
        {"/LayoutTests/images/resources/green.jpg",
         BitmapImageMetrics::kImageJPEG},
        {"/LayoutTests/images/resources/"
         "palatted-color-png-gamma-one-color-profile.png",
         BitmapImageMetrics::kImagePNG},
        {"/LayoutTests/images/resources/animated-10color.gif",
         BitmapImageMetrics::kImageGIF},
        {"/LayoutTests/images/resources/webp-color-profile-lossy.webp",
         BitmapImageMetrics::kImageWebP},
        {"/LayoutTests/images/resources/wrong-frame-dimensions.ico",
         BitmapImageMetrics::kImageICO},
        {"/LayoutTests/images/resources/lenna.bmp",
         BitmapImageMetrics::kImageBMP}};

INSTANTIATE_TEST_CASE_P(
    DecodedImageTypeHistogramTest,
    DecodedImageTypeHistogramTest,
    ::testing::ValuesIn(kDecodedImageTypeHistogramTestparams));

using DecodedImageOrientationHistogramTest =
    BitmapHistogramTest<ImageOrientationEnum>;

TEST_P(DecodedImageOrientationHistogramTest, ImageOrientation) {
  RunTest("Blink.DecodedImage.Orientation");
}

const DecodedImageOrientationHistogramTest::ParamType
    kDecodedImageOrientationHistogramTestParams[] = {
        {"/LayoutTests/images/resources/exif-orientation-1-ul.jpg",
         kOriginTopLeft},
        {"/LayoutTests/images/resources/exif-orientation-2-ur.jpg",
         kOriginTopRight},
        {"/LayoutTests/images/resources/exif-orientation-3-lr.jpg",
         kOriginBottomRight},
        {"/LayoutTests/images/resources/exif-orientation-4-lol.jpg",
         kOriginBottomLeft},
        {"/LayoutTests/images/resources/exif-orientation-5-lu.jpg",
         kOriginLeftTop},
        {"/LayoutTests/images/resources/exif-orientation-6-ru.jpg",
         kOriginRightTop},
        {"/LayoutTests/images/resources/exif-orientation-7-rl.jpg",
         kOriginRightBottom},
        {"/LayoutTests/images/resources/exif-orientation-8-llo.jpg",
         kOriginLeftBottom}};

INSTANTIATE_TEST_CASE_P(
    DecodedImageOrientationHistogramTest,
    DecodedImageOrientationHistogramTest,
    ::testing::ValuesIn(kDecodedImageOrientationHistogramTestParams));

}  // namespace blink