From e980d5680ed98ea898f65c8ff09f36c97040b1f3 Mon Sep 17 00:00:00 2001 From: rnephew Date: Tue, 18 Aug 2015 08:58:00 -0700 Subject: [PATCH] [Android] Change battery_utils TieredSetCharging from exceptions based to discovery based Currently, it throws an exception to know it cannot do hardware level disable, this changes it to use profile discovery to decide how to disable charging. BUG=520958 Review URL: https://codereview.chromium.org/1293143002 Cr-Original-Commit-Position: refs/heads/master@{#343929} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: c0202d6ab7d128fe54e6fea8ab654918e5e2f3f2 --- android/pylib/device/battery_utils.py | 17 +++++++++-------- android/pylib/device/battery_utils_test.py | 2 -- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/android/pylib/device/battery_utils.py b/android/pylib/device/battery_utils.py index ac5e7a87a..1c7a06e7d 100644 --- a/android/pylib/device/battery_utils.py +++ b/android/pylib/device/battery_utils.py @@ -487,20 +487,21 @@ class BatteryUtils(object): logging.warning('Device charging already in expected state: %s', enabled) return + self._DiscoverDeviceProfile() if enabled: - try: + if self._cache['profile']['enable_command']: self.SetCharging(enabled) - except device_errors.CommandFailedError: - logging.info('Unable to enable charging via hardware.' - ' Falling back to software enabling.') + else: + logging.info('Unable to enable charging via hardware. ' + 'Falling back to software enabling.') self.EnableBatteryUpdates() else: - try: + if self._cache['profile']['enable_command']: self._ClearPowerData() self.SetCharging(enabled) - except device_errors.CommandFailedError: - logging.info('Unable to disable charging via hardware.' - ' Falling back to software disabling.') + else: + logging.info('Unable to disable charging via hardware. ' + 'Falling back to software disabling.') self.DisableBatteryUpdates() @contextlib.contextmanager diff --git a/android/pylib/device/battery_utils_test.py b/android/pylib/device/battery_utils_test.py index bc1e34122..c740e930d 100755 --- a/android/pylib/device/battery_utils_test.py +++ b/android/pylib/device/battery_utils_test.py @@ -439,7 +439,6 @@ class BatteryUtilsTieredSetCharging(BatteryUtilsTest): with self.assertCalls( (self.call.battery.GetCharging(), True), (self.call.battery._ClearPowerData(), True), - (self.call.battery._ClearPowerData(), True), (self.call.device.RunShellCommand( ['dumpsys', 'battery', 'set', 'ac', '0'], check_return=True), []), (self.call.device.RunShellCommand( @@ -492,7 +491,6 @@ class BatteryUtilsPowerMeasurement(BatteryUtilsTest): with self.assertCalls( (self.call.battery.GetCharging(), True), (self.call.battery._ClearPowerData(), True), - (self.call.battery._ClearPowerData(), True), (self.call.device.RunShellCommand( ['dumpsys', 'battery', 'set', 'ac', '0'], check_return=True), []), (self.call.device.RunShellCommand(