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/filters/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, int 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, int 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(int 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 uint32_t last_encoded_length_, accumulated_position_; | |
73 | |
74 private: | |
75 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest); | |
76 }; | |
77 | |
78 // Checks that the WriteCallback is called with appropriate params when | |
79 // WebmMuxer::Write() method is called. | |
80 TEST_F(WebmMuxerTest, Write) { | |
81 const char encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; | |
82 const int encoded_len = arraysize(encoded_data); | |
83 | |
84 EXPECT_CALL(*mock_handler_.get(), | |
85 WriteCallback(encoded_data, encoded_len)).Times(1); | |
86 webm_muxer_->Write(encoded_data, encoded_len); | |
87 base::RunLoop().RunUntilIdle(); | |
88 | |
89 EXPECT_EQ(webm_muxer_->Position(), encoded_len); | |
90 Mock::VerifyAndClearExpectations(mock_handler_.get()); | |
91 } | |
92 | |
93 // Checks that the WriteCallback is called with appropriate params when | |
94 // WebmMuxer::OnEncodedVideo() method is called. | |
95 TEST_F(WebmMuxerTest, OnEncodedVideo) { | |
96 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); | |
97 | |
98 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
99 .Times(AtLeast(1)) | |
100 .WillRepeatedly(WithArgs<1>( | |
101 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
102 webm_muxer_->OnEncodedVideo(0 /* track_index */, | |
103 encoded_data, | |
104 base::TimeTicks::Now(), | |
105 false /* keyframe */, | |
106 gfx::Size(320, 240) /* frame_size */, | |
107 30.0f /* frame_rate */); | |
108 base::RunLoop().RunUntilIdle(); | |
109 | |
110 // First time around WriteCallback() is pinged a number of times to write the | |
111 // Matroska header, but at the end it dumps |encoded_data|. | |
112 EXPECT_EQ(last_encoded_length_, encoded_data.length()); | |
113 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); | |
114 EXPECT_GE(webm_muxer_->Position(), last_encoded_length_); | |
115 const uint32_t begin_of_second_block = accumulated_position_; | |
116 | |
117 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
118 .Times(AtLeast(1)) | |
119 .WillRepeatedly(WithArgs<1>( | |
120 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
121 webm_muxer_->OnEncodedVideo(0 /* track_index */, | |
122 encoded_data, | |
123 base::TimeTicks::Now(), | |
124 false /* keyframe */, | |
125 gfx::Size(320, 240) /* frame_size */, | |
126 30.0f /* frame_rate */); | |
127 base::RunLoop().RunUntilIdle(); | |
128 | |
129 // The second time around the callbacks should include a SimpleBlock header, | |
130 // namely the track index, a timestamp and a flags byte, for a total of 6B. | |
131 EXPECT_EQ(last_encoded_length_, encoded_data.length()); | |
132 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); | |
133 const uint32_t kSimpleBlockSize = 6u; | |
134 EXPECT_EQ(begin_of_second_block + kSimpleBlockSize + encoded_data.length(), | |
135 accumulated_position_); | |
136 | |
137 Mock::VerifyAndClearExpectations(mock_handler_.get()); | |
138 } | |
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
| |
139 | |
140 } // namespace media | |
OLD | NEW |