Index: media/capture/video/video_capture_device_unittest.cc |
diff --git a/media/capture/video/video_capture_device_unittest.cc b/media/capture/video/video_capture_device_unittest.cc |
index ba72fdba8d642820c733169c6808b4262994c048..c5e35a6df509a8743d826abbc2d82b157861c0ea 100644 |
--- a/media/capture/video/video_capture_device_unittest.cc |
+++ b/media/capture/video/video_capture_device_unittest.cc |
@@ -19,6 +19,7 @@ |
#include "base/threading/thread.h" |
#include "base/threading/thread_task_runner_handle.h" |
#include "build/build_config.h" |
+#include "media/base/bind_to_current_loop.h" |
#include "media/base/video_capture_types.h" |
#include "media/capture/video/video_capture_device_factory.h" |
#include "testing/gmock/include/gmock/gmock.h" |
@@ -45,9 +46,11 @@ |
#define MAYBE_AllocateBadSize DISABLED_AllocateBadSize |
// We will always get YUYV from the Mac AVFoundation implementations. |
#define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg |
+#define MAYBE_TakePhoto TakePhoto |
#elif defined(OS_WIN) |
#define MAYBE_AllocateBadSize AllocateBadSize |
#define MAYBE_CaptureMjpeg CaptureMjpeg |
+#define MAYBE_TakePhoto DISABLED_TakePhoto |
#elif defined(OS_ANDROID) |
// TODO(wjia): enable those tests on Android. |
// On Android, native camera (JAVA) delivers frames on UI thread which is the |
@@ -57,6 +60,7 @@ |
#define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning |
#define DeAllocateCameraWhileRunning DISABLED_DeAllocateCameraWhileRunning |
#define MAYBE_CaptureMjpeg DISABLED_CaptureMjpeg |
+#define MAYBE_TakePhoto DISABLED_TakePhoto |
#elif defined(OS_LINUX) |
// AllocateBadSize will hang when a real camera is attached and if more than one |
// test is trying to use the camera (even across processes). Do NOT renable |
@@ -64,9 +68,11 @@ |
// http://crbug.com/94134 http://crbug.com/137260 http://crbug.com/417824 |
#define MAYBE_AllocateBadSize DISABLED_AllocateBadSize |
#define MAYBE_CaptureMjpeg CaptureMjpeg |
+#define MAYBE_TakePhoto DISABLED_TakePhoto |
#else |
#define MAYBE_AllocateBadSize AllocateBadSize |
#define MAYBE_CaptureMjpeg CaptureMjpeg |
+#define MAYBE_TakePhoto DISABLED_TakePhoto |
#endif |
using ::testing::_; |
@@ -135,6 +141,24 @@ class MockClient : public VideoCaptureDevice::Client { |
base::Callback<void(const VideoCaptureFormat&)> frame_cb_; |
}; |
+class MockImageCaptureClient : public base::RefCounted<MockImageCaptureClient> { |
+ public: |
+ // GMock doesn't support move-only arguments, so we use this forward method. |
+ void DoOnPhotoTaken(mojo::String mime_type, mojo::Array<uint8_t> data) { |
+ EXPECT_STREQ("image/jpeg", mime_type.storage().c_str()); |
+ ASSERT_GT(data.size(), 4u); |
+ OnCorrectPhotoTaken(); |
+ } |
+ MOCK_METHOD0(OnCorrectPhotoTaken, void(void)); |
+ MOCK_METHOD1( |
+ OnTakePhotoFailure, |
+ void(const base::Callback<void(mojo::String, mojo::Array<uint8_t>)>&)); |
+ |
+ private: |
+ friend class base::RefCounted<MockImageCaptureClient>; |
+ virtual ~MockImageCaptureClient() {} |
+}; |
+ |
class DeviceEnumerationListener |
: public base::RefCounted<DeviceEnumerationListener> { |
public: |
@@ -162,10 +186,10 @@ class VideoCaptureDeviceTest : public testing::TestWithParam<gfx::Size> { |
client_( |
new MockClient(base::Bind(&VideoCaptureDeviceTest::OnFrameCaptured, |
base::Unretained(this)))), |
+ device_enumeration_listener_(new DeviceEnumerationListener()), |
+ image_capture_client_(new MockImageCaptureClient()), |
video_capture_device_factory_(VideoCaptureDeviceFactory::CreateFactory( |
- base::ThreadTaskRunnerHandle::Get())) { |
- device_enumeration_listener_ = new DeviceEnumerationListener(); |
- } |
+ base::ThreadTaskRunnerHandle::Get())) {} |
void SetUp() override { |
#if defined(OS_ANDROID) |
@@ -253,12 +277,14 @@ class VideoCaptureDeviceTest : public testing::TestWithParam<gfx::Size> { |
base::win::ScopedCOMInitializer initialize_com_; |
#endif |
std::unique_ptr<VideoCaptureDevice::Names> names_; |
- std::unique_ptr<base::MessageLoop> loop_; |
+ const std::unique_ptr<base::MessageLoop> loop_; |
std::unique_ptr<base::RunLoop> run_loop_; |
std::unique_ptr<MockClient> client_; |
- scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
+ const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
+ const scoped_refptr<MockImageCaptureClient> image_capture_client_; |
VideoCaptureFormat last_format_; |
- std::unique_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; |
+ const std::unique_ptr<VideoCaptureDeviceFactory> |
+ video_capture_device_factory_; |
}; |
// Cause hangs on Windows Debug. http://crbug.com/417824 |
@@ -323,8 +349,7 @@ TEST_P(VideoCaptureDeviceTest, CaptureWithSize) { |
VideoCaptureParams capture_params; |
capture_params.requested_format.frame_size.SetSize(width, height); |
capture_params.requested_format.frame_rate = 30.0f; |
- capture_params.requested_format.pixel_format = |
- PIXEL_FORMAT_I420; |
+ capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
device->AllocateAndStart(capture_params, std::move(client_)); |
// Get captured video frames. |
WaitForCapturedFrame(); |
@@ -359,8 +384,7 @@ TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { |
VideoCaptureParams capture_params; |
capture_params.requested_format.frame_size.SetSize(637, 472); |
capture_params.requested_format.frame_rate = 35; |
- capture_params.requested_format.pixel_format = |
- PIXEL_FORMAT_I420; |
+ capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
device->AllocateAndStart(capture_params, std::move(client_)); |
WaitForCapturedFrame(); |
device->StopAndDeAllocate(); |
@@ -482,10 +506,43 @@ TEST_F(VideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
// GetDeviceSupportedFormats(). |
std::unique_ptr<VideoCaptureDevice::Name> name = |
GetFirstDeviceNameSupportingPixelFormat(PIXEL_FORMAT_MAX); |
- // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else |
- // to test here |
+ // Verify no camera returned for PIXEL_FORMAT_MAX. Nothing else to test here |
// since we cannot forecast the hardware capabilities. |
ASSERT_FALSE(name); |
} |
+// Start the camera and take a photo. |
+TEST_F(VideoCaptureDeviceTest, MAYBE_TakePhoto) { |
+ DVLOG(1) << __FUNCTION__; |
+ names_ = EnumerateDevices(); |
+ if (names_->empty()) { |
+ VLOG(1) << "No camera available. Exiting test."; |
+ return; |
+ } |
+ std::unique_ptr<VideoCaptureDevice> device( |
+ video_capture_device_factory_->Create(names_->front())); |
+ ASSERT_TRUE(device); |
+ |
+ EXPECT_CALL(*client_, OnError(_, _)).Times(0); |
+ |
+ VideoCaptureParams capture_params; |
+ capture_params.requested_format.frame_size.SetSize(640, 480); |
+ capture_params.requested_format.frame_rate = 30; |
+ capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
+ device->AllocateAndStart(capture_params, std::move(client_)); |
+ WaitForCapturedFrame(); |
+ |
+ VideoCaptureDevice::TakePhotoCallback scoped_callback( |
+ base::Bind(&MockImageCaptureClient::DoOnPhotoTaken, |
+ image_capture_client_), |
+ media::BindToCurrentLoop(base::Bind( |
+ &MockImageCaptureClient::OnTakePhotoFailure, image_capture_client_))); |
+ |
+ EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); |
+ device->TakePhoto(std::move(scoped_callback)); |
+ WaitForCapturedFrame(); |
+ |
+ device->StopAndDeAllocate(); |
+} |
+ |
}; // namespace media |