| 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" | 7 #include "services/shell/public/cpp/service_test.h" |
| 8 #include "services/video_capture/public/interfaces/service_configurator.mojom.h" |
| 9 #include "services/video_capture/public/interfaces/video_capture_device_access.m
ojom.h" |
| 8 #include "services/video_capture/public/interfaces/video_capture_device_factory.
mojom.h" | 10 #include "services/video_capture/public/interfaces/video_capture_device_factory.
mojom.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 10 | 12 |
| 11 using testing::Exactly; | 13 using testing::Exactly; |
| 12 using testing::_; | 14 using testing::_; |
| 15 using testing::Invoke; |
| 16 using testing::InvokeWithoutArgs; |
| 13 | 17 |
| 14 namespace video_capture { | 18 namespace video_capture { |
| 15 | 19 |
| 16 ACTION_P(RunClosure, closure) { | 20 class MockDeviceDescriptorReceiver { |
| 17 closure.Run(); | |
| 18 } | |
| 19 | |
| 20 class MockClient { | |
| 21 public: | 21 public: |
| 22 // Use forwarding method to work around gmock not supporting move-only types. | 22 // Use forwarding method to work around gmock not supporting move-only types. |
| 23 void HandleEnumerateDeviceDescriptorsCallback( | 23 void HandleEnumerateDeviceDescriptorsCallback( |
| 24 std::vector<mojom::VideoCaptureDeviceDescriptorPtr> descriptors) { | 24 std::vector<mojom::VideoCaptureDeviceDescriptorPtr> descriptors) { |
| 25 OnEnumerateDeviceDescriptorsCallback(descriptors); | 25 OnEnumerateDeviceDescriptorsCallback(descriptors); |
| 26 } | 26 } |
| 27 | 27 |
| 28 MOCK_METHOD1(OnEnumerateDeviceDescriptorsCallback, | 28 MOCK_METHOD1(OnEnumerateDeviceDescriptorsCallback, |
| 29 void(const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>& | 29 void(const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>& |
| 30 descriptors)); | 30 descriptors)); |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 void HandleAddFakeVideoCaptureDeviceCallback( |
| 34 base::RunLoop* loop, |
| 35 mojom::VideoCaptureDeviceDescriptorPtr* out_descriptor, |
| 36 mojom::VideoCaptureDeviceDescriptorPtr in_descriptor) { |
| 37 *out_descriptor = std::move(in_descriptor); |
| 38 loop->Quit(); |
| 39 } |
| 40 |
| 33 class VideoCaptureServiceTest : public shell::test::ServiceTest { | 41 class VideoCaptureServiceTest : public shell::test::ServiceTest { |
| 34 public: | 42 public: |
| 35 VideoCaptureServiceTest() | 43 VideoCaptureServiceTest() |
| 36 : shell::test::ServiceTest("exe:video_capture_unittests") {} | 44 : shell::test::ServiceTest("exe:video_capture_unittests") {} |
| 37 ~VideoCaptureServiceTest() override {} | 45 ~VideoCaptureServiceTest() override {} |
| 38 | 46 |
| 39 void SetUp() override { | 47 void SetUp() override { |
| 40 ServiceTest::SetUp(); | 48 ServiceTest::SetUp(); |
| 49 connector()->ConnectToInterface("mojo:video_capture", &configurator_); |
| 41 connector()->ConnectToInterface("mojo:video_capture", &factory_); | 50 connector()->ConnectToInterface("mojo:video_capture", &factory_); |
| 42 } | 51 } |
| 43 | 52 |
| 44 protected: | 53 protected: |
| 54 mojom::VideoCaptureDeviceDescriptorPtr AddFakeCaptureDevice() { |
| 55 mojom::VideoCaptureDeviceDescriptorPtr fake_device_descriptor; |
| 56 base::RunLoop wait_loop; |
| 57 configurator_->AddFakeVideoCaptureDevice( |
| 58 base::Bind(HandleAddFakeVideoCaptureDeviceCallback, &wait_loop, |
| 59 &fake_device_descriptor)); |
| 60 wait_loop.Run(); |
| 61 return fake_device_descriptor; |
| 62 } |
| 63 |
| 64 mojom::ServiceConfiguratorPtr configurator_; |
| 45 mojom::VideoCaptureDeviceFactoryPtr factory_; | 65 mojom::VideoCaptureDeviceFactoryPtr factory_; |
| 46 MockClient client_; | 66 MockDeviceDescriptorReceiver descriptor_receiver_; |
| 47 }; | 67 }; |
| 48 | 68 |
| 49 TEST_F(VideoCaptureServiceTest, EnumerateDeviceDescriptorsCallbackArrives) { | 69 TEST_F(VideoCaptureServiceTest, EnumerateDeviceDescriptorsCallbackArrives) { |
| 50 base::RunLoop wait_loop; | 70 base::RunLoop wait_loop; |
| 51 EXPECT_CALL(client_, OnEnumerateDeviceDescriptorsCallback(_)) | 71 EXPECT_CALL(descriptor_receiver_, OnEnumerateDeviceDescriptorsCallback(_)) |
| 52 .Times(Exactly(1)) | 72 .Times(Exactly(1)) |
| 53 .WillOnce(RunClosure(wait_loop.QuitClosure())); | 73 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); |
| 54 ; | |
| 55 | 74 |
| 56 factory_->EnumerateDeviceDescriptors( | 75 factory_->EnumerateDeviceDescriptors(base::Bind( |
| 57 base::Bind(&MockClient::HandleEnumerateDeviceDescriptorsCallback, | 76 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, |
| 58 base::Unretained(&client_))); | 77 base::Unretained(&descriptor_receiver_))); |
| 59 wait_loop.Run(); | 78 wait_loop.Run(); |
| 60 } | 79 } |
| 61 | 80 |
| 81 TEST_F(VideoCaptureServiceTest, FakeCaptureDeviceGetsEnumerated) { |
| 82 auto fake_device_descriptor = AddFakeCaptureDevice(); |
| 83 |
| 84 base::RunLoop wait_loop; |
| 85 EXPECT_CALL(descriptor_receiver_, OnEnumerateDeviceDescriptorsCallback(_)) |
| 86 .Times(Exactly(1)) |
| 87 .WillOnce(Invoke([&fake_device_descriptor, &wait_loop]( |
| 88 const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>& |
| 89 descriptors) { |
| 90 bool fake_device_found = false; |
| 91 for (const auto& descriptor : descriptors) { |
| 92 if (descriptor->device_id == fake_device_descriptor->device_id) { |
| 93 fake_device_found = true; |
| 94 break; |
| 95 } |
| 96 } |
| 97 ASSERT_TRUE(fake_device_found); |
| 98 wait_loop.Quit(); |
| 99 })); |
| 100 |
| 101 factory_->EnumerateDeviceDescriptors(base::Bind( |
| 102 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, |
| 103 base::Unretained(&descriptor_receiver_))); |
| 104 wait_loop.Run(); |
| 105 } |
| 106 |
| 62 } // namespace video_capture | 107 } // namespace video_capture |
| OLD | NEW |