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 # pylint: disable=W0613 |
| 11 |
| 12 import logging |
| 13 import os |
| 14 import sys |
| 15 import unittest |
| 16 |
| 17 from pylib import constants |
| 18 from pylib.device import battery_utils |
| 19 from pylib.device import device_errors |
| 20 from pylib.device import device_utils |
| 21 from pylib.device import device_utils_test |
| 22 from pylib.utils import mock_calls |
| 23 |
| 24 # RunCommand from third_party/android_testrunner/run_command.py is mocked |
| 25 # below, so its path needs to be in sys.path. |
| 26 sys.path.append(os.path.join( |
| 27 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) |
| 28 |
| 29 sys.path.append(os.path.join( |
| 30 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
| 31 import mock # pylint: disable=F0401 |
| 32 |
| 33 _DUMPSYS_OUTPUT = [ |
| 34 '9,0,i,uid,1000,test_package1', |
| 35 '9,0,i,uid,1001,test_package2', |
| 36 '9,1000,l,pwi,uid,1', |
| 37 '9,1001,l,pwi,uid,2' |
| 38 ] |
| 39 |
| 40 |
| 41 class BatteryUtilsTest(mock_calls.TestCase): |
| 42 |
| 43 _NEXUS_5 = { |
| 44 'name': 'Nexus 5', |
| 45 'witness_file': '/sys/kernel/debug/bq24192/INPUT_SRC_CONT', |
| 46 'enable_command': ( |
| 47 'echo 0x4A > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' |
| 48 'echo 1 > /sys/class/power_supply/usb/online'), |
| 49 'disable_command': ( |
| 50 'echo 0xCA > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' |
| 51 'chmod 644 /sys/class/power_supply/usb/online && ' |
| 52 'echo 0 > /sys/class/power_supply/usb/online'), |
| 53 'charge_counter': None, |
| 54 'voltage': None, |
| 55 'current': None, |
| 56 } |
| 57 |
| 58 _NEXUS_6 = { |
| 59 'name': 'Nexus 6', |
| 60 'witness_file': None, |
| 61 'enable_command': None, |
| 62 'disable_command': None, |
| 63 'charge_counter': ( |
| 64 '/sys/class/power_supply/max170xx_battery/charge_counter_ext'), |
| 65 'voltage': '/sys/class/power_supply/max170xx_battery/voltage_now', |
| 66 'current': '/sys/class/power_supply/max170xx_battery/current_now', |
| 67 } |
| 68 |
| 69 _NEXUS_10 = { |
| 70 'name': 'Nexus 10', |
| 71 'witness_file': None, |
| 72 'enable_command': None, |
| 73 'disable_command': None, |
| 74 'charge_counter': ( |
| 75 '/sys/class/power_supply/ds2784-fuelgauge/charge_counter_ext'), |
| 76 'voltage': '/sys/class/power_supply/ds2784-fuelgauge/voltage_now', |
| 77 'current': '/sys/class/power_supply/ds2784-fuelgauge/current_now', |
| 78 } |
| 79 |
| 80 def ShellError(self, output=None, status=1): |
| 81 def action(cmd, *args, **kwargs): |
| 82 raise device_errors.AdbShellCommandFailedError( |
| 83 cmd, output, status, str(self.device)) |
| 84 if output is None: |
| 85 output = 'Permission denied\n' |
| 86 return action |
| 87 |
| 88 def setUp(self): |
| 89 self.adb = device_utils_test._AdbWrapperMock('0123456789abcdef') |
| 90 self.device = device_utils.DeviceUtils( |
| 91 self.adb, default_timeout=10, default_retries=0) |
| 92 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) |
| 93 self.battery = battery_utils.BatteryUtils( |
| 94 self.device, default_timeout=10, default_retries=0) |
| 95 |
| 96 |
| 97 class BatteryUtilsInitTest(unittest.TestCase): |
| 98 |
| 99 def testInitWithDeviceUtil(self): |
| 100 serial = '0fedcba987654321' |
| 101 d = device_utils.DeviceUtils(serial) |
| 102 b = battery_utils.BatteryUtils(d) |
| 103 self.assertEqual(d, b._device) |
| 104 |
| 105 def testInitWithMissing_fails(self): |
| 106 with self.assertRaises(TypeError): |
| 107 battery_utils.BatteryUtils(None) |
| 108 with self.assertRaises(TypeError): |
| 109 battery_utils.BatteryUtils('') |
| 110 |
| 111 |
| 112 class BatteryUtilsSetChargingTest(BatteryUtilsTest): |
| 113 |
| 114 @mock.patch('time.sleep', mock.Mock()) |
| 115 def testSetCharging_enabled(self): |
| 116 self.battery._cache['profile'] = self._NEXUS_5 |
| 117 with self.assertCalls( |
| 118 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 119 (self.call.battery.GetCharging(), False), |
| 120 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 121 (self.call.battery.GetCharging(), True)): |
| 122 self.battery.SetCharging(True) |
| 123 |
| 124 def testSetCharging_alreadyEnabled(self): |
| 125 self.battery._cache['profile'] = self._NEXUS_5 |
| 126 with self.assertCalls( |
| 127 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 128 (self.call.battery.GetCharging(), True)): |
| 129 self.battery.SetCharging(True) |
| 130 |
| 131 @mock.patch('time.sleep', mock.Mock()) |
| 132 def testSetCharging_disabled(self): |
| 133 self.battery._cache['profile'] = self._NEXUS_5 |
| 134 with self.assertCalls( |
| 135 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 136 (self.call.battery.GetCharging(), True), |
| 137 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 138 (self.call.battery.GetCharging(), False)): |
| 139 self.battery.SetCharging(False) |
| 140 |
| 141 |
| 142 class BatteryUtilsSetBatteryMeasurementTest(BatteryUtilsTest): |
| 143 |
| 144 @mock.patch('time.sleep', mock.Mock()) |
| 145 def testBatteryMeasurementWifi(self): |
| 146 with self.assertCalls( |
| 147 (self.call.device.RunShellCommand( |
| 148 mock.ANY, retries=0, single_line=True, |
| 149 timeout=10, check_return=True), '22'), |
| 150 (self.call.battery._ClearPowerData(), True), |
| 151 (self.call.device.RunShellCommand( |
| 152 ['dumpsys', 'battery', 'set', 'ac', '0'], check_return=True), []), |
| 153 (self.call.device.RunShellCommand( |
| 154 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), |
| 155 (self.call.battery.GetCharging(), False), |
| 156 (self.call.device.RunShellCommand( |
| 157 ['dumpsys', 'battery', 'reset'], check_return=True), []), |
| 158 (self.call.battery.GetCharging(), False), |
| 159 (self.call.device.RunShellCommand( |
| 160 ['dumpsys', 'battery'], check_return=True), ['UPDATES STOPPED']), |
| 161 (self.call.battery.GetCharging(), False), |
| 162 (self.call.device.RunShellCommand( |
| 163 ['dumpsys', 'battery'], check_return=True), [])): |
| 164 with self.battery.BatteryMeasurement(): |
| 165 pass |
| 166 |
| 167 @mock.patch('time.sleep', mock.Mock()) |
| 168 def testBatteryMeasurementUsb(self): |
| 169 with self.assertCalls( |
| 170 (self.call.device.RunShellCommand( |
| 171 mock.ANY, retries=0, single_line=True, |
| 172 timeout=10, check_return=True), '22'), |
| 173 (self.call.battery._ClearPowerData(), True), |
| 174 (self.call.device.RunShellCommand( |
| 175 ['dumpsys', 'battery', 'set', 'ac', '0'], check_return=True), []), |
| 176 (self.call.device.RunShellCommand( |
| 177 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), |
| 178 (self.call.battery.GetCharging(), False), |
| 179 (self.call.device.RunShellCommand( |
| 180 ['dumpsys', 'battery', 'reset'], check_return=True), []), |
| 181 (self.call.battery.GetCharging(), False), |
| 182 (self.call.device.RunShellCommand( |
| 183 ['dumpsys', 'battery'], check_return=True), ['UPDATES STOPPED']), |
| 184 (self.call.battery.GetCharging(), True)): |
| 185 with self.battery.BatteryMeasurement(): |
| 186 pass |
| 187 |
| 188 |
| 189 class BatteryUtilsGetPowerData(BatteryUtilsTest): |
| 190 |
| 191 def testGetPowerData(self): |
| 192 with self.assertCalls( |
| 193 (self.call.device.RunShellCommand( |
| 194 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 195 _DUMPSYS_OUTPUT)): |
| 196 data = self.battery.GetPowerData() |
| 197 check = { |
| 198 'test_package1': {'uid': '1000', 'data': [1.0]}, |
| 199 'test_package2': {'uid': '1001', 'data': [2.0]} |
| 200 } |
| 201 self.assertEqual(data, check) |
| 202 |
| 203 def testGetPowerData_packageCollisionSame(self): |
| 204 self.battery._cache['uids'] = {'test_package1': '1000'} |
| 205 with self.assertCall( |
| 206 self.call.device.RunShellCommand( |
| 207 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 208 _DUMPSYS_OUTPUT): |
| 209 data = self.battery.GetPowerData() |
| 210 check = { |
| 211 'test_package1': {'uid': '1000', 'data': [1.0]}, |
| 212 'test_package2': {'uid': '1001', 'data': [2.0]} |
| 213 } |
| 214 self.assertEqual(data, check) |
| 215 |
| 216 def testGetPowerData_packageCollisionDifferent(self): |
| 217 self.battery._cache['uids'] = {'test_package1': '1'} |
| 218 with self.assertCall( |
| 219 self.call.device.RunShellCommand( |
| 220 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 221 _DUMPSYS_OUTPUT): |
| 222 with self.assertRaises(device_errors.CommandFailedError): |
| 223 self.battery.GetPowerData() |
| 224 |
| 225 def testGetPowerData_cacheCleared(self): |
| 226 with self.assertCalls( |
| 227 (self.call.device.RunShellCommand( |
| 228 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 229 _DUMPSYS_OUTPUT)): |
| 230 self.battery._cache.clear() |
| 231 data = self.battery.GetPowerData() |
| 232 check = { |
| 233 'test_package1': {'uid': '1000', 'data': [1.0]}, |
| 234 'test_package2': {'uid': '1001', 'data': [2.0]} |
| 235 } |
| 236 self.assertEqual(data, check) |
| 237 |
| 238 def testGetPackagePowerData(self): |
| 239 with self.assertCalls( |
| 240 (self.call.device.RunShellCommand( |
| 241 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 242 _DUMPSYS_OUTPUT)): |
| 243 data = self.battery.GetPackagePowerData('test_package2') |
| 244 self.assertEqual(data, {'uid': '1001', 'data': [2.0]}) |
| 245 |
| 246 def testGetPackagePowerData_badPackage(self): |
| 247 with self.assertCalls( |
| 248 (self.call.device.RunShellCommand( |
| 249 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 250 _DUMPSYS_OUTPUT)): |
| 251 data = self.battery.GetPackagePowerData('not_a_package') |
| 252 self.assertEqual(data, None) |
| 253 |
| 254 |
| 255 class BatteryUtilsChargeDevice(BatteryUtilsTest): |
| 256 |
| 257 @mock.patch('time.sleep', mock.Mock()) |
| 258 def testChargeDeviceToLevel(self): |
| 259 with self.assertCalls( |
| 260 (self.call.battery.SetCharging(True)), |
| 261 (self.call.battery.GetBatteryInfo(), {'level': '50'}), |
| 262 (self.call.battery.GetBatteryInfo(), {'level': '100'})): |
| 263 self.battery.ChargeDeviceToLevel(95) |
| 264 |
| 265 |
| 266 class BatteryUtilsGetBatteryInfoTest(BatteryUtilsTest): |
| 267 |
| 268 def testGetBatteryInfo_normal(self): |
| 269 with self.assertCall( |
| 270 self.call.device.RunShellCommand( |
| 271 ['dumpsys', 'battery'], check_return=True), |
| 272 [ |
| 273 'Current Battery Service state:', |
| 274 ' AC powered: false', |
| 275 ' USB powered: true', |
| 276 ' level: 100', |
| 277 ' temperature: 321', |
| 278 ]): |
| 279 self.assertEquals( |
| 280 { |
| 281 'AC powered': 'false', |
| 282 'USB powered': 'true', |
| 283 'level': '100', |
| 284 'temperature': '321', |
| 285 }, |
| 286 self.battery.GetBatteryInfo()) |
| 287 |
| 288 def testGetBatteryInfo_nothing(self): |
| 289 with self.assertCall( |
| 290 self.call.device.RunShellCommand( |
| 291 ['dumpsys', 'battery'], check_return=True), []): |
| 292 self.assertEquals({}, self.battery.GetBatteryInfo()) |
| 293 |
| 294 |
| 295 class BatteryUtilsGetChargingTest(BatteryUtilsTest): |
| 296 |
| 297 def testGetCharging_usb(self): |
| 298 with self.assertCall( |
| 299 self.call.battery.GetBatteryInfo(), {'USB powered': 'true'}): |
| 300 self.assertTrue(self.battery.GetCharging()) |
| 301 |
| 302 def testGetCharging_usbFalse(self): |
| 303 with self.assertCall( |
| 304 self.call.battery.GetBatteryInfo(), {'USB powered': 'false'}): |
| 305 self.assertFalse(self.battery.GetCharging()) |
| 306 |
| 307 def testGetCharging_ac(self): |
| 308 with self.assertCall( |
| 309 self.call.battery.GetBatteryInfo(), {'AC powered': 'true'}): |
| 310 self.assertTrue(self.battery.GetCharging()) |
| 311 |
| 312 def testGetCharging_wireless(self): |
| 313 with self.assertCall( |
| 314 self.call.battery.GetBatteryInfo(), {'Wireless powered': 'true'}): |
| 315 self.assertTrue(self.battery.GetCharging()) |
| 316 |
| 317 def testGetCharging_unknown(self): |
| 318 with self.assertCall( |
| 319 self.call.battery.GetBatteryInfo(), {'level': '42'}): |
| 320 self.assertFalse(self.battery.GetCharging()) |
| 321 |
| 322 |
| 323 class BatteryUtilsGetNetworkDataTest(BatteryUtilsTest): |
| 324 |
| 325 def testGetNetworkData_noDataUsage(self): |
| 326 with self.assertCalls( |
| 327 (self.call.device.RunShellCommand( |
| 328 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 329 _DUMPSYS_OUTPUT), |
| 330 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), |
| 331 self.ShellError()), |
| 332 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), |
| 333 self.ShellError())): |
| 334 self.assertEquals(self.battery.GetNetworkData('test_package1'), (0, 0)) |
| 335 |
| 336 def testGetNetworkData_badPackage(self): |
| 337 with self.assertCall( |
| 338 self.call.device.RunShellCommand( |
| 339 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 340 _DUMPSYS_OUTPUT): |
| 341 self.assertEqual(self.battery.GetNetworkData('asdf'), None) |
| 342 |
| 343 def testGetNetworkData_packageNotCached(self): |
| 344 with self.assertCalls( |
| 345 (self.call.device.RunShellCommand( |
| 346 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 347 _DUMPSYS_OUTPUT), |
| 348 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), 1), |
| 349 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), 2)): |
| 350 self.assertEqual(self.battery.GetNetworkData('test_package1'), (1,2)) |
| 351 |
| 352 def testGetNetworkData_packageCached(self): |
| 353 self.battery._cache['uids'] = {'test_package1': '1000'} |
| 354 with self.assertCalls( |
| 355 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), 1), |
| 356 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), 2)): |
| 357 self.assertEqual(self.battery.GetNetworkData('test_package1'), (1,2)) |
| 358 |
| 359 def testGetNetworkData_clearedCache(self): |
| 360 with self.assertCalls( |
| 361 (self.call.device.RunShellCommand( |
| 362 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 363 _DUMPSYS_OUTPUT), |
| 364 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), 1), |
| 365 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), 2)): |
| 366 self.battery._cache.clear() |
| 367 self.assertEqual(self.battery.GetNetworkData('test_package1'), (1,2)) |
| 368 |
| 369 |
| 370 class BatteryUtilsLetBatteryCoolToTemperatureTest(BatteryUtilsTest): |
| 371 |
| 372 @mock.patch('time.sleep', mock.Mock()) |
| 373 def testLetBatteryCoolToTemperature_startUnder(self): |
| 374 with self.assertCalls( |
| 375 (self.call.battery.EnableBatteryUpdates(), []), |
| 376 (self.call.battery.GetBatteryInfo(), {'temperature': '500'})): |
| 377 self.battery.LetBatteryCoolToTemperature(600) |
| 378 |
| 379 @mock.patch('time.sleep', mock.Mock()) |
| 380 def testLetBatteryCoolToTemperature_startOver(self): |
| 381 with self.assertCalls( |
| 382 (self.call.battery.EnableBatteryUpdates(), []), |
| 383 (self.call.battery.GetBatteryInfo(), {'temperature': '500'}), |
| 384 (self.call.battery.GetBatteryInfo(), {'temperature': '400'})): |
| 385 self.battery.LetBatteryCoolToTemperature(400) |
| 386 |
| 387 class BatteryUtilsSupportsFuelGaugeTest(BatteryUtilsTest): |
| 388 |
| 389 def testSupportsFuelGauge_false(self): |
| 390 self.battery._cache['profile'] = self._NEXUS_5 |
| 391 self.assertFalse(self.battery.SupportsFuelGauge()) |
| 392 |
| 393 def testSupportsFuelGauge_trueMax(self): |
| 394 self.battery._cache['profile'] = self._NEXUS_6 |
| 395 # TODO(rnephew): Change this to assertTrue when we have support for |
| 396 # disabling hardware charging on nexus 6. |
| 397 self.assertFalse(self.battery.SupportsFuelGauge()) |
| 398 |
| 399 def testSupportsFuelGauge_trueDS(self): |
| 400 self.battery._cache['profile'] = self._NEXUS_10 |
| 401 # TODO(rnephew): Change this to assertTrue when we have support for |
| 402 # disabling hardware charging on nexus 10. |
| 403 self.assertFalse(self.battery.SupportsFuelGauge()) |
| 404 |
| 405 |
| 406 class BatteryUtilsGetFuelGaugeChargeCounterTest(BatteryUtilsTest): |
| 407 |
| 408 def testGetFuelGaugeChargeCounter_noFuelGauge(self): |
| 409 self.battery._cache['profile'] = self._NEXUS_5 |
| 410 with self.assertRaises(device_errors.CommandFailedError): |
| 411 self.battery.GetFuelGaugeChargeCounter() |
| 412 |
| 413 def testGetFuelGaugeChargeCounter_fuelGaugePresent(self): |
| 414 self.battery._cache['profile']= self._NEXUS_6 |
| 415 with self.assertCalls( |
| 416 (self.call.battery.SupportsFuelGauge(), True), |
| 417 (self.call.device.ReadFile(mock.ANY), '123')): |
| 418 self.assertEqual(self.battery.GetFuelGaugeChargeCounter(), 123) |
| 419 |
| 420 |
| 421 class BatteryUtilsTieredSetCharging(BatteryUtilsTest): |
| 422 |
| 423 @mock.patch('time.sleep', mock.Mock()) |
| 424 def testTieredSetCharging_softwareSetTrue(self): |
| 425 self.battery._cache['profile'] = self._NEXUS_6 |
| 426 with self.assertCalls( |
| 427 (self.call.battery.GetCharging(), False), |
| 428 (self.call.device.RunShellCommand( |
| 429 ['dumpsys', 'battery', 'reset'], check_return=True), []), |
| 430 (self.call.battery.GetCharging(), False), |
| 431 (self.call.device.RunShellCommand( |
| 432 ['dumpsys', 'battery'], check_return=True), ['UPDATES STOPPED']), |
| 433 (self.call.battery.GetCharging(), True)): |
| 434 self.battery.TieredSetCharging(True) |
| 435 |
| 436 @mock.patch('time.sleep', mock.Mock()) |
| 437 def testTieredSetCharging_softwareSetFalse(self): |
| 438 self.battery._cache['profile'] = self._NEXUS_6 |
| 439 with self.assertCalls( |
| 440 (self.call.battery.GetCharging(), True), |
| 441 (self.call.battery._ClearPowerData(), True), |
| 442 (self.call.battery._ClearPowerData(), True), |
| 443 (self.call.device.RunShellCommand( |
| 444 ['dumpsys', 'battery', 'set', 'ac', '0'], check_return=True), []), |
| 445 (self.call.device.RunShellCommand( |
| 446 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), |
| 447 (self.call.battery.GetCharging(), False)): |
| 448 self.battery.TieredSetCharging(False) |
| 449 |
| 450 @mock.patch('time.sleep', mock.Mock()) |
| 451 def testTieredSetCharging_hardwareSetTrue(self): |
| 452 self.battery._cache['profile'] = self._NEXUS_5 |
| 453 with self.assertCalls( |
| 454 (self.call.battery.GetCharging(), False), |
| 455 (self.call.battery.SetCharging(True))): |
| 456 self.battery.TieredSetCharging(True) |
| 457 |
| 458 @mock.patch('time.sleep', mock.Mock()) |
| 459 def testTieredSetCharging_hardwareSetFalse(self): |
| 460 self.battery._cache['profile'] = self._NEXUS_5 |
| 461 with self.assertCalls( |
| 462 (self.call.battery.GetCharging(), True), |
| 463 (self.call.battery._ClearPowerData(), True), |
| 464 (self.call.battery.SetCharging(False))): |
| 465 self.battery.TieredSetCharging(False) |
| 466 |
| 467 def testTieredSetCharging_expectedStateAlreadyTrue(self): |
| 468 with self.assertCalls((self.call.battery.GetCharging(), True)): |
| 469 self.battery.TieredSetCharging(True) |
| 470 |
| 471 def testTieredSetCharging_expectedStateAlreadyFalse(self): |
| 472 with self.assertCalls((self.call.battery.GetCharging(), False)): |
| 473 self.battery.TieredSetCharging(False) |
| 474 |
| 475 |
| 476 class BatteryUtilsPowerMeasurement(BatteryUtilsTest): |
| 477 |
| 478 def testPowerMeasurement_hardware(self): |
| 479 self.battery._cache['profile'] = self._NEXUS_5 |
| 480 with self.assertCalls( |
| 481 (self.call.battery.GetCharging(), True), |
| 482 (self.call.battery._ClearPowerData(), True), |
| 483 (self.call.battery.SetCharging(False)), |
| 484 (self.call.battery.GetCharging(), False), |
| 485 (self.call.battery.SetCharging(True))): |
| 486 with self.battery.PowerMeasurement(): |
| 487 pass |
| 488 |
| 489 @mock.patch('time.sleep', mock.Mock()) |
| 490 def testPowerMeasurement_software(self): |
| 491 self.battery._cache['profile'] = self._NEXUS_6 |
| 492 with self.assertCalls( |
| 493 (self.call.battery.GetCharging(), True), |
| 494 (self.call.battery._ClearPowerData(), True), |
| 495 (self.call.battery._ClearPowerData(), True), |
| 496 (self.call.device.RunShellCommand( |
| 497 ['dumpsys', 'battery', 'set', 'ac', '0'], check_return=True), []), |
| 498 (self.call.device.RunShellCommand( |
| 499 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), |
| 500 (self.call.battery.GetCharging(), False), |
| 501 (self.call.battery.GetCharging(), False), |
| 502 (self.call.device.RunShellCommand( |
| 503 ['dumpsys', 'battery', 'reset'], check_return=True), []), |
| 504 (self.call.battery.GetCharging(), False), |
| 505 (self.call.device.RunShellCommand( |
| 506 ['dumpsys', 'battery'], check_return=True), ['UPDATES STOPPED']), |
| 507 (self.call.battery.GetCharging(), True)): |
| 508 with self.battery.PowerMeasurement(): |
| 509 pass |
| 510 |
| 511 |
| 512 class BatteryUtilsDiscoverDeviceProfile(BatteryUtilsTest): |
| 513 |
| 514 def testDiscoverDeviceProfile_known(self): |
| 515 with self.assertCalls( |
| 516 (self.call.adb.Shell('getprop ro.product.model'), "Nexus 4")): |
| 517 self.battery._DiscoverDeviceProfile() |
| 518 self.assertEqual(self.battery._cache['profile']['name'], "Nexus 4") |
| 519 |
| 520 def testDiscoverDeviceProfile_unknown(self): |
| 521 with self.assertCalls( |
| 522 (self.call.adb.Shell('getprop ro.product.model'), "Other")): |
| 523 self.battery._DiscoverDeviceProfile() |
| 524 self.assertEqual(self.battery._cache['profile']['name'], None) |
| 525 |
| 526 |
| 527 class BatteryUtilsClearPowerData(BatteryUtilsTest): |
| 528 |
| 529 def testClearPowerData_preL(self): |
| 530 with self.assertCalls( |
| 531 (self.call.device.RunShellCommand(mock.ANY, retries=0, |
| 532 single_line=True, timeout=10, check_return=True), '20')): |
| 533 self.assertFalse(self.battery._ClearPowerData()) |
| 534 |
| 535 def testClearPowerData_clearedL(self): |
| 536 with self.assertCalls( |
| 537 (self.call.device.RunShellCommand(mock.ANY, retries=0, |
| 538 single_line=True, timeout=10, check_return=True), '22'), |
| 539 (self.call.device.RunShellCommand( |
| 540 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True), []), |
| 541 (self.call.device.RunShellCommand( |
| 542 ['dumpsys', 'battery', 'set', 'ac', '1'], check_return=True), []), |
| 543 (self.call.device.RunShellCommand( |
| 544 ['dumpsys', 'batterystats', '--reset'], check_return=True), []), |
| 545 (self.call.device.RunShellCommand( |
| 546 ['dumpsys', 'batterystats', '--charged', '--checkin'], |
| 547 check_return=True, large_output=True), []), |
| 548 (self.call.device.RunShellCommand( |
| 549 ['dumpsys', 'battery', 'reset'], check_return=True), [])): |
| 550 self.assertTrue(self.battery._ClearPowerData()) |
| 551 |
| 552 def testClearPowerData_notClearedL(self): |
| 553 with self.assertCalls( |
| 554 (self.call.device.RunShellCommand(mock.ANY, retries=0, |
| 555 single_line=True, timeout=10, check_return=True), '22'), |
| 556 (self.call.device.RunShellCommand( |
| 557 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True), []), |
| 558 (self.call.device.RunShellCommand( |
| 559 ['dumpsys', 'battery', 'set', 'ac', '1'], check_return=True), []), |
| 560 (self.call.device.RunShellCommand( |
| 561 ['dumpsys', 'batterystats', '--reset'], check_return=True), []), |
| 562 (self.call.device.RunShellCommand( |
| 563 ['dumpsys', 'batterystats', '--charged', '--checkin'], |
| 564 check_return=True, large_output=True), |
| 565 ['9,1000,l,pwi,uid,0.0327']), |
| 566 (self.call.device.RunShellCommand( |
| 567 ['dumpsys', 'battery', 'reset'], check_return=True), [])): |
| 568 with self.assertRaises(device_errors.CommandFailedError): |
| 569 self.battery._ClearPowerData() |
| 570 |
| 571 |
| 572 if __name__ == '__main__': |
| 573 logging.getLogger().setLevel(logging.DEBUG) |
| 574 unittest.main(verbosity=2) |
OLD | NEW |