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/video_capture_device_factory. mojom.h" | 8 #include "services/video_capture/public/interfaces/video_capture_device_factory. mojom.h" |
9 #include "services/video_capture/public/interfaces/video_capture_service.mojom.h " | |
9 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
10 | 11 |
11 using testing::Exactly; | 12 using testing::Exactly; |
12 using testing::_; | 13 using testing::_; |
14 using testing::Invoke; | |
15 using testing::InvokeWithoutArgs; | |
13 | 16 |
14 namespace video_capture { | 17 namespace video_capture { |
15 | 18 |
16 ACTION_P(RunClosure, closure) { | 19 class MockDeviceDescriptorReceiver { |
17 closure.Run(); | |
18 } | |
19 | |
20 class MockClient { | |
21 public: | 20 public: |
22 // Use forwarding method to work around gmock not supporting move-only types. | 21 // Use forwarding method to work around gmock not supporting move-only types. |
23 void HandleEnumerateDeviceDescriptorsCallback( | 22 void HandleEnumerateDeviceDescriptorsCallback( |
24 std::vector<mojom::VideoCaptureDeviceDescriptorPtr> descriptors) { | 23 std::vector<mojom::VideoCaptureDeviceDescriptorPtr> descriptors) { |
25 OnEnumerateDeviceDescriptorsCallback(descriptors); | 24 OnEnumerateDeviceDescriptorsCallback(descriptors); |
26 } | 25 } |
27 | 26 |
28 MOCK_METHOD1( | 27 MOCK_METHOD1( |
29 OnEnumerateDeviceDescriptorsCallback, | 28 OnEnumerateDeviceDescriptorsCallback, |
30 void(const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>&)); | 29 void(const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>&)); |
31 }; | 30 }; |
32 | 31 |
32 void HandleAddFakeVideoCaptureDeviceCallback( | |
mcasas
2016/08/18 21:11:21
Unused? Remove if so.
chfremer
2016/08/19 16:10:19
Done.
| |
33 base::RunLoop* loop, | |
34 mojom::VideoCaptureDeviceDescriptorPtr* out_descriptor, | |
35 mojom::VideoCaptureDeviceDescriptorPtr in_descriptor) { | |
36 *out_descriptor = std::move(in_descriptor); | |
37 loop->Quit(); | |
38 } | |
39 | |
33 class VideoCaptureServiceTest : public shell::test::ServiceTest { | 40 class VideoCaptureServiceTest : public shell::test::ServiceTest { |
34 public: | 41 public: |
35 VideoCaptureServiceTest() | 42 VideoCaptureServiceTest() |
36 : shell::test::ServiceTest("exe:video_capture_unittests") {} | 43 : shell::test::ServiceTest("exe:video_capture_unittests") {} |
37 ~VideoCaptureServiceTest() override {} | 44 ~VideoCaptureServiceTest() override {} |
38 | 45 |
39 void SetUp() override { | 46 void SetUp() override { |
40 ServiceTest::SetUp(); | 47 ServiceTest::SetUp(); |
41 connector()->ConnectToInterface("mojo:video_capture", &factory_); | 48 connector()->ConnectToInterface("mojo:video_capture", &service_); |
49 service_->ConnectToFakeDeviceFactory(mojo::GetProxy(&factory_)); | |
42 } | 50 } |
43 | 51 |
44 protected: | 52 protected: |
53 mojom::VideoCaptureServicePtr service_; | |
45 mojom::VideoCaptureDeviceFactoryPtr factory_; | 54 mojom::VideoCaptureDeviceFactoryPtr factory_; |
46 MockClient client_; | 55 MockDeviceDescriptorReceiver descriptor_receiver_; |
47 }; | 56 }; |
48 | 57 |
49 // Tests that an answer arrives from the service when calling | 58 // Tests that an answer arrives from the service when calling |
50 // EnumerateDeviceDescriptors(). | 59 // EnumerateDeviceDescriptors(). |
51 TEST_F(VideoCaptureServiceTest, EnumerateDeviceDescriptorsCallbackArrives) { | 60 TEST_F(VideoCaptureServiceTest, EnumerateDeviceDescriptorsCallbackArrives) { |
52 base::RunLoop wait_loop; | 61 base::RunLoop wait_loop; |
53 EXPECT_CALL(client_, OnEnumerateDeviceDescriptorsCallback(_)) | 62 EXPECT_CALL(descriptor_receiver_, OnEnumerateDeviceDescriptorsCallback(_)) |
54 .Times(Exactly(1)) | 63 .Times(Exactly(1)) |
55 .WillOnce(RunClosure(wait_loop.QuitClosure())); | 64 .WillOnce(InvokeWithoutArgs([&wait_loop]() { wait_loop.Quit(); })); |
65 ; | |
mcasas
2016/08/18 21:11:21
Remove extra ;
chfremer
2016/08/19 16:10:19
Done.
| |
56 | 66 |
57 factory_->EnumerateDeviceDescriptors( | 67 factory_->EnumerateDeviceDescriptors(base::Bind( |
58 base::Bind(&MockClient::HandleEnumerateDeviceDescriptorsCallback, | 68 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, |
59 base::Unretained(&client_))); | 69 base::Unretained(&descriptor_receiver_))); |
60 wait_loop.Run(); | 70 wait_loop.Run(); |
61 } | 71 } |
62 | 72 |
73 TEST_F(VideoCaptureServiceTest, FakeDeviceFactroyEnumeratesOneDevice) { | |
mcasas
2016/08/18 21:11:21
s/Factroy/Factory/
chfremer
2016/08/19 16:10:19
Done.
| |
74 base::RunLoop wait_loop; | |
75 size_t num_devices_enumerated = 0; | |
76 EXPECT_CALL(descriptor_receiver_, OnEnumerateDeviceDescriptorsCallback(_)) | |
77 .Times(Exactly(1)) | |
78 .WillOnce(Invoke([&wait_loop, &num_devices_enumerated]( | |
79 const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>& | |
80 descriptors) { | |
81 num_devices_enumerated = descriptors.size(); | |
82 wait_loop.Quit(); | |
83 })); | |
84 | |
85 factory_->EnumerateDeviceDescriptors(base::Bind( | |
86 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, | |
87 base::Unretained(&descriptor_receiver_))); | |
88 wait_loop.Run(); | |
89 ASSERT_EQ(1u, num_devices_enumerated); | |
90 } | |
91 | |
63 } // namespace video_capture | 92 } // namespace video_capture |
OLD | NEW |