1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/callback.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "content/child/child_process.h"
11 #include "content/renderer/media/video_capture_impl.h"
12 #include "content/renderer/media/video_capture_impl_manager.h"
13 #include "content/renderer/media/video_capture_message_filter.h"
14 #include "media/base/bind_to_current_loop.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 using ::testing::DoAll;
20 using ::testing::SaveArg;
21 using media::BindToCurrentLoop;
25 ACTION_P(RunClosure, closure) {
29 class MockVideoCaptureImpl : public VideoCaptureImpl {
31 MockVideoCaptureImpl(media::VideoCaptureSessionId session_id,
32 VideoCaptureMessageFilter* filter,
33 base::Closure destruct_callback)
34 : VideoCaptureImpl(session_id, filter),
35 destruct_callback_(destruct_callback) {
38 ~MockVideoCaptureImpl() override { destruct_callback_.Run(); }
41 base::Closure destruct_callback_;
43 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl);
46 class MockVideoCaptureImplManager : public VideoCaptureImplManager {
48 explicit MockVideoCaptureImplManager(
49 base::Closure destruct_video_capture_callback)
50 : destruct_video_capture_callback_(
51 destruct_video_capture_callback) {}
52 ~MockVideoCaptureImplManager() override {}
55 VideoCaptureImpl* CreateVideoCaptureImplForTesting(
56 media::VideoCaptureSessionId id,
57 VideoCaptureMessageFilter* filter) const override {
58 return new MockVideoCaptureImpl(id,
60 destruct_video_capture_callback_);
64 base::Closure destruct_video_capture_callback_;
66 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager);
69 class VideoCaptureImplManagerTest : public ::testing::Test {
71 VideoCaptureImplManagerTest()
72 : manager_(new MockVideoCaptureImplManager(
73 BindToCurrentLoop(cleanup_run_loop_.QuitClosure()))) {
74 params_.requested_format = media::VideoCaptureFormat(
75 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
76 child_process_.reset(new ChildProcess());
79 void FakeChannelSetup() {
80 scoped_refptr<base::MessageLoopProxy> loop =
81 child_process_->io_message_loop_proxy();
82 if (!loop->BelongsToCurrentThread()) {
86 &VideoCaptureImplManagerTest::FakeChannelSetup,
87 base::Unretained(this)));
90 manager_->video_capture_message_filter()->OnFilterAdded(NULL);
94 MOCK_METHOD3(OnFrameReady,
95 void(const scoped_refptr<media::VideoFrame>&,
96 const media::VideoCaptureFormat&,
97 const base::TimeTicks& estimated_capture_time));
98 MOCK_METHOD0(OnStarted, void());
99 MOCK_METHOD0(OnStopped, void());
101 void OnStateUpdate(VideoCaptureState state) {
103 case VIDEO_CAPTURE_STATE_STARTED:
106 case VIDEO_CAPTURE_STATE_STOPPED:
114 base::Closure StartCapture(const media::VideoCaptureParams& params) {
115 return manager_->StartCapture(
117 base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate,
118 base::Unretained(this)),
119 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady,
120 base::Unretained(this)));
123 base::MessageLoop message_loop_;
124 scoped_ptr<ChildProcess> child_process_;
125 media::VideoCaptureParams params_;
126 base::RunLoop cleanup_run_loop_;
127 scoped_ptr<MockVideoCaptureImplManager> manager_;
130 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest);
133 // Multiple clients with the same session id. There is only one
134 // media::VideoCapture object.
135 TEST_F(VideoCaptureImplManagerTest, MultipleClients) {
136 base::Closure release_cb1 = manager_->UseDevice(0);
137 base::Closure release_cb2 = manager_->UseDevice(0);
138 base::Closure stop_cb1, stop_cb2;
140 base::RunLoop run_loop;
141 base::Closure quit_closure = BindToCurrentLoop(
142 run_loop.QuitClosure());
143 EXPECT_CALL(*this, OnStarted()).WillOnce(
144 RunClosure(quit_closure));
145 EXPECT_CALL(*this, OnStarted()).RetiresOnSaturation();
146 stop_cb1 = StartCapture(params_);
147 stop_cb2 = StartCapture(params_);
153 base::RunLoop run_loop;
154 base::Closure quit_closure = BindToCurrentLoop(
155 run_loop.QuitClosure());
156 EXPECT_CALL(*this, OnStopped()).WillOnce(
157 RunClosure(quit_closure));
158 EXPECT_CALL(*this, OnStopped()).RetiresOnSaturation();
166 cleanup_run_loop_.Run();
169 TEST_F(VideoCaptureImplManagerTest, NoLeak) {
170 manager_->UseDevice(0).Reset();
172 cleanup_run_loop_.Run();
175 } // namespace content