Chromium Code Reviews| Index: media/capture/webm_muxer_unittest.cc |
| diff --git a/media/capture/webm_muxer_unittest.cc b/media/capture/webm_muxer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..099515e2dbd64fd326374cb168ab73b5c586b9d1 |
| --- /dev/null |
| +++ b/media/capture/webm_muxer_unittest.cc |
| @@ -0,0 +1,185 @@ |
| +// 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/capture/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, size_t 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, size_t len)); |
| + |
| + private: |
| + friend class base::RefCounted<MockWebmMuxerEventHandler>; |
| + ~MockWebmMuxerEventHandler() override {} |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockWebmMuxerEventHandler); |
| +}; |
| + |
| +class WebmMuxerTest : public testing::Test { |
| + public: |
| + void SaveEncodedDataLen(size_t 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_; |
| + |
| + size_t last_encoded_length_; |
| + int64_t 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(); |
|
miu
2015/07/17 22:23:19
IIUC, you don't need any of these base::RunLoop().
mcasas
2015/07/18 00:05:22
True, Done.
|
| + |
| + EXPECT_EQ(webm_muxer_->Position(), encoded_len); |
| + Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| +} |
| + |
| +// This test sends an empty frame, checks that the file header is sent to the |
| +// WriteCallback(), then sends a non-empty frame and checks that the Track |
| +// header and a SimpleBlock are sent to the callback. |
| +TEST_F(WebmMuxerTest, OnEncodedVideoEmptyAndNormalFrames) { |
| + |
| + EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
| + .Times(AtLeast(1)) |
| + .WillRepeatedly(WithArgs<1>( |
| + Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| + webm_muxer_->OnEncodedVideo(0 /* track_number */, |
| + std::string() /* Passing no data */, |
| + base::TimeDelta::FromMicroseconds(0), |
| + false /* keyframe */, |
| + gfx::Size(320, 240) /* frame_size */, |
| + 30.0f /* frame_rate */); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| + |
| + const uint32_t begin_of_second_block = accumulated_position_; |
| + 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_number */, |
| + encoded_data, |
| + base::TimeDelta::FromMicroseconds(1), |
| + false /* keyframe */, |
| + gfx::Size(320, 240) /* frame_size */, |
| + 30.0f /* frame_rate */); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(last_encoded_length_, encoded_data.length()); |
| + EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| + const uint32_t kTrackInfoSize = 15u; |
| + const uint32_t kSimpleBlockSize = 6u; |
| + EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kTrackInfoSize + |
| + kSimpleBlockSize + encoded_data.length()), |
| + accumulated_position_); |
| + |
| + Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| +} |
| + |
| +// This test sends two frames and checks that the WriteCallback is called with |
| +// appropriate params in both cases. |
| +TEST_F(WebmMuxerTest, OnEncodedVideoNormalFrames) { |
| + 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(1 /* track_number */, |
| + encoded_data, |
| + base::TimeDelta::FromMicroseconds(0), |
| + 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(), |
| + static_cast<int64_t>(last_encoded_length_)); |
| + |
| + const int64_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(1 /* track_number */, |
| + encoded_data, |
| + base::TimeDelta::FromMicroseconds(1), |
| + 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(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + |
| + encoded_data.length()), |
| + accumulated_position_); |
| + |
| + Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| +} |
| + |
| +} // namespace media |