OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <array> | 5 #include <array> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 PauseResumeCallback* pause_callback, | 54 PauseResumeCallback* pause_callback, |
55 base::Closure destruct_callback) | 55 base::Closure destruct_callback) |
56 : VideoCaptureImpl(session_id, | 56 : VideoCaptureImpl(session_id, |
57 filter, | 57 filter, |
58 ChildProcess::current()->io_task_runner()), | 58 ChildProcess::current()->io_task_runner()), |
59 pause_callback_(pause_callback), | 59 pause_callback_(pause_callback), |
60 destruct_callback_(destruct_callback) {} | 60 destruct_callback_(destruct_callback) {} |
61 | 61 |
62 ~MockVideoCaptureImpl() override { destruct_callback_.Run(); } | 62 ~MockVideoCaptureImpl() override { destruct_callback_.Run(); } |
63 | 63 |
64 void Send(IPC::Message* message) override { | |
65 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) | |
66 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, Start) | |
67 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Resume, Resume) | |
68 default: | |
69 VideoCaptureImpl::Send(message); | |
70 return; | |
71 IPC_END_MESSAGE_MAP() | |
72 delete message; | |
73 } | |
74 | |
75 private: | 64 private: |
76 MOCK_METHOD1(Stop, void(int32_t)); | 65 void Start(int32_t device_id, |
77 MOCK_METHOD1(RequestRefreshFrame, void(int32_t)); | 66 int32_t session_id, |
78 | 67 const media::VideoCaptureParams& params) override { |
79 void Start(int device_id, | 68 // For every Start(), expect a corresponding Stop() call. |
80 media::VideoCaptureSessionId session_id, | |
81 const media::VideoCaptureParams& params) { | |
82 EXPECT_CALL(*this, Stop(_)); | 69 EXPECT_CALL(*this, Stop(_)); |
83 } | 70 } |
84 | 71 |
85 void Pause(int device_id) { | 72 MOCK_METHOD1(Stop, void(int32_t)); |
86 pause_callback_->OnPaused(session_id()); | 73 |
| 74 void Pause(int device_id) { pause_callback_->OnPaused(session_id()); } |
| 75 |
| 76 void Resume(int32_t device_id, |
| 77 int32_t session_id, |
| 78 const media::VideoCaptureParams& params) override { |
| 79 pause_callback_->OnResumed(session_id); |
87 } | 80 } |
88 | 81 |
89 void Resume(int device_id, | 82 MOCK_METHOD1(RequestRefreshFrame, void(int32_t)); |
90 media::VideoCaptureSessionId session_id, | |
91 const media::VideoCaptureParams& params) { | |
92 pause_callback_->OnResumed(session_id); | |
93 } | |
94 | 83 |
95 PauseResumeCallback* const pause_callback_; | 84 PauseResumeCallback* const pause_callback_; |
96 const base::Closure destruct_callback_; | 85 const base::Closure destruct_callback_; |
97 | 86 |
98 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl); | 87 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl); |
99 }; | 88 }; |
100 | 89 |
101 class MockVideoCaptureImplManager : public VideoCaptureImplManager { | 90 class MockVideoCaptureImplManager : public VideoCaptureImplManager { |
102 public: | 91 public: |
103 MockVideoCaptureImplManager(PauseResumeCallback* pause_callback, | 92 MockVideoCaptureImplManager(PauseResumeCallback* pause_callback, |
(...skipping 18 matching lines...) Expand all Loading... |
122 | 111 |
123 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); | 112 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); |
124 }; | 113 }; |
125 | 114 |
126 } // namespace | 115 } // namespace |
127 | 116 |
128 class VideoCaptureImplManagerTest : public ::testing::Test, | 117 class VideoCaptureImplManagerTest : public ::testing::Test, |
129 public PauseResumeCallback { | 118 public PauseResumeCallback { |
130 public: | 119 public: |
131 VideoCaptureImplManagerTest() | 120 VideoCaptureImplManagerTest() |
132 : child_process_(new ChildProcess()), | 121 : manager_(new MockVideoCaptureImplManager( |
133 manager_(new MockVideoCaptureImplManager( | |
134 this, | 122 this, |
135 BindToCurrentLoop(cleanup_run_loop_.QuitClosure()))) {} | 123 BindToCurrentLoop(cleanup_run_loop_.QuitClosure()))) {} |
136 | 124 |
137 protected: | 125 protected: |
138 static constexpr size_t kNumClients = 3; | 126 static constexpr size_t kNumClients = 3; |
139 | 127 |
140 std::array<base::Closure, kNumClients> StartCaptureForAllClients( | 128 std::array<base::Closure, kNumClients> StartCaptureForAllClients( |
141 bool same_session_id) { | 129 bool same_session_id) { |
142 base::RunLoop run_loop; | 130 base::RunLoop run_loop; |
143 base::Closure quit_closure = BindToCurrentLoop(run_loop.QuitClosure()); | 131 base::Closure quit_closure = BindToCurrentLoop(run_loop.QuitClosure()); |
144 EXPECT_CALL(*this, OnStarted(_)).Times(kNumClients - 1) | 132 EXPECT_CALL(*this, OnStarted(_)).Times(kNumClients - 1) |
145 .RetiresOnSaturation(); | 133 .RetiresOnSaturation(); |
146 EXPECT_CALL(*this, OnStarted(_)).WillOnce(RunClosure(quit_closure)) | 134 EXPECT_CALL(*this, OnStarted(_)).WillOnce(RunClosure(quit_closure)) |
147 .RetiresOnSaturation(); | 135 .RetiresOnSaturation(); |
148 std::array<base::Closure, kNumClients> stop_callbacks; | 136 std::array<base::Closure, kNumClients> stop_callbacks; |
149 media::VideoCaptureParams params; | 137 media::VideoCaptureParams params; |
150 params.requested_format = media::VideoCaptureFormat( | 138 params.requested_format = media::VideoCaptureFormat( |
151 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); | 139 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); |
152 for (size_t i = 0; i < kNumClients; ++i) { | 140 for (size_t i = 0; i < kNumClients; ++i) { |
153 stop_callbacks[i] = StartCapture( | 141 stop_callbacks[i] = StartCapture( |
154 same_session_id ? 0 : static_cast<media::VideoCaptureSessionId>(i), | 142 same_session_id ? 0 : static_cast<media::VideoCaptureSessionId>(i), |
155 params); | 143 params); |
156 } | 144 } |
157 child_process_->io_task_runner()->PostTask( | 145 child_process_.io_task_runner()->PostTask( |
158 FROM_HERE, | 146 FROM_HERE, |
159 base::Bind(&VideoCaptureMessageFilter::OnFilterAdded, | 147 base::Bind(&VideoCaptureMessageFilter::OnFilterAdded, |
160 base::Unretained(manager_->video_capture_message_filter()), | 148 base::Unretained(manager_->video_capture_message_filter()), |
161 nullptr)); | 149 nullptr)); |
162 run_loop.Run(); | 150 run_loop.Run(); |
163 return stop_callbacks; | 151 return stop_callbacks; |
164 } | 152 } |
165 | 153 |
166 void StopCaptureForAllClients( | 154 void StopCaptureForAllClients( |
167 std::array<base::Closure, kNumClients>* stop_callbacks) { | 155 std::array<base::Closure, kNumClients>* stop_callbacks) { |
168 base::RunLoop run_loop; | 156 base::RunLoop run_loop; |
169 base::Closure quit_closure = BindToCurrentLoop(run_loop.QuitClosure()); | 157 base::Closure quit_closure = BindToCurrentLoop(run_loop.QuitClosure()); |
170 EXPECT_CALL(*this, OnStopped(_)).Times(kNumClients - 1) | 158 EXPECT_CALL(*this, OnStopped(_)).Times(kNumClients - 1) |
171 .RetiresOnSaturation(); | 159 .RetiresOnSaturation(); |
172 EXPECT_CALL(*this, OnStopped(_)).WillOnce(RunClosure(quit_closure)) | 160 EXPECT_CALL(*this, OnStopped(_)).WillOnce(RunClosure(quit_closure)) |
173 .RetiresOnSaturation(); | 161 .RetiresOnSaturation(); |
174 for (size_t i = 0; i < kNumClients; ++i) | 162 for (const auto& stop_callback : *stop_callbacks) |
175 (*stop_callbacks)[i].Run(); | 163 stop_callback.Run(); |
176 run_loop.Run(); | 164 run_loop.Run(); |
177 } | 165 } |
178 | 166 |
179 MOCK_METHOD2(OnFrameReady, | 167 MOCK_METHOD2(OnFrameReady, |
180 void(const scoped_refptr<media::VideoFrame>&, | 168 void(const scoped_refptr<media::VideoFrame>&, |
181 base::TimeTicks estimated_capture_time)); | 169 base::TimeTicks estimated_capture_time)); |
182 MOCK_METHOD1(OnStarted, void(media::VideoCaptureSessionId id)); | 170 MOCK_METHOD1(OnStarted, void(media::VideoCaptureSessionId id)); |
183 MOCK_METHOD1(OnStopped, void(media::VideoCaptureSessionId id)); | 171 MOCK_METHOD1(OnStopped, void(media::VideoCaptureSessionId id)); |
184 MOCK_METHOD1(OnPaused, void(media::VideoCaptureSessionId id)); | 172 MOCK_METHOD1(OnPaused, void(media::VideoCaptureSessionId id)); |
185 MOCK_METHOD1(OnResumed, void(media::VideoCaptureSessionId id)); | 173 MOCK_METHOD1(OnResumed, void(media::VideoCaptureSessionId id)); |
(...skipping 10 matching lines...) Expand all Loading... |
196 base::Closure StartCapture(media::VideoCaptureSessionId id, | 184 base::Closure StartCapture(media::VideoCaptureSessionId id, |
197 const media::VideoCaptureParams& params) { | 185 const media::VideoCaptureParams& params) { |
198 return manager_->StartCapture( | 186 return manager_->StartCapture( |
199 id, params, base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate, | 187 id, params, base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate, |
200 base::Unretained(this), id), | 188 base::Unretained(this), id), |
201 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady, | 189 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady, |
202 base::Unretained(this))); | 190 base::Unretained(this))); |
203 } | 191 } |
204 | 192 |
205 const base::MessageLoop message_loop_; | 193 const base::MessageLoop message_loop_; |
206 const std::unique_ptr<ChildProcess> child_process_; | 194 ChildProcess child_process_; |
207 base::RunLoop cleanup_run_loop_; | 195 base::RunLoop cleanup_run_loop_; |
208 std::unique_ptr<MockVideoCaptureImplManager> manager_; | 196 std::unique_ptr<MockVideoCaptureImplManager> manager_; |
209 | 197 |
210 private: | 198 private: |
211 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); | 199 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); |
212 }; | 200 }; |
213 | 201 |
214 // Multiple clients with the same session id. There is only one | 202 // Multiple clients with the same session id. There is only one |
215 // media::VideoCapture object. | 203 // media::VideoCapture object. |
216 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { | 204 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { |
217 std::array<base::Closure, kNumClients> release_callbacks; | 205 std::array<base::Closure, kNumClients> release_callbacks; |
218 for (size_t i = 0; i < kNumClients; ++i) | 206 for (size_t i = 0; i < kNumClients; ++i) |
219 release_callbacks[i] = manager_->UseDevice(0); | 207 release_callbacks[i] = manager_->UseDevice(0); |
220 std::array<base::Closure, kNumClients> stop_callbacks = | 208 std::array<base::Closure, kNumClients> stop_callbacks = |
221 StartCaptureForAllClients(true); | 209 StartCaptureForAllClients(true); |
222 StopCaptureForAllClients(&stop_callbacks); | 210 StopCaptureForAllClients(&stop_callbacks); |
223 for (size_t i = 0; i < kNumClients; ++i) | 211 for (const auto& release_callback : release_callbacks) |
224 release_callbacks[i].Run(); | 212 release_callback.Run(); |
225 cleanup_run_loop_.Run(); | 213 cleanup_run_loop_.Run(); |
226 } | 214 } |
227 | 215 |
228 TEST_F(VideoCaptureImplManagerTest, NoLeak) { | 216 TEST_F(VideoCaptureImplManagerTest, NoLeak) { |
229 manager_->UseDevice(0).Reset(); | 217 manager_->UseDevice(0).Reset(); |
230 manager_.reset(); | 218 manager_.reset(); |
231 cleanup_run_loop_.Run(); | 219 cleanup_run_loop_.Run(); |
232 } | 220 } |
233 | 221 |
234 TEST_F(VideoCaptureImplManagerTest, SuspendAndResumeSessions) { | 222 TEST_F(VideoCaptureImplManagerTest, SuspendAndResumeSessions) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 base::Closure quit_closure = BindToCurrentLoop(run_loop.QuitClosure()); | 289 base::Closure quit_closure = BindToCurrentLoop(run_loop.QuitClosure()); |
302 EXPECT_CALL(*this, OnResumed(0)).Times(1).RetiresOnSaturation(); | 290 EXPECT_CALL(*this, OnResumed(0)).Times(1).RetiresOnSaturation(); |
303 EXPECT_CALL(*this, OnResumed(1)).Times(1).RetiresOnSaturation(); | 291 EXPECT_CALL(*this, OnResumed(1)).Times(1).RetiresOnSaturation(); |
304 EXPECT_CALL(*this, OnResumed(2)).WillOnce(RunClosure(quit_closure)) | 292 EXPECT_CALL(*this, OnResumed(2)).WillOnce(RunClosure(quit_closure)) |
305 .RetiresOnSaturation(); | 293 .RetiresOnSaturation(); |
306 manager_->SuspendDevices(false); | 294 manager_->SuspendDevices(false); |
307 run_loop.Run(); | 295 run_loop.Run(); |
308 } | 296 } |
309 | 297 |
310 StopCaptureForAllClients(&stop_callbacks); | 298 StopCaptureForAllClients(&stop_callbacks); |
311 for (size_t i = 0; i < kNumClients; ++i) | 299 for (const auto& release_callback : release_callbacks) |
312 release_callbacks[i].Run(); | 300 release_callback.Run(); |
313 cleanup_run_loop_.Run(); | 301 cleanup_run_loop_.Run(); |
314 } | 302 } |
315 | 303 |
316 } // namespace content | 304 } // namespace content |
OLD | NEW |