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

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 comments & rebase Created 3 years, 11 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 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_TRUE(storage == media::PIXEL_STORAGE_CPU);
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(
309 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS), 316 Values(PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16, PIXEL_FORMAT_ARGB),
310 Values(20, 29.97, 30, 50, 60))); 317 Values(FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS,
318 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_CLIENT_BUFFERS),
319 Values(20, 29.97, 30, 50, 60)));
311 320
312 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { 321 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) {
313 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 322 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
314 switches::kUseFakeDeviceForMediaStream, "device-count=3"); 323 switches::kUseFakeDeviceForMediaStream, "device-count=3");
315 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( 324 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors(
316 EnumerateDevices()); 325 EnumerateDevices());
317 ASSERT_EQ(3u, descriptors->size()); 326 ASSERT_EQ(3u, descriptors->size());
318 327
319 for (const auto& descriptors_iterator : *descriptors) { 328 for (const auto& descriptors_iterator : *descriptors) {
320 VideoCaptureFormats supported_formats; 329 VideoCaptureFormats supported_formats;
(...skipping 20 matching lines...) Expand all
341 EXPECT_EQ(expected_format, supported_formats[3].pixel_format); 350 EXPECT_EQ(expected_format, supported_formats[3].pixel_format);
342 EXPECT_GE(supported_formats[3].frame_rate, 20.0); 351 EXPECT_GE(supported_formats[3].frame_rate, 20.0);
343 EXPECT_EQ(1920, supported_formats[4].frame_size.width()); 352 EXPECT_EQ(1920, supported_formats[4].frame_size.width());
344 EXPECT_EQ(1080, supported_formats[4].frame_size.height()); 353 EXPECT_EQ(1080, supported_formats[4].frame_size.height());
345 EXPECT_EQ(expected_format, supported_formats[4].pixel_format); 354 EXPECT_EQ(expected_format, supported_formats[4].pixel_format);
346 EXPECT_GE(supported_formats[4].frame_rate, 20.0); 355 EXPECT_GE(supported_formats[4].frame_rate, 20.0);
347 } 356 }
348 } 357 }
349 358
350 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { 359 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) {
351 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( 360 auto device = FakeVideoCaptureDeviceMaker::MakeInstance(
352 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); 361 PIXEL_FORMAT_I420,
362 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS, 30.0);
353 ASSERT_TRUE(device); 363 ASSERT_TRUE(device);
354 364
355 VideoCaptureParams capture_params; 365 VideoCaptureParams capture_params;
356 capture_params.requested_format.frame_size.SetSize(640, 480); 366 capture_params.requested_format.frame_size.SetSize(640, 480);
357 capture_params.requested_format.frame_rate = 30.0; 367 capture_params.requested_format.frame_rate = 30.0;
358 device->AllocateAndStart(capture_params, std::move(client_)); 368 device->AllocateAndStart(capture_params, std::move(client_));
359 369
360 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( 370 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback(
361 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, 371 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities,
362 image_capture_client_), 372 image_capture_client_),
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 device->GetPhotoCapabilities(std::move(scoped_get_callback2)); 460 device->GetPhotoCapabilities(std::move(scoped_get_callback2));
451 run_loop_.reset(new base::RunLoop()); 461 run_loop_.reset(new base::RunLoop());
452 run_loop_->Run(); 462 run_loop_->Run();
453 EXPECT_EQ(max_zoom_value, 463 EXPECT_EQ(max_zoom_value,
454 image_capture_client_->capabilities()->zoom->current); 464 image_capture_client_->capabilities()->zoom->current);
455 465
456 device->StopAndDeAllocate(); 466 device->StopAndDeAllocate();
457 } 467 }
458 468
459 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { 469 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) {
460 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( 470 auto device = FakeVideoCaptureDeviceMaker::MakeInstance(
461 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); 471 PIXEL_FORMAT_I420,
472 FakeVideoCaptureDeviceMaker::DeliveryMode::USE_OWN_BUFFERS, 30.0);
462 ASSERT_TRUE(device); 473 ASSERT_TRUE(device);
463 474
464 VideoCaptureParams capture_params; 475 VideoCaptureParams capture_params;
465 capture_params.requested_format.frame_size.SetSize(640, 480); 476 capture_params.requested_format.frame_size.SetSize(640, 480);
466 capture_params.requested_format.frame_rate = 30.0; 477 capture_params.requested_format.frame_rate = 30.0;
467 device->AllocateAndStart(capture_params, std::move(client_)); 478 device->AllocateAndStart(capture_params, std::move(client_));
468 479
469 VideoCaptureDevice::TakePhotoCallback scoped_callback( 480 VideoCaptureDevice::TakePhotoCallback scoped_callback(
470 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), 481 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_),
471 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, 482 base::Bind(&ImageCaptureClient::OnTakePhotoFailure,
472 image_capture_client_)); 483 image_capture_client_));
473 484
474 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); 485 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1);
475 device->TakePhoto(std::move(scoped_callback)); 486 device->TakePhoto(std::move(scoped_callback));
476 487
477 run_loop_.reset(new base::RunLoop()); 488 run_loop_.reset(new base::RunLoop());
478 run_loop_->Run(); 489 run_loop_->Run();
479 device->StopAndDeAllocate(); 490 device->StopAndDeAllocate();
480 } 491 }
481 492
482 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRateAndDeviceCount) { 493 struct CommandLineTestData {
494 std::string switch_value_string;
495 float expected_fps;
496 size_t expected_device_count;
497 std::vector<VideoPixelFormat> expected_pixel_formats;
498 };
499
500 class FakeVideoCaptureDeviceFactoryTest
501 : public FakeVideoCaptureDeviceBase,
502 public ::testing::WithParamInterface<CommandLineTestData> {};
503
504 // Tests that the FakeVideoCaptureDeviceFactory delivers the expected number
505 // of devices and formats when being configured using command-line switches.
506 TEST_P(FakeVideoCaptureDeviceFactoryTest, FrameRateAndDeviceCount) {
483 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 507 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
484 switches::kUseFakeDeviceForMediaStream, GetParam().argument); 508 switches::kUseFakeDeviceForMediaStream, GetParam().switch_value_string);
485 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( 509 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors(
486 EnumerateDevices()); 510 EnumerateDevices());
487 EXPECT_EQ(descriptors->size(), GetParam().device_count); 511 EXPECT_EQ(GetParam().expected_device_count, descriptors->size());
488 ASSERT_FALSE(descriptors->empty()); 512 ASSERT_FALSE(descriptors->empty());
489 513
514 int device_index = 0;
490 for (const auto& descriptors_iterator : *descriptors) { 515 for (const auto& descriptors_iterator : *descriptors) {
516 media::VideoCaptureFormats supported_formats;
517 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator,
518 &supported_formats);
519 for (const auto& supported_formats_entry : supported_formats) {
520 EXPECT_EQ(GetParam().expected_pixel_formats[device_index],
521 supported_formats_entry.pixel_format);
522 }
523
491 std::unique_ptr<VideoCaptureDevice> device = 524 std::unique_ptr<VideoCaptureDevice> device =
492 video_capture_device_factory_->CreateDevice(descriptors_iterator); 525 video_capture_device_factory_->CreateDevice(descriptors_iterator);
493 ASSERT_TRUE(device); 526 ASSERT_TRUE(device);
494 527
495 VideoCaptureParams capture_params; 528 VideoCaptureParams capture_params;
496 capture_params.requested_format.frame_size.SetSize(1280, 720); 529 capture_params.requested_format.frame_size.SetSize(1280, 720);
497 capture_params.requested_format.frame_rate = GetParam().fps; 530 capture_params.requested_format.frame_rate = GetParam().expected_fps;
531 capture_params.requested_format.pixel_format =
532 GetParam().expected_pixel_formats[device_index];
498 device->AllocateAndStart(capture_params, CreateClient()); 533 device->AllocateAndStart(capture_params, CreateClient());
499 WaitForCapturedFrame(); 534 WaitForCapturedFrame();
500 EXPECT_EQ(last_format().frame_size.width(), 1280); 535 EXPECT_EQ(1280, last_format().frame_size.width());
501 EXPECT_EQ(last_format().frame_size.height(), 720); 536 EXPECT_EQ(720, last_format().frame_size.height());
502 EXPECT_EQ(last_format().frame_rate, GetParam().fps); 537 EXPECT_EQ(GetParam().expected_fps, last_format().frame_rate);
538 EXPECT_EQ(GetParam().expected_pixel_formats[device_index],
539 last_format().pixel_format);
503 device->StopAndDeAllocate(); 540 device->StopAndDeAllocate();
541
542 device_index++;
504 } 543 }
505 } 544 }
506 545
507 INSTANTIATE_TEST_CASE_P( 546 INSTANTIATE_TEST_CASE_P(
508 , 547 ,
509 FakeVideoCaptureDeviceCommandLineTest, 548 FakeVideoCaptureDeviceFactoryTest,
510 Values(CommandLineTestData{"fps=-1", 5, 1u}, 549 Values(
511 CommandLineTestData{"fps=29.97, device-count=1", 29.97f, 1u}, 550 CommandLineTestData{"fps=-1", 5, 1u,
512 CommandLineTestData{"fps=60, device-count=2", 60, 2u}, 551 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})},
513 CommandLineTestData{"fps=1000, device-count=-1", 60, 1u}, 552 CommandLineTestData{"fps=29.97,device-count=1", 29.97f, 1u,
514 CommandLineTestData{"device-count=2", 20, 2u}, 553 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})},
515 CommandLineTestData{"device-count=0", 20, 1u})); 554 CommandLineTestData{"fps=60,device-count=2", 60, 2u,
555 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420,
556 PIXEL_FORMAT_Y16})},
557 CommandLineTestData{"fps=1000,device-count=-1", 60, 1u,
558 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})},
559 CommandLineTestData{
560 "device-count=3", 20, 3u,
561 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16,
562 PIXEL_FORMAT_I420})},
563 CommandLineTestData{
564 "device-count=3,ownership=client", 20, 3u,
565 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420, PIXEL_FORMAT_Y16,
566 PIXEL_FORMAT_I420})},
567 CommandLineTestData{
568 "device-count=0", 20, 1u,
569 std::vector<VideoPixelFormat>({PIXEL_FORMAT_I420})}));
516 }; // namespace media 570 }; // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698