summaryrefslogtreecommitdiffstats
path: root/image/test/gtest/TestDecoders.cpp
blob: f043b011d0330693c185a6b63d610f38699dd944 (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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "Common.h"
#include "AnimationSurfaceProvider.h"
#include "DecodePool.h"
#include "Decoder.h"
#include "DecoderFactory.h"
#include "decoders/nsBMPDecoder.h"
#include "IDecodingTask.h"
#include "ImageOps.h"
#include "imgIContainer.h"
#include "ImageFactory.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/gfx/2D.h"
#include "nsComponentManagerUtils.h"
#include "nsCOMPtr.h"
#include "nsIInputStream.h"
#include "mozilla/RefPtr.h"
#include "nsStreamUtils.h"
#include "nsString.h"
#include "nsThreadUtils.h"
#include "ProgressTracker.h"
#include "SourceBuffer.h"

using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::image;

static already_AddRefed<SourceSurface> CheckDecoderState(
    const ImageTestCase& aTestCase, image::Decoder* aDecoder) {
  // image::Decoder should match what we asked for in the MIME type.
  EXPECT_NE(aDecoder->GetType(), DecoderType::UNKNOWN);
  EXPECT_EQ(aDecoder->GetType(),
            DecoderFactory::GetDecoderType(aTestCase.mMimeType));

  EXPECT_TRUE(aDecoder->GetDecodeDone());
  EXPECT_EQ(bool(aTestCase.mFlags & TEST_CASE_HAS_ERROR), aDecoder->HasError());

  // Verify that the decoder made the expected progress.
  Progress progress = aDecoder->TakeProgress();
  EXPECT_EQ(bool(aTestCase.mFlags & TEST_CASE_HAS_ERROR),
            bool(progress & FLAG_HAS_ERROR));

  if (aTestCase.mFlags & TEST_CASE_HAS_ERROR) {
    return nullptr;  // That's all we can check for bad images.
  }

  EXPECT_TRUE(bool(progress & FLAG_SIZE_AVAILABLE));
  EXPECT_TRUE(bool(progress & FLAG_DECODE_COMPLETE));
  EXPECT_TRUE(bool(progress & FLAG_FRAME_COMPLETE));
  EXPECT_EQ(bool(aTestCase.mFlags & TEST_CASE_IS_TRANSPARENT),
            bool(progress & FLAG_HAS_TRANSPARENCY));
  EXPECT_EQ(bool(aTestCase.mFlags & TEST_CASE_IS_ANIMATED),
            bool(progress & FLAG_IS_ANIMATED));

  // The decoder should get the correct size.
  OrientedIntSize size = aDecoder->Size();
  EXPECT_EQ(aTestCase.mSize.width, size.width);
  EXPECT_EQ(aTestCase.mSize.height, size.height);

  // Get the current frame, which is always the first frame of the image
  // because CreateAnonymousDecoder() forces a first-frame-only decode.
  RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
  RefPtr<SourceSurface> surface = currentFrame->GetSourceSurface();

  // Verify that the resulting surfaces matches our expectations.
  EXPECT_TRUE(surface->IsDataSourceSurface());
  EXPECT_TRUE(surface->GetFormat() == SurfaceFormat::OS_RGBX ||
              surface->GetFormat() == SurfaceFormat::OS_RGBA);
  EXPECT_EQ(aTestCase.mOutputSize, surface->GetSize());

  return surface.forget();
}

static void CheckDecoderResults(const ImageTestCase& aTestCase,
                                image::Decoder* aDecoder) {
  RefPtr<SourceSurface> surface = CheckDecoderState(aTestCase, aDecoder);
  if (!surface) {
    return;
  }

  if (aTestCase.mFlags & TEST_CASE_IGNORE_OUTPUT) {
    return;
  }

  // Check the output.
  EXPECT_TRUE(IsSolidColor(surface, aTestCase.Color(), aTestCase.Fuzz()));
}

template <typename Func>
void WithBadBufferDecode(const ImageTestCase& aTestCase,
                         const Maybe<IntSize>& aOutputSize,
                         Func aResultChecker) {
  // Prepare a SourceBuffer with an error that will immediately move iterators
  // to COMPLETE.
  auto sourceBuffer = MakeNotNull<RefPtr<SourceBuffer>>();
  sourceBuffer->ExpectLength(SIZE_MAX);

  // Create a decoder.
  DecoderType decoderType = DecoderFactory::GetDecoderType(aTestCase.mMimeType);
  RefPtr<image::Decoder> decoder = DecoderFactory::CreateAnonymousDecoder(
      decoderType, sourceBuffer, aOutputSize, DecoderFlags::FIRST_FRAME_ONLY,
      aTestCase.mSurfaceFlags);
  ASSERT_TRUE(decoder != nullptr);
  RefPtr<IDecodingTask> task =
      new AnonymousDecodingTask(WrapNotNull(decoder), /* aResumable */ false);

  // Run the full decoder synchronously on the main thread.
  task->Run();

  // Call the lambda to verify the expected results.
  aResultChecker(decoder);
}

static void CheckDecoderBadBuffer(const ImageTestCase& aTestCase) {
  WithBadBufferDecode(aTestCase, Nothing(), [&](image::Decoder* aDecoder) {
    CheckDecoderResults(aTestCase, aDecoder);
  });
}

template <typename Func>
void WithSingleChunkDecode(const ImageTestCase& aTestCase,
                           const Maybe<IntSize>& aOutputSize,
                           bool aUseDecodePool, Func aResultChecker) {
  nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
  ASSERT_TRUE(inputStream != nullptr);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_NS_SUCCEEDED(rv);

  // Write the data into a SourceBuffer.
  auto sourceBuffer = MakeNotNull<RefPtr<SourceBuffer>>();
  sourceBuffer->ExpectLength(length);
  rv = sourceBuffer->AppendFromInputStream(inputStream, length);
  ASSERT_NS_SUCCEEDED(rv);
  sourceBuffer->Complete(NS_OK);

  // Create a decoder.
  DecoderType decoderType = DecoderFactory::GetDecoderType(aTestCase.mMimeType);
  DecoderFlags decoderFlags =
      DecoderFactory::GetDefaultDecoderFlagsForType(decoderType) |
      DecoderFlags::FIRST_FRAME_ONLY;
  RefPtr<image::Decoder> decoder = DecoderFactory::CreateAnonymousDecoder(
      decoderType, sourceBuffer, aOutputSize, decoderFlags,
      aTestCase.mSurfaceFlags);
  ASSERT_TRUE(decoder != nullptr);
  RefPtr<IDecodingTask> task =
      new AnonymousDecodingTask(WrapNotNull(decoder), /* aResumable */ false);

  if (aUseDecodePool) {
    DecodePool::Singleton()->AsyncRun(task.get());

    while (!decoder->GetDecodeDone()) {
      task->Resume();
    }
  } else {  // Run the full decoder synchronously on the main thread.
    task->Run();
  }

  // Call the lambda to verify the expected results.
  aResultChecker(decoder);
}

static void CheckDecoderSingleChunk(const ImageTestCase& aTestCase,
                                    bool aUseDecodePool = false) {
  WithSingleChunkDecode(aTestCase, Nothing(), aUseDecodePool,
                        [&](image::Decoder* aDecoder) {
                          CheckDecoderResults(aTestCase, aDecoder);
                        });
}

template <typename Func>
void WithDelayedChunkDecode(const ImageTestCase& aTestCase,
                            const Maybe<IntSize>& aOutputSize,
                            Func aResultChecker) {
  nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
  ASSERT_TRUE(inputStream != nullptr);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_NS_SUCCEEDED(rv);

  // Prepare an empty SourceBuffer.
  auto sourceBuffer = MakeNotNull<RefPtr<SourceBuffer>>();

  // Create a decoder.
  DecoderType decoderType = DecoderFactory::GetDecoderType(aTestCase.mMimeType);
  RefPtr<image::Decoder> decoder = DecoderFactory::CreateAnonymousDecoder(
      decoderType, sourceBuffer, aOutputSize, DecoderFlags::FIRST_FRAME_ONLY,
      aTestCase.mSurfaceFlags);
  ASSERT_TRUE(decoder != nullptr);
  RefPtr<IDecodingTask> task =
      new AnonymousDecodingTask(WrapNotNull(decoder), /* aResumable */ true);

  // Run the full decoder synchronously. It should now be waiting on
  // the iterator to yield some data since we haven't written anything yet.
  task->Run();

  // Writing all of the data should wake up the decoder to complete.
  sourceBuffer->ExpectLength(length);
  rv = sourceBuffer->AppendFromInputStream(inputStream, length);
  ASSERT_NS_SUCCEEDED(rv);
  sourceBuffer->Complete(NS_OK);

  // It would have gotten posted to the main thread to avoid mutex contention.
  SpinPendingEvents();

  // Call the lambda to verify the expected results.
  aResultChecker(decoder);
}

static void CheckDecoderDelayedChunk(const ImageTestCase& aTestCase) {
  WithDelayedChunkDecode(aTestCase, Nothing(), [&](image::Decoder* aDecoder) {
    CheckDecoderResults(aTestCase, aDecoder);
  });
}

static void CheckDecoderMultiChunk(const ImageTestCase& aTestCase,
                                   uint64_t aChunkSize = 1) {
  nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
  ASSERT_TRUE(inputStream != nullptr);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_NS_SUCCEEDED(rv);

  // Create a SourceBuffer and a decoder.
  auto sourceBuffer = MakeNotNull<RefPtr<SourceBuffer>>();
  sourceBuffer->ExpectLength(length);
  DecoderType decoderType = DecoderFactory::GetDecoderType(aTestCase.mMimeType);
  DecoderFlags decoderFlags =
      DecoderFactory::GetDefaultDecoderFlagsForType(decoderType) |
      DecoderFlags::FIRST_FRAME_ONLY;
  RefPtr<image::Decoder> decoder = DecoderFactory::CreateAnonymousDecoder(
      decoderType, sourceBuffer, Nothing(), decoderFlags,
      aTestCase.mSurfaceFlags);
  ASSERT_TRUE(decoder != nullptr);
  RefPtr<IDecodingTask> task =
      new AnonymousDecodingTask(WrapNotNull(decoder), /* aResumable */ true);

  // Run the full decoder synchronously. It should now be waiting on
  // the iterator to yield some data since we haven't written anything yet.
  task->Run();

  while (length > 0) {
    uint64_t read = length > aChunkSize ? aChunkSize : length;
    length -= read;

    uint64_t available = 0;
    rv = inputStream->Available(&available);
    ASSERT_TRUE(available >= read);
    ASSERT_NS_SUCCEEDED(rv);

    // Writing any data should wake up the decoder to complete.
    rv = sourceBuffer->AppendFromInputStream(inputStream, read);
    ASSERT_NS_SUCCEEDED(rv);

    // It would have gotten posted to the main thread to avoid mutex contention.
    SpinPendingEvents();
  }

  sourceBuffer->Complete(NS_OK);
  SpinPendingEvents();

  CheckDecoderResults(aTestCase, decoder);
}

static void CheckDownscaleDuringDecode(const ImageTestCase& aTestCase) {
  // This function expects that |aTestCase| consists of 25 lines of green,
  // followed by 25 lines of red, followed by 25 lines of green, followed by 25
  // more lines of red. We'll downscale it from 100x100 to 20x20.
  IntSize outputSize(20, 20);

  WithSingleChunkDecode(
      aTestCase, Some(outputSize), /* aUseDecodePool */ false,
      [&](image::Decoder* aDecoder) {
        RefPtr<SourceSurface> surface = CheckDecoderState(aTestCase, aDecoder);

        // There are no downscale-during-decode tests that have
        // TEST_CASE_HAS_ERROR set, so we expect to always get a surface here.
        EXPECT_TRUE(surface != nullptr);

        if (aTestCase.mFlags & TEST_CASE_IGNORE_OUTPUT) {
          return;
        }

        // Check that the downscaled image is correct. Note that we skip rows
        // near the transitions between colors, since the downscaler does not
        // produce a sharp boundary at these points. Even some of the rows we
        // test need a small amount of fuzz; this is just the nature of Lanczos
        // downscaling.
        EXPECT_TRUE(RowsAreSolidColor(surface, 0, 4,
                                      aTestCase.ChooseColor(BGRAColor::Green()),
                                      /* aFuzz = */ 47));
        EXPECT_TRUE(RowsAreSolidColor(surface, 6, 3,
                                      aTestCase.ChooseColor(BGRAColor::Red()),
                                      /* aFuzz = */ 27));
        EXPECT_TRUE(RowsAreSolidColor(surface, 11, 3, BGRAColor::Green(),
                                      /* aFuzz = */ 47));
        EXPECT_TRUE(RowsAreSolidColor(surface, 16, 4,
                                      aTestCase.ChooseColor(BGRAColor::Red()),
                                      /* aFuzz = */ 27));
      });
}

static void CheckAnimationDecoderResults(const ImageTestCase& aTestCase,
                                         AnimationSurfaceProvider* aProvider,
                                         image::Decoder* aDecoder) {
  EXPECT_TRUE(aDecoder->GetDecodeDone());
  EXPECT_EQ(bool(aTestCase.mFlags & TEST_CASE_HAS_ERROR), aDecoder->HasError());

  if (aTestCase.mFlags & TEST_CASE_HAS_ERROR) {
    return;  // That's all we can check for bad images.
  }

  // The decoder should get the correct size.
  OrientedIntSize size = aDecoder->Size();
  EXPECT_EQ(aTestCase.mSize.width, size.width);
  EXPECT_EQ(aTestCase.mSize.height, size.height);

  if (aTestCase.mFlags & TEST_CASE_IGNORE_OUTPUT) {
    return;
  }

  // Check the output.
  AutoTArray<BGRAColor, 2> framePixels;
  framePixels.AppendElement(aTestCase.ChooseColor(BGRAColor::Green()));
  framePixels.AppendElement(
      aTestCase.ChooseColor(BGRAColor(0x7F, 0x7F, 0x7F, 0xFF)));

  DrawableSurface drawableSurface(WrapNotNull(aProvider));
  for (size_t i = 0; i < framePixels.Length(); ++i) {
    nsresult rv = drawableSurface.Seek(i);
    EXPECT_NS_SUCCEEDED(rv);

    // Check the first frame, all green.
    RawAccessFrameRef rawFrame = drawableSurface->RawAccessRef();
    RefPtr<SourceSurface> surface = rawFrame->GetSourceSurface();

    // Verify that the resulting surfaces matches our expectations.
    EXPECT_TRUE(surface->IsDataSourceSurface());
    EXPECT_TRUE(surface->GetFormat() == SurfaceFormat::OS_RGBX ||
                surface->GetFormat() == SurfaceFormat::OS_RGBA);
    EXPECT_EQ(aTestCase.mOutputSize, surface->GetSize());
    EXPECT_TRUE(IsSolidColor(surface, framePixels[i], aTestCase.Fuzz()));
  }

  // Should be no more frames.
  nsresult rv = drawableSurface.Seek(framePixels.Length());
  EXPECT_NS_FAILED(rv);
}

template <typename Func>
static void WithSingleChunkAnimationDecode(const ImageTestCase& aTestCase,
                                           Func aResultChecker) {
  // Create an image.
  RefPtr<Image> image = ImageFactory::CreateAnonymousImage(
      nsDependentCString(aTestCase.mMimeType));
  ASSERT_TRUE(!image->HasError());

  NotNull<RefPtr<RasterImage>> rasterImage =
      WrapNotNull(static_cast<RasterImage*>(image.get()));

  nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
  ASSERT_TRUE(inputStream != nullptr);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_NS_SUCCEEDED(rv);

  // Write the data into a SourceBuffer.
  NotNull<RefPtr<SourceBuffer>> sourceBuffer = WrapNotNull(new SourceBuffer());
  sourceBuffer->ExpectLength(length);
  rv = sourceBuffer->AppendFromInputStream(inputStream, length);
  ASSERT_NS_SUCCEEDED(rv);
  sourceBuffer->Complete(NS_OK);

  // Create a metadata decoder first, because otherwise RasterImage will get
  // unhappy about finding out the image is animated during a full decode.
  DecoderType decoderType = DecoderFactory::GetDecoderType(aTestCase.mMimeType);
  DecoderFlags decoderFlags =
      DecoderFactory::GetDefaultDecoderFlagsForType(decoderType);
  RefPtr<IDecodingTask> task = DecoderFactory::CreateMetadataDecoder(
      decoderType, rasterImage, decoderFlags, sourceBuffer);
  ASSERT_TRUE(task != nullptr);

  // Run the metadata decoder synchronously.
  task->Run();

  // Create a decoder.
  SurfaceFlags surfaceFlags = aTestCase.mSurfaceFlags;
  RefPtr<image::Decoder> decoder = DecoderFactory::CreateAnonymousDecoder(
      decoderType, sourceBuffer, Nothing(), decoderFlags, surfaceFlags);
  ASSERT_TRUE(decoder != nullptr);

  // Create an AnimationSurfaceProvider which will manage the decoding process
  // and make this decoder's output available in the surface cache.
  SurfaceKey surfaceKey = RasterSurfaceKey(aTestCase.mOutputSize, surfaceFlags,
                                           PlaybackType::eAnimated);
  RefPtr<AnimationSurfaceProvider> provider = new AnimationSurfaceProvider(
      rasterImage, surfaceKey, WrapNotNull(decoder),
      /* aCurrentFrame */ 0);

  // Run the full decoder synchronously.
  provider->Run();

  // Call the lambda to verify the expected results.
  aResultChecker(provider, decoder);
}

static void CheckAnimationDecoderSingleChunk(const ImageTestCase& aTestCase) {
  WithSingleChunkAnimationDecode(
      aTestCase,
      [&](AnimationSurfaceProvider* aProvider, image::Decoder* aDecoder) {
        CheckAnimationDecoderResults(aTestCase, aProvider, aDecoder);
      });
}

static void CheckDecoderFrameFirst(const ImageTestCase& aTestCase) {
  // Verify that we can decode this test case and retrieve the first frame using
  // imgIContainer::FRAME_FIRST. This ensures that we correctly trigger a
  // single-frame decode rather than an animated decode when
  // imgIContainer::FRAME_FIRST is requested.

  // Create an image.
  RefPtr<Image> image = ImageFactory::CreateAnonymousImage(
      nsDependentCString(aTestCase.mMimeType));
  ASSERT_TRUE(!image->HasError());

  nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
  ASSERT_TRUE(inputStream);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_NS_SUCCEEDED(rv);

  // Write the data into the image.
  rv = image->OnImageDataAvailable(nullptr, inputStream, 0,
                                   static_cast<uint32_t>(length));
  ASSERT_NS_SUCCEEDED(rv);

  // Let the image know we've sent all the data.
  rv = image->OnImageDataComplete(nullptr, NS_OK, true);
  ASSERT_NS_SUCCEEDED(rv);

  RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
  tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);

  // Lock the image so its surfaces don't disappear during the test.
  image->LockImage();

  auto unlock = mozilla::MakeScopeExit([&] { image->UnlockImage(); });

  // Use GetFrame() to force a sync decode of the image, specifying FRAME_FIRST
  // to ensure that we don't get an animated decode.
  RefPtr<SourceSurface> surface = image->GetFrame(
      imgIContainer::FRAME_FIRST, imgIContainer::FLAG_SYNC_DECODE);

  // Ensure that the image's metadata meets our expectations.
  IntSize imageSize(0, 0);
  rv = image->GetWidth(&imageSize.width);
  EXPECT_NS_SUCCEEDED(rv);
  rv = image->GetHeight(&imageSize.height);
  EXPECT_NS_SUCCEEDED(rv);

  EXPECT_EQ(aTestCase.mSize.width, imageSize.width);
  EXPECT_EQ(aTestCase.mSize.height, imageSize.height);

  Progress imageProgress = tracker->GetProgress();

  EXPECT_TRUE(bool(imageProgress & FLAG_HAS_TRANSPARENCY) == false);
  EXPECT_TRUE(bool(imageProgress & FLAG_IS_ANIMATED) == true);

  // Ensure that we decoded the static version of the image.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eStatic),
        /* aMarkUsed = */ false);
    ASSERT_EQ(MatchType::EXACT, result.Type());
    EXPECT_TRUE(bool(result.Surface()));
  }

  // Ensure that we didn't decode the animated version of the image.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eAnimated),
        /* aMarkUsed = */ false);
    ASSERT_EQ(MatchType::NOT_FOUND, result.Type());
  }

  // Use GetFrame() to force a sync decode of the image, this time specifying
  // FRAME_CURRENT to ensure that we get an animated decode.
  RefPtr<SourceSurface> animatedSurface = image->GetFrame(
      imgIContainer::FRAME_CURRENT, imgIContainer::FLAG_SYNC_DECODE);

  // Ensure that we decoded both frames of the animated version of the image.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eAnimated),
        /* aMarkUsed = */ true);
    ASSERT_EQ(MatchType::EXACT, result.Type());

    EXPECT_NS_SUCCEEDED(result.Surface().Seek(0));
    EXPECT_TRUE(bool(result.Surface()));

    RefPtr<imgFrame> partialFrame = result.Surface().GetFrame(1);
    EXPECT_TRUE(bool(partialFrame));
  }

  // Ensure that the static version is still around.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eStatic),
        /* aMarkUsed = */ true);
    ASSERT_EQ(MatchType::EXACT, result.Type());
    EXPECT_TRUE(bool(result.Surface()));
  }
}

