1# Copyright (c) 2013 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 re
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server import test
9
10
11class power_ChargeStatus(test.test):
12    """
13    Test power_supply_info AC and BAT "state" on power OFF and ON.
14
15    If DUT is connected to RPM(default) - No need to pass any command line args.
16    If DUT is connected to USB powerstrip(via servo), Need to pass cmdlineargs
17    as --args=power_control="servoj10".
18    If DUT is not connected to servo and RPM. i.e to handle manually, Need to
19    pass cmdlineargs as --args=power_control="manual".
20    """
21    version = 1
22
23    def initialize(self, host, cmdline_args):
24        args = {}
25        for arg in cmdline_args:
26            match = re.search("^(\w+)=(.+)", arg)
27            if match:
28                args[match.group(1)] = match.group(2)
29        self.power_control = args.get('power_control', host.POWER_CONTROL_RPM)
30        if self.power_control not in host.POWER_CONTROL_VALID_ARGS:
31            raise error.TestError('Valid values for --args=power_control '
32                                  'are %s. But you entered wrong argument '
33                                  'as "%s".'
34                                   % (host.POWER_CONTROL_VALID_ARGS,
35                                   self.power_control))
36
37
38    def run_once(self, host):
39        ac_state = self.get_ac_status(host)
40        bat_state = self.get_bat_status(host)
41        self.test_charge_state(ac_state, bat_state)
42        host.power_off(self.power_control)
43        ac_state = self.get_ac_status(host)
44        bat_state = self.get_bat_status(host)
45        self.test_discharge_state(ac_state, bat_state)
46        host.power_on(self.power_control)
47
48
49    def test_charge_state(self, ac_state, bat_state):
50        """Tests when on AC- the Main line is "ON" and "Charging/Charged".
51
52        @param ac_state Specifies the power_supply_info "Line Power"
53                        online value.
54        @param bat_state Specifies the power_supply_info "Battery"
55                         charge state value.
56        """
57        if not (ac_state == "yes" and (bat_state == "Charging"
58            or bat_state == "Fully charged")):
59            raise error.TestFail("AC is not online and BAT state is %s."
60                                  % bat_state)
61
62
63    def test_discharge_state(self, ac_state, bat_state):
64        """Tests when on DC - the Main line is "No" and "Discharging".
65
66        @param ac_state Specifies the power_supply_info "Line Power"
67                        online value.
68        @param bat_state Specifies the power_supply_info "Battery"
69                         charge state value.
70        """
71        if not (ac_state == "no" and bat_state == "Discharging"):
72            raise error.TestFail("Not Discharging, on AC and BAT state is %s."
73                                  % bat_state)
74
75
76    def get_ac_status(self, host):
77        """Get the AC state info from "power_supply_info"."""
78        ac_state_info = host.run(
79            "power_supply_info | egrep 'online'").stdout.strip()
80        return self.split_info(ac_state_info)
81
82
83    def get_bat_status(self, host):
84        """ Get the DC state info from "power_supply_info"."""
85        bat_state_info = host.run(
86            "power_supply_info | egrep 'state'").stdout.strip()
87        return self.split_info(bat_state_info)
88
89
90    def split_info(self, info_list):
91        """Splits & trims stdout and returns the AC and BAT state Values.
92
93        @param info_list Specifies the stdout value.
94        """
95        split_list = info_list.split(":")
96        return split_list[-1].strip()
97