• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, sys, commands
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        (status, output) = commands.getstatusoutput(cmd)
37        if status:
38            logging.error('Unsupported Key : %s' % key_name)
39            return False
40        logging.info('%s : %s' % (key_name, output))
41        return True
42
43    def run_once(self):
44        high_key_count = 0
45        high_key_event = ''
46        for event in glob.glob(hardware_Keyboard.dev_input_event_path):
47            # Find the event file with the most keys
48            cmd = os.path.join(self.srcdir, 'evtest') + ' ' + event
49            cmd += ' -n'
50            (status, output) = commands.getstatusoutput(cmd)
51            if status:  ## bad event, log the command's output as a warning
52                logging.warning("Bad event. cmd : %s" % cmd)
53                logging.warning(output)
54                continue
55            num_keys = int(output)
56            if (num_keys > high_key_count):
57                high_key_count = num_keys
58                high_key_event = event
59        logging.info('Event with most is %s with %d keys' % (high_key_event,
60                                                             high_key_count))
61        if (high_key_count < len(hardware_Keyboard.supported_keys)):
62            raise error.TestError('No suitable keyboard found.')
63        # Check that all necessary keyboard keys exist.
64        if not all(self._supported(high_key_event, key_name)
65                   for key_name in hardware_Keyboard.supported_keys):
66            raise error.TestError('Required key unsupported in %s' %
67                                  high_key_event)
68        # Test one live keystroke. Test will wait on user input.
69        cmd = os.path.join(self.srcdir, 'evtest') + ' ' + high_key_event
70        cmd += ' -k'
71        (status, output) = commands.getstatusoutput(cmd)
72        if status:
73            raise error.TestError('Key Capture Test failed : %s' % output);
74        if (output != hardware_Keyboard.live_test_key):
75            raise error.TestError('Incorrect key pressed : %s' % output);
76