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
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
10
11
12class firmware_ECKeyboard(FirmwareTest):
13    """
14    Servo based EC keyboard test.
15    """
16    version = 1
17
18    # Delay between commands
19    CMD_DELAY = 1
20
21    def initialize(self, host, cmdline_args):
22        super(firmware_ECKeyboard, self).initialize(host, cmdline_args)
23        # Only run in normal mode
24        self.switcher.setup_mode('normal')
25
26    def switch_tty2(self):
27        """Switch to tty2 console."""
28        self.ec.key_down('<ctrl_l>')
29        self.ec.key_down('<alt_l>')
30        self.ec.key_down('<f2>')
31        self.ec.key_up('<f2>')
32        self.ec.key_up('<alt_l>')
33        self.ec.key_up('<ctrl_l>')
34        time.sleep(self.CMD_DELAY)
35
36    def reboot_by_keyboard(self):
37        """
38        Simulate key press sequence to log into console and then issue reboot
39        command.
40        """
41        self.switch_tty2()
42        self.ec.send_key_string('root<enter>')
43        time.sleep(self.CMD_DELAY)
44        self.ec.send_key_string('test0000<enter>')
45        time.sleep(self.CMD_DELAY)
46        self.ec.send_key_string('reboot<enter>')
47
48    def run_once(self):
49        if not self.check_ec_capability(['keyboard']):
50            raise error.TestNAError("Nothing needs to be tested on this device")
51
52        logging.info("Use key press simulation to issue reboot command.")
53        self.switcher.mode_aware_reboot('custom', self.reboot_by_keyboard)
54