| 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..56d03e9d2bfe554c825db2ccb9f330688ae295ab
|
| --- /dev/null
|
| +++ b/media/capture/service/video_capture_unittest.cc
|
| @@ -0,0 +1,219 @@
|
| +// 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(
|
| + const VideoCaptureHandlerImpl::RequestMediaDevicesCallback& callback) {
|
| + video_capture_handler_impl_->RequestMediaDevices(callback);
|
| + }
|
| +
|
| + MOCK_METHOD0(DoOnRequestMediaDevicesReply, void(void));
|
| + void OnRequestMediaDevicesReply(mojo::Array<mojom::WebSourceInfoPtr> reply) {
|
| + DoOnRequestMediaDevicesReply();
|
| + media_devices_info_ = std::move(reply);
|
| + }
|
| +
|
| + MOCK_METHOD1(DoOnRequestStreamReply, void(const mojom::Stream&));
|
| + void OnRequestStreamReply(mojom::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() {
|
| + 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())
|
| + .Times(1)
|
| + .WillOnce(RunClosure(quit_closure));
|
| + RequestMediaDevices(std::move(callback));
|
| +
|
| + run_loop.Run();
|
| + }
|
| +
|
| + // This block runs a RequestStream() --> get a Stream().
|
| + void RequestStream() {
|
| + mojom::StreamOptionsPtr options = mojom::StreamOptions::New();
|
| + options->device_id = media_devices_info_[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.
|
| + mojom::StreamPtr stream_;
|
| +
|
| + // Variable to hold on to the returned list of enumerated devices.
|
| + mojo::Array<mojom::WebSourceInfoPtr> 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_.size() == 0u) // No real devices.
|
| + return;
|
| + RequestStream();
|
| +
|
| + StartAndExpectFrames();
|
| + stream_->Stop();
|
| +}
|
| +
|
| +} // namespace media
|
|
|