Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/renderer_host/media/service_video_capture_device_launc her.h" | |
| 6 | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/test/mock_callback.h" | |
| 9 #include "base/test/scoped_task_environment.h" | |
| 10 #include "base/threading/thread.h" | |
| 11 #include "mojo/public/cpp/bindings/binding.h" | |
| 12 #include "services/video_capture/public/interfaces/device_factory.mojom.h" | |
| 13 #include "testing/gmock/include/gmock/gmock.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 using testing::Invoke; | |
| 17 using testing::InvokeWithoutArgs; | |
| 18 using testing::_; | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 static const std::string kStubDeviceId = "StubDevice"; | |
| 23 static const media::VideoCaptureParams kArbitraryParams; | |
| 24 static const base::WeakPtr<media::VideoFrameReceiver> kNullReceiver; | |
| 25 | |
| 26 class MockDeviceFactory : public video_capture::mojom::DeviceFactory { | |
| 27 public: | |
| 28 void CreateDevice(const std::string& device_id, | |
| 29 video_capture::mojom::DeviceRequest device_request, | |
| 30 const CreateDeviceCallback& callback) override { | |
| 31 DoCreateDevice(device_id, &device_request, callback); | |
| 32 } | |
| 33 | |
| 34 MOCK_METHOD1(GetDeviceInfos, void(const GetDeviceInfosCallback& callback)); | |
| 35 MOCK_METHOD3(DoCreateDevice, | |
| 36 void(const std::string& device_id, | |
| 37 video_capture::mojom::DeviceRequest* device_request, | |
| 38 const CreateDeviceCallback& callback)); | |
| 39 }; | |
| 40 | |
| 41 class MockVideoCaptureDeviceLauncherCallbacks | |
| 42 : public VideoCaptureDeviceLauncher::Callbacks { | |
| 43 public: | |
| 44 void OnDeviceLaunched( | |
| 45 std::unique_ptr<LaunchedVideoCaptureDevice> device) override { | |
| 46 DoOnDeviceLaunched(&device); | |
| 47 } | |
| 48 | |
| 49 MOCK_METHOD1(DoOnDeviceLaunched, | |
| 50 void(std::unique_ptr<LaunchedVideoCaptureDevice>* device)); | |
| 51 MOCK_METHOD0(OnDeviceLaunchFailed, void()); | |
| 52 MOCK_METHOD0(OnDeviceLaunchAborted, void()); | |
| 53 }; | |
| 54 | |
| 55 class ServiceVideoCaptureDeviceLauncherTest : public testing::Test { | |
| 56 public: | |
| 57 ServiceVideoCaptureDeviceLauncherTest() {} | |
| 58 ~ServiceVideoCaptureDeviceLauncherTest() override {} | |
| 59 | |
| 60 protected: | |
| 61 void SetUp() override { | |
| 62 factory_binding_ = | |
| 63 base::MakeUnique<mojo::Binding<video_capture::mojom::DeviceFactory>>( | |
| 64 &mock_device_factory_, mojo::MakeRequest(&device_factory_)); | |
| 65 launcher_ = | |
| 66 base::MakeUnique<ServiceVideoCaptureDeviceLauncher>(&device_factory_); | |
| 67 } | |
| 68 | |
| 69 void TearDown() override {} | |
| 70 | |
| 71 void RunLaunchingDeviceIsAbortedTest( | |
| 72 video_capture::mojom::DeviceAccessResultCode service_result_code); | |
| 73 | |
| 74 base::test::ScopedTaskEnvironment scoped_task_environment_; | |
| 75 MockDeviceFactory mock_device_factory_; | |
| 76 MockVideoCaptureDeviceLauncherCallbacks mock_callbacks_; | |
| 77 video_capture::mojom::DeviceFactoryPtr device_factory_; | |
| 78 std::unique_ptr<mojo::Binding<video_capture::mojom::DeviceFactory>> | |
| 79 factory_binding_; | |
| 80 std::unique_ptr<ServiceVideoCaptureDeviceLauncher> launcher_; | |
| 81 base::MockCallback<base::OnceClosure> done_cb; | |
| 82 | |
| 83 private: | |
| 84 DISALLOW_COPY_AND_ASSIGN(ServiceVideoCaptureDeviceLauncherTest); | |
| 85 }; | |
| 86 | |
| 87 TEST_F(ServiceVideoCaptureDeviceLauncherTest, LaunchingDeviceSucceeds) { | |
| 88 base::RunLoop run_loop; | |
| 89 | |
| 90 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | |
| 91 .WillOnce(Invoke([](const std::string& device_id, | |
| 92 video_capture::mojom::DeviceRequest* device_request, | |
| 93 const video_capture::mojom::DeviceFactory:: | |
| 94 CreateDeviceCallback& callback) { | |
| 95 // Note: We must keep |device_request| alive at least until we have | |
| 96 // sent out the callback. Otherwise, |launcher_| may interpret this | |
| 97 // as the connection having been lost before receiving the callback. | |
| 98 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 99 FROM_HERE, | |
| 100 base::Bind( | |
| 101 [](video_capture::mojom::DeviceRequest device_request, | |
| 102 const video_capture::mojom::DeviceFactory:: | |
| 103 CreateDeviceCallback& callback) { | |
| 104 callback.Run( | |
| 105 video_capture::mojom::DeviceAccessResultCode::SUCCESS); | |
| 106 }, | |
| 107 base::Passed(device_request), callback)); | |
| 108 })); | |
| 109 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(1); | |
| 110 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | |
| 111 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); | |
| 112 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | |
| 113 run_loop.Quit(); | |
| 114 })); | |
| 115 | |
| 116 // Exercise | |
| 117 launcher_->LaunchDeviceAsync( | |
| 118 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | |
| 119 kNullReceiver, &mock_callbacks_, done_cb.Get()); | |
| 120 | |
| 121 run_loop.Run(); | |
| 122 } | |
| 123 | |
| 124 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | |
| 125 LaunchingDeviceIsAbortedBeforeServiceRespondsWithSuccess) { | |
| 126 RunLaunchingDeviceIsAbortedTest( | |
| 127 video_capture::mojom::DeviceAccessResultCode::SUCCESS); | |
| 128 } | |
| 129 | |
| 130 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | |
| 131 LaunchingDeviceIsAbortedBeforeServiceRespondsWithNotFound) { | |
| 132 RunLaunchingDeviceIsAbortedTest( | |
| 133 video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND); | |
| 134 } | |
| 135 | |
| 136 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | |
| 137 LaunchingDeviceIsAbortedBeforeServiceRespondsWithNotInitialized) { | |
| 138 RunLaunchingDeviceIsAbortedTest( | |
| 139 video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED); | |
| 140 } | |
| 141 | |
| 142 void ServiceVideoCaptureDeviceLauncherTest::RunLaunchingDeviceIsAbortedTest( | |
| 143 video_capture::mojom::DeviceAccessResultCode service_result_code) { | |
| 144 base::RunLoop step_1_run_loop; | |
| 145 base::RunLoop step_2_run_loop; | |
| 146 | |
| 147 base::Closure create_device_success_answer_cb; | |
| 148 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | |
| 149 .WillOnce(Invoke([&create_device_success_answer_cb, &step_1_run_loop, | |
| 150 service_result_code]( | |
| 151 const std::string& device_id, | |
| 152 video_capture::mojom::DeviceRequest* device_request, | |
| 153 const video_capture::mojom::DeviceFactory:: | |
| 154 CreateDeviceCallback& callback) { | |
| 155 // Prepare the callback, but save it for now instead of invoking it. | |
| 156 create_device_success_answer_cb = base::Bind( | |
| 157 [](video_capture::mojom::DeviceRequest device_request, | |
| 158 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& | |
| 159 callback, | |
| 160 video_capture::mojom::DeviceAccessResultCode | |
| 161 service_result_code) { callback.Run(service_result_code); }, | |
| 162 base::Passed(device_request), callback, service_result_code); | |
| 163 step_1_run_loop.Quit(); | |
| 164 })); | |
| 165 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | |
| 166 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(1); | |
| 167 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0); | |
| 168 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&step_2_run_loop]() { | |
| 169 step_2_run_loop.Quit(); | |
| 170 })); | |
| 171 | |
| 172 // Exercise | |
| 173 launcher_->LaunchDeviceAsync( | |
| 174 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | |
| 175 kNullReceiver, &mock_callbacks_, done_cb.Get()); | |
| 176 step_1_run_loop.Run(); | |
| 177 launcher_->AbortLaunch(); | |
| 178 | |
| 179 create_device_success_answer_cb.Run(); | |
| 180 step_2_run_loop.Run(); | |
| 181 } | |
| 182 | |
| 183 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | |
| 184 LaunchingDeviceFailsBecauseDeviceNotFound) { | |
| 185 base::RunLoop run_loop; | |
| 186 | |
| 187 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | |
| 188 .WillOnce(Invoke( | |
| 189 [](const std::string& device_id, | |
| 190 video_capture::mojom::DeviceRequest* device_request, | |
| 191 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& | |
| 192 callback) { | |
| 193 // Note: We must keep |device_request| alive at least until we have | |
| 194 // sent out the callback. Otherwise, |launcher_| may interpret this | |
| 195 // as the connection having been lost before receiving the callback. | |
| 196 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 197 FROM_HERE, | |
| 198 base::Bind( | |
| 199 [](video_capture::mojom::DeviceRequest device_request, | |
| 200 const video_capture::mojom::DeviceFactory:: | |
| 201 CreateDeviceCallback& callback) { | |
| 202 callback.Run( | |
| 203 video_capture::mojom::DeviceAccessResultCode:: | |
| 204 ERROR_DEVICE_NOT_FOUND); | |
| 205 }, | |
| 206 base::Passed(device_request), callback)); | |
| 207 })); | |
| 208 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | |
| 209 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | |
| 210 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | |
| 211 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | |
| 212 run_loop.Quit(); | |
| 213 })); | |
| 214 | |
| 215 // Exercise | |
| 216 launcher_->LaunchDeviceAsync( | |
| 217 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | |
| 218 kNullReceiver, &mock_callbacks_, done_cb.Get()); | |
| 219 | |
| 220 run_loop.Run(); | |
| 221 } | |
| 222 | |
| 223 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | |
| 224 LaunchingDeviceFailsBecauseFactoryIsUnbound) { | |
| 225 base::RunLoop run_loop; | |
| 226 | |
| 227 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | |
| 228 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | |
| 229 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | |
| 230 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | |
| 231 run_loop.Quit(); | |
| 232 })); | |
| 233 | |
| 234 // Exercise | |
| 235 device_factory_.reset(); | |
| 236 launcher_->LaunchDeviceAsync( | |
| 237 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | |
| 238 kNullReceiver, &mock_callbacks_, done_cb.Get()); | |
| 239 | |
| 240 run_loop.Run(); | |
| 241 } | |
| 242 | |
| 243 TEST_F(ServiceVideoCaptureDeviceLauncherTest, | |
| 244 LaunchingDeviceFailsBecauseConnectionLostWhileLaunching) { | |
| 245 base::RunLoop run_loop; | |
| 246 | |
| 247 video_capture::mojom::DeviceFactory::CreateDeviceCallback create_device_cb; | |
| 248 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _)) | |
| 249 .WillOnce(Invoke( | |
| 250 [&create_device_cb]( | |
| 251 const std::string& device_id, | |
| 252 video_capture::mojom::DeviceRequest* device_request, | |
| 253 const video_capture::mojom::DeviceFactory::CreateDeviceCallback& | |
| 254 callback) { | |
| 255 // Simulate connection lost by not invoking |callback| and releasing | |
| 256 // |device_request|. We have to save |callback| and invoke it later | |
| 257 // to avoid hitting a DCHECK. | |
| 258 create_device_cb = callback; | |
| 259 })); | |
| 260 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0); | |
| 261 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0); | |
| 262 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1); | |
| 263 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() { | |
| 264 run_loop.Quit(); | |
| 265 })); | |
| 266 | |
| 267 // Exercise | |
| 268 launcher_->LaunchDeviceAsync( | |
| 269 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams, | |
| 270 kNullReceiver, &mock_callbacks_, done_cb.Get()); | |
| 271 | |
| 272 run_loop.Run(); | |
| 273 | |
| 274 // Cut the connection to the factory, so that the outstanding | |
| 275 // |create_device_cb| will be dropped. | |
|
Ken Rockot(use gerrit already)
2017/05/26 00:45:04
nit: "can" be dropped. Resetting the binding doesn
| |
| 276 factory_binding_.reset(); | |
| 277 // We have to invoke the callback, because not doing so triggers a DCHECK. | |
|
Ken Rockot(use gerrit already)
2017/05/26 00:45:05
nit: This shouldn't be necessary since you're rese
| |
| 278 const video_capture::mojom::DeviceAccessResultCode arbitrary_result_code = | |
| 279 video_capture::mojom::DeviceAccessResultCode::SUCCESS; | |
| 280 create_device_cb.Run(arbitrary_result_code); | |
| 281 } | |
| 282 | |
| 283 } // namespace content | |
| OLD | NEW |