| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "device/usb/usb_descriptors.h" |
| 8 #include "device/usb/usb_device.h" | 9 #include "device/usb/usb_device.h" |
| 9 #include "device/usb/usb_device_filter.h" | 10 #include "device/usb/usb_device_filter.h" |
| 10 #include "device/usb/usb_device_handle.h" | 11 #include "device/usb/usb_device_handle.h" |
| 11 #include "device/usb/usb_interface.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace device { | 15 namespace device { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 class MockUsbInterfaceAltSettingDescriptor | 19 using testing::NiceMock; |
| 19 : public UsbInterfaceAltSettingDescriptor { | 20 using testing::ReturnRef; |
| 20 public: | |
| 21 MockUsbInterfaceAltSettingDescriptor(int interface_number, | |
| 22 int alternate_setting, | |
| 23 int interface_class, | |
| 24 int interface_subclass, | |
| 25 int interface_protocol) | |
| 26 : interface_number_(interface_number), | |
| 27 alternate_setting_(alternate_setting), | |
| 28 interface_class_(interface_class), | |
| 29 interface_subclass_(interface_subclass), | |
| 30 interface_protocol_(interface_protocol) {} | |
| 31 | |
| 32 virtual size_t GetNumEndpoints() const OVERRIDE { return 0; } | |
| 33 virtual scoped_refptr<const UsbEndpointDescriptor> GetEndpoint( | |
| 34 size_t index) const OVERRIDE { | |
| 35 return NULL; | |
| 36 } | |
| 37 virtual int GetInterfaceNumber() const OVERRIDE { return interface_number_; } | |
| 38 virtual int GetAlternateSetting() const OVERRIDE { | |
| 39 return alternate_setting_; | |
| 40 } | |
| 41 virtual int GetInterfaceClass() const OVERRIDE { return interface_class_; } | |
| 42 virtual int GetInterfaceSubclass() const OVERRIDE { | |
| 43 return interface_subclass_; | |
| 44 } | |
| 45 virtual int GetInterfaceProtocol() const OVERRIDE { | |
| 46 return interface_protocol_; | |
| 47 } | |
| 48 | |
| 49 protected: | |
| 50 virtual ~MockUsbInterfaceAltSettingDescriptor() {} | |
| 51 | |
| 52 private: | |
| 53 int interface_number_; | |
| 54 int alternate_setting_; | |
| 55 int interface_class_; | |
| 56 int interface_subclass_; | |
| 57 int interface_protocol_; | |
| 58 }; | |
| 59 | |
| 60 typedef std::vector<scoped_refptr<UsbInterfaceAltSettingDescriptor> > | |
| 61 UsbInterfaceAltSettingDescriptorList; | |
| 62 | |
| 63 class MockUsbInterfaceDescriptor : public UsbInterfaceDescriptor { | |
| 64 public: | |
| 65 MockUsbInterfaceDescriptor( | |
| 66 const UsbInterfaceAltSettingDescriptorList& alt_settings) | |
| 67 : alt_settings_(alt_settings) {} | |
| 68 | |
| 69 virtual size_t GetNumAltSettings() const OVERRIDE { | |
| 70 return alt_settings_.size(); | |
| 71 } | |
| 72 virtual scoped_refptr<const UsbInterfaceAltSettingDescriptor> GetAltSetting( | |
| 73 size_t index) const OVERRIDE { | |
| 74 return alt_settings_[index]; | |
| 75 } | |
| 76 | |
| 77 protected: | |
| 78 virtual ~MockUsbInterfaceDescriptor() {} | |
| 79 | |
| 80 private: | |
| 81 UsbInterfaceAltSettingDescriptorList alt_settings_; | |
| 82 }; | |
| 83 | |
| 84 typedef std::vector<scoped_refptr<UsbInterfaceDescriptor> > | |
| 85 UsbInterfaceDescriptorList; | |
| 86 | |
| 87 class MockUsbConfigDescriptor : public UsbConfigDescriptor { | |
| 88 public: | |
| 89 MockUsbConfigDescriptor(const UsbInterfaceDescriptorList& interfaces) | |
| 90 : interfaces_(interfaces) {} | |
| 91 | |
| 92 virtual size_t GetNumInterfaces() const OVERRIDE { | |
| 93 return interfaces_.size(); | |
| 94 } | |
| 95 virtual scoped_refptr<const UsbInterfaceDescriptor> GetInterface( | |
| 96 size_t index) const OVERRIDE { | |
| 97 return interfaces_[index]; | |
| 98 } | |
| 99 | |
| 100 protected: | |
| 101 virtual ~MockUsbConfigDescriptor() {} | |
| 102 | |
| 103 private: | |
| 104 UsbInterfaceDescriptorList interfaces_; | |
| 105 }; | |
| 106 | 21 |
| 107 class MockUsbDevice : public UsbDevice { | 22 class MockUsbDevice : public UsbDevice { |
| 108 public: | 23 public: |
| 109 MockUsbDevice(uint16 vendor_id, | 24 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) |
| 110 uint16 product_id, | 25 : UsbDevice(vendor_id, product_id, unique_id) {} |
| 111 uint32 unique_id, | |
| 112 scoped_refptr<UsbConfigDescriptor> config_desc) | |
| 113 : UsbDevice(vendor_id, product_id, unique_id), | |
| 114 config_desc_(config_desc) {} | |
| 115 | 26 |
| 116 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { return NULL; } | 27 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>()); |
| 117 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { | 28 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); |
| 118 NOTREACHED(); | |
| 119 return true; | |
| 120 } | |
| 121 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
| 122 virtual void RequestUsbAccess( | 30 MOCK_METHOD2(RequestUsbAccess, void(int, const base::Callback<void(bool)>&)); |
| 123 int interface_id, | 31 #endif |
| 124 const base::Callback<void(bool success)>& callback) OVERRIDE { | 32 MOCK_METHOD0(GetConfiguration, const UsbConfigDescriptor&()); |
| 125 NOTREACHED(); | |
| 126 } | |
| 127 #endif // OS_CHROMEOS | |
| 128 virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces() OVERRIDE { | |
| 129 return config_desc_; | |
| 130 } | |
| 131 | |
| 132 protected: | |
| 133 virtual ~MockUsbDevice() {} | |
| 134 | 33 |
| 135 private: | 34 private: |
| 136 scoped_refptr<UsbConfigDescriptor> config_desc_; | 35 virtual ~MockUsbDevice() {} |
| 137 }; | 36 }; |
| 138 | 37 |
| 139 class UsbFilterTest : public testing::Test { | 38 class UsbFilterTest : public testing::Test { |
| 140 public: | 39 public: |
| 141 virtual void SetUp() OVERRIDE { | 40 virtual void SetUp() OVERRIDE { |
| 142 UsbInterfaceAltSettingDescriptorList alt_settings; | 41 UsbInterfaceDescriptor interface; |
| 143 alt_settings.push_back(make_scoped_refptr( | 42 interface.interface_number = 1; |
| 144 new MockUsbInterfaceAltSettingDescriptor(1, 0, 0xFF, 0x42, 1))); | 43 interface.alternate_setting = 0; |
| 44 interface.interface_class = 0xFF; |
| 45 interface.interface_subclass = 0x42; |
| 46 interface.interface_protocol = 0x01; |
| 47 config_.interfaces.push_back(interface); |
| 145 | 48 |
| 146 UsbInterfaceDescriptorList interfaces; | 49 android_phone_ = new MockUsbDevice(0x18d1, 0x4ee2, 0); |
| 147 interfaces.push_back( | 50 ON_CALL(*android_phone_.get(), GetConfiguration()) |
| 148 make_scoped_refptr(new MockUsbInterfaceDescriptor(alt_settings))); | 51 .WillByDefault(ReturnRef(config_)); |
| 149 | |
| 150 scoped_refptr<UsbConfigDescriptor> config_desc( | |
| 151 new MockUsbConfigDescriptor(interfaces)); | |
| 152 | |
| 153 android_phone_ = new MockUsbDevice(0x18d1, 0x4ee2, 0, config_desc); | |
| 154 } | 52 } |
| 155 | 53 |
| 156 protected: | 54 protected: |
| 157 scoped_refptr<UsbDevice> android_phone_; | 55 UsbConfigDescriptor config_; |
| 56 scoped_refptr<MockUsbDevice> android_phone_; |
| 158 }; | 57 }; |
| 159 | 58 |
| 160 TEST_F(UsbFilterTest, MatchAny) { | 59 TEST_F(UsbFilterTest, MatchAny) { |
| 161 UsbDeviceFilter filter; | 60 UsbDeviceFilter filter; |
| 162 ASSERT_TRUE(filter.Matches(android_phone_)); | 61 ASSERT_TRUE(filter.Matches(android_phone_)); |
| 163 } | 62 } |
| 164 | 63 |
| 165 TEST_F(UsbFilterTest, MatchVendorId) { | 64 TEST_F(UsbFilterTest, MatchVendorId) { |
| 166 UsbDeviceFilter filter; | 65 UsbDeviceFilter filter; |
| 167 filter.SetVendorId(0x18d1); | 66 filter.SetVendorId(0x18d1); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 142 |
| 244 TEST_F(UsbFilterTest, MatchesAnyVendorIdNegative) { | 143 TEST_F(UsbFilterTest, MatchesAnyVendorIdNegative) { |
| 245 std::vector<UsbDeviceFilter> filters(1); | 144 std::vector<UsbDeviceFilter> filters(1); |
| 246 filters.back().SetVendorId(0x1d6b); | 145 filters.back().SetVendorId(0x1d6b); |
| 247 ASSERT_FALSE(UsbDeviceFilter::MatchesAny(android_phone_, filters)); | 146 ASSERT_FALSE(UsbDeviceFilter::MatchesAny(android_phone_, filters)); |
| 248 } | 147 } |
| 249 | 148 |
| 250 } // namespace | 149 } // namespace |
| 251 | 150 |
| 252 } // namespace device | 151 } // namespace device |
| OLD | NEW |