static void CheckDecoderFrameCurrent(const ImageTestCase& aTestCase) {
  // Verify that we can decode this test case and retrieve the entire sequence
  // of frames using imgIContainer::FRAME_CURRENT. This ensures that we
  // correctly trigger an animated decode rather than a single-frame decode when
  // imgIContainer::FRAME_CURRENT is requested.

  // Create an image.
  RefPtr<Image> image = ImageFactory::CreateAnonymousImage(
      nsDependentCString(aTestCase.mMimeType));
  ASSERT_TRUE(!image->HasError());

  nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
  ASSERT_TRUE(inputStream);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_NS_SUCCEEDED(rv);

  // Write the data into the image.
  rv = image->OnImageDataAvailable(nullptr, inputStream, 0,
                                   static_cast<uint32_t>(length));
  ASSERT_NS_SUCCEEDED(rv);

  // Let the image know we've sent all the data.
  rv = image->OnImageDataComplete(nullptr, NS_OK, true);
  ASSERT_NS_SUCCEEDED(rv);

  RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
  tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);

  // Lock the image so its surfaces don't disappear during the test.
  image->LockImage();

  // Use GetFrame() to force a sync decode of the image, specifying
  // FRAME_CURRENT to ensure we get an animated decode.
  RefPtr<SourceSurface> surface = image->GetFrame(
      imgIContainer::FRAME_CURRENT, imgIContainer::FLAG_SYNC_DECODE);

  // Ensure that the image's metadata meets our expectations.
  IntSize imageSize(0, 0);
  rv = image->GetWidth(&imageSize.width);
  EXPECT_NS_SUCCEEDED(rv);
  rv = image->GetHeight(&imageSize.height);
  EXPECT_NS_SUCCEEDED(rv);

  EXPECT_EQ(aTestCase.mSize.width, imageSize.width);
  EXPECT_EQ(aTestCase.mSize.height, imageSize.height);

  Progress imageProgress = tracker->GetProgress();

  EXPECT_TRUE(bool(imageProgress & FLAG_HAS_TRANSPARENCY) == false);
  EXPECT_TRUE(bool(imageProgress & FLAG_IS_ANIMATED) == true);

  // Ensure that we decoded both frames of the animated version of the image.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eAnimated),
        /* aMarkUsed = */ true);
    ASSERT_EQ(MatchType::EXACT, result.Type());

    EXPECT_NS_SUCCEEDED(result.Surface().Seek(0));
    EXPECT_TRUE(bool(result.Surface()));

    RefPtr<imgFrame> partialFrame = result.Surface().GetFrame(1);
    EXPECT_TRUE(bool(partialFrame));
  }

  // Ensure that we didn't decode the static version of the image.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eStatic),
        /* aMarkUsed = */ false);
    ASSERT_EQ(MatchType::NOT_FOUND, result.Type());
  }

  // Use GetFrame() to force a sync decode of the image, this time specifying
  // FRAME_FIRST to ensure that we get a single-frame decode.
  RefPtr<SourceSurface> animatedSurface = image->GetFrame(
      imgIContainer::FRAME_FIRST, imgIContainer::FLAG_SYNC_DECODE);

  // Ensure that we decoded the static version of the image.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eStatic),
        /* aMarkUsed = */ true);
    ASSERT_EQ(MatchType::EXACT, result.Type());
    EXPECT_TRUE(bool(result.Surface()));
  }

  // Ensure that both frames of the animated version are still around.
  {
    LookupResult result = SurfaceCache::Lookup(
        ImageKey(image.get()),
        RasterSurfaceKey(imageSize, aTestCase.mSurfaceFlags,
                         PlaybackType::eAnimated),
        /* aMarkUsed = */ true);
    ASSERT_EQ(MatchType::EXACT, result.Type());

    EXPECT_NS_SUCCEEDED(result.Surface().Seek(0));
    EXPECT_TRUE(bool(result.Surface()));

    RefPtr<imgFrame> partialFrame = result.Surface().GetFrame(1);
    EXPECT_TRUE(bool(partialFrame));
  }
}

