xref: /aosp_15_r20/external/autotest/server/site_tests/firmware_ECKeyboard/firmware_ECKeyboard.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Copyright (c) 2012 The Chromium OS 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
5import logging
6from threading import Timer
7
8from autotest_lib.client.bin.input import linux_input
9from autotest_lib.client.common_lib import error
10from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
11
12
13class firmware_ECKeyboard(FirmwareTest):
14    """
15    Servo based EC keyboard test.
16    """
17    version = 1
18
19    # Delay to ensure client is ready to read the key press.
20    KEY_PRESS_DELAY = 2
21
22    # Map the to-be-tested keys to the expected linux keycodes.
23    TEST_KEY_MAP = {
24            '0': linux_input.KEY_0,
25            'b': linux_input.KEY_B,
26            'e': linux_input.KEY_E,
27            'o': linux_input.KEY_O,
28            'r': linux_input.KEY_R,
29            's': linux_input.KEY_S,
30            't': linux_input.KEY_T,
31            '<enter>': linux_input.KEY_ENTER,
32            '<ctrl_l>': linux_input.KEY_LEFTCTRL,
33            '<alt_l>': linux_input.KEY_LEFTALT
34    }
35
36    def initialize(self, host, cmdline_args):
37        super(firmware_ECKeyboard, self).initialize(host, cmdline_args)
38        # Only run in normal mode
39        self.switcher.setup_mode('normal')
40
41    def cleanup(self):
42        self.faft_client.system.run_shell_command('start ui')
43        super(firmware_ECKeyboard, self).cleanup()
44
45    def send_string(self, keys):
46        """Send a string over a servo"""
47        for key in keys:
48            self.servo.set_nocheck('arb_key_config', key)
49            self.servo.set_nocheck('arb_key', 'tab')
50
51    def run_once(self):
52        """Runs a single iteration of the test."""
53        if not self.check_ec_capability(['keyboard']):
54            raise error.TestNAError("Nothing needs to be tested on this device")
55
56        test_keys = []
57        expected_keycodes = []
58
59        for key in self.TEST_KEY_MAP:
60            test_keys.append(key)
61            expected_keycodes.append(self.TEST_KEY_MAP[key])
62
63        # Stop UI so that key presses don't go to Chrome.
64        self.faft_client.system.run_shell_command('stop ui')
65
66        if self.servo.has_control('init_usb_keyboard'):
67            logging.debug('Turning off HID keyboard emulator.')
68            self.servo.set_nocheck('init_usb_keyboard', 'off')
69
70        Timer(self.KEY_PRESS_DELAY, lambda: self.send_string(test_keys)).start(
71        )
72        keys_matched = self.faft_client.system.check_keys(expected_keycodes)
73        logging.debug("Matched %d keys", keys_matched)
74        if (keys_matched < 0):
75            raise error.TestFail("Some test keys are not captured.")
76