| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/renderer_host/media/service_video_capture_device_launc
her.h" | 5 #include "content/browser/renderer_host/media/service_video_capture_device_launc
her.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/test/mock_callback.h" | 8 #include "base/test/mock_callback.h" |
| 9 #include "base/test/scoped_task_environment.h" | 9 #include "base/test/scoped_task_environment.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| 11 #include "content/browser/renderer_host/media/service_launched_video_capture_dev
ice.h" |
| 11 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
| 12 #include "services/video_capture/public/interfaces/device_factory.mojom.h" | 13 #include "services/video_capture/public/interfaces/device_factory.mojom.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using testing::Invoke; | 17 using testing::Invoke; |
| 17 using testing::InvokeWithoutArgs; | 18 using testing::InvokeWithoutArgs; |
| 18 using testing::_; | 19 using testing::_; |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 void RunLaunchingDeviceIsAbortedTest( | 72 void RunLaunchingDeviceIsAbortedTest( |
| 72 video_capture::mojom::DeviceAccessResultCode service_result_code); | 73 video_capture::mojom::DeviceAccessResultCode service_result_code); |
| 73 | 74 |
| 74 base::test::ScopedTaskEnvironment scoped_task_environment_; | 75 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 75 MockDeviceFactory mock_device_factory_; | 76 MockDeviceFactory mock_device_factory_; |
| 76 MockVideoCaptureDeviceLauncherCallbacks mock_callbacks_; | 77 MockVideoCaptureDeviceLauncherCallbacks mock_callbacks_; |
| 77 video_capture::mojom::DeviceFactoryPtr device_factory_; | 78 video_capture::mojom::DeviceFactoryPtr device_factory_; |
| 78 std::unique_ptr<mojo::Binding<video_capture::mojom::DeviceFactory>> | 79 std::unique_ptr<mojo::Binding<video_capture::mojom::DeviceFactory>> |
| 79 factory_binding_; | 80 factory_binding_; |
| 80 std::unique_ptr<ServiceVideoCaptureDeviceLauncher> launcher_; | 81 std::unique_ptr<ServiceVideoCaptureDeviceLauncher> launcher_; |
| 81 base::MockCallback<base::OnceClosure> done_cb; | 82 base::MockCallback<base::OnceClosure> connection_lost_cb_; |
| 83 base::MockCallback<base::OnceClosure> done_cb_; |
| 82 | 84 |
| 83 private: | 85 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(ServiceVideoCaptureDeviceLauncherTest); | 86 DISALLOW_COPY_AND_ASSIGN(ServiceVideoCaptureDeviceLauncherTest); |
| 85 }; | 87 }; |
| 86 | 88 |
| 87 TEST_F(ServiceVideoCaptureDeviceLauncherTest, LaunchingDeviceSucceeds) { | 89 TEST_F(ServiceVideoCaptureDeviceLauncherTest, LaunchingDeviceSucceeds) { |
| 88 base::RunLoop run_loop; | 90 base::RunLoop run_loop; |
| 89 | 91 |
| 90 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | 92 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) |
| 91 .WillOnce(Invoke([](const std::string& device_id, | 93 .WillOnce(Invoke([](const std::string& device_id, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 102 const video_capture::mojom::DeviceFactory:: | 104 const video_capture::mojom::DeviceFactory:: |
| 103 CreateDeviceCallback& callback) { | 105 CreateDeviceCallback& callback) { |
| 104 callback.Run( | 106 callback.Run( |
| 105 video_capture::mojom::DeviceAccessResultCode::SUCCESS); | 107 video_capture::mojom::DeviceAccessResultCode::SUCCESS); |
| 106 }, | 108 }, |
| 107 base::Passed(device_request), callback)); | 109 base::Passed(device_request), callback)); |
| 108 })); | 110 })); |
| 109 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(1); | 111 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(1); |
| 110 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | 112 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); |
| 111 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); | 113 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); |
| 112 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 114 EXPECT_CALL(connection_lost_cb_, Run()).Times(0); |
| 115 EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { |
| 113 run_loop.Quit(); | 116 run_loop.Quit(); |
| 114 })); | 117 })); |
| 115 | 118 |
| 116 // Exercise | 119 // Exercise |
| 117 launcher_->LaunchDeviceAsync( | 120 launcher_->LaunchDeviceAsync( |
| 118 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 121 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 119 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 122 kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_, |
| 123 done_cb_.Get()); |
| 120 | 124 |
| 121 run_loop.Run(); | 125 run_loop.Run(); |
| 122 } | 126 } |
| 123 | 127 |
| 124 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 128 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 125 LaunchingDeviceIsAbortedBeforeServiceRespondsWithSuccess) { | 129 LaunchingDeviceIsAbortedBeforeServiceRespondsWithSuccess) { |
| 126 RunLaunchingDeviceIsAbortedTest( | 130 RunLaunchingDeviceIsAbortedTest( |
| 127 video_capture::mojom::DeviceAccessResultCode::SUCCESS); | 131 video_capture::mojom::DeviceAccessResultCode::SUCCESS); |
| 128 } | 132 } |
| 129 | 133 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 158 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& | 162 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& |
| 159 callback, | 163 callback, |
| 160 video_capture::mojom::DeviceAccessResultCode | 164 video_capture::mojom::DeviceAccessResultCode |
| 161 service_result_code) { callback.Run(service_result_code); }, | 165 service_result_code) { callback.Run(service_result_code); }, |
| 162 base::Passed(device_request), callback, service_result_code); | 166 base::Passed(device_request), callback, service_result_code); |
| 163 step_1_run_loop.Quit(); | 167 step_1_run_loop.Quit(); |
| 164 })); | 168 })); |
| 165 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 169 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 166 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(1); | 170 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(1); |
| 167 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); | 171 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); |
| 168 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&step_2_run_loop]() { | 172 EXPECT_CALL(connection_lost_cb_, Run()).Times(0); |
| 173 EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&step_2_run_loop]() { |
| 169 step_2_run_loop.Quit(); | 174 step_2_run_loop.Quit(); |
| 170 })); | 175 })); |
| 171 | 176 |
| 172 // Exercise | 177 // Exercise |
| 173 launcher_->LaunchDeviceAsync( | 178 launcher_->LaunchDeviceAsync( |
| 174 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 179 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 175 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 180 kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_, |
| 181 done_cb_.Get()); |
| 176 step_1_run_loop.Run(); | 182 step_1_run_loop.Run(); |
| 177 launcher_->AbortLaunch(); | 183 launcher_->AbortLaunch(); |
| 178 | 184 |
| 179 create_device_success_answer_cb.Run(); | 185 create_device_success_answer_cb.Run(); |
| 180 step_2_run_loop.Run(); | 186 step_2_run_loop.Run(); |
| 181 } | 187 } |
| 182 | 188 |
| 183 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 189 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 184 LaunchingDeviceFailsBecauseDeviceNotFound) { | 190 LaunchingDeviceFailsBecauseDeviceNotFound) { |
| 185 base::RunLoop run_loop; | 191 base::RunLoop run_loop; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 201 CreateDeviceCallback& callback) { | 207 CreateDeviceCallback& callback) { |
| 202 callback.Run( | 208 callback.Run( |
| 203 video_capture::mojom::DeviceAccessResultCode:: | 209 video_capture::mojom::DeviceAccessResultCode:: |
| 204 ERROR_DEVICE_NOT_FOUND); | 210 ERROR_DEVICE_NOT_FOUND); |
| 205 }, | 211 }, |
| 206 base::Passed(device_request), callback)); | 212 base::Passed(device_request), callback)); |
| 207 })); | 213 })); |
| 208 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 214 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 209 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | 215 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); |
| 210 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | 216 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); |
| 211 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 217 EXPECT_CALL(connection_lost_cb_, Run()).Times(0); |
| 218 EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { |
| 212 run_loop.Quit(); | 219 run_loop.Quit(); |
| 213 })); | 220 })); |
| 214 | 221 |
| 215 // Exercise | 222 // Exercise |
| 216 launcher_->LaunchDeviceAsync( | 223 launcher_->LaunchDeviceAsync( |
| 217 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 224 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 218 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 225 kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_, |
| 226 done_cb_.Get()); |
| 219 | 227 |
| 220 run_loop.Run(); | 228 run_loop.Run(); |
| 221 } | 229 } |
| 222 | 230 |
| 223 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 231 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 224 LaunchingDeviceFailsBecauseFactoryIsUnbound) { | 232 LaunchingDeviceFailsBecauseFactoryIsUnbound) { |
| 225 base::RunLoop run_loop; | 233 base::RunLoop run_loop; |
| 226 | 234 |
| 227 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 235 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 228 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | 236 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); |
| 229 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | 237 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); |
| 230 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 238 EXPECT_CALL(connection_lost_cb_, Run()).Times(0); |
| 239 EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { |
| 231 run_loop.Quit(); | 240 run_loop.Quit(); |
| 232 })); | 241 })); |
| 233 | 242 |
| 234 // Exercise | 243 // Exercise |
| 235 device_factory_.reset(); | 244 device_factory_.reset(); |
| 236 launcher_->LaunchDeviceAsync( | 245 launcher_->LaunchDeviceAsync( |
| 237 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 246 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 238 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 247 kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_, |
| 248 done_cb_.Get()); |
| 239 | 249 |
| 240 run_loop.Run(); | 250 run_loop.Run(); |
| 241 } | 251 } |
| 242 | 252 |
| 243 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 253 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 244 LaunchingDeviceFailsBecauseConnectionLostWhileLaunching) { | 254 LaunchingDeviceFailsBecauseConnectionLostWhileLaunching) { |
| 245 base::RunLoop run_loop; | 255 base::RunLoop run_loop; |
| 246 | 256 |
| 247 video_capture::mojom::DeviceFactory::CreateDeviceCallback create_device_cb; | 257 video_capture::mojom::DeviceFactory::CreateDeviceCallback create_device_cb; |
| 248 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | 258 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) |
| 249 .WillOnce(Invoke( | 259 .WillOnce(Invoke( |
| 250 [&create_device_cb]( | 260 [&create_device_cb]( |
| 251 const std::string& device_id, | 261 const std::string& device_id, |
| 252 video_capture::mojom::DeviceRequest* device_request, | 262 video_capture::mojom::DeviceRequest* device_request, |
| 253 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& | 263 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& |
| 254 callback) { | 264 callback) { |
| 255 // Simulate connection lost by not invoking |callback| and releasing | 265 // Simulate connection lost by not invoking |callback| and releasing |
| 256 // |device_request|. We have to save |callback| and invoke it later | 266 // |device_request|. We have to save |callback| and invoke it later |
| 257 // to avoid hitting a DCHECK. | 267 // to avoid hitting a DCHECK. |
| 258 create_device_cb = callback; | 268 create_device_cb = callback; |
| 259 })); | 269 })); |
| 260 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 270 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 261 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | 271 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); |
| 262 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | 272 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); |
| 263 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 273 // Note: |connection_lost_cb_| is only meant to be called when the connection |
| 274 // to a successfully-launched device is lost, which is not the case here. |
| 275 EXPECT_CALL(connection_lost_cb_, Run()).Times(0); |
| 276 EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { |
| 264 run_loop.Quit(); | 277 run_loop.Quit(); |
| 265 })); | 278 })); |
| 266 | 279 |
| 267 // Exercise | 280 // Exercise |
| 268 launcher_->LaunchDeviceAsync( | 281 launcher_->LaunchDeviceAsync( |
| 269 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 282 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 270 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 283 kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_, |
| 284 done_cb_.Get()); |
| 271 | 285 |
| 272 run_loop.Run(); | 286 run_loop.Run(); |
| 273 | 287 |
| 274 // Cut the connection to the factory, so that the outstanding | 288 // Cut the connection to the factory, so that the outstanding |
| 275 // |create_device_cb| will be dropped. | 289 // |create_device_cb| will be dropped. |
| 276 factory_binding_.reset(); | 290 factory_binding_.reset(); |
| 277 // We have to invoke the callback, because not doing so triggers a DCHECK. | 291 // We have to invoke the callback, because not doing so triggers a DCHECK. |
| 278 const video_capture::mojom::DeviceAccessResultCode arbitrary_result_code = | 292 const video_capture::mojom::DeviceAccessResultCode arbitrary_result_code = |
| 279 video_capture::mojom::DeviceAccessResultCode::SUCCESS; | 293 video_capture::mojom::DeviceAccessResultCode::SUCCESS; |
| 280 create_device_cb.Run(arbitrary_result_code); | 294 create_device_cb.Run(arbitrary_result_code); |
| 281 } | 295 } |
| 282 | 296 |
| 297 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 298 ConnectionLostAfterSuccessfulLaunch) { |
| 299 video_capture::mojom::DeviceRequest device_request_owned_by_service; |
| 300 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) |
| 301 .WillOnce(Invoke([&device_request_owned_by_service]( |
| 302 const std::string& device_id, |
| 303 video_capture::mojom::DeviceRequest* device_request, |
| 304 const video_capture::mojom::DeviceFactory:: |
| 305 CreateDeviceCallback& callback) { |
| 306 // The service holds on to the |device_request|. |
| 307 device_request_owned_by_service = std::move(*device_request); |
| 308 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 309 FROM_HERE, |
| 310 base::Bind( |
| 311 [](const video_capture::mojom::DeviceFactory:: |
| 312 CreateDeviceCallback& callback) { |
| 313 callback.Run( |
| 314 video_capture::mojom::DeviceAccessResultCode::SUCCESS); |
| 315 }, |
| 316 callback)); |
| 317 })); |
| 318 std::unique_ptr<LaunchedVideoCaptureDevice> launched_device; |
| 319 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)) |
| 320 .WillOnce( |
| 321 Invoke([&launched_device]( |
| 322 std::unique_ptr<LaunchedVideoCaptureDevice>* device) { |
| 323 // We must keep the launched device alive, because otherwise it will |
| 324 // no longer listen for connection errors. |
| 325 launched_device = std::move(*device); |
| 326 })); |
| 327 base::RunLoop step_1_run_loop; |
| 328 EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&step_1_run_loop]() { |
| 329 step_1_run_loop.Quit(); |
| 330 })); |
| 331 // Exercise step 1 |
| 332 launcher_->LaunchDeviceAsync( |
| 333 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 334 kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_, |
| 335 done_cb_.Get()); |
| 336 step_1_run_loop.Run(); |
| 337 |
| 338 base::RunLoop step_2_run_loop; |
| 339 EXPECT_CALL(connection_lost_cb_, Run()).WillOnce(Invoke([&step_2_run_loop]() { |
| 340 step_2_run_loop.Quit(); |
| 341 })); |
| 342 // Exercise step 2: The service cuts/loses the connection |
| 343 device_request_owned_by_service = nullptr; |
| 344 step_2_run_loop.Run(); |
| 345 } |
| 346 |
| 283 } // namespace content | 347 } // namespace content |
| OLD | NEW |