class ImageDecoders : public ::testing::Test {
 protected:
  AutoInitializeImageLib mInit;
};

#define IMAGE_GTEST_DECODER_BASE_F(test_prefix)                              \
  TEST_F(ImageDecoders, test_prefix##SingleChunk) {                          \
    CheckDecoderSingleChunk(Green##test_prefix##TestCase());                 \
  }                                                                          \
                                                                             \
  TEST_F(ImageDecoders, test_prefix##DelayedChunk) {                         \
    CheckDecoderDelayedChunk(Green##test_prefix##TestCase());                \
  }                                                                          \
                                                                             \
  TEST_F(ImageDecoders, test_prefix##MultiChunk) {                           \
    CheckDecoderMultiChunk(Green##test_prefix##TestCase());                  \
  }                                                                          \
                                                                             \
  TEST_F(ImageDecoders, test_prefix##DownscaleDuringDecode) {                \
    CheckDownscaleDuringDecode(Downscaled##test_prefix##TestCase());         \
  }                                                                          \
                                                                             \
  TEST_F(ImageDecoders, test_prefix##ForceSRGB) {                            \
    CheckDecoderSingleChunk(Green##test_prefix##TestCase().WithSurfaceFlags( \
        SurfaceFlags::TO_SRGB_COLORSPACE));                                  \
  }                                                                          \
                                                                             \
  TEST_F(ImageDecoders, test_prefix##BadBuffer) {                            \
    CheckDecoderBadBuffer(Green##test_prefix##TestCase().WithFlags(          \
        TEST_CASE_HAS_ERROR | TEST_CASE_IGNORE_OUTPUT));                     \
  }

IMAGE_GTEST_DECODER_BASE_F(PNG)
IMAGE_GTEST_DECODER_BASE_F(GIF)
IMAGE_GTEST_DECODER_BASE_F(JPG)
IMAGE_GTEST_DECODER_BASE_F(BMP)
IMAGE_GTEST_DECODER_BASE_F(ICO)
IMAGE_GTEST_DECODER_BASE_F(Icon)
IMAGE_GTEST_DECODER_BASE_F(WebP)
#ifdef MOZ_JXL
IMAGE_GTEST_DECODER_BASE_F(JXL)
#endif

TEST_F(ImageDecoders, ICOWithANDMaskDownscaleDuringDecode) {
  CheckDownscaleDuringDecode(DownscaledTransparentICOWithANDMaskTestCase());
}

TEST_F(ImageDecoders, WebPLargeMultiChunk) {
  CheckDecoderMultiChunk(LargeWebPTestCase(), /* aChunkSize */ 64);
}

TEST_F(ImageDecoders, WebPIccSrgbMultiChunk) {
  CheckDecoderMultiChunk(GreenWebPIccSrgbTestCase());
}

TEST_F(ImageDecoders, WebPTransparentSingleChunk) {
  CheckDecoderSingleChunk(TransparentWebPTestCase());
}

TEST_F(ImageDecoders, WebPTransparentNoAlphaHeaderSingleChunk) {
  CheckDecoderSingleChunk(TransparentNoAlphaHeaderWebPTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunk) {
  CheckDecoderSingleChunk(GreenAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkNonzeroReserved) {
  CheckDecoderSingleChunk(NonzeroReservedAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkMultipleColr) {
  CheckDecoderSingleChunk(MultipleColrAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent10bit420) {
  CheckDecoderSingleChunk(Transparent10bit420AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent10bit422) {
  CheckDecoderSingleChunk(Transparent10bit422AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent10bit444) {
  CheckDecoderSingleChunk(Transparent10bit444AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent12bit420) {
  CheckDecoderSingleChunk(Transparent12bit420AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent12bit422) {
  CheckDecoderSingleChunk(Transparent12bit422AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent12bit444) {
  CheckDecoderSingleChunk(Transparent12bit444AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent8bit420) {
  CheckDecoderSingleChunk(Transparent8bit420AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent8bit422) {
  CheckDecoderSingleChunk(Transparent8bit422AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkTransparent8bit444) {
  CheckDecoderSingleChunk(Transparent8bit444AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitLimitedRangeBT601) {
  CheckDecoderSingleChunk(Gray8bitLimitedRangeBT601AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitLimitedRangeBT709) {
  CheckDecoderSingleChunk(Gray8bitLimitedRangeBT709AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitLimitedRangeBT2020) {
  CheckDecoderSingleChunk(Gray8bitLimitedRangeBT2020AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitFullRangeBT601) {
  CheckDecoderSingleChunk(Gray8bitFullRangeBT601AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitFullRangeBT709) {
  CheckDecoderSingleChunk(Gray8bitFullRangeBT709AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitFullRangeBT2020) {
  CheckDecoderSingleChunk(Gray8bitFullRangeBT2020AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitLimitedRangeBT601) {
  CheckDecoderSingleChunk(Gray10bitLimitedRangeBT601AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitLimitedRangeBT709) {
  CheckDecoderSingleChunk(Gray10bitLimitedRangeBT709AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitLimitedRangeBT2020) {
  CheckDecoderSingleChunk(Gray10bitLimitedRangeBT2020AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitFullRangeBT601) {
  CheckDecoderSingleChunk(Gray10bitFullRangeBT601AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitFullRangeBT709) {
  CheckDecoderSingleChunk(Gray10bitFullRangeBT709AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitFullRangeBT2020) {
  CheckDecoderSingleChunk(Gray10bitFullRangeBT2020AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitLimitedRangeBT601) {
  CheckDecoderSingleChunk(Gray12bitLimitedRangeBT601AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitLimitedRangeBT709) {
  CheckDecoderSingleChunk(Gray12bitLimitedRangeBT709AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitLimitedRangeBT2020) {
  CheckDecoderSingleChunk(Gray12bitLimitedRangeBT2020AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitFullRangeBT601) {
  CheckDecoderSingleChunk(Gray12bitFullRangeBT601AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitFullRangeBT709) {
  CheckDecoderSingleChunk(Gray12bitFullRangeBT709AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitFullRangeBT2020) {
  CheckDecoderSingleChunk(Gray12bitFullRangeBT2020AVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitLimitedRangeGrayscale) {
  CheckDecoderSingleChunk(Gray8bitLimitedRangeGrayscaleAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray8bitFullRangeGrayscale) {
  CheckDecoderSingleChunk(Gray8bitFullRangeGrayscaleAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitLimitedRangeGrayscale) {
  CheckDecoderSingleChunk(Gray10bitLimitedRangeGrayscaleAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray10bitFullRangeGrayscale) {
  CheckDecoderSingleChunk(Gray10bitFullRangeGrayscaleAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitLimitedRangeGrayscale) {
  CheckDecoderSingleChunk(Gray12bitLimitedRangeGrayscaleAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFSingleChunkGray12bitFullRangeGrayscale) {
  CheckDecoderSingleChunk(Gray12bitFullRangeGrayscaleAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFMultiLayerSingleChunk) {
  CheckDecoderSingleChunk(MultiLayerAVIFTestCase());
}

// This test must use the decode pool in order to check for regressions
// of crashing the dav1d decoder when the ImgDecoder threads have a standard-
// sized stack.
TEST_F(ImageDecoders, AVIFStackCheck) {
  CheckDecoderSingleChunk(StackCheckAVIFTestCase(), /* aUseDecodePool */ true);
}

TEST_F(ImageDecoders, AVIFDelayedChunk) {
  CheckDecoderDelayedChunk(GreenAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFMultiChunk) {
  CheckDecoderMultiChunk(GreenAVIFTestCase());
}

TEST_F(ImageDecoders, AVIFLargeMultiChunk) {
  CheckDecoderMultiChunk(LargeAVIFTestCase(), /* aChunkSize */ 64);
}

TEST_F(ImageDecoders, AVIFDownscaleDuringDecode) {
  CheckDownscaleDuringDecode(DownscaledAVIFTestCase());
}

#ifdef MOZ_JXL
TEST_F(ImageDecoders, JXLLargeMultiChunk) {
  CheckDecoderMultiChunk(LargeJXLTestCase(), /* aChunkSize */ 64);
}
#endif

TEST_F(ImageDecoders, AnimatedGIFSingleChunk) {
  CheckDecoderSingleChunk(GreenFirstFrameAnimatedGIFTestCase());
}

TEST_F(ImageDecoders, AnimatedGIFMultiChunk) {
  CheckDecoderMultiChunk(GreenFirstFrameAnimatedGIFTestCase());
}

TEST_F(ImageDecoders, AnimatedGIFWithBlendedFrames) {
  CheckAnimationDecoderSingleChunk(GreenFirstFrameAnimatedGIFTestCase());
}

TEST_F(ImageDecoders, AnimatedPNGSingleChunk) {
  CheckDecoderSingleChunk(GreenFirstFrameAnimatedPNGTestCase());
}

TEST_F(ImageDecoders, AnimatedPNGMultiChunk) {
  CheckDecoderMultiChunk(GreenFirstFrameAnimatedPNGTestCase());
}

TEST_F(ImageDecoders, AnimatedPNGWithBlendedFrames) {
  CheckAnimationDecoderSingleChunk(GreenFirstFrameAnimatedPNGTestCase());
}

TEST_F(ImageDecoders, AnimatedWebPSingleChunk) {
  CheckDecoderSingleChunk(GreenFirstFrameAnimatedWebPTestCase());
}

TEST_F(ImageDecoders, AnimatedWebPMultiChunk) {
  CheckDecoderMultiChunk(GreenFirstFrameAnimatedWebPTestCase());
}

TEST_F(ImageDecoders, AnimatedWebPWithBlendedFrames) {
  CheckAnimationDecoderSingleChunk(GreenFirstFrameAnimatedWebPTestCase());
}

TEST_F(ImageDecoders, AnimatedAVIFSingleChunk) {
  CheckDecoderSingleChunk(GreenFirstFrameAnimatedAVIFTestCase());
}

TEST_F(ImageDecoders, AnimatedAVIFMultiChunk) {
  CheckDecoderMultiChunk(GreenFirstFrameAnimatedAVIFTestCase());
}

TEST_F(ImageDecoders, AnimatedAVIFWithBlendedFrames) {
  CheckAnimationDecoderSingleChunk(GreenFirstFrameAnimatedAVIFTestCase());
}

TEST_F(ImageDecoders, CorruptSingleChunk) {
  CheckDecoderSingleChunk(CorruptTestCase());
}

TEST_F(ImageDecoders, CorruptMultiChunk) {
  CheckDecoderMultiChunk(CorruptTestCase());
}

TEST_F(ImageDecoders, CorruptBMPWithTruncatedHeaderSingleChunk) {
  CheckDecoderSingleChunk(CorruptBMPWithTruncatedHeader());
}

TEST_F(ImageDecoders, CorruptBMPWithTruncatedHeaderMultiChunk) {
  CheckDecoderMultiChunk(CorruptBMPWithTruncatedHeader());
}

TEST_F(ImageDecoders, CorruptICOWithBadBMPWidthSingleChunk) {
  CheckDecoderSingleChunk(CorruptICOWithBadBMPWidthTestCase());
}

TEST_F(ImageDecoders, CorruptICOWithBadBMPWidthMultiChunk) {
  CheckDecoderMultiChunk(CorruptICOWithBadBMPWidthTestCase());
}

TEST_F(ImageDecoders, CorruptICOWithBadBMPHeightSingleChunk) {
  CheckDecoderSingleChunk(CorruptICOWithBadBMPHeightTestCase());
}

TEST_F(ImageDecoders, CorruptICOWithBadBMPHeightMultiChunk) {
  CheckDecoderMultiChunk(CorruptICOWithBadBMPHeightTestCase());
}

TEST_F(ImageDecoders, CorruptICOWithBadBppSingleChunk) {
  CheckDecoderSingleChunk(CorruptICOWithBadBppTestCase());
}

// Running this test under emulation for Android 7 on x86_64 seems to result
// in the large allocation succeeding, but leaving so little memory left the
// system falls over and it kills the test run, so we skip it instead.
// See bug 1655846 for more details.
#ifndef ANDROID
TEST_F(ImageDecoders, CorruptAVIFSingleChunk) {
  CheckDecoderSingleChunk(CorruptAVIFTestCase());
}
#endif

TEST_F(ImageDecoders, AnimatedGIFWithFRAME_FIRST) {
  CheckDecoderFrameFirst(GreenFirstFrameAnimatedGIFTestCase());
}

TEST_F(ImageDecoders, AnimatedGIFWithFRAME_CURRENT) {
  CheckDecoderFrameCurrent(GreenFirstFrameAnimatedGIFTestCase());
}

TEST_F(ImageDecoders, AnimatedGIFWithExtraImageSubBlocks) {
  ImageTestCase testCase = ExtraImageSubBlocksAnimatedGIFTestCase();

  // Verify that we can decode this test case and get two frames, even though
  // there are extra image sub blocks between the first and second frame. The
  // extra data shouldn't confuse the decoder or cause the decode to fail.

  // Create an image.
  RefPtr<Image> image = TestCaseToDecodedImage(testCase);

  // Ensure that the image's metadata meets our expectations.
  IntSize imageSize(0, 0);
  nsresult rv = image->GetWidth(&imageSize.width);
  EXPECT_NS_SUCCEEDED(rv);
  rv = image->GetHeight(&imageSize.height);
  EXPECT_NS_SUCCEEDED(rv);

  EXPECT_EQ(testCase.mSize.width, imageSize.width);
  EXPECT_EQ(testCase.mSize.height, imageSize.height);

  RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
  Progress imageProgress = tracker->GetProgress();

  EXPECT_TRUE(bool(imageProgress & FLAG_HAS_TRANSPARENCY) == false);
  EXPECT_TRUE(bool(imageProgress & FLAG_IS_ANIMATED) == true);

  // Ensure that we decoded both frames of the image.
  LookupResult result =
      SurfaceCache::Lookup(ImageKey(image.get()),
                           RasterSurfaceKey(imageSize, testCase.mSurfaceFlags,
                                            PlaybackType::eAnimated),
                           /* aMarkUsed = */ true);
  ASSERT_EQ(MatchType::EXACT, result.Type());

  EXPECT_NS_SUCCEEDED(result.Surface().Seek(0));
  EXPECT_TRUE(bool(result.Surface()));

  RefPtr<imgFrame> partialFrame = result.Surface().GetFrame(1);
  EXPECT_TRUE(bool(partialFrame));
}

TEST_F(ImageDecoders, AnimatedWebPWithFRAME_FIRST) {
  CheckDecoderFrameFirst(GreenFirstFrameAnimatedWebPTestCase());
}

TEST_F(ImageDecoders, AnimatedWebPWithFRAME_CURRENT) {
  CheckDecoderFrameCurrent(GreenFirstFrameAnimatedWebPTestCase());
}

TEST_F(ImageDecoders, TruncatedSmallGIFSingleChunk) {
  CheckDecoderSingleChunk(TruncatedSmallGIFTestCase());
}

TEST_F(ImageDecoders, LargeICOWithBMPSingleChunk) {
  CheckDecoderSingleChunk(LargeICOWithBMPTestCase());
}

TEST_F(ImageDecoders, LargeICOWithBMPMultiChunk) {
  CheckDecoderMultiChunk(LargeICOWithBMPTestCase(), /* aChunkSize */ 64);
}

TEST_F(ImageDecoders, LargeICOWithPNGSingleChunk) {
  CheckDecoderSingleChunk(LargeICOWithPNGTestCase());
}

TEST_F(ImageDecoders, LargeICOWithPNGMultiChunk) {
  CheckDecoderMultiChunk(LargeICOWithPNGTestCase());
}

TEST_F(ImageDecoders, MultipleSizesICOSingleChunk) {
  ImageTestCase testCase = GreenMultipleSizesICOTestCase();

  // Create an image.
  RefPtr<Image> image = ImageFactory::CreateAnonymousImage(
      nsDependentCString(testCase.mMimeType));
  ASSERT_TRUE(!image->HasError());

  nsCOMPtr<nsIInputStream> inputStream = LoadFile(testCase.mPath);
  ASSERT_TRUE(inputStream);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_NS_SUCCEEDED(rv);

  // Write the data into the image.
  rv = image->OnImageDataAvailable(nullptr, inputStream, 0,
                                   static_cast<uint32_t>(length));
  ASSERT_NS_SUCCEEDED(rv);

  // Let the image know we've sent all the data.
  rv = image->OnImageDataComplete(nullptr, NS_OK, true);
  ASSERT_NS_SUCCEEDED(rv);

  RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
  tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);

  // Use GetFrame() to force a sync decode of the image.
  RefPtr<SourceSurface> surface = image->GetFrame(
      imgIContainer::FRAME_CURRENT, imgIContainer::FLAG_SYNC_DECODE);

  // Ensure that the image's metadata meets our expectations.
  IntSize imageSize(0, 0);
  rv = image->GetWidth(&imageSize.width);
  EXPECT_NS_SUCCEEDED(rv);
  rv = image->GetHeight(&imageSize.height);
  EXPECT_NS_SUCCEEDED(rv);

  EXPECT_EQ(testCase.mSize.width, imageSize.width);
  EXPECT_EQ(testCase.mSize.height, imageSize.height);

  nsTArray<IntSize> nativeSizes;
  rv = image->GetNativeSizes(nativeSizes);
  EXPECT_NS_SUCCEEDED(rv);
  ASSERT_EQ(6u, nativeSizes.Length());

  IntSize expectedSizes[] = {IntSize(16, 16),   IntSize(32, 32),
                             IntSize(64, 64),   IntSize(128, 128),
                             IntSize(256, 256), IntSize(256, 128)};

  for (int i = 0; i < 6; ++i) {
    EXPECT_EQ(expectedSizes[i], nativeSizes[i]);
  }

  RefPtr<Image> image90 =
      ImageOps::Orient(image, Orientation(Angle::D90, Flip::Unflipped));
  rv = image90->GetNativeSizes(nativeSizes);
  EXPECT_NS_SUCCEEDED(rv);
  ASSERT_EQ(6u, nativeSizes.Length());

  for (int i = 0; i < 5; ++i) {
    EXPECT_EQ(expectedSizes[i], nativeSizes[i]);
  }
  EXPECT_EQ(IntSize(128, 256), nativeSizes[5]);

  RefPtr<Image> image180 =
      ImageOps::Orient(image, Orientation(Angle::D180, Flip::Unflipped));
  rv = image180->GetNativeSizes(nativeSizes);
  EXPECT_NS_SUCCEEDED(rv);
  ASSERT_EQ(6u, nativeSizes.Length());

  for (int i = 0; i < 6; ++i) {
    EXPECT_EQ(expectedSizes[i], nativeSizes[i]);
  }
}

TEST_F(ImageDecoders, ExifResolutionEven) {
  RefPtr<Image> image = TestCaseToDecodedImage(ExifResolutionTestCase());
  EXPECT_EQ(image->GetResolution(), Resolution(2.0, 2.0));
}