Add window.gc back to Smoothness.WillNavigateToPageHook
[chromium-blink-merge.git] / tools / usb_gadget / hid_gadget_test.py
blobf0a6a29185b7a5089c6155e55ebbbd696c32d580
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.
6 import unittest
8 import mock
10 import hid_constants
11 import hid_descriptors
12 import hid_gadget
13 import usb_constants
16 report_desc = hid_descriptors.ReportDescriptor(
17 hid_descriptors.UsagePage(0xFF00), # Vendor Defined
18 hid_descriptors.Usage(0x00),
19 hid_descriptors.Collection(
20 hid_constants.CollectionType.APPLICATION,
21 hid_descriptors.LogicalMinimum(0, force_length=1),
22 hid_descriptors.LogicalMaximum(255, force_length=2),
23 hid_descriptors.ReportSize(8),
24 hid_descriptors.ReportCount(8),
25 hid_descriptors.Input(hid_descriptors.Data,
26 hid_descriptors.Variable,
27 hid_descriptors.Absolute,
28 hid_descriptors.BufferedBytes),
29 hid_descriptors.Output(hid_descriptors.Data,
30 hid_descriptors.Variable,
31 hid_descriptors.Absolute,
32 hid_descriptors.BufferedBytes),
33 hid_descriptors.Feature(hid_descriptors.Data,
34 hid_descriptors.Variable,
35 hid_descriptors.Absolute,
36 hid_descriptors.BufferedBytes)
40 combo_report_desc = hid_descriptors.ReportDescriptor(
41 hid_descriptors.ReportID(1),
42 report_desc,
43 hid_descriptors.ReportID(2),
44 report_desc
48 class HidGadgetTest(unittest.TestCase):
50 def test_bad_intervals(self):
51 with self.assertRaisesRegexp(ValueError, 'Full speed'):
52 hid_gadget.HidGadget(report_desc, features={}, interval_ms=50000,
53 vendor_id=0, product_id=0)
54 with self.assertRaisesRegexp(ValueError, 'High speed'):
55 hid_gadget.HidGadget(report_desc, features={}, interval_ms=5000,
56 vendor_id=0, product_id=0)
58 def test_get_string_descriptor(self):
59 g = hid_gadget.HidGadget(report_desc=report_desc, features={},
60 vendor_id=0, product_id=0)
61 chip = mock.Mock()
62 g.Connected(chip, usb_constants.Speed.HIGH)
63 g.AddStringDescriptor(2, 'HID Gadget')
64 desc = g.ControlRead(0x80, 6, 0x0302, 0x0409, 255)
65 self.assertEquals(desc, '\x16\x03H\0I\0D\0 \0G\0a\0d\0g\0e\0t\0')
67 def test_get_report_descriptor(self):
68 g = hid_gadget.HidGadget(report_desc=report_desc, features={},
69 vendor_id=0, product_id=0)
70 chip = mock.Mock()
71 g.Connected(chip, usb_constants.Speed.HIGH)
72 desc = g.ControlRead(0x81, 6, 0x2200, 0, 63)
73 self.assertEquals(desc, report_desc)
75 def test_set_idle(self):
76 g = hid_gadget.HidGadget(report_desc=report_desc, features={},
77 vendor_id=0, product_id=0)
78 chip = mock.Mock()
79 g.Connected(chip, usb_constants.Speed.HIGH)
80 self.assertTrue(g.ControlWrite(0x21, 0x0A, 0, 0, ''))
82 def test_class_wrong_target(self):
83 g = hid_gadget.HidGadget(report_desc=report_desc, features={},
84 vendor_id=0, product_id=0)
85 chip = mock.Mock()
86 g.Connected(chip, usb_constants.Speed.HIGH)
87 self.assertIsNone(g.ControlRead(0xA0, 0, 0, 0, 0)) # Device
88 self.assertIsNone(g.ControlRead(0xA1, 0, 0, 1, 0)) # Interface 1
89 self.assertIsNone(g.ControlWrite(0x20, 0, 0, 0, '')) # Device
90 self.assertIsNone(g.ControlWrite(0x21, 0, 0, 1, '')) # Interface 1
92 def test_send_report_zero(self):
93 g = hid_gadget.HidGadget(report_desc=report_desc, features={},
94 vendor_id=0, product_id=0)
95 chip = mock.Mock()
96 g.Connected(chip, usb_constants.Speed.HIGH)
97 g.SendReport(0, 'Hello world!')
98 chip.SendPacket.assert_called_once_with(0x81, 'Hello world!')
100 def test_send_multiple_reports(self):
101 g = hid_gadget.HidGadget(report_desc=report_desc, features={},
102 vendor_id=0, product_id=0)
103 chip = mock.Mock()
104 g.Connected(chip, usb_constants.Speed.HIGH)
105 g.SendReport(1, 'Hello!')
106 g.SendReport(2, 'World!')
107 chip.SendPacket.assert_has_calls([
108 mock.call(0x81, '\x01Hello!'),
109 mock.call(0x81, '\x02World!'),
113 class TestFeature(hid_gadget.HidFeature):
115 def SetInputReport(self, data):
116 self.input_report = data
117 return True
119 def SetOutputReport(self, data):
120 self.output_report = data
121 return True
123 def SetFeatureReport(self, data):
124 self.feature_report = data
125 return True
127 def GetInputReport(self):
128 return 'Input report.'
130 def GetOutputReport(self):
131 return 'Output report.'
133 def GetFeatureReport(self):
134 return 'Feature report.'
137 class HidFeatureTest(unittest.TestCase):
139 def test_disconnected(self):
140 feature = TestFeature()
141 with self.assertRaisesRegexp(RuntimeError, 'not connected'):
142 feature.SendReport('Hello world!')
144 def test_send_report(self):
145 feature = TestFeature()
146 g = hid_gadget.HidGadget(report_desc, features={1: feature},
147 vendor_id=0, product_id=0)
148 chip = mock.Mock()
149 g.Connected(chip, usb_constants.Speed.HIGH)
150 feature.SendReport('Hello world!')
151 chip.SendPacket.assert_called_once_with(0x81, '\x01Hello world!')
152 g.Disconnected()
154 def test_get_bad_report(self):
155 feature = TestFeature()
156 g = hid_gadget.HidGadget(report_desc, features={1: feature},
157 vendor_id=0, product_id=0)
158 chip = mock.Mock()
159 g.Connected(chip, usb_constants.Speed.HIGH)
160 self.assertIsNone(g.ControlRead(0xA1, 1, 0x0102, 0, 8))
162 def test_set_bad_report(self):
163 feature = TestFeature()
164 g = hid_gadget.HidGadget(report_desc, features={1: feature},
165 vendor_id=0, product_id=0)
166 chip = mock.Mock()
167 g.Connected(chip, usb_constants.Speed.HIGH)
168 self.assertIsNone(g.ControlWrite(0x21, 0x09, 0x0102, 0, 'Hello!'))
170 def test_get_input_report(self):
171 feature = TestFeature()
172 g = hid_gadget.HidGadget(report_desc, features={1: feature},
173 vendor_id=0, product_id=0)
174 chip = mock.Mock()
175 g.Connected(chip, usb_constants.Speed.HIGH)
176 report = g.ControlRead(0xA1, 1, 0x0101, 0, 8)
177 self.assertEquals(report, 'Input re')
179 def test_set_input_report(self):
180 feature = TestFeature()
181 g = hid_gadget.HidGadget(report_desc, features={1: feature},
182 vendor_id=0, product_id=0)
183 chip = mock.Mock()
184 g.Connected(chip, usb_constants.Speed.HIGH)
185 self.assertTrue(g.ControlWrite(0x21, 0x09, 0x0101, 0, 'Hello!'))
186 self.assertEquals(feature.input_report, 'Hello!')
188 def test_get_output_report(self):
189 feature = TestFeature()
190 g = hid_gadget.HidGadget(report_desc, features={1: feature},
191 vendor_id=0, product_id=0)
192 chip = mock.Mock()
193 g.Connected(chip, usb_constants.Speed.HIGH)
194 report = g.ControlRead(0xA1, 1, 0x0201, 0, 8)
195 self.assertEquals(report, 'Output r')
197 def test_set_output_report(self):
198 feature = TestFeature()
199 g = hid_gadget.HidGadget(report_desc, features={1: feature},
200 vendor_id=0, product_id=0)
201 chip = mock.Mock()
202 g.Connected(chip, usb_constants.Speed.HIGH)
203 self.assertTrue(g.ControlWrite(0x21, 0x09, 0x0201, 0, 'Hello!'))
204 self.assertEquals(feature.output_report, 'Hello!')
206 def test_receive_interrupt(self):
207 feature = TestFeature()
208 g = hid_gadget.HidGadget(report_desc, features={1: feature},
209 vendor_id=0, product_id=0)
210 chip = mock.Mock()
211 g.Connected(chip, usb_constants.Speed.HIGH)
212 g.SetConfiguration(1)
213 g.ReceivePacket(0x01, '\x01Hello!')
214 self.assertFalse(chip.HaltEndpoint.called)
215 self.assertEquals(feature.output_report, 'Hello!')
217 def test_receive_interrupt_report_zero(self):
218 feature = TestFeature()
219 g = hid_gadget.HidGadget(report_desc, features={0: feature},
220 vendor_id=0, product_id=0)
221 chip = mock.Mock()
222 g.Connected(chip, usb_constants.Speed.HIGH)
223 g.SetConfiguration(1)
224 g.ReceivePacket(0x01, 'Hello!')
225 self.assertFalse(chip.HaltEndpoint.called)
226 self.assertEquals(feature.output_report, 'Hello!')
228 def test_receive_bad_interrupt(self):
229 feature = TestFeature()
230 g = hid_gadget.HidGadget(report_desc, features={1: feature},
231 vendor_id=0, product_id=0)
232 chip = mock.Mock()
233 g.Connected(chip, usb_constants.Speed.HIGH)
234 g.SetConfiguration(1)
235 g.ReceivePacket(0x01, '\x00Hello!')
236 chip.HaltEndpoint.assert_called_once_with(0x01)
238 def test_get_feature_report(self):
239 feature = TestFeature()
240 g = hid_gadget.HidGadget(report_desc, features={1: feature},
241 vendor_id=0, product_id=0)
242 chip = mock.Mock()
243 g.Connected(chip, usb_constants.Speed.HIGH)
244 report = g.ControlRead(0xA1, 1, 0x0301, 0, 8)
245 self.assertEquals(report, 'Feature ')
247 def test_set_feature_report(self):
248 feature = TestFeature()
249 g = hid_gadget.HidGadget(report_desc, features={1: feature},
250 vendor_id=0, product_id=0)
251 chip = mock.Mock()
252 g.Connected(chip, usb_constants.Speed.HIGH)
253 self.assertTrue(g.ControlWrite(0x21, 0x09, 0x0301, 0, 'Hello!'))
254 self.assertEquals(feature.feature_report, 'Hello!')
257 if __name__ == '__main__':
258 unittest.main()