OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "services/shell/public/cpp/service_test.h" | |
8 #include "services/video_capture/public/interfaces/video_capture_device_factory.
mojom.h" | 7 #include "services/video_capture/public/interfaces/video_capture_device_factory.
mojom.h" |
9 #include "services/video_capture/public/interfaces/video_capture_service.mojom.h
" | 8 #include "services/video_capture/video_capture_service_test.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | |
11 | 9 |
12 using testing::Exactly; | 10 using testing::Exactly; |
13 using testing::_; | 11 using testing::_; |
14 using testing::Invoke; | 12 using testing::Invoke; |
15 using testing::InvokeWithoutArgs; | 13 using testing::InvokeWithoutArgs; |
16 | 14 |
17 namespace video_capture { | 15 namespace video_capture { |
18 | 16 |
19 class MockDeviceDescriptorReceiver { | |
20 public: | |
21 // Use forwarding method to work around gmock not supporting move-only types. | |
22 void HandleEnumerateDeviceDescriptorsCallback( | |
23 std::vector<mojom::VideoCaptureDeviceDescriptorPtr> descriptors) { | |
24 OnEnumerateDeviceDescriptorsCallback(descriptors); | |
25 } | |
26 | |
27 MOCK_METHOD1( | |
28 OnEnumerateDeviceDescriptorsCallback, | |
29 void(const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>&)); | |
30 }; | |
31 | |
32 class VideoCaptureServiceTest : public shell::test::ServiceTest { | |
33 public: | |
34 VideoCaptureServiceTest() | |
35 : shell::test::ServiceTest("exe:video_capture_unittests") {} | |
36 ~VideoCaptureServiceTest() override {} | |
37 | |
38 void SetUp() override { | |
39 ServiceTest::SetUp(); | |
40 connector()->ConnectToInterface("mojo:video_capture", &service_); | |
41 service_->ConnectToFakeDeviceFactory(mojo::GetProxy(&factory_)); | |
42 } | |
43 | |
44 protected: | |
45 mojom::VideoCaptureServicePtr service_; | |
46 mojom::VideoCaptureDeviceFactoryPtr factory_; | |
47 MockDeviceDescriptorReceiver descriptor_receiver_; | |
48 }; | |
49 | |
50 // Tests that an answer arrives from the service when calling | 17 // Tests that an answer arrives from the service when calling |
51 // EnumerateDeviceDescriptors(). | 18 // EnumerateDeviceDescriptors(). |
52 TEST_F(VideoCaptureServiceTest, EnumerateDeviceDescriptorsCallbackArrives) { | 19 TEST_F(VideoCaptureServiceTest, EnumerateDeviceDescriptorsCallbackArrives) { |
53 base::RunLoop wait_loop; | 20 base::RunLoop wait_loop; |
54 EXPECT_CALL(descriptor_receiver_, OnEnumerateDeviceDescriptorsCallback(_)) | 21 EXPECT_CALL(descriptor_receiver_, OnEnumerateDeviceDescriptorsCallback(_)) |
55 .Times(Exactly(1)) | 22 .Times(Exactly(1)) |
56 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); | 23 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); |
57 | 24 |
58 factory_->EnumerateDeviceDescriptors(base::Bind( | 25 factory_->EnumerateDeviceDescriptors(base::Bind( |
59 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, | 26 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, |
(...skipping 13 matching lines...) Expand all Loading... |
73 wait_loop.Quit(); | 40 wait_loop.Quit(); |
74 })); | 41 })); |
75 | 42 |
76 factory_->EnumerateDeviceDescriptors(base::Bind( | 43 factory_->EnumerateDeviceDescriptors(base::Bind( |
77 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, | 44 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, |
78 base::Unretained(&descriptor_receiver_))); | 45 base::Unretained(&descriptor_receiver_))); |
79 wait_loop.Run(); | 46 wait_loop.Run(); |
80 ASSERT_EQ(1u, num_devices_enumerated); | 47 ASSERT_EQ(1u, num_devices_enumerated); |
81 } | 48 } |
82 | 49 |
| 50 // Tests that VideoCaptureDeviceFactory::CreateDeviceProxy() returns an error |
| 51 // code when trying to create a device for an invalid descriptor. |
| 52 TEST_F(VideoCaptureServiceTest, ErrorCodeOnCreateDeviceForInvalidDescriptor) { |
| 53 auto invalid_descriptor = mojom::VideoCaptureDeviceDescriptor::New(); |
| 54 invalid_descriptor->device_id = "invalid"; |
| 55 invalid_descriptor->model_id = "invalid"; |
| 56 base::RunLoop wait_loop; |
| 57 mojom::VideoCaptureDeviceProxyPtr fake_device_proxy; |
| 58 mojom::DeviceAccessResultCode result_code; |
| 59 factory_->CreateDeviceProxy( |
| 60 std::move(invalid_descriptor), mojo::GetProxy(&fake_device_proxy), |
| 61 base::Bind( |
| 62 [](base::RunLoop* wait_loop, mojom::DeviceAccessResultCode* target, |
| 63 mojom::DeviceAccessResultCode result_code) { |
| 64 *target = result_code; |
| 65 wait_loop->Quit(); |
| 66 }, |
| 67 &wait_loop, &result_code)); |
| 68 wait_loop.Run(); |
| 69 ASSERT_EQ(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND, result_code); |
| 70 } |
| 71 |
83 } // namespace video_capture | 72 } // namespace video_capture |
OLD | NEW |