| 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" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/thread_task_runner_handle.h" |
| 13 #include "device/core/device_client.h" | 14 #include "device/core/device_client.h" |
| 14 #include "device/usb/device_impl.h" | 15 #include "device/usb/device_impl.h" |
| 15 #include "device/usb/device_manager_impl.h" | 16 #include "device/usb/device_manager_impl.h" |
| 16 #include "device/usb/mock_usb_device.h" | 17 #include "device/usb/mock_usb_device.h" |
| 17 #include "device/usb/mock_usb_device_handle.h" | 18 #include "device/usb/mock_usb_device_handle.h" |
| 18 #include "device/usb/mock_usb_service.h" | 19 #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_delegate.h" |
| 20 #include "device/usb/public/cpp/device_manager_factory.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.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" | 23 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
| 24 | 24 |
| 25 using ::testing::Invoke; | 25 using ::testing::Invoke; |
| 26 using ::testing::_; | 26 using ::testing::_; |
| 27 | 27 |
| 28 namespace device { | 28 namespace device { |
| 29 namespace usb { | 29 namespace usb { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 bool DefaultDelegateFilter(const DeviceInfo& device_info) { | |
| 34 return true; | |
| 35 } | |
| 36 | |
| 37 class TestDeviceManagerDelegate : public DeviceManagerDelegate { | 33 class TestDeviceManagerDelegate : public DeviceManagerDelegate { |
| 38 public: | 34 public: |
| 39 using Filter = base::Callback<bool(const DeviceInfo&)>; | 35 TestDeviceManagerDelegate() {} |
| 40 | |
| 41 TestDeviceManagerDelegate(const Filter& filter) : filter_(filter) {} | |
| 42 ~TestDeviceManagerDelegate() override {} | 36 ~TestDeviceManagerDelegate() override {} |
| 43 | 37 |
| 44 void set_filter(const Filter& filter) { filter_ = filter; } | |
| 45 | |
| 46 private: | 38 private: |
| 47 // DeviceManagerDelegate implementation: | 39 // DeviceManagerDelegate implementation: |
| 48 bool IsDeviceAllowed(const DeviceInfo& device_info) override { | 40 bool IsDeviceAllowed(const DeviceInfo& device_info) override { return true; } |
| 49 return filter_.Run(device_info); | |
| 50 } | |
| 51 | |
| 52 Filter filter_; | |
| 53 }; | 41 }; |
| 54 | 42 |
| 55 class TestDeviceClient : public DeviceClient { | 43 class TestDeviceClient : public DeviceClient { |
| 56 public: | 44 public: |
| 57 TestDeviceClient() : delegate_filter_(base::Bind(&DefaultDelegateFilter)) {} | 45 TestDeviceClient() {} |
| 58 ~TestDeviceClient() override {} | 46 ~TestDeviceClient() override {} |
| 59 | 47 |
| 60 MockUsbService& mock_usb_service() { return mock_usb_service_; } | 48 MockUsbService& mock_usb_service() { return mock_usb_service_; } |
| 61 | 49 |
| 62 void SetDelegateFilter(const TestDeviceManagerDelegate::Filter& filter) { | |
| 63 delegate_filter_ = filter; | |
| 64 } | |
| 65 | |
| 66 private: | 50 private: |
| 67 // DeviceClient implementation: | 51 // DeviceClient implementation: |
| 68 UsbService* GetUsbService() override { return &mock_usb_service_; } | 52 UsbService* GetUsbService() override { return &mock_usb_service_; } |
| 69 | 53 |
| 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_; | 54 MockUsbService mock_usb_service_; |
| 79 }; | 55 }; |
| 80 | 56 |
| 81 class USBDeviceManagerImplTest : public testing::Test { | 57 class USBDeviceManagerImplTest : public testing::Test { |
| 82 public: | 58 public: |
| 83 USBDeviceManagerImplTest() | 59 USBDeviceManagerImplTest() |
| 84 : message_loop_(new base::MessageLoop), | 60 : message_loop_(new base::MessageLoop), |
| 85 device_client_(new TestDeviceClient) {} | 61 device_client_(new TestDeviceClient) {} |
| 86 ~USBDeviceManagerImplTest() override {} | 62 ~USBDeviceManagerImplTest() override {} |
| 87 | 63 |
| 88 protected: | 64 protected: |
| 89 MockUsbService& mock_usb_service() { | 65 MockUsbService& mock_usb_service() { |
| 90 return device_client_->mock_usb_service(); | 66 return device_client_->mock_usb_service(); |
| 91 } | 67 } |
| 92 | 68 |
| 93 void SetDelegateFilter(const TestDeviceManagerDelegate::Filter& filter) { | 69 DeviceManagerPtr ConnectToDeviceManager() { |
| 94 device_client_->SetDelegateFilter(filter); | 70 DeviceManagerPtr device_manager; |
| 71 new DeviceManagerImpl( |
| 72 mojo::GetProxy(&device_manager), |
| 73 scoped_ptr<DeviceManagerDelegate>(new TestDeviceManagerDelegate), |
| 74 base::ThreadTaskRunnerHandle::Get()); |
| 75 return device_manager.Pass(); |
| 95 } | 76 } |
| 96 | 77 |
| 97 private: | 78 private: |
| 98 scoped_ptr<base::MessageLoop> message_loop_; | 79 scoped_ptr<base::MessageLoop> message_loop_; |
| 99 scoped_ptr<TestDeviceClient> device_client_; | 80 scoped_ptr<TestDeviceClient> device_client_; |
| 100 }; | 81 }; |
| 101 | 82 |
| 102 // This is used this to watch a MessagePipe and run a given callback when the | 83 // This is used this to watch a MessagePipe and run a given callback when the |
| 103 // pipe is closed. | 84 // pipe is closed. |
| 104 class PipeWatcher : public mojo::ErrorHandler { | 85 class PipeWatcher : public mojo::ErrorHandler { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | 149 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); |
| 169 scoped_refptr<MockUsbDevice> device1 = | 150 scoped_refptr<MockUsbDevice> device1 = |
| 170 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); | 151 new MockUsbDevice(0x1234, 0x5679, "ACME", "Frobinator+", "GHIJKL"); |
| 171 scoped_refptr<MockUsbDevice> device2 = | 152 scoped_refptr<MockUsbDevice> device2 = |
| 172 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR"); | 153 new MockUsbDevice(0x1234, 0x567a, "ACME", "Frobinator Mk II", "MNOPQR"); |
| 173 | 154 |
| 174 mock_usb_service().AddDevice(device0); | 155 mock_usb_service().AddDevice(device0); |
| 175 mock_usb_service().AddDevice(device1); | 156 mock_usb_service().AddDevice(device1); |
| 176 mock_usb_service().AddDevice(device2); | 157 mock_usb_service().AddDevice(device2); |
| 177 | 158 |
| 178 DeviceManagerPtr device_manager; | 159 DeviceManagerPtr device_manager = ConnectToDeviceManager(); |
| 179 DeviceClient::Get()->ConnectToUSBDeviceManager( | |
| 180 mojo::GetProxy(&device_manager)); | |
| 181 | 160 |
| 182 EnumerationOptionsPtr options = EnumerationOptions::New(); | 161 EnumerationOptionsPtr options = EnumerationOptions::New(); |
| 183 options->filters = mojo::Array<DeviceFilterPtr>::New(1); | 162 options->filters = mojo::Array<DeviceFilterPtr>::New(1); |
| 184 options->filters[0] = DeviceFilter::New(); | 163 options->filters[0] = DeviceFilter::New(); |
| 185 options->filters[0]->has_vendor_id = true; | 164 options->filters[0]->has_vendor_id = true; |
| 186 options->filters[0]->vendor_id = 0x1234; | 165 options->filters[0]->vendor_id = 0x1234; |
| 187 | 166 |
| 188 std::set<std::string> guids; | 167 std::set<std::string> guids; |
| 189 guids.insert(device0->guid()); | 168 guids.insert(device0->guid()); |
| 190 guids.insert(device1->guid()); | 169 guids.insert(device1->guid()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 202 loop.Run(); | 181 loop.Run(); |
| 203 } | 182 } |
| 204 | 183 |
| 205 // Test requesting a single Device by GUID. | 184 // Test requesting a single Device by GUID. |
| 206 TEST_F(USBDeviceManagerImplTest, OpenDevice) { | 185 TEST_F(USBDeviceManagerImplTest, OpenDevice) { |
| 207 scoped_refptr<MockUsbDevice> mock_device = | 186 scoped_refptr<MockUsbDevice> mock_device = |
| 208 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); | 187 new MockUsbDevice(0x1234, 0x5678, "ACME", "Frobinator", "ABCDEF"); |
| 209 | 188 |
| 210 mock_usb_service().AddDevice(mock_device); | 189 mock_usb_service().AddDevice(mock_device); |
| 211 | 190 |
| 212 DeviceManagerPtr device_manager; | 191 DeviceManagerPtr device_manager = ConnectToDeviceManager(); |
| 213 DeviceClient::Get()->ConnectToUSBDeviceManager( | |
| 214 mojo::GetProxy(&device_manager)); | |
| 215 | 192 |
| 216 // Should be called on the mock as a result of OpenDevice() below. | 193 // Should be called on the mock as a result of OpenDevice() below. |
| 217 EXPECT_CALL(*mock_device.get(), Open(_)); | 194 EXPECT_CALL(*mock_device.get(), Open(_)); |
| 218 | 195 |
| 219 MockOpenCallback open_callback(mock_device.get()); | 196 MockOpenCallback open_callback(mock_device.get()); |
| 220 ON_CALL(*mock_device.get(), Open(_)) | 197 ON_CALL(*mock_device.get(), Open(_)) |
| 221 .WillByDefault(Invoke(&open_callback, &MockOpenCallback::Open)); | 198 .WillByDefault(Invoke(&open_callback, &MockOpenCallback::Open)); |
| 222 | 199 |
| 223 // Should be called on the mock as a result of GetDeviceInfo() below. | 200 // Should be called on the mock as a result of GetDeviceInfo() below. |
| 224 EXPECT_CALL(*mock_device.get(), GetConfiguration()); | 201 EXPECT_CALL(*mock_device.get(), GetConfiguration()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 247 base::RunLoop loop; | 224 base::RunLoop loop; |
| 248 scoped_ptr<PipeWatcher> watcher(new PipeWatcher(loop.QuitClosure())); | 225 scoped_ptr<PipeWatcher> watcher(new PipeWatcher(loop.QuitClosure())); |
| 249 bad_device.set_error_handler(watcher.get()); | 226 bad_device.set_error_handler(watcher.get()); |
| 250 bad_device->GetDeviceInfo(base::Bind(&FailOnGetDeviceInfoResponse)); | 227 bad_device->GetDeviceInfo(base::Bind(&FailOnGetDeviceInfoResponse)); |
| 251 loop.Run(); | 228 loop.Run(); |
| 252 } | 229 } |
| 253 } | 230 } |
| 254 | 231 |
| 255 } // namespace usb | 232 } // namespace usb |
| 256 } // namespace device | 233 } // namespace device |
| OLD | NEW |