• 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.cros import tpm_utils
8from autotest_lib.server import autotest
9from autotest_lib.server import test
10
11
12class policy_WilcoServerUSBPowershare(test.test):
13    """Test that verifies DeviceUsbPowerShareEnabled policy.
14
15    If the policy is enabled or not set, USB provides power when device is off.
16    If the policy is disabled, USB does not provide power when device is off.
17
18    This test uses the servo board to interact with the device once it's
19    powered off.
20
21    Note: This test only checks the behavior of the device when it is off,
22    it doesn't do the part of the policy when the device is asleep.
23
24    This test has to run on a Wilco device.
25    """
26    version = 1
27
28
29    def initialize(self, host):
30        """Initialize DUT for testing."""
31        pass
32
33
34    def cleanup(self):
35        """Clean up DUT."""
36        tpm_utils.ClearTPMIfOwned(self.host)
37
38
39    def _get_average_power_output(self):
40        """Gets the average of the list.
41
42        When using the vbus_power command with the DeviceUsbPowerShareEnabled
43        policy enabled, it sometimes still returns 0.0. This is why it's safer
44        to run the command multiple times instead of once.
45
46        """
47        vbus_list = []
48        t_end = time.time() + 7
49        while time.time() < t_end:
50            vbus_list.append(self.host.servo.vbus_power_get())
51            time.sleep(1)
52        return sum(vbus_list)/len(vbus_list)
53
54
55    def run_once(self, client_test, host, case):
56        """Run the test.
57
58        @param client_test: the name of the Client test to run.
59        @param case: the case to run for the given Client test.
60        """
61        self.host = host
62        tpm_utils.ClearTPMIfOwned(self.host)
63
64        self.autotest_client = autotest.Autotest(self.host)
65        self.autotest_client.run_test(
66            client_test, case=case, check_client_result=False)
67
68        # Turns off the device.
69        self.host.servo.power_key('long_press')
70        # This makes sure the device is really off before checking power.
71        time.sleep(10)
72
73        vbus_average_power = self._get_average_power_output()
74
75        # Bring device back up. I tried using power_key method to power up the
76        # device but could not consisnently get it to work. pwr_button method
77        # works all the time.
78        self.host.servo.pwr_button()
79        time.sleep(1)
80        self.host.servo.pwr_button('release')
81
82        if case is False:
83            if vbus_average_power > 0.0:
84                raise error.TestFail('USB is providing power, it should not.')
85        else:
86            if vbus_average_power is 0.0:
87                raise error.TestFail('USB is not providing power, it should.')
88