1# Copyright 2021 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
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
9
10
11class firmware_MenuDevBootUSB(FirmwareTest):
12    """
13    Servo based test for USB boot in developer mode through the UI menu.
14    """
15    version = 1
16
17    def initialize(self, host, cmdline_args):
18        super(firmware_MenuDevBootUSB, self).initialize(host, cmdline_args)
19        if not self.menu_switcher:
20            raise error.TestNAError('Test skipped for menuless UI')
21        if not self.faft_config.chrome_ec:
22            raise error.TestNAError('Cannot check power state without EC')
23        self.switcher.setup_mode('dev')
24        self.setup_usbkey(usbkey=True, host=True, used_for_recovery=False)
25
26    def cleanup(self):
27        """Clean up the test."""
28        try:
29            self.faft_client.system.set_dev_boot_usb(0)
30            self.servo.switch_usbkey('host')
31        except Exception as e:
32            logging.error("Caught exception: %s", str(e))
33        super(firmware_MenuDevBootUSB, self).cleanup()
34
35    def _dev_reboot_and_unplug_usb(self):
36        """Reboot from internal disk and unplug USB disk."""
37        # Device must be in dev mode
38        logging.info('Reboot to dev mode and unplug USB')
39        self.switcher.mode_aware_reboot()
40        self.servo.switch_usbkey('host')
41
42    def run_once(self):
43        """Method which actually runs the test."""
44        self.check_state((self.checkers.mode_checker, 'dev'))
45        self.servo.switch_usbkey('dut')
46        self.faft_client.system.set_dev_boot_usb(1)
47        self.faft_client.system.set_dev_default_boot('disk')
48
49        # Now the device should be in dev screen
50        logging.info('Boot from USB in developer screen')
51        self.switcher.simple_reboot()
52        self.menu_switcher.dev_boot_from_external()
53        self.switcher.wait_for_client()
54        self.check_state((self.checkers.dev_boot_usb_checker, (True, True),
55                          'Device not booted from USB'))
56
57        # Reboot from internal disk in order to unplug USB
58        self._dev_reboot_and_unplug_usb()
59
60        # For menu UI, boot from USB in external boot screen, a polling screen
61        # that repeatedly checks for USB disks
62        if self.faft_config.mode_switcher_type != 'tablet_detachable_switcher':
63            logging.info('Boot from USB in external boot screen')
64            self.switcher.simple_reboot()
65            self.menu_switcher.dev_boot_from_external()
66            self.switcher.wait_for_client_offline()
67
68            # Since there is no USB plugged-in, now the device should be in
69            # external boot screen
70            self.servo.switch_usbkey('dut')
71            self.switcher.wait_for_client()
72            self.check_state((self.checkers.dev_boot_usb_checker, (True, True),
73                              'Device not booted from USB properly'))
74            self._dev_reboot_and_unplug_usb()
75        else:
76            logging.info('Skipped polling screen test for switcher type %s',
77                         self.faft_config.mode_switcher_type)
78
79        # After selecting "Boot from external disk" while no USB is plugged-in,
80        # the UI should still work
81        logging.info('Try to boot from USB without USB plugged-in')
82        self.switcher.simple_reboot()
83        self.menu_switcher.dev_boot_from_external()
84        self.wait_for('keypress_delay')
85        if self.faft_config.mode_switcher_type == 'tablet_detachable_switcher':
86            # In legacy menu UI, the device should be still in developer boot
87            # options screen
88            self.menu_switcher.menu.down()  # Boot From Internal Disk
89        else:
90            # In menu UI, the device should have changed to external boot screen
91            self.menu_switcher.menu.select('Going back to dev screen...')
92            self.wait_for('keypress_delay')
93            self.menu_switcher.menu.up()  # Boot from internal disk
94        self.wait_for('keypress_delay')
95        self.menu_switcher.menu.select(
96                'Selecting "Boot from internal disk"...')
97        self.switcher.wait_for_client()
98        self.check_state((self.checkers.dev_boot_usb_checker, False,
99                          'Device not booted from internal disk properly'))
100