| 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 "device/devices_app/usb/device_manager_impl.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/run_loop.h" | |
| 17 #include "base/thread_task_runner_handle.h" | |
| 18 #include "device/core/mock_device_client.h" | |
| 19 #include "device/devices_app/usb/device_impl.h" | |
| 20 #include "device/devices_app/usb/fake_permission_provider.h" | |
| 21 #include "device/usb/mock_usb_device.h" | |
| 22 #include "device/usb/mock_usb_device_handle.h" | |
| 23 #include "device/usb/mock_usb_service.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 using ::testing::Invoke; | |
| 27 using ::testing::_; | |
| 28 | |
| 29 namespace device { | |
| 30 namespace usb { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 class USBDeviceManagerImplTest : public testing::Test { | |
| 35 public: | |
| 36 USBDeviceManagerImplTest() : message_loop_(new base::MessageLoop) {} | |
| 37 ~USBDeviceManagerImplTest() override {} | |
| 38 | |
| 39 protected: | |
| 40 DeviceManagerPtr ConnectToDeviceManager() { | |
| 41 PermissionProviderPtr permission_provider; | |
| 42 permission_provider_.Bind(mojo::GetProxy(&permission_provider)); | |
| 43 DeviceManagerPtr device_manager; | |
| 44 DeviceManagerImpl::Create(std::move(permission_provider), | |
| 45 mojo::GetProxy(&device_manager)); | |
| 46 return device_manager; | |
| 47 } | |
| 48 | |
| 49 MockDeviceClient device_client_; | |
| 50 | |
| 51 private: | |
| 52 FakePermissionProvider permission_provider_; | |
| 53 scoped_ptr<base::MessageLoop> message_loop_; | |
| 54 }; | |
| 55 | |
| 56 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids, | |
| 57 const base::Closure& continuation, | |
| 58 mojo::Array<DeviceInfoPtr> results) { | |
| 59 EXPECT_EQ(expected_guids.size(), results.size()); | |
| 60 std::set<std::string> actual_guids; | |
| 61 for (size_t i = 0; i < results.size(); ++i) | |
| 62 actual_guids.insert(results[i]->guid); | |
| 63 EXPECT_EQ(expected_guids, actual_guids); | |
| 64 continuation.Run(); | |
| 65 } | |
| 66 | |
| 67 void ExpectDeviceChangesAndThen( | |
| 68 const std::set<std::string>& expected_added_guids, | |
| 69 const std::set<std::string>& expected_removed_guids, | |
| 70 const base::Closure& continuation, | |
| 71 DeviceChangeNotificationPtr results) { | |
| 72 EXPECT_EQ(expected_added_guids.size(), results->devices_added.size()); | |
| 73 std::set<std::string> actual_added_guids; | |
| 74 for (size_t i = 0; i < results->devices_added.size(); ++i) | |
| 75 actual_added_guids.insert(results->devices_added[i]->guid); | |
| 76 EXPECT_EQ(expected_added_guids, actual_added_guids); | |
| 77 EXPECT_EQ(expected_removed_guids.size(), results->devices_removed.size()); | |
| 78 std::set<std::string> actual_removed_guids; | |
| 79 for (size_t i = 0; i < results->devices_removed.size(); ++i) | |
| 80 actual_removed_guids.insert(results->devices_removed[i]->guid); | |
| 81 EXPECT_EQ(expected_removed_guids, actual_removed_guids); | |
| 82 continuation.Run(); | |
| 83 } | |
| 84 | |
| 85 void ExpectDeviceInfoAndThen(const std::string& expected_guid, | |
| 86 const base::Closure& continuation, | |
| 87 DeviceInfoPtr device_info) { | |
| 88 ASSERT_TRUE(device_info); | |
| 89 EXPECT_EQ(expected_guid, device_info->guid); | |
| 90 continuation.Run(); | |
| 91 } | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 // Test basic GetDevices functionality to ensure that all mock devices are | |
| 96 // returned by the service. | |
| 97 TEST_F(USBDeviceManagerImplTest, GetDevices) { | |
| 98 scoped_refptr<MockUsbDevice> device0 = | |
| 99 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | |
| 100 scoped_refptr<MockUsbDevice> device1 = | |
| 101 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); | |
| 102 scoped_refptr<MockUsbDevice> device2 = | |
| 103 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR"); | |
| 104 | |
| 105 device_client_.usb_service()->AddDevice(device0); | |
| 106 device_client_.usb_service()->AddDevice(device1); | |
| 107 device_client_.usb_service()->AddDevice(device2); | |
| 108 | |
| 109 DeviceManagerPtr device_manager = ConnectToDeviceManager(); | |
| 110 | |
| 111 EnumerationOptionsPtr options = EnumerationOptions::New(); | |
| 112 options->filters = mojo::Array<DeviceFilterPtr>::New(1); | |
| 113 options->filters[0] = DeviceFilter::New(); | |
| 114 options->filters[0]->has_vendor_id = true; | |
| 115 options->filters[0]->vendor_id = 0x1234; | |
| 116 | |
| 117 std::set<std::string> guids; | |
| 118 guids.insert(device0->guid()); | |
| 119 guids.insert(device1->guid()); | |
| 120 guids.insert(device2->guid()); | |
| 121 | |
| 122 base::RunLoop loop; | |
| 123 device_manager->GetDevices( | |
| 124 std::move(options), | |
| 125 base::Bind(&ExpectDevicesAndThen, guids, loop.QuitClosure())); | |
| 126 loop.Run(); | |
| 127 } | |
| 128 | |
| 129 // Test requesting a single Device by GUID. | |
| 130 TEST_F(USBDeviceManagerImplTest, GetDevice) { | |
| 131 scoped_refptr<MockUsbDevice> mock_device = | |
| 132 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | |
| 133 | |
| 134 device_client_.usb_service()->AddDevice(mock_device); | |
| 135 | |
| 136 DeviceManagerPtr device_manager = ConnectToDeviceManager(); | |
| 137 | |
| 138 { | |
| 139 base::RunLoop loop; | |
| 140 DevicePtr device; | |
| 141 device_manager->GetDevice(mock_device->guid(), mojo::GetProxy(&device)); | |
| 142 device->GetDeviceInfo(base::Bind(&ExpectDeviceInfoAndThen, | |
| 143 mock_device->guid(), loop.QuitClosure())); | |
| 144 loop.Run(); | |
| 145 } | |
| 146 | |
| 147 DevicePtr bad_device; | |
| 148 device_manager->GetDevice("not a real guid", mojo::GetProxy(&bad_device)); | |
| 149 | |
| 150 { | |
| 151 base::RunLoop loop; | |
| 152 bad_device.set_connection_error_handler(loop.QuitClosure()); | |
| 153 loop.Run(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 // Test requesting device enumeration updates with GetDeviceChanges. | |
| 158 TEST_F(USBDeviceManagerImplTest, GetDeviceChanges) { | |
| 159 scoped_refptr<MockUsbDevice> device0 = | |
| 160 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | |
| 161 scoped_refptr<MockUsbDevice> device1 = | |
| 162 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); | |
| 163 scoped_refptr<MockUsbDevice> device2 = | |
| 164 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR"); | |
| 165 scoped_refptr<MockUsbDevice> device3 = | |
| 166 new MockUsbDevice(0x1234, 0x567b, "ACME", "Frobinator Xtreme", "STUVWX"); | |
| 167 | |
| 168 device_client_.usb_service()->AddDevice(device0); | |
| 169 | |
| 170 DeviceManagerPtr device_manager = ConnectToDeviceManager(); | |
| 171 | |
| 172 { | |
| 173 // Call GetDevices once to make sure the device manager is up and running | |
| 174 // or else we could end up waiting forever for device changes as the next | |
| 175 // block races with the ServiceThreadHelper startup. | |
| 176 std::set<std::string> guids; | |
| 177 guids.insert(device0->guid()); | |
| 178 base::RunLoop loop; | |
| 179 device_manager->GetDevices( | |
| 180 nullptr, base::Bind(&ExpectDevicesAndThen, guids, loop.QuitClosure())); | |
| 181 loop.Run(); | |
| 182 } | |
| 183 | |
| 184 device_client_.usb_service()->AddDevice(device1); | |
| 185 device_client_.usb_service()->AddDevice(device2); | |
| 186 device_client_.usb_service()->RemoveDevice(device1); | |
| 187 | |
| 188 { | |
| 189 std::set<std::string> added_guids; | |
| 190 std::set<std::string> removed_guids; | |
| 191 added_guids.insert(device2->guid()); | |
| 192 base::RunLoop loop; | |
| 193 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen, | |
| 194 added_guids, removed_guids, | |
| 195 loop.QuitClosure())); | |
| 196 loop.Run(); | |
| 197 } | |
| 198 | |
| 199 device_client_.usb_service()->RemoveDevice(device0); | |
| 200 device_client_.usb_service()->RemoveDevice(device2); | |
| 201 device_client_.usb_service()->AddDevice(device3); | |
| 202 | |
| 203 { | |
| 204 std::set<std::string> added_guids; | |
| 205 std::set<std::string> removed_guids; | |
| 206 added_guids.insert(device3->guid()); | |
| 207 removed_guids.insert(device0->guid()); | |
| 208 removed_guids.insert(device2->guid()); | |
| 209 base::RunLoop loop; | |
| 210 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen, | |
| 211 added_guids, removed_guids, | |
| 212 loop.QuitClosure())); | |
| 213 loop.Run(); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 } // namespace usb | |
| 218 } // namespace device | |
| OLD | NEW |