Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / filters / decrypting_audio_decoder_unittest.cc
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 <string>
6 #include <vector>
7
8 #include "base/bind.h"
9 #include "base/callback_helpers.h"
10 #include "base/message_loop/message_loop.h"
11 #include "media/base/audio_buffer.h"
12 #include "media/base/buffers.h"
13 #include "media/base/decoder_buffer.h"
14 #include "media/base/decrypt_config.h"
15 #include "media/base/gmock_callback_support.h"
16 #include "media/base/mock_filters.h"
17 #include "media/base/test_helpers.h"
18 #include "media/filters/decrypting_audio_decoder.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20
21 using ::testing::_;
22 using ::testing::AtMost;
23 using ::testing::IsNull;
24 using ::testing::SaveArg;
25 using ::testing::StrictMock;
26
27 namespace media {
28
29 // Make sure the kFakeAudioFrameSize is a valid frame size for all audio decoder
30 // configs used in this test.
31 static const int kFakeAudioFrameSize = 48;
32 static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 };
33 static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 };
34
35 // Create a fake non-empty encrypted buffer.
36 static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() {
37   const int buffer_size = 16;  // Need a non-empty buffer;
38   scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size));
39   buffer->set_decrypt_config(scoped_ptr<DecryptConfig>(new DecryptConfig(
40       std::string(reinterpret_cast<const char*>(kFakeKeyId),
41                   arraysize(kFakeKeyId)),
42       std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)),
43       std::vector<SubsampleEntry>())));
44   return buffer;
45 }
46
47 // Use anonymous namespace here to prevent the actions to be defined multiple
48 // times across multiple test files. Sadly we can't use static for them.
49 namespace {
50
51 ACTION_P(ReturnBuffer, buffer) {
52   arg0.Run(buffer.get() ? DemuxerStream::kOk : DemuxerStream::kAborted, buffer);
53 }
54
55 ACTION_P(RunCallbackIfNotNull, param) {
56   if (!arg0.is_null())
57     arg0.Run(param);
58 }
59
60 ACTION_P2(ResetAndRunCallback, callback, param) {
61   base::ResetAndReturn(callback).Run(param);
62 }
63
64 MATCHER(IsEndOfStream, "end of stream") {
65   return (arg->end_of_stream());
66 }
67
68 }  // namespace
69
70 class DecryptingAudioDecoderTest : public testing::Test {
71  public:
72   DecryptingAudioDecoderTest()
73       : decoder_(new DecryptingAudioDecoder(
74             message_loop_.message_loop_proxy(),
75             base::Bind(
76                 &DecryptingAudioDecoderTest::RequestDecryptorNotification,
77                 base::Unretained(this)))),
78         decryptor_(new StrictMock<MockDecryptor>()),
79         demuxer_(new StrictMock<MockDemuxerStream>(DemuxerStream::AUDIO)),
80         encrypted_buffer_(CreateFakeEncryptedBuffer()),
81         decoded_frame_(NULL),
82         end_of_stream_frame_(AudioBuffer::CreateEOSBuffer()),
83         decoded_frame_list_() {
84   }
85
86   virtual ~DecryptingAudioDecoderTest() {
87     Stop();
88   }
89
90   void InitializeAndExpectStatus(const AudioDecoderConfig& config,
91                                  PipelineStatus status) {
92     // Initialize data now that the config is known. Since the code uses
93     // invalid values (that CreateEmptyBuffer() doesn't support), tweak them
94     // just for CreateEmptyBuffer().
95     int channels = ChannelLayoutToChannelCount(config.channel_layout());
96     if (channels < 1)
97       channels = 1;
98     decoded_frame_ = AudioBuffer::CreateEmptyBuffer(
99         channels, kFakeAudioFrameSize, kNoTimestamp(), kNoTimestamp());
100     decoded_frame_list_.push_back(decoded_frame_);
101
102     demuxer_->set_audio_decoder_config(config);
103     decoder_->Initialize(demuxer_.get(), NewExpectedStatusCB(status),
104                          base::Bind(&MockStatisticsCB::OnStatistics,
105                                     base::Unretained(&statistics_cb_)));
106     message_loop_.RunUntilIdle();
107   }
108
109   void Initialize() {
110     EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
111         .Times(AtMost(1))
112         .WillOnce(RunCallback<1>(true));
113     EXPECT_CALL(*this, RequestDecryptorNotification(_))
114         .WillOnce(RunCallbackIfNotNull(decryptor_.get()));
115     EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
116         .WillOnce(SaveArg<1>(&key_added_cb_));
117
118     config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
119                        CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, true, true,
120                        base::TimeDelta(), base::TimeDelta());
121     InitializeAndExpectStatus(config_, PIPELINE_OK);
122
123     EXPECT_EQ(DecryptingAudioDecoder::kSupportedBitsPerChannel,
124               decoder_->bits_per_channel());
125     EXPECT_EQ(config_.channel_layout(), decoder_->channel_layout());
126     EXPECT_EQ(config_.samples_per_second(), decoder_->samples_per_second());
127   }
128
129   void ReadAndExpectFrameReadyWith(
130       AudioDecoder::Status status,
131       const scoped_refptr<AudioBuffer>& audio_frame) {
132     if (status != AudioDecoder::kOk)
133       EXPECT_CALL(*this, FrameReady(status, IsNull()));
134     else if (audio_frame->end_of_stream())
135       EXPECT_CALL(*this, FrameReady(status, IsEndOfStream()));
136     else
137       EXPECT_CALL(*this, FrameReady(status, audio_frame));
138
139     decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
140                               base::Unretained(this)));
141     message_loop_.RunUntilIdle();
142   }
143
144   // Sets up expectations and actions to put DecryptingAudioDecoder in an
145   // active normal decoding state.
146   void EnterNormalDecodingState() {
147     Decryptor::AudioBuffers end_of_stream_frames_(1, end_of_stream_frame_);
148
149     EXPECT_CALL(*demuxer_, Read(_))
150         .WillOnce(ReturnBuffer(encrypted_buffer_))
151         .WillRepeatedly(ReturnBuffer(DecoderBuffer::CreateEOSBuffer()));
152     EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
153         .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_))
154         .WillRepeatedly(RunCallback<1>(Decryptor::kNeedMoreData,
155                                        Decryptor::AudioBuffers()));
156     EXPECT_CALL(statistics_cb_, OnStatistics(_));
157
158     ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
159   }
160
161   // Sets up expectations and actions to put DecryptingAudioDecoder in an end
162   // of stream state. This function must be called after
163   // EnterNormalDecodingState() to work.
164   void EnterEndOfStreamState() {
165     ReadAndExpectFrameReadyWith(AudioDecoder::kOk, end_of_stream_frame_);
166   }
167
168   // Make the read callback pending by saving and not firing it.
169   void EnterPendingReadState() {
170     EXPECT_TRUE(pending_demuxer_read_cb_.is_null());
171     EXPECT_CALL(*demuxer_, Read(_))
172         .WillOnce(SaveArg<0>(&pending_demuxer_read_cb_));
173     decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
174                               base::Unretained(this)));
175     message_loop_.RunUntilIdle();
176     // Make sure the Read() on the decoder triggers a Read() on the demuxer.
177     EXPECT_FALSE(pending_demuxer_read_cb_.is_null());
178   }
179
180   // Make the audio decode callback pending by saving and not firing it.
181   void EnterPendingDecodeState() {
182     EXPECT_TRUE(pending_audio_decode_cb_.is_null());
183     EXPECT_CALL(*demuxer_, Read(_))
184         .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
185     EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
186         .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
187
188     decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
189                               base::Unretained(this)));
190     message_loop_.RunUntilIdle();
191     // Make sure the Read() on the decoder triggers a DecryptAndDecode() on the
192     // decryptor.
193     EXPECT_FALSE(pending_audio_decode_cb_.is_null());
194   }
195
196   void EnterWaitingForKeyState() {
197     EXPECT_CALL(*demuxer_, Read(_))
198         .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
199     EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
200         .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey,
201                                        Decryptor::AudioBuffers()));
202     decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
203                               base::Unretained(this)));
204     message_loop_.RunUntilIdle();
205   }
206
207   void AbortPendingAudioDecodeCB() {
208     if (!pending_audio_decode_cb_.is_null()) {
209       base::ResetAndReturn(&pending_audio_decode_cb_).Run(
210           Decryptor::kSuccess, Decryptor::AudioBuffers());
211     }
212   }
213
214   void AbortAllPendingCBs() {
215     if (!pending_init_cb_.is_null()) {
216       ASSERT_TRUE(pending_audio_decode_cb_.is_null());
217       base::ResetAndReturn(&pending_init_cb_).Run(false);
218       return;
219     }
220
221     AbortPendingAudioDecodeCB();
222   }
223
224   void Reset() {
225     EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
226         .WillRepeatedly(InvokeWithoutArgs(
227             this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
228
229     decoder_->Reset(NewExpectedClosure());
230     message_loop_.RunUntilIdle();
231   }
232
233   void Stop() {
234     EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
235         .WillRepeatedly(InvokeWithoutArgs(
236             this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
237
238     decoder_->Stop(NewExpectedClosure());
239     message_loop_.RunUntilIdle();
240   }
241
242   MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&));
243
244   MOCK_METHOD2(FrameReady,
245                void(AudioDecoder::Status, const scoped_refptr<AudioBuffer>&));
246
247   base::MessageLoop message_loop_;
248   scoped_ptr<DecryptingAudioDecoder> decoder_;
249   scoped_ptr<StrictMock<MockDecryptor> > decryptor_;
250   scoped_ptr<StrictMock<MockDemuxerStream> > demuxer_;
251   MockStatisticsCB statistics_cb_;
252   AudioDecoderConfig config_;
253
254   DemuxerStream::ReadCB pending_demuxer_read_cb_;
255   Decryptor::DecoderInitCB pending_init_cb_;
256   Decryptor::NewKeyCB key_added_cb_;
257   Decryptor::AudioDecodeCB pending_audio_decode_cb_;
258
259   // Constant buffer/frames to be returned by the |demuxer_| and |decryptor_|.
260   scoped_refptr<DecoderBuffer> encrypted_buffer_;
261   scoped_refptr<AudioBuffer> decoded_frame_;
262   scoped_refptr<AudioBuffer> end_of_stream_frame_;
263   Decryptor::AudioBuffers decoded_frame_list_;
264
265  private:
266   DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
267 };
268
269 TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
270   Initialize();
271 }
272
273 // Ensure that DecryptingAudioDecoder only accepts encrypted audio.
274 TEST_F(DecryptingAudioDecoderTest, Initialize_UnencryptedAudioConfig) {
275   AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
276                             CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, false);
277
278   InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
279 }
280
281 // Ensure decoder handles invalid audio configs without crashing.
282 TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
283   AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
284                             CHANNEL_LAYOUT_STEREO, 0, NULL, 0, true);
285
286   InitializeAndExpectStatus(config, PIPELINE_ERROR_DECODE);
287 }
288
289 // Ensure decoder handles unsupported audio configs without crashing.
290 TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
291   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
292       .WillOnce(RunCallback<1>(false));
293   EXPECT_CALL(*this, RequestDecryptorNotification(_))
294       .WillOnce(RunCallbackIfNotNull(decryptor_.get()));
295
296   AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
297                             CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, true);
298   InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
299 }
300
301 TEST_F(DecryptingAudioDecoderTest, Initialize_NullDecryptor) {
302   EXPECT_CALL(*this, RequestDecryptorNotification(_))
303       .WillRepeatedly(RunCallbackIfNotNull(static_cast<Decryptor*>(NULL)));
304
305   AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
306                             CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, true);
307   InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
308 }
309
310 // Test normal decrypt and decode case.
311 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
312   Initialize();
313   EnterNormalDecodingState();
314 }
315
316 // Test the case where the decryptor returns error when doing decrypt and
317 // decode.
318 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
319   Initialize();
320
321   EXPECT_CALL(*demuxer_, Read(_))
322       .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
323   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
324       .WillRepeatedly(RunCallback<1>(Decryptor::kError,
325                                      Decryptor::AudioBuffers()));
326
327   ReadAndExpectFrameReadyWith(AudioDecoder::kDecodeError, NULL);
328 }
329
330 // Test the case where the decryptor returns kNeedMoreData to ask for more
331 // buffers before it can produce a frame.
332 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_NeedMoreData) {
333   Initialize();
334
335   EXPECT_CALL(*demuxer_, Read(_))
336       .Times(2)
337       .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
338   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
339       .WillOnce(RunCallback<1>(Decryptor::kNeedMoreData,
340                                Decryptor::AudioBuffers()))
341       .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
342   EXPECT_CALL(statistics_cb_, OnStatistics(_))
343       .Times(2);
344
345   ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
346 }
347
348 // Test the case where the decryptor returns multiple decoded frames.
349 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
350   Initialize();
351
352   scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
353       ChannelLayoutToChannelCount(config_.channel_layout()),
354       kFakeAudioFrameSize,
355       kNoTimestamp(),
356       kNoTimestamp());
357   scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer(
358       ChannelLayoutToChannelCount(config_.channel_layout()),
359       kFakeAudioFrameSize,
360       kNoTimestamp(),
361       kNoTimestamp());
362   decoded_frame_list_.push_back(frame_a);
363   decoded_frame_list_.push_back(frame_b);
364
365   EXPECT_CALL(*demuxer_, Read(_))
366       .WillOnce(ReturnBuffer(encrypted_buffer_));
367   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
368       .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
369   EXPECT_CALL(statistics_cb_, OnStatistics(_));
370
371   ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
372   ReadAndExpectFrameReadyWith(AudioDecoder::kOk, frame_a);
373   ReadAndExpectFrameReadyWith(AudioDecoder::kOk, frame_b);
374 }
375
376 // Test the case where the decryptor receives end-of-stream buffer.
377 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
378   Initialize();
379   EnterNormalDecodingState();
380   EnterEndOfStreamState();
381 }
382
383 // Test aborted read on the demuxer stream.
384 TEST_F(DecryptingAudioDecoderTest, DemuxerRead_Aborted) {
385   Initialize();
386
387   // ReturnBuffer() with NULL triggers aborted demuxer read.
388   EXPECT_CALL(*demuxer_, Read(_))
389       .WillOnce(ReturnBuffer(scoped_refptr<DecoderBuffer>()));
390
391   ReadAndExpectFrameReadyWith(AudioDecoder::kAborted, NULL);
392 }
393
394 // Test config change on the demuxer stream.
395 TEST_F(DecryptingAudioDecoderTest, DemuxerRead_ConfigChange) {
396   Initialize();
397
398   // The new config is different from the initial config in bits-per-channel,
399   // channel layout and samples_per_second.
400   AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
401                                 CHANNEL_LAYOUT_5_1, 88200, NULL, 0, false);
402   EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
403   EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
404   EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
405
406   demuxer_->set_audio_decoder_config(new_config);
407   EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
408   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
409       .WillOnce(RunCallback<1>(true));
410   EXPECT_CALL(*demuxer_, Read(_))
411       .WillOnce(RunCallback<0>(DemuxerStream::kConfigChanged,
412                                scoped_refptr<DecoderBuffer>()))
413       .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
414   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
415       .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
416   EXPECT_CALL(statistics_cb_, OnStatistics(_));
417
418   ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
419
420   EXPECT_EQ(new_config.bits_per_channel(), decoder_->bits_per_channel());
421   EXPECT_EQ(new_config.channel_layout(), decoder_->channel_layout());
422   EXPECT_EQ(new_config.samples_per_second(), decoder_->samples_per_second());
423 }
424
425 // Test config change failure.
426 TEST_F(DecryptingAudioDecoderTest, DemuxerRead_ConfigChangeFailed) {
427   Initialize();
428
429   EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
430   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
431       .WillOnce(RunCallback<1>(false));
432   EXPECT_CALL(*demuxer_, Read(_))
433       .WillOnce(RunCallback<0>(DemuxerStream::kConfigChanged,
434                                scoped_refptr<DecoderBuffer>()))
435       .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
436
437   ReadAndExpectFrameReadyWith(AudioDecoder::kDecodeError, NULL);
438 }
439
440 // Test the case where the a key is added when the decryptor is in
441 // kWaitingForKey state.
442 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
443   Initialize();
444   EnterWaitingForKeyState();
445
446   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
447       .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
448   EXPECT_CALL(statistics_cb_, OnStatistics(_));
449   EXPECT_CALL(*this, FrameReady(AudioDecoder::kOk, decoded_frame_));
450   key_added_cb_.Run();
451   message_loop_.RunUntilIdle();
452 }
453
454 // Test the case where the a key is added when the decryptor is in
455 // kPendingDecode state.
456 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
457   Initialize();
458   EnterPendingDecodeState();
459
460   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
461       .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
462   EXPECT_CALL(statistics_cb_, OnStatistics(_));
463   EXPECT_CALL(*this, FrameReady(AudioDecoder::kOk, decoded_frame_));
464   // The audio decode callback is returned after the correct decryption key is
465   // added.
466   key_added_cb_.Run();
467   base::ResetAndReturn(&pending_audio_decode_cb_).Run(
468       Decryptor::kNoKey, Decryptor::AudioBuffers());
469   message_loop_.RunUntilIdle();
470 }
471
472 // Test resetting when the decoder is in kIdle state but has not decoded any
473 // frame.
474 TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
475   Initialize();
476   Reset();
477 }
478
479 // Test resetting when the decoder is in kIdle state after it has decoded one
480 // frame.
481 TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
482   Initialize();
483   EnterNormalDecodingState();
484   Reset();
485 }
486
487 // Test resetting when the decoder is in kPendingDemuxerRead state and the read
488 // callback is returned with kOk.
489 TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_Ok) {
490   Initialize();
491   EnterPendingReadState();
492
493   EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
494
495   Reset();
496   base::ResetAndReturn(&pending_demuxer_read_cb_).Run(DemuxerStream::kOk,
497                                                       encrypted_buffer_);
498   message_loop_.RunUntilIdle();
499 }
500
501 // Test resetting when the decoder is in kPendingDemuxerRead state and the read
502 // callback is returned with kAborted.
503 TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_Aborted) {
504   Initialize();
505   EnterPendingReadState();
506
507   // Make sure we get a NULL audio frame returned.
508   EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
509
510   Reset();
511   base::ResetAndReturn(&pending_demuxer_read_cb_).Run(DemuxerStream::kAborted,
512                                                       NULL);
513   message_loop_.RunUntilIdle();
514 }
515
516 // Test resetting when the decoder is in kPendingDemuxerRead state and the read
517 // callback is returned with kConfigChanged.
518 TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_ConfigChange) {
519   Initialize();
520   EnterPendingReadState();
521
522   Reset();
523
524   // The new config is different from the initial config in bits-per-channel,
525   // channel layout and samples_per_second.
526   AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
527                                 CHANNEL_LAYOUT_5_1, 88200, NULL, 0, false);
528   EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
529   EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
530   EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
531
532   // Even during pending reset, the decoder still needs to be initialized with
533   // the new config.
534   demuxer_->set_audio_decoder_config(new_config);
535   EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
536   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
537       .WillOnce(RunCallback<1>(true));
538   EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
539
540   base::ResetAndReturn(&pending_demuxer_read_cb_)
541       .Run(DemuxerStream::kConfigChanged, NULL);
542   message_loop_.RunUntilIdle();
543
544   EXPECT_EQ(new_config.bits_per_channel(), decoder_->bits_per_channel());
545   EXPECT_EQ(new_config.channel_layout(), decoder_->channel_layout());
546   EXPECT_EQ(new_config.samples_per_second(), decoder_->samples_per_second());
547 }
548
549 // Test resetting when the decoder is in kPendingDemuxerRead state, the read
550 // callback is returned with kConfigChanged and the config change fails.
551 TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_ConfigChangeFailed) {
552   Initialize();
553   EnterPendingReadState();
554
555   Reset();
556
557   // Even during pending reset, the decoder still needs to be initialized with
558   // the new config.
559   EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
560   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
561       .WillOnce(RunCallback<1>(false));
562   EXPECT_CALL(*this, FrameReady(AudioDecoder::kDecodeError, IsNull()));
563
564   base::ResetAndReturn(&pending_demuxer_read_cb_)
565       .Run(DemuxerStream::kConfigChanged, NULL);
566   message_loop_.RunUntilIdle();
567 }
568
569 // Test resetting when the decoder is in kPendingConfigChange state.
570 TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingConfigChange) {
571   Initialize();
572   EnterNormalDecodingState();
573
574   EXPECT_CALL(*demuxer_, Read(_))
575       .WillOnce(RunCallback<0>(DemuxerStream::kConfigChanged,
576                                scoped_refptr<DecoderBuffer>()));
577   EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
578   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
579       .WillOnce(SaveArg<1>(&pending_init_cb_));
580
581   decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
582                             base::Unretained(this)));
583   message_loop_.RunUntilIdle();
584   EXPECT_FALSE(pending_init_cb_.is_null());
585
586   EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
587
588   Reset();
589   base::ResetAndReturn(&pending_init_cb_).Run(true);
590   message_loop_.RunUntilIdle();
591 }
592
593 // Test resetting when the decoder is in kPendingDecode state.
594 TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
595   Initialize();
596   EnterPendingDecodeState();
597
598   EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
599
600   Reset();
601 }
602
603 // Test resetting when the decoder is in kWaitingForKey state.
604 TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
605   Initialize();
606   EnterWaitingForKeyState();
607
608   EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
609
610   Reset();
611 }
612
613 // Test resetting when the decoder has hit end of stream and is in
614 // kDecodeFinished state.
615 TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
616   Initialize();
617   EnterNormalDecodingState();
618   EnterEndOfStreamState();
619   Reset();
620 }
621
622 // Test resetting after the decoder has been reset.
623 TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
624   Initialize();
625   EnterNormalDecodingState();
626   Reset();
627   Reset();
628 }
629
630 }  // namespace media