Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / tools / usb_gadget / keyboard_gadget_test.py
blobd3b085fda8c7af0a0c964a2e86f5518e5b76cc01
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 keyboard_gadget
12 import usb_constants
15 class KeyboardGadgetTest(unittest.TestCase):
17 def test_key_press(self):
18 g = keyboard_gadget.KeyboardGadget()
19 chip = mock.Mock()
20 g.Connected(chip, usb_constants.Speed.FULL)
21 g.KeyDown(0x04)
22 self.assertEqual(g.ControlRead(0xA1, 1, 0x0100, 0, 8),
23 '\x00\x00\x04\x00\x00\x00\x00\x00')
24 g.KeyUp(0x04)
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()
34 chip = mock.Mock()
35 g.Connected(chip, usb_constants.Speed.FULL)
36 g.ModifierDown(hid_constants.ModifierKey.L_SHIFT)
37 g.KeyDown(0x04)
38 g.KeyDown(0x05)
39 g.KeyUp(0x04)
40 g.KeyUp(0x05)
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()
53 chip = mock.Mock()
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__':
63 unittest.main()