OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import unittest |
| 7 |
| 8 import hid_constants |
| 9 import hid_descriptors |
| 10 |
| 11 |
| 12 class HidTest(unittest.TestCase): |
| 13 |
| 14 def test_keyboard_example(self): |
| 15 report_desc = hid_descriptors.ReportDescriptor( |
| 16 hid_descriptors.UsagePage(0x01), # Generic Desktop |
| 17 hid_descriptors.Usage(0x06), # Keyboard |
| 18 hid_descriptors.Collection( |
| 19 hid_constants.CollectionType.APPLICATION, |
| 20 hid_descriptors.UsagePage(0x07), # Key Codes |
| 21 hid_descriptors.UsageMinimum(224), |
| 22 hid_descriptors.UsageMaximum(231), |
| 23 hid_descriptors.LogicalMinimum(0, force_length=1), |
| 24 hid_descriptors.LogicalMaximum(1), |
| 25 hid_descriptors.ReportSize(1), |
| 26 hid_descriptors.ReportCount(8), |
| 27 hid_descriptors.Input(hid_descriptors.Data, |
| 28 hid_descriptors.Variable, |
| 29 hid_descriptors.Absolute), |
| 30 hid_descriptors.ReportCount(1), |
| 31 hid_descriptors.ReportSize(8), |
| 32 hid_descriptors.Input(hid_descriptors.Constant), |
| 33 hid_descriptors.ReportCount(5), |
| 34 hid_descriptors.ReportSize(1), |
| 35 hid_descriptors.UsagePage(0x08), # LEDs |
| 36 hid_descriptors.UsageMinimum(1), |
| 37 hid_descriptors.UsageMaximum(5), |
| 38 hid_descriptors.Output(hid_descriptors.Data, |
| 39 hid_descriptors.Variable, |
| 40 hid_descriptors.Absolute), |
| 41 hid_descriptors.ReportCount(1), |
| 42 hid_descriptors.ReportSize(3), |
| 43 hid_descriptors.Output(hid_descriptors.Constant), |
| 44 hid_descriptors.ReportCount(6), |
| 45 hid_descriptors.ReportSize(8), |
| 46 hid_descriptors.LogicalMinimum(0, force_length=1), |
| 47 hid_descriptors.LogicalMaximum(101), |
| 48 hid_descriptors.UsagePage(0x07), # Key Codes |
| 49 hid_descriptors.UsageMinimum(0, force_length=1), |
| 50 hid_descriptors.UsageMaximum(101), |
| 51 hid_descriptors.Input(hid_descriptors.Data, hid_descriptors.Array) |
| 52 ) |
| 53 ) |
| 54 |
| 55 expected = ''.join(chr(x) for x in [ |
| 56 0x05, 0x01, 0x09, 0x06, 0xA1, 0x01, 0x05, 0x07, 0x19, 0xE0, 0x29, |
| 57 0xE7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, |
| 58 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x05, 0x75, 0x01, 0x05, |
| 59 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, |
| 60 0x91, 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, |
| 61 0x07, 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xC0 |
| 62 ]) |
| 63 self.assertEquals(report_desc, expected) |
| 64 |
| 65 def test_mouse_example(self): |
| 66 report_desc = hid_descriptors.ReportDescriptor( |
| 67 hid_descriptors.UsagePage(0x01), # Generic Desktop |
| 68 hid_descriptors.Usage(0x02), # Mouse |
| 69 hid_descriptors.Collection( |
| 70 hid_constants.CollectionType.APPLICATION, |
| 71 hid_descriptors.Usage(0x01), # Pointer |
| 72 hid_descriptors.Collection( |
| 73 hid_constants.CollectionType.PHYSICAL, |
| 74 hid_descriptors.UsagePage(0x09), # Buttons |
| 75 hid_descriptors.UsageMinimum(1), |
| 76 hid_descriptors.UsageMaximum(3), |
| 77 hid_descriptors.LogicalMinimum(0, force_length=1), |
| 78 hid_descriptors.LogicalMaximum(1), |
| 79 hid_descriptors.ReportCount(3), |
| 80 hid_descriptors.ReportSize(1), |
| 81 hid_descriptors.Input(hid_descriptors.Data, |
| 82 hid_descriptors.Variable, |
| 83 hid_descriptors.Absolute), |
| 84 hid_descriptors.ReportCount(1), |
| 85 hid_descriptors.ReportSize(5), |
| 86 hid_descriptors.Input(hid_descriptors.Constant), |
| 87 hid_descriptors.UsagePage(0x01), # Generic Desktop |
| 88 hid_descriptors.Usage(0x30), # X |
| 89 hid_descriptors.Usage(0x31), # Y |
| 90 hid_descriptors.LogicalMinimum(0x81), # -127 |
| 91 hid_descriptors.LogicalMaximum(127), |
| 92 hid_descriptors.ReportSize(8), |
| 93 hid_descriptors.ReportCount(2), |
| 94 hid_descriptors.Input(hid_descriptors.Data, |
| 95 hid_descriptors.Variable, |
| 96 hid_descriptors.Relative) |
| 97 ) |
| 98 ) |
| 99 ) |
| 100 |
| 101 expected = ''.join(chr(x) for x in [ |
| 102 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, 0xA1, 0x00, 0x05, 0x09, |
| 103 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, |
| 104 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, |
| 105 0x09, 0x31, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x02, 0x81, 0x06, |
| 106 0xC0, 0xC0 |
| 107 ]) |
| 108 self.assertEquals(report_desc, expected) |
| 109 |
| 110 def test_tag(self): |
| 111 self.assertEquals(hid_descriptors.Push(), '\xa4') |
| 112 |
| 113 def test_2byte_tag(self): |
| 114 self.assertEquals(hid_descriptors.LogicalMaximum(0xFF00), '\x26\x00\xFF') |
| 115 |
| 116 def test_4byte_tag(self): |
| 117 self.assertEquals(hid_descriptors.LogicalMaximum(0xFF884400), |
| 118 '\x27\x00\x44\x88\xFF') |
| 119 |
| 120 def test_long_tag(self): |
| 121 with self.assertRaises(NotImplementedError): |
| 122 hid_descriptors.LogicalMaximum(0xFFFFFFFFFFFFFFFF) |
| 123 |
| 124 if __name__ == '__main__': |
| 125 unittest.main() |
OLD | NEW |