OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 virtual void OnError() {} | 62 virtual void OnError() {} |
63 void OnFrameInfo(const media::VideoCaptureDevice::Capability& info) {} | 63 void OnFrameInfo(const media::VideoCaptureDevice::Capability& info) {} |
64 virtual void OnIncomingCapturedFrame(const uint8* data, int length, | 64 virtual void OnIncomingCapturedFrame(const uint8* data, int length, |
65 base::Time timestamp) {} | 65 base::Time timestamp) {} |
66 }; | 66 }; |
67 | 67 |
68 // Test class | 68 // Test class |
69 class VideoCaptureManagerTest : public testing::Test { | 69 class VideoCaptureManagerTest : public testing::Test { |
70 public: | 70 public: |
71 VideoCaptureManagerTest() | 71 VideoCaptureManagerTest() |
72 : listener_(), | 72 : vcm_(), |
| 73 listener_(), |
73 message_loop_(), | 74 message_loop_(), |
74 io_thread_(), | 75 io_thread_(), |
75 frame_observer_() { | 76 frame_observer_() { |
76 } | 77 } |
77 virtual ~VideoCaptureManagerTest() {} | 78 virtual ~VideoCaptureManagerTest() {} |
78 | 79 |
79 protected: | 80 protected: |
80 virtual void SetUp() { | 81 virtual void SetUp() { |
81 listener_.reset(new media_stream::MockMediaStreamProviderListener()); | 82 listener_.reset(new media_stream::MockMediaStreamProviderListener()); |
82 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 83 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
83 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); | 84 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); |
| 85 vcm_.reset(new media_stream::VideoCaptureManager()); |
| 86 vcm_->UseFakeDevice(); |
| 87 vcm_->Register(listener_.get()); |
84 frame_observer_.reset(new MockFrameObserver()); | 88 frame_observer_.reset(new MockFrameObserver()); |
85 } | 89 } |
86 | 90 |
87 virtual void TearDown() { | 91 virtual void TearDown() { |
88 io_thread_.reset(); | 92 io_thread_.reset(); |
89 } | 93 } |
90 | 94 |
91 // Called on the VideoCaptureManager thread. | 95 // Called on the VideoCaptureManager thread. |
92 static void PostQuitMessageLoop(MessageLoop* message_loop) { | 96 static void PostQuitMessageLoop(MessageLoop* message_loop) { |
93 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 97 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
94 } | 98 } |
95 | 99 |
96 // Called on the main thread. | 100 // Called on the main thread. |
97 static void PostQuitOnVideoCaptureManagerThread(MessageLoop* message_loop) { | 101 static void PostQuitOnVideoCaptureManagerThread( |
98 media_stream::VideoCaptureManager::Get()->GetMessageLoop()->PostTask( | 102 MessageLoop* message_loop, media_stream::VideoCaptureManager* vcm) { |
| 103 vcm->GetMessageLoop()->PostTask( |
99 FROM_HERE, NewRunnableFunction(&PostQuitMessageLoop, message_loop)); | 104 FROM_HERE, NewRunnableFunction(&PostQuitMessageLoop, message_loop)); |
100 } | 105 } |
101 | 106 |
102 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | 107 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the |
103 // video_capture_manager internal thread are executed while also processing | 108 // video_capture_manager internal thread are executed while also processing |
104 // pending task in message_loop_ on the current thread. It is used to | 109 // pending task in message_loop_ on the current thread. It is used to |
105 // synchronize with the video capture manager thread when we are stopping a | 110 // synchronize with the video capture manager thread when we are stopping a |
106 // video capture device. | 111 // video capture device. |
107 void SyncWithVideoCaptureManagerThread() { | 112 void SyncWithVideoCaptureManagerThread() { |
108 message_loop_->PostTask( | 113 message_loop_->PostTask( |
109 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, | 114 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, |
110 message_loop_.get())); | 115 message_loop_.get(), |
| 116 vcm_.get())); |
111 message_loop_->Run(); | 117 message_loop_->Run(); |
112 } | 118 } |
| 119 scoped_ptr<media_stream::VideoCaptureManager> vcm_; |
113 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; | 120 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; |
114 scoped_ptr<MessageLoop> message_loop_; | 121 scoped_ptr<MessageLoop> message_loop_; |
115 scoped_ptr<BrowserThread> io_thread_; | 122 scoped_ptr<BrowserThread> io_thread_; |
116 scoped_ptr<MockFrameObserver> frame_observer_; | 123 scoped_ptr<MockFrameObserver> frame_observer_; |
117 | 124 |
118 private: | 125 private: |
119 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); | 126 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); |
120 }; | 127 }; |
121 | 128 |
122 // Test cases | 129 // Test cases |
123 | 130 |
124 // Try to open, start, stop and close a device. | 131 // Try to open, start, stop and close a device. |
125 TEST_F(VideoCaptureManagerTest, CreateAndClose) { | 132 TEST_F(VideoCaptureManagerTest, CreateAndClose) { |
126 InSequence s; | 133 InSequence s; |
127 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 134 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
128 .Times(1); | 135 .Times(1); |
129 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) | 136 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) |
130 .Times(1); | 137 .Times(1); |
131 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) | 138 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) |
132 .Times(1); | 139 .Times(1); |
133 | 140 |
134 media_stream::VideoCaptureManager* vcm = | 141 vcm_->EnumerateDevices(); |
135 media_stream::VideoCaptureManager::Get(); | |
136 // Make sure fake devices are used | |
137 vcm->UseFakeDevice(); | |
138 vcm->Register(listener_.get()); | |
139 vcm->EnumerateDevices(); | |
140 | 142 |
141 // Wait to get device callback... | 143 // Wait to get device callback... |
142 SyncWithVideoCaptureManagerThread(); | 144 SyncWithVideoCaptureManagerThread(); |
143 | 145 |
144 int video_session_id = vcm->Open(listener_->devices_.front()); | 146 int video_session_id = vcm_->Open(listener_->devices_.front()); |
145 | 147 |
146 media::VideoCaptureParams capture_params; | 148 media::VideoCaptureParams capture_params; |
147 capture_params.session_id = video_session_id; | 149 capture_params.session_id = video_session_id; |
148 capture_params.width = 320; | 150 capture_params.width = 320; |
149 capture_params.height = 240; | 151 capture_params.height = 240; |
150 capture_params.frame_per_second = 30; | 152 capture_params.frame_per_second = 30; |
151 vcm->Start(capture_params, frame_observer_.get()); | 153 vcm_->Start(capture_params, frame_observer_.get()); |
152 | 154 |
153 vcm->Stop(video_session_id, NULL); | 155 vcm_->Stop(video_session_id, NULL); |
154 vcm->Close(video_session_id); | 156 vcm_->Close(video_session_id); |
155 | 157 |
156 // Wait to check callbacks before removing the listener | 158 // Wait to check callbacks before removing the listener |
157 SyncWithVideoCaptureManagerThread(); | 159 SyncWithVideoCaptureManagerThread(); |
158 vcm->Unregister(); | 160 vcm_->Unregister(); |
159 } | 161 } |
160 | 162 |
161 // Open the same device twice, should fail. | 163 // Open the same device twice, should fail. |
162 TEST_F(VideoCaptureManagerTest, OpenTwice) { | 164 TEST_F(VideoCaptureManagerTest, OpenTwice) { |
163 InSequence s; | 165 InSequence s; |
164 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 166 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
165 .Times(1); | 167 .Times(1); |
166 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) | 168 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) |
167 .Times(1); | 169 .Times(1); |
168 EXPECT_CALL(*listener_, Error(media_stream::kVideoCapture, _, | 170 EXPECT_CALL(*listener_, Error(media_stream::kVideoCapture, _, |
169 media_stream::kDeviceAlreadyInUse)) | 171 media_stream::kDeviceAlreadyInUse)) |
170 .Times(1); | 172 .Times(1); |
171 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) | 173 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) |
172 .Times(1); | 174 .Times(1); |
173 | 175 |
174 media_stream::VideoCaptureManager* vcm = | 176 vcm_->EnumerateDevices(); |
175 media_stream::VideoCaptureManager::Get(); | |
176 // Make sure fake devices are used | |
177 vcm->UseFakeDevice(); | |
178 vcm->Register(listener_.get()); | |
179 vcm->EnumerateDevices(); | |
180 | 177 |
181 // Wait to get device callback... | 178 // Wait to get device callback... |
182 SyncWithVideoCaptureManagerThread(); | 179 SyncWithVideoCaptureManagerThread(); |
183 | 180 |
184 int video_session_id = vcm->Open(listener_->devices_.front()); | 181 int video_session_id = vcm_->Open(listener_->devices_.front()); |
185 | 182 |
186 // This should trigger an error callback with error code 'kDeviceAlreadyInUse' | 183 // This should trigger an error callback with error code 'kDeviceAlreadyInUse' |
187 vcm->Open(listener_->devices_.front()); | 184 vcm_->Open(listener_->devices_.front()); |
188 | 185 |
189 vcm->Close(video_session_id); | 186 vcm_->Close(video_session_id); |
190 | 187 |
191 // Wait to check callbacks before removing the listener | 188 // Wait to check callbacks before removing the listener |
192 SyncWithVideoCaptureManagerThread(); | 189 SyncWithVideoCaptureManagerThread(); |
193 vcm->Unregister(); | 190 vcm_->Unregister(); |
194 } | 191 } |
195 | 192 |
196 // Open two different devices. | 193 // Open two different devices. |
197 TEST_F(VideoCaptureManagerTest, OpenTwo) { | 194 TEST_F(VideoCaptureManagerTest, OpenTwo) { |
198 InSequence s; | 195 InSequence s; |
199 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 196 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
200 .Times(1); | 197 .Times(1); |
201 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) | 198 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) |
202 .Times(2); | 199 .Times(2); |
203 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) | 200 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) |
204 .Times(2); | 201 .Times(2); |
205 | 202 |
206 media_stream::VideoCaptureManager* vcm = | 203 vcm_->EnumerateDevices(); |
207 media_stream::VideoCaptureManager::Get(); | |
208 // Make sure fake devices are used | |
209 vcm->UseFakeDevice(); | |
210 vcm->Register(listener_.get()); | |
211 vcm->EnumerateDevices(); | |
212 | 204 |
213 // Wait to get device callback... | 205 // Wait to get device callback... |
214 SyncWithVideoCaptureManagerThread(); | 206 SyncWithVideoCaptureManagerThread(); |
215 | 207 |
216 media_stream::StreamDeviceInfoArray::iterator it = | 208 media_stream::StreamDeviceInfoArray::iterator it = |
217 listener_->devices_.begin(); | 209 listener_->devices_.begin(); |
218 | 210 |
219 int video_session_id_first = vcm->Open(*it); | 211 int video_session_id_first = vcm_->Open(*it); |
220 | 212 |
221 // This should trigger an error callback with error code 'kDeviceAlreadyInUse' | 213 // This should trigger an error callback with error code 'kDeviceAlreadyInUse' |
222 ++it; | 214 ++it; |
223 int video_session_id_second = vcm->Open(*it); | 215 int video_session_id_second = vcm_->Open(*it); |
224 | 216 |
225 vcm->Close(video_session_id_first); | 217 vcm_->Close(video_session_id_first); |
226 vcm->Close(video_session_id_second); | 218 vcm_->Close(video_session_id_second); |
227 | 219 |
228 // Wait to check callbacks before removing the listener | 220 // Wait to check callbacks before removing the listener |
229 SyncWithVideoCaptureManagerThread(); | 221 SyncWithVideoCaptureManagerThread(); |
230 vcm->Unregister(); | 222 vcm_->Unregister(); |
231 } | 223 } |
232 | 224 |
233 // Try open a non-existing device. | 225 // Try open a non-existing device. |
234 TEST_F(VideoCaptureManagerTest, OpenNotExisting) { | 226 TEST_F(VideoCaptureManagerTest, OpenNotExisting) { |
235 InSequence s; | 227 InSequence s; |
236 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | 228 EXPECT_CALL(*listener_, DevicesEnumerated(_)) |
237 .Times(1); | 229 .Times(1); |
238 EXPECT_CALL(*listener_, Error(media_stream::kVideoCapture, _, | 230 EXPECT_CALL(*listener_, Error(media_stream::kVideoCapture, _, |
239 media_stream::kDeviceNotAvailable)) | 231 media_stream::kDeviceNotAvailable)) |
240 .Times(1); | 232 .Times(1); |
241 | 233 |
242 media_stream::VideoCaptureManager* vcm = | 234 vcm_->EnumerateDevices(); |
243 media_stream::VideoCaptureManager::Get(); | |
244 // Make sure fake devices are used | |
245 vcm->UseFakeDevice(); | |
246 vcm->Register(listener_.get()); | |
247 vcm->EnumerateDevices(); | |
248 | 235 |
249 // Wait to get device callback... | 236 // Wait to get device callback... |
250 SyncWithVideoCaptureManagerThread(); | 237 SyncWithVideoCaptureManagerThread(); |
251 | 238 |
252 media_stream::MediaStreamType stream_type = media_stream::kVideoCapture; | 239 media_stream::MediaStreamType stream_type = media_stream::kVideoCapture; |
253 std::string device_name("device_doesnt_exist"); | 240 std::string device_name("device_doesnt_exist"); |
254 std::string device_id("id_doesnt_exist"); | 241 std::string device_id("id_doesnt_exist"); |
255 media_stream::StreamDeviceInfo dummy_device(stream_type, device_name, | 242 media_stream::StreamDeviceInfo dummy_device(stream_type, device_name, |
256 device_id, false); | 243 device_id, false); |
257 | 244 |
258 // This should fail with error code 'kDeviceNotAvailable' | 245 // This should fail with error code 'kDeviceNotAvailable' |
259 vcm->Open(dummy_device); | 246 vcm_->Open(dummy_device); |
260 | 247 |
261 // Wait to check callbacks before removing the listener | 248 // Wait to check callbacks before removing the listener |
262 SyncWithVideoCaptureManagerThread(); | 249 SyncWithVideoCaptureManagerThread(); |
263 vcm->Unregister(); | 250 vcm_->Unregister(); |
264 } | 251 } |
265 | 252 |
266 // Start a device using "magic" id, i.e. call Start without calling Open. | 253 // Start a device using "magic" id, i.e. call Start without calling Open. |
267 TEST_F(VideoCaptureManagerTest, StartUsingId) { | 254 TEST_F(VideoCaptureManagerTest, StartUsingId) { |
268 InSequence s; | 255 InSequence s; |
269 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) | 256 EXPECT_CALL(*listener_, Opened(media_stream::kVideoCapture, _)) |
270 .Times(1); | 257 .Times(1); |
271 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) | 258 EXPECT_CALL(*listener_, Closed(media_stream::kVideoCapture, _)) |
272 .Times(1); | 259 .Times(1); |
273 | 260 |
274 media_stream::VideoCaptureManager* vcm = | |
275 media_stream::VideoCaptureManager::Get(); | |
276 // Make sure fake devices are used | |
277 vcm->UseFakeDevice(); | |
278 vcm->Register(listener_.get()); | |
279 | |
280 media::VideoCaptureParams capture_params; | 261 media::VideoCaptureParams capture_params; |
281 capture_params.session_id = | 262 capture_params.session_id = |
282 media_stream::VideoCaptureManager::kStartOpenSessionId; | 263 media_stream::VideoCaptureManager::kStartOpenSessionId; |
283 capture_params.width = 320; | 264 capture_params.width = 320; |
284 capture_params.height = 240; | 265 capture_params.height = 240; |
285 capture_params.frame_per_second = 30; | 266 capture_params.frame_per_second = 30; |
286 // Start shall trigger the Open callback | 267 // Start shall trigger the Open callback |
287 vcm->Start(capture_params, frame_observer_.get()); | 268 vcm_->Start(capture_params, frame_observer_.get()); |
288 | 269 |
289 // Stop shall trigger the Close callback | 270 // Stop shall trigger the Close callback |
290 vcm->Stop(media_stream::VideoCaptureManager::kStartOpenSessionId, NULL); | 271 vcm_->Stop(media_stream::VideoCaptureManager::kStartOpenSessionId, NULL); |
291 | 272 |
292 // Wait to check callbacks before removing the listener | 273 // Wait to check callbacks before removing the listener |
293 SyncWithVideoCaptureManagerThread(); | 274 SyncWithVideoCaptureManagerThread(); |
294 vcm->Unregister(); | 275 vcm_->Unregister(); |
295 } | 276 } |
296 | 277 |
297 } // namespace | 278 } // namespace |
OLD | NEW |