summaryrefslogtreecommitdiff
path: root/chromium/content/browser/renderer_host/media/service_video_capture_device_launcher_unittest.cc
blob: aa6c20373933459748a5baf1cc4f3eed188a00d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/media/service_video_capture_device_launcher.h"

#include "base/run_loop.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "content/browser/renderer_host/media/service_launched_video_capture_device.h"
#include "content/browser/renderer_host/media/video_capture_factory_delegate.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "services/video_capture/public/mojom/device_factory.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::_;

namespace content {

static const std::string kStubDeviceId = "StubDevice";
static const media::VideoCaptureParams kArbitraryParams;
static const base::WeakPtr<media::VideoFrameReceiver> kNullReceiver;

class MockDeviceFactory : public video_capture::mojom::DeviceFactory {
 public:
  void CreateDevice(const std::string& device_id,
                    video_capture::mojom::DeviceRequest device_request,
                    CreateDeviceCallback callback) override {
    DoCreateDevice(device_id, &device_request, callback);
  }

  void GetDeviceInfos(GetDeviceInfosCallback callback) override {
    DoGetDeviceInfos(callback);
  }

  void AddVirtualDevice(const media::VideoCaptureDeviceInfo& device_info,
                        video_capture::mojom::ProducerPtr producer,
                        video_capture::mojom::VirtualDeviceRequest
                            virtual_device_request) override {
    DoAddVirtualDevice(device_info, producer.get(), &virtual_device_request);
  }

  MOCK_METHOD1(DoGetDeviceInfos, void(GetDeviceInfosCallback& callback));
  MOCK_METHOD3(DoCreateDevice,
               void(const std::string& device_id,
                    video_capture::mojom::DeviceRequest* device_request,
                    CreateDeviceCallback& callback));
  MOCK_METHOD3(
      DoAddVirtualDevice,
      void(const media::VideoCaptureDeviceInfo& device_info,
           video_capture::mojom::ProducerProxy* producer,
           video_capture::mojom::VirtualDeviceRequest* virtual_device_request));
};

class MockVideoCaptureDeviceLauncherCallbacks
    : public VideoCaptureDeviceLauncher::Callbacks {
 public:
  void OnDeviceLaunched(
      std::unique_ptr<LaunchedVideoCaptureDevice> device) override {
    DoOnDeviceLaunched(&device);
  }

  MOCK_METHOD1(DoOnDeviceLaunched,
               void(std::unique_ptr<LaunchedVideoCaptureDevice>* device));
  MOCK_METHOD0(OnDeviceLaunchFailed, void());
  MOCK_METHOD0(OnDeviceLaunchAborted, void());
};

class ServiceVideoCaptureDeviceLauncherTest : public testing::Test {
 public:
  ServiceVideoCaptureDeviceLauncherTest() {}
  ~ServiceVideoCaptureDeviceLauncherTest() override {}

 protected:
  void SetUp() override {
    factory_binding_ =
        std::make_unique<mojo::Binding<video_capture::mojom::DeviceFactory>>(
            &mock_device_factory_, mojo::MakeRequest(&device_factory_));
    launcher_ = std::make_unique<ServiceVideoCaptureDeviceLauncher>(
        connect_to_device_factory_cb_.Get());
    launcher_has_connected_to_device_factory_ = false;
    launcher_has_released_device_factory_ = false;

    ON_CALL(connect_to_device_factory_cb_, Run(_))
        .WillByDefault(Invoke(
            [this](std::unique_ptr<VideoCaptureFactoryDelegate>* out_factory) {
              launcher_has_connected_to_device_factory_ = true;
              *out_factory = std::make_unique<VideoCaptureFactoryDelegate>(
                  &device_factory_, release_connection_cb_.Get());
            }));

    ON_CALL(release_connection_cb_, Run())
        .WillByDefault(InvokeWithoutArgs([this]() {
          launcher_has_released_device_factory_ = true;
          wait_for_release_connection_cb_.Quit();
        }));
  }

  void TearDown() override {}

  void RunLaunchingDeviceIsAbortedTest(
      video_capture::mojom::DeviceAccessResultCode service_result_code);

  base::test::ScopedTaskEnvironment scoped_task_environment_;
  MockDeviceFactory mock_device_factory_;
  MockVideoCaptureDeviceLauncherCallbacks mock_callbacks_;
  video_capture::mojom::DeviceFactoryPtr device_factory_;
  std::unique_ptr<mojo::Binding<video_capture::mojom::DeviceFactory>>
      factory_binding_;
  std::unique_ptr<ServiceVideoCaptureDeviceLauncher> launcher_;
  base::MockCallback<base::OnceClosure> connection_lost_cb_;
  base::MockCallback<base::OnceClosure> done_cb_;
  base::MockCallback<
      ServiceVideoCaptureDeviceLauncher::ConnectToDeviceFactoryCB>
      connect_to_device_factory_cb_;
  base::MockCallback<base::OnceClosure> release_connection_cb_;
  bool launcher_has_connected_to_device_factory_;
  bool launcher_has_released_device_factory_;
  base::RunLoop wait_for_release_connection_cb_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ServiceVideoCaptureDeviceLauncherTest);
};

