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

Side by Side Diff: device/usb/device_manager_impl_unittest.cc

Issue 1155163008: Build a basic Mojo service framework for device/usb (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: DISALLOW Created 5 years, 6 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
« no previous file with comments | « device/usb/device_manager_impl.cc ('k') | device/usb/public/cpp/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include <vector>
8
9 #include "base/barrier_closure.h"
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "device/core/device_client.h"
15 #include "device/usb/device_impl.h"
16 #include "device/usb/device_manager_impl.h"
17 #include "device/usb/mock_usb_device.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/interface_request.h"
23
24 namespace device {
25 namespace usb {
26
27 namespace {
28
29 bool DefaultDelegateFilter(const DeviceInfo& device_info) {
30 return true;
31 }
32
33 class TestDeviceManagerDelegate : public DeviceManagerDelegate {
34 public:
35 using Filter = base::Callback<bool(const DeviceInfo&)>;
36
37 TestDeviceManagerDelegate(const Filter& filter) : filter_(filter) {}
38 ~TestDeviceManagerDelegate() override {}
39
40 void set_filter(const Filter& filter) { filter_ = filter; }
41
42 private:
43 // DeviceManagerDelegate implementation:
44 bool IsDeviceAllowed(const DeviceInfo& device_info) override {
45 return filter_.Run(device_info);
46 }
47
48 Filter filter_;
49 };
50
51 class TestDeviceClient : public DeviceClient {
52 public:
53 TestDeviceClient() : delegate_filter_(base::Bind(&DefaultDelegateFilter)) {}
54 ~TestDeviceClient() override {}
55
56 MockUsbService& mock_usb_service() { return mock_usb_service_; }
57
58 void SetDelegateFilter(const TestDeviceManagerDelegate::Filter& filter) {
59 delegate_filter_ = filter;
60 }
61
62 private:
63 // DeviceClient implementation:
64 UsbService* GetUsbService() override { return &mock_usb_service_; }
65
66 void ConnectToUSBDeviceManager(
67 mojo::InterfaceRequest<DeviceManager> request) override {
68 new DeviceManagerImpl(request.Pass(),
69 scoped_ptr<DeviceManagerDelegate>(
70 new TestDeviceManagerDelegate(delegate_filter_)));
71 }
72
73 TestDeviceManagerDelegate::Filter delegate_filter_;
74 MockUsbService mock_usb_service_;
75 };
76
77 class DeviceManagerImplTest : public testing::Test {
78 public:
79 DeviceManagerImplTest()
80 : message_loop_(new base::MessageLoop),
81 device_client_(new TestDeviceClient) {}
82 ~DeviceManagerImplTest() override {}
83
84 protected:
85 MockUsbService& mock_usb_service() {
86 return device_client_->mock_usb_service();
87 }
88
89 void SetDelegateFilter(const TestDeviceManagerDelegate::Filter& filter) {
90 device_client_->SetDelegateFilter(filter);
91 }
92
93 private:
94 scoped_ptr<base::MessageLoop> message_loop_;
95 scoped_ptr<TestDeviceClient> device_client_;
96 };
97
98 void VerifyDevicesAndThen(const std::set<std::string>& expected_serials,
99 scoped_ptr<std::set<std::string>> actual_serials,
100 const base::Closure& continuation) {
101 EXPECT_EQ(expected_serials, *actual_serials);
102 continuation.Run();
103 }
104
105 void OnGetDeviceInfo(std::set<std::string>* actual_serials,
106 const base::Closure& barrier,
107 DevicePtr device,
108 DeviceInfoPtr info) {
109 actual_serials->insert(info->serial_number.To<std::string>());
110 barrier.Run();
111 }
112
113 void ExpectDevicesAndThen(const std::set<std::string>& serials,
114 const base::Closure& continuation,
115 mojo::Array<EnumerationResultPtr> results) {
116 EXPECT_EQ(serials.size(), results.size());
117 scoped_ptr<std::set<std::string>> actual_serials(new std::set<std::string>);
118 std::set<std::string>* actual_serials_raw = actual_serials.get();
119 base::Closure barrier = base::BarrierClosure(
120 static_cast<int>(results.size()),
121 base::Bind(&VerifyDevicesAndThen, serials, base::Passed(&actual_serials),
122 continuation));
123 for (size_t i = 0; i < results.size(); ++i) {
124 DevicePtr device = results[i]->device.Pass();
125 Device* raw_device = device.get();
126 raw_device->GetDeviceInfo(base::Bind(&OnGetDeviceInfo, actual_serials_raw,
127 barrier, base::Passed(&device)));
128 }
129 }
130
131 } // namespace
132
133 // Test basic GetDevices functionality to ensure that all mock devices are
134 // returned by the service.
135 TEST_F(DeviceManagerImplTest, GetDevices) {
136 scoped_refptr<MockUsbDevice> device0 =
137 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF");
138 scoped_refptr<MockUsbDevice> device1 =
139 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL");
140 scoped_refptr<MockUsbDevice> device2 =
141 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR");
142
143 mock_usb_service().AddDevice(device0);
144 mock_usb_service().AddDevice(device1);
145 mock_usb_service().AddDevice(device2);
146
147 DeviceManagerPtr device_manager;
148 DeviceClient::Get()->ConnectToUSBDeviceManager(
149 mojo::GetProxy(&device_manager));
150
151 EnumerationOptionsPtr options = EnumerationOptions::New();
152 options->filters = mojo::Array<DeviceFilterPtr>::New(1);
153 options->filters[0] = DeviceFilter::New();
154 options->filters[0]->has_vendor_id = true;
155 options->filters[0]->vendor_id = 0x1234;
156
157 std::set<std::string> serials;
158 serials.insert("ABCDEF");
159 serials.insert("GHIJKL");
160 serials.insert("MNOPQR");
161
162 EXPECT_CALL(*device0.get(), GetConfiguration());
163 EXPECT_CALL(*device1.get(), GetConfiguration());
164 EXPECT_CALL(*device2.get(), GetConfiguration());
165
166 base::RunLoop run_loop;
167 device_manager->GetDevices(
168 options.Pass(),
169 base::Bind(&ExpectDevicesAndThen, serials, run_loop.QuitClosure()));
170 run_loop.Run();
171 }
172
173 } // namespace usb
174 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/device_manager_impl.cc ('k') | device/usb/public/cpp/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698