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..9b40c78e1b4a79f2201d03c3ef2a90284a65d77c |
--- /dev/null |
+++ b/media/capture/service/video_capture_unittest.cc |
@@ -0,0 +1,235 @@ |
+// 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 RequestMediaDevices( |
+ video_capture::MediaDevicesInfoRequestPtr request, |
+ const VideoCaptureHandlerImpl::RequestMediaDevicesCallback& callback) { |
+ video_capture_handler_impl_->RequestMediaDevices(std::move(request), |
+ callback); |
+ } |
+ |
+ MOCK_METHOD1(DoOnRequestMediaDevicesReply, |
+ void(const video_capture::MediaDevicesInfoReply&)); |
+ void OnRequestMediaDevicesReply( |
+ video_capture::MediaDevicesInfoReplyPtr reply) { |
+ DVLOG(1) << __FUNCTION__; |
+ DoOnRequestMediaDevicesReply(*reply); |
+ media_devices_info_ = std::move(reply); |
+ } |
+ |
+ MOCK_METHOD1(DoOnRequestStreamReply, void(const video_capture::Stream&)); |
+ void OnRequestStreamReply(video_capture::StreamPtr stream) { |
+ EXPECT_TRUE(stream.is_bound()); |
+ DoOnRequestStreamReply(*stream); |
+ stream_ = std::move(stream); |
+ } |
+ |
+ // Enumerate devices to get a valid |id| for the first one. |
+ void EnumerateDevices() { |
+ const int kRequestId = 123; |
+ video_capture::MediaDevicesInfoRequestPtr request = |
+ video_capture::MediaDevicesInfoRequest::New(); |
+ request->request_id = kRequestId; |
+ |
+ VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback = base::Bind( |
+ &VideoCaptureTest::OnRequestMediaDevicesReply, base::Unretained(this)); |
+ |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ EXPECT_CALL(*this, DoOnRequestMediaDevicesReply(Field( |
+ &video_capture::MediaDevicesInfoReply::request_id, |
+ kRequestId))) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ RequestMediaDevices(std::move(request), std::move(callback)); |
+ |
+ run_loop.Run(); |
+ } |
+ |
+ // This block runs a RequestStream() --> get a Stream(). |
+ void RequestStream() { |
+ const int kRequestId = 456; |
+ |
+ video_capture::StreamOptionsPtr options = |
+ video_capture::StreamOptions::New(); |
+ options->request_id = kRequestId; |
+ options->device_id = media_devices_info_->sources[0]->device_id; |
+ options->capture_size = mojo::Size::From(gfx::Size(320, 240)); |
+ options->frame_rate = 30.0; |
+ |
+ VideoCaptureHandlerImpl::RequestStreamCallback callback = base::Bind( |
+ &VideoCaptureTest::OnRequestStreamReply, base::Unretained(this)); |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ |
+ EXPECT_CALL(*this, DoOnRequestStreamReply(_)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ |
+ video_capture_handler_impl_->RequestStream( |
+ std::move(options), "https://localhost", 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_, DoFrameAvailable(_, _)).Times(3); |
+ EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _)) |
+ .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_, DoFrameAvailable(_, _)).Times(AnyNumber()); |
+ stream_->Start(stream_client_impl_.CreateProxy()); |
+ |
+ EXPECT_CALL(stream_client_impl_, Error(_)) |
+ .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. |
+ video_capture::StreamPtr stream_; |
+ |
+ // Variable to hold on to the returned list of enumerated devices. |
+ video_capture::MediaDevicesInfoReplyPtr media_devices_info_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(VideoCaptureTest); |
+}; |
+ |
+TEST_F(VideoCaptureTest, CreateAndDestroy) {} |
+ |
+// Tests a full cycle RequestMediaDevices() -> RequestMediaDevicesReply -> |
+// RequestStream()-> RequestStreamReply -> Start() -> get frames. |
+// This test uses a FakeVideoCaptureDeviceFactory and succeeds all right. |
+TEST_F(VideoCaptureTest, RequestStreamAndStartAndCapture) { |
+ InSequence s; |
+ UseFakeFactory(); |
+ InitializeVideoCaptureHandlerImpl(); |
+ EnumerateDevices(); |
+ RequestStream(); |
+ |
+ StartAndExpectFrames(); |
+ stream_->Stop(); |
+} |
+ |
+// Tests a full cycle RequestMediaDevices() -> RequestMediaDevicesReply -> |
+// RequestStream()-> RequestStreamReply -> Start() -> Error !!!. |
+TEST_F(VideoCaptureTest, RequestStreamAndStartAndError) { |
+ InSequence s; |
+ UseFakeFactory(); |
+ InitializeVideoCaptureHandlerImpl(); |
+ EnumerateDevices(); |
+ RequestStream(); |
+ |
+ StartAndExpectFramesAndError(); |
+ stream_->Stop(); |
+} |
+ |
+// Same as RequestStreamAndStartAndCapture, with a real capture device. |
+TEST_F(VideoCaptureTest, RequestStreamAndStartAndCaptureWithRealDevice) { |
+ InSequence s; |
+ |
+ UseRealFactory(); |
+ InitializeVideoCaptureHandlerImpl(); |
+ EnumerateDevices(); |
+ if (media_devices_info_->sources.size() == 0u) // No real devices. |
+ return; |
+ RequestStream(); |
+ |
+ StartAndExpectFrames(); |
+ stream_->Stop(); |
+} |
+ |
+} // namespace media |