| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 permission_provider.Pass(), | 82 permission_provider.Pass(), |
| 83 base::ThreadTaskRunnerHandle::Get()); | 83 base::ThreadTaskRunnerHandle::Get()); |
| 84 return device_manager.Pass(); | 84 return device_manager.Pass(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 scoped_ptr<TestDeviceClient> device_client_; | 88 scoped_ptr<TestDeviceClient> device_client_; |
| 89 scoped_ptr<base::MessageLoop> message_loop_; | 89 scoped_ptr<base::MessageLoop> message_loop_; |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 class MockOpenCallback { | |
| 93 public: | |
| 94 explicit MockOpenCallback(UsbDevice* device) : device_(device) {} | |
| 95 | |
| 96 void Open(const UsbDevice::OpenCallback& callback) { | |
| 97 device_handle_ = new MockUsbDeviceHandle(device_); | |
| 98 callback.Run(device_handle_); | |
| 99 } | |
| 100 | |
| 101 scoped_refptr<MockUsbDeviceHandle> mock_handle() { return device_handle_; } | |
| 102 | |
| 103 private: | |
| 104 UsbDevice* device_; | |
| 105 scoped_refptr<MockUsbDeviceHandle> device_handle_; | |
| 106 }; | |
| 107 | |
| 108 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids, | 92 void ExpectDevicesAndThen(const std::set<std::string>& expected_guids, |
| 109 const base::Closure& continuation, | 93 const base::Closure& continuation, |
| 110 mojo::Array<DeviceInfoPtr> results) { | 94 mojo::Array<DeviceInfoPtr> results) { |
| 111 EXPECT_EQ(expected_guids.size(), results.size()); | 95 EXPECT_EQ(expected_guids.size(), results.size()); |
| 112 std::set<std::string> actual_guids; | 96 std::set<std::string> actual_guids; |
| 113 for (size_t i = 0; i < results.size(); ++i) | 97 for (size_t i = 0; i < results.size(); ++i) |
| 114 actual_guids.insert(results[i]->guid); | 98 actual_guids.insert(results[i]->guid); |
| 115 EXPECT_EQ(expected_guids, actual_guids); | 99 EXPECT_EQ(expected_guids, actual_guids); |
| 116 continuation.Run(); | 100 continuation.Run(); |
| 117 } | 101 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 130 std::set<std::string> actual_removed_guids; | 114 std::set<std::string> actual_removed_guids; |
| 131 for (size_t i = 0; i < results->devices_removed.size(); ++i) | 115 for (size_t i = 0; i < results->devices_removed.size(); ++i) |
| 132 actual_removed_guids.insert(results->devices_removed[i]->guid); | 116 actual_removed_guids.insert(results->devices_removed[i]->guid); |
| 133 EXPECT_EQ(expected_removed_guids, actual_removed_guids); | 117 EXPECT_EQ(expected_removed_guids, actual_removed_guids); |
| 134 continuation.Run(); | 118 continuation.Run(); |
| 135 } | 119 } |
| 136 | 120 |
| 137 void ExpectDeviceInfoAndThen(const std::string& expected_guid, | 121 void ExpectDeviceInfoAndThen(const std::string& expected_guid, |
| 138 const base::Closure& continuation, | 122 const base::Closure& continuation, |
| 139 DeviceInfoPtr device_info) { | 123 DeviceInfoPtr device_info) { |
| 124 ASSERT_TRUE(device_info); |
| 140 EXPECT_EQ(expected_guid, device_info->guid); | 125 EXPECT_EQ(expected_guid, device_info->guid); |
| 141 continuation.Run(); | 126 continuation.Run(); |
| 142 } | 127 } |
| 143 | 128 |
| 144 void ExpectOpenDeviceError(OpenDeviceError expected_error, | |
| 145 OpenDeviceError actual_error) { | |
| 146 EXPECT_EQ(expected_error, actual_error); | |
| 147 } | |
| 148 | |
| 149 void FailOnGetDeviceInfoResponse(DeviceInfoPtr device_info) { | |
| 150 FAIL(); | |
| 151 } | |
| 152 | |
| 153 } // namespace | 129 } // namespace |
| 154 | 130 |
| 155 // Test basic GetDevices functionality to ensure that all mock devices are | 131 // Test basic GetDevices functionality to ensure that all mock devices are |
| 156 // returned by the service. | 132 // returned by the service. |
| 157 TEST_F(USBDeviceManagerImplTest, GetDevices) { | 133 TEST_F(USBDeviceManagerImplTest, GetDevices) { |
| 158 scoped_refptr<MockUsbDevice> device0 = | 134 scoped_refptr<MockUsbDevice> device0 = |
| 159 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | 135 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); |
| 160 scoped_refptr<MockUsbDevice> device1 = | 136 scoped_refptr<MockUsbDevice> device1 = |
| 161 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); | 137 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); |
| 162 scoped_refptr<MockUsbDevice> device2 = | 138 scoped_refptr<MockUsbDevice> device2 = |
| (...skipping 17 matching lines...) Expand all Loading... |
| 180 guids.insert(device2->guid()); | 156 guids.insert(device2->guid()); |
| 181 | 157 |
| 182 base::RunLoop loop; | 158 base::RunLoop loop; |
| 183 device_manager->GetDevices( | 159 device_manager->GetDevices( |
| 184 options.Pass(), | 160 options.Pass(), |
| 185 base::Bind(&ExpectDevicesAndThen, guids, loop.QuitClosure())); | 161 base::Bind(&ExpectDevicesAndThen, guids, loop.QuitClosure())); |
| 186 loop.Run(); | 162 loop.Run(); |
| 187 } | 163 } |
| 188 | 164 |
| 189 // Test requesting a single Device by GUID. | 165 // Test requesting a single Device by GUID. |
| 190 TEST_F(USBDeviceManagerImplTest, OpenDevice) { | 166 TEST_F(USBDeviceManagerImplTest, GetDevice) { |
| 191 scoped_refptr<MockUsbDevice> mock_device = | 167 scoped_refptr<MockUsbDevice> mock_device = |
| 192 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | 168 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); |
| 193 | 169 |
| 194 mock_usb_service().AddDevice(mock_device); | 170 mock_usb_service().AddDevice(mock_device); |
| 195 | 171 |
| 196 DeviceManagerPtr device_manager = ConnectToDeviceManager(); | 172 DeviceManagerPtr device_manager = ConnectToDeviceManager(); |
| 197 | 173 |
| 198 // Should be called on the mock as a result of OpenDevice() below. | |
| 199 EXPECT_CALL(*mock_device.get(), Open(_)); | |
| 200 | |
| 201 MockOpenCallback open_callback(mock_device.get()); | |
| 202 ON_CALL(*mock_device.get(), Open(_)) | |
| 203 .WillByDefault(Invoke(&open_callback, &MockOpenCallback::Open)); | |
| 204 | |
| 205 { | 174 { |
| 206 base::RunLoop loop; | 175 base::RunLoop loop; |
| 207 DevicePtr device; | 176 DevicePtr device; |
| 208 device_manager->OpenDevice( | 177 device_manager->GetDevice(mock_device->guid(), mojo::GetProxy(&device)); |
| 209 mock_device->guid(), mojo::GetProxy(&device), | |
| 210 base::Bind(&ExpectOpenDeviceError, OPEN_DEVICE_ERROR_OK)); | |
| 211 device->GetDeviceInfo(base::Bind(&ExpectDeviceInfoAndThen, | 178 device->GetDeviceInfo(base::Bind(&ExpectDeviceInfoAndThen, |
| 212 mock_device->guid(), loop.QuitClosure())); | 179 mock_device->guid(), loop.QuitClosure())); |
| 213 loop.Run(); | 180 loop.Run(); |
| 214 } | 181 } |
| 215 | 182 |
| 216 // The device should eventually be closed when its MessagePipe is closed. | |
| 217 DCHECK(open_callback.mock_handle()); | |
| 218 EXPECT_CALL(*open_callback.mock_handle().get(), Close()); | |
| 219 | |
| 220 DevicePtr bad_device; | 183 DevicePtr bad_device; |
| 221 device_manager->OpenDevice( | 184 device_manager->GetDevice("not a real guid", mojo::GetProxy(&bad_device)); |
| 222 "not a real guid", mojo::GetProxy(&bad_device), | |
| 223 base::Bind(&ExpectOpenDeviceError, OPEN_DEVICE_ERROR_NOT_FOUND)); | |
| 224 | 185 |
| 225 { | 186 { |
| 226 base::RunLoop loop; | 187 base::RunLoop loop; |
| 227 bad_device.set_connection_error_handler(loop.QuitClosure()); | 188 bad_device.set_connection_error_handler(loop.QuitClosure()); |
| 228 bad_device->GetDeviceInfo(base::Bind(&FailOnGetDeviceInfoResponse)); | |
| 229 loop.Run(); | 189 loop.Run(); |
| 230 } | 190 } |
| 231 } | 191 } |
| 232 | 192 |
| 233 // Test requesting device enumeration updates with GetDeviceChanges. | 193 // Test requesting device enumeration updates with GetDeviceChanges. |
| 234 TEST_F(USBDeviceManagerImplTest, GetDeviceChanges) { | 194 TEST_F(USBDeviceManagerImplTest, GetDeviceChanges) { |
| 235 scoped_refptr<MockUsbDevice> device0 = | 195 scoped_refptr<MockUsbDevice> device0 = |
| 236 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | 196 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); |
| 237 scoped_refptr<MockUsbDevice> device1 = | 197 scoped_refptr<MockUsbDevice> device1 = |
| 238 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); | 198 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 base::RunLoop loop; | 245 base::RunLoop loop; |
| 286 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen, | 246 device_manager->GetDeviceChanges(base::Bind(&ExpectDeviceChangesAndThen, |
| 287 added_guids, removed_guids, | 247 added_guids, removed_guids, |
| 288 loop.QuitClosure())); | 248 loop.QuitClosure())); |
| 289 loop.Run(); | 249 loop.Run(); |
| 290 } | 250 } |
| 291 } | 251 } |
| 292 | 252 |
| 293 } // namespace usb | 253 } // namespace usb |
| 294 } // namespace device | 254 } // namespace device |
| OLD | NEW |