Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(804)

Side by Side Diff: content/browser/renderer_host/media/service_video_capture_device_launcher_unittest.cc

Issue 2886303002: [Mojo Video Capture] Add unit tests for ServiceVideoCaptureDeviceLauncher (Closed)
Patch Set: Rebase to May 19th Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 base::test::ScopedTaskEnvironment scoped_task_environment_;
72 MockDeviceFactory mock_device_factory_;
73 MockVideoCaptureDeviceLauncherCallbacks mock_callbacks_;
74 video_capture::mojom::DeviceFactoryPtr device_factory_;
75 std::unique_ptr<mojo::Binding<video_capture::mojom::DeviceFactory>>
76 factory_binding_;
77 std::unique_ptr<ServiceVideoCaptureDeviceLauncher> launcher_;
78 base::MockCallback<base::OnceClosure> done_cb;
79
80 private:
81 DISALLOW_COPY_AND_ASSIGN(ServiceVideoCaptureDeviceLauncherTest);
82 };
83
84 TEST_F(ServiceVideoCaptureDeviceLauncherTest, LaunchingDeviceSucceeds) {
85 base::RunLoop run_loop;
86
87 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
88 .WillOnce(Invoke([](const std::string& device_id,
89 video_capture::mojom::DeviceRequest* device_request,
90 const video_capture::mojom::DeviceFactory::
91 CreateDeviceCallback& callback) {
92 // Note: We must keep |device_request| alive at least until we have
93 // sent out the callback. Otherwise, |launcher_| may interpret this
94 // as the connection having been lost before receiving the callback.
95 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE,
97 base::Bind(
98 [](video_capture::mojom::DeviceRequest device_request,
99 const video_capture::mojom::DeviceFactory::
100 CreateDeviceCallback& callback) {
101 callback.Run(
102 video_capture::mojom::DeviceAccessResultCode::SUCCESS);
103 },
104 base::Passed(device_request), callback));
105 }));
106 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(1);
107 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
108 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0);
109 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() {
110 run_loop.Quit();
111 }));
112
113 // Exercise
114 launcher_->LaunchDeviceAsync(
115 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
116 kNullReceiver, &mock_callbacks_, done_cb.Get());
117
118 run_loop.Run();
119 }
120
121 TEST_F(ServiceVideoCaptureDeviceLauncherTest, LaunchingDeviceIsAborted) {
122 base::RunLoop step_1_run_loop;
123 base::RunLoop step_2_run_loop;
124
125 base::Closure create_device_success_answer_cb;
126 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
127 .WillOnce(Invoke([&create_device_success_answer_cb, &step_1_run_loop](
128 const std::string& device_id,
129 video_capture::mojom::DeviceRequest* device_request,
130 const video_capture::mojom::DeviceFactory::
131 CreateDeviceCallback& callback) {
132 // Prepare the callback, but save it for now instead of invoking it.
133 create_device_success_answer_cb = base::Bind(
134 [](video_capture::mojom::DeviceRequest device_request,
135 const video_capture::mojom::DeviceFactory::CreateDeviceCallback&
136 callback) {
137 callback.Run(
138 video_capture::mojom::DeviceAccessResultCode::SUCCESS);
emircan 2017/05/23 23:05:40 As we discussed offline, if this returns ERROR_DEV
chfremer 2017/05/23 23:39:18 Done.
139 },
140 base::Passed(device_request), callback);
141 step_1_run_loop.Quit();
142 }));
143 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
144 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(1);
145 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(0);
146 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&step_2_run_loop]() {
147 step_2_run_loop.Quit();
148 }));
149
150 // Exercise
151 launcher_->LaunchDeviceAsync(
152 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
153 kNullReceiver, &mock_callbacks_, done_cb.Get());
154 step_1_run_loop.Run();
155 launcher_->AbortLaunch();
156
157 create_device_success_answer_cb.Run();
158 step_2_run_loop.Run();
159 }
160
161 TEST_F(ServiceVideoCaptureDeviceLauncherTest,
162 LaunchingDeviceFailsBecauseDeviceNotFound) {
163 base::RunLoop run_loop;
164
165 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
166 .WillOnce(Invoke(
167 [](const std::string& device_id,
168 video_capture::mojom::DeviceRequest* device_request,
169 const video_capture::mojom::DeviceFactory::CreateDeviceCallback&
170 callback) {
171 // Note: We must keep |device_request| alive at least until we have
172 // sent out the callback. Otherwise, |launcher_| may interpret this
173 // as the connection having been lost before receiving the callback.
174 base::ThreadTaskRunnerHandle::Get()->PostTask(
175 FROM_HERE,
176 base::Bind(
177 [](video_capture::mojom::DeviceRequest device_request,
178 const video_capture::mojom::DeviceFactory::
179 CreateDeviceCallback& callback) {
180 callback.Run(
181 video_capture::mojom::DeviceAccessResultCode::
182 ERROR_DEVICE_NOT_FOUND);
183 },
184 base::Passed(device_request), callback));
185 }));
186 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
187 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
188 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1);
189 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() {
190 run_loop.Quit();
191 }));
192
193 // Exercise
194 launcher_->LaunchDeviceAsync(
195 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
196 kNullReceiver, &mock_callbacks_, done_cb.Get());
197
198 run_loop.Run();
199 }
200
201 TEST_F(ServiceVideoCaptureDeviceLauncherTest,
202 LaunchingDeviceFailsBecauseFactoryIsUnbound) {
203 base::RunLoop run_loop;
204
205 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
206 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
207 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1);
208 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() {
209 run_loop.Quit();
210 }));
211
212 // Exercise
213 device_factory_.reset();
214 launcher_->LaunchDeviceAsync(
215 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
216 kNullReceiver, &mock_callbacks_, done_cb.Get());
217
218 run_loop.Run();
219 }
220
221 TEST_F(ServiceVideoCaptureDeviceLauncherTest,
222 LaunchingDeviceFailsBecauseConnectionLostWhileLaunching) {
223 base::RunLoop run_loop;
224
225 video_capture::mojom::DeviceFactory::CreateDeviceCallback create_device_cb;
226 EXPECT_CALL(mock_device_factory_, DoCreateDevice(kStubDeviceId, _, _))
227 .WillOnce(Invoke(
228 [&create_device_cb](
229 const std::string& device_id,
230 video_capture::mojom::DeviceRequest* device_request,
231 const video_capture::mojom::DeviceFactory::CreateDeviceCallback&
232 callback) {
233 // Simulate connection lost by not invoking |callback| and releasing
234 // |device_request|. We have to save |callback| and invoke it later
235 // to avoid hitting a DCHECK.
236 create_device_cb = callback;
237 }));
238 EXPECT_CALL(mock_callbacks_, DoOnDeviceLaunched(_)).Times(0);
239 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchAborted()).Times(0);
240 EXPECT_CALL(mock_callbacks_, OnDeviceLaunchFailed()).Times(1);
241 EXPECT_CALL(done_cb, Run()).WillOnce(InvokeWithoutArgs([&run_loop]() {
242 run_loop.Quit();
243 }));
244
245 // Exercise
246 launcher_->LaunchDeviceAsync(
247 kStubDeviceId, content::MEDIA_DEVICE_VIDEO_CAPTURE, kArbitraryParams,
248 kNullReceiver, &mock_callbacks_, done_cb.Get());
249
250 run_loop.Run();
251
252 // Cut the connection to the factory, so that the outstanding
253 // |create_device_cb| will be dropped.
254 factory_binding_.reset();
255 // We have to invoke the callback, because not doing so triggers a DCHECK.
256 const video_capture::mojom::DeviceAccessResultCode arbitrary_result_code =
257 video_capture::mojom::DeviceAccessResultCode::SUCCESS;
258 create_device_cb.Run(arbitrary_result_code);
259 }
260
261 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698