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_METHOD3(OnFrameReady
,
97 void(const scoped_refptr
<media::VideoFrame
>&,
98 const media::VideoCaptureFormat
&,
99 const base::TimeTicks
& estimated_capture_time
));
100 MOCK_METHOD0(OnStarted
, void());
101 MOCK_METHOD0(OnStopped
, void());
103 void OnStateUpdate(VideoCaptureState state
) {
105 case VIDEO_CAPTURE_STATE_STARTED
:
108 case VIDEO_CAPTURE_STATE_STOPPED
:
116 base::Closure
StartCapture(const media::VideoCaptureParams
& params
) {
117 return manager_
->StartCapture(
119 base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate
,
120 base::Unretained(this)),
121 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady
,
122 base::Unretained(this)));
125 base::MessageLoop message_loop_
;
126 scoped_ptr
<ChildProcess
> child_process_
;
127 media::VideoCaptureParams params_
;
128 base::RunLoop cleanup_run_loop_
;
129 scoped_ptr
<MockVideoCaptureImplManager
> manager_
;
132 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest
);
135 // Multiple clients with the same session id. There is only one
136 // media::VideoCapture object.
137 TEST_F(VideoCaptureImplManagerTest
, MultipleClients
) {
138 base::Closure release_cb1
= manager_
->UseDevice(0);
139 base::Closure release_cb2
= manager_
->UseDevice(0);
140 base::Closure stop_cb1
, stop_cb2
;
142 base::RunLoop run_loop
;
143 base::Closure quit_closure
= BindToCurrentLoop(
144 run_loop
.QuitClosure());
145 EXPECT_CALL(*this, OnStarted()).WillOnce(
146 RunClosure(quit_closure
));
147 EXPECT_CALL(*this, OnStarted()).RetiresOnSaturation();
148 stop_cb1
= StartCapture(params_
);
149 stop_cb2
= StartCapture(params_
);
155 base::RunLoop run_loop
;
156 base::Closure quit_closure
= BindToCurrentLoop(
157 run_loop
.QuitClosure());
158 EXPECT_CALL(*this, OnStopped()).WillOnce(
159 RunClosure(quit_closure
));
160 EXPECT_CALL(*this, OnStopped()).RetiresOnSaturation();
168 cleanup_run_loop_
.Run();
171 TEST_F(VideoCaptureImplManagerTest
, NoLeak
) {
172 manager_
->UseDevice(0).Reset();
174 cleanup_run_loop_
.Run();
177 } // namespace content