OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <sstream> |
| 6 |
| 7 #include "device/hid/hid_report_descriptor.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 using namespace testing; |
| 12 |
| 13 namespace device { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // See 'E.6 Report Descriptor (Keyboard)' |
| 18 // in HID specifications (v1.11) |
| 19 const uint8_t kKeyboard[] = { |
| 20 0x05, 0x01, 0x09, 0x06, 0xA1, 0x01, 0x05, 0x07, 0x19, 0xE0, 0x29, |
| 21 0xE7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, |
| 22 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x05, 0x75, 0x01, 0x05, |
| 23 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, |
| 24 0x91, 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, |
| 25 0x07, 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xC0}; |
| 26 |
| 27 // See 'E.10 Report Descriptor (Mouse)' |
| 28 // in HID specifications (v1.11) |
| 29 const uint8_t kMouse[] = {0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, 0xA1, |
| 30 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, |
| 31 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, |
| 32 0x01, 0x75, 0x05, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, |
| 33 0x09, 0x31, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, |
| 34 0x02, 0x81, 0x06, 0xC0, 0xC0}; |
| 35 |
| 36 const uint8_t kLogitechUnifyingReceiver[] = { |
| 37 0x06, 0x00, 0xFF, 0x09, 0x01, 0xA1, 0x01, 0x85, 0x10, 0x75, 0x08, |
| 38 0x95, 0x06, 0x15, 0x00, 0x26, 0xFF, 0x00, 0x09, 0x01, 0x81, 0x00, |
| 39 0x09, 0x01, 0x91, 0x00, 0xC0, 0x06, 0x00, 0xFF, 0x09, 0x02, 0xA1, |
| 40 0x01, 0x85, 0x11, 0x75, 0x08, 0x95, 0x13, 0x15, 0x00, 0x26, 0xFF, |
| 41 0x00, 0x09, 0x02, 0x81, 0x00, 0x09, 0x02, 0x91, 0x00, 0xC0, 0x06, |
| 42 0x00, 0xFF, 0x09, 0x04, 0xA1, 0x01, 0x85, 0x20, 0x75, 0x08, 0x95, |
| 43 0x0E, 0x15, 0x00, 0x26, 0xFF, 0x00, 0x09, 0x41, 0x81, 0x00, 0x09, |
| 44 0x41, 0x91, 0x00, 0x85, 0x21, 0x95, 0x1F, 0x15, 0x00, 0x26, 0xFF, |
| 45 0x00, 0x09, 0x42, 0x81, 0x00, 0x09, 0x42, 0x91, 0x00, 0xC0}; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 class HidReportDescriptorTest : public testing::Test { |
| 50 |
| 51 protected: |
| 52 virtual void SetUp() OVERRIDE { descriptor_ = NULL; } |
| 53 |
| 54 virtual void TearDown() OVERRIDE { |
| 55 if (descriptor_) { |
| 56 delete descriptor_; |
| 57 } |
| 58 } |
| 59 |
| 60 public: |
| 61 void ParseDescriptor(const std::string& expected, |
| 62 const uint8_t* bytes, |
| 63 size_t size) { |
| 64 descriptor_ = new HidReportDescriptor(bytes, size); |
| 65 |
| 66 std::stringstream actual; |
| 67 actual << *descriptor_; |
| 68 |
| 69 std::cout << "HID report descriptor:" << std::endl; |
| 70 std::cout << actual.str(); |
| 71 |
| 72 ASSERT_EQ(expected, actual.str()); |
| 73 } |
| 74 |
| 75 void GetTopLevelCollections(const std::vector<HidUsageAndPage>& expected, |
| 76 const uint8_t* bytes, |
| 77 size_t size) { |
| 78 descriptor_ = new HidReportDescriptor(bytes, size); |
| 79 |
| 80 std::vector<HidUsageAndPage> actual; |
| 81 descriptor_->GetTopLevelCollections(&actual); |
| 82 |
| 83 std::cout << "HID top-level collections:" << std::endl; |
| 84 for (std::vector<HidUsageAndPage>::const_iterator iter = actual.begin(); |
| 85 iter != actual.end(); |
| 86 ++iter) { |
| 87 std::cout << *iter << std::endl; |
| 88 } |
| 89 |
| 90 ASSERT_THAT(actual, ContainerEq(expected)); |
| 91 } |
| 92 |
| 93 private: |
| 94 HidReportDescriptor* descriptor_; |
| 95 }; |
| 96 |
| 97 TEST_F(HidReportDescriptorTest, ParseDescriptor_Keyboard) { |
| 98 const char expected[] = { |
| 99 "Usage Page (Generic Desktop)\n" |
| 100 "Usage (0x6)\n" |
| 101 "Collection (Physical)\n" |
| 102 " Usage Page (Keyboard)\n" |
| 103 " Usage Minimum (224)\n" |
| 104 " Usage Maximum (231)\n" |
| 105 " Logical Minimum (0)\n" |
| 106 " Logical Maximum (1)\n" |
| 107 " Report Size (1)\n" |
| 108 " Report Count (8)\n" |
| 109 " Input (Dat|Arr|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 110 " Report Count (1)\n" |
| 111 " Report Size (8)\n" |
| 112 " Input (Con|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 113 " Report Count (5)\n" |
| 114 " Report Size (1)\n" |
| 115 " Usage Page (Led)\n" |
| 116 " Usage Minimum (1)\n" |
| 117 " Usage Maximum (5)\n" |
| 118 " Output (Dat|Arr|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 119 " Report Count (1)\n" |
| 120 " Report Size (3)\n" |
| 121 " Output (Con|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 122 " Report Count (6)\n" |
| 123 " Report Size (8)\n" |
| 124 " Logical Minimum (0)\n" |
| 125 " Logical Maximum (101)\n" |
| 126 " Usage Page (Keyboard)\n" |
| 127 " Usage Minimum (0)\n" |
| 128 " Usage Maximum (101)\n" |
| 129 " Input (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 130 "End Collection\n"}; |
| 131 |
| 132 ParseDescriptor(std::string(expected), kKeyboard, sizeof(kKeyboard)); |
| 133 } |
| 134 |
| 135 TEST_F(HidReportDescriptorTest, TopLevelCollections_Keyboard) { |
| 136 HidUsageAndPage expected[] = { |
| 137 HidUsageAndPage(0x06, HidUsageAndPage::kPageGenericDesktop)}; |
| 138 |
| 139 GetTopLevelCollections(std::vector<HidUsageAndPage>( |
| 140 expected, expected + ARRAYSIZE_UNSAFE(expected)), |
| 141 kKeyboard, |
| 142 sizeof(kKeyboard)); |
| 143 } |
| 144 |
| 145 TEST_F(HidReportDescriptorTest, ParseDescriptor_Mouse) { |
| 146 const char expected[] = { |
| 147 "Usage Page (Generic Desktop)\n" |
| 148 "Usage (0x2)\n" |
| 149 "Collection (Physical)\n" |
| 150 " Usage (0x1)\n" |
| 151 " Collection (Physical)\n" |
| 152 " Usage Page (Button)\n" |
| 153 " Usage Minimum (1)\n" |
| 154 " Usage Maximum (3)\n" |
| 155 " Logical Minimum (0)\n" |
| 156 " Logical Maximum (1)\n" |
| 157 " Report Count (3)\n" |
| 158 " Report Size (1)\n" |
| 159 " Input (Dat|Arr|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 160 " Report Count (1)\n" |
| 161 " Report Size (5)\n" |
| 162 " Input (Con|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 163 " Usage Page (Generic Desktop)\n" |
| 164 " Usage (0x30)\n" |
| 165 " Usage (0x31)\n" |
| 166 " Logical Minimum (129)\n" |
| 167 " Logical Maximum (127)\n" |
| 168 " Report Size (8)\n" |
| 169 " Report Count (2)\n" |
| 170 " Input (Dat|Arr|Abs|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 171 " End Collection\n" |
| 172 "End Collection\n"}; |
| 173 |
| 174 ParseDescriptor(std::string(expected), kMouse, sizeof(kMouse)); |
| 175 } |
| 176 |
| 177 TEST_F(HidReportDescriptorTest, TopLevelCollections_Mouse) { |
| 178 HidUsageAndPage expected[] = { |
| 179 HidUsageAndPage(0x02, HidUsageAndPage::kPageGenericDesktop)}; |
| 180 |
| 181 GetTopLevelCollections(std::vector<HidUsageAndPage>( |
| 182 expected, expected + ARRAYSIZE_UNSAFE(expected)), |
| 183 kMouse, |
| 184 sizeof(kMouse)); |
| 185 } |
| 186 |
| 187 TEST_F(HidReportDescriptorTest, ParseDescriptor_LogitechUnifyingReceiver) { |
| 188 const char expected[] = { |
| 189 "Usage Page (Vendor)\n" |
| 190 "Usage (0x1)\n" |
| 191 "Collection (Physical)\n" |
| 192 " Report ID (0x10)\n" |
| 193 " Report Size (8)\n" |
| 194 " Report Count (6)\n" |
| 195 " Logical Minimum (0)\n" |
| 196 " Logical Maximum (255)\n" |
| 197 " Usage (0x1)\n" |
| 198 " Input (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 199 " Usage (0x1)\n" |
| 200 " Output (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 201 "End Collection\n" |
| 202 "Usage Page (Vendor)\n" |
| 203 "Usage (0x2)\n" |
| 204 "Collection (Physical)\n" |
| 205 " Report ID (0x11)\n" |
| 206 " Report Size (8)\n" |
| 207 " Report Count (19)\n" |
| 208 " Logical Minimum (0)\n" |
| 209 " Logical Maximum (255)\n" |
| 210 " Usage (0x2)\n" |
| 211 " Input (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 212 " Usage (0x2)\n" |
| 213 " Output (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 214 "End Collection\n" |
| 215 "Usage Page (Vendor)\n" |
| 216 "Usage (0x4)\n" |
| 217 "Collection (Physical)\n" |
| 218 " Report ID (0x20)\n" |
| 219 " Report Size (8)\n" |
| 220 " Report Count (14)\n" |
| 221 " Logical Minimum (0)\n" |
| 222 " Logical Maximum (255)\n" |
| 223 " Usage (0x41)\n" |
| 224 " Input (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 225 " Usage (0x41)\n" |
| 226 " Output (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 227 " Report ID (0x21)\n" |
| 228 " Report Count (31)\n" |
| 229 " Logical Minimum (0)\n" |
| 230 " Logical Maximum (255)\n" |
| 231 " Usage (0x42)\n" |
| 232 " Input (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 233 " Usage (0x42)\n" |
| 234 " Output (Dat|Var|Rel|NoWrp|Lin|Prf|NoNull|BitF)\n" |
| 235 "End Collection\n"}; |
| 236 |
| 237 ParseDescriptor(std::string(expected), |
| 238 kLogitechUnifyingReceiver, |
| 239 sizeof(kLogitechUnifyingReceiver)); |
| 240 } |
| 241 |
| 242 TEST_F(HidReportDescriptorTest, TopLevelCollections_LogitechUnifyingReceiver) { |
| 243 HidUsageAndPage expected[] = { |
| 244 HidUsageAndPage(0x01, HidUsageAndPage::kPageVendor), |
| 245 HidUsageAndPage(0x02, HidUsageAndPage::kPageVendor), |
| 246 HidUsageAndPage(0x04, HidUsageAndPage::kPageVendor), }; |
| 247 |
| 248 GetTopLevelCollections(std::vector<HidUsageAndPage>( |
| 249 expected, expected + ARRAYSIZE_UNSAFE(expected)), |
| 250 kLogitechUnifyingReceiver, |
| 251 sizeof(kLogitechUnifyingReceiver)); |
| 252 } |
| 253 |
| 254 } // namespace device |
OLD | NEW |