OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
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 "base/run_loop.h" | |
11 #include "media/capture/webm_muxer.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using ::testing::_; | |
16 using ::testing::AtLeast; | |
17 using ::testing::Mock; | |
18 using ::testing::WithArgs; | |
19 | |
20 namespace media { | |
21 | |
22 // Dummy interface class to be able to MOCK its only function below. | |
23 class EventHandlerInterface { | |
24 public: | |
25 virtual void WriteCallback(const char* data, size_t len) {} | |
26 virtual ~EventHandlerInterface() {} | |
27 }; | |
28 | |
29 // Mock reference counted class to EXPECT calls to WriteCallback(). | |
30 class MockWebmMuxerEventHandler | |
31 : public base::RefCounted<MockWebmMuxerEventHandler>, | |
32 public EventHandlerInterface { | |
33 public: | |
34 MockWebmMuxerEventHandler() {} | |
35 | |
36 MOCK_METHOD2(WriteCallback, void(const char* data, size_t len)); | |
37 | |
38 private: | |
39 friend class base::RefCounted<MockWebmMuxerEventHandler>; | |
40 ~MockWebmMuxerEventHandler() override {} | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(MockWebmMuxerEventHandler); | |
43 }; | |
44 | |
45 class WebmMuxerTest : public testing::Test { | |
46 public: | |
47 void SaveEncodedDataLen(size_t len) { | |
48 last_encoded_length_ = len; | |
49 accumulated_position_ += len; | |
50 } | |
51 | |
52 protected: | |
53 WebmMuxerTest() | |
54 : loop_(new base::MessageLoop()), | |
55 mock_handler_(new MockWebmMuxerEventHandler()), | |
56 webm_muxer_(new WebmMuxer( | |
57 base::Bind(&MockWebmMuxerEventHandler::WriteCallback, | |
58 mock_handler_))), | |
59 last_encoded_length_(0), | |
60 accumulated_position_(0) {} | |
61 | |
62 void SetUp() override { | |
63 EXPECT_EQ(webm_muxer_->Position(), 0); | |
64 EXPECT_FALSE(webm_muxer_->Seekable()); | |
65 EXPECT_EQ(webm_muxer_->segment_->mode(), mkvmuxer::Segment::kLive); | |
66 } | |
67 | |
68 const scoped_ptr<base::MessageLoop> loop_; | |
69 const scoped_refptr<MockWebmMuxerEventHandler> mock_handler_; | |
70 const scoped_ptr<WebmMuxer> webm_muxer_; | |
71 | |
72 size_t last_encoded_length_; | |
73 int64_t accumulated_position_; | |
74 | |
75 private: | |
76 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest); | |
77 }; | |
78 | |
79 // Checks that the WriteCallback is called with appropriate params when | |
80 // WebmMuxer::Write() method is called. | |
81 TEST_F(WebmMuxerTest, Write) { | |
82 const char encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; | |
83 const int encoded_len = arraysize(encoded_data); | |
84 | |
85 EXPECT_CALL(*mock_handler_.get(), | |
86 WriteCallback(encoded_data, encoded_len)).Times(1); | |
87 webm_muxer_->Write(encoded_data, encoded_len); | |
88 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.
| |
89 | |
90 EXPECT_EQ(webm_muxer_->Position(), encoded_len); | |
91 Mock::VerifyAndClearExpectations(mock_handler_.get()); | |
92 } | |
93 | |
94 // This test sends an empty frame, checks that the file header is sent to the | |
95 // WriteCallback(), then sends a non-empty frame and checks that the Track | |
96 // header and a SimpleBlock are sent to the callback. | |
97 TEST_F(WebmMuxerTest, OnEncodedVideoEmptyAndNormalFrames) { | |
98 | |
99 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
100 .Times(AtLeast(1)) | |
101 .WillRepeatedly(WithArgs<1>( | |
102 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
103 webm_muxer_->OnEncodedVideo(0 /* track_number */, | |
104 std::string() /* Passing no data */, | |
105 base::TimeDelta::FromMicroseconds(0), | |
106 false /* keyframe */, | |
107 gfx::Size(320, 240) /* frame_size */, | |
108 30.0f /* frame_rate */); | |
109 base::RunLoop().RunUntilIdle(); | |
110 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); | |
111 | |
112 const uint32_t begin_of_second_block = accumulated_position_; | |
113 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); | |
114 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
115 .Times(AtLeast(1)) | |
116 .WillRepeatedly(WithArgs<1>( | |
117 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
118 webm_muxer_->OnEncodedVideo(0 /* track_number */, | |
119 encoded_data, | |
120 base::TimeDelta::FromMicroseconds(1), | |
121 false /* keyframe */, | |
122 gfx::Size(320, 240) /* frame_size */, | |
123 30.0f /* frame_rate */); | |
124 base::RunLoop().RunUntilIdle(); | |
125 EXPECT_EQ(last_encoded_length_, encoded_data.length()); | |
126 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); | |
127 const uint32_t kTrackInfoSize = 15u; | |
128 const uint32_t kSimpleBlockSize = 6u; | |
129 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kTrackInfoSize + | |
130 kSimpleBlockSize + encoded_data.length()), | |
131 accumulated_position_); | |
132 | |
133 Mock::VerifyAndClearExpectations(mock_handler_.get()); | |
134 } | |
135 | |
136 // This test sends two frames and checks that the WriteCallback is called with | |
137 // appropriate params in both cases. | |
138 TEST_F(WebmMuxerTest, OnEncodedVideoNormalFrames) { | |
139 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); | |
140 | |
141 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
142 .Times(AtLeast(1)) | |
143 .WillRepeatedly(WithArgs<1>( | |
144 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
145 webm_muxer_->OnEncodedVideo(1 /* track_number */, | |
146 encoded_data, | |
147 base::TimeDelta::FromMicroseconds(0), | |
148 false /* keyframe */, | |
149 gfx::Size(320, 240) /* frame_size */, | |
150 30.0f /* frame_rate */); | |
151 base::RunLoop().RunUntilIdle(); | |
152 | |
153 // First time around WriteCallback() is pinged a number of times to write the | |
154 // Matroska header, but at the end it dumps |encoded_data|. | |
155 EXPECT_EQ(last_encoded_length_, encoded_data.length()); | |
156 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); | |
157 EXPECT_GE(webm_muxer_->Position(), | |
158 static_cast<int64_t>(last_encoded_length_)); | |
159 | |
160 const int64_t begin_of_second_block = accumulated_position_; | |
161 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
162 .Times(AtLeast(1)) | |
163 .WillRepeatedly(WithArgs<1>( | |
164 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
165 webm_muxer_->OnEncodedVideo(1 /* track_number */, | |
166 encoded_data, | |
167 base::TimeDelta::FromMicroseconds(1), | |
168 false /* keyframe */, | |
169 gfx::Size(320, 240) /* frame_size */, | |
170 30.0f /* frame_rate */); | |
171 base::RunLoop().RunUntilIdle(); | |
172 | |
173 // The second time around the callbacks should include a SimpleBlock header, | |
174 // namely the track index, a timestamp and a flags byte, for a total of 6B. | |
175 EXPECT_EQ(last_encoded_length_, encoded_data.length()); | |
176 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); | |
177 const uint32_t kSimpleBlockSize = 6u; | |
178 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + | |
179 encoded_data.length()), | |
180 accumulated_position_); | |
181 | |
182 Mock::VerifyAndClearExpectations(mock_handler_.get()); | |
183 } | |
184 | |
185 } // namespace media | |
OLD | NEW |