OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/bind.h" | |
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" | |
17 | |
18 using ::testing::_; | |
19 using ::testing::DoAll; | |
20 using ::testing::SaveArg; | |
21 using media::BindToCurrentLoop; | |
22 | |
23 namespace content { | |
24 | |
25 ACTION_P(RunClosure, closure) { | |
26 closure.Run(); | |
27 } | |
28 | |
29 class MockVideoCaptureImpl : public VideoCaptureImpl { | |
30 public: | |
31 MockVideoCaptureImpl(media::VideoCaptureSessionId session_id, | |
32 VideoCaptureMessageFilter* filter, | |
33 base::Closure destruct_callback) | |
34 : VideoCaptureImpl(session_id, filter), | |
35 destruct_callback_(destruct_callback) { | |
36 } | |
37 | |
38 ~MockVideoCaptureImpl() override { destruct_callback_.Run(); } | |
39 | |
40 private: | |
41 base::Closure destruct_callback_; | |
42 | |
43 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl); | |
44 }; | |
45 | |
46 class MockVideoCaptureImplManager : public VideoCaptureImplManager { | |
47 public: | |
48 explicit MockVideoCaptureImplManager( | |
49 base::Closure destruct_video_capture_callback) | |
50 : destruct_video_capture_callback_( | |
51 destruct_video_capture_callback) {} | |
52 ~MockVideoCaptureImplManager() override {} | |
53 | |
54 protected: | |
55 VideoCaptureImpl* CreateVideoCaptureImplForTesting( | |
56 media::VideoCaptureSessionId id, | |
57 VideoCaptureMessageFilter* filter) const override { | |
58 return new MockVideoCaptureImpl(id, | |
59 filter, | |
60 destruct_video_capture_callback_); | |
61 } | |
62 | |
63 private: | |
64 base::Closure destruct_video_capture_callback_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); | |
67 }; | |
68 | |
69 class VideoCaptureImplManagerTest : public ::testing::Test { | |
70 public: | |
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()); | |
77 } | |
78 | |
79 void FakeChannelSetup() { | |
80 scoped_refptr<base::MessageLoopProxy> loop = | |
81 child_process_->io_message_loop_proxy(); | |
82 if (!loop->BelongsToCurrentThread()) { | |
83 loop->PostTask( | |
84 FROM_HERE, | |
85 base::Bind( | |
86 &VideoCaptureImplManagerTest::FakeChannelSetup, | |
87 base::Unretained(this))); | |
88 return; | |
89 } | |
90 manager_->video_capture_message_filter()->OnFilterAdded(NULL); | |
91 } | |
92 | |
93 protected: | |
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()); | |
100 | |
101 void OnStateUpdate(VideoCaptureState state) { | |
102 switch (state) { | |
103 case VIDEO_CAPTURE_STATE_STARTED: | |
104 OnStarted(); | |
105 break; | |
106 case VIDEO_CAPTURE_STATE_STOPPED: | |
107 OnStopped(); | |
108 break; | |
109 default: | |
110 NOTREACHED(); | |
111 } | |
112 } | |
113 | |
114 base::Closure StartCapture(const media::VideoCaptureParams& params) { | |
115 return manager_->StartCapture( | |
116 0, params, | |
117 base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate, | |
118 base::Unretained(this)), | |
119 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady, | |
120 base::Unretained(this))); | |
121 } | |
122 | |
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_; | |
128 | |
129 private: | |
130 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); | |
131 }; | |
132 | |
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; | |
139 { | |
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_); | |
148 FakeChannelSetup(); | |
149 run_loop.Run(); | |
150 } | |
151 | |
152 { | |
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(); | |
159 stop_cb1.Run(); | |
160 stop_cb2.Run(); | |
161 run_loop.Run(); | |
162 } | |
163 | |
164 release_cb1.Run(); | |
165 release_cb2.Run(); | |
166 cleanup_run_loop_.Run(); | |
167 } | |
168 | |
169 TEST_F(VideoCaptureImplManagerTest, NoLeak) { | |
170 manager_->UseDevice(0).Reset(); | |
171 manager_.reset(); | |
172 cleanup_run_loop_.Run(); | |
173 } | |
174 | |
175 } // namespace content | |
OLD | NEW |