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 virtual ~MockVideoCaptureImpl() {
39 destruct_callback_.Run();
43 base::Closure destruct_callback_;
45 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl);
48 class MockVideoCaptureImplManager : public VideoCaptureImplManager {
50 explicit MockVideoCaptureImplManager(
51 base::Closure destruct_video_capture_callback)
52 : destruct_video_capture_callback_(
53 destruct_video_capture_callback) {}
54 virtual ~MockVideoCaptureImplManager() {}
57 virtual VideoCaptureImpl* CreateVideoCaptureImplForTesting(
58 media::VideoCaptureSessionId id,
59 VideoCaptureMessageFilter* filter) const OVERRIDE {
60 return new MockVideoCaptureImpl(id,
62 destruct_video_capture_callback_);
66 base::Closure destruct_video_capture_callback_;
68 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager);
71 class VideoCaptureImplManagerTest : public ::testing::Test {
73 VideoCaptureImplManagerTest()
74 : manager_(new MockVideoCaptureImplManager(
75 BindToCurrentLoop(cleanup_run_loop_.QuitClosure()))) {
76 params_.requested_format = media::VideoCaptureFormat(
77 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
78 child_process_.reset(new ChildProcess());
81 void FakeChannelSetup() {
82 scoped_refptr<base::MessageLoopProxy> loop =
83 child_process_->io_message_loop_proxy();
84 if (!loop->BelongsToCurrentThread()) {
88 &VideoCaptureImplManagerTest::FakeChannelSetup,
89 base::Unretained(this)));
92 manager_->video_capture_message_filter()->OnFilterAdded(NULL);
96 MOCK_METHOD2(OnFrameReady,
97 void(const scoped_refptr<media::VideoFrame>&,
98 const media::VideoCaptureFormat&));
99 MOCK_METHOD0(OnStarted, void());
100 MOCK_METHOD0(OnStopped, void());
102 void OnStateUpdate(VideoCaptureState state) {
104 case VIDEO_CAPTURE_STATE_STARTED:
107 case VIDEO_CAPTURE_STATE_STOPPED:
115 base::Closure StartCapture(const media::VideoCaptureParams& params) {
116 return manager_->StartCapture(
118 base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate,
119 base::Unretained(this)),
120 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady,
121 base::Unretained(this)));
124 base::MessageLoop message_loop_;
125 scoped_ptr<ChildProcess> child_process_;
126 media::VideoCaptureParams params_;
127 base::RunLoop cleanup_run_loop_;
128 scoped_ptr<MockVideoCaptureImplManager> manager_;
131 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest);
134 // Multiple clients with the same session id. There is only one
135 // media::VideoCapture object.
136 TEST_F(VideoCaptureImplManagerTest, MultipleClients) {
137 base::Closure release_cb1 = manager_->UseDevice(0);
138 base::Closure release_cb2 = manager_->UseDevice(0);
139 base::Closure stop_cb1, stop_cb2;
141 base::RunLoop run_loop;
142 base::Closure quit_closure = BindToCurrentLoop(
143 run_loop.QuitClosure());
144 EXPECT_CALL(*this, OnStarted()).WillOnce(
145 RunClosure(quit_closure));
146 EXPECT_CALL(*this, OnStarted()).RetiresOnSaturation();
147 stop_cb1 = StartCapture(params_);
148 stop_cb2 = StartCapture(params_);
154 base::RunLoop run_loop;
155 base::Closure quit_closure = BindToCurrentLoop(
156 run_loop.QuitClosure());
157 EXPECT_CALL(*this, OnStopped()).WillOnce(
158 RunClosure(quit_closure));
159 EXPECT_CALL(*this, OnStopped()).RetiresOnSaturation();
167 cleanup_run_loop_.Run();
170 TEST_F(VideoCaptureImplManagerTest, NoLeak) {
171 manager_->UseDevice(0).Reset();
173 cleanup_run_loop_.Run();
176 } // namespace content