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

Side by Side Diff: content/renderer/media/media_recorder_handler_unittest.cc

Issue 1313603004: MediaRecorderHandler (video part) and unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase, gyp and MediaRecorderHandlerTest Created 5 years, 3 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/run_loop.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "content/child/child_process.h"
8 #include "content/renderer/media/media_recorder_handler.h"
9 #include "content/renderer/media/mock_media_stream_registry.h"
10 #include "media/base/video_frame.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14
15 using ::testing::_;
16 using ::testing::AtLeast;
17 using ::testing::InSequence;
18 using ::testing::Lt;
19 using ::testing::Mock;
20
21 namespace content {
22
23 ACTION_P(RunClosure, closure) {
24 closure.Run();
25 }
26
27 static const std::string kTestStreamUrl = "stream_url";
28 static const std::string kTestVideoTrackId = "video_track_id";
29
30 class MediaRecorderHandlerTest
31 : public testing::Test
32 , public MediaRecorderHandlerClient {
33 public:
34 MediaRecorderHandlerTest()
35 : media_recorder_handler_(new MediaRecorderHandler()) {
36 EXPECT_FALSE(media_recorder_handler_->recording_);
37
38 registry_.Init(kTestStreamUrl);
39 registry_.AddVideoTrack(kTestVideoTrackId);
40 }
41
42 MOCK_METHOD3(writeData, void(const char*, size_t, bool));
43
44 bool recording() const { return media_recorder_handler_->recording_; }
45 bool hasVideoRecorders() const {
46 return !media_recorder_handler_->video_recorders_.empty();
47 }
48
49 void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame) {
50 media_recorder_handler_->OnVideoFrameForTesting(frame,
51 base::TimeTicks::Now());
52 }
53
54 // The Class under test. Needs to be scoped_ptr to force its destruction.
55 scoped_ptr<MediaRecorderHandler> media_recorder_handler_;
56
57 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks
58 // and Sources in |registry_| into believing they are on the right threads.
59 const base::MessageLoopForUI message_loop_;
60 const ChildProcess child_process_;
61 MockMediaStreamRegistry registry_;
62
63 private:
64 DISALLOW_COPY_AND_ASSIGN(MediaRecorderHandlerTest);
65 };
66
67 // Checks that canSupportMimeType() works as expected.
68 // TODO(mcasas): revisit this when canSupportMimeType() is fully implemented.
69 TEST_F(MediaRecorderHandlerTest, CanSupportMimeType) {
70 const blink::WebString mime_type(base::UTF8ToUTF16("video/vp8"));
71 EXPECT_FALSE(media_recorder_handler_->canSupportMimeType(mime_type));
72 }
73
74 // Checks that the initialization-destruction sequence works fine.
75 TEST_F(MediaRecorderHandlerTest, InitializeStartStop) {
76 const blink::WebString mime_type(base::UTF8ToUTF16("video/vp8"));
77 EXPECT_TRUE(media_recorder_handler_->initialize(this,
78 registry_.test_stream(),
79 mime_type));
80 EXPECT_FALSE(recording());
81 EXPECT_FALSE(hasVideoRecorders());
82
83 EXPECT_TRUE(media_recorder_handler_->start());
84 EXPECT_TRUE(recording());
85 EXPECT_TRUE(hasVideoRecorders());
86
87 media_recorder_handler_->stop();
88 EXPECT_FALSE(recording());
89 EXPECT_FALSE(hasVideoRecorders());
90
91 // Expect a last call on destruction.
92 EXPECT_CALL(*this, writeData(_, _, true)).Times(1);
93 media_recorder_handler_.reset();
94 }
95
96 // Sends 2 frames and expect them as WebM contained encoded data in writeData().
97 TEST_F(MediaRecorderHandlerTest, EncodeVideoFrames) {
98 const blink::WebString mime_type(base::UTF8ToUTF16("video/vp8"));
99 EXPECT_TRUE(media_recorder_handler_->initialize(this, registry_.test_stream(),
100 mime_type));
101 EXPECT_TRUE(media_recorder_handler_->start());
102
103 InSequence s;
104 const scoped_refptr<media::VideoFrame> video_frame =
105 media::VideoFrame::CreateBlackFrame(gfx::Size(160, 80));
106
107 {
108 base::RunLoop run_loop;
109 base::Closure quit_closure = run_loop.QuitClosure();
110 // writeData() is pinged a number of times as the WebM header is written;
111 // the last time it is called it has the encoded data.
112 const size_t kEncodedDataSize = 52;
113 EXPECT_CALL(*this, writeData(_, Lt(kEncodedDataSize), false))
114 .Times(AtLeast(1));
115 EXPECT_CALL(*this, writeData(_, kEncodedDataSize, false))
116 .Times(1)
117 .WillOnce(RunClosure(quit_closure));
118
119 OnVideoFrameForTesting(video_frame);
120 run_loop.Run();
121 }
122
123 {
124 base::RunLoop run_loop;
125 base::Closure quit_closure = run_loop.QuitClosure();
126 // The second time around writeData() is called a number of times to write
127 // the WebM frame header, and then is pinged with the encoded data.
128 const size_t kSecondEncodedDataSize = 32;
129 EXPECT_CALL(*this, writeData(_, Lt(kSecondEncodedDataSize), false))
130 .Times(AtLeast(1));
131 EXPECT_CALL(*this, writeData(_, kSecondEncodedDataSize, false))
132 .Times(1)
133 .WillOnce(RunClosure(quit_closure));
134
135 OnVideoFrameForTesting(video_frame);
136 run_loop.Run();
137 }
138
139 media_recorder_handler_->stop();
140
141 // Expect a last call on destruction, with size 0 and |lastInSlice| true.
142 EXPECT_CALL(*this, writeData(nullptr, 0, true)).Times(1);
143 media_recorder_handler_.reset();
144 }
145
146 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698