OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Provides a variety of device interactions with power. | 5 """Provides a variety of device interactions with power. |
6 """ | 6 """ |
7 # pylint: disable=unused-argument | 7 # pylint: disable=unused-argument |
8 | 8 |
9 import collections | 9 import collections |
10 import contextlib | 10 import contextlib |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 device_errors.CommandFailedError: If device is not L or higher. | 239 device_errors.CommandFailedError: If device is not L or higher. |
240 """ | 240 """ |
241 if (self._device.build_version_sdk < | 241 if (self._device.build_version_sdk < |
242 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | 242 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
243 raise device_errors.DeviceVersionError('Device must be L or higher.') | 243 raise device_errors.DeviceVersionError('Device must be L or higher.') |
244 try: | 244 try: |
245 self.DisableBatteryUpdates(timeout=timeout, retries=retries) | 245 self.DisableBatteryUpdates(timeout=timeout, retries=retries) |
246 yield | 246 yield |
247 finally: | 247 finally: |
248 self.EnableBatteryUpdates(timeout=timeout, retries=retries) | 248 self.EnableBatteryUpdates(timeout=timeout, retries=retries) |
| 249 |
| 250 def ChargeDeviceToLevel(self, level, wait_period=60): |
| 251 """Enables charging and waits for device to be charged to given level. |
| 252 |
| 253 Args: |
| 254 level: level of charge to wait for. |
| 255 wait_period: time in seconds to wait between checking. |
| 256 """ |
| 257 self.SetCharging(True) |
| 258 |
| 259 def device_charged(): |
| 260 battery_level = self.GetBatteryInfo().get('level') |
| 261 if battery_level is None: |
| 262 logging.warning('Unable to find current battery level.') |
| 263 battery_level = 100 |
| 264 else: |
| 265 logging.info('current battery level: %s', battery_level) |
| 266 battery_level = int(battery_level) |
| 267 return battery_level >= level |
| 268 |
| 269 timeout_retry.WaitFor(device_charged, wait_period=wait_period) |
OLD | NEW |