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.
11 import keyboard_gadget
15 class KeyboardGadgetTest(unittest
.TestCase
):
17 def test_key_press(self
):
18 g
= keyboard_gadget
.KeyboardGadget()
20 g
.Connected(chip
, usb_constants
.Speed
.FULL
)
22 self
.assertEqual(g
.ControlRead(0xA1, 1, 0x0100, 0, 8),
23 '\x00\x00\x04\x00\x00\x00\x00\x00')
25 self
.assertEqual(g
.ControlRead(0xA1, 1, 0x0100, 0, 8),
26 '\x00\x00\x00\x00\x00\x00\x00\x00')
27 chip
.SendPacket
.assert_has_calls([
28 mock
.call(0x81, '\x00\x00\x04\x00\x00\x00\x00\x00'),
29 mock
.call(0x81, '\x00\x00\x00\x00\x00\x00\x00\x00'),
32 def test_key_press_with_modifier(self
):
33 g
= keyboard_gadget
.KeyboardGadget()
35 g
.Connected(chip
, usb_constants
.Speed
.FULL
)
36 g
.ModifierDown(hid_constants
.ModifierKey
.L_SHIFT
)
41 g
.ModifierUp(hid_constants
.ModifierKey
.L_SHIFT
)
42 chip
.SendPacket
.assert_has_calls([
43 mock
.call(0x81, '\x02\x00\x00\x00\x00\x00\x00\x00'),
44 mock
.call(0x81, '\x02\x00\x04\x00\x00\x00\x00\x00'),
45 mock
.call(0x81, '\x02\x00\x04\x05\x00\x00\x00\x00'),
46 mock
.call(0x81, '\x02\x00\x00\x05\x00\x00\x00\x00'),
47 mock
.call(0x81, '\x02\x00\x00\x00\x00\x00\x00\x00'),
48 mock
.call(0x81, '\x00\x00\x00\x00\x00\x00\x00\x00'),
51 def test_set_leds(self
):
52 g
= keyboard_gadget
.KeyboardGadget()
54 g
.Connected(chip
, usb_constants
.Speed
.FULL
)
55 self
.assertEqual(g
.ControlRead(0xA1, 1, 0x0200, 0, 8), '\x00')
56 self
.assertTrue(g
.ControlWrite(0x21, 9, 0x0200, 0, '\x01'))
57 self
.assertEqual(g
.ControlRead(0xA1, 1, 0x0200, 0, 8), '\x01')
58 g
.ReceivePacket(0x01, '\x03')
59 self
.assertFalse(chip
.HaltEndpoint
.called
)
60 self
.assertEqual(g
.ControlRead(0xA1, 1, 0x0200, 0, 8), '\x03')
62 if __name__
== '__main__':