| 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..5b846ef30ecd8daad4fc7d43e5eb2644e93c56f5
|
| --- /dev/null
|
| +++ b/media/capture/service/video_capture_handler_impl_unittest.cc
|
| @@ -0,0 +1,191 @@
|
| +// 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(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) {
|
| + DVLOG(1) << __FUNCTION__;
|
| + DoOnRequestStreamReply(*stream);
|
| + }
|
| +
|
| + void RequestMediaDevices(
|
| + const VideoCaptureHandlerImpl::RequestMediaDevicesCallback& callback) {
|
| + video_capture_handler_impl_->RequestMediaDevices(callback);
|
| + }
|
| +
|
| + void SetNumFakeDevices(int num) {
|
| + static_cast<media::FakeVideoCaptureDeviceFactory*>(
|
| + video_capture_handler_impl_->video_capture_device_factory_.get())
|
| + ->set_number_of_devices(num);
|
| + }
|
| +
|
| + void RequestStream(
|
| + mojom::StreamOptionsPtr options,
|
| + const mojo::String& security_origin,
|
| + const VideoCaptureHandlerImpl::RequestStreamCallback& callback) {
|
| + video_capture_handler_impl_->RequestStream(std::move(options),
|
| + security_origin, 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::WebSourceInfoPtr> media_devices_info_;
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandlerImplTest);
|
| +};
|
| +
|
| +TEST_F(VideoCaptureHandlerImplTest, CreateAndDestroy) {}
|
| +
|
| +// Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback().
|
| +TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevices) {
|
| + InSequence s;
|
| +
|
| + VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
|
| + base::Bind(&VideoCaptureHandlerImplTest::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();
|
| + ASSERT_EQ(1u, media_devices_info_.size());
|
| + EXPECT_EQ(mojom::SourceKind::Video, media_devices_info_[0]->kind);
|
| +}
|
| +
|
| +// Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback() before
|
| +// and after a change in system devices has happened.
|
| +TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevicesAndReenumerate) {
|
| + InSequence s;
|
| + { // 1 Enumeration RequestMediaDevices()-> RequestMediaDevicesCallback().
|
| + VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
|
| + base::Bind(&VideoCaptureHandlerImplTest::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();
|
| +
|
| + ASSERT_GE(media_devices_info_.size(), 1u);
|
| + }
|
| +
|
| + SetNumFakeDevices(2);
|
| + system_monitor_.ProcessDevicesChanged(
|
| + base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + {
|
| + VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
|
| + base::Bind(&VideoCaptureHandlerImplTest::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();
|
| +
|
| + ASSERT_GE(media_devices_info_.size(), 2u);
|
| + }
|
| +}
|
| +
|
| +// Tests a cycle RequestStream() -> RequestStreamCallback().
|
| +TEST_F(VideoCaptureHandlerImplTest, RequestStream) {
|
| + InSequence s;
|
| +
|
| + { // Enumerate devices to get a valid |id| for the first one.
|
| + VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
|
| + base::Bind(&VideoCaptureHandlerImplTest::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();
|
| + }
|
| + {
|
| + mojom::StreamOptionsPtr options = mojom::StreamOptions::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::RequestStreamCallback callback =
|
| + base::Bind(&VideoCaptureHandlerImplTest::OnRequestStreamReply,
|
| + 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, DoOnRequestStreamReply(_))
|
| + .Times(1)
|
| + .WillOnce(RunClosure(quit_closure));
|
| + RequestStream(std::move(options), "https://localhost", std::move(callback));
|
| +
|
| + run_loop.Run();
|
| + }
|
| +}
|
| +
|
| +} // namespace media
|
|
|