| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "content/browser/browser_thread_impl.h" | 14 #include "content/browser/browser_thread_impl.h" |
| 15 #include "content/browser/renderer_host/media/media_stream_provider.h" | 15 #include "content/browser/renderer_host/media/media_stream_provider.h" |
| 16 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 16 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
| 17 #include "content/browser/renderer_host/media/video_capture_manager.h" | 17 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 18 #include "content/common/media/media_stream_options.h" | 18 #include "content/common/media/media_stream_options.h" |
| 19 #include "media/video/capture/fake_video_capture_device.h" | |
| 20 #include "media/video/capture/video_capture_device.h" | 19 #include "media/video/capture/video_capture_device.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 22 |
| 24 using ::testing::_; | 23 using ::testing::_; |
| 25 using ::testing::AnyNumber; | 24 using ::testing::AnyNumber; |
| 26 using ::testing::InSequence; | 25 using ::testing::InSequence; |
| 27 using ::testing::Return; | 26 using ::testing::Return; |
| 28 using ::testing::SaveArg; | 27 using ::testing::SaveArg; |
| 29 | 28 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 message_loop_->RunUntilIdle(); | 160 message_loop_->RunUntilIdle(); |
| 162 vcm_->Unregister(); | 161 vcm_->Unregister(); |
| 163 } | 162 } |
| 164 | 163 |
| 165 // Open the same device twice. | 164 // Open the same device twice. |
| 166 TEST_F(VideoCaptureManagerTest, OpenTwice) { | 165 TEST_F(VideoCaptureManagerTest, OpenTwice) { |
| 167 StreamDeviceInfoArray devices; | 166 StreamDeviceInfoArray devices; |
| 168 | 167 |
| 169 InSequence s; | 168 InSequence s; |
| 170 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) | 169 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) |
| 171 .Times(1) | 170 .Times(1).WillOnce(SaveArg<1>(&devices)); |
| 172 .WillOnce(SaveArg<1>(&devices)); | |
| 173 EXPECT_CALL(*listener_, Opened(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); | 171 EXPECT_CALL(*listener_, Opened(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); |
| 174 EXPECT_CALL(*listener_, Closed(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); | 172 EXPECT_CALL(*listener_, Closed(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); |
| 175 | 173 |
| 176 vcm_->EnumerateDevices(MEDIA_DEVICE_VIDEO_CAPTURE); | 174 vcm_->EnumerateDevices(MEDIA_DEVICE_VIDEO_CAPTURE); |
| 177 | 175 |
| 178 // Wait to get device callback. | 176 // Wait to get device callback. |
| 179 message_loop_->RunUntilIdle(); | 177 message_loop_->RunUntilIdle(); |
| 180 | 178 |
| 181 int video_session_id_first = vcm_->Open(devices.front()); | 179 int video_session_id_first = vcm_->Open(devices.front()); |
| 182 | 180 |
| 183 // This should trigger an error callback with error code | 181 // This should trigger an error callback with error code |
| 184 // 'kDeviceAlreadyInUse'. | 182 // 'kDeviceAlreadyInUse'. |
| 185 int video_session_id_second = vcm_->Open(devices.front()); | 183 int video_session_id_second = vcm_->Open(devices.front()); |
| 186 EXPECT_NE(video_session_id_first, video_session_id_second); | 184 EXPECT_NE(video_session_id_first, video_session_id_second); |
| 187 | 185 |
| 188 vcm_->Close(video_session_id_first); | 186 vcm_->Close(video_session_id_first); |
| 189 vcm_->Close(video_session_id_second); | 187 vcm_->Close(video_session_id_second); |
| 190 | 188 |
| 191 // Wait to check callbacks before removing the listener. | 189 // Wait to check callbacks before removing the listener. |
| 192 message_loop_->RunUntilIdle(); | 190 message_loop_->RunUntilIdle(); |
| 193 vcm_->Unregister(); | 191 vcm_->Unregister(); |
| 194 } | 192 } |
| 195 | 193 |
| 196 // Connect and disconnect devices. | |
| 197 TEST_F(VideoCaptureManagerTest, ConnectAndDisconnectDevices) { | |
| 198 StreamDeviceInfoArray devices; | |
| 199 int number_of_devices_keep = | |
| 200 media::FakeVideoCaptureDevice::NumberOfFakeDevices(); | |
| 201 | |
| 202 InSequence s; | |
| 203 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) | |
| 204 .Times(1) | |
| 205 .WillOnce(SaveArg<1>(&devices)); | |
| 206 vcm_->EnumerateDevices(MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 207 message_loop_->RunUntilIdle(); | |
| 208 ASSERT_EQ(devices.size(), 2u); | |
| 209 | |
| 210 // Simulate we remove 1 fake device. | |
| 211 media::FakeVideoCaptureDevice::SetNumberOfFakeDevices(1); | |
| 212 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) | |
| 213 .Times(1) | |
| 214 .WillOnce(SaveArg<1>(&devices)); | |
| 215 vcm_->EnumerateDevices(MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 216 message_loop_->RunUntilIdle(); | |
| 217 ASSERT_EQ(devices.size(), 1u); | |
| 218 | |
| 219 // Simulate we add 2 fake devices. | |
| 220 media::FakeVideoCaptureDevice::SetNumberOfFakeDevices(3); | |
| 221 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) | |
| 222 .Times(1) | |
| 223 .WillOnce(SaveArg<1>(&devices)); | |
| 224 vcm_->EnumerateDevices(MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 225 message_loop_->RunUntilIdle(); | |
| 226 ASSERT_EQ(devices.size(), 3u); | |
| 227 | |
| 228 vcm_->Unregister(); | |
| 229 media::FakeVideoCaptureDevice::SetNumberOfFakeDevices(number_of_devices_keep); | |
| 230 } | |
| 231 | |
| 232 // Enumerate devices and open the first, then check the capabilities. Then start | |
| 233 // the opened device. The capability list should be reduced to just one format, | |
| 234 // and this should be the one used when configuring-starting the device. Finally | |
| 235 // stop the device and check that the capabilities have been restored. | |
| 236 TEST_F(VideoCaptureManagerTest, ManipulateDeviceAndCheckCapabilities) { | |
| 237 StreamDeviceInfoArray devices; | |
| 238 | |
| 239 InSequence s; | |
| 240 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) | |
| 241 .Times(1) | |
| 242 .WillOnce(SaveArg<1>(&devices)); | |
| 243 vcm_->EnumerateDevices(MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 244 message_loop_->RunUntilIdle(); | |
| 245 | |
| 246 EXPECT_CALL(*listener_, Opened(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(1); | |
| 247 int video_session_id = vcm_->Open(devices.front()); | |
| 248 message_loop_->RunUntilIdle(); | |
| 249 | |
| 250 // When the device has been opened, we should see all the devices' | |
| 251 // capabilities. | |
| 252 media::VideoCaptureCapabilities device_capabilities; | |
| 253 vcm_->GetDeviceCapabilities(video_session_id, &device_capabilities); | |
| 254 ASSERT_EQ(devices.size(), 2u); | |
| 255 ASSERT_GT(device_capabilities.size(), 1u); | |
| 256 EXPECT_GT(device_capabilities[0].width, 1); | |
| 257 EXPECT_GT(device_capabilities[0].height, 1); | |
| 258 EXPECT_GT(device_capabilities[0].frame_rate, 1); | |
| 259 EXPECT_GT(device_capabilities[1].width, 1); | |
| 260 EXPECT_GT(device_capabilities[1].height, 1); | |
| 261 EXPECT_GT(device_capabilities[1].frame_rate, 1); | |
| 262 | |
| 263 VideoCaptureControllerID client_id = StartClient(video_session_id, true); | |
| 264 message_loop_->RunUntilIdle(); | |
| 265 // After StartClient(), the device's capabilities should be reduced to one. | |
| 266 vcm_->GetDeviceCapabilities(video_session_id, &device_capabilities); | |
| 267 ASSERT_EQ(device_capabilities.size(), 1u); | |
| 268 EXPECT_GT(device_capabilities[0].width, 1); | |
| 269 EXPECT_GT(device_capabilities[0].height, 1); | |
| 270 EXPECT_GT(device_capabilities[0].frame_rate, 1); | |
| 271 | |
| 272 EXPECT_CALL(*listener_, Closed(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(1); | |
| 273 StopClient(client_id); | |
| 274 // After StopClient(), the device's list of capabilities should be restored | |
| 275 // to the original one. | |
| 276 vcm_->GetDeviceCapabilities(video_session_id, &device_capabilities); | |
| 277 ASSERT_GT(device_capabilities.size(), 1u); | |
| 278 EXPECT_GT(device_capabilities[0].width, 1); | |
| 279 EXPECT_GT(device_capabilities[0].height, 1); | |
| 280 EXPECT_GT(device_capabilities[0].frame_rate, 1); | |
| 281 EXPECT_GT(device_capabilities[1].width, 1); | |
| 282 EXPECT_GT(device_capabilities[1].height, 1); | |
| 283 EXPECT_GT(device_capabilities[1].frame_rate, 1); | |
| 284 | |
| 285 vcm_->Close(video_session_id); | |
| 286 message_loop_->RunUntilIdle(); | |
| 287 vcm_->Unregister(); | |
| 288 } | |
| 289 | |
| 290 // Open two different devices. | 194 // Open two different devices. |
| 291 TEST_F(VideoCaptureManagerTest, OpenTwo) { | 195 TEST_F(VideoCaptureManagerTest, OpenTwo) { |
| 292 StreamDeviceInfoArray devices; | 196 StreamDeviceInfoArray devices; |
| 293 | 197 |
| 294 InSequence s; | 198 InSequence s; |
| 295 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) | 199 EXPECT_CALL(*listener_, DevicesEnumerated(MEDIA_DEVICE_VIDEO_CAPTURE, _)) |
| 296 .Times(1).WillOnce(SaveArg<1>(&devices)); | 200 .Times(1).WillOnce(SaveArg<1>(&devices)); |
| 297 EXPECT_CALL(*listener_, Opened(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); | 201 EXPECT_CALL(*listener_, Opened(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); |
| 298 EXPECT_CALL(*listener_, Closed(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); | 202 EXPECT_CALL(*listener_, Closed(MEDIA_DEVICE_VIDEO_CAPTURE, _)).Times(2); |
| 299 | 203 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 // VideoCaptureManager destructor otherwise. | 285 // VideoCaptureManager destructor otherwise. |
| 382 vcm_->Close(video_session_id); | 286 vcm_->Close(video_session_id); |
| 383 StopClient(client_id); | 287 StopClient(client_id); |
| 384 | 288 |
| 385 // Wait to check callbacks before removing the listener | 289 // Wait to check callbacks before removing the listener |
| 386 message_loop_->RunUntilIdle(); | 290 message_loop_->RunUntilIdle(); |
| 387 vcm_->Unregister(); | 291 vcm_->Unregister(); |
| 388 } | 292 } |
| 389 | 293 |
| 390 } // namespace content | 294 } // namespace content |
| OLD | NEW |