Index: media/capture/service/video_capture_handler_impl_unittest.cc |
diff --git a/media/capture/service/video_capture_handler_impl_unittest.cc b/media/capture/service/video_capture_handler_impl_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ece89d7400f4c46977ae9bcd555b2b82adc409e7 |
--- /dev/null |
+++ b/media/capture/service/video_capture_handler_impl_unittest.cc |
@@ -0,0 +1,194 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/command_line.h" |
+#include "base/run_loop.h" |
+#include "media/base/media_switches.h" |
+#include "media/capture/service/video_capture_handler_impl.h" |
+#include "media/capture/video/fake_video_capture_device_factory.h" |
+#include "mojo/converters/geometry/geometry_type_converters.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::_; |
+using ::testing::InSequence; |
+ |
+namespace media { |
+ |
+namespace { |
+ |
+ACTION_P(RunClosure, closure) { |
+ closure.Run(); |
+} |
+ |
+} // anonymous namespace |
+ |
+class VideoCaptureHandlerImplTest : public testing::Test { |
+ public: |
+ VideoCaptureHandlerImplTest() {} |
+ |
+ void SetUp() override { |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kUseFakeDeviceForMediaStream); |
+ video_capture_handler_impl_.reset( |
+ new VideoCaptureHandlerImpl(nullptr /* app */)); |
+ } |
+ |
+ MOCK_METHOD0(DoOnEnumerateDevicesReply, void(void)); |
+ void OnEnumerateDevicesReply( |
+ mojo::Array<mojom::VideoCaptureDeviceInfoPtr> reply) { |
+ DoOnEnumerateDevicesReply(); |
+ media_devices_info_ = std::move(reply); |
+ } |
+ MOCK_METHOD1(DoOnRequestVideoCaptureStreamReply, |
+ void(const mojom::VideoCaptureStream&)); |
+ void OnRequestVideoCaptureStreamReply(mojom::VideoCaptureStreamPtr stream) { |
+ DVLOG(1) << __FUNCTION__; |
+ DoOnRequestVideoCaptureStreamReply(*stream); |
+ } |
+ |
+ void EnumerateDevices( |
+ const VideoCaptureHandlerImpl::EnumerateDevicesCallback& callback) { |
+ video_capture_handler_impl_->EnumerateDevices(callback); |
+ } |
+ |
+ void SetNumFakeDevices(int num) { |
+ static_cast<media::FakeVideoCaptureDeviceFactory*>( |
+ video_capture_handler_impl_->video_capture_device_factory_.get()) |
+ ->set_number_of_devices(num); |
+ } |
+ |
+ void RequestVideoCaptureStream( |
+ mojom::VideoCaptureOptionsPtr options, |
+ const VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback& |
+ callback) { |
+ video_capture_handler_impl_->RequestVideoCaptureStream(std::move(options), |
+ callback); |
+ } |
+ |
+ protected: |
+ // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl. |
+ const base::MessageLoop loop_; |
+ |
+ // Need a SystemMonitor for VideoCaptureHandlerImpl to register to. |
+ base::SystemMonitor system_monitor_; |
+ |
+ // The class under test. Needs to be a scoped_ptr to be initialized after |
+ // adding |kUseFakeDeviceForMediaStream| command line flag. |
+ scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_; |
+ |
+ // Variable to hold on to the returned list of enumerated devices. |
+ mojo::Array<mojom::VideoCaptureDeviceInfoPtr> media_devices_info_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandlerImplTest); |
+}; |
+ |
+TEST_F(VideoCaptureHandlerImplTest, CreateAndDestroy) {} |
+ |
+// Tests a cycle EnumerateDevices()-> EnumerateDevicesCallback(). |
+TEST_F(VideoCaptureHandlerImplTest, EnumerateDevices) { |
+ InSequence s; |
+ |
+ VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
+ base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
+ base::Unretained(this)); |
+ |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ EnumerateDevices(std::move(callback)); |
+ |
+ run_loop.Run(); |
+ ASSERT_EQ(1u, media_devices_info_.size()); |
+} |
+ |
+// Tests a cycle EnumerateDevices()-> EnumerateDevicesCallback() before |
+// and after a change in system devices has happened. |
+TEST_F(VideoCaptureHandlerImplTest, EnumerateDevicesAndReenumerate) { |
+ InSequence s; |
+ { // 1 Enumeration EnumerateDevices()-> EnumerateDevicesCallback(). |
+ VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
+ base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
+ base::Unretained(this)); |
+ |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ EnumerateDevices(std::move(callback)); |
+ run_loop.Run(); |
+ |
+ ASSERT_GE(media_devices_info_.size(), 1u); |
+ } |
+ |
+ SetNumFakeDevices(2); |
+ system_monitor_.ProcessDevicesChanged( |
+ base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ { |
+ VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
+ base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
+ base::Unretained(this)); |
+ |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ EnumerateDevices(std::move(callback)); |
+ run_loop.Run(); |
+ |
+ ASSERT_GE(media_devices_info_.size(), 2u); |
+ } |
+} |
+ |
+// Tests a cycle RequestVideoCaptureStream() -> |
+// RequestVideoCaptureStreamCallback(). |
+TEST_F(VideoCaptureHandlerImplTest, RequestVideoCaptureStream) { |
+ InSequence s; |
+ |
+ { // Enumerate devices to get a valid |id| for the first one. |
+ VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
+ base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
+ base::Unretained(this)); |
+ |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ EnumerateDevices(std::move(callback)); |
+ |
+ run_loop.Run(); |
+ } |
+ { |
+ mojom::VideoCaptureOptionsPtr options = mojom::VideoCaptureOptions::New(); |
+ options->device_id = media_devices_info_[0]->device_id; |
+ options->capture_size = mojo::Size::From(gfx::Size(640, 480)); |
+ options->frame_rate = 30.0; |
+ |
+ VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback callback = |
+ base::Bind( |
+ &VideoCaptureHandlerImplTest::OnRequestVideoCaptureStreamReply, |
+ base::Unretained(this)); |
+ |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ |
+ // TODO(mcasas): Inspect the replied sources[0] contents. |
+ EXPECT_CALL(*this, DoOnRequestVideoCaptureStreamReply(_)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ RequestVideoCaptureStream(std::move(options), std::move(callback)); |
+ |
+ run_loop.Run(); |
+ } |
+} |
+ |
+} // namespace media |