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 "base/test/mock_callback.h" | 7 #include "base/test/mock_callback.h" |
| 8 #include "services/video_capture/public/interfaces/constants.mojom.h" |
8 #include "services/video_capture/public/interfaces/device_factory.mojom.h" | 9 #include "services/video_capture/public/interfaces/device_factory.mojom.h" |
9 #include "services/video_capture/test/service_test.h" | 10 #include "services/video_capture/test/device_factory_provider_test.h" |
10 | 11 |
11 using testing::Exactly; | 12 using testing::Exactly; |
12 using testing::_; | 13 using testing::_; |
13 using testing::Invoke; | 14 using testing::Invoke; |
14 using testing::InvokeWithoutArgs; | 15 using testing::InvokeWithoutArgs; |
15 | 16 |
16 namespace video_capture { | 17 namespace video_capture { |
17 | 18 |
18 // This alias ensures test output is easily attributed to this service's tests. | 19 // This alias ensures test output is easily attributed to this service's tests. |
19 // TODO(rockot/chfremer): Consider just renaming the type. | 20 // TODO(rockot/chfremer): Consider just renaming the type. |
20 using VideoCaptureServiceTest = ServiceTest; | 21 using VideoCaptureServiceDeviceFactoryProviderTest = DeviceFactoryProviderTest; |
21 | 22 |
22 // Tests that an answer arrives from the service when calling | 23 // Tests that an answer arrives from the service when calling |
23 // GetDeviceInfos(). | 24 // GetDeviceInfos(). |
24 TEST_F(VideoCaptureServiceTest, GetDeviceInfosCallbackArrives) { | 25 TEST_F(VideoCaptureServiceDeviceFactoryProviderTest, |
| 26 GetDeviceInfosCallbackArrives) { |
25 base::RunLoop wait_loop; | 27 base::RunLoop wait_loop; |
26 EXPECT_CALL(device_info_receiver_, Run(_)) | 28 EXPECT_CALL(device_info_receiver_, Run(_)) |
27 .Times(Exactly(1)) | 29 .Times(Exactly(1)) |
28 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); | 30 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); |
29 | 31 |
30 factory_->GetDeviceInfos(device_info_receiver_.Get()); | 32 factory_->GetDeviceInfos(device_info_receiver_.Get()); |
31 wait_loop.Run(); | 33 wait_loop.Run(); |
32 } | 34 } |
33 | 35 |
34 TEST_F(VideoCaptureServiceTest, FakeDeviceFactoryEnumeratesOneDevice) { | 36 TEST_F(VideoCaptureServiceDeviceFactoryProviderTest, |
| 37 FakeDeviceFactoryEnumeratesOneDevice) { |
35 base::RunLoop wait_loop; | 38 base::RunLoop wait_loop; |
36 size_t num_devices_enumerated = 0; | 39 size_t num_devices_enumerated = 0; |
37 EXPECT_CALL(device_info_receiver_, Run(_)) | 40 EXPECT_CALL(device_info_receiver_, Run(_)) |
38 .Times(Exactly(1)) | 41 .Times(Exactly(1)) |
39 .WillOnce( | 42 .WillOnce( |
40 Invoke([&wait_loop, &num_devices_enumerated]( | 43 Invoke([&wait_loop, &num_devices_enumerated]( |
41 const std::vector<media::VideoCaptureDeviceInfo>& infos) { | 44 const std::vector<media::VideoCaptureDeviceInfo>& infos) { |
42 num_devices_enumerated = infos.size(); | 45 num_devices_enumerated = infos.size(); |
43 wait_loop.Quit(); | 46 wait_loop.Quit(); |
44 })); | 47 })); |
45 | 48 |
46 factory_->GetDeviceInfos(device_info_receiver_.Get()); | 49 factory_->GetDeviceInfos(device_info_receiver_.Get()); |
47 wait_loop.Run(); | 50 wait_loop.Run(); |
48 ASSERT_EQ(1u, num_devices_enumerated); | 51 ASSERT_EQ(1u, num_devices_enumerated); |
49 } | 52 } |
50 | 53 |
51 // Tests that VideoCaptureDeviceFactory::CreateDeviceProxy() returns an error | 54 // Tests that VideoCaptureDeviceFactory::CreateDeviceProxy() returns an error |
52 // code when trying to create a device for an invalid descriptor. | 55 // code when trying to create a device for an invalid descriptor. |
53 TEST_F(VideoCaptureServiceTest, ErrorCodeOnCreateDeviceForInvalidDescriptor) { | 56 TEST_F(VideoCaptureServiceDeviceFactoryProviderTest, |
| 57 ErrorCodeOnCreateDeviceForInvalidDescriptor) { |
54 const std::string invalid_device_id = "invalid"; | 58 const std::string invalid_device_id = "invalid"; |
55 base::RunLoop wait_loop; | 59 base::RunLoop wait_loop; |
56 mojom::DevicePtr fake_device_proxy; | 60 mojom::DevicePtr fake_device_proxy; |
57 base::MockCallback<mojom::DeviceFactory::CreateDeviceCallback> | 61 base::MockCallback<mojom::DeviceFactory::CreateDeviceCallback> |
58 create_device_proxy_callback; | 62 create_device_proxy_callback; |
59 EXPECT_CALL(create_device_proxy_callback, | 63 EXPECT_CALL(create_device_proxy_callback, |
60 Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND)) | 64 Run(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND)) |
61 .Times(1) | 65 .Times(1) |
62 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); | 66 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); |
63 factory_->GetDeviceInfos(device_info_receiver_.Get()); | 67 factory_->GetDeviceInfos(device_info_receiver_.Get()); |
64 factory_->CreateDevice(invalid_device_id, | 68 factory_->CreateDevice(invalid_device_id, |
65 mojo::MakeRequest(&fake_device_proxy), | 69 mojo::MakeRequest(&fake_device_proxy), |
66 create_device_proxy_callback.Get()); | 70 create_device_proxy_callback.Get()); |
67 wait_loop.Run(); | 71 wait_loop.Run(); |
68 } | 72 } |
69 | 73 |
| 74 // Tests that the service requests to be closed when the last client disconnects |
| 75 // after not having done anything other than obtaining a connection to the |
| 76 // fake device factory. |
| 77 TEST_F(VideoCaptureServiceDeviceFactoryProviderTest, |
| 78 ServiceQuitsWhenNoClientConnected) { |
| 79 base::RunLoop wait_loop; |
| 80 EXPECT_CALL(*service_state_observer_, OnServiceStopped(_)) |
| 81 .WillOnce(Invoke([&wait_loop](const service_manager::Identity& identity) { |
| 82 if (identity.name() == mojom::kServiceName) |
| 83 wait_loop.Quit(); |
| 84 })); |
| 85 |
| 86 // Exercise: Disconnect from service by discarding our references to it. |
| 87 factory_.reset(); |
| 88 factory_provider_.reset(); |
| 89 |
| 90 wait_loop.Run(); |
| 91 } |
| 92 |
70 } // namespace video_capture | 93 } // namespace video_capture |
OLD | NEW |