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