OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 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 |
| 6 """ |
| 7 Unit tests for the contents of battery_utils.py |
| 8 """ |
| 9 |
| 10 import logging |
| 11 import os |
| 12 import sys |
| 13 import unittest |
| 14 |
| 15 from pylib import android_commands |
| 16 from pylib import constants |
| 17 from pylib.device import battery_utils |
| 18 from pylib.device import device_errors |
| 19 from pylib.device import device_utils |
| 20 from pylib.device import device_utils_test |
| 21 from pylib.utils import mock_calls |
| 22 |
| 23 # RunCommand from third_party/android_testrunner/run_command.py is mocked |
| 24 # below, so its path needs to be in sys.path. |
| 25 sys.path.append(os.path.join( |
| 26 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) |
| 27 |
| 28 sys.path.append(os.path.join( |
| 29 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
| 30 import mock # pylint: disable=F0401 |
| 31 |
| 32 |
| 33 class BatteryUtilsTest(mock_calls.TestCase): |
| 34 |
| 35 def setUp(self): |
| 36 self.adb = device_utils_test._AdbWrapperMock('0123456789abcdef') |
| 37 self.device = device_utils.DeviceUtils( |
| 38 self.adb, default_timeout=10, default_retries=0) |
| 39 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) |
| 40 self.battery = battery_utils.BatteryUtils( |
| 41 self.device, default_timeout=10, default_retries=0) |
| 42 |
| 43 |
| 44 class BatteryUtilsInitTest(unittest.TestCase): |
| 45 |
| 46 def testInitWithDeviceUtil(self): |
| 47 serial = '0fedcba987654321' |
| 48 a = android_commands.AndroidCommands(device=serial) |
| 49 d = device_utils.DeviceUtils(a) |
| 50 b = battery_utils.BatteryUtils(d) |
| 51 self.assertEqual(d, b._device) |
| 52 |
| 53 def testInitWithMissing_fails(self): |
| 54 with self.assertRaises(TypeError): |
| 55 battery_utils.BatteryUtils(None) |
| 56 with self.assertRaises(TypeError): |
| 57 battery_utils.BatteryUtils('') |
| 58 |
| 59 |
| 60 class BatteryUtilsSetChargingTest(BatteryUtilsTest): |
| 61 |
| 62 @mock.patch('time.sleep', mock.Mock()) |
| 63 def testSetCharging_enabled(self): |
| 64 with self.assertCalls( |
| 65 (self.call.device.FileExists(mock.ANY), True), |
| 66 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 67 (self.call.device.GetCharging(), False), |
| 68 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 69 (self.call.device.GetCharging(), True)): |
| 70 self.battery.SetCharging(True) |
| 71 |
| 72 def testSetCharging_alreadyEnabled(self): |
| 73 with self.assertCalls( |
| 74 (self.call.device.FileExists(mock.ANY), True), |
| 75 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 76 (self.call.device.GetCharging(), True)): |
| 77 self.battery.SetCharging(True) |
| 78 |
| 79 @mock.patch('time.sleep', mock.Mock()) |
| 80 def testSetCharging_disabled(self): |
| 81 with self.assertCalls( |
| 82 (self.call.device.FileExists(mock.ANY), True), |
| 83 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 84 (self.call.device.GetCharging(), True), |
| 85 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 86 (self.call.device.GetCharging(), False)): |
| 87 self.battery.SetCharging(False) |
| 88 |
| 89 |
| 90 class BatteryUtilsSetBatteryMeasurementTest(BatteryUtilsTest): |
| 91 |
| 92 def testBatteryMeasurement(self): |
| 93 with self.assertCalls( |
| 94 (self.call.device.RunShellCommand( |
| 95 mock.ANY, retries=0, single_line=True, |
| 96 timeout=10, check_return=True), '22'), |
| 97 (self.call.device.RunShellCommand( |
| 98 ['dumpsys', 'batterystats', '--reset'], check_return=True), []), |
| 99 (self.call.device.RunShellCommand( |
| 100 ['dumpsys', 'batterystats', '--charged', '--checkin'], |
| 101 check_return=True), []), |
| 102 (self.call.device.RunShellCommand( |
| 103 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), |
| 104 (self.call.device.GetCharging(), False), |
| 105 (self.call.device.RunShellCommand( |
| 106 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True), []), |
| 107 (self.call.device.RunShellCommand( |
| 108 ['dumpsys', 'battery', 'reset'], check_return=True), []), |
| 109 (self.call.device.GetCharging(), True)): |
| 110 with self.battery.BatteryMeasurement(): |
| 111 pass |
| 112 |
| 113 |
| 114 class BatteryUtilsGetPowerData(BatteryUtilsTest): |
| 115 |
| 116 _DUMPSYS_OUTPUT = [ |
| 117 '9,0,i,uid,1000,test_package1', |
| 118 '9,0,i,uid,1001,test_package2', |
| 119 '9,1000,l,pwi,uid,1', |
| 120 '9,1001,l,pwi,uid,2' |
| 121 ] |
| 122 |
| 123 def testGetPowerData(self): |
| 124 with self.assertCalls( |
| 125 (self.call.device.RunShellCommand( |
| 126 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 127 self._DUMPSYS_OUTPUT)): |
| 128 data = self.battery.GetPowerData() |
| 129 check = { |
| 130 'test_package1': {'uid': '1000', 'data': [1.0]}, |
| 131 'test_package2': {'uid': '1001', 'data': [2.0]} |
| 132 } |
| 133 self.assertEqual(data, check) |
| 134 |
| 135 def testGetPackagePowerData(self): |
| 136 with self.assertCalls( |
| 137 (self.call.device.RunShellCommand( |
| 138 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 139 self._DUMPSYS_OUTPUT)): |
| 140 data = self.battery.GetPackagePowerData('test_package2') |
| 141 self.assertEqual(data, {'uid': '1001', 'data': [2.0]}) |
| 142 |
| 143 |
| 144 if __name__ == '__main__': |
| 145 logging.getLogger().setLevel(logging.DEBUG) |
| 146 unittest.main(verbosity=2) |
OLD | NEW |