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

Side by Side Diff: media/capture/video/fake_video_capture_device_unittest.cc

Issue 2619503003: Split FakeVideoCaptureDevice into classes with single responsibility (Closed)
Patch Set: mcasas@ suggestions Created 3 years, 10 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
« no previous file with comments | « media/capture/video/fake_video_capture_device_factory.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "media/capture/video/fake_video_capture_device.h" 5 #include "media/capture/video/fake_video_capture_device.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 base::TimeTicks reference_time, 114 base::TimeTicks reference_time,
115 base::TimeDelta timestamp, 115 base::TimeDelta timestamp,
116 int frame_feedback_id) override { 116 int frame_feedback_id) override {
117 frame_cb_.Run(format); 117 frame_cb_.Run(format);
118 } 118 }
119 // Virtual methods for capturing using Client's Buffers. 119 // Virtual methods for capturing using Client's Buffers.
120 Buffer ReserveOutputBuffer(const gfx::Size& dimensions, 120 Buffer ReserveOutputBuffer(const gfx::Size& dimensions,
121 media::VideoPixelFormat format, 121 media::VideoPixelFormat format,
122 media::VideoPixelStorage storage, 122 media::VideoPixelStorage storage,
123 int frame_feedback_id) override { 123 int frame_feedback_id) override {
124 EXPECT_TRUE((format == media::PIXEL_FORMAT_ARGB && 124 EXPECT_EQ(media::PIXEL_STORAGE_CPU, storage);
125 storage == media::PIXEL_STORAGE_CPU));
126 EXPECT_GT(dimensions.GetArea(), 0); 125 EXPECT_GT(dimensions.GetArea(), 0);
127 const VideoCaptureFormat frame_format(dimensions, 0.0, format); 126 const VideoCaptureFormat frame_format(dimensions, 0.0, format);
128 return CreateStubBuffer(0, frame_format.ImageAllocationSize()); 127 return CreateStubBuffer(0, frame_format.ImageAllocationSize());
129 } 128 }
130 void OnIncomingCapturedBuffer(Buffer buffer, 129 void OnIncomingCapturedBuffer(Buffer buffer,
131 const VideoCaptureFormat& format, 130 const VideoCaptureFormat& format,
132 base::TimeTicks reference_time, 131 base::TimeTicks reference_time,
133 base::TimeDelta timestamp) override { 132 base::TimeDelta timestamp) override {
134 frame_cb_.Run(format); 133 frame_cb_.Run(format);
135 } 134 }
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; 259 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_;
261 const scoped_refptr<ImageCaptureClient> image_capture_client_; 260 const scoped_refptr<ImageCaptureClient> image_capture_client_;
262 VideoCaptureFormat last_format_; 261 VideoCaptureFormat last_format_;
263 const std::unique_ptr<VideoCaptureDeviceFactory> 262 const std::unique_ptr<VideoCaptureDeviceFactory>
264 video_capture_device_factory_; 263 video_capture_device_factory_;
265 }; 264 };
266 265
267 class FakeVideoCaptureDeviceTest 266 class FakeVideoCaptureDeviceTest
268 : public FakeVideoCaptureDeviceBase, 267 : public FakeVideoCaptureDeviceBase,
269 public ::testing::WithParamInterface< 268 public ::testing::WithParamInterface<
270 ::testing::tuple<FakeVideoCaptureDevice::BufferOwnership, float>> {}; 269 ::testing::tuple<VideoPixelFormat,
270 FakeVideoCaptureDeviceMaker::DeliveryMode,
271 float>> {};
271 272
272 struct CommandLineTestData { 273 // Tests that a frame is delivered with the expected settings.
273 // Command line argument 274 // Sweeps through a fixed set of requested/expected resolutions.
274 std::string argument;
275 // Expected values
276 float fps;
277 size_t device_count;
278 };
279
280 class FakeVideoCaptureDeviceCommandLineTest
281 : public FakeVideoCaptureDeviceBase,
282 public ::testing::WithParamInterface<CommandLineTestData> {};
283
284 TEST_P(FakeVideoCaptureDeviceTest, CaptureUsing) { 275 TEST_P(FakeVideoCaptureDeviceTest, CaptureUsing) {
285 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( 276 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors(
286 EnumerateDevices()); 277 EnumerateDevices());
287 ASSERT_FALSE(descriptors->empty()); 278 ASSERT_FALSE(descriptors->empty());
288 279
289 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( 280 auto device = FakeVideoCaptureDeviceMaker::MakeInstance(
290 testing::get<0>(GetParam()), testing::get<1>(GetParam()))); 281 testing::get<0>(GetParam()), testing::get<1>(GetParam()),
282 testing::get<2>(GetParam()));
291 ASSERT_TRUE(device); 283 ASSERT_TRUE(device);
292 284
293 VideoCaptureParams capture_params; 285 // First: Requested, Second: Expected
294 capture_params.requested_format.frame_size.SetSize(640, 480); 286 std::vector<std::pair<gfx::Size, gfx::Size>> resolutions_to_test;
295 capture_params.requested_format.frame_rate = testing::get<1>(GetParam()); 287 resolutions_to_test.emplace_back(gfx::Size(640, 480), gfx::Size(640, 480));
296 device->AllocateAndStart(capture_params, std::move(client_)); 288 resolutions_to_test.emplace_back(gfx::Size(104, 105), gfx::Size(320, 240));
289 resolutions_to_test.emplace_back(gfx::Size(0, 0), gfx::Size(96, 96));
290 resolutions_to_test.emplace_back(gfx::Size(0, 720), gfx::Size(96, 96));
291 resolutions_to_test.emplace_back(gfx::Size(1920, 1080),
292 gfx::Size(1920, 1080));
297 293
298 WaitForCapturedFrame(); 294 for (const auto& resolution : resolutions_to_test) {
299 EXPECT_EQ(last_format().frame_size.width(), 640); 295 auto client = CreateClient();
300 EXPECT_EQ(last_format().frame_size.height(), 480); 296 EXPECT_CALL(*client, OnError(_, _)).Times(0);
301 EXPECT_EQ(last_format().frame_rate, testing::get<1>(GetParam())); 297
302 device->StopAndDeAllocate(); 298 VideoCaptureParams capture_params;
299 capture_params.requested_format.frame_size = resolution.first;
300 capture_params.requested_format.frame_rate = testing::get<2>(GetParam());
301 device->AllocateAndStart(capture_params, std::move(client));
302
303 WaitForCapturedFrame();
304 EXPECT_EQ(resolution.second.width(), last_format().frame_size.width());
305 EXPECT_EQ(resolution.second.height(), last_format().frame_size.height());
306 EXPECT_EQ(last_format().pixel_format, testing::get<0>(GetParam()));
307 EXPECT_EQ(last_format().frame_rate, testing::get<2>(GetParam()));
308 device->StopAndDeAllocate();
309 }
303 } 310 }
304 311
305 INSTANTIATE_TEST_CASE_P( 312 INSTANTIATE_TEST_CASE_P(
306 , 313 ,
307 FakeVideoCaptureDeviceTest, 314 FakeVideoCaptureDeviceTest,
308 Combine(Values(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 315 Combine(Values(PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, PIXEL_FORMAT_ARGB),
309 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS), 316 Values(FakeVideoCaptureDeviceMaker::DeliveryMode::
317 USE_DEVICE_INTERNAL_BUFFERS,
318 FakeVideoCaptureDeviceMaker::DeliveryMode::
319 USE_CLIENT_PROVIDED_BUFFERS),
310 Values(20, 29.97, 30, 50, 60))); 320 Values(20, 29.97, 30, 50, 60)));
311 321
312 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { 322 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) {
313 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 323 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
314 switches::kUseFakeDeviceForMediaStream, "device-count=3"); 324 switches::kUseFakeDeviceForMediaStream, "device-count=3");
315 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( 325 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors(
316 EnumerateDevices()); 326 EnumerateDevices());
317 ASSERT_EQ(3u, descriptors->size()); 327 ASSERT_EQ(3u, descriptors->size());
318 328
319 for (const auto& descriptors_iterator : *descriptors) { 329 for (const auto& descriptors_iterator : *descriptors) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 const VideoCaptureDeviceDescriptor& depth_device = descriptors->at(1); 367 const VideoCaptureDeviceDescriptor& depth_device = descriptors->at(1);
358 EXPECT_EQ("/dev/video1", depth_device.device_id); 368 EXPECT_EQ("/dev/video1", depth_device.device_id);
359 ASSERT_TRUE(depth_device.camera_calibration); 369 ASSERT_TRUE(depth_device.camera_calibration);
360 EXPECT_EQ(135.0, depth_device.camera_calibration->focal_length_x); 370 EXPECT_EQ(135.0, depth_device.camera_calibration->focal_length_x);
361 EXPECT_EQ(135.6, depth_device.camera_calibration->focal_length_y); 371 EXPECT_EQ(135.6, depth_device.camera_calibration->focal_length_y);
362 EXPECT_EQ(0.0, depth_device.camera_calibration->depth_near); 372 EXPECT_EQ(0.0, depth_device.camera_calibration->depth_near);
363 EXPECT_EQ(65.535, depth_device.camera_calibration->depth_far); 373 EXPECT_EQ(65.535, depth_device.camera_calibration->depth_far);
364 } 374 }
365 375
366 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { 376 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) {
367 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( 377 auto device = FakeVideoCaptureDeviceMaker::MakeInstance(
368 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); 378 PIXEL_FORMAT_I420,
379 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_DEVICE_INTERNAL_BUFFERS,
380 30.0);
369 ASSERT_TRUE(device); 381 ASSERT_TRUE(device);
370 382
371 VideoCaptureParams capture_params; 383 VideoCaptureParams capture_params;
372 capture_params.requested_format.frame_size.SetSize(640, 480); 384 capture_params.requested_format.frame_size.SetSize(640, 480);
373 capture_params.requested_format.frame_rate = 30.0; 385 capture_params.requested_format.frame_rate = 30.0;
374 device->AllocateAndStart(capture_params, std::move(client_)); 386 device->AllocateAndStart(capture_params, std::move(client_));
375 387
376 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( 388 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback(
377 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, 389 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities,
378 image_capture_client_), 390 image_capture_client_),
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 device->GetPhotoCapabilities(std::move(scoped_get_callback2)); 478 device->GetPhotoCapabilities(std::move(scoped_get_callback2));
467 run_loop_.reset(new base::RunLoop()); 479 run_loop_.reset(new base::RunLoop());
468 run_loop_->Run(); 480 run_loop_->Run();
469 EXPECT_EQ(max_zoom_value, 481 EXPECT_EQ(max_zoom_value,
470 image_capture_client_->capabilities()->zoom->current); 482 image_capture_client_->capabilities()->zoom->current);
471 483
472 device->StopAndDeAllocate(); 484 device->StopAndDeAllocate();
473 } 485 }
474 486
475 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { 487 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) {
476 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( 488 auto device = FakeVideoCaptureDeviceMaker::MakeInstance(
477 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); 489 PIXEL_FORMAT_I420,
490 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_DEVICE_INTERNAL_BUFFERS,
491 30.0);
478 ASSERT_TRUE(device); 492 ASSERT_TRUE(device);
479 493
480 VideoCaptureParams capture_params; 494 VideoCaptureParams capture_params;
481 capture_params.requested_format.frame_size.SetSize(640, 480); 495 capture_params.requested_format.frame_size.SetSize(640, 480);
482 capture_params.requested_format.frame_rate = 30.0; 496 capture_params.requested_format.frame_rate = 30.0;
483 device->AllocateAndStart(capture_params, std::move(client_)); 497 device->AllocateAndStart(capture_params, std::move(client_));
484 498
485 VideoCaptureDevice::TakePhotoCallback scoped_callback( 499 VideoCaptureDevice::TakePhotoCallback scoped_callback(
486 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), 500 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_),
487 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, 501 base::Bind(&ImageCaptureClient::OnTakePhotoFailure,
488 image_capture_client_)); 502 image_capture_client_));
489 503
490 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); 504 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1);
491 device->TakePhoto(std::move(scoped_callback)); 505 device->TakePhoto(std::move(scoped_callback));
492 506
493 run_loop_.reset(new base::RunLoop()); 507 run_loop_.reset(new base::RunLoop());
494 run_loop_->Run(); 508 run_loop_->Run();
495 device->StopAndDeAllocate(); 509 device->StopAndDeAllocate();
496 } 510 }
497 511
498 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRateAndDeviceCount) { 512 struct CommandLineTestData {
513 std::string switch_value_string;
514 float expected_fps;
515 size_t expected_device_count;
516 std::vector<VideoPixelFormat> expected_pixel_formats;
517 };
518
519 class FakeVideoCaptureDeviceFactoryTest
520 : public FakeVideoCaptureDeviceBase,
521 public ::testing::WithParamInterface<CommandLineTestData> {};
522
523 // Tests that the FakeVideoCaptureDeviceFactory delivers the expected number
524 // of devices and formats when being configured using command-line switches.
525 TEST_P(FakeVideoCaptureDeviceFactoryTest, FrameRateAndDeviceCount) {
499 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 526 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
500 switches::kUseFakeDeviceForMediaStream, GetParam().argument); 527 switches::kUseFakeDeviceForMediaStream, GetParam().switch_value_string);
501 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( 528 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors(
502 EnumerateDevices()); 529 EnumerateDevices());
503 EXPECT_EQ(descriptors->size(), GetParam().device_count); 530 EXPECT_EQ(GetParam().expected_device_count, descriptors->size());
504 ASSERT_FALSE(descriptors->empty()); 531 ASSERT_FALSE(descriptors->empty());
505 532
533 int device_index = 0;
506 for (const auto& descriptors_iterator : *descriptors) { 534 for (const auto& descriptors_iterator : *descriptors) {
535 media::VideoCaptureFormats supported_formats;
536 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator,
537 &supported_formats);
538 for (const auto& supported_formats_entry : supported_formats) {
539 EXPECT_EQ(GetParam().expected_pixel_formats[device_index],
540 supported_formats_entry.pixel_format);
541 }
542
507 std::unique_ptr<VideoCaptureDevice> device = 543 std::unique_ptr<VideoCaptureDevice> device =
508 video_capture_device_factory_->CreateDevice(descriptors_iterator); 544 video_capture_device_factory_->CreateDevice(descriptors_iterator);
509 ASSERT_TRUE(device); 545 ASSERT_TRUE(device);
510 546
511 VideoCaptureParams capture_params; 547 VideoCaptureParams capture_params;
512 capture_params.requested_format.frame_size.SetSize(1280, 720); 548 capture_params.requested_format.frame_size.SetSize(1280, 720);
513 capture_params.requested_format.frame_rate = GetParam().fps; 549 capture_params.requested_format.frame_rate = GetParam().expected_fps;
550 capture_params.requested_format.pixel_format =
551 GetParam().expected_pixel_formats[device_index];
514 device->AllocateAndStart(capture_params, CreateClient()); 552 device->AllocateAndStart(capture_params, CreateClient());
515 WaitForCapturedFrame(); 553 WaitForCapturedFrame();
516 EXPECT_EQ(last_format().frame_size.width(), 1280); 554 EXPECT_EQ(1280, last_format().frame_size.width());
517 EXPECT_EQ(last_format().frame_size.height(), 720); 555 EXPECT_EQ(720, last_format().frame_size.height());
518 EXPECT_EQ(last_format().frame_rate, GetParam().fps); 556 EXPECT_EQ(GetParam().expected_fps, last_format().frame_rate);
557 EXPECT_EQ(GetParam().expected_pixel_formats[device_index],
558 last_format().pixel_format);
519 device->StopAndDeAllocate(); 559 device->StopAndDeAllocate();
560
561 device_index++;
520 } 562 }
521 } 563 }
522 564
523 INSTANTIATE_TEST_CASE_P( 565 INSTANTIATE_TEST_CASE_P(
524 , 566 ,
525 FakeVideoCaptureDeviceCommandLineTest, 567 FakeVideoCaptureDeviceFactoryTest,
526 Values(CommandLineTestData{"fps=-1", 5, 1u}, 568 Values(CommandLineTestData{"fps=-1", 5, 1u, {PIXEL_FORMAT_I420}},
527 CommandLineTestData{"fps=29.97, device-count=1", 29.97f, 1u}, 569 CommandLineTestData{"fps=29.97,device-count=1",
528 CommandLineTestData{"fps=60, device-count=2", 60, 2u}, 570 29.97f,
529 CommandLineTestData{"fps=1000, device-count=-1", 60, 1u}, 571 1u,
530 CommandLineTestData{"device-count=2", 20, 2u}, 572 {PIXEL_FORMAT_I420}},
531 CommandLineTestData{"device-count=0", 20, 1u})); 573 CommandLineTestData{"fps=60,device-count=2",
574 60,
575 2u,
576 {PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16}},
577 CommandLineTestData{"fps=1000,device-count=-1",
578 60,
579 1u,
580 {PIXEL_FORMAT_I420}},
581 CommandLineTestData{
582 "device-count=3",
583 20,
584 3u,
585 {PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, PIXEL_FORMAT_I420}},
586 CommandLineTestData{
587 "device-count=3,ownership=client",
588 20,
589 3u,
590 {PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, PIXEL_FORMAT_I420}},
591 CommandLineTestData{"device-count=0", 20, 1u, {PIXEL_FORMAT_I420}}));
532 }; // namespace media 592 }; // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/fake_video_capture_device_factory.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698