cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / media / capture / webm_muxer_unittest.cc
blobe3fd4d4f0fa0e603eb848ba0b55ab1b9f398c3b2
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/location.h"
7 #include "base/macros.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "media/base/video_frame.h"
11 #include "media/capture/webm_muxer.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::_;
16 using ::testing::AtLeast;
17 using ::testing::Mock;
18 using ::testing::WithArgs;
20 namespace media {
22 // Dummy interface class to be able to MOCK its only function below.
23 class EventHandlerInterface {
24 public:
25 virtual void WriteCallback(const base::StringPiece& encoded_data) = 0;
26 virtual ~EventHandlerInterface() {}
29 class WebmMuxerTest : public testing::Test, public EventHandlerInterface {
30 public:
31 WebmMuxerTest()
32 : webm_muxer_(base::Bind(&WebmMuxerTest::WriteCallback,
33 base::Unretained(this))),
34 last_encoded_length_(0),
35 accumulated_position_(0) {
36 EXPECT_EQ(webm_muxer_.Position(), 0);
37 EXPECT_FALSE(webm_muxer_.Seekable());
38 EXPECT_EQ(webm_muxer_.segment_.mode(), mkvmuxer::Segment::kLive);
41 MOCK_METHOD1(WriteCallback, void(const base::StringPiece&));
43 void SaveEncodedDataLen(const base::StringPiece& encoded_data) {
44 last_encoded_length_ = encoded_data.size();
45 accumulated_position_ += encoded_data.size();
48 mkvmuxer::int64 GetWebmMuxerPosition() const {
49 return webm_muxer_.Position();
52 mkvmuxer::int32 WebmMuxerWrite(const void* buf, mkvmuxer::uint32 len) {
53 return webm_muxer_.Write(buf, len);
56 WebmMuxer webm_muxer_;
58 size_t last_encoded_length_;
59 int64_t accumulated_position_;
61 private:
62 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest);
65 // Checks that the WriteCallback is called with appropriate params when
66 // WebmMuxer::Write() method is called.
67 TEST_F(WebmMuxerTest, Write) {
68 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz");
70 EXPECT_CALL(*this, WriteCallback(encoded_data));
71 WebmMuxerWrite(encoded_data.data(), encoded_data.size());
73 EXPECT_EQ(GetWebmMuxerPosition(), static_cast<int64_t>(encoded_data.size()));
76 // This test sends two frames and checks that the WriteCallback is called with
77 // appropriate params in both cases.
78 TEST_F(WebmMuxerTest, OnEncodedVideoTwoFrames) {
79 const gfx::Size frame_size(160, 80);
80 const scoped_refptr<VideoFrame> video_frame =
81 VideoFrame::CreateBlackFrame(frame_size);
82 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz");
84 EXPECT_CALL(*this, WriteCallback(_))
85 .Times(AtLeast(1))
86 .WillRepeatedly(WithArgs<0>(
87 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
88 webm_muxer_.OnEncodedVideo(video_frame,
89 encoded_data,
90 base::TimeTicks::Now(),
91 false /* keyframe */);
93 // First time around WriteCallback() is pinged a number of times to write the
94 // Matroska header, but at the end it dumps |encoded_data|.
95 EXPECT_EQ(last_encoded_length_, encoded_data.size());
96 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_);
97 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_));
99 const int64_t begin_of_second_block = accumulated_position_;
100 EXPECT_CALL(*this, WriteCallback(_))
101 .Times(AtLeast(1))
102 .WillRepeatedly(WithArgs<0>(
103 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
104 webm_muxer_.OnEncodedVideo(video_frame,
105 encoded_data,
106 base::TimeTicks::Now(),
107 false /* keyframe */);
109 // The second time around the callbacks should include a SimpleBlock header,
110 // namely the track index, a timestamp and a flags byte, for a total of 6B.
111 EXPECT_EQ(last_encoded_length_, encoded_data.size());
112 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_);
113 const uint32_t kSimpleBlockSize = 6u;
114 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize +
115 encoded_data.size()),
116 accumulated_position_);
119 } // namespace media