xref: /aosp_15_r20/external/autotest/client/site_tests/hardware_Keyboard/hardware_Keyboard.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Copyright (c) 2010 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 glob, logging, os, subprocess
6
7from autotest_lib.client.bin import test, utils
8from autotest_lib.client.common_lib import error
9
10class hardware_Keyboard(test.test):
11    """
12    Test the keyboard through the user mode /dev/input/event interface.
13    """
14    version = 1
15    dev_input_event_path = '/dev/input/event*'
16    supported_keys = ['Esc', 'Grave', 'Minus', 'Equal', 'Backspace',
17                      '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
18                      'Tab', 'LeftBrace', 'RightBrace', 'BackSlash',
19                      'LeftMeta', 'Semicolon', 'Apostrophe', 'Enter',
20                      'LeftShift', 'Comma', 'Dot', 'Slash', 'RightShift',
21                      'LeftControl', 'LeftAlt', 'Space', 'RightAlt',
22                      'RightCtrl', 'Up', 'Down', 'Left', 'Right',
23                      'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
24                      'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
25                      'Z', 'X', 'C', 'V', 'B', 'N', 'M']
26    live_test_key = 'LeftMeta'
27    preserve_srcdir = True
28
29    def setup(self):
30        os.chdir(self.srcdir)
31        utils.make()
32
33    def _supported(self, event, key_name):
34        cmd = os.path.join(self.srcdir, 'evtest') + ' ' + event
35        cmd += ' -s ' + key_name
36        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
37        output, _ = proc.communicate()
38        status = proc.returncode
39        if status:
40            logging.error('Unsupported Key : %s', key_name)
41            return False
42        logging.info('%s : %s', key_name, output)
43        return True
44
45    def run_once(self):
46        high_key_count = 0
47        high_key_event = ''
48        for event in glob.glob(hardware_Keyboard.dev_input_event_path):
49            # Find the event file with the most keys
50            cmd = os.path.join(self.srcdir, 'evtest') + ' ' + event
51            cmd += ' -n'
52            proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
53            output, _ = proc.communicate()
54            status = proc.returncode
55            if status:  ## bad event, log the command's output as a warning
56                logging.warning("Bad event. cmd : %s", cmd)
57                logging.warning(output)
58                continue
59            num_keys = int(output)
60            if (num_keys > high_key_count):
61                high_key_count = num_keys
62                high_key_event = event
63        logging.info('Event with most is %s with %d keys', high_key_event,
64                     high_key_count)
65        if (high_key_count < len(hardware_Keyboard.supported_keys)):
66            raise error.TestError('No suitable keyboard found.')
67        # Check that all necessary keyboard keys exist.
68        if not all(self._supported(high_key_event, key_name)
69                   for key_name in hardware_Keyboard.supported_keys):
70            raise error.TestError('Required key unsupported in %s' %
71                                  high_key_event)
72        # Test one live keystroke. Test will wait on user input.
73        cmd = os.path.join(self.srcdir, 'evtest') + ' ' + high_key_event
74        cmd += ' -k'
75        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
76        output, _ = proc.communicate()
77        status = proc.returncode
78        if status:
79            raise error.TestError('Key Capture Test failed : %s' % output);
80        if (output.decode() != hardware_Keyboard.live_test_key):
81            raise error.TestError('Incorrect key pressed : %s' % output);
82