Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(580)

Side by Side Diff: media/capture/webm_muxer_unittest.cc

Issue 1225123006: media/capture: Adding WebmMuxer class and unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@m_crbug262211__MSRecorder__2__libwebm_reland_in_third_party
Patch Set: Removed MockWebmMuxerEventHandler Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "media/capture/webm_muxer.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using ::testing::_;
15 using ::testing::AtLeast;
16 using ::testing::Mock;
17 using ::testing::WithArgs;
18
19 namespace media {
20
21 // Dummy interface class to be able to MOCK its only function below.
22 class EventHandlerInterface {
23 public:
24 virtual void WriteCallback(const char* data, size_t len) = 0;
25 virtual ~EventHandlerInterface() {}
26 };
27
28 class WebmMuxerTest : public testing::Test, public EventHandlerInterface {
29 public:
30 WebmMuxerTest()
31 : webm_muxer_(base::Bind(&WebmMuxerTest::WriteCallback,
32 base::Unretained(this))),
33 last_encoded_length_(0),
34 accumulated_position_(0) {
35 EXPECT_EQ(webm_muxer_.Position(), 0);
36 EXPECT_FALSE(webm_muxer_.Seekable());
37 EXPECT_EQ(webm_muxer_.segment_.mode(), mkvmuxer::Segment::kLive);
38 }
39
40 MOCK_METHOD2(WriteCallback, void(const char* data, size_t len));
41
42 void SaveEncodedDataLen(size_t len) {
43 last_encoded_length_ = len;
44 accumulated_position_ += len;
45 }
46
47 mkvmuxer::int64 GetWebmMuxerPosition() const {
48 return webm_muxer_.Position();
49 }
50
51 const mkvmuxer::Segment& GetWebmMuxerSegment() const {
52 return webm_muxer_.segment_;
53 }
54
55 mkvmuxer::int32 WebmMuxerWrite(const void* buf, mkvmuxer::uint32 len) {
56 return webm_muxer_.Write(buf, len);
57 }
58
59 WebmMuxer webm_muxer_;
60
61 size_t last_encoded_length_;
62 int64_t accumulated_position_;
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest);
66 };
67
68 // Checks that AddVideoTrack adds a Track.
69 TEST_F(WebmMuxerTest, AddVideoTrack) {
70 const uint64_t track_number = webm_muxer_.AddVideoTrack(gfx::Size(320, 240),
71 30.0f);
72 EXPECT_TRUE(GetWebmMuxerSegment().GetTrackByNumber(track_number));
73 }
74
75 // Checks that the WriteCallback is called with appropriate params when
76 // WebmMuxer::Write() method is called.
77 TEST_F(WebmMuxerTest, Write) {
78 const char encoded_data[] = "abcdefghijklmnopqrstuvwxyz";
79 const int encoded_len = arraysize(encoded_data);
80
81 EXPECT_CALL(*this, WriteCallback(encoded_data, encoded_len));
82 WebmMuxerWrite(encoded_data, encoded_len);
83
84 EXPECT_EQ(GetWebmMuxerPosition(), encoded_len);
85 }
86
87 // This test sends two frames and checks that the WriteCallback is called with
88 // appropriate params in both cases.
89 TEST_F(WebmMuxerTest, OnEncodedVideoNormalFrames) {
90 const uint8_t encoded_data[] = "abcdefghijklmnopqrstuvwxyz";
91 const uint64_t track_number = webm_muxer_.AddVideoTrack(gfx::Size(320, 240),
92 30.0f);
93
94 EXPECT_CALL(*this, WriteCallback(_, _))
95 .Times(AtLeast(1))
96 .WillRepeatedly(WithArgs<1>(
97 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
98 webm_muxer_.OnEncodedVideo(track_number,
99 encoded_data,
100 arraysize(encoded_data),
101 base::TimeDelta::FromMicroseconds(0),
102 false /* keyframe */);
103
104 // First time around WriteCallback() is pinged a number of times to write the
105 // Matroska header, but at the end it dumps |encoded_data|.
106 EXPECT_EQ(last_encoded_length_, arraysize(encoded_data));
107 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_);
108 EXPECT_GE(GetWebmMuxerPosition(),
109 static_cast<int64_t>(last_encoded_length_));
110
111 const int64_t begin_of_second_block = accumulated_position_;
112 EXPECT_CALL(*this, WriteCallback(_, _))
113 .Times(AtLeast(1))
114 .WillRepeatedly(WithArgs<1>(
115 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
116 webm_muxer_.OnEncodedVideo(track_number,
117 encoded_data,
118 arraysize(encoded_data),
119 base::TimeDelta::FromMicroseconds(1),
120 false /* keyframe */);
121
122 // The second time around the callbacks should include a SimpleBlock header,
123 // namely the track index, a timestamp and a flags byte, for a total of 6B.
124 EXPECT_EQ(last_encoded_length_, arraysize(encoded_data));
125 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_);
126 const uint32_t kSimpleBlockSize = 6u;
127 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize +
128 arraysize(encoded_data)),
129 accumulated_position_);
130 }
131
132 } // namespace media
OLDNEW
« media/capture/webm_muxer.h ('K') | « media/capture/webm_muxer.cc ('k') | media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698