• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2011 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7
8import pyauto_functional  # Must be imported before pyauto
9import pyauto
10
11from chromeos.power_strip import PowerStrip
12
13class ChromeosBattery(pyauto.PyUITest):
14  """Tests ChromeOS Battery Status.
15
16  Preconditions:
17     1) Device under test (DUT) is connected to the LAN via an
18     Ethernet-to-USB adapter plugged into one of its USB ports.
19     2) AC power cable is connected to the DUT, and plugged into
20     the IP controlled Power Switch, outlet #4, located in the lab.
21     3) Battery is installed in the DUT, and battery is not fully
22     discharged.
23     4) Tester should have physical access to the power switch.
24
25  Note about time calculation:
26     When AC power is turned off or on, the battery will take from 2
27     to 60 seconds to calculate the time remaining to empty or full.
28     While calculating, the keys 'battery_time_to_full' and
29     'battery_time_to_empty' are absent.
30  """
31
32  _BATTERY_CONFIG_FILE = os.path.join(pyauto.PyUITest.DataDir(),
33                                      'pyauto_private', 'chromeos', 'power',
34                                      'battery_testbed_config')
35
36  def setUp(self):
37    pyauto.PyUITest.setUp(self)
38    self.InitPowerStrip()
39
40  def tearDown(self):
41    # Leave AC power ON so battery does not discharge between tests
42    self._power_strip.PowerOn(self._outlet_battery_full)
43    pyauto.PyUITest.tearDown(self)
44
45  def InitPowerStrip(self):
46    assert os.path.exists(ChromeosBattery._BATTERY_CONFIG_FILE), \
47        'Power Strip configuration file does not exist.'
48    power_config = pyauto.PyUITest.EvalDataFrom(
49        ChromeosBattery._BATTERY_CONFIG_FILE)
50    self._power_strip = PowerStrip(power_config['strip_ip'])
51    self._outlet_battery_full = (power_config['configs']
52                                             ['battery_full']
53                                             ['outlet_id'])
54
55  def WaitUntilBatteryState(self, ac_power_on, time_key):
56    assert self.WaitUntil(lambda: self.BatteryPowerAndChargeStateAgree(
57        ac_power_on, time_key), timeout=60, retry_sleep=1), \
58        'Battery charge/discharge time was not calculated.'
59    return
60
61  def BatteryPowerAndChargeStateAgree(self, ac_power_on, time_key):
62    battery_status = self.GetBatteryInfo()
63    return (battery_status.get('line_power_on') == ac_power_on and
64        time_key in battery_status)
65
66  def testBatteryChargesWhenACisOn(self):
67    """Calculate battery time to full when AC is ON"""
68    # Apply AC power to chromebook with battery.
69    self._power_strip.PowerOn(self._outlet_battery_full)
70
71    # Get info about charging battery
72    self.WaitUntilBatteryState(True,'battery_time_to_full')
73    battery_status = self.GetBatteryInfo()
74    self.assertTrue(battery_status.get('battery_is_present'),
75        msg='Battery is not present.')
76    self.assertTrue(battery_status.get('line_power_on'),
77        msg='Line power is off.')
78    self.assertTrue(battery_status.get('battery_time_to_full') >= 0,
79        msg='Battery charge time is negative.')
80
81  def testBatteryDischargesWhenACisOff(self):
82    """Calculate battery time to empty when AC is OFF"""
83    self._power_strip.PowerOff(self._outlet_battery_full)
84
85    # Get info about discharging battery
86    self.WaitUntilBatteryState(False,'battery_time_to_empty')
87    battery_status = self.GetBatteryInfo()
88    self.assertTrue(battery_status.get('battery_is_present'),
89        msg='Battery is not present.')
90    self.assertFalse(battery_status.get('line_power_on'),
91        msg='Line power is on.')
92    self.assertTrue(battery_status.get('battery_time_to_empty') >= 0,
93        msg='Battery discharge time is negative.')
94
95  def testBatteryTimesAreDifferent(self):
96    """Time to full and time to empty should be different"""
97    # Turn AC Power ON
98    self._power_strip.PowerOn(self._outlet_battery_full)
99
100    # Get charging battery time to full
101    self.WaitUntilBatteryState(True,'battery_time_to_full')
102    battery_status = self.GetBatteryInfo()
103    time_to_full = battery_status.get('battery_time_to_full')
104
105    # Turn AC Power OFF
106    self._power_strip.PowerOff(self._outlet_battery_full)
107
108    # Get discharging battery time to empty
109    self.WaitUntilBatteryState(False,'battery_time_to_empty')
110    battery_status = self.GetBatteryInfo()
111    time_to_empty = battery_status.get('battery_time_to_empty')
112
113    # Compare charge to discharge time
114    """Confirm that time to full and time to empty are not
115       returning the same value, but that the values are
116       different (e.g., both are not zero)."""
117    self.assertNotEqual(time_to_full, time_to_empty,
118        msg='Battery time to full equals time to empty. '
119            'Though very unlikely, this is not impossible. '
120            'If test failed falsely, Kris owes Scott a beer.')
121
122
123if __name__ == '__main__':
124  pyauto_functional.Main()
125