summaryrefslogtreecommitdiffstats
path: root/image/test/gtest/TestSourceBuffer.cpp
blob: 478ab5661055a278daf32ff2eb495923faa3cce5 (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
/* 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 <algorithm>
#include <cstdint>
#include <utility>

#include "Common.h"
#include "SourceBuffer.h"
#include "SurfaceCache.h"
#include "gtest/gtest.h"
#include "nsIInputStream.h"

using namespace mozilla;
using namespace mozilla::image;

using std::min;

void ExpectChunkAndByteCount(const SourceBufferIterator& aIterator,
                             uint32_t aChunks, size_t aBytes) {
  EXPECT_EQ(aChunks, aIterator.ChunkCount());
  EXPECT_EQ(aBytes, aIterator.ByteCount());
}

void ExpectRemainingBytes(const SourceBufferIterator& aIterator,
                          size_t aBytes) {
  EXPECT_TRUE(aIterator.RemainingBytesIsNoMoreThan(aBytes));
  EXPECT_TRUE(aIterator.RemainingBytesIsNoMoreThan(aBytes + 1));

  if (aBytes > 0) {
    EXPECT_FALSE(aIterator.RemainingBytesIsNoMoreThan(0));
    EXPECT_FALSE(aIterator.RemainingBytesIsNoMoreThan(aBytes - 1));
  }
}

char GenerateByte(size_t aIndex) {
  uint8_t byte = aIndex % 256;
  return *reinterpret_cast<char*>(&byte);
}

void GenerateData(char* aOutput, size_t aOffset, size_t aLength) {
  for (size_t i = 0; i < aLength; ++i) {
    aOutput[i] = GenerateByte(aOffset + i);
  }
}

void GenerateData(char* aOutput, size_t aLength) {
  GenerateData(aOutput, 0, aLength);
}

void CheckData(const char* aData, size_t aOffset, size_t aLength) {
  for (size_t i = 0; i < aLength; ++i) {
    ASSERT_EQ(GenerateByte(aOffset + i), aData[i]);
  }
}

enum class AdvanceMode { eAdvanceAsMuchAsPossible, eAdvanceByLengthExactly };

class ImageSourceBuffer : public ::testing::Test {
 public:
  ImageSourceBuffer()
      : mSourceBuffer(new SourceBuffer),
        mExpectNoResume(new ExpectNoResume),
        mCountResumes(new CountResumes) {
    GenerateData(mData, sizeof(mData));
    EXPECT_FALSE(mSourceBuffer->IsComplete());
  }

 protected:
  void CheckedAppendToBuffer(const char* aData, size_t aLength) {
    EXPECT_NS_SUCCEEDED(mSourceBuffer->Append(aData, aLength));
  }

  void CheckedAppendToBufferLastByteForLength(size_t aLength) {
    const char lastByte = GenerateByte(aLength);
    CheckedAppendToBuffer(&lastByte, 1);
  }

  void CheckedAppendToBufferInChunks(size_t aChunkLength, size_t aTotalLength) {
    char* data = new char[aChunkLength];

    size_t bytesWritten = 0;
    while (bytesWritten < aTotalLength) {
      GenerateData(data, bytesWritten, aChunkLength);
      size_t toWrite = min(aChunkLength, aTotalLength - bytesWritten);
      CheckedAppendToBuffer(data, toWrite);
      bytesWritten += toWrite;
    }

    delete[] data;
  }

  void CheckedCompleteBuffer(nsresult aCompletionStatus = NS_OK) {
    mSourceBuffer->Complete(aCompletionStatus);
    EXPECT_TRUE(mSourceBuffer->IsComplete());
  }

  void CheckedCompleteBuffer(SourceBufferIterator& aIterator, size_t aLength,
                             nsresult aCompletionStatus = NS_OK) {
    CheckedCompleteBuffer(aCompletionStatus);
    ExpectRemainingBytes(aIterator, aLength);
  }

  void CheckedAdvanceIteratorStateOnly(
      SourceBufferIterator& aIterator, size_t aLength, uint32_t aChunks,
      size_t aTotalLength,
      AdvanceMode aAdvanceMode = AdvanceMode::eAdvanceAsMuchAsPossible) {
    const size_t advanceBy =
        aAdvanceMode == AdvanceMode::eAdvanceAsMuchAsPossible ? SIZE_MAX
                                                              : aLength;

    auto state = aIterator.AdvanceOrScheduleResume(advanceBy, mExpectNoResume);
    ASSERT_EQ(SourceBufferIterator::READY, state);
    EXPECT_TRUE(aIterator.Data());
    EXPECT_EQ(aLength, aIterator.Length());

    ExpectChunkAndByteCount(aIterator, aChunks, aTotalLength);
  }

  void CheckedAdvanceIteratorStateOnly(SourceBufferIterator& aIterator,
                                       size_t aLength) {
    CheckedAdvanceIteratorStateOnly(aIterator, aLength, 1, aLength);
  }

  void CheckedAdvanceIterator(
      SourceBufferIterator& aIterator, size_t aLength, uint32_t aChunks,
      size_t aTotalLength,
      AdvanceMode aAdvanceMode = AdvanceMode::eAdvanceAsMuchAsPossible) {
    // Check that the iterator is in the expected state.
    CheckedAdvanceIteratorStateOnly(aIterator, aLength, aChunks, aTotalLength,
                                    aAdvanceMode);

    // Check that we read the expected data. To do this, we need to compute our
    // offset in the SourceBuffer, but fortunately that's pretty easy: it's the
    // total number of bytes the iterator has advanced through, minus the length
    // of the current chunk.
    const size_t offset = aIterator.ByteCount() - aIterator.Length();
    CheckData(aIterator.Data(), offset, aIterator.Length());
  }

  void CheckedAdvanceIterator(SourceBufferIterator& aIterator, size_t aLength) {
    CheckedAdvanceIterator(aIterator, aLength, 1, aLength);
  }

  void CheckIteratorMustWait(SourceBufferIterator& aIterator,
                             IResumable* aOnResume) {
    auto state = aIterator.AdvanceOrScheduleResume(1, aOnResume);
    EXPECT_EQ(SourceBufferIterator::WAITING, state);
  }

  void CheckIteratorIsComplete(SourceBufferIterator& aIterator,
                               uint32_t aChunks, size_t aTotalLength,
                               nsresult aCompletionStatus = NS_OK) {
    ASSERT_TRUE(mSourceBuffer->IsComplete());
    auto state = aIterator.AdvanceOrScheduleResume(1, mExpectNoResume);
    ASSERT_EQ(SourceBufferIterator::COMPLETE, state);
    EXPECT_EQ(aCompletionStatus, aIterator.CompletionStatus());
    ExpectRemainingBytes(aIterator, 0);
    ExpectChunkAndByteCount(aIterator, aChunks, aTotalLength);
  }

  void CheckIteratorIsComplete(SourceBufferIterator& aIterator,
                               size_t aTotalLength) {
    CheckIteratorIsComplete(aIterator, 1, aTotalLength);
  }

  AutoInitializeImageLib mInit;
  char mData[9];
  RefPtr<SourceBuffer> mSourceBuffer;
  RefPtr<ExpectNoResume> mExpectNoResume;
  RefPtr<CountResumes> mCountResumes;
};

TEST_F(ImageSourceBuffer, InitialState) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // RemainingBytesIsNoMoreThan() should always return false in the initial
  // state, since we can't know the answer until Complete() has been called.
  EXPECT_FALSE(iterator.RemainingBytesIsNoMoreThan(0));
  EXPECT_FALSE(iterator.RemainingBytesIsNoMoreThan(SIZE_MAX));

  // We haven't advanced our iterator at all, so its counters should be zero.
  ExpectChunkAndByteCount(iterator, 0, 0);

  // Attempt to advance; we should fail, and end up in the WAITING state. We
  // expect no resumes because we don't actually append anything to the
  // SourceBuffer in this test.
  CheckIteratorMustWait(iterator, mExpectNoResume);
}

TEST_F(ImageSourceBuffer, ZeroLengthBufferAlwaysFails) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Complete the buffer without writing to it, providing a successful
  // completion status.
  CheckedCompleteBuffer(iterator, 0);

  // Completing a buffer without writing to it results in an automatic failure;
  // make sure that the actual completion status we get from the iterator
  // reflects this.
  CheckIteratorIsComplete(iterator, 0, 0, NS_ERROR_FAILURE);
}

TEST_F(ImageSourceBuffer, CompleteSuccess) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write a single byte to the buffer and complete the buffer. (We have to
  // write at least one byte because completing a zero length buffer always
  // fails; see the ZeroLengthBufferAlwaysFails test.)
  CheckedAppendToBuffer(mData, 1);
  CheckedCompleteBuffer(iterator, 1);

  // We should be able to advance once (to read the single byte) and then should
  // reach the COMPLETE state with a successful status.
  CheckedAdvanceIterator(iterator, 1);
  CheckIteratorIsComplete(iterator, 1);
}

TEST_F(ImageSourceBuffer, CompleteFailure) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write a single byte to the buffer and complete the buffer. (We have to
  // write at least one byte because completing a zero length buffer always
  // fails; see the ZeroLengthBufferAlwaysFails test.)
  CheckedAppendToBuffer(mData, 1);
  CheckedCompleteBuffer(iterator, 1, NS_ERROR_FAILURE);

  // Advance the iterator. Because a failing status is propagated to the
  // iterator as soon as it advances, we won't be able to read the single byte
  // that we wrote above; we go directly into the COMPLETE state.
  CheckIteratorIsComplete(iterator, 0, 0, NS_ERROR_FAILURE);
}

TEST_F(ImageSourceBuffer, Append) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write test data to the buffer.
  EXPECT_NS_SUCCEEDED(mSourceBuffer->ExpectLength(sizeof(mData)));
  CheckedAppendToBuffer(mData, sizeof(mData));
  CheckedCompleteBuffer(iterator, sizeof(mData));

  // Verify that we can read it back via the iterator, and that the final state
  // is what we expect.
  CheckedAdvanceIterator(iterator, sizeof(mData));
  CheckIteratorIsComplete(iterator, sizeof(mData));
}

TEST_F(ImageSourceBuffer, HugeAppendFails) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // We should fail to append anything bigger than what the SurfaceCache can
  // hold, so use the SurfaceCache's maximum capacity to calculate what a
  // "massive amount of data" (see below) consists of on this platform.
  ASSERT_LT(SurfaceCache::MaximumCapacity(), SIZE_MAX);
  const size_t hugeSize = SurfaceCache::MaximumCapacity() + 1;

  // Attempt to write a massive amount of data and verify that it fails. (We'd
  // get a buffer overrun during the test if it succeeds, but if it succeeds
  // that's the least of our problems.)
  EXPECT_NS_FAILED(mSourceBuffer->Append(mData, hugeSize));
  EXPECT_TRUE(mSourceBuffer->IsComplete());
  CheckIteratorIsComplete(iterator, 0, 0, NS_ERROR_OUT_OF_MEMORY);
}

TEST_F(ImageSourceBuffer, AppendFromInputStream) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Construct an input stream with some arbitrary data. (We use test data from
  // one of the decoder tests.)
  nsCOMPtr<nsIInputStream> inputStream = LoadFile(GreenPNGTestCase().mPath);
  ASSERT_TRUE(inputStream != nullptr);

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

  // Write test data to the buffer.
  EXPECT_TRUE(
      NS_SUCCEEDED(mSourceBuffer->AppendFromInputStream(inputStream, length)));
  CheckedCompleteBuffer(iterator, length);

  // Verify that the iterator sees the appropriate amount of data.
  CheckedAdvanceIteratorStateOnly(iterator, length);
  CheckIteratorIsComplete(iterator, length);
}

TEST_F(ImageSourceBuffer, AppendAfterComplete) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write test data to the buffer.
  EXPECT_NS_SUCCEEDED(mSourceBuffer->ExpectLength(sizeof(mData)));
  CheckedAppendToBuffer(mData, sizeof(mData));
  CheckedCompleteBuffer(iterator, sizeof(mData));

  // Verify that we can read it back via the iterator, and that the final state
  // is what we expect.
  CheckedAdvanceIterator(iterator, sizeof(mData));
  CheckIteratorIsComplete(iterator, sizeof(mData));

  // Write more data to the completed buffer.
  EXPECT_NS_FAILED(mSourceBuffer->Append(mData, sizeof(mData)));

  // Try to read with a new iterator and verify that the new data got ignored.
  SourceBufferIterator iterator2 = mSourceBuffer->Iterator();
  CheckedAdvanceIterator(iterator2, sizeof(mData));
  CheckIteratorIsComplete(iterator2, sizeof(mData));
}

TEST_F(ImageSourceBuffer, MinChunkCapacity) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write test data to the buffer using many small appends. Since
  // ExpectLength() isn't being called, we should be able to write up to
  // SourceBuffer::MIN_CHUNK_CAPACITY bytes without a second chunk being
  // allocated.
  CheckedAppendToBufferInChunks(10, SourceBuffer::MIN_CHUNK_CAPACITY);

  // Verify that the iterator sees the appropriate amount of data.
  CheckedAdvanceIterator(iterator, SourceBuffer::MIN_CHUNK_CAPACITY);

  // Write one more byte; we expect to see that it triggers an allocation.
  CheckedAppendToBufferLastByteForLength(SourceBuffer::MIN_CHUNK_CAPACITY);
  CheckedCompleteBuffer(iterator, 1);

  // Verify that the iterator sees the new byte and a new chunk has been
  // allocated.
  CheckedAdvanceIterator(iterator, 1, 2, SourceBuffer::MIN_CHUNK_CAPACITY + 1);
  CheckIteratorIsComplete(iterator, 2, SourceBuffer::MIN_CHUNK_CAPACITY + 1);
}

TEST_F(ImageSourceBuffer, ExpectLengthAllocatesRequestedCapacity) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write SourceBuffer::MIN_CHUNK_CAPACITY bytes of test data to the buffer,
  // but call ExpectLength() first to make SourceBuffer expect only a single
  // byte. We expect this to still result in two chunks, because we trust the
  // initial guess of ExpectLength() but after that it will only allocate chunks
  // of at least MIN_CHUNK_CAPACITY bytes.
  EXPECT_NS_SUCCEEDED(mSourceBuffer->ExpectLength(1));
  CheckedAppendToBufferInChunks(10, SourceBuffer::MIN_CHUNK_CAPACITY);
  CheckedCompleteBuffer(iterator, SourceBuffer::MIN_CHUNK_CAPACITY);

  // Verify that the iterator sees a first chunk with 1 byte, and a second chunk
  // with the remaining data.
  CheckedAdvanceIterator(iterator, 1, 1, 1);
  CheckedAdvanceIterator(iterator, SourceBuffer::MIN_CHUNK_CAPACITY - 1, 2,
                         SourceBuffer::MIN_CHUNK_CAPACITY);
  CheckIteratorIsComplete(iterator, 2, SourceBuffer::MIN_CHUNK_CAPACITY);
}

TEST_F(ImageSourceBuffer, ExpectLengthGrowsAboveMinCapacity) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write two times SourceBuffer::MIN_CHUNK_CAPACITY bytes of test data to the
  // buffer, calling ExpectLength() with the correct length first. We expect
  // this to result in only one chunk, because ExpectLength() allows us to
  // allocate a larger first chunk than MIN_CHUNK_CAPACITY bytes.
  const size_t length = 2 * SourceBuffer::MIN_CHUNK_CAPACITY;
  EXPECT_NS_SUCCEEDED(mSourceBuffer->ExpectLength(length));
  CheckedAppendToBufferInChunks(10, length);

  // Verify that the iterator sees a single chunk.
  CheckedAdvanceIterator(iterator, length);

  // Write one more byte; we expect to see that it triggers an allocation.
  CheckedAppendToBufferLastByteForLength(length);
  CheckedCompleteBuffer(iterator, 1);

  // Verify that the iterator sees the new byte and a new chunk has been
  // allocated.
  CheckedAdvanceIterator(iterator, 1, 2, length + 1);
  CheckIteratorIsComplete(iterator, 2, length + 1);
}

TEST_F(ImageSourceBuffer, HugeExpectLengthFails) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // ExpectLength() should fail if the length is bigger than what the
  // SurfaceCache can hold, so use the SurfaceCache's maximum capacity to
  // calculate what a "massive amount of data" (see below) consists of on this
  // platform.
  ASSERT_LT(SurfaceCache::MaximumCapacity(), SIZE_MAX);
  const size_t hugeSize = SurfaceCache::MaximumCapacity() + 1;

  // Attempt to write a massive amount of data and verify that it fails. (We'd
  // get a buffer overrun during the test if it succeeds, but if it succeeds
  // that's the least of our problems.)
  EXPECT_NS_FAILED(mSourceBuffer->ExpectLength(hugeSize));
  EXPECT_TRUE(mSourceBuffer->IsComplete());
  CheckIteratorIsComplete(iterator, 0, 0, NS_ERROR_INVALID_ARG);
}

TEST_F(ImageSourceBuffer, LargeAppendsAllocateOnlyOneChunk) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Write two times SourceBuffer::MIN_CHUNK_CAPACITY bytes of test data to the
  // buffer in a single Append() call. We expect this to result in only one
  // chunk even though ExpectLength() wasn't called, because we should always
  // allocate a new chunk large enough to store the data we have at hand.
  constexpr size_t length = 2 * SourceBuffer::MIN_CHUNK_CAPACITY;
  char data[length];
  GenerateData(data, sizeof(data));
  CheckedAppendToBuffer(data, length);

  // Verify that the iterator sees a single chunk.
  CheckedAdvanceIterator(iterator, length);

  // Write one more byte; we expect to see that it triggers an allocation.
  CheckedAppendToBufferLastByteForLength(length);
  CheckedCompleteBuffer(iterator, 1);

  // Verify that the iterator sees the new byte and a new chunk has been
  // allocated.
  CheckedAdvanceIterator(iterator, 1, 2, length + 1);
  CheckIteratorIsComplete(iterator, 2, length + 1);
}

TEST_F(ImageSourceBuffer, LargeAppendsAllocateAtMostOneChunk) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Allocate some data we'll use below.
  constexpr size_t firstWriteLength = SourceBuffer::MIN_CHUNK_CAPACITY / 2;
  constexpr size_t secondWriteLength = 3 * SourceBuffer::MIN_CHUNK_CAPACITY;
  constexpr size_t totalLength = firstWriteLength + secondWriteLength;
  char data[totalLength];
  GenerateData(data, sizeof(data));

  // Write half of SourceBuffer::MIN_CHUNK_CAPACITY bytes of test data to the
  // buffer in a single Append() call. This should fill half of the first chunk.
  CheckedAppendToBuffer(data, firstWriteLength);

  // Write three times SourceBuffer::MIN_CHUNK_CAPACITY bytes of test data to
  // the buffer in a single Append() call. We expect this to result in the first
  // of the first chunk being filled and a new chunk being allocated for the
  // remainder.
  CheckedAppendToBuffer(data + firstWriteLength, secondWriteLength);

  // Verify that the iterator sees a MIN_CHUNK_CAPACITY-length chunk.
  CheckedAdvanceIterator(iterator, SourceBuffer::MIN_CHUNK_CAPACITY);

  // Verify that the iterator sees a second chunk of the length we expect.
  const size_t expectedSecondChunkLength =
      totalLength - SourceBuffer::MIN_CHUNK_CAPACITY;
  CheckedAdvanceIterator(iterator, expectedSecondChunkLength, 2, totalLength);

  // Write one more byte; we expect to see that it triggers an allocation.
  CheckedAppendToBufferLastByteForLength(totalLength);
  CheckedCompleteBuffer(iterator, 1);

  // Verify that the iterator sees the new byte and a new chunk has been
  // allocated.
  CheckedAdvanceIterator(iterator, 1, 3, totalLength + 1);
  CheckIteratorIsComplete(iterator, 3, totalLength + 1);
}

TEST_F(ImageSourceBuffer, OversizedAppendsAllocateAtMostOneChunk) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Allocate some data we'll use below.
  constexpr size_t writeLength = SourceBuffer::MAX_CHUNK_CAPACITY + 1;

  // Write SourceBuffer::MAX_CHUNK_CAPACITY + 1 bytes of test data to the
  // buffer in a single Append() call. This should cause one chunk to be
  // allocated because we wrote it as a single block.
  CheckedAppendToBufferInChunks(writeLength, writeLength);

  // Verify that the iterator sees a MAX_CHUNK_CAPACITY+1-length chunk.
  CheckedAdvanceIterator(iterator, writeLength);

  CheckedCompleteBuffer(NS_OK);
  CheckIteratorIsComplete(iterator, 1, writeLength);
}

TEST_F(ImageSourceBuffer, CompactionHappensWhenBufferIsComplete) {
  constexpr size_t chunkLength = SourceBuffer::MIN_CHUNK_CAPACITY;
  constexpr size_t totalLength = 2 * chunkLength;

  // Write enough data to create two chunks.
  CheckedAppendToBufferInChunks(chunkLength, totalLength);

  {
    SourceBufferIterator iterator = mSourceBuffer->Iterator();

    // Verify that the iterator sees two chunks.
    CheckedAdvanceIterator(iterator, chunkLength);
    CheckedAdvanceIterator(iterator, chunkLength, 2, totalLength);
  }

  // Complete the buffer, which should trigger compaction implicitly.
  CheckedCompleteBuffer();

  {
    SourceBufferIterator iterator = mSourceBuffer->Iterator();

    // Verify that compaction happened and there's now only one chunk.
    CheckedAdvanceIterator(iterator, totalLength);
    CheckIteratorIsComplete(iterator, 1, totalLength);
  }
}

TEST_F(ImageSourceBuffer, CompactionIsDelayedWhileIteratorsExist) {
  constexpr size_t chunkLength = SourceBuffer::MIN_CHUNK_CAPACITY;
  constexpr size_t totalLength = 2 * chunkLength;

  {
    SourceBufferIterator outerIterator = mSourceBuffer->Iterator();

    {
      SourceBufferIterator iterator = mSourceBuffer->Iterator();

      // Write enough data to create two chunks.
      CheckedAppendToBufferInChunks(chunkLength, totalLength);
      CheckedCompleteBuffer(iterator, totalLength);

      // Verify that the iterator sees two chunks. Since there are live
      // iterators, compaction shouldn't have happened when we completed the
      // buffer.
      CheckedAdvanceIterator(iterator, chunkLength);
      CheckedAdvanceIterator(iterator, chunkLength, 2, totalLength);
      CheckIteratorIsComplete(iterator, 2, totalLength);
    }

    // Now |iterator| has been destroyed, but |outerIterator| still exists, so
    // we expect no compaction to have occurred at this point.
    CheckedAdvanceIterator(outerIterator, chunkLength);
    CheckedAdvanceIterator(outerIterator, chunkLength, 2, totalLength);
    CheckIteratorIsComplete(outerIterator, 2, totalLength);
  }

  // Now all iterators have been destroyed. Since the buffer was already
  // complete, we expect compaction to happen implicitly here.

  {
    SourceBufferIterator iterator = mSourceBuffer->Iterator();

    // Verify that compaction happened and there's now only one chunk.
    CheckedAdvanceIterator(iterator, totalLength);
    CheckIteratorIsComplete(iterator, 1, totalLength);
  }
}

TEST_F(ImageSourceBuffer, SourceBufferIteratorsCanBeMoved) {
  constexpr size_t chunkLength = SourceBuffer::MIN_CHUNK_CAPACITY;
  constexpr size_t totalLength = 2 * chunkLength;

  // Write enough data to create two chunks. We create an iterator here to make
  // sure that compaction doesn't happen during the test.
  SourceBufferIterator iterator = mSourceBuffer->Iterator();
  CheckedAppendToBufferInChunks(chunkLength, totalLength);
  CheckedCompleteBuffer(iterator, totalLength);

  auto GetIterator = [&] {
    SourceBufferIterator lambdaIterator = mSourceBuffer->Iterator();
    CheckedAdvanceIterator(lambdaIterator, chunkLength);
    return lambdaIterator;
  };

  // Move-construct |movedIterator| from the iterator returned from
  // GetIterator() and check that its state is as we expect.
  SourceBufferIterator tmpIterator = GetIterator();
  SourceBufferIterator movedIterator(std::move(tmpIterator));
  EXPECT_TRUE(movedIterator.Data());
  EXPECT_EQ(chunkLength, movedIterator.Length());
  ExpectChunkAndByteCount(movedIterator, 1, chunkLength);

  // Make sure that we can advance the iterator.
  CheckedAdvanceIterator(movedIterator, chunkLength, 2, totalLength);

  // Make sure that the iterator handles completion properly.
  CheckIteratorIsComplete(movedIterator, 2, totalLength);

  // Move-assign |movedIterator| from the iterator returned from
  // GetIterator() and check that its state is as we expect.
  tmpIterator = GetIterator();
  movedIterator = std::move(tmpIterator);
  EXPECT_TRUE(movedIterator.Data());
  EXPECT_EQ(chunkLength, movedIterator.Length());
  ExpectChunkAndByteCount(movedIterator, 1, chunkLength);

  // Make sure that we can advance the iterator.
  CheckedAdvanceIterator(movedIterator, chunkLength, 2, totalLength);

  // Make sure that the iterator handles completion properly.
  CheckIteratorIsComplete(movedIterator, 2, totalLength);
}

TEST_F(ImageSourceBuffer, SubchunkAdvance) {
  constexpr size_t chunkLength = SourceBuffer::MIN_CHUNK_CAPACITY;
  constexpr size_t totalLength = 2 * chunkLength;

  // Write enough data to create two chunks. We create our iterator here to make
  // sure that compaction doesn't happen during the test.
  SourceBufferIterator iterator = mSourceBuffer->Iterator();
  CheckedAppendToBufferInChunks(chunkLength, totalLength);
  CheckedCompleteBuffer(iterator, totalLength);

  // Advance through the first chunk. The chunk count should not increase.
  // We check that by always passing 1 for the |aChunks| parameter of
  // CheckedAdvanceIteratorStateOnly(). We have to call CheckData() manually
  // because the offset calculation in CheckedAdvanceIterator() assumes that
  // we're advancing a chunk at a time.
  size_t offset = 0;
  while (offset < chunkLength) {
    CheckedAdvanceIteratorStateOnly(iterator, 1, 1, chunkLength,
                                    AdvanceMode::eAdvanceByLengthExactly);
    CheckData(iterator.Data(), offset++, iterator.Length());
  }

  // Read the first byte of the second chunk. This is the point at which we
  // can't advance within the same chunk, so the chunk count should increase. We
  // check that by passing 2 for the |aChunks| parameter of
  // CheckedAdvanceIteratorStateOnly().
  CheckedAdvanceIteratorStateOnly(iterator, 1, 2, totalLength,
                                  AdvanceMode::eAdvanceByLengthExactly);
  CheckData(iterator.Data(), offset++, iterator.Length());

  // Read the rest of the second chunk. The chunk count should not increase.
  while (offset < totalLength) {
    CheckedAdvanceIteratorStateOnly(iterator, 1, 2, totalLength,
                                    AdvanceMode::eAdvanceByLengthExactly);
    CheckData(iterator.Data(), offset++, iterator.Length());
  }

  // Make sure we reached the end.
  CheckIteratorIsComplete(iterator, 2, totalLength);
}

TEST_F(ImageSourceBuffer, SubchunkZeroByteAdvance) {
  constexpr size_t chunkLength = SourceBuffer::MIN_CHUNK_CAPACITY;
  constexpr size_t totalLength = 2 * chunkLength;

  // Write enough data to create two chunks. We create our iterator here to make
  // sure that compaction doesn't happen during the test.
  SourceBufferIterator iterator = mSourceBuffer->Iterator();
  CheckedAppendToBufferInChunks(chunkLength, totalLength);
  CheckedCompleteBuffer(iterator, totalLength);

  // Make an initial zero-length advance. Although a zero-length advance
  // normally won't cause us to read a chunk from the SourceBuffer, we'll do so
  // if the iterator is in the initial state to keep the invariant that
  // SourceBufferIterator in the READY state always returns a non-null pointer
  // from Data().
  CheckedAdvanceIteratorStateOnly(iterator, 0, 1, chunkLength,
                                  AdvanceMode::eAdvanceByLengthExactly);

  // Advance through the first chunk. As in the |SubchunkAdvance| test, the
  // chunk count should not increase. We do a zero-length advance after each
  // normal advance to ensure that zero-length advances do not change the
  // iterator's position or cause a new chunk to be read.
  size_t offset = 0;
  while (offset < chunkLength) {
    CheckedAdvanceIteratorStateOnly(iterator, 1, 1, chunkLength,
                                    AdvanceMode::eAdvanceByLengthExactly);
    CheckData(iterator.Data(), offset++, iterator.Length());
    CheckedAdvanceIteratorStateOnly(iterator, 0, 1, chunkLength,
                                    AdvanceMode::eAdvanceByLengthExactly);
  }

  // Read the first byte of the second chunk. This is the point at which we
  // can't advance within the same chunk, so the chunk count should increase. As
  // before, we do a zero-length advance afterward.
  CheckedAdvanceIteratorStateOnly(iterator, 1, 2, totalLength,
                                  AdvanceMode::eAdvanceByLengthExactly);
  CheckData(iterator.Data(), offset++, iterator.Length());
  CheckedAdvanceIteratorStateOnly(iterator, 0, 2, totalLength,
                                  AdvanceMode::eAdvanceByLengthExactly);

  // Read the rest of the second chunk. The chunk count should not increase. As
  // before, we do a zero-length advance after each normal advance.
  while (offset < totalLength) {
    CheckedAdvanceIteratorStateOnly(iterator, 1, 2, totalLength,
                                    AdvanceMode::eAdvanceByLengthExactly);
    CheckData(iterator.Data(), offset++, iterator.Length());
    CheckedAdvanceIteratorStateOnly(iterator, 0, 2, totalLength,
                                    AdvanceMode::eAdvanceByLengthExactly);
  }

  // Make sure we reached the end.
  CheckIteratorIsComplete(iterator, 2, totalLength);
}

TEST_F(ImageSourceBuffer, SubchunkZeroByteAdvanceWithNoData) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Check that advancing by zero bytes still makes us enter the WAITING state.
  // This is because if we entered the READY state before reading any data at
  // all, we'd break the invariant that SourceBufferIterator::Data() always
  // returns a non-null pointer in the READY state.
  auto state = iterator.AdvanceOrScheduleResume(0, mCountResumes);
  EXPECT_EQ(SourceBufferIterator::WAITING, state);

  // Call Complete(). This should trigger a resume.
  CheckedCompleteBuffer();
  EXPECT_EQ(1u, mCountResumes->Count());
}

TEST_F(ImageSourceBuffer, NullIResumable) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Check that we can't advance.
  CheckIteratorMustWait(iterator, nullptr);

  // Append to the buffer, which would cause a resume if we had passed a
  // non-null IResumable.
  CheckedAppendToBuffer(mData, sizeof(mData));
  CheckedCompleteBuffer(iterator, sizeof(mData));
}

TEST_F(ImageSourceBuffer, AppendTriggersResume) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Check that we can't advance.
  CheckIteratorMustWait(iterator, mCountResumes);

  // Call Append(). This should trigger a resume.
  mSourceBuffer->Append(mData, sizeof(mData));
  EXPECT_EQ(1u, mCountResumes->Count());
}

TEST_F(ImageSourceBuffer, OnlyOneResumeTriggeredPerAppend) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Check that we can't advance.
  CheckIteratorMustWait(iterator, mCountResumes);

  // Allocate some data we'll use below.
  constexpr size_t firstWriteLength = SourceBuffer::MIN_CHUNK_CAPACITY / 2;
  constexpr size_t secondWriteLength = 3 * SourceBuffer::MIN_CHUNK_CAPACITY;
  constexpr size_t totalLength = firstWriteLength + secondWriteLength;
  char data[totalLength];
  GenerateData(data, sizeof(data));

  // Write half of SourceBuffer::MIN_CHUNK_CAPACITY bytes of test data to the
  // buffer in a single Append() call. This should fill half of the first chunk.
  // This should trigger a resume.
  CheckedAppendToBuffer(data, firstWriteLength);
  EXPECT_EQ(1u, mCountResumes->Count());

  // Advance past the new data and wait again.
  CheckedAdvanceIterator(iterator, firstWriteLength);
  CheckIteratorMustWait(iterator, mCountResumes);

  // Write three times SourceBuffer::MIN_CHUNK_CAPACITY bytes of test data to
  // the buffer in a single Append() call. We expect this to result in the first
  // of the first chunk being filled and a new chunk being allocated for the
  // remainder. Even though two chunks are getting written to here, only *one*
  // resume should get triggered, for a total of two in this test.
  CheckedAppendToBuffer(data + firstWriteLength, secondWriteLength);
  EXPECT_EQ(2u, mCountResumes->Count());
}

TEST_F(ImageSourceBuffer, CompleteTriggersResume) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Check that we can't advance.
  CheckIteratorMustWait(iterator, mCountResumes);

  // Call Complete(). This should trigger a resume.
  CheckedCompleteBuffer();
  EXPECT_EQ(1u, mCountResumes->Count());
}

TEST_F(ImageSourceBuffer, ExpectLengthDoesNotTriggerResume) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator();

  // Check that we can't advance.
  CheckIteratorMustWait(iterator, mExpectNoResume);

  // Call ExpectLength(). If this triggers a resume, |mExpectNoResume| will
  // ensure that the test fails.
  mSourceBuffer->ExpectLength(1000);
}

TEST_F(ImageSourceBuffer, CompleteSuccessWithSameReadLength) {
  SourceBufferIterator iterator = mSourceBuffer->Iterator(1);

  // Write a single byte to the buffer and complete the buffer. (We have to
  // write at least one byte because completing a zero length buffer always
  // fails; see the ZeroLengthBufferAlwaysFails test.)
  CheckedAppendToBuffer(mData, 1);
  CheckedCompleteBuffer(iterator, 1);

  // We should be able to advance once (to read the single byte) and then should
  // reach the COMPLETE state with a successful status.
  CheckedAdvanceIterator(iterator, 1);
  CheckIteratorIsComplete(iterator, 1);
}

TEST_F(ImageSourceBuffer, CompleteSuccessWithSmallerReadLength) {
  // Create an iterator limited to one byte.
  SourceBufferIterator iterator = mSourceBuffer->Iterator(1);

  // Write two bytes to the buffer and complete the buffer. (We have to
  // write at least one byte because completing a zero length buffer always
  // fails; see the ZeroLengthBufferAlwaysFails test.)
  CheckedAppendToBuffer(mData, 2);
  CheckedCompleteBuffer(iterator, 2);

  // We should be able to advance once (to read the single byte) and then should
  // reach the COMPLETE state with a successful status, because our iterator is
  // limited to a single byte, rather than the full length.
  CheckedAdvanceIterator(iterator, 1);
  CheckIteratorIsComplete(iterator, 1);
}

TEST_F(ImageSourceBuffer, CompleteSuccessWithGreaterReadLength) {
  // Create an iterator limited to one byte.
  SourceBufferIterator iterator = mSourceBuffer->Iterator(2);

  // Write a single byte to the buffer and complete the buffer. (We have to
  // write at least one byte because completing a zero length buffer always
  // fails; see the ZeroLengthBufferAlwaysFails test.)
  CheckedAppendToBuffer(mData, 1);
  CheckedCompleteBuffer(iterator, 1);

  // We should be able to advance once (to read the single byte) and then should
  // reach the COMPLETE state with a successful status. Our iterator lets us
  // read more but the underlying buffer has been completed.
  CheckedAdvanceIterator(iterator, 1);
  CheckIteratorIsComplete(iterator, 1);
}