diff options
Diffstat (limited to 'chromium/media/filters/decrypting_video_decoder_unittest.cc')
-rw-r--r-- | chromium/media/filters/decrypting_video_decoder_unittest.cc | 155 |
1 files changed, 81 insertions, 74 deletions
diff --git a/chromium/media/filters/decrypting_video_decoder_unittest.cc b/chromium/media/filters/decrypting_video_decoder_unittest.cc index 1e8bee9fece..738753b518b 100644 --- a/chromium/media/filters/decrypting_video_decoder_unittest.cc +++ b/chromium/media/filters/decrypting_video_decoder_unittest.cc @@ -18,16 +18,15 @@ #include "testing/gmock/include/gmock/gmock.h" using ::testing::_; -using ::testing::AtMost; -using ::testing::IsNull; -using ::testing::ReturnRef; +using ::testing::Invoke; using ::testing::SaveArg; using ::testing::StrictMock; namespace media { -static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 }; -static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 }; +const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 }; +const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 }; +const int kDecodingDelay = 3; // Create a fake non-empty encrypted buffer. static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() { @@ -37,7 +36,6 @@ static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() { std::string(reinterpret_cast<const char*>(kFakeKeyId), arraysize(kFakeKeyId)), std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)), - 0, std::vector<SubsampleEntry>()))); return buffer; } @@ -55,10 +53,6 @@ ACTION_P2(ResetAndRunCallback, callback, param) { base::ResetAndReturn(callback).Run(param); } -MATCHER(IsEndOfStream, "end of stream") { - return (arg->end_of_stream()); -} - } // namespace class DecryptingVideoDecoderTest : public testing::Test { @@ -70,11 +64,12 @@ class DecryptingVideoDecoderTest : public testing::Test { &DecryptingVideoDecoderTest::RequestDecryptorNotification, base::Unretained(this)))), decryptor_(new StrictMock<MockDecryptor>()), + num_decrypt_and_decode_calls_(0), + num_frames_in_decryptor_(0), encrypted_buffer_(CreateFakeEncryptedBuffer()), decoded_video_frame_(VideoFrame::CreateBlackFrame( TestVideoConfig::NormalCodedSize())), - null_video_frame_(scoped_refptr<VideoFrame>()), - end_of_stream_video_frame_(VideoFrame::CreateEOSFrame()) { + null_video_frame_(scoped_refptr<VideoFrame>()) { EXPECT_CALL(*this, RequestDecryptorNotification(_)) .WillRepeatedly(RunCallbackIfNotNull(decryptor_.get())); } @@ -83,61 +78,85 @@ class DecryptingVideoDecoderTest : public testing::Test { Stop(); } + // Initializes the |decoder_| and expects |status|. Note the initialization + // can succeed or fail. void InitializeAndExpectStatus(const VideoDecoderConfig& config, PipelineStatus status) { - decoder_->Initialize(config, NewExpectedStatusCB(status)); + decoder_->Initialize(config, false, NewExpectedStatusCB(status), + base::Bind(&DecryptingVideoDecoderTest::FrameReady, + base::Unretained(this))); message_loop_.RunUntilIdle(); } + // Initialize the |decoder_| and expects it to succeed. void Initialize() { EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) - .WillRepeatedly(RunCallback<1>(true)); + .WillOnce(RunCallback<1>(true)); EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _)) - .WillRepeatedly(SaveArg<1>(&key_added_cb_)); + .WillOnce(SaveArg<1>(&key_added_cb_)); InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), PIPELINE_OK); } + // Reinitialize the |decoder_| and expects it to succeed. void Reinitialize() { EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kVideo)); + EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) + .WillOnce(RunCallback<1>(true)); + EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _)) + .WillOnce(SaveArg<1>(&key_added_cb_)); + InitializeAndExpectStatus(TestVideoConfig::LargeEncrypted(), PIPELINE_OK); } - void ReadAndExpectFrameReadyWith( - const scoped_refptr<DecoderBuffer>& buffer, - VideoDecoder::Status status, - const scoped_refptr<VideoFrame>& video_frame) { - if (status != VideoDecoder::kOk) - EXPECT_CALL(*this, FrameReady(status, IsNull())); - else if (video_frame.get() && video_frame->end_of_stream()) - EXPECT_CALL(*this, FrameReady(status, IsEndOfStream())); - else - EXPECT_CALL(*this, FrameReady(status, video_frame)); - + // Decode |buffer| and expect DecodeDone to get called with |status|. + void DecodeAndExpect(const scoped_refptr<DecoderBuffer>& buffer, + VideoDecoder::Status status) { + EXPECT_CALL(*this, DecodeDone(status)); decoder_->Decode(buffer, - base::Bind(&DecryptingVideoDecoderTest::FrameReady, + base::Bind(&DecryptingVideoDecoderTest::DecodeDone, base::Unretained(this))); message_loop_.RunUntilIdle(); } + // Helper function to simulate the decrypting and decoding process in the + // |decryptor_| with a decoding delay of kDecodingDelay buffers. + void DecryptAndDecodeVideo(const scoped_refptr<DecoderBuffer>& encrypted, + const Decryptor::VideoDecodeCB& video_decode_cb) { + num_decrypt_and_decode_calls_++; + if (!encrypted->end_of_stream()) + num_frames_in_decryptor_++; + + if (num_decrypt_and_decode_calls_ <= kDecodingDelay || + num_frames_in_decryptor_ == 0) { + video_decode_cb.Run(Decryptor::kNeedMoreData, + scoped_refptr<VideoFrame>()); + return; + } + + num_frames_in_decryptor_--; + video_decode_cb.Run(Decryptor::kSuccess, decoded_video_frame_); + } + // Sets up expectations and actions to put DecryptingVideoDecoder in an // active normal decoding state. void EnterNormalDecodingState() { - EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) - .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_video_frame_)) - .WillRepeatedly(RunCallback<1>(Decryptor::kNeedMoreData, - scoped_refptr<VideoFrame>())); - ReadAndExpectFrameReadyWith( - encrypted_buffer_, VideoDecoder::kOk, decoded_video_frame_); + EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)).WillRepeatedly( + Invoke(this, &DecryptingVideoDecoderTest::DecryptAndDecodeVideo)); + EXPECT_CALL(*this, FrameReady(decoded_video_frame_)); + for (int i = 0; i < kDecodingDelay + 1; ++i) + DecodeAndExpect(encrypted_buffer_, VideoDecoder::kOk); } // Sets up expectations and actions to put DecryptingVideoDecoder in an end // of stream state. This function must be called after // EnterNormalDecodingState() to work. void EnterEndOfStreamState() { - ReadAndExpectFrameReadyWith(DecoderBuffer::CreateEOSBuffer(), - VideoDecoder::kOk, - end_of_stream_video_frame_); + // The codec in the |decryptor_| will be flushed. + EXPECT_CALL(*this, FrameReady(decoded_video_frame_)) + .Times(kDecodingDelay); + DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), VideoDecoder::kOk); + EXPECT_EQ(0, num_frames_in_decryptor_); } // Make the video decode callback pending by saving and not firing it. @@ -147,7 +166,7 @@ class DecryptingVideoDecoderTest : public testing::Test { .WillOnce(SaveArg<1>(&pending_video_decode_cb_)); decoder_->Decode(encrypted_buffer_, - base::Bind(&DecryptingVideoDecoderTest::FrameReady, + base::Bind(&DecryptingVideoDecoderTest::DecodeDone, base::Unretained(this))); message_loop_.RunUntilIdle(); // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on @@ -159,7 +178,7 @@ class DecryptingVideoDecoderTest : public testing::Test { EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey, null_video_frame_)); decoder_->Decode(encrypted_buffer_, - base::Bind(&DecryptingVideoDecoderTest::FrameReady, + base::Bind(&DecryptingVideoDecoderTest::DecodeDone, base::Unretained(this))); message_loop_.RunUntilIdle(); } @@ -191,26 +210,27 @@ class DecryptingVideoDecoderTest : public testing::Test { } void Stop() { - EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, - IsNullCallback())) - .Times(AtMost(1)); EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kVideo)) .WillRepeatedly(InvokeWithoutArgs( this, &DecryptingVideoDecoderTest::AbortAllPendingCBs)); - decoder_->Stop(NewExpectedClosure()); + decoder_->Stop(); message_loop_.RunUntilIdle(); } MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&)); - MOCK_METHOD2(FrameReady, void(VideoDecoder::Status, - const scoped_refptr<VideoFrame>&)); + MOCK_METHOD1(FrameReady, void(const scoped_refptr<VideoFrame>&)); + MOCK_METHOD1(DecodeDone, void(VideoDecoder::Status)); base::MessageLoop message_loop_; scoped_ptr<DecryptingVideoDecoder> decoder_; scoped_ptr<StrictMock<MockDecryptor> > decryptor_; + // Variables to help the |decryptor_| to simulate decoding delay and flushing. + int num_decrypt_and_decode_calls_; + int num_frames_in_decryptor_; + Decryptor::DecoderInitCB pending_init_cb_; Decryptor::NewKeyCB key_added_cb_; Decryptor::VideoDecodeCB pending_video_decode_cb_; @@ -219,7 +239,6 @@ class DecryptingVideoDecoderTest : public testing::Test { scoped_refptr<DecoderBuffer> encrypted_buffer_; scoped_refptr<VideoFrame> decoded_video_frame_; scoped_refptr<VideoFrame> null_video_frame_; - scoped_refptr<VideoFrame> end_of_stream_video_frame_; private: DISALLOW_COPY_AND_ASSIGN(DecryptingVideoDecoderTest); @@ -260,6 +279,8 @@ TEST_F(DecryptingVideoDecoderTest, Reinitialize_Failure) { EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) .WillOnce(RunCallback<1>(false)); + // Reinitialize() expects the reinitialization to succeed. Call + // InitializeAndExpectStatus() directly to test the reinitialization failure. InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), DECODER_ERROR_NOT_SUPPORTED); } @@ -279,29 +300,10 @@ TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_DecodeError) { .WillRepeatedly(RunCallback<1>(Decryptor::kError, scoped_refptr<VideoFrame>(NULL))); - ReadAndExpectFrameReadyWith( - encrypted_buffer_, VideoDecoder::kDecodeError, null_video_frame_); + DecodeAndExpect(encrypted_buffer_, VideoDecoder::kDecodeError); // After a decode error occurred, all following decode returns kDecodeError. - ReadAndExpectFrameReadyWith( - encrypted_buffer_, VideoDecoder::kDecodeError, null_video_frame_); -} - -// Test the case where the decryptor returns kNeedMoreData to ask for more -// buffers before it can produce a frame. -TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_NeedMoreData) { - Initialize(); - - EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) - .WillOnce(RunCallback<1>(Decryptor::kNeedMoreData, - scoped_refptr<VideoFrame>())) - .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, - decoded_video_frame_)); - - ReadAndExpectFrameReadyWith( - encrypted_buffer_, VideoDecoder::kNotEnoughData, decoded_video_frame_); - ReadAndExpectFrameReadyWith( - encrypted_buffer_, VideoDecoder::kOk, decoded_video_frame_); + DecodeAndExpect(encrypted_buffer_, VideoDecoder::kDecodeError); } // Test the case where the decryptor receives end-of-stream buffer. @@ -320,7 +322,8 @@ TEST_F(DecryptingVideoDecoderTest, KeyAdded_DuringWaitingForKey) { EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_video_frame_)); - EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_)); + EXPECT_CALL(*this, FrameReady(decoded_video_frame_)); + EXPECT_CALL(*this, DecodeDone(VideoDecoder::kOk)); key_added_cb_.Run(); message_loop_.RunUntilIdle(); } @@ -334,7 +337,8 @@ TEST_F(DecryptingVideoDecoderTest, KeyAdded_DruingPendingDecode) { EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_video_frame_)); - EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_)); + EXPECT_CALL(*this, FrameReady(decoded_video_frame_)); + EXPECT_CALL(*this, DecodeDone(VideoDecoder::kOk)); // The video decode callback is returned after the correct decryption key is // added. key_added_cb_.Run(); @@ -363,7 +367,7 @@ TEST_F(DecryptingVideoDecoderTest, Reset_DuringPendingDecode) { Initialize(); EnterPendingDecodeState(); - EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, IsNull())); + EXPECT_CALL(*this, DecodeDone(VideoDecoder::kAborted)); Reset(); } @@ -373,7 +377,7 @@ TEST_F(DecryptingVideoDecoderTest, Reset_DuringWaitingForKey) { Initialize(); EnterWaitingForKeyState(); - EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, IsNull())); + EXPECT_CALL(*this, DecodeDone(VideoDecoder::kAborted)); Reset(); } @@ -401,7 +405,10 @@ TEST_F(DecryptingVideoDecoderTest, Stop_DuringDecryptorRequested) { EXPECT_CALL(*this, RequestDecryptorNotification(_)) .WillOnce(SaveArg<0>(&decryptor_ready_cb)); decoder_->Initialize(TestVideoConfig::NormalEncrypted(), - NewExpectedStatusCB(DECODER_ERROR_NOT_SUPPORTED)); + false, + NewExpectedStatusCB(DECODER_ERROR_NOT_SUPPORTED), + base::Bind(&DecryptingVideoDecoderTest::FrameReady, + base::Unretained(this))); message_loop_.RunUntilIdle(); // |decryptor_ready_cb| is saved but not called here. EXPECT_FALSE(decryptor_ready_cb.is_null()); @@ -446,7 +453,7 @@ TEST_F(DecryptingVideoDecoderTest, Stop_DuringPendingDecode) { Initialize(); EnterPendingDecodeState(); - EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, IsNull())); + EXPECT_CALL(*this, DecodeDone(VideoDecoder::kAborted)); Stop(); } @@ -456,7 +463,7 @@ TEST_F(DecryptingVideoDecoderTest, Stop_DuringWaitingForKey) { Initialize(); EnterWaitingForKeyState(); - EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, IsNull())); + EXPECT_CALL(*this, DecodeDone(VideoDecoder::kAborted)); Stop(); } @@ -478,7 +485,7 @@ TEST_F(DecryptingVideoDecoderTest, Stop_DuringPendingReset) { EnterPendingDecodeState(); EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kVideo)); - EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, IsNull())); + EXPECT_CALL(*this, DecodeDone(VideoDecoder::kAborted)); decoder_->Reset(NewExpectedClosure()); Stop(); |