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 // Unit test for VideoCaptureManager. | 5 // Unit test for VideoCaptureManager. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 : devices_() { | 36 : devices_() { |
37 } | 37 } |
38 ~MockMediaStreamProviderListener() {} | 38 ~MockMediaStreamProviderListener() {} |
39 | 39 |
40 MOCK_METHOD2(Opened, void(MediaStreamType, int)); | 40 MOCK_METHOD2(Opened, void(MediaStreamType, int)); |
41 MOCK_METHOD2(Closed, void(MediaStreamType, int)); | 41 MOCK_METHOD2(Closed, void(MediaStreamType, int)); |
42 MOCK_METHOD1(DevicesEnumerated, void(const StreamDeviceInfoArray&)); | 42 MOCK_METHOD1(DevicesEnumerated, void(const StreamDeviceInfoArray&)); |
43 MOCK_METHOD3(Error, void(MediaStreamType, int, | 43 MOCK_METHOD3(Error, void(MediaStreamType, int, |
44 MediaStreamProviderError)); | 44 MediaStreamProviderError)); |
45 | 45 |
46 virtual void DevicesEnumerated(MediaStreamType stream_type, | 46 virtual void DevicesEnumerated( |
47 const StreamDeviceInfoArray& devices) { | 47 MediaStreamType stream_type, |
| 48 const StreamDeviceInfoArray& devices) OVERRIDE { |
48 devices_.clear(); | 49 devices_.clear(); |
49 for (StreamDeviceInfoArray::const_iterator it = devices.begin(); | 50 for (StreamDeviceInfoArray::const_iterator it = devices.begin(); |
50 it != devices.end(); | 51 it != devices.end(); |
51 ++it) { | 52 ++it) { |
52 devices_.push_back(*it); | 53 devices_.push_back(*it); |
53 } | 54 } |
54 DevicesEnumerated(devices); | 55 DevicesEnumerated(devices); |
55 } | 56 } |
56 | 57 |
57 media_stream::StreamDeviceInfoArray devices_; | 58 media_stream::StreamDeviceInfoArray devices_; |
58 }; // class MockMediaStreamProviderListener | 59 }; // class MockMediaStreamProviderListener |
59 | 60 |
60 } // namespace media_stream | 61 } // namespace media_stream |
61 | 62 |
62 namespace { | 63 namespace { |
63 | 64 |
64 // Needed as an input argument to Start(). | 65 // Needed as an input argument to Start(). |
65 class MockFrameObserver: public media::VideoCaptureDevice::EventHandler { | 66 class MockFrameObserver : public media::VideoCaptureDevice::EventHandler { |
66 public: | 67 public: |
67 virtual void OnError() {} | 68 virtual void OnError() OVERRIDE {} |
68 void OnFrameInfo(const media::VideoCaptureCapability& info) {} | 69 void OnFrameInfo(const media::VideoCaptureCapability& info) {} |
69 virtual void OnIncomingCapturedFrame(const uint8* data, int length, | 70 virtual void OnIncomingCapturedFrame(const uint8* data, int length, |
70 base::Time timestamp) {} | 71 base::Time timestamp) OVERRIDE {} |
71 }; | 72 }; |
72 | 73 |
73 // Test class | 74 // Test class |
74 class VideoCaptureManagerTest : public testing::Test { | 75 class VideoCaptureManagerTest : public testing::Test { |
75 public: | 76 public: |
76 VideoCaptureManagerTest() | 77 VideoCaptureManagerTest() {} |
77 : vcm_(), | |
78 listener_(), | |
79 message_loop_(), | |
80 io_thread_(), | |
81 frame_observer_() { | |
82 } | |
83 virtual ~VideoCaptureManagerTest() {} | 78 virtual ~VideoCaptureManagerTest() {} |
84 | 79 |
85 protected: | 80 protected: |
86 virtual void SetUp() { | 81 virtual void SetUp() OVERRIDE { |
87 listener_.reset(new media_stream::MockMediaStreamProviderListener()); | 82 listener_.reset(new media_stream::MockMediaStreamProviderListener()); |
88 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 83 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
89 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 84 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
90 message_loop_.get())); | 85 message_loop_.get())); |
91 vcm_ = new media_stream::VideoCaptureManager(); | 86 vcm_ = new media_stream::VideoCaptureManager(); |
92 vcm_->UseFakeDevice(); | 87 vcm_->UseFakeDevice(); |
93 vcm_->Register(listener_.get()); | 88 vcm_->Register(listener_.get(), message_loop_->message_loop_proxy()); |
94 frame_observer_.reset(new MockFrameObserver()); | 89 frame_observer_.reset(new MockFrameObserver()); |
95 } | 90 } |
96 | 91 |
97 virtual void TearDown() { | 92 virtual void TearDown() OVERRIDE {} |
98 io_thread_.reset(); | |
99 } | |
100 | 93 |
101 // Called on the VideoCaptureManager thread. | |
102 static void PostQuitMessageLoop(MessageLoop* message_loop) { | |
103 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
104 } | |
105 | |
106 // Called on the main thread. | |
107 static void PostQuitOnVideoCaptureManagerThread( | |
108 MessageLoop* message_loop, media_stream::VideoCaptureManager* vcm) { | |
109 vcm->GetMessageLoop()->PostTask( | |
110 FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop)); | |
111 } | |
112 | |
113 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | |
114 // video_capture_manager internal thread are executed while also processing | |
115 // pending task in message_loop_ on the current thread. It is used to | |
116 // synchronize with the video capture manager thread when we are stopping a | |
117 // video capture device. | |
118 void SyncWithVideoCaptureManagerThread() { | |
119 message_loop_->PostTask( | |
120 FROM_HERE, base::Bind(&PostQuitOnVideoCaptureManagerThread, | |
121 message_loop_.get(), | |
122 vcm_)); | |
123 message_loop_->Run(); | |
124 } | |
125 scoped_refptr<media_stream::VideoCaptureManager> vcm_; | 94 scoped_refptr<media_stream::VideoCaptureManager> vcm_; |
126 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; | 95 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; |
127 scoped_ptr<MessageLoop> message_loop_; | 96 scoped_ptr<MessageLoop> message_loop_; |
128 scoped_ptr<BrowserThreadImpl> io_thread_; | 97 scoped_ptr<BrowserThreadImpl> io_thread_; |
129 scoped_ptr<MockFrameObserver> frame_observer_; | 98 scoped_ptr<MockFrameObserver> frame_observer_; |
130 | 99 |
131 private: | 100 private: |
132 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); | 101 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); |
133 }; | 102 }; |
134 | 103 |
135 // Test cases | 104 // Test cases |
136 | 105 |
137 // Try to open, start, stop and close a device. | 106 // Try to open, start, stop and close a device. |
138 TEST_F(VideoCaptureManagerTest, CreateAndClose) { | 107 TEST_F(VideoCaptureManagerTest, CreateAndClose) { |
139 InSequence s; | 108 InSequence s; |
140 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 109 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
141 .Times(1); | 110 .Times(1); |
142 EXPECT_CALL(*listener_, | 111 EXPECT_CALL(*listener_, |
143 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 112 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
144 .Times(1); | 113 .Times(1); |
145 EXPECT_CALL(*listener_, | 114 EXPECT_CALL(*listener_, |
146 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 115 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
147 .Times(1); | 116 .Times(1); |
148 | 117 |
149 vcm_->EnumerateDevices(); | 118 vcm_->EnumerateDevices(); |
150 | 119 |
151 // Wait to get device callback. | 120 // Wait to get device callback. |
152 SyncWithVideoCaptureManagerThread(); | 121 message_loop_->RunAllPending(); |
153 | 122 |
154 int video_session_id = vcm_->Open(listener_->devices_.front()); | 123 int video_session_id = vcm_->Open(listener_->devices_.front()); |
155 | 124 |
156 media::VideoCaptureParams capture_params; | 125 media::VideoCaptureParams capture_params; |
157 capture_params.session_id = video_session_id; | 126 capture_params.session_id = video_session_id; |
158 capture_params.width = 320; | 127 capture_params.width = 320; |
159 capture_params.height = 240; | 128 capture_params.height = 240; |
160 capture_params.frame_per_second = 30; | 129 capture_params.frame_per_second = 30; |
161 vcm_->Start(capture_params, frame_observer_.get()); | 130 vcm_->Start(capture_params, frame_observer_.get()); |
162 | 131 |
163 vcm_->Stop(video_session_id, base::Closure()); | 132 vcm_->Stop(video_session_id, base::Closure()); |
164 vcm_->Close(video_session_id); | 133 vcm_->Close(video_session_id); |
165 | 134 |
166 // Wait to check callbacks before removing the listener. | 135 // Wait to check callbacks before removing the listener. |
167 SyncWithVideoCaptureManagerThread(); | 136 message_loop_->RunAllPending(); |
168 vcm_->Unregister(); | 137 vcm_->Unregister(); |
169 } | 138 } |
170 | 139 |
171 // Open the same device twice. | 140 // Open the same device twice. |
172 TEST_F(VideoCaptureManagerTest, OpenTwice) { | 141 TEST_F(VideoCaptureManagerTest, OpenTwice) { |
173 InSequence s; | 142 InSequence s; |
174 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 143 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
175 .Times(1); | 144 .Times(1); |
176 EXPECT_CALL(*listener_, | 145 EXPECT_CALL(*listener_, |
177 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 146 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
178 .Times(2); | 147 .Times(2); |
179 EXPECT_CALL(*listener_, | 148 EXPECT_CALL(*listener_, |
180 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 149 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
181 .Times(2); | 150 .Times(2); |
182 | 151 |
183 vcm_->EnumerateDevices(); | 152 vcm_->EnumerateDevices(); |
184 | 153 |
185 // Wait to get device callback. | 154 // Wait to get device callback. |
186 SyncWithVideoCaptureManagerThread(); | 155 message_loop_->RunAllPending(); |
187 | 156 |
188 int video_session_id_first = vcm_->Open(listener_->devices_.front()); | 157 int video_session_id_first = vcm_->Open(listener_->devices_.front()); |
189 | 158 |
190 // This should trigger an error callback with error code | 159 // This should trigger an error callback with error code |
191 // 'kDeviceAlreadyInUse'. | 160 // 'kDeviceAlreadyInUse'. |
192 int video_session_id_second = vcm_->Open(listener_->devices_.front()); | 161 int video_session_id_second = vcm_->Open(listener_->devices_.front()); |
193 EXPECT_NE(video_session_id_first, video_session_id_second); | 162 EXPECT_NE(video_session_id_first, video_session_id_second); |
194 | 163 |
195 vcm_->Close(video_session_id_first); | 164 vcm_->Close(video_session_id_first); |
196 vcm_->Close(video_session_id_second); | 165 vcm_->Close(video_session_id_second); |
197 | 166 |
198 // Wait to check callbacks before removing the listener. | 167 // Wait to check callbacks before removing the listener. |
199 SyncWithVideoCaptureManagerThread(); | 168 message_loop_->RunAllPending(); |
200 vcm_->Unregister(); | 169 vcm_->Unregister(); |
201 } | 170 } |
202 | 171 |
203 // Open two different devices. | 172 // Open two different devices. |
204 TEST_F(VideoCaptureManagerTest, OpenTwo) { | 173 TEST_F(VideoCaptureManagerTest, OpenTwo) { |
205 InSequence s; | 174 InSequence s; |
206 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 175 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
207 .Times(1); | 176 .Times(1); |
208 EXPECT_CALL(*listener_, | 177 EXPECT_CALL(*listener_, |
209 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 178 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
210 .Times(2); | 179 .Times(2); |
211 EXPECT_CALL(*listener_, | 180 EXPECT_CALL(*listener_, |
212 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 181 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
213 .Times(2); | 182 .Times(2); |
214 | 183 |
215 vcm_->EnumerateDevices(); | 184 vcm_->EnumerateDevices(); |
216 | 185 |
217 // Wait to get device callback. | 186 // Wait to get device callback. |
218 SyncWithVideoCaptureManagerThread(); | 187 message_loop_->RunAllPending(); |
219 | 188 |
220 media_stream::StreamDeviceInfoArray::iterator it = | 189 media_stream::StreamDeviceInfoArray::iterator it = |
221 listener_->devices_.begin(); | 190 listener_->devices_.begin(); |
222 | 191 |
223 int video_session_id_first = vcm_->Open(*it); | 192 int video_session_id_first = vcm_->Open(*it); |
224 ++it; | 193 ++it; |
225 int video_session_id_second = vcm_->Open(*it); | 194 int video_session_id_second = vcm_->Open(*it); |
226 | 195 |
227 vcm_->Close(video_session_id_first); | 196 vcm_->Close(video_session_id_first); |
228 vcm_->Close(video_session_id_second); | 197 vcm_->Close(video_session_id_second); |
229 | 198 |
230 // Wait to check callbacks before removing the listener. | 199 // Wait to check callbacks before removing the listener. |
231 SyncWithVideoCaptureManagerThread(); | 200 message_loop_->RunAllPending(); |
232 vcm_->Unregister(); | 201 vcm_->Unregister(); |
233 } | 202 } |
234 | 203 |
235 // Try open a non-existing device. | 204 // Try open a non-existing device. |
236 TEST_F(VideoCaptureManagerTest, OpenNotExisting) { | 205 TEST_F(VideoCaptureManagerTest, OpenNotExisting) { |
237 InSequence s; | 206 InSequence s; |
238 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 207 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
239 .Times(1); | 208 .Times(1); |
240 EXPECT_CALL(*listener_, Error(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, | 209 EXPECT_CALL(*listener_, Error(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, |
241 _, media_stream::kDeviceNotAvailable)) | 210 _, media_stream::kDeviceNotAvailable)) |
242 .Times(1); | 211 .Times(1); |
243 | 212 |
244 vcm_->EnumerateDevices(); | 213 vcm_->EnumerateDevices(); |
245 | 214 |
246 // Wait to get device callback. | 215 // Wait to get device callback. |
247 SyncWithVideoCaptureManagerThread(); | 216 message_loop_->RunAllPending(); |
248 | 217 |
249 media_stream::MediaStreamType stream_type = | 218 media_stream::MediaStreamType stream_type = |
250 content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE; | 219 content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE; |
251 std::string device_name("device_doesnt_exist"); | 220 std::string device_name("device_doesnt_exist"); |
252 std::string device_id("id_doesnt_exist"); | 221 std::string device_id("id_doesnt_exist"); |
253 media_stream::StreamDeviceInfo dummy_device(stream_type, device_name, | 222 media_stream::StreamDeviceInfo dummy_device(stream_type, device_name, |
254 device_id, false); | 223 device_id, false); |
255 | 224 |
256 // This should fail with error code 'kDeviceNotAvailable'. | 225 // This should fail with error code 'kDeviceNotAvailable'. |
257 vcm_->Open(dummy_device); | 226 vcm_->Open(dummy_device); |
258 | 227 |
259 // Wait to check callbacks before removing the listener. | 228 // Wait to check callbacks before removing the listener. |
260 SyncWithVideoCaptureManagerThread(); | 229 message_loop_->RunAllPending(); |
261 vcm_->Unregister(); | 230 vcm_->Unregister(); |
262 } | 231 } |
263 | 232 |
264 // Start a device using "magic" id, i.e. call Start without calling Open. | 233 // Start a device using "magic" id, i.e. call Start without calling Open. |
265 TEST_F(VideoCaptureManagerTest, StartUsingId) { | 234 TEST_F(VideoCaptureManagerTest, StartUsingId) { |
266 InSequence s; | 235 InSequence s; |
267 EXPECT_CALL(*listener_, | 236 EXPECT_CALL(*listener_, |
268 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 237 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
269 .Times(1); | 238 .Times(1); |
270 EXPECT_CALL(*listener_, | 239 EXPECT_CALL(*listener_, |
271 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 240 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
272 .Times(1); | 241 .Times(1); |
273 | 242 |
274 media::VideoCaptureParams capture_params; | 243 media::VideoCaptureParams capture_params; |
275 capture_params.session_id = | 244 capture_params.session_id = |
276 media_stream::VideoCaptureManager::kStartOpenSessionId; | 245 media_stream::VideoCaptureManager::kStartOpenSessionId; |
277 capture_params.width = 320; | 246 capture_params.width = 320; |
278 capture_params.height = 240; | 247 capture_params.height = 240; |
279 capture_params.frame_per_second = 30; | 248 capture_params.frame_per_second = 30; |
280 | 249 |
281 // Start shall trigger the Open callback. | 250 // Start shall trigger the Open callback. |
282 vcm_->Start(capture_params, frame_observer_.get()); | 251 vcm_->Start(capture_params, frame_observer_.get()); |
283 | 252 |
284 // Stop shall trigger the Close callback | 253 // Stop shall trigger the Close callback |
285 vcm_->Stop(media_stream::VideoCaptureManager::kStartOpenSessionId, | 254 vcm_->Stop(media_stream::VideoCaptureManager::kStartOpenSessionId, |
286 base::Closure()); | 255 base::Closure()); |
287 | 256 |
288 // Wait to check callbacks before removing the listener. | 257 // Wait to check callbacks before removing the listener. |
289 SyncWithVideoCaptureManagerThread(); | 258 message_loop_->RunAllPending(); |
290 vcm_->Unregister(); | 259 vcm_->Unregister(); |
291 } | 260 } |
292 | 261 |
293 // Open and start a device, close it before calling Stop. | 262 // Open and start a device, close it before calling Stop. |
294 TEST_F(VideoCaptureManagerTest, CloseWithoutStop) { | 263 TEST_F(VideoCaptureManagerTest, CloseWithoutStop) { |
295 InSequence s; | 264 InSequence s; |
296 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 265 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
297 .Times(1); | 266 .Times(1); |
298 EXPECT_CALL(*listener_, | 267 EXPECT_CALL(*listener_, |
299 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 268 Opened(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
300 .Times(1); | 269 .Times(1); |
301 EXPECT_CALL(*listener_, | 270 EXPECT_CALL(*listener_, |
302 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) | 271 Closed(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, _)) |
303 .Times(1); | 272 .Times(1); |
304 | 273 |
305 vcm_->EnumerateDevices(); | 274 vcm_->EnumerateDevices(); |
306 | 275 |
307 // Wait to get device callback. | 276 // Wait to get device callback. |
308 SyncWithVideoCaptureManagerThread(); | 277 message_loop_->RunAllPending(); |
309 | 278 |
310 int video_session_id = vcm_->Open(listener_->devices_.front()); | 279 int video_session_id = vcm_->Open(listener_->devices_.front()); |
311 | 280 |
312 media::VideoCaptureParams capture_params; | 281 media::VideoCaptureParams capture_params; |
313 capture_params.session_id = video_session_id; | 282 capture_params.session_id = video_session_id; |
314 capture_params.width = 320; | 283 capture_params.width = 320; |
315 capture_params.height = 240; | 284 capture_params.height = 240; |
316 capture_params.frame_per_second = 30; | 285 capture_params.frame_per_second = 30; |
317 vcm_->Start(capture_params, frame_observer_.get()); | 286 vcm_->Start(capture_params, frame_observer_.get()); |
318 | 287 |
319 // Close will stop the running device, an assert will be triggered in | 288 // Close will stop the running device, an assert will be triggered in |
320 // VideoCaptureManager destructor otherwise. | 289 // VideoCaptureManager destructor otherwise. |
321 vcm_->Close(video_session_id); | 290 vcm_->Close(video_session_id); |
322 vcm_->Stop(video_session_id, base::Closure()); | 291 vcm_->Stop(video_session_id, base::Closure()); |
323 | 292 |
324 // Wait to check callbacks before removing the listener | 293 // Wait to check callbacks before removing the listener |
325 SyncWithVideoCaptureManagerThread(); | 294 message_loop_->RunAllPending(); |
326 vcm_->Unregister(); | 295 vcm_->Unregister(); |
327 } | 296 } |
328 | 297 |
329 } // namespace | 298 } // namespace |
OLD | NEW |