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" | |
9 #include "device/usb/usb_device.h" | 8 #include "device/usb/usb_device.h" |
10 #include "device/usb/usb_device_filter.h" | 9 #include "device/usb/usb_device_filter.h" |
11 #include "device/usb/usb_device_handle.h" | 10 #include "device/usb/usb_device_handle.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "device/usb/usb_interface.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 | 13 |
15 namespace device { | 14 namespace device { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 using testing::NiceMock; | 18 class MockUsbInterfaceAltSettingDescriptor |
20 using testing::ReturnRef; | 19 : public UsbInterfaceAltSettingDescriptor { |
| 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 }; |
21 | 106 |
22 class MockUsbDevice : public UsbDevice { | 107 class MockUsbDevice : public UsbDevice { |
23 public: | 108 public: |
24 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) | 109 MockUsbDevice(uint16 vendor_id, |
25 : UsbDevice(vendor_id, product_id, unique_id) {} | 110 uint16 product_id, |
| 111 uint32 unique_id, |
| 112 scoped_refptr<UsbConfigDescriptor> config_desc) |
| 113 : UsbDevice(vendor_id, product_id, unique_id), |
| 114 config_desc_(config_desc) {} |
26 | 115 |
27 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>()); | 116 virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE { return NULL; } |
28 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); | 117 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE { |
| 118 NOTREACHED(); |
| 119 return true; |
| 120 } |
29 #if defined(OS_CHROMEOS) | 121 #if defined(OS_CHROMEOS) |
30 MOCK_METHOD2(RequestUsbAccess, void(int, const base::Callback<void(bool)>&)); | 122 virtual void RequestUsbAccess( |
31 #endif | 123 int interface_id, |
32 MOCK_METHOD0(GetConfiguration, const UsbConfigDescriptor&()); | 124 const base::Callback<void(bool success)>& callback) OVERRIDE { |
| 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() {} |
33 | 134 |
34 private: | 135 private: |
35 virtual ~MockUsbDevice() {} | 136 scoped_refptr<UsbConfigDescriptor> config_desc_; |
36 }; | 137 }; |
37 | 138 |
38 class UsbFilterTest : public testing::Test { | 139 class UsbFilterTest : public testing::Test { |
39 public: | 140 public: |
40 virtual void SetUp() OVERRIDE { | 141 virtual void SetUp() OVERRIDE { |
41 UsbInterfaceDescriptor interface; | 142 UsbInterfaceAltSettingDescriptorList alt_settings; |
42 interface.interface_number = 1; | 143 alt_settings.push_back(make_scoped_refptr( |
43 interface.alternate_setting = 0; | 144 new MockUsbInterfaceAltSettingDescriptor(1, 0, 0xFF, 0x42, 1))); |
44 interface.interface_class = 0xFF; | |
45 interface.interface_subclass = 0x42; | |
46 interface.interface_protocol = 0x01; | |
47 config_.interfaces.push_back(interface); | |
48 | 145 |
49 android_phone_ = new MockUsbDevice(0x18d1, 0x4ee2, 0); | 146 UsbInterfaceDescriptorList interfaces; |
50 ON_CALL(*android_phone_.get(), GetConfiguration()) | 147 interfaces.push_back( |
51 .WillByDefault(ReturnRef(config_)); | 148 make_scoped_refptr(new MockUsbInterfaceDescriptor(alt_settings))); |
| 149 |
| 150 scoped_refptr<UsbConfigDescriptor> config_desc( |
| 151 new MockUsbConfigDescriptor(interfaces)); |
| 152 |
| 153 android_phone_ = new MockUsbDevice(0x18d1, 0x4ee2, 0, config_desc); |
52 } | 154 } |
53 | 155 |
54 protected: | 156 protected: |
55 UsbConfigDescriptor config_; | 157 scoped_refptr<UsbDevice> android_phone_; |
56 scoped_refptr<MockUsbDevice> android_phone_; | |
57 }; | 158 }; |
58 | 159 |
59 TEST_F(UsbFilterTest, MatchAny) { | 160 TEST_F(UsbFilterTest, MatchAny) { |
60 UsbDeviceFilter filter; | 161 UsbDeviceFilter filter; |
61 ASSERT_TRUE(filter.Matches(android_phone_)); | 162 ASSERT_TRUE(filter.Matches(android_phone_)); |
62 } | 163 } |
63 | 164 |
64 TEST_F(UsbFilterTest, MatchVendorId) { | 165 TEST_F(UsbFilterTest, MatchVendorId) { |
65 UsbDeviceFilter filter; | 166 UsbDeviceFilter filter; |
66 filter.SetVendorId(0x18d1); | 167 filter.SetVendorId(0x18d1); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 243 |
143 TEST_F(UsbFilterTest, MatchesAnyVendorIdNegative) { | 244 TEST_F(UsbFilterTest, MatchesAnyVendorIdNegative) { |
144 std::vector<UsbDeviceFilter> filters(1); | 245 std::vector<UsbDeviceFilter> filters(1); |
145 filters.back().SetVendorId(0x1d6b); | 246 filters.back().SetVendorId(0x1d6b); |
146 ASSERT_FALSE(UsbDeviceFilter::MatchesAny(android_phone_, filters)); | 247 ASSERT_FALSE(UsbDeviceFilter::MatchesAny(android_phone_, filters)); |
147 } | 248 } |
148 | 249 |
149 } // namespace | 250 } // namespace |
150 | 251 |
151 } // namespace device | 252 } // namespace device |
OLD | NEW |