Chromium Code Reviews| 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_; |
| 82 base::MockCallback<base::OnceClosure> connection_lost_cb; | |
|
emircan
2017/06/01 18:13:29
s/connection_lost_cb/connection_lost_cb_
chfremer
2017/06/01 19:25:56
Done.
| |
| 81 base::MockCallback<base::OnceClosure> done_cb; | 83 base::MockCallback<base::OnceClosure> done_cb; |
|
emircan
2017/06/01 18:13:29
s/done_cb/done_cb_
chfremer
2017/06/01 19:25:56
Done.
| |
| 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); |
| 114 EXPECT_CALL(connection_lost_cb, Run()).Times(0); | |
| 112 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 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_, done_cb.Get()); |
| 120 | 123 |
| 121 run_loop.Run(); | 124 run_loop.Run(); |
| 122 } | 125 } |
| 123 | 126 |
| 124 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 127 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 125 LaunchingDeviceIsAbortedBeforeServiceRespondsWithSuccess) { | 128 LaunchingDeviceIsAbortedBeforeServiceRespondsWithSuccess) { |
| 126 RunLaunchingDeviceIsAbortedTest( | 129 RunLaunchingDeviceIsAbortedTest( |
| 127 video_capture::mojom::DeviceAccessResultCode::SUCCESS); | 130 video_capture::mojom::DeviceAccessResultCode::SUCCESS); |
| 128 } | 131 } |
| 129 | 132 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 158 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& | 161 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& |
| 159 callback, | 162 callback, |
| 160 video_capture::mojom::DeviceAccessResultCode | 163 video_capture::mojom::DeviceAccessResultCode |
| 161 service_result_code) { callback.Run(service_result_code); }, | 164 service_result_code) { callback.Run(service_result_code); }, |
| 162 base::Passed(device_request), callback, service_result_code); | 165 base::Passed(device_request), callback, service_result_code); |
| 163 step_1_run_loop.Quit(); | 166 step_1_run_loop.Quit(); |
| 164 })); | 167 })); |
| 165 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 168 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 166 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(1); | 169 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(1); |
| 167 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); | 170 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); |
| 171 EXPECT_CALL(connection_lost_cb, Run()).Times(0); | |
| 168 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&step_2_run_loop]() { | 172 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&step_2_run_loop]() { |
| 169 step_2_run_loop.Quit(); | 173 step_2_run_loop.Quit(); |
| 170 })); | 174 })); |
| 171 | 175 |
| 172 // Exercise | 176 // Exercise |
| 173 launcher_->LaunchDeviceAsync( | 177 launcher_->LaunchDeviceAsync( |
| 174 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 178 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 175 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 179 kNullReceiver, connection_lost_cb.Get(), &mock_callbacks_, done_cb.Get()); |
| 176 step_1_run_loop.Run(); | 180 step_1_run_loop.Run(); |
| 177 launcher_->AbortLaunch(); | 181 launcher_->AbortLaunch(); |
| 178 | 182 |
| 179 create_device_success_answer_cb.Run(); | 183 create_device_success_answer_cb.Run(); |
| 180 step_2_run_loop.Run(); | 184 step_2_run_loop.Run(); |
| 181 } | 185 } |
| 182 | 186 |
| 183 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 187 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 184 LaunchingDeviceFailsBecauseDeviceNotFound) { | 188 LaunchingDeviceFailsBecauseDeviceNotFound) { |
| 185 base::RunLoop run_loop; | 189 base::RunLoop run_loop; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 201 CreateDeviceCallback& callback) { | 205 CreateDeviceCallback& callback) { |
| 202 callback.Run( | 206 callback.Run( |
| 203 video_capture::mojom::DeviceAccessResultCode:: | 207 video_capture::mojom::DeviceAccessResultCode:: |
| 204 ERROR_DEVICE_NOT_FOUND); | 208 ERROR_DEVICE_NOT_FOUND); |
| 205 }, | 209 }, |
| 206 base::Passed(device_request), callback)); | 210 base::Passed(device_request), callback)); |
| 207 })); | 211 })); |
| 208 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 212 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 209 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | 213 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); |
| 210 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | 214 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); |
| 215 EXPECT_CALL(connection_lost_cb, Run()).Times(0); | |
| 211 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 216 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { |
| 212 run_loop.Quit(); | 217 run_loop.Quit(); |
| 213 })); | 218 })); |
| 214 | 219 |
| 215 // Exercise | 220 // Exercise |
| 216 launcher_->LaunchDeviceAsync( | 221 launcher_->LaunchDeviceAsync( |
| 217 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 222 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 218 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 223 kNullReceiver, connection_lost_cb.Get(), &mock_callbacks_, done_cb.Get()); |
| 219 | 224 |
| 220 run_loop.Run(); | 225 run_loop.Run(); |
| 221 } | 226 } |
| 222 | 227 |
| 223 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 228 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 224 LaunchingDeviceFailsBecauseFactoryIsUnbound) { | 229 LaunchingDeviceFailsBecauseFactoryIsUnbound) { |
| 225 base::RunLoop run_loop; | 230 base::RunLoop run_loop; |
| 226 | 231 |
| 227 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 232 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 228 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | 233 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); |
| 229 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | 234 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); |
| 235 EXPECT_CALL(connection_lost_cb, Run()).Times(0); | |
| 230 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 236 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { |
| 231 run_loop.Quit(); | 237 run_loop.Quit(); |
| 232 })); | 238 })); |
| 233 | 239 |
| 234 // Exercise | 240 // Exercise |
| 235 device_factory_.reset(); | 241 device_factory_.reset(); |
| 236 launcher_->LaunchDeviceAsync( | 242 launcher_->LaunchDeviceAsync( |
| 237 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 243 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 238 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 244 kNullReceiver, connection_lost_cb.Get(), &mock_callbacks_, done_cb.Get()); |
| 239 | 245 |
| 240 run_loop.Run(); | 246 run_loop.Run(); |
| 241 } | 247 } |
| 242 | 248 |
| 243 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | 249 TEST_F(ServiceVideoCaptureDeviceLauncherTest, |
| 244 LaunchingDeviceFailsBecauseConnectionLostWhileLaunching) { | 250 LaunchingDeviceFailsBecauseConnectionLostWhileLaunching) { |
| 245 base::RunLoop run_loop; | 251 base::RunLoop run_loop; |
| 246 | 252 |
| 247 video_capture::mojom::DeviceFactory::CreateDeviceCallback create_device_cb; | 253 video_capture::mojom::DeviceFactory::CreateDeviceCallback create_device_cb; |
| 248 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | 254 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) |
| 249 .WillOnce(Invoke( | 255 .WillOnce(Invoke( |
| 250 [&create_device_cb]( | 256 [&create_device_cb]( |
| 251 const std::string& device_id, | 257 const std::string& device_id, |
| 252 video_capture::mojom::DeviceRequest* device_request, | 258 video_capture::mojom::DeviceRequest* device_request, |
| 253 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& | 259 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& |
| 254 callback) { | 260 callback) { |
| 255 // Simulate connection lost by not invoking |callback| and releasing | 261 // Simulate connection lost by not invoking |callback| and releasing |
| 256 // |device_request|. We have to save |callback| and invoke it later | 262 // |device_request|. We have to save |callback| and invoke it later |
| 257 // to avoid hitting a DCHECK. | 263 // to avoid hitting a DCHECK. |
| 258 create_device_cb = callback; | 264 create_device_cb = callback; |
| 259 })); | 265 })); |
| 260 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | 266 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); |
| 261 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | 267 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); |
| 262 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | 268 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); |
| 269 // Note: |connection_lost_cb| is only meant to be called when the connection | |
| 270 // to a successfully-launched device is lost, which is not the case here. | |
| 271 EXPECT_CALL(connection_lost_cb, Run()).Times(0); | |
| 263 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | 272 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { |
| 264 run_loop.Quit(); | 273 run_loop.Quit(); |
| 265 })); | 274 })); |
| 266 | 275 |
| 267 // Exercise | 276 // Exercise |
| 268 launcher_->LaunchDeviceAsync( | 277 launcher_->LaunchDeviceAsync( |
| 269 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | 278 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, |
| 270 kNullReceiver, &mock_callbacks_, done_cb.Get()); | 279 kNullReceiver, connection_lost_cb.Get(), &mock_callbacks_, done_cb.Get()); |
| 271 | 280 |
| 272 run_loop.Run(); | 281 run_loop.Run(); |
| 273 | 282 |
| 274 // Cut the connection to the factory, so that the outstanding | 283 // Cut the connection to the factory, so that the outstanding |
| 275 // |create_device_cb| will be dropped. | 284 // |create_device_cb| will be dropped. |
| 276 factory_binding_.reset(); | 285 factory_binding_.reset(); |
| 277 // We have to invoke the callback, because not doing so triggers a DCHECK. | 286 // We have to invoke the callback, because not doing so triggers a DCHECK. |
| 278 const video_capture::mojom::DeviceAccessResultCode arbitrary_result_code = | 287 const video_capture::mojom::DeviceAccessResultCode arbitrary_result_code = |
| 279 video_capture::mojom::DeviceAccessResultCode::SUCCESS; | 288 video_capture::mojom::DeviceAccessResultCode::SUCCESS; |
| 280 create_device_cb.Run(arbitrary_result_code); | 289 create_device_cb.Run(arbitrary_result_code); |
| 281 } | 290 } |
| 282 | 291 |
| 292 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | |
| 293 ConnectionLostAfterSuccessfulLaunch) { | |
| 294 video_capture::mojom::DeviceRequest device_request_owned_by_service; | |
| 295 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | |
| 296 .WillOnce(Invoke([&device_request_owned_by_service]( | |
| 297 const std::string& device_id, | |
| 298 video_capture::mojom::DeviceRequest* device_request, | |
| 299 const video_capture::mojom::DeviceFactory:: | |
| 300 CreateDeviceCallback& callback) { | |
| 301 // The service holds on to the |device_request|. | |
| 302 device_request_owned_by_service = std::move(*device_request); | |
| 303 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 304 FROM_HERE, | |
| 305 base::Bind( | |
| 306 [](const video_capture::mojom::DeviceFactory:: | |
| 307 CreateDeviceCallback& callback) { | |
| 308 callback.Run( | |
| 309 video_capture::mojom::DeviceAccessResultCode::SUCCESS); | |
| 310 }, | |
| 311 callback)); | |
| 312 })); | |
| 313 std::unique_ptr<LaunchedVideoCaptureDevice> launched_device; | |
| 314 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)) | |
| 315 .WillOnce( | |
| 316 Invoke([&launched_device]( | |
| 317 std::unique_ptr<LaunchedVideoCaptureDevice>* device) { | |
| 318 // We must keep the launched device alive, because otherwise it will | |
| 319 // no longer listen for connection errors. | |
| 320 launched_device = std::move(*device); | |
| 321 })); | |
| 322 base::RunLoop step_1_run_loop; | |
| 323 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&step_1_run_loop]() { | |
| 324 step_1_run_loop.Quit(); | |
| 325 })); | |
| 326 // Exercise step 1 | |
| 327 launcher_->LaunchDeviceAsync( | |
| 328 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | |
| 329 kNullReceiver, connection_lost_cb.Get(), &mock_callbacks_, done_cb.Get()); | |
| 330 step_1_run_loop.Run(); | |
| 331 | |
| 332 base::RunLoop step_2_run_loop; | |
| 333 EXPECT_CALL(connection_lost_cb, Run()).WillOnce(Invoke([&step_2_run_loop]() { | |
| 334 step_2_run_loop.Quit(); | |
| 335 })); | |
| 336 // Exercise step 2: The service cuts/loses the connection | |
| 337 device_request_owned_by_service = nullptr; | |
| 338 step_2_run_loop.Run(); | |
| 339 } | |
| 340 | |
| 283 } // namespace content | 341 } // namespace content |
| OLD | NEW |