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..979aad2482e03d31d9dee57ebd9b439a89eb0c9a |
--- /dev/null |
+++ b/media/capture/webm_muxer_unittest.cc |
@@ -0,0 +1,190 @@ |
+// 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 "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) {} |
DaleCurtis
2015/07/21 16:52:31
= 0;
mcasas
2015/07/21 19:47:00
Done.
|
+ 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: |
DaleCurtis
2015/07/21 16:52:31
Just leave everything public, no need to worry abo
mcasas
2015/07/21 19:46:59
Done.
|
+ WebmMuxerTest() |
+ : mock_handler_(new MockWebmMuxerEventHandler()), |
+ webm_muxer_(new WebmMuxer( |
+ base::Bind(&MockWebmMuxerEventHandler::WriteCallback, |
DaleCurtis
2015/07/21 16:52:31
Why bind to a whole new class? Just add the MOCK_M
mcasas
2015/07/21 19:47:00
Then I'd need to use base::Unretained since this c
DaleCurtis
2015/07/21 19:52:26
That's fine, we use that all over the place and si
|
+ mock_handler_))), |
+ last_encoded_length_(0), |
+ accumulated_position_(0) {} |
+ |
+ void SetUp() override { |
DaleCurtis
2015/07/21 16:52:31
Choose SetUp() or a constructor, no need for both.
mcasas
2015/07/21 19:47:00
Done.
|
+ EXPECT_EQ(webm_muxer_->Position(), 0); |
+ EXPECT_FALSE(webm_muxer_->Seekable()); |
+ EXPECT_EQ(webm_muxer_->segment_.mode(), mkvmuxer::Segment::kLive); |
+ } |
+ |
+ mkvmuxer::int64 GetWebmMuxerPosition() const { |
+ return webm_muxer_->Position(); |
+ } |
+ |
+ mkvmuxer::Segment& GetWebmMuxerSegment() const { |
+ return webm_muxer_->segment_; |
+ } |
+ |
+ mkvmuxer::int32 WebmMuxerWrite(const void* buf, mkvmuxer::uint32 len) { |
+ return webm_muxer_->Write(buf, len); |
+ } |
+ |
+ const scoped_refptr<MockWebmMuxerEventHandler> mock_handler_; |
DaleCurtis
2015/07/21 16:52:31
Why is this ref counted? Is this to deal with the
mcasas
2015/07/21 19:47:00
It's to Bind() the callback.
|
+ const scoped_ptr<WebmMuxer> webm_muxer_; |
DaleCurtis
2015/07/21 16:52:31
No scoped_ptr, just stack allocate.
mcasas
2015/07/21 19:47:00
Done.
|
+ |
+ size_t last_encoded_length_; |
+ int64_t accumulated_position_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest); |
+}; |
+// Checks that AddVideoTrack adds a Track. |
DaleCurtis
2015/07/21 16:52:31
Newline.
mcasas
2015/07/21 19:47:00
Done.
|
+TEST_F(WebmMuxerTest, AddVideoTrack) { |
+ const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240), |
+ 30.0f); |
+ EXPECT_NE(nullptr, GetWebmMuxerSegment().GetTrackByNumber(track_number)); |
DaleCurtis
2015/07/21 16:52:31
EXPECT_TRUE
mcasas
2015/07/21 19:47:00
Done.
|
+} |
+ |
+// 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)); |
+ WebmMuxerWrite(encoded_data, encoded_len); |
+ |
+ EXPECT_EQ(GetWebmMuxerPosition(), encoded_len); |
+} |
+ |
+// 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) { |
+ const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240), |
+ 30.0f); |
+ |
+ EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
+ .Times(AtLeast(1)) |
+ .WillRepeatedly(WithArgs<1>( |
+ Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
+ const uint8_t empty_data[] = ""; |
+ webm_muxer_->OnEncodedVideo(track_number, |
+ empty_data, |
+ arraysize(empty_data), |
+ base::TimeDelta::FromMicroseconds(0), |
+ false /* keyframe */); |
+ EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
+ |
+ const uint32_t begin_of_second_block = accumulated_position_; |
+ const uint8_t encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; |
+ EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
+ .Times(AtLeast(1)) |
+ .WillRepeatedly(WithArgs<1>( |
+ Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
+ webm_muxer_->OnEncodedVideo(track_number, |
+ encoded_data, |
+ arraysize(encoded_data), |
+ base::TimeDelta::FromMicroseconds(1), |
+ false /* keyframe */); |
+ EXPECT_EQ(last_encoded_length_, arraysize(encoded_data)); |
+ EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
+ const uint32_t kTrackInfoSize = 15u; |
+ const uint32_t kSimpleBlockSize = 6u; |
+ EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kTrackInfoSize + |
+ kSimpleBlockSize + arraysize(encoded_data)), |
+ accumulated_position_); |
+} |
+ |
+// This test sends two frames and checks that the WriteCallback is called with |
+// appropriate params in both cases. |
+TEST_F(WebmMuxerTest, OnEncodedVideoNormalFrames) { |
+ const uint8_t encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; |
+ const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240), |
+ 30.0f); |
+ |
+ EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
+ .Times(AtLeast(1)) |
+ .WillRepeatedly(WithArgs<1>( |
+ Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
+ webm_muxer_->OnEncodedVideo(track_number, |
+ encoded_data, |
+ arraysize(encoded_data), |
+ base::TimeDelta::FromMicroseconds(0), |
+ false /* keyframe */); |
+ |
+ // 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_, arraysize(encoded_data)); |
+ EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
+ EXPECT_GE(GetWebmMuxerPosition(), |
+ 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(track_number, |
+ encoded_data, |
+ arraysize(encoded_data), |
+ base::TimeDelta::FromMicroseconds(1), |
+ false /* keyframe */); |
+ |
+ // 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_, arraysize(encoded_data)); |
+ EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
+ const uint32_t kSimpleBlockSize = 6u; |
+ EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + |
+ arraysize(encoded_data)), |
+ accumulated_position_); |
+} |
+ |
+} // namespace media |