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

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

Issue 242013002: Refactor video capturing code in the render process (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merged Created 6 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "content/child/child_process.h" 6 #include "content/child/child_process.h"
8 #include "content/common/media/video_capture_messages.h" 7 #include "content/common/media/video_capture_messages.h"
9 #include "content/renderer/media/video_capture_impl.h" 8 #include "content/renderer/media/video_capture_impl.h"
10 #include "media/base/bind_to_current_loop.h" 9 #include "media/base/bind_to_current_loop.h"
11 #include "media/video/capture/mock_video_capture_event_handler.h"
12 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
14 12
15 using ::testing::_; 13 using ::testing::_;
16 using ::testing::AtLeast; 14 using ::testing::AtLeast;
17 using ::testing::InvokeWithoutArgs; 15 using ::testing::InvokeWithoutArgs;
18 using ::testing::Return; 16 using ::testing::Return;
19 using ::testing::SaveArg; 17 using ::testing::SaveArg;
20 using media::MockVideoCaptureEventHandler;
21 18
22 namespace content { 19 namespace content {
23 20
24 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { 21 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
25 public: 22 public:
26 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} 23 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
27 24
28 // Filter implementation. 25 // Filter implementation.
29 MOCK_METHOD1(Send, bool(IPC::Message* message)); 26 MOCK_METHOD1(Send, bool(IPC::Message* message));
30 27
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); 100 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
104 101
105 params_large_.requested_format = media::VideoCaptureFormat( 102 params_large_.requested_format = media::VideoCaptureFormat(
106 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); 103 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
107 104
108 child_process_.reset(new ChildProcess()); 105 child_process_.reset(new ChildProcess());
109 106
110 message_filter_ = new MockVideoCaptureMessageFilter; 107 message_filter_ = new MockVideoCaptureMessageFilter;
111 session_id_ = 1; 108 session_id_ = 1;
112 109
113 video_capture_impl_ = new MockVideoCaptureImpl( 110 video_capture_impl_.reset(new MockVideoCaptureImpl(
114 session_id_, message_filter_.get()); 111 session_id_, message_filter_.get()));
115 112
116 video_capture_impl_->device_id_ = 2; 113 video_capture_impl_->device_id_ = 2;
117 } 114 }
118 115
119 virtual ~VideoCaptureImplTest() { 116 virtual ~VideoCaptureImplTest() {
120 delete video_capture_impl_;
121 } 117 }
122 118
123 protected: 119 protected:
120 MOCK_METHOD3(OnFrameReady,
121 void(const scoped_refptr<media::VideoFrame>&,
122 const media::VideoCaptureFormat&,
123 const base::TimeTicks&));
124 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState));
125 MOCK_METHOD1(OnDeviceFormatsInUse,
126 void(const media::VideoCaptureFormats&));
127 MOCK_METHOD1(OnDeviceSupportedFormats,
128 void(const media::VideoCaptureFormats&));
129
130 void Init() {
131 video_capture_impl_->Init();
132 }
133
134 void StartCapture(int client_id,
135 const media::VideoCaptureParams& params) {
136 video_capture_impl_->StartCapture(
137 client_id, params,
138 base::Bind(&VideoCaptureImplTest::OnStateUpdate,
139 base::Unretained(this)),
140 base::Bind(&VideoCaptureImplTest::OnFrameReady,
141 base::Unretained(this)));
142 }
143
144 void StopCapture(int client_id) {
145 video_capture_impl_->StopCapture(client_id);
146 }
147
148 void DeInit() {
149 video_capture_impl_->DeInit();
150 }
151
152 void GetDeviceSupportedFormats() {
153 const base::Callback<void(const media::VideoCaptureFormats&)>
154 callback = base::Bind(
155 &VideoCaptureImplTest::OnDeviceSupportedFormats,
156 base::Unretained(this));
157 video_capture_impl_->GetDeviceSupportedFormats(callback);
158 }
159
160 void GetDeviceFormatsInUse() {
161 const base::Callback<void(const media::VideoCaptureFormats&)>
162 callback = base::Bind(
163 &VideoCaptureImplTest::OnDeviceFormatsInUse,
164 base::Unretained(this));
165 video_capture_impl_->GetDeviceFormatsInUse(callback);
166 }
167
124 base::MessageLoop message_loop_; 168 base::MessageLoop message_loop_;
125 base::RunLoop run_loop_;
126 scoped_ptr<ChildProcess> child_process_; 169 scoped_ptr<ChildProcess> child_process_;
127 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; 170 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
128 media::VideoCaptureSessionId session_id_; 171 media::VideoCaptureSessionId session_id_;
129 MockVideoCaptureImpl* video_capture_impl_; 172 scoped_ptr<MockVideoCaptureImpl> video_capture_impl_;
130 media::VideoCaptureParams params_small_; 173 media::VideoCaptureParams params_small_;
131 media::VideoCaptureParams params_large_; 174 media::VideoCaptureParams params_large_;
132 175
133 private: 176 private:
134 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); 177 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
135 }; 178 };
136 179
137 TEST_F(VideoCaptureImplTest, Simple) { 180 TEST_F(VideoCaptureImplTest, Simple) {
138 // Execute SetCapture() and StopCapture() for one client. 181 // Execute SetCapture() and StopCapture() for one client.
139 scoped_ptr<MockVideoCaptureEventHandler> client( 182 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
140 new MockVideoCaptureEventHandler); 183 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
141 184
142 EXPECT_CALL(*client, OnStarted(_)); 185 Init();
143 EXPECT_CALL(*client, OnStopped(_)); 186 StartCapture(0, params_small_);
144 EXPECT_CALL(*client, OnRemoved(_)); 187 StopCapture(0);
145 188 DeInit();
146 video_capture_impl_->StartCapture(client.get(), params_small_);
147 video_capture_impl_->StopCapture(client.get());
148 video_capture_impl_->DeInit(
149 media::BindToCurrentLoop(run_loop_.QuitClosure()));
150 run_loop_.Run();
151 } 189 }
152 190
153 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { 191 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
154 // Execute SetCapture() and StopCapture() for 2 clients in sequence. 192 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
155 scoped_ptr<MockVideoCaptureEventHandler> client1( 193 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
156 new MockVideoCaptureEventHandler);
157 scoped_ptr<MockVideoCaptureEventHandler> client2(
158 new MockVideoCaptureEventHandler);
159 194
160 EXPECT_CALL(*client1, OnStarted(_)); 195 Init();
161 EXPECT_CALL(*client1, OnStopped(_)); 196 StartCapture(0, params_small_);
162 EXPECT_CALL(*client1, OnRemoved(_)); 197 StopCapture(0);
163 EXPECT_CALL(*client2, OnStarted(_)); 198 StartCapture(1, params_small_);
164 EXPECT_CALL(*client2, OnStopped(_)); 199 StopCapture(1);
165 EXPECT_CALL(*client2, OnRemoved(_)); 200 DeInit();
166
167 video_capture_impl_->StartCapture(client1.get(), params_small_);
168 video_capture_impl_->StopCapture(client1.get());
169 video_capture_impl_->StartCapture(client2.get(), params_small_);
170 video_capture_impl_->StopCapture(client2.get());
171 video_capture_impl_->DeInit(
172 media::BindToCurrentLoop(run_loop_.QuitClosure()));
173 run_loop_.Run();
174 } 201 }
175 202
176 TEST_F(VideoCaptureImplTest, LargeAndSmall) { 203 TEST_F(VideoCaptureImplTest, LargeAndSmall) {
177 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 204 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
178 // The large client starts first and stops first. 205 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
179 scoped_ptr<MockVideoCaptureEventHandler> client_small(
180 new MockVideoCaptureEventHandler);
181 scoped_ptr<MockVideoCaptureEventHandler> client_large(
182 new MockVideoCaptureEventHandler);
183 206
184 EXPECT_CALL(*client_large, OnStarted(_)); 207 Init();
185 EXPECT_CALL(*client_small, OnStarted(_)); 208 StartCapture(0, params_large_);
186 EXPECT_CALL(*client_large, OnStopped(_)); 209 StopCapture(0);
187 EXPECT_CALL(*client_large, OnRemoved(_)); 210 StartCapture(1, params_small_);
188 EXPECT_CALL(*client_small, OnStopped(_)); 211 StopCapture(1);
189 EXPECT_CALL(*client_small, OnRemoved(_)); 212 DeInit();
190
191 video_capture_impl_->StartCapture(client_large.get(), params_large_);
192 video_capture_impl_->StartCapture(client_small.get(), params_small_);
193 video_capture_impl_->StopCapture(client_large.get());
194 video_capture_impl_->StopCapture(client_small.get());
195 video_capture_impl_->DeInit(
196 media::BindToCurrentLoop(run_loop_.QuitClosure()));
197 run_loop_.Run();
198 } 213 }
199 214
200 TEST_F(VideoCaptureImplTest, SmallAndLarge) { 215 TEST_F(VideoCaptureImplTest, SmallAndLarge) {
201 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 216 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
202 // The small client starts first and stops first. 217 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
203 scoped_ptr<MockVideoCaptureEventHandler> client_small(
204 new MockVideoCaptureEventHandler);
205 scoped_ptr<MockVideoCaptureEventHandler> client_large(
206 new MockVideoCaptureEventHandler);
207 218
208 EXPECT_CALL(*client_small, OnStarted(_)); 219 Init();
209 EXPECT_CALL(*client_large, OnStarted(_)); 220 StartCapture(0, params_small_);
210 EXPECT_CALL(*client_small, OnStopped(_)); 221 StopCapture(0);
211 EXPECT_CALL(*client_small, OnRemoved(_)); 222 StartCapture(1, params_large_);
212 EXPECT_CALL(*client_large, OnStopped(_)); 223 StopCapture(1);
213 EXPECT_CALL(*client_large, OnRemoved(_)); 224 DeInit();
214
215 video_capture_impl_->StartCapture(client_small.get(), params_small_);
216 video_capture_impl_->StartCapture(client_large.get(), params_large_);
217 video_capture_impl_->StopCapture(client_small.get());
218 video_capture_impl_->StopCapture(client_large.get());
219 video_capture_impl_->DeInit(
220 media::BindToCurrentLoop(run_loop_.QuitClosure()));
221 run_loop_.Run();
222 } 225 }
223 226
224 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the 227 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the
225 // provided callback. 228 // provided callback.
226 TEST_F(VideoCaptureImplTest, GetDeviceFormats) { 229 TEST_F(VideoCaptureImplTest, GetDeviceFormats) {
227 scoped_ptr<MockVideoCaptureEventHandler> client( 230 EXPECT_CALL(*this, OnDeviceSupportedFormats(_));
228 new MockVideoCaptureEventHandler);
229 231
230 EXPECT_CALL(*client, OnDeviceSupportedFormatsEnumerated(_)); 232 Init();
231 233 GetDeviceSupportedFormats();
232 const base::Callback<void(const media::VideoCaptureFormats&)> 234 DeInit();
233 callback = base::Bind(
234 &MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
235 base::Unretained(client.get()));
236 video_capture_impl_->GetDeviceSupportedFormats(callback);
237 video_capture_impl_->DeInit(
238 media::BindToCurrentLoop(run_loop_.QuitClosure()));
239 run_loop_.Run();
240 } 235 }
241 236
242 // Check that two requests to GetDeviceSupportedFormats() end up eventually 237 // Check that two requests to GetDeviceSupportedFormats() end up eventually
243 // calling the provided callbacks. 238 // calling the provided callbacks.
244 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) { 239 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) {
245 scoped_ptr<MockVideoCaptureEventHandler> client1( 240 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)).Times(2);
246 new MockVideoCaptureEventHandler);
247 scoped_ptr<MockVideoCaptureEventHandler> client2(
248 new MockVideoCaptureEventHandler);
249 241
250 EXPECT_CALL(*client1, OnDeviceSupportedFormatsEnumerated(_)); 242 Init();
251 EXPECT_CALL(*client2, OnDeviceSupportedFormatsEnumerated(_)); 243 GetDeviceSupportedFormats();
252 244 GetDeviceSupportedFormats();
253 const base::Callback<void(const media::VideoCaptureFormats&)> 245 DeInit();
254 callback1 = base::Bind(
255 &MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
256 base::Unretained(client1.get()));
257 const base::Callback<void(const media::VideoCaptureFormats&)>
258 callback2 = base::Bind(
259 &MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
260 base::Unretained(client2.get()));
261
262 video_capture_impl_->GetDeviceSupportedFormats(callback1);
263 video_capture_impl_->GetDeviceSupportedFormats(callback2);
264 video_capture_impl_->DeInit(
265 media::BindToCurrentLoop(run_loop_.QuitClosure()));
266 run_loop_.Run();
267 } 246 }
268 247
269 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the 248 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
270 // provided callback. 249 // provided callback.
271 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { 250 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) {
272 scoped_ptr<MockVideoCaptureEventHandler> client( 251 EXPECT_CALL(*this, OnDeviceFormatsInUse(_));
273 new MockVideoCaptureEventHandler);
274 252
275 media::VideoCaptureFormats formats_in_use; 253 Init();
276 EXPECT_CALL(*client, OnDeviceFormatsInUseReceived(_)) 254 GetDeviceFormatsInUse();
277 .WillOnce(SaveArg<0>(&formats_in_use)); 255 DeInit();
278
279 const base::Callback<void(const media::VideoCaptureFormats&)> callback =
280 base::Bind(&MockVideoCaptureEventHandler::OnDeviceFormatsInUseReceived,
281 base::Unretained(client.get()));
282 video_capture_impl_->GetDeviceFormatsInUse(callback);
283 video_capture_impl_->DeInit(
284 media::BindToCurrentLoop(run_loop_.QuitClosure()));
285 run_loop_.Run();
286
287 EXPECT_TRUE(formats_in_use.empty());
288 } 256 }
289 257
290 } // namespace content 258 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698