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', 'F1', 'F2', 'F3', 'F4', 'F5','F6', 'F7', 'F8', 17 'F9', 'F10', 'Grave', 'Minus', 'Equal', 'Backspace', 18 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 19 'Tab', 'LeftBrace', 'RightBrace', 'BackSlash', 20 'LeftMeta', 'Semicolon', 'Apostrophe', 'Enter', 21 'LeftShift', 'Comma', 'Dot', 'Slash', 'RightShift', 22 'LeftControl', 'LeftAlt', 'Space', 'RightAlt', 23 'RightCtrl', 'Up', 'Down', 'Left', 'Right', 24 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 25 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 26 'Z', 'X', 'C', 'V', 'B', 'N', 'M'] 27 live_test_key = 'LeftMeta' 28 preserve_srcdir = True 29 30 def setup(self): 31 os.chdir(self.srcdir) 32 utils.make() 33 34 def _supported(self, event, key_name): 35 cmd = os.path.join(self.srcdir, 'evtest') + ' ' + event 36 cmd += ' -s ' + key_name 37 (status, output) = commands.getstatusoutput(cmd) 38 if status: 39 logging.error('Unsupported Key : %s' % key_name) 40 return False 41 logging.info('%s : %s' % (key_name, output)) 42 return True 43 44 def run_once(self): 45 high_key_count = 0 46 high_key_event = '' 47 for event in glob.glob(hardware_Keyboard.dev_input_event_path): 48 # Find the event file with the most keys 49 cmd = os.path.join(self.srcdir, 'evtest') + ' ' + event 50 cmd += ' -n' 51 (status, output) = commands.getstatusoutput(cmd) 52 if status: ## bad event, log the command's output as a warning 53 logging.warning("Bad event. cmd : %s" % cmd) 54 logging.warning(output) 55 continue 56 num_keys = int(output) 57 if (num_keys > high_key_count): 58 high_key_count = num_keys 59 high_key_event = event 60 logging.info('Event with most is %s with %d keys' % (high_key_event, 61 high_key_count)) 62 if (high_key_count < len(hardware_Keyboard.supported_keys)): 63 raise error.TestError('No suitable keyboard found.') 64 # Check that all necessary keyboard keys exist. 65 if not all(self._supported(high_key_event, key_name) 66 for key_name in hardware_Keyboard.supported_keys): 67 raise error.TestError('Required key unsupported in %s' % 68 high_key_event) 69 # Test one live keystroke. Test will wait on user input. 70 cmd = os.path.join(self.srcdir, 'evtest') + ' ' + high_key_event 71 cmd += ' -k' 72 (status, output) = commands.getstatusoutput(cmd) 73 if status: 74 raise error.TestError('Key Capture Test failed : %s' % output); 75 if (output != hardware_Keyboard.live_test_key): 76 raise error.TestError('Incorrect key pressed : %s' % output); 77