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

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

Issue 2721113002: getUserMedia: handle the device starting status report. (Closed)
Patch Set: Created 3 years, 9 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
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/shared_memory.h" 8 #include "base/memory/shared_memory.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "content/child/child_process.h" 10 #include "content/child/child_process.h"
11 #include "content/common/video_capture.mojom.h" 11 #include "content/common/video_capture.mojom.h"
12 #include "content/renderer/media/video_capture_impl.h" 12 #include "content/renderer/media/video_capture_impl.h"
13 #include "mojo/public/cpp/system/platform_handle.h" 13 #include "mojo/public/cpp/system/platform_handle.h"
14 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 using ::testing::_; 17 using ::testing::_;
18 using ::testing::InSequence;
18 using ::testing::Invoke; 19 using ::testing::Invoke;
19 using ::testing::InvokeWithoutArgs; 20 using ::testing::InvokeWithoutArgs;
20 using ::testing::SaveArg; 21 using ::testing::SaveArg;
21 using ::testing::WithArgs; 22 using ::testing::WithArgs;
22 23
23 namespace content { 24 namespace content {
24 25
25 const int kSessionId = 11; 26 const int kSessionId = 11;
26 27
27 void RunEmptyFormatsCallback(const VideoCaptureDeviceFormatsCB& callback) { 28 void RunEmptyFormatsCallback(const VideoCaptureDeviceFormatsCB& callback) {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 media::VideoCaptureParams params_small_; 170 media::VideoCaptureParams params_small_;
170 media::VideoCaptureParams params_large_; 171 media::VideoCaptureParams params_large_;
171 172
172 private: 173 private:
173 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); 174 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
174 }; 175 };
175 176
176 TEST_F(VideoCaptureImplTest, Simple) { 177 TEST_F(VideoCaptureImplTest, Simple) {
177 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 178 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
178 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); 179 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
179 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); 180 // Simulate the device started event here (and for all tests below), since we
181 // can't call the |onStateChange| through |mojom::VideoCaptureObserverPtr| in
182 // |MockMojoVideoCaptureHost::Start()| by far.
chfremer 2017/03/02 17:56:10 I understand the added code in the EXPECT_CALL, an
braveyao 2017/03/03 17:53:12 Done.
183 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_))
184 .WillOnce(InvokeWithoutArgs([this]() {
185 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
186 }));
chfremer 2017/03/02 17:56:10 Instead of adding this to all the test cases, you
braveyao 2017/03/03 17:53:12 Done. Thanks a lot for the suggestion!
180 EXPECT_CALL(mock_video_capture_host_, Stop(_)); 187 EXPECT_CALL(mock_video_capture_host_, Stop(_));
181 188
182 StartCapture(0, params_small_); 189 StartCapture(0, params_small_);
183 StopCapture(0); 190 StopCapture(0);
184 } 191 }
185 192
186 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { 193 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
187 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); 194 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
188 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); 195 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
189 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); 196 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_))
197 .WillOnce(InvokeWithoutArgs([this]() {
198 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
199 }));
190 EXPECT_CALL(mock_video_capture_host_, Stop(_)); 200 EXPECT_CALL(mock_video_capture_host_, Stop(_));
191 201
192 StartCapture(0, params_small_); 202 StartCapture(0, params_small_);
193 StopCapture(0); 203 StopCapture(0);
194 StartCapture(1, params_small_); 204 StartCapture(1, params_small_);
195 StopCapture(1); 205 StopCapture(1);
196 } 206 }
197 207
198 TEST_F(VideoCaptureImplTest, LargeAndSmall) { 208 TEST_F(VideoCaptureImplTest, LargeAndSmall) {
199 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); 209 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
200 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); 210 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
201 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); 211 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_))
212 .WillOnce(InvokeWithoutArgs([this]() {
213 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
214 }));
202 EXPECT_CALL(mock_video_capture_host_, Stop(_)); 215 EXPECT_CALL(mock_video_capture_host_, Stop(_));
203 216
204 StartCapture(0, params_large_); 217 StartCapture(0, params_large_);
205 StopCapture(0); 218 StopCapture(0);
206 StartCapture(1, params_small_); 219 StartCapture(1, params_small_);
207 StopCapture(1); 220 StopCapture(1);
208 } 221 }
209 222
210 TEST_F(VideoCaptureImplTest, SmallAndLarge) { 223 TEST_F(VideoCaptureImplTest, SmallAndLarge) {
211 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); 224 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
212 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); 225 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
213 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); 226 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_))
227 .WillOnce(InvokeWithoutArgs([this]() {
228 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
229 }));
214 EXPECT_CALL(mock_video_capture_host_, Stop(_)); 230 EXPECT_CALL(mock_video_capture_host_, Stop(_));
215 231
216 StartCapture(0, params_small_); 232 StartCapture(0, params_small_);
217 StopCapture(0); 233 StopCapture(0);
218 StartCapture(1, params_large_); 234 StartCapture(1, params_large_);
219 StopCapture(1); 235 StopCapture(1);
220 } 236 }
221 237
222 // Checks that a request to GetDeviceSupportedFormats() ends up eventually in 238 // Checks that a request to GetDeviceSupportedFormats() ends up eventually in
223 // the provided callback. 239 // the provided callback.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 const int kBufferId = 11; 271 const int kBufferId = 11;
256 272
257 base::SharedMemory shm; 273 base::SharedMemory shm;
258 const size_t frame_size = media::VideoFrame::AllocationSize( 274 const size_t frame_size = media::VideoFrame::AllocationSize(
259 media::PIXEL_FORMAT_I420, params_small_.requested_format.frame_size); 275 media::PIXEL_FORMAT_I420, params_small_.requested_format.frame_size);
260 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); 276 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size));
261 277
262 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 278 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
263 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); 279 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
264 EXPECT_CALL(*this, OnFrameReady(_, _)); 280 EXPECT_CALL(*this, OnFrameReady(_, _));
265 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); 281 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_))
282 .WillOnce(InvokeWithoutArgs([this]() {
283 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
284 }));
266 EXPECT_CALL(mock_video_capture_host_, Stop(_)); 285 EXPECT_CALL(mock_video_capture_host_, Stop(_));
267 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _)) 286 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _))
268 .Times(0); 287 .Times(0);
269 288
270 StartCapture(0, params_small_); 289 StartCapture(0, params_small_);
271 SimulateOnBufferCreated(kBufferId, shm); 290 SimulateOnBufferCreated(kBufferId, shm);
272 SimulateBufferReceived(kBufferId, params_small_.requested_format.frame_size); 291 SimulateBufferReceived(kBufferId, params_small_.requested_format.frame_size);
273 StopCapture(0); 292 StopCapture(0);
274 SimulateBufferDestroyed(kBufferId); 293 SimulateBufferDestroyed(kBufferId);
275 294
276 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 0); 295 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 0);
277 } 296 }
278 297
279 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { 298 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) {
280 const int kBufferId = 12; 299 const int kBufferId = 12;
281 300
282 base::SharedMemory shm; 301 base::SharedMemory shm;
283 const size_t frame_size = media::VideoFrame::AllocationSize( 302 const size_t frame_size = media::VideoFrame::AllocationSize(
284 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); 303 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size);
285 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); 304 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size));
286 305
287 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 306 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
288 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); 307 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
289 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); 308 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0);
290 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); 309 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_))
310 .WillOnce(InvokeWithoutArgs([this]() {
311 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
312 }));
291 EXPECT_CALL(mock_video_capture_host_, Stop(_)); 313 EXPECT_CALL(mock_video_capture_host_, Stop(_));
292 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _)); 314 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _));
293 315
294 StartCapture(0, params_large_); 316 StartCapture(0, params_large_);
295 SimulateOnBufferCreated(kBufferId, shm); 317 SimulateOnBufferCreated(kBufferId, shm);
296 StopCapture(0); 318 StopCapture(0);
297 // A buffer received after StopCapture() triggers an instant ReleaseBuffer(). 319 // A buffer received after StopCapture() triggers an instant ReleaseBuffer().
298 SimulateBufferReceived(kBufferId, params_large_.requested_format.frame_size); 320 SimulateBufferReceived(kBufferId, params_large_.requested_format.frame_size);
299 SimulateBufferDestroyed(kBufferId); 321 SimulateBufferDestroyed(kBufferId);
300 322
301 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1); 323 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1);
302 } 324 }
303 325
304 TEST_F(VideoCaptureImplTest, AlreadyStarted) { 326 TEST_F(VideoCaptureImplTest, AlreadyStarted) {
305 media::VideoCaptureParams params = {}; 327 media::VideoCaptureParams params = {};
306 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); 328 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
307 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); 329 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
308 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, _)) 330 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_))
309 .WillOnce(SaveArg<2>(&params)); 331 .WillOnce(DoAll(InvokeWithoutArgs([this]() {
332 video_capture_impl_->OnStateChanged(
333 mojom::VideoCaptureState::STARTED);
334 }),
335 SaveArg<2>(&params)));
310 EXPECT_CALL(mock_video_capture_host_, Stop(_)); 336 EXPECT_CALL(mock_video_capture_host_, Stop(_));
311 337
312 StartCapture(0, params_small_); 338 StartCapture(0, params_small_);
313 StartCapture(1, params_large_); 339 StartCapture(1, params_large_);
314 StopCapture(0); 340 StopCapture(0);
315 StopCapture(1); 341 StopCapture(1);
316 DCHECK(params.requested_format == params_small_.requested_format); 342 DCHECK(params.requested_format == params_small_.requested_format);
317 } 343 }
318 344
319 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { 345 TEST_F(VideoCaptureImplTest, EndedBeforeStop) {
320 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 346 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
321 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); 347 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
322 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); 348 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_))
349 .WillOnce(InvokeWithoutArgs([this]() {
350 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
351 }));
323 352
324 StartCapture(0, params_small_); 353 StartCapture(0, params_small_);
325 354
326 OnStateChanged(mojom::VideoCaptureState::ENDED); 355 OnStateChanged(mojom::VideoCaptureState::ENDED);
327 356
328 StopCapture(0); 357 StopCapture(0);
329 } 358 }
330 359
331 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { 360 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) {
332 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 361 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
333 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); 362 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR));
334 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); 363 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_))
364 .WillOnce(InvokeWithoutArgs([this]() {
365 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
366 }));
335 367
336 StartCapture(0, params_small_); 368 StartCapture(0, params_small_);
337 369
338 OnStateChanged(mojom::VideoCaptureState::FAILED); 370 OnStateChanged(mojom::VideoCaptureState::FAILED);
339 371
340 StopCapture(0); 372 StopCapture(0);
341 } 373 }
342 374
375 TEST_F(VideoCaptureImplTest, BufferReceivedBeforeOnStarted) {
chfremer 2017/03/02 17:56:10 Great to see a test case for this! I can think of
braveyao 2017/03/03 17:53:12 Done.
376 const int kBufferId = 16;
377
378 base::SharedMemory shm;
379 const size_t frame_size = media::VideoFrame::AllocationSize(
380 media::PIXEL_FORMAT_I420, params_small_.requested_format.frame_size);
381 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size));
382
383 InSequence s;
384 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_));
385 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _));
chfremer 2017/03/02 17:56:10 Are these two EXPECT_CALL relevant for this test c
braveyao 2017/03/03 17:53:12 Kinda. I want to verify that the frame is receive
386 StartCapture(0, params_small_);
387 SimulateOnBufferCreated(kBufferId, shm);
388 SimulateBufferReceived(kBufferId, params_small_.requested_format.frame_size);
389
390 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
391 EXPECT_CALL(mock_video_capture_host_, RequestRefreshFrame(_));
392 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED);
393
394 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
395 EXPECT_CALL(mock_video_capture_host_, Stop(_));
396 StopCapture(0);
397 SimulateBufferDestroyed(kBufferId);
398
399 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1);
chfremer 2017/03/02 17:56:10 Is all of the shutdown logic starting at line 394
braveyao 2017/03/03 17:53:12 StopCapture and the above two lines are needed for
400 }
401
343 } // namespace content 402 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698