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