Index: media/capture/service/video_capture_unittest.cc |
diff --git a/media/capture/service/video_capture_unittest.cc b/media/capture/service/video_capture_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e145aed7e438baf706d444940780f4b872565e3e |
--- /dev/null |
+++ b/media/capture/service/video_capture_unittest.cc |
@@ -0,0 +1,225 @@ |
+// 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/interfaces/video_capture.mojom.h" |
+#include "media/capture/service/mock_stream_client.h" |
+#include "media/capture/service/stream_impl.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" |
+ |
+#if defined(OS_MACOSX) |
+#include "media/base/mac/avfoundation_glue.h" |
+#endif |
+ |
+using ::testing::_; |
+using ::testing::AnyNumber; |
+using ::testing::InSequence; |
+using ::testing::Field; |
+ |
+namespace media { |
+ |
+namespace { |
+ |
+ACTION_P(RunClosure, closure) { |
+ closure.Run(); |
+} |
+ |
+void UseFakeFactory() { |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kUseFakeDeviceForMediaStream); |
+} |
+ |
+void UseRealFactory() { |
+#if defined(OS_MACOSX) |
+ AVFoundationGlue::InitializeAVFoundation(); |
+#endif |
+} |
+ |
+} // anonymous namespace |
+ |
+// This test coalesces all the classes and objects that conform a video capture |
+// component from the outside. It then exercises the .mojom interfaces. |
+// TODO(mcasas): This test is an integration test of sorts, but we use |
+// a VideoCaptureHandlerImpl class directly and not via a Mojo pipe. Remove this |
+// test when an VideoCapture Mojo App (i.e. a truly black box) test is landed. |
+class VideoCaptureTest : public ::testing::Test { |
+ public: |
+ VideoCaptureTest() {} |
+ |
+ void InitializeVideoCaptureHandlerImpl() { |
+ video_capture_handler_impl_.reset( |
+ new VideoCaptureHandlerImpl(nullptr /* app */)); |
+ base::RunLoop().RunUntilIdle(); |
+ } |
+ |
+ void EnumerateDevices( |
+ const VideoCaptureHandlerImpl::EnumerateDevicesCallback& callback) { |
+ video_capture_handler_impl_->EnumerateDevices(callback); |
+ } |
+ |
+ MOCK_METHOD0(DoOnEnumerateDevicesReply, void(void)); |
+ void OnEnumerateDevicesReply( |
+ mojo::Array<mojom::VideoCaptureDeviceInfoPtr> reply) { |
+ DoOnEnumerateDevicesReply(); |
+ video_devices_info_ = std::move(reply); |
+ } |
+ |
+ MOCK_METHOD1(DoOnRequestVideoCaptureStreamReply, |
+ void(const mojom::VideoCaptureStream&)); |
+ void OnRequestVideoCaptureStreamReply(mojom::VideoCaptureStreamPtr stream) { |
+ EXPECT_TRUE(stream.is_bound()); |
+ DoOnRequestVideoCaptureStreamReply(*stream); |
+ stream_ = std::move(stream); |
+ } |
+ |
+ // Enumerate devices to get a valid |id| for the first one. |
+ void EnumerateDevices() { |
+ VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = base::Bind( |
+ &VideoCaptureTest::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(); |
+ } |
+ |
+ // This block runs a RequestVideoCaptureStream() --> get a Stream(). |
+ void RequestVideoCaptureStream() { |
+ mojom::VideoCaptureOptionsPtr options = mojom::VideoCaptureOptions::New(); |
+ options->device_id = video_devices_info_[0]->device_id; |
+ options->capture_size = mojo::Size::From(gfx::Size(320, 240)); |
+ options->frame_rate = 30.0; |
+ |
+ VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback callback = |
+ base::Bind(&VideoCaptureTest::OnRequestVideoCaptureStreamReply, |
+ base::Unretained(this)); |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ |
+ EXPECT_CALL(*this, DoOnRequestVideoCaptureStreamReply(_)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ |
+ video_capture_handler_impl_->RequestVideoCaptureStream(std::move(options), |
+ std::move(callback)); |
+ run_loop.Run(); |
+ ASSERT_TRUE(!!stream_.get()); |
+ } |
+ |
+ // Call Stream::Start() and expect some frames. |
+ void StartAndExpectFrames() { |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ |
+ EXPECT_CALL(stream_client_impl_, DoOnFrameAvailable(_, _)).Times(3); |
+ EXPECT_CALL(stream_client_impl_, DoOnFrameAvailable(_, _)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ stream_->Start(stream_client_impl_.CreateProxy()); |
+ |
+ run_loop.Run(); |
+ } |
+ |
+ void StartAndExpectFramesAndError() { |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ |
+ EXPECT_CALL(stream_client_impl_, DoOnFrameAvailable(_, _)) |
+ .Times(AnyNumber()); |
+ stream_->Start(stream_client_impl_.CreateProxy()); |
+ |
+ EXPECT_CALL(stream_client_impl_, OnError(_)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ |
+ // Access the first and only StreamImpl and ping its OnError() callback. |
+ ASSERT_EQ(1u, video_capture_handler_impl_->device_and_streams_.size()); |
+ video_capture_handler_impl_->device_and_streams_.begin() |
+ ->second.second->OnError("Terrible error"); |
+ run_loop.Run(); |
+ } |
+ |
+ void FakeOnErrorFromDevice() {} |
+ |
+ 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 component's entry point. Needs to be a scoped_ptr to be initialized |
+ // after adding command line flag for the appropriate Device Factory. |
+ scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_; |
+ |
+ // The mock of the (remote) video capture client. |
+ MockStreamClient stream_client_impl_; |
+ |
+ // Local pointer to the generated Stream spitting VideoFrames. |
+ mojom::VideoCaptureStreamPtr stream_; |
+ |
+ // Variable to hold on to the returned list of enumerated devices. |
+ mojo::Array<mojom::VideoCaptureDeviceInfoPtr> video_devices_info_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(VideoCaptureTest); |
+}; |
+ |
+TEST_F(VideoCaptureTest, CreateAndDestroy) {} |
+ |
+// Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply -> |
+// RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() -> |
+// get frames. This test uses a FakeVideoCaptureDeviceFactory and succeeds all |
+// right. |
+TEST_F(VideoCaptureTest, RequestStreamAndStartAndCapture) { |
+ InSequence s; |
+ UseFakeFactory(); |
+ InitializeVideoCaptureHandlerImpl(); |
+ EnumerateDevices(); |
+ RequestVideoCaptureStream(); |
+ |
+ StartAndExpectFrames(); |
+ stream_->Stop(); |
+} |
+ |
+// Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply -> |
+// RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() -> |
+// Error !!!. |
+TEST_F(VideoCaptureTest, RequestStreamAndStartAndError) { |
+ InSequence s; |
+ UseFakeFactory(); |
+ InitializeVideoCaptureHandlerImpl(); |
+ EnumerateDevices(); |
+ RequestVideoCaptureStream(); |
+ |
+ StartAndExpectFramesAndError(); |
+ stream_->Stop(); |
+} |
+ |
+// Same as RequestStreamAndStartAndCapture, with a real capture device. |
+TEST_F(VideoCaptureTest, RequestStreamAndStartAndCaptureWithRealDevice) { |
+ InSequence s; |
+ |
+ UseRealFactory(); |
+ InitializeVideoCaptureHandlerImpl(); |
+ EnumerateDevices(); |
+ if (video_devices_info_.size() == 0u) // No real devices. |
+ return; |
+ RequestVideoCaptureStream(); |
+ |
+ StartAndExpectFrames(); |
+ stream_->Stop(); |
+} |
+ |
+} // namespace media |