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