TEST_F(ServiceVideoCaptureDeviceLauncherTest, LaunchingDeviceSucceeds) {
  EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
      .WillOnce(Invoke([](const std::string& device_id,
                          video_capture::mojom::DeviceRequest* device_request,
                          video_capture::mojom::DeviceFactory::
                              CreateDeviceCallback& callback) {
        // Note: We must keep |device_request| alive at least until we have
        // sent out the callback. Otherwise, |launcher_| may interpret this
        // as the connection having been lost before receiving the callback.
        base::ThreadTaskRunnerHandle::Get()->PostTask(
            FROM_HERE,
            base::BindOnce(
                [](video_capture::mojom::DeviceRequest device_request,
                   video_capture::mojom::DeviceFactory::CreateDeviceCallback
                       callback) {
                  std::move(callback).Run(
                      video_capture::mojom::DeviceAccessResultCode::SUCCESS);
                },
                std::move(*device_request), std::move(callback)));
      }));

  EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(1);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0);
  EXPECT_CALL(connection_lost_cb_, Run()).Times(0);
  base::RunLoop wait_for_done_cb;
  EXPECT_CALL(done_cb_, Run())
      .WillOnce(InvokeWithoutArgs(
          [&wait_for_done_cb]() { wait_for_done_cb.Quit(); }));

  // Exercise
  launcher_->LaunchDeviceAsync(
      kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
      kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_,
      done_cb_.Get());
  wait_for_done_cb.Run();

  launcher_.reset();
  wait_for_release_connection_cb_.Run();
  EXPECT_TRUE(launcher_has_connected_to_device_factory_);
  EXPECT_TRUE(launcher_has_released_device_factory_);
}

TEST_F(ServiceVideoCaptureDeviceLauncherTest,
       LaunchingDeviceIsAbortedBeforeServiceRespondsWithSuccess) {
  RunLaunchingDeviceIsAbortedTest(
      video_capture::mojom::DeviceAccessResultCode::SUCCESS);
}

TEST_F(ServiceVideoCaptureDeviceLauncherTest,
       LaunchingDeviceIsAbortedBeforeServiceRespondsWithNotFound) {
  RunLaunchingDeviceIsAbortedTest(
      video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND);
}

TEST_F(ServiceVideoCaptureDeviceLauncherTest,
       LaunchingDeviceIsAbortedBeforeServiceRespondsWithNotInitialized) {
  RunLaunchingDeviceIsAbortedTest(
      video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED);
}

void ServiceVideoCaptureDeviceLauncherTest::RunLaunchingDeviceIsAbortedTest(
    video_capture::mojom::DeviceAccessResultCode service_result_code) {
  base::RunLoop step_1_run_loop;
  base::RunLoop step_2_run_loop;

  base::OnceClosure create_device_success_answer_cb;
  EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
      .WillOnce(
          Invoke([&create_device_success_answer_cb, &step_1_run_loop,
                  service_result_code](
                     const std::string& device_id,
                     video_capture::mojom::DeviceRequest* device_request,
                     video_capture::mojom::DeviceFactory::CreateDeviceCallback&
                         callback) {
            // Prepare the callback, but save it for now instead of invoking it.
            create_device_success_answer_cb = base::BindOnce(
                [](video_capture::mojom::DeviceRequest device_request,
                   video_capture::mojom::DeviceFactory::CreateDeviceCallback
                       callback,
                   video_capture::mojom::DeviceAccessResultCode
                       service_result_code) {
                  std::move(callback).Run(service_result_code);
                },
                std::move(*device_request), std::move(callback),
                service_result_code);
            step_1_run_loop.Quit();
          }));
  EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(1);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0);
  EXPECT_CALL(connection_lost_cb_, Run()).Times(0);
  EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&step_2_run_loop]() {
    step_2_run_loop.Quit();
  }));

  // Exercise
  launcher_->LaunchDeviceAsync(
      kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
      kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_,
      done_cb_.Get());
  step_1_run_loop.Run();
  launcher_->AbortLaunch();

  std::move(create_device_success_answer_cb).Run();
  step_2_run_loop.Run();

  EXPECT_TRUE(launcher_has_connected_to_device_factory_);
  EXPECT_TRUE(launcher_has_released_device_factory_);
}

TEST_F(ServiceVideoCaptureDeviceLauncherTest,
       LaunchingDeviceFailsBecauseDeviceNotFound) {
  base::RunLoop run_loop;

  EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
      .WillOnce(
          Invoke([](const std::string& device_id,
                    video_capture::mojom::DeviceRequest* device_request,
                    video_capture::mojom::DeviceFactory::CreateDeviceCallback&
                        callback) {
            // Note: We must keep |device_request| alive at least until we have
            // sent out the callback. Otherwise, |launcher_| may interpret this
            // as the connection having been lost before receiving the callback.
            base::ThreadTaskRunnerHandle::Get()->PostTask(
                FROM_HERE,
                base::BindOnce(
                    [](video_capture::mojom::DeviceRequest device_request,
                       video_capture::mojom::DeviceFactory::CreateDeviceCallback
                           callback) {
                      std::move(callback).Run(
                          video_capture::mojom::DeviceAccessResultCode::
                              ERROR_DEVICE_NOT_FOUND);
                    },
                    std::move(*device_request), std::move(callback)));
          }));
  EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1);
  EXPECT_CALL(connection_lost_cb_, Run()).Times(0);
  EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() {
    run_loop.Quit();
  }));

  // Exercise
  launcher_->LaunchDeviceAsync(
      kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
      kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_,
      done_cb_.Get());
  run_loop.Run();
}

