1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/bind.h"
6#include "base/callback_helpers.h"
7#include "base/message_loop/message_loop.h"
8#include "media/base/gmock_callback_support.h"
9#include "media/base/mock_filters.h"
10#include "media/base/test_helpers.h"
11#include "media/filters/decoder_stream.h"
12#include "media/filters/fake_demuxer_stream.h"
13#include "media/filters/fake_video_decoder.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16using ::testing::_;
17using ::testing::AnyNumber;
18using ::testing::Assign;
19using ::testing::Invoke;
20using ::testing::InvokeWithoutArgs;
21using ::testing::NiceMock;
22using ::testing::Return;
23using ::testing::SaveArg;
24
25static const int kNumConfigs = 3;
26static const int kNumBuffersInOneConfig = 5;
27
28// Use anonymous namespace here to prevent the actions to be defined multiple
29// times across multiple test files. Sadly we can't use static for them.
30namespace {
31
32ACTION_P3(ExecuteCallbackWithVerifier, decryptor, done_cb, verifier) {
33  // verifier must be called first since |done_cb| call will invoke it as well.
34  verifier->RecordACalled();
35  arg0.Run(decryptor, done_cb);
36}
37
38ACTION_P(ReportCallback, verifier) {
39  verifier->RecordBCalled();
40}
41
42}  // namespace
43
44namespace media {
45
46struct VideoFrameStreamTestParams {
47  VideoFrameStreamTestParams(bool is_encrypted,
48                             int decoding_delay,
49                             int parallel_decoding)
50      : is_encrypted(is_encrypted),
51        decoding_delay(decoding_delay),
52        parallel_decoding(parallel_decoding) {}
53
54  bool is_encrypted;
55  int decoding_delay;
56  int parallel_decoding;
57};
58
59class VideoFrameStreamTest
60    : public testing::Test,
61      public testing::WithParamInterface<VideoFrameStreamTestParams> {
62 public:
63  VideoFrameStreamTest()
64      : demuxer_stream_(new FakeDemuxerStream(kNumConfigs,
65                                              kNumBuffersInOneConfig,
66                                              GetParam().is_encrypted)),
67        decryptor_(new NiceMock<MockDecryptor>()),
68        decoder_(new FakeVideoDecoder(GetParam().decoding_delay,
69                                      GetParam().parallel_decoding)),
70        is_initialized_(false),
71        num_decoded_frames_(0),
72        pending_initialize_(false),
73        pending_read_(false),
74        pending_reset_(false),
75        pending_stop_(false),
76        total_bytes_decoded_(0),
77        has_no_key_(false) {
78    ScopedVector<VideoDecoder> decoders;
79    decoders.push_back(decoder_);
80
81    video_frame_stream_.reset(new VideoFrameStream(
82        message_loop_.message_loop_proxy(),
83        decoders.Pass(),
84        base::Bind(&VideoFrameStreamTest::SetDecryptorReadyCallback,
85                   base::Unretained(this)),
86        new MediaLog()));
87
88    // Decryptor can only decrypt (not decrypt-and-decode) so that
89    // DecryptingDemuxerStream will be used.
90    EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
91        .WillRepeatedly(RunCallback<1>(false));
92    EXPECT_CALL(*decryptor_, Decrypt(_, _, _))
93        .WillRepeatedly(Invoke(this, &VideoFrameStreamTest::Decrypt));
94  }
95
96  ~VideoFrameStreamTest() {
97    // Check that the pipeline statistics callback was fired correctly.
98    if (decoder_)
99      EXPECT_EQ(decoder_->total_bytes_decoded(), total_bytes_decoded_);
100
101    is_initialized_ = false;
102    decoder_ = NULL;
103    video_frame_stream_.reset();
104    message_loop_.RunUntilIdle();
105
106    DCHECK(!pending_initialize_);
107    DCHECK(!pending_read_);
108    DCHECK(!pending_reset_);
109    DCHECK(!pending_stop_);
110  }
111
112  MOCK_METHOD1(OnNewSpliceBuffer, void(base::TimeDelta));
113  MOCK_METHOD1(SetDecryptorReadyCallback, void(const media::DecryptorReadyCB&));
114  MOCK_METHOD1(DecryptorSet, void(bool));
115
116  void OnStatistics(const PipelineStatistics& statistics) {
117    total_bytes_decoded_ += statistics.video_bytes_decoded;
118  }
119
120  void OnInitialized(bool success) {
121    DCHECK(!pending_read_);
122    DCHECK(!pending_reset_);
123    DCHECK(pending_initialize_);
124    pending_initialize_ = false;
125
126    is_initialized_ = success;
127    if (!success)
128      decoder_ = NULL;
129  }
130
131  void InitializeVideoFrameStream() {
132    pending_initialize_ = true;
133    video_frame_stream_->Initialize(
134        demuxer_stream_.get(),
135        false,
136        base::Bind(&VideoFrameStreamTest::OnStatistics, base::Unretained(this)),
137        base::Bind(&VideoFrameStreamTest::OnInitialized,
138                   base::Unretained(this)));
139    message_loop_.RunUntilIdle();
140  }
141
142  // Fake Decrypt() function used by DecryptingDemuxerStream. It does nothing
143  // but removes the DecryptConfig to make the buffer unencrypted.
144  void Decrypt(Decryptor::StreamType stream_type,
145               const scoped_refptr<DecoderBuffer>& encrypted,
146               const Decryptor::DecryptCB& decrypt_cb) {
147    DCHECK(encrypted->decrypt_config());
148    if (has_no_key_) {
149      decrypt_cb.Run(Decryptor::kNoKey, NULL);
150      return;
151    }
152
153    DCHECK_EQ(stream_type, Decryptor::kVideo);
154    scoped_refptr<DecoderBuffer> decrypted =
155        DecoderBuffer::CopyFrom(encrypted->data(), encrypted->data_size());
156    decrypted->set_timestamp(encrypted->timestamp());
157    decrypted->set_duration(encrypted->duration());
158    decrypt_cb.Run(Decryptor::kSuccess, decrypted);
159  }
160
161  // Callback for VideoFrameStream::Read().
162  void FrameReady(VideoFrameStream::Status status,
163                  const scoped_refptr<VideoFrame>& frame) {
164    DCHECK(pending_read_);
165    frame_read_ = frame;
166    last_read_status_ = status;
167    if (frame.get() && !frame->end_of_stream())
168      num_decoded_frames_++;
169    pending_read_ = false;
170  }
171
172  void FrameReadyHoldDemuxer(VideoFrameStream::Status status,
173                             const scoped_refptr<VideoFrame>& frame) {
174    FrameReady(status, frame);
175
176  }
177
178  void OnReset() {
179    DCHECK(!pending_read_);
180    DCHECK(pending_reset_);
181    pending_reset_ = false;
182  }
183
184  void ReadOneFrame() {
185    frame_read_ = NULL;
186    pending_read_ = true;
187    video_frame_stream_->Read(base::Bind(
188        &VideoFrameStreamTest::FrameReady, base::Unretained(this)));
189    message_loop_.RunUntilIdle();
190  }
191
192  void ReadUntilPending() {
193    do {
194      ReadOneFrame();
195    } while (!pending_read_);
196  }
197
198  void ReadAllFrames() {
199    do {
200      ReadOneFrame();
201    } while (frame_read_.get() && !frame_read_->end_of_stream());
202
203    const int total_num_frames = kNumConfigs * kNumBuffersInOneConfig;
204    DCHECK_EQ(num_decoded_frames_, total_num_frames);
205  }
206
207  enum PendingState {
208    NOT_PENDING,
209    DEMUXER_READ_NORMAL,
210    DEMUXER_READ_CONFIG_CHANGE,
211    SET_DECRYPTOR,
212    DECRYPTOR_NO_KEY,
213    DECODER_INIT,
214    DECODER_REINIT,
215    DECODER_DECODE,
216    DECODER_RESET
217  };
218
219  void ExpectDecryptorNotification() {
220    EXPECT_CALL(*this, SetDecryptorReadyCallback(_))
221        .WillRepeatedly(ExecuteCallbackWithVerifier(
222            decryptor_.get(),
223            base::Bind(&VideoFrameStreamTest::DecryptorSet,
224                       base::Unretained(this)),
225            &verifier_));
226    EXPECT_CALL(*this, DecryptorSet(true))
227        .WillRepeatedly(ReportCallback(&verifier_));
228  }
229
230  void EnterPendingState(PendingState state) {
231    DCHECK_NE(state, NOT_PENDING);
232    switch (state) {
233      case DEMUXER_READ_NORMAL:
234        demuxer_stream_->HoldNextRead();
235        ReadUntilPending();
236        break;
237
238      case DEMUXER_READ_CONFIG_CHANGE:
239        demuxer_stream_->HoldNextConfigChangeRead();
240        ReadUntilPending();
241        break;
242
243      case SET_DECRYPTOR:
244        // Hold DecryptorReadyCB.
245        EXPECT_CALL(*this, SetDecryptorReadyCallback(_))
246            .Times(2);
247        // Initialize will fail because no decryptor is available.
248        InitializeVideoFrameStream();
249        break;
250
251      case DECRYPTOR_NO_KEY:
252        ExpectDecryptorNotification();
253        has_no_key_ = true;
254        ReadOneFrame();
255        break;
256
257      case DECODER_INIT:
258        ExpectDecryptorNotification();
259        decoder_->HoldNextInit();
260        InitializeVideoFrameStream();
261        break;
262
263      case DECODER_REINIT:
264        decoder_->HoldNextInit();
265        ReadUntilPending();
266        break;
267
268      case DECODER_DECODE:
269        decoder_->HoldDecode();
270        ReadUntilPending();
271        break;
272
273      case DECODER_RESET:
274        decoder_->HoldNextReset();
275        pending_reset_ = true;
276        video_frame_stream_->Reset(base::Bind(&VideoFrameStreamTest::OnReset,
277                                              base::Unretained(this)));
278        message_loop_.RunUntilIdle();
279        break;
280
281      case NOT_PENDING:
282        NOTREACHED();
283        break;
284    }
285  }
286
287  void SatisfyPendingCallback(PendingState state) {
288    DCHECK_NE(state, NOT_PENDING);
289    switch (state) {
290      case DEMUXER_READ_NORMAL:
291      case DEMUXER_READ_CONFIG_CHANGE:
292        demuxer_stream_->SatisfyRead();
293        break;
294
295      // These two cases are only interesting to test during
296      // VideoFrameStream destruction.  There's no need to satisfy a callback.
297      case SET_DECRYPTOR:
298      case DECRYPTOR_NO_KEY:
299        NOTREACHED();
300        break;
301
302      case DECODER_INIT:
303        decoder_->SatisfyInit();
304        break;
305
306      case DECODER_REINIT:
307        decoder_->SatisfyInit();
308        break;
309
310      case DECODER_DECODE:
311        decoder_->SatisfyDecode();
312        break;
313
314      case DECODER_RESET:
315        decoder_->SatisfyReset();
316        break;
317
318      case NOT_PENDING:
319        NOTREACHED();
320        break;
321    }
322
323    message_loop_.RunUntilIdle();
324  }
325
326  void Initialize() {
327    EnterPendingState(DECODER_INIT);
328    SatisfyPendingCallback(DECODER_INIT);
329  }
330
331  void Read() {
332    EnterPendingState(DECODER_DECODE);
333    SatisfyPendingCallback(DECODER_DECODE);
334  }
335
336  void Reset() {
337    EnterPendingState(DECODER_RESET);
338    SatisfyPendingCallback(DECODER_RESET);
339  }
340
341  base::MessageLoop message_loop_;
342
343  scoped_ptr<VideoFrameStream> video_frame_stream_;
344  scoped_ptr<FakeDemuxerStream> demuxer_stream_;
345  // Use NiceMock since we don't care about most of calls on the decryptor,
346  // e.g. RegisterNewKeyCB().
347  scoped_ptr<NiceMock<MockDecryptor> > decryptor_;
348  FakeVideoDecoder* decoder_;  // Owned by |video_frame_stream_|.
349
350  bool is_initialized_;
351  int num_decoded_frames_;
352  bool pending_initialize_;
353  bool pending_read_;
354  bool pending_reset_;
355  bool pending_stop_;
356  int total_bytes_decoded_;
357  scoped_refptr<VideoFrame> frame_read_;
358  VideoFrameStream::Status last_read_status_;
359
360  // Decryptor has no key to decrypt a frame.
361  bool has_no_key_;
362
363  CallbackPairChecker verifier_;
364
365 private:
366  DISALLOW_COPY_AND_ASSIGN(VideoFrameStreamTest);
367};
368
369INSTANTIATE_TEST_CASE_P(
370    Clear,
371    VideoFrameStreamTest,
372    ::testing::Values(
373        VideoFrameStreamTestParams(false, 0, 1),
374        VideoFrameStreamTestParams(false, 3, 1),
375        VideoFrameStreamTestParams(false, 7, 1)));
376
377INSTANTIATE_TEST_CASE_P(
378    Encrypted,
379    VideoFrameStreamTest,
380    ::testing::Values(
381        VideoFrameStreamTestParams(true, 7, 1)));
382
383INSTANTIATE_TEST_CASE_P(
384    Clear_Parallel,
385    VideoFrameStreamTest,
386    ::testing::Values(
387        VideoFrameStreamTestParams(false, 0, 3),
388        VideoFrameStreamTestParams(false, 2, 3)));
389
390
391TEST_P(VideoFrameStreamTest, Initialization) {
392  Initialize();
393}
394
395TEST_P(VideoFrameStreamTest, ReadOneFrame) {
396  Initialize();
397  Read();
398}
399
400TEST_P(VideoFrameStreamTest, ReadAllFrames) {
401  Initialize();
402  ReadAllFrames();
403}
404
405TEST_P(VideoFrameStreamTest, Read_AfterReset) {
406  Initialize();
407  Reset();
408  Read();
409  Reset();
410  Read();
411}
412
413TEST_P(VideoFrameStreamTest, Read_BlockedDemuxer) {
414  Initialize();
415  demuxer_stream_->HoldNextRead();
416  ReadOneFrame();
417  EXPECT_TRUE(pending_read_);
418
419  int demuxed_buffers = 0;
420
421  // Pass frames from the demuxer to the VideoFrameStream until the first read
422  // request is satisfied.
423  while (pending_read_) {
424    ++demuxed_buffers;
425    demuxer_stream_->SatisfyReadAndHoldNext();
426    message_loop_.RunUntilIdle();
427  }
428
429  EXPECT_EQ(std::min(GetParam().decoding_delay + 1, kNumBuffersInOneConfig + 1),
430            demuxed_buffers);
431
432  // At this point the stream is waiting on read from the demuxer, but there is
433  // no pending read from the stream. The stream should be blocked if we try
434  // reading from it again.
435  ReadUntilPending();
436
437  demuxer_stream_->SatisfyRead();
438  message_loop_.RunUntilIdle();
439  EXPECT_FALSE(pending_read_);
440}
441
442TEST_P(VideoFrameStreamTest, Read_BlockedDemuxerAndDecoder) {
443  // Test applies only when the decoder allows multiple parallel requests.
444  if (GetParam().parallel_decoding == 1)
445    return;
446
447  Initialize();
448  demuxer_stream_->HoldNextRead();
449  decoder_->HoldDecode();
450  ReadOneFrame();
451  EXPECT_TRUE(pending_read_);
452
453  int demuxed_buffers = 0;
454
455  // Pass frames from the demuxer to the VideoFrameStream until the first read
456  // request is satisfied, while always keeping one decode request pending.
457  while (pending_read_) {
458    ++demuxed_buffers;
459    demuxer_stream_->SatisfyReadAndHoldNext();
460    message_loop_.RunUntilIdle();
461
462    // Always keep one decode request pending.
463    if (demuxed_buffers > 1) {
464      decoder_->SatisfySingleDecode();
465      message_loop_.RunUntilIdle();
466    }
467  }
468
469  ReadUntilPending();
470  EXPECT_TRUE(pending_read_);
471
472  // Unblocking one decode request should unblock read even when demuxer is
473  // still blocked.
474  decoder_->SatisfySingleDecode();
475  message_loop_.RunUntilIdle();
476  EXPECT_FALSE(pending_read_);
477
478  // Stream should still be blocked on the demuxer after unblocking the decoder.
479  decoder_->SatisfyDecode();
480  ReadUntilPending();
481  EXPECT_TRUE(pending_read_);
482
483  // Verify that the stream has returned all frames that have been demuxed,
484  // accounting for the decoder delay.
485  EXPECT_EQ(demuxed_buffers - GetParam().decoding_delay, num_decoded_frames_);
486
487  // Unblocking the demuxer will unblock the stream.
488  demuxer_stream_->SatisfyRead();
489  message_loop_.RunUntilIdle();
490  EXPECT_FALSE(pending_read_);
491}
492
493TEST_P(VideoFrameStreamTest, Read_DuringEndOfStreamDecode) {
494  // Test applies only when the decoder allows multiple parallel requests, and
495  // they are not satisfied in a single batch.
496  if (GetParam().parallel_decoding == 1 || GetParam().decoding_delay != 0)
497    return;
498
499  Initialize();
500  decoder_->HoldDecode();
501
502  // Read all of the frames up to end of stream. Since parallel decoding is
503  // enabled, the end of stream buffer will be sent to the decoder immediately,
504  // but we don't satisfy it yet.
505  for (int configuration = 0; configuration < kNumConfigs; configuration++) {
506    for (int frame = 0; frame < kNumBuffersInOneConfig; frame++) {
507      ReadOneFrame();
508      while (pending_read_) {
509        decoder_->SatisfySingleDecode();
510        message_loop_.RunUntilIdle();
511      }
512    }
513  }
514
515  // Read() again. The callback must be delayed until the decode completes.
516  ReadOneFrame();
517  ASSERT_TRUE(pending_read_);
518
519  // Satisfy decoding of the end of stream buffer. The read should complete.
520  decoder_->SatisfySingleDecode();
521  message_loop_.RunUntilIdle();
522  ASSERT_FALSE(pending_read_);
523  EXPECT_EQ(last_read_status_, VideoFrameStream::OK);
524
525  // The read output should indicate end of stream.
526  ASSERT_TRUE(frame_read_.get());
527  EXPECT_TRUE(frame_read_->end_of_stream());
528}
529
530// No Reset() before initialization is successfully completed.
531TEST_P(VideoFrameStreamTest, Reset_AfterInitialization) {
532  Initialize();
533  Reset();
534  Read();
535}
536
537TEST_P(VideoFrameStreamTest, Reset_DuringReinitialization) {
538  Initialize();
539  EnterPendingState(DECODER_REINIT);
540  // VideoDecoder::Reset() is not called when we reset during reinitialization.
541  pending_reset_ = true;
542  video_frame_stream_->Reset(
543      base::Bind(&VideoFrameStreamTest::OnReset, base::Unretained(this)));
544  SatisfyPendingCallback(DECODER_REINIT);
545  Read();
546}
547
548TEST_P(VideoFrameStreamTest, Reset_AfterReinitialization) {
549  Initialize();
550  EnterPendingState(DECODER_REINIT);
551  SatisfyPendingCallback(DECODER_REINIT);
552  Reset();
553  Read();
554}
555
556TEST_P(VideoFrameStreamTest, Reset_DuringDemuxerRead_Normal) {
557  Initialize();
558  EnterPendingState(DEMUXER_READ_NORMAL);
559  EnterPendingState(DECODER_RESET);
560  SatisfyPendingCallback(DEMUXER_READ_NORMAL);
561  SatisfyPendingCallback(DECODER_RESET);
562  Read();
563}
564
565TEST_P(VideoFrameStreamTest, Reset_DuringDemuxerRead_ConfigChange) {
566  Initialize();
567  EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
568  EnterPendingState(DECODER_RESET);
569  SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
570  SatisfyPendingCallback(DECODER_RESET);
571  Read();
572}
573
574TEST_P(VideoFrameStreamTest, Reset_DuringNormalDecoderDecode) {
575  Initialize();
576  EnterPendingState(DECODER_DECODE);
577  EnterPendingState(DECODER_RESET);
578  SatisfyPendingCallback(DECODER_DECODE);
579  SatisfyPendingCallback(DECODER_RESET);
580  Read();
581}
582
583TEST_P(VideoFrameStreamTest, Reset_AfterNormalRead) {
584  Initialize();
585  Read();
586  Reset();
587  Read();
588}
589
590TEST_P(VideoFrameStreamTest, Reset_AfterNormalReadWithActiveSplice) {
591  video_frame_stream_->set_splice_observer(base::Bind(
592      &VideoFrameStreamTest::OnNewSpliceBuffer, base::Unretained(this)));
593  Initialize();
594
595  // Send buffers with a splice timestamp, which sets the active splice flag.
596  const base::TimeDelta splice_timestamp = base::TimeDelta();
597  demuxer_stream_->set_splice_timestamp(splice_timestamp);
598  EXPECT_CALL(*this, OnNewSpliceBuffer(splice_timestamp)).Times(AnyNumber());
599  Read();
600
601  // Issue an explicit Reset() and clear the splice timestamp.
602  Reset();
603  demuxer_stream_->set_splice_timestamp(kNoTimestamp());
604
605  // Ensure none of the upcoming calls indicate they have a splice timestamp.
606  EXPECT_CALL(*this, OnNewSpliceBuffer(_)).Times(0);
607  Read();
608}
609
610TEST_P(VideoFrameStreamTest, Reset_AfterDemuxerRead_ConfigChange) {
611  Initialize();
612  EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
613  SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
614  Reset();
615  Read();
616}
617
618TEST_P(VideoFrameStreamTest, Reset_AfterEndOfStream) {
619  Initialize();
620  ReadAllFrames();
621  Reset();
622  num_decoded_frames_ = 0;
623  demuxer_stream_->SeekToStart();
624  ReadAllFrames();
625}
626
627TEST_P(VideoFrameStreamTest, Reset_DuringNoKeyRead) {
628  Initialize();
629  EnterPendingState(DECRYPTOR_NO_KEY);
630  Reset();
631}
632
633// In the following Destroy_* tests, |video_frame_stream_| is destroyed in
634// VideoFrameStreamTest dtor.
635
636TEST_P(VideoFrameStreamTest, Destroy_BeforeInitialization) {
637}
638
639TEST_P(VideoFrameStreamTest, Destroy_DuringSetDecryptor) {
640  if (!GetParam().is_encrypted) {
641    DVLOG(1) << "SetDecryptor test only runs when the stream is encrytped.";
642    return;
643  }
644
645  EnterPendingState(SET_DECRYPTOR);
646}
647
648TEST_P(VideoFrameStreamTest, Destroy_DuringInitialization) {
649  EnterPendingState(DECODER_INIT);
650}
651
652TEST_P(VideoFrameStreamTest, Destroy_AfterInitialization) {
653  Initialize();
654}
655
656TEST_P(VideoFrameStreamTest, Destroy_DuringReinitialization) {
657  Initialize();
658  EnterPendingState(DECODER_REINIT);
659}
660
661TEST_P(VideoFrameStreamTest, Destroy_AfterReinitialization) {
662  Initialize();
663  EnterPendingState(DECODER_REINIT);
664  SatisfyPendingCallback(DECODER_REINIT);
665}
666
667TEST_P(VideoFrameStreamTest, Destroy_DuringDemuxerRead_Normal) {
668  Initialize();
669  EnterPendingState(DEMUXER_READ_NORMAL);
670}
671
672TEST_P(VideoFrameStreamTest, Destroy_DuringDemuxerRead_ConfigChange) {
673  Initialize();
674  EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
675}
676
677TEST_P(VideoFrameStreamTest, Destroy_DuringNormalDecoderDecode) {
678  Initialize();
679  EnterPendingState(DECODER_DECODE);
680}
681
682TEST_P(VideoFrameStreamTest, Destroy_AfterNormalRead) {
683  Initialize();
684  Read();
685}
686
687TEST_P(VideoFrameStreamTest, Destroy_AfterConfigChangeRead) {
688  Initialize();
689  EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
690  SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
691}
692
693TEST_P(VideoFrameStreamTest, Destroy_DuringNoKeyRead) {
694  Initialize();
695  EnterPendingState(DECRYPTOR_NO_KEY);
696}
697
698TEST_P(VideoFrameStreamTest, Destroy_DuringReset) {
699  Initialize();
700  EnterPendingState(DECODER_RESET);
701}
702
703TEST_P(VideoFrameStreamTest, Destroy_AfterReset) {
704  Initialize();
705  Reset();
706}
707
708TEST_P(VideoFrameStreamTest, Destroy_DuringRead_DuringReset) {
709  Initialize();
710  EnterPendingState(DECODER_DECODE);
711  EnterPendingState(DECODER_RESET);
712}
713
714TEST_P(VideoFrameStreamTest, Destroy_AfterRead_DuringReset) {
715  Initialize();
716  EnterPendingState(DECODER_DECODE);
717  EnterPendingState(DECODER_RESET);
718  SatisfyPendingCallback(DECODER_DECODE);
719}
720
721TEST_P(VideoFrameStreamTest, Destroy_AfterRead_AfterReset) {
722  Initialize();
723  Read();
724  Reset();
725}
726
727TEST_P(VideoFrameStreamTest, DecoderErrorWhenReading) {
728  Initialize();
729  EnterPendingState(DECODER_DECODE);
730  decoder_->SimulateError();
731  message_loop_.RunUntilIdle();
732  ASSERT_FALSE(pending_read_);
733  ASSERT_EQ(VideoFrameStream::DECODE_ERROR, last_read_status_);
734}
735
736TEST_P(VideoFrameStreamTest, DecoderErrorWhenNotReading) {
737  Initialize();
738
739  decoder_->HoldDecode();
740  ReadOneFrame();
741  EXPECT_TRUE(pending_read_);
742
743  // Satisfy decode requests until we get the first frame out.
744  while (pending_read_) {
745    decoder_->SatisfySingleDecode();
746    message_loop_.RunUntilIdle();
747  }
748
749  // Trigger an error in the decoding.
750  decoder_->SimulateError();
751
752  // The error must surface from Read() as DECODE_ERROR.
753  while (last_read_status_ == VideoFrameStream::OK) {
754    ReadOneFrame();
755    message_loop_.RunUntilIdle();
756    EXPECT_FALSE(pending_read_);
757  }
758  EXPECT_EQ(VideoFrameStream::DECODE_ERROR, last_read_status_);
759}
760
761}  // namespace media
762