| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 arraysize(kFakeKeyId)), | 37 arraysize(kFakeKeyId)), |
| 38 std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)), | 38 std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)), |
| 39 std::vector<SubsampleEntry>()))); | 39 std::vector<SubsampleEntry>()))); |
| 40 return buffer; | 40 return buffer; |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Use anonymous namespace here to prevent the actions to be defined multiple | 43 // Use anonymous namespace here to prevent the actions to be defined multiple |
| 44 // times across multiple test files. Sadly we can't use static for them. | 44 // times across multiple test files. Sadly we can't use static for them. |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 ACTION_P(RunCallbackIfNotNull, param) { | 47 ACTION_P3(ResetAndRunCallback, callback, p1, p2) { |
| 48 if (!arg0.is_null()) | 48 base::ResetAndReturn(callback).Run(p1, p2); |
| 49 arg0.Run(param); | |
| 50 } | |
| 51 | |
| 52 ACTION_P2(ResetAndRunCallback, callback, param) { | |
| 53 base::ResetAndReturn(callback).Run(param); | |
| 54 } | 49 } |
| 55 | 50 |
| 56 } // namespace | 51 } // namespace |
| 57 | 52 |
| 58 class DecryptingVideoDecoderTest : public testing::Test { | 53 class DecryptingVideoDecoderTest : public testing::Test { |
| 59 public: | 54 public: |
| 60 DecryptingVideoDecoderTest() | 55 DecryptingVideoDecoderTest() |
| 61 : decoder_(new DecryptingVideoDecoder( | 56 : decoder_(new DecryptingVideoDecoder( |
| 62 message_loop_.message_loop_proxy(), | 57 message_loop_.message_loop_proxy(), |
| 63 base::Bind( | 58 base::Bind( |
| 64 &DecryptingVideoDecoderTest::RequestDecryptorNotification, | 59 &DecryptingVideoDecoderTest::RequestDecryptorNotification, |
| 65 base::Unretained(this)))), | 60 base::Unretained(this)))), |
| 66 decryptor_(new StrictMock<MockDecryptor>()), | 61 decryptor_(new StrictMock<MockDecryptor>()), |
| 67 num_decrypt_and_decode_calls_(0), | 62 num_decrypt_and_decode_calls_(0), |
| 68 num_frames_in_decryptor_(0), | 63 num_frames_in_decryptor_(0), |
| 69 encrypted_buffer_(CreateFakeEncryptedBuffer()), | 64 encrypted_buffer_(CreateFakeEncryptedBuffer()), |
| 70 decoded_video_frame_(VideoFrame::CreateBlackFrame( | 65 decoded_video_frame_(VideoFrame::CreateBlackFrame( |
| 71 TestVideoConfig::NormalCodedSize())), | 66 TestVideoConfig::NormalCodedSize())), |
| 72 null_video_frame_(scoped_refptr<VideoFrame>()) { | 67 null_video_frame_(scoped_refptr<VideoFrame>()) { |
| 73 EXPECT_CALL(*this, RequestDecryptorNotification(_)) | |
| 74 .WillRepeatedly(RunCallbackIfNotNull(decryptor_.get())); | |
| 75 } | 68 } |
| 76 | 69 |
| 77 virtual ~DecryptingVideoDecoderTest() { | 70 virtual ~DecryptingVideoDecoderTest() { |
| 78 Destroy(); | 71 Destroy(); |
| 79 } | 72 } |
| 80 | 73 |
| 74 void ExpectDecryptorNotification(Decryptor* decryptor, bool expected_result) { |
| 75 EXPECT_CALL(*this, RequestDecryptorNotification(_)).WillOnce( |
| 76 RunCallback<0>(decryptor, |
| 77 base::Bind(&DecryptingVideoDecoderTest::DecryptorSet, |
| 78 base::Unretained(this)))); |
| 79 EXPECT_CALL(*this, DecryptorSet(expected_result)); |
| 80 } |
| 81 |
| 81 // Initializes the |decoder_| and expects |status|. Note the initialization | 82 // Initializes the |decoder_| and expects |status|. Note the initialization |
| 82 // can succeed or fail. | 83 // can succeed or fail. |
| 83 void InitializeAndExpectStatus(const VideoDecoderConfig& config, | 84 void InitializeAndExpectStatus(const VideoDecoderConfig& config, |
| 84 PipelineStatus status) { | 85 PipelineStatus status) { |
| 85 decoder_->Initialize(config, false, NewExpectedStatusCB(status), | 86 decoder_->Initialize(config, false, NewExpectedStatusCB(status), |
| 86 base::Bind(&DecryptingVideoDecoderTest::FrameReady, | 87 base::Bind(&DecryptingVideoDecoderTest::FrameReady, |
| 87 base::Unretained(this))); | 88 base::Unretained(this))); |
| 88 message_loop_.RunUntilIdle(); | 89 message_loop_.RunUntilIdle(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 // Initialize the |decoder_| and expects it to succeed. | 92 // Initialize the |decoder_| and expects it to succeed. |
| 92 void Initialize() { | 93 void Initialize() { |
| 94 ExpectDecryptorNotification(decryptor_.get(), true); |
| 93 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) | 95 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) |
| 94 .WillOnce(RunCallback<1>(true)); | 96 .WillOnce(RunCallback<1>(true)); |
| 95 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _)) | 97 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _)) |
| 96 .WillOnce(SaveArg<1>(&key_added_cb_)); | 98 .WillOnce(SaveArg<1>(&key_added_cb_)); |
| 97 | 99 |
| 98 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), PIPELINE_OK); | 100 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), PIPELINE_OK); |
| 99 } | 101 } |
| 100 | 102 |
| 101 // Reinitialize the |decoder_| and expects it to succeed. | 103 // Reinitialize the |decoder_| and expects it to succeed. |
| 102 void Reinitialize() { | 104 void Reinitialize() { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 | 218 |
| 217 decoder_.reset(); | 219 decoder_.reset(); |
| 218 message_loop_.RunUntilIdle(); | 220 message_loop_.RunUntilIdle(); |
| 219 } | 221 } |
| 220 | 222 |
| 221 MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&)); | 223 MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&)); |
| 222 | 224 |
| 223 MOCK_METHOD1(FrameReady, void(const scoped_refptr<VideoFrame>&)); | 225 MOCK_METHOD1(FrameReady, void(const scoped_refptr<VideoFrame>&)); |
| 224 MOCK_METHOD1(DecodeDone, void(VideoDecoder::Status)); | 226 MOCK_METHOD1(DecodeDone, void(VideoDecoder::Status)); |
| 225 | 227 |
| 228 MOCK_METHOD1(DecryptorSet, void(bool)); |
| 229 |
| 226 base::MessageLoop message_loop_; | 230 base::MessageLoop message_loop_; |
| 227 scoped_ptr<DecryptingVideoDecoder> decoder_; | 231 scoped_ptr<DecryptingVideoDecoder> decoder_; |
| 228 scoped_ptr<StrictMock<MockDecryptor> > decryptor_; | 232 scoped_ptr<StrictMock<MockDecryptor> > decryptor_; |
| 229 | 233 |
| 230 // Variables to help the |decryptor_| to simulate decoding delay and flushing. | 234 // Variables to help the |decryptor_| to simulate decoding delay and flushing. |
| 231 int num_decrypt_and_decode_calls_; | 235 int num_decrypt_and_decode_calls_; |
| 232 int num_frames_in_decryptor_; | 236 int num_frames_in_decryptor_; |
| 233 | 237 |
| 234 Decryptor::DecoderInitCB pending_init_cb_; | 238 Decryptor::DecoderInitCB pending_init_cb_; |
| 235 Decryptor::NewKeyCB key_added_cb_; | 239 Decryptor::NewKeyCB key_added_cb_; |
| 236 Decryptor::VideoDecodeCB pending_video_decode_cb_; | 240 Decryptor::VideoDecodeCB pending_video_decode_cb_; |
| 237 | 241 |
| 238 // Constant buffer/frames. | 242 // Constant buffer/frames. |
| 239 scoped_refptr<DecoderBuffer> encrypted_buffer_; | 243 scoped_refptr<DecoderBuffer> encrypted_buffer_; |
| 240 scoped_refptr<VideoFrame> decoded_video_frame_; | 244 scoped_refptr<VideoFrame> decoded_video_frame_; |
| 241 scoped_refptr<VideoFrame> null_video_frame_; | 245 scoped_refptr<VideoFrame> null_video_frame_; |
| 242 | 246 |
| 243 private: | 247 private: |
| 244 DISALLOW_COPY_AND_ASSIGN(DecryptingVideoDecoderTest); | 248 DISALLOW_COPY_AND_ASSIGN(DecryptingVideoDecoderTest); |
| 245 }; | 249 }; |
| 246 | 250 |
| 247 TEST_F(DecryptingVideoDecoderTest, Initialize_Normal) { | 251 TEST_F(DecryptingVideoDecoderTest, Initialize_Normal) { |
| 248 Initialize(); | 252 Initialize(); |
| 249 } | 253 } |
| 250 | 254 |
| 251 TEST_F(DecryptingVideoDecoderTest, Initialize_NullDecryptor) { | 255 TEST_F(DecryptingVideoDecoderTest, Initialize_NullDecryptor) { |
| 252 EXPECT_CALL(*this, RequestDecryptorNotification(_)) | 256 ExpectDecryptorNotification(NULL, false); |
| 253 .WillRepeatedly(RunCallbackIfNotNull(static_cast<Decryptor*>(NULL))); | |
| 254 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), | 257 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), |
| 255 DECODER_ERROR_NOT_SUPPORTED); | 258 DECODER_ERROR_NOT_SUPPORTED); |
| 256 } | 259 } |
| 257 | 260 |
| 258 TEST_F(DecryptingVideoDecoderTest, Initialize_Failure) { | 261 TEST_F(DecryptingVideoDecoderTest, Initialize_Failure) { |
| 259 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) | 262 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) |
| 260 .WillRepeatedly(RunCallback<1>(false)); | 263 .WillRepeatedly(RunCallback<1>(false)); |
| 261 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _)) | 264 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _)) |
| 262 .WillRepeatedly(SaveArg<1>(&key_added_cb_)); | 265 .WillRepeatedly(SaveArg<1>(&key_added_cb_)); |
| 263 | 266 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 false, | 411 false, |
| 409 NewExpectedStatusCB(DECODER_ERROR_NOT_SUPPORTED), | 412 NewExpectedStatusCB(DECODER_ERROR_NOT_SUPPORTED), |
| 410 base::Bind(&DecryptingVideoDecoderTest::FrameReady, | 413 base::Bind(&DecryptingVideoDecoderTest::FrameReady, |
| 411 base::Unretained(this))); | 414 base::Unretained(this))); |
| 412 message_loop_.RunUntilIdle(); | 415 message_loop_.RunUntilIdle(); |
| 413 // |decryptor_ready_cb| is saved but not called here. | 416 // |decryptor_ready_cb| is saved but not called here. |
| 414 EXPECT_FALSE(decryptor_ready_cb.is_null()); | 417 EXPECT_FALSE(decryptor_ready_cb.is_null()); |
| 415 | 418 |
| 416 // During destruction, RequestDecryptorNotification() should be called with a | 419 // During destruction, RequestDecryptorNotification() should be called with a |
| 417 // NULL callback to cancel the |decryptor_ready_cb|. | 420 // NULL callback to cancel the |decryptor_ready_cb|. |
| 418 EXPECT_CALL(*this, RequestDecryptorNotification(IsNullCallback())) | 421 EXPECT_CALL(*this, RequestDecryptorNotification(IsNullCallback())).WillOnce( |
| 419 .WillOnce(ResetAndRunCallback(&decryptor_ready_cb, | 422 ResetAndRunCallback(&decryptor_ready_cb, |
| 420 reinterpret_cast<Decryptor*>(NULL))); | 423 reinterpret_cast<Decryptor*>(NULL), |
| 424 base::Bind(&DecryptingVideoDecoderTest::DecryptorSet, |
| 425 base::Unretained(this)))); |
| 426 EXPECT_CALL(*this, DecryptorSet(_)).Times(0); |
| 421 Destroy(); | 427 Destroy(); |
| 422 } | 428 } |
| 423 | 429 |
| 424 // Test destruction when the decoder is in kPendingDecoderInit state. | 430 // Test destruction when the decoder is in kPendingDecoderInit state. |
| 425 TEST_F(DecryptingVideoDecoderTest, Destroy_DuringPendingDecoderInit) { | 431 TEST_F(DecryptingVideoDecoderTest, Destroy_DuringPendingDecoderInit) { |
| 432 ExpectDecryptorNotification(decryptor_.get(), true); |
| 426 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) | 433 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _)) |
| 427 .WillOnce(SaveArg<1>(&pending_init_cb_)); | 434 .WillOnce(SaveArg<1>(&pending_init_cb_)); |
| 428 | 435 |
| 429 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), | 436 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), |
| 430 DECODER_ERROR_NOT_SUPPORTED); | 437 DECODER_ERROR_NOT_SUPPORTED); |
| 431 EXPECT_FALSE(pending_init_cb_.is_null()); | 438 EXPECT_FALSE(pending_init_cb_.is_null()); |
| 432 | 439 |
| 433 Destroy(); | 440 Destroy(); |
| 434 } | 441 } |
| 435 | 442 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 | 500 |
| 494 // Test destruction after the decoder has been reset. | 501 // Test destruction after the decoder has been reset. |
| 495 TEST_F(DecryptingVideoDecoderTest, Destroy_AfterReset) { | 502 TEST_F(DecryptingVideoDecoderTest, Destroy_AfterReset) { |
| 496 Initialize(); | 503 Initialize(); |
| 497 EnterNormalDecodingState(); | 504 EnterNormalDecodingState(); |
| 498 Reset(); | 505 Reset(); |
| 499 Destroy(); | 506 Destroy(); |
| 500 } | 507 } |
| 501 | 508 |
| 502 } // namespace media | 509 } // namespace media |
| OLD | NEW |