TEST_F(ServiceVideoCaptureDeviceLauncherTest,
       LaunchingDeviceFailsBecauseFactoryIsUnbound) {
  base::RunLoop run_loop;

  EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1);
  EXPECT_CALL(connection_lost_cb_, Run()).Times(0);
  EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() {
    run_loop.Quit();
  }));

  // Exercise
  device_factory_.reset();
  launcher_->LaunchDeviceAsync(
      kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
      kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_,
      done_cb_.Get());

  run_loop.Run();
}

TEST_F(ServiceVideoCaptureDeviceLauncherTest,
       LaunchingDeviceFailsBecauseConnectionLostWhileLaunching) {
  base::RunLoop run_loop;

  video_capture::mojom::DeviceFactory::CreateDeviceCallback create_device_cb;
  EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
      .WillOnce(
          Invoke([&create_device_cb](
                     const std::string& device_id,
                     video_capture::mojom::DeviceRequest* device_request,
                     video_capture::mojom::DeviceFactory::CreateDeviceCallback&
                         callback) {
            // Simulate connection lost by not invoking |callback| and releasing
            // |device_request|. We have to save |callback| and invoke it later
            // to avoid hitting a DCHECK.
            create_device_cb = std::move(callback);
          }));
  EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
  EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1);
  // Note: |connection_lost_cb_| is only meant to be called when the connection
  // to a successfully-launched device is lost, which is not the case here.
  EXPECT_CALL(connection_lost_cb_, Run()).Times(0);
  EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() {
    run_loop.Quit();
  }));

  // Exercise
  launcher_->LaunchDeviceAsync(
      kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
      kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_,
      done_cb_.Get());

  run_loop.Run();

  // Cut the connection to the factory, so that the outstanding
  // |create_device_cb| will be dropped.
  factory_binding_.reset();
  // We have to invoke the callback, because not doing so triggers a DCHECK.
  const video_capture::mojom::DeviceAccessResultCode arbitrary_result_code =
      video_capture::mojom::DeviceAccessResultCode::SUCCESS;
  std::move(create_device_cb).Run(arbitrary_result_code);
}

TEST_F(ServiceVideoCaptureDeviceLauncherTest,
       ConnectionLostAfterSuccessfulLaunch) {
  video_capture::mojom::DeviceRequest device_request_owned_by_service;
  EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
      .WillOnce(Invoke([&device_request_owned_by_service](
                           const std::string& device_id,
                           video_capture::mojom::DeviceRequest* device_request,
                           video_capture::mojom::DeviceFactory::
                               CreateDeviceCallback& callback) {
        // The service holds on to the |device_request|.
        device_request_owned_by_service = std::move(*device_request);
        base::ThreadTaskRunnerHandle::Get()->PostTask(
            FROM_HERE,
            base::BindOnce(
                [](video_capture::mojom::DeviceFactory::CreateDeviceCallback
                       callback) {
                  std::move(callback).Run(
                      video_capture::mojom::DeviceAccessResultCode::SUCCESS);
                },
                std::move(callback)));
      }));
  std::unique_ptr<LaunchedVideoCaptureDevice> launched_device;
  EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_))
      .WillOnce(
          Invoke([&launched_device](
                     std::unique_ptr<LaunchedVideoCaptureDevice>* device) {
            // We must keep the launched device alive, because otherwise it will
            // no longer listen for connection errors.
            launched_device = std::move(*device);
          }));
  base::RunLoop step_1_run_loop;
  EXPECT_CALL(done_cb_, Run()).WillOnce(InvokeWithoutArgs([&step_1_run_loop]() {
    step_1_run_loop.Quit();
  }));
  // Exercise step 1
  launcher_->LaunchDeviceAsync(
      kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
      kNullReceiver, connection_lost_cb_.Get(), &mock_callbacks_,
      done_cb_.Get());
  step_1_run_loop.Run();

  base::RunLoop step_2_run_loop;
  EXPECT_CALL(connection_lost_cb_, Run()).WillOnce(Invoke([&step_2_run_loop]() {
    step_2_run_loop.Quit();
  }));
  // Exercise step 2: The service cuts/loses the connection
  device_request_owned_by_service = nullptr;
  step_2_run_loop.Run();

  launcher_.reset();
  wait_for_release_connection_cb_.Run();
  EXPECT_TRUE(launcher_has_connected_to_device_factory_);
  EXPECT_TRUE(launcher_has_released_device_factory_);
}

}  // namespace content