OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "content/child/child_process.h" | 6 #include "content/child/child_process.h" |
7 #include "content/common/media/video_capture_messages.h" | 7 #include "content/common/media/video_capture_messages.h" |
8 #include "content/renderer/media/video_capture_impl.h" | 8 #include "content/renderer/media/video_capture_impl.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 MOCK_METHOD1(OnStarted, void(media::VideoCapture* capture)); | 38 MOCK_METHOD1(OnStarted, void(media::VideoCapture* capture)); |
39 MOCK_METHOD1(OnStopped, void(media::VideoCapture* capture)); | 39 MOCK_METHOD1(OnStopped, void(media::VideoCapture* capture)); |
40 MOCK_METHOD1(OnPaused, void(media::VideoCapture* capture)); | 40 MOCK_METHOD1(OnPaused, void(media::VideoCapture* capture)); |
41 MOCK_METHOD2(OnError, void(media::VideoCapture* capture, int error_code)); | 41 MOCK_METHOD2(OnError, void(media::VideoCapture* capture, int error_code)); |
42 MOCK_METHOD1(OnRemoved, void(media::VideoCapture* capture)); | 42 MOCK_METHOD1(OnRemoved, void(media::VideoCapture* capture)); |
43 MOCK_METHOD2(OnFrameReady, | 43 MOCK_METHOD2(OnFrameReady, |
44 void(media::VideoCapture* capture, | 44 void(media::VideoCapture* capture, |
45 const scoped_refptr<media::VideoFrame>& frame)); | 45 const scoped_refptr<media::VideoFrame>& frame)); |
46 MOCK_METHOD2(OnDeviceInfoReceived, | 46 MOCK_METHOD2(OnDeviceInfoReceived, |
47 void(media::VideoCapture* capture, | 47 void(media::VideoCapture* capture, |
48 const media::VideoCaptureParams& device_info)); | 48 const media::VideoCaptureFormat& device_info)); |
49 | 49 |
50 private: | 50 private: |
51 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient); | 51 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient); |
52 }; | 52 }; |
53 | 53 |
54 class VideoCaptureImplTest : public ::testing::Test { | 54 class VideoCaptureImplTest : public ::testing::Test { |
55 public: | 55 public: |
56 class MockVideoCaptureImpl : public VideoCaptureImpl { | 56 class MockVideoCaptureImpl : public VideoCaptureImpl { |
57 public: | 57 public: |
58 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, | 58 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, |
(...skipping 16 matching lines...) Expand all Loading... |
75 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, | 75 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, |
76 DeviceReceiveEmptyBuffer) | 76 DeviceReceiveEmptyBuffer) |
77 IPC_MESSAGE_UNHANDLED(handled = false) | 77 IPC_MESSAGE_UNHANDLED(handled = false) |
78 IPC_END_MESSAGE_MAP() | 78 IPC_END_MESSAGE_MAP() |
79 EXPECT_TRUE(handled); | 79 EXPECT_TRUE(handled); |
80 delete message; | 80 delete message; |
81 } | 81 } |
82 | 82 |
83 void DeviceStartCapture(int device_id, | 83 void DeviceStartCapture(int device_id, |
84 const media::VideoCaptureParams& params) { | 84 const media::VideoCaptureParams& params) { |
85 media::VideoCaptureParams device_info = params; | |
86 OnDeviceInfoReceived(device_info); | |
87 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED); | 85 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED); |
88 } | 86 } |
89 | 87 |
90 void DevicePauseCapture(int device_id) {} | 88 void DevicePauseCapture(int device_id) {} |
91 | 89 |
92 void DeviceStopCapture(int device_id) { | 90 void DeviceStopCapture(int device_id) { |
93 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); | 91 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); |
94 } | 92 } |
95 | 93 |
96 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {} | 94 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {} |
97 }; | 95 }; |
98 | 96 |
99 VideoCaptureImplTest() | 97 VideoCaptureImplTest() { |
100 : capability_small_(176, | 98 params_small_.requested_format = media::VideoCaptureFormat( |
101 144, | 99 176, 144, 30, media::ConstantResolutionVideoCaptureDevice); |
102 30, | 100 |
103 media::PIXEL_FORMAT_I420, | 101 params_large_.requested_format = media::VideoCaptureFormat( |
104 0, | 102 320, 240, 30, media::ConstantResolutionVideoCaptureDevice); |
105 false, | 103 |
106 media::ConstantResolutionVideoCaptureDevice), | |
107 capability_large_(320, | |
108 240, | |
109 30, | |
110 media::PIXEL_FORMAT_I420, | |
111 0, | |
112 false, | |
113 media::ConstantResolutionVideoCaptureDevice) { | |
114 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); | 104 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); |
115 message_loop_proxy_ = base::MessageLoopProxy::current().get(); | 105 message_loop_proxy_ = base::MessageLoopProxy::current().get(); |
116 child_process_.reset(new ChildProcess()); | 106 child_process_.reset(new ChildProcess()); |
117 | 107 |
118 message_filter_ = new MockVideoCaptureMessageFilter; | 108 message_filter_ = new MockVideoCaptureMessageFilter; |
119 session_id_ = 1; | 109 session_id_ = 1; |
120 | 110 |
121 video_capture_impl_ = new MockVideoCaptureImpl( | 111 video_capture_impl_ = new MockVideoCaptureImpl( |
122 session_id_, message_loop_proxy_, message_filter_.get()); | 112 session_id_, message_loop_proxy_, message_filter_.get()); |
123 | 113 |
124 video_capture_impl_->device_id_ = 2; | 114 video_capture_impl_->device_id_ = 2; |
125 } | 115 } |
126 | 116 |
127 virtual ~VideoCaptureImplTest() { | 117 virtual ~VideoCaptureImplTest() { |
128 delete video_capture_impl_; | 118 delete video_capture_impl_; |
129 } | 119 } |
130 | 120 |
131 protected: | 121 protected: |
132 scoped_ptr<base::MessageLoop> message_loop_; | 122 scoped_ptr<base::MessageLoop> message_loop_; |
133 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 123 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
134 scoped_ptr<ChildProcess> child_process_; | 124 scoped_ptr<ChildProcess> child_process_; |
135 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; | 125 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; |
136 media::VideoCaptureSessionId session_id_; | 126 media::VideoCaptureSessionId session_id_; |
137 MockVideoCaptureImpl* video_capture_impl_; | 127 MockVideoCaptureImpl* video_capture_impl_; |
138 const media::VideoCaptureCapability capability_small_; | 128 media::VideoCaptureParams params_small_; |
139 const media::VideoCaptureCapability capability_large_; | 129 media::VideoCaptureParams params_large_; |
140 | 130 |
141 private: | 131 private: |
142 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); | 132 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); |
143 }; | 133 }; |
144 | 134 |
145 TEST_F(VideoCaptureImplTest, Simple) { | 135 TEST_F(VideoCaptureImplTest, Simple) { |
146 // Execute SetCapture() and StopCapture() for one client. | 136 // Execute SetCapture() and StopCapture() for one client. |
147 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); | 137 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); |
148 | 138 |
149 EXPECT_CALL(*client, OnStarted(_)) | 139 EXPECT_CALL(*client, OnStarted(_)) |
150 .WillOnce(Return()); | 140 .WillOnce(Return()); |
151 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_)) | |
152 .WillOnce(Return()); | |
153 | 141 |
154 video_capture_impl_->StartCapture(client.get(), capability_small_); | 142 video_capture_impl_->StartCapture(client.get(), params_small_); |
155 message_loop_->RunUntilIdle(); | 143 message_loop_->RunUntilIdle(); |
156 | 144 |
157 EXPECT_CALL(*client, OnStopped(_)) | 145 EXPECT_CALL(*client, OnStopped(_)) |
158 .WillOnce(Return()); | 146 .WillOnce(Return()); |
159 EXPECT_CALL(*client, OnRemoved(_)) | 147 EXPECT_CALL(*client, OnRemoved(_)) |
160 .WillOnce(Return()); | 148 .WillOnce(Return()); |
161 | 149 |
162 video_capture_impl_->StopCapture(client.get()); | 150 video_capture_impl_->StopCapture(client.get()); |
163 message_loop_->RunUntilIdle(); | 151 message_loop_->RunUntilIdle(); |
164 } | 152 } |
165 | 153 |
166 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { | 154 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { |
167 // Execute SetCapture() and StopCapture() for 2 clients in sequence. | 155 // Execute SetCapture() and StopCapture() for 2 clients in sequence. |
168 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); | 156 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); |
169 | 157 |
170 EXPECT_CALL(*client, OnStarted(_)) | 158 EXPECT_CALL(*client, OnStarted(_)) |
171 .WillOnce(Return()); | 159 .WillOnce(Return()); |
172 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_)) | |
173 .WillOnce(Return()); | |
174 | 160 |
175 video_capture_impl_->StartCapture(client.get(), capability_small_); | 161 video_capture_impl_->StartCapture(client.get(), params_small_); |
176 message_loop_->RunUntilIdle(); | 162 message_loop_->RunUntilIdle(); |
177 | 163 |
178 EXPECT_CALL(*client, OnStopped(_)) | 164 EXPECT_CALL(*client, OnStopped(_)) |
179 .WillOnce(Return()); | 165 .WillOnce(Return()); |
180 EXPECT_CALL(*client, OnRemoved(_)) | 166 EXPECT_CALL(*client, OnRemoved(_)) |
181 .WillOnce(Return()); | 167 .WillOnce(Return()); |
182 | 168 |
183 video_capture_impl_->StopCapture(client.get()); | 169 video_capture_impl_->StopCapture(client.get()); |
184 message_loop_->RunUntilIdle(); | 170 message_loop_->RunUntilIdle(); |
185 | 171 |
186 EXPECT_CALL(*client, OnStarted(_)) | 172 EXPECT_CALL(*client, OnStarted(_)) |
187 .WillOnce(Return()); | 173 .WillOnce(Return()); |
188 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_)) | |
189 .WillOnce(Return()); | |
190 | 174 |
191 video_capture_impl_->StartCapture(client.get(), capability_small_); | 175 video_capture_impl_->StartCapture(client.get(), params_small_); |
192 message_loop_->RunUntilIdle(); | 176 message_loop_->RunUntilIdle(); |
193 | 177 |
194 EXPECT_CALL(*client, OnStopped(_)) | 178 EXPECT_CALL(*client, OnStopped(_)) |
195 .WillOnce(Return()); | 179 .WillOnce(Return()); |
196 EXPECT_CALL(*client, OnRemoved(_)) | 180 EXPECT_CALL(*client, OnRemoved(_)) |
197 .WillOnce(Return()); | 181 .WillOnce(Return()); |
198 | 182 |
199 video_capture_impl_->StopCapture(client.get()); | 183 video_capture_impl_->StopCapture(client.get()); |
200 message_loop_->RunUntilIdle(); | 184 message_loop_->RunUntilIdle(); |
201 } | 185 } |
202 | 186 |
203 TEST_F(VideoCaptureImplTest, LargeAndSmall) { | 187 TEST_F(VideoCaptureImplTest, LargeAndSmall) { |
204 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 188 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
205 // The large client starts first and stops first. | 189 // The large client starts first and stops first. |
206 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); | 190 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); |
207 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); | 191 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); |
208 | 192 |
209 EXPECT_CALL(*client_large, OnStarted(_)) | 193 EXPECT_CALL(*client_large, OnStarted(_)) |
210 .WillOnce(Return()); | 194 .WillOnce(Return()); |
211 EXPECT_CALL(*client_large, OnDeviceInfoReceived(_,_)) | |
212 .WillOnce(Return()); | |
213 EXPECT_CALL(*client_small, OnStarted(_)) | 195 EXPECT_CALL(*client_small, OnStarted(_)) |
214 .WillOnce(Return()); | 196 .WillOnce(Return()); |
215 EXPECT_CALL(*client_small, OnDeviceInfoReceived(_,_)) | |
216 .WillOnce(Return()); | |
217 | 197 |
218 video_capture_impl_->StartCapture(client_large.get(), capability_large_); | 198 video_capture_impl_->StartCapture(client_large.get(), params_large_); |
219 video_capture_impl_->StartCapture(client_small.get(), capability_small_); | 199 video_capture_impl_->StartCapture(client_small.get(), params_small_); |
220 message_loop_->RunUntilIdle(); | 200 message_loop_->RunUntilIdle(); |
221 | 201 |
222 EXPECT_CALL(*client_large, OnStopped(_)) | 202 EXPECT_CALL(*client_large, OnStopped(_)) |
223 .WillOnce(Return()); | 203 .WillOnce(Return()); |
224 EXPECT_CALL(*client_large, OnRemoved(_)) | 204 EXPECT_CALL(*client_large, OnRemoved(_)) |
225 .WillOnce(Return()); | 205 .WillOnce(Return()); |
226 EXPECT_CALL(*client_small, OnStopped(_)) | 206 EXPECT_CALL(*client_small, OnStopped(_)) |
227 .WillOnce(Return()); | 207 .WillOnce(Return()); |
228 EXPECT_CALL(*client_small, OnRemoved(_)) | 208 EXPECT_CALL(*client_small, OnRemoved(_)) |
229 .WillOnce(Return()); | 209 .WillOnce(Return()); |
230 | 210 |
231 video_capture_impl_->StopCapture(client_large.get()); | 211 video_capture_impl_->StopCapture(client_large.get()); |
232 video_capture_impl_->StopCapture(client_small.get()); | 212 video_capture_impl_->StopCapture(client_small.get()); |
233 message_loop_->RunUntilIdle(); | 213 message_loop_->RunUntilIdle(); |
234 } | 214 } |
235 | 215 |
236 TEST_F(VideoCaptureImplTest, SmallAndLarge) { | 216 TEST_F(VideoCaptureImplTest, SmallAndLarge) { |
237 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 217 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
238 // The small client starts first and stops first. | 218 // The small client starts first and stops first. |
239 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); | 219 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); |
240 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); | 220 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); |
241 | 221 |
242 EXPECT_CALL(*client_large, OnStarted(_)) | 222 EXPECT_CALL(*client_large, OnStarted(_)) |
243 .WillOnce(Return()); | 223 .WillOnce(Return()); |
244 EXPECT_CALL(*client_large, OnDeviceInfoReceived(_,_)) | |
245 .WillOnce(Return()); | |
246 EXPECT_CALL(*client_small, OnStarted(_)) | 224 EXPECT_CALL(*client_small, OnStarted(_)) |
247 .WillOnce(Return()); | 225 .WillOnce(Return()); |
248 EXPECT_CALL(*client_small, OnDeviceInfoReceived(_,_)) | |
249 .Times(AtLeast(1)) | |
250 .WillRepeatedly(Return()); | |
251 | 226 |
252 video_capture_impl_->StartCapture(client_small.get(), capability_small_); | 227 video_capture_impl_->StartCapture(client_small.get(), params_small_); |
253 video_capture_impl_->StartCapture(client_large.get(), capability_large_); | 228 video_capture_impl_->StartCapture(client_large.get(), params_large_); |
254 message_loop_->RunUntilIdle(); | 229 message_loop_->RunUntilIdle(); |
255 | 230 |
256 EXPECT_CALL(*client_large, OnStopped(_)) | 231 EXPECT_CALL(*client_large, OnStopped(_)) |
257 .WillOnce(Return()); | 232 .WillOnce(Return()); |
258 EXPECT_CALL(*client_large, OnRemoved(_)) | 233 EXPECT_CALL(*client_large, OnRemoved(_)) |
259 .WillOnce(Return()); | 234 .WillOnce(Return()); |
260 EXPECT_CALL(*client_small, OnStopped(_)) | 235 EXPECT_CALL(*client_small, OnStopped(_)) |
261 .WillOnce(Return()); | 236 .WillOnce(Return()); |
262 EXPECT_CALL(*client_small, OnRemoved(_)) | 237 EXPECT_CALL(*client_small, OnRemoved(_)) |
263 .WillOnce(Return()); | 238 .WillOnce(Return()); |
264 | 239 |
265 video_capture_impl_->StopCapture(client_small.get()); | 240 video_capture_impl_->StopCapture(client_small.get()); |
266 video_capture_impl_->StopCapture(client_large.get()); | 241 video_capture_impl_->StopCapture(client_large.get()); |
267 message_loop_->RunUntilIdle(); | 242 message_loop_->RunUntilIdle(); |
268 } | 243 } |
269 | 244 |
270 TEST_F(VideoCaptureImplTest, TwoClientsWithSameSize) { | 245 TEST_F(VideoCaptureImplTest, TwoClientsWithSameSize) { |
271 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 246 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
272 // The client1 starts first and stops first. | 247 // The client1 starts first and stops first. |
273 scoped_ptr<MockVideoCaptureClient> client1(new MockVideoCaptureClient); | 248 scoped_ptr<MockVideoCaptureClient> client1(new MockVideoCaptureClient); |
274 scoped_ptr<MockVideoCaptureClient> client2(new MockVideoCaptureClient); | 249 scoped_ptr<MockVideoCaptureClient> client2(new MockVideoCaptureClient); |
275 | 250 |
276 EXPECT_CALL(*client1, OnStarted(_)) | 251 EXPECT_CALL(*client1, OnStarted(_)) |
277 .WillOnce(Return()); | 252 .WillOnce(Return()); |
278 EXPECT_CALL(*client1, OnDeviceInfoReceived(_,_)) | |
279 .WillOnce(Return()); | |
280 EXPECT_CALL(*client2, OnStarted(_)) | 253 EXPECT_CALL(*client2, OnStarted(_)) |
281 .WillOnce(Return()); | 254 .WillOnce(Return()); |
282 EXPECT_CALL(*client2, OnDeviceInfoReceived(_,_)) | |
283 .WillOnce(Return()); | |
284 | 255 |
285 video_capture_impl_->StartCapture(client1.get(), capability_small_); | 256 video_capture_impl_->StartCapture(client1.get(), params_small_); |
286 video_capture_impl_->StartCapture(client2.get(), capability_small_); | 257 video_capture_impl_->StartCapture(client2.get(), params_small_); |
287 message_loop_->RunUntilIdle(); | 258 message_loop_->RunUntilIdle(); |
288 | 259 |
289 EXPECT_CALL(*client1, OnStopped(_)) | 260 EXPECT_CALL(*client1, OnStopped(_)) |
290 .WillOnce(Return()); | 261 .WillOnce(Return()); |
291 EXPECT_CALL(*client1, OnRemoved(_)) | 262 EXPECT_CALL(*client1, OnRemoved(_)) |
292 .WillOnce(Return()); | 263 .WillOnce(Return()); |
293 EXPECT_CALL(*client2, OnStopped(_)) | 264 EXPECT_CALL(*client2, OnStopped(_)) |
294 .WillOnce(Return()); | 265 .WillOnce(Return()); |
295 EXPECT_CALL(*client2, OnRemoved(_)) | 266 EXPECT_CALL(*client2, OnRemoved(_)) |
296 .WillOnce(Return()); | 267 .WillOnce(Return()); |
297 | 268 |
298 video_capture_impl_->StopCapture(client1.get()); | 269 video_capture_impl_->StopCapture(client1.get()); |
299 video_capture_impl_->StopCapture(client2.get()); | 270 video_capture_impl_->StopCapture(client2.get()); |
300 message_loop_->RunUntilIdle(); | 271 message_loop_->RunUntilIdle(); |
301 } | 272 } |
302 | 273 |
303 } // namespace content | 274 } // namespace content |
OLD | NEW |