Chromium Code Reviews| Index: media/filters/webm_muxer_unittest.cc |
| diff --git a/media/filters/webm_muxer_unittest.cc b/media/filters/webm_muxer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..24eed95b2260678f99c12478d79d93087d26475a |
| --- /dev/null |
| +++ b/media/filters/webm_muxer_unittest.cc |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/run_loop.h" |
| +#include "media/filters/webm_muxer.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using ::testing::_; |
| +using ::testing::AtLeast; |
| +using ::testing::Mock; |
| +using ::testing::WithArgs; |
| + |
| +namespace media { |
| + |
| +// Dummy interface class to be able to MOCK its only function below. |
| +class EventHandlerInterface { |
| + public: |
| + virtual void WriteCallback(const char* data, int len) {} |
| + virtual ~EventHandlerInterface() {} |
| +}; |
| + |
| +// Mock reference counted class to EXPECT calls to WriteCallback(). |
| +class MockWebmMuxerEventHandler |
| + : public base::RefCounted<MockWebmMuxerEventHandler>, |
| + public EventHandlerInterface { |
| + public: |
| + MockWebmMuxerEventHandler() {} |
| + |
| + MOCK_METHOD2(WriteCallback, void(const char* data, int len)); |
| + |
| + private: |
| + friend class base::RefCounted<MockWebmMuxerEventHandler>; |
| + ~MockWebmMuxerEventHandler() override {} |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockWebmMuxerEventHandler); |
| +}; |
| + |
| +class WebmMuxerTest : public testing::Test { |
| + public: |
| + void SaveEncodedDataLen(int len) { |
| + last_encoded_length_ = len; |
| + accumulated_position_ += len; |
| + } |
| + |
| + protected: |
| + WebmMuxerTest() |
| + : loop_(new base::MessageLoop()), |
| + mock_handler_(new MockWebmMuxerEventHandler()), |
| + webm_muxer_(new WebmMuxer( |
| + base::Bind(&MockWebmMuxerEventHandler::WriteCallback, |
| + mock_handler_))), |
| + last_encoded_length_(0), |
| + accumulated_position_(0) {} |
| + |
| + void SetUp() override { |
| + EXPECT_EQ(webm_muxer_->Position(), 0); |
| + EXPECT_FALSE(webm_muxer_->Seekable()); |
| + EXPECT_EQ(webm_muxer_->segment_->mode(), mkvmuxer::Segment::kLive); |
| + } |
| + |
| + const scoped_ptr<base::MessageLoop> loop_; |
| + const scoped_refptr<MockWebmMuxerEventHandler> mock_handler_; |
| + const scoped_ptr<WebmMuxer> webm_muxer_; |
| + |
| + uint32_t last_encoded_length_, accumulated_position_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest); |
| +}; |
| + |
| +// Checks that the WriteCallback is called with appropriate params when |
| +// WebmMuxer::Write() method is called. |
| +TEST_F(WebmMuxerTest, Write) { |
| + const char encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; |
| + const int encoded_len = arraysize(encoded_data); |
| + |
| + EXPECT_CALL(*mock_handler_.get(), |
| + WriteCallback(encoded_data, encoded_len)).Times(1); |
| + webm_muxer_->Write(encoded_data, encoded_len); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ(webm_muxer_->Position(), encoded_len); |
| + Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| +} |
| + |
| +// Checks that the WriteCallback is called with appropriate params when |
| +// WebmMuxer::OnEncodedVideo() method is called. |
| +TEST_F(WebmMuxerTest, OnEncodedVideo) { |
| + const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| + |
| + EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
| + .Times(AtLeast(1)) |
| + .WillRepeatedly(WithArgs<1>( |
| + Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| + webm_muxer_->OnEncodedVideo(0 /* track_index */, |
| + encoded_data, |
| + base::TimeTicks::Now(), |
| + false /* keyframe */, |
| + gfx::Size(320, 240) /* frame_size */, |
| + 30.0f /* frame_rate */); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // First time around WriteCallback() is pinged a number of times to write the |
| + // Matroska header, but at the end it dumps |encoded_data|. |
| + EXPECT_EQ(last_encoded_length_, encoded_data.length()); |
| + EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| + EXPECT_GE(webm_muxer_->Position(), last_encoded_length_); |
| + const uint32_t begin_of_second_block = accumulated_position_; |
| + |
| + EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
| + .Times(AtLeast(1)) |
| + .WillRepeatedly(WithArgs<1>( |
| + Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| + webm_muxer_->OnEncodedVideo(0 /* track_index */, |
| + encoded_data, |
| + base::TimeTicks::Now(), |
| + false /* keyframe */, |
| + gfx::Size(320, 240) /* frame_size */, |
| + 30.0f /* frame_rate */); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // The second time around the callbacks should include a SimpleBlock header, |
| + // namely the track index, a timestamp and a flags byte, for a total of 6B. |
| + EXPECT_EQ(last_encoded_length_, encoded_data.length()); |
| + EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| + const uint32_t kSimpleBlockSize = 6u; |
| + EXPECT_EQ(begin_of_second_block + kSimpleBlockSize + encoded_data.length(), |
| + accumulated_position_); |
| + |
| + Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| +} |
|
miu
2015/07/16 01:46:26
Consider writing a test that encodes multiple trac
mcasas
2015/07/17 17:45:07
It is possible, I think Segment seems to output th
miu
2015/07/17 22:23:18
Weird. I guess no need to test that you can't do
|
| + |
| +} // namespace media |