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

Side by Side Diff: services/video_capture/service_unittest.cc

Issue 2238083004: Video Capture Mojo (1.4b): Implement ability to use fake device instance (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@FillServicePart1
Patch Set: Created 4 years, 4 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
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/mock_video_capture_device_client.h"
8 #include "services/video_capture/public/interfaces/video_capture_device_factory. mojom.h" 9 #include "services/video_capture/public/interfaces/video_capture_device_factory. mojom.h"
9 #include "services/video_capture/public/interfaces/video_capture_service.mojom.h " 10 #include "services/video_capture/public/interfaces/video_capture_service.mojom.h "
10 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
11 12
12 using testing::Exactly; 13 using testing::Exactly;
13 using testing::_; 14 using testing::_;
14 using testing::Invoke; 15 using testing::Invoke;
15 using testing::InvokeWithoutArgs; 16 using testing::InvokeWithoutArgs;
16 17
17 namespace video_capture { 18 namespace video_capture {
18 19
19 class MockDeviceDescriptorReceiver { 20 class MockDeviceDescriptorReceiver {
20 public: 21 public:
21 // 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.
22 void HandleEnumerateDeviceDescriptorsCallback( 23 void HandleEnumerateDeviceDescriptorsCallback(
23 std::vector<mojom::VideoCaptureDeviceDescriptorPtr> descriptors) { 24 std::vector<mojom::VideoCaptureDeviceDescriptorPtr> descriptors) {
24 OnEnumerateDeviceDescriptorsCallback(descriptors); 25 OnEnumerateDeviceDescriptorsCallback(descriptors);
25 } 26 }
26 27
27 MOCK_METHOD1( 28 MOCK_METHOD1(
28 OnEnumerateDeviceDescriptorsCallback, 29 OnEnumerateDeviceDescriptorsCallback,
29 void(const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>&)); 30 void(const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>&));
30 }; 31 };
31 32
33 // Basic test fixture that sets up a connection to the fake device factory.
32 class VideoCaptureServiceTest : public shell::test::ServiceTest { 34 class VideoCaptureServiceTest : public shell::test::ServiceTest {
33 public: 35 public:
34 VideoCaptureServiceTest() 36 VideoCaptureServiceTest()
35 : shell::test::ServiceTest("exe:video_capture_unittests") {} 37 : shell::test::ServiceTest("exe:video_capture_unittests") {}
36 ~VideoCaptureServiceTest() override {} 38 ~VideoCaptureServiceTest() override {}
37 39
38 void SetUp() override { 40 void SetUp() override {
39 ServiceTest::SetUp(); 41 ServiceTest::SetUp();
40 connector()->ConnectToInterface("mojo:video_capture", &service_); 42 connector()->ConnectToInterface("mojo:video_capture", &service_);
41 service_->ConnectToFakeDeviceFactory(mojo::GetProxy(&factory_)); 43 service_->ConnectToFakeDeviceFactory(mojo::GetProxy(&factory_));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 wait_loop.Quit(); 75 wait_loop.Quit();
74 })); 76 }));
75 77
76 factory_->EnumerateDeviceDescriptors(base::Bind( 78 factory_->EnumerateDeviceDescriptors(base::Bind(
77 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback, 79 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback,
78 base::Unretained(&descriptor_receiver_))); 80 base::Unretained(&descriptor_receiver_)));
79 wait_loop.Run(); 81 wait_loop.Run();
80 ASSERT_EQ(1u, num_devices_enumerated); 82 ASSERT_EQ(1u, num_devices_enumerated);
81 } 83 }
82 84
85 // Tests that VideoCaptureDeviceFactory::CreateDeviceProxy() returns an error
86 // code when trying to create a device for an invalid descriptor.
87 TEST_F(VideoCaptureServiceTest, ErrorCodeOnCreateDeviceForInvalidDescriptor) {
88 auto invalid_descriptor = mojom::VideoCaptureDeviceDescriptor::New();
89 invalid_descriptor->device_id = "invalid";
90 invalid_descriptor->model_id = "invalid";
91 base::RunLoop wait_loop;
92 mojom::VideoCaptureDeviceProxyPtr fake_device_proxy_;
93 mojom::DeviceAccessResultCode result_code;
94 factory_->CreateDeviceProxy(
95 std::move(invalid_descriptor), mojo::GetProxy(&fake_device_proxy_),
96 base::Bind(
97 [](base::RunLoop* wait_loop, mojom::DeviceAccessResultCode* target,
98 mojom::DeviceAccessResultCode result_code) {
99 *target = result_code;
100 wait_loop->Quit();
101 },
102 &wait_loop, &result_code));
103 wait_loop.Run();
104 ASSERT_EQ(mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND, result_code);
105 }
106
107 // Test fixture that creates a proxy to the fake device provided by the fake
108 // device factory.
109 class FakeDeviceVideoCaptureServiceTest : public VideoCaptureServiceTest {
mcasas 2016/08/30 01:20:26 Separate this new class and associated test cases
chfremer 2016/08/30 18:22:54 Done.
110 public:
111 FakeDeviceVideoCaptureServiceTest() : VideoCaptureServiceTest() {}
112 ~FakeDeviceVideoCaptureServiceTest() override {}
113
114 void SetUp() override {
115 VideoCaptureServiceTest::SetUp();
116
117 base::RunLoop wait_loop;
118 mojom::VideoCaptureDeviceDescriptorPtr fake_device_descriptor;
119 EXPECT_CALL(descriptor_receiver_, OnEnumerateDeviceDescriptorsCallback(_))
120 .WillOnce(Invoke([&wait_loop, &fake_device_descriptor](
121 const std::vector<mojom::VideoCaptureDeviceDescriptorPtr>&
122 descriptors) {
123 fake_device_descriptor = descriptors[0].Clone();
124 wait_loop.Quit();
125 }));
126 factory_->EnumerateDeviceDescriptors(base::Bind(
127 &MockDeviceDescriptorReceiver::HandleEnumerateDeviceDescriptorsCallback,
128 base::Unretained(&descriptor_receiver_)));
129 wait_loop.Run();
130
131 factory_->CreateDeviceProxy(
132 std::move(fake_device_descriptor), mojo::GetProxy(&fake_device_proxy_),
133 base::Bind([](mojom::DeviceAccessResultCode result_code) {
134 ASSERT_EQ(mojom::DeviceAccessResultCode::SUCCESS, result_code);
135 }));
136 }
137
138 protected:
139 mojom::VideoCaptureDeviceProxyPtr fake_device_proxy_;
140 };
141
142 TEST_F(FakeDeviceVideoCaptureServiceTest, FrameCallbacksArrive) {
143 auto arbitrary_requested_format = mojom::VideoCaptureFormat::New();
144 arbitrary_requested_format->frame_size.SetSize(640, 480);
145 arbitrary_requested_format->frame_rate = 15;
146 arbitrary_requested_format->pixel_format = media::mojom::VideoFormat::I420;
147 arbitrary_requested_format->pixel_storage = mojom::VideoPixelStorage::CPU;
148
149 base::RunLoop wait_loop;
150 const int kNumFramesToWaitFor = 3;
151 int num_frames_arrived = 0;
152 mojom::VideoCaptureDeviceClientPtr client_proxy;
153 MockVideoCaptureDeviceClient client(mojo::GetProxy(&client_proxy));
154 EXPECT_CALL(client, OnFrameAvailablePtr(_))
155 .WillRepeatedly(InvokeWithoutArgs(
156 [&wait_loop, &kNumFramesToWaitFor, &num_frames_arrived]() {
157 num_frames_arrived += 1;
158 if (num_frames_arrived >= kNumFramesToWaitFor) {
159 wait_loop.Quit();
160 }
161 }));
162
163 fake_device_proxy_->Start(std::move(arbitrary_requested_format),
164 mojom::ResolutionChangePolicy::FIXED_RESOLUTION,
165 mojom::PowerLineFrequency::DEFAULT,
166 std::move(client_proxy));
167 wait_loop.Run();
168 }
169
83 } // namespace video_capture 170 } // namespace video_capture
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698