• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2019 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.
4import time
5
6from autotest_lib.client.common_lib import error
7from autotest_lib.client.common_lib import utils
8from autotest_lib.client.common_lib.cros import tpm_utils
9from autotest_lib.server import autotest
10from autotest_lib.server import test
11
12
13class policy_DeviceBootOnAcEnabled(test.test):
14    """Test that verifies DeviceBootOnAcEnabled policy.
15
16    If this policy is set to true then boot on AC will always be enabled.
17
18    If this policy is set to false, boot on AC will always be disabled.
19
20    If this policy is left unset, boot on AC is disabled.
21
22    This test has to run on a Wilco device with a servo board.
23    """
24    version = 1
25
26
27    def cleanup(self):
28        """Clean up DUT.
29
30        Make sure device is on.
31        Make sure servo board is supplying device with power.
32        Clear TPM.
33        """
34        self._if_device_off_turn_back_on()
35        self.host.servo.set_servo_v4_role('src')
36        tpm_utils.ClearTPMIfOwned(self.host)
37
38
39    def _check_power_discharging(self):
40        power_status = self.host.run('cat /sys/class/power_supply/BAT0/status')
41        power_status = power_status.stdout.lower().rstrip()
42        return power_status == 'discharging'
43
44
45    def _turn_device_on(self):
46        """Turns device back on."""
47        self.host.servo.pwr_button()
48        time.sleep(1)
49        self.host.servo.pwr_button('release')
50
51
52    def _confirm_dut_off(self):
53        """Confirms the DUT is off.
54
55        Note: tried using wait_down instead but the test would just hang.
56        """
57        if self.host.wait_up(timeout=10):
58            raise error.TestError('DUT is on, expected off.')
59
60
61    def _confirm_dut_on(self):
62        """Confirms the DUT is on."""
63        if not self.host.wait_up(timeout=20):
64            raise error.TestError('DUT is off, expected on.')
65
66
67    def _if_device_off_turn_back_on(self):
68        """Verify device is on, if not turn it on."""
69        if not self.host.wait_up(timeout=20):
70            self._turn_device_on()
71
72
73    def run_once(self, host, case):
74        """Run the test.
75
76        @param case: the case to run for the given Client test.
77        """
78        self.host = host
79        tpm_utils.ClearTPMIfOwned(self.host)
80
81        self.autotest_client = autotest.Autotest(self.host)
82        self.autotest_client.run_test(
83            'policy_DeviceBootOnAcEnabled',
84            case=case)
85
86        # Stop supplying power from servo, emulating unplugging power.
87        self.host.servo.set_servo_v4_role('snk')
88        # Verify the dut is running on battery.
89        utils.poll_for_condition(
90            lambda: self._check_power_discharging(),
91            exception=error.TestFail(
92                'Device should be running on battery but it is not.'),
93            timeout=5,
94            sleep_interval=1,
95            desc='Polling for power status change.')
96
97        # Turns off the device.
98        self.host.servo.power_key('long_press')
99
100        self._confirm_dut_off()
101
102        # Begin supplying power from servo, emulating plugging in power.
103        self.host.servo.set_servo_v4_role('src')
104
105        if case is True:
106            self._confirm_dut_on()
107        else:
108            self._confirm_dut_off()
109            # Bring device back up.
110            self._turn_device_on()
111            self._confirm_dut_on()
112