OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <set> | |
6 #include <string> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/run_loop.h" | |
13 #include "device/core/device_client.h" | |
14 #include "device/usb/device_impl.h" | |
15 #include "device/usb/device_manager_impl.h" | |
16 #include "device/usb/mock_usb_device.h" | |
17 #include "device/usb/mock_usb_device_handle.h" | |
18 #include "device/usb/mock_usb_service.h" | |
19 #include "device/usb/public/cpp/device_manager_delegate.h" | |
20 #include "device/usb/public/cpp/device_manager_factory.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" | |
23 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | |
24 | |
25 using ::testing::Invoke; | |
26 using ::testing::_; | |
27 | |
28 namespace device { | |
29 namespace usb { | |
30 | |
31 namespace { | |
32 | |
33 bool DefaultDelegateFilter(const DeviceInfo& device_info) { | |
34 return true; | |
35 } | |
36 | |
37 class TestDeviceManagerDelegate : public DeviceManagerDelegate { | |
38 public: | |
39 using Filter = base::Callback<bool(const DeviceInfo&)>; | |
40 | |
41 TestDeviceManagerDelegate(const Filter& filter) : filter_(filter) {} | |
42 ~TestDeviceManagerDelegate() override {} | |
43 | |
44 void set_filter(const Filter& filter) { filter_ = filter; } | |
45 | |
46 private: | |
47 // DeviceManagerDelegate implementation: | |
48 bool IsDeviceAllowed(const DeviceInfo& device_info) override { | |
49 return filter_.Run(device_info); | |
50 } | |
51 | |
52 Filter filter_; | |
53 }; | |
54 | |
55 class TestDeviceClient : public DeviceClient { | |
56 public: | |
57 TestDeviceClient() : delegate_filter_(base::Bind(&DefaultDelegateFilter)) {} | |
58 ~TestDeviceClient() override {} | |
59 | |
60 MockUsbService& mock_usb_service() { return mock_usb_service_; } | |
61 | |
62 void SetDelegateFilter(const TestDeviceManagerDelegate::Filter& filter) { | |
63 delegate_filter_ = filter; | |
64 } | |
65 | |
66 private: | |
67 // DeviceClient implementation: | |
68 UsbService* GetUsbService() override { return &mock_usb_service_; } | |
69 | |
70 void ConnectToUSBDeviceManager( | |
71 mojo::InterfaceRequest<DeviceManager> request) override { | |
72 new DeviceManagerImpl(request.Pass(), | |
73 scoped_ptr<DeviceManagerDelegate>( | |
74 new TestDeviceManagerDelegate(delegate_filter_))); | |
75 } | |
76 | |
77 TestDeviceManagerDelegate::Filter delegate_filter_; | |
78 MockUsbService mock_usb_service_; | |
79 }; | |
80 | |
81 class USBDeviceManagerImplTest : public testing::Test { | |
82 public: | |
83 USBDeviceManagerImplTest() | |
84 : message_loop_(new base::MessageLoop), | |
85 device_client_(new TestDeviceClient) {} | |
86 ~USBDeviceManagerImplTest() override {} | |
87 | |
88 protected: | |
89 MockUsbService& mock_usb_service() { | |
90 return device_client_->mock_usb_service(); | |
91 } | |
92 | |
93 void SetDelegateFilter(const TestDeviceManagerDelegate::Filter& filter) { | |
94 device_client_->SetDelegateFilter(filter); | |
95 } | |
96 | |
97 private: | |
98 scoped_ptr<base::MessageLoop> message_loop_; | |
99 scoped_ptr<TestDeviceClient> device_client_; | |
100 }; | |
101 | |
102 // This is used this to watch a MessagePipe and run a given callback when the | |
103 // pipe is closed. | |
104 class PipeWatcher : public mojo::ErrorHandler { | |
105 public: | |
106 PipeWatcher(const base::Closure& error_callback) | |
107 : error_callback_(error_callback) {} | |
108 ~PipeWatcher() override {} | |
109 | |
110 private: | |
111 // mojo::ErrorHandler: | |
112 void OnConnectionError() override { error_callback_.Run(); } | |
113 | |
114 const base::Closure error_callback_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(PipeWatcher); | |
117 }; | |
118 | |
119 class MockOpenCallback { | |
120 public: | |
121 explicit MockOpenCallback(UsbDevice* device) : device_(device) {} | |
122 | |
123 void Open(const UsbDevice::OpenCallback& callback) { | |
124 device_handle_ = new MockUsbDeviceHandle(device_); | |
125 callback.Run(device_handle_); | |
126 } | |
127 | |
128 scoped_refptr<MockUsbDeviceHandle> mock_handle() { return device_handle_; } | |
129 | |
130 private: | |
131 UsbDevice* device_; | |
132 scoped_refptr<MockUsbDeviceHandle> device_handle_; | |
133 }; | |
134 | |
135 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids, | |
136 const base::Closure& continuation, | |
137 mojo::Array<DeviceInfoPtr> results) { | |
138 EXPECT_EQ(expected_guids.size(), results.size()); | |
139 std::set<std::string> actual_guids; | |
140 for (size_t i = 0; i < results.size(); ++i) | |
141 actual_guids.insert(results[i]->guid); | |
142 EXPECT_EQ(expected_guids, actual_guids); | |
143 continuation.Run(); | |
144 } | |
145 | |
146 void ExpectDeviceInfoAndThen(const std::string& expected_guid, | |
147 const base::Closure& continuation, | |
148 DeviceInfoPtr device_info) { | |
149 EXPECT_EQ(expected_guid, device_info->guid); | |
150 continuation.Run(); | |
151 } | |
152 | |
153 void ExpectOpenDeviceError(OpenDeviceError expected_error, | |
154 OpenDeviceError actual_error) { | |
155 EXPECT_EQ(expected_error, actual_error); | |
156 } | |
157 | |
158 void FailOnGetDeviceInfoResponse(DeviceInfoPtr device_info) { | |
159 FAIL(); | |
160 } | |
161 | |
162 } // namespace | |
163 | |
164 // Test basic GetDevices functionality to ensure that all mock devices are | |
165 // returned by the service. | |
166 TEST_F(USBDeviceManagerImplTest, GetDevices) { | |
167 scoped_refptr<MockUsbDevice> device0 = | |
168 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | |
169 scoped_refptr<MockUsbDevice> device1 = | |
170 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); | |
171 scoped_refptr<MockUsbDevice> device2 = | |
172 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR"); | |
173 | |
174 mock_usb_service().AddDevice(device0); | |
175 mock_usb_service().AddDevice(device1); | |
176 mock_usb_service().AddDevice(device2); | |
177 | |
178 DeviceManagerPtr device_manager; | |
179 DeviceClient::Get()->ConnectToUSBDeviceManager( | |
180 mojo::GetProxy(&device_manager)); | |
181 | |
182 EnumerationOptionsPtr options = EnumerationOptions::New(); | |
183 options->filters = mojo::Array<DeviceFilterPtr>::New(1); | |
184 options->filters[0] = DeviceFilter::New(); | |
185 options->filters[0]->has_vendor_id = true; | |
186 options->filters[0]->vendor_id = 0x1234; | |
187 | |
188 std::set<std::string> guids; | |
189 guids.insert(device0->guid()); | |
190 guids.insert(device1->guid()); | |
191 guids.insert(device2->guid()); | |
192 | |
193 // One call to GetConfiguration for each device during enumeration. | |
194 EXPECT_CALL(*device0.get(), GetConfiguration()); | |
195 EXPECT_CALL(*device1.get(), GetConfiguration()); | |
196 EXPECT_CALL(*device2.get(), GetConfiguration()); | |
197 | |
198 base::RunLoop loop; | |
199 device_manager->GetDevices( | |
200 options.Pass(), | |
201 base::Bind(&ExpectDevicesAndThen, guids, loop.QuitClosure())); | |
202 loop.Run(); | |
203 } | |
204 | |
205 // Test requesting a single Device by GUID. | |
206 TEST_F(USBDeviceManagerImplTest, OpenDevice) { | |
207 scoped_refptr<MockUsbDevice> mock_device = | |
208 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | |
209 | |
210 mock_usb_service().AddDevice(mock_device); | |
211 | |
212 DeviceManagerPtr device_manager; | |
213 DeviceClient::Get()->ConnectToUSBDeviceManager( | |
214 mojo::GetProxy(&device_manager)); | |
215 | |
216 // Should be called on the mock as a result of OpenDevice() below. | |
217 EXPECT_CALL(*mock_device.get(), Open(_)); | |
218 | |
219 MockOpenCallback open_callback(mock_device.get()); | |
220 ON_CALL(*mock_device.get(), Open(_)) | |
221 .WillByDefault(Invoke(&open_callback, &MockOpenCallback::Open)); | |
222 | |
223 // Should be called on the mock as a result of GetDeviceInfo() below. | |
224 EXPECT_CALL(*mock_device.get(), GetConfiguration()); | |
225 | |
226 { | |
227 base::RunLoop loop; | |
228 DevicePtr device; | |
229 device_manager->OpenDevice( | |
230 mock_device->guid(), mojo::GetProxy(&device), | |
231 base::Bind(&ExpectOpenDeviceError, OPEN_DEVICE_ERROR_OK)); | |
232 device->GetDeviceInfo(base::Bind(&ExpectDeviceInfoAndThen, | |
233 mock_device->guid(), loop.QuitClosure())); | |
234 loop.Run(); | |
235 } | |
236 | |
237 // The device should eventually be closed when its MessagePipe is closed. | |
238 DCHECK(open_callback.mock_handle()); | |
239 EXPECT_CALL(*open_callback.mock_handle().get(), Close()); | |
240 | |
241 DevicePtr bad_device; | |
242 device_manager->OpenDevice( | |
243 "not a real guid", mojo::GetProxy(&bad_device), | |
244 base::Bind(&ExpectOpenDeviceError, OPEN_DEVICE_ERROR_NOT_FOUND)); | |
245 | |
246 { | |
247 base::RunLoop loop; | |
248 scoped_ptr<PipeWatcher> watcher(new PipeWatcher(loop.QuitClosure())); | |
249 bad_device.set_error_handler(watcher.get()); | |
250 bad_device->GetDeviceInfo(base::Bind(&FailOnGetDeviceInfoResponse)); | |
251 loop.Run(); | |
252 } | |
253 } | |
254 | |
255 } // namespace usb | |
256 } // namespace device | |
OLD | NEW |