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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 | 193 |
194 Args: | 194 Args: |
195 enabled: A boolean indicating whether charging should be enabled or | 195 enabled: A boolean indicating whether charging should be enabled or |
196 disabled. | 196 disabled. |
197 timeout: timeout in seconds | 197 timeout: timeout in seconds |
198 retries: number of retries | 198 retries: number of retries |
199 | 199 |
200 Raises: | 200 Raises: |
201 device_errors.CommandFailedError: If method of disabling charging cannot | 201 device_errors.CommandFailedError: If method of disabling charging cannot |
202 be determined. | 202 be determined. |
203 device_errors.DeviceVersionError: If device is not L or higher. | |
jbudorick
2015/04/08 18:35:23
er... wrong Raises section.
fmeawad
2015/04/08 18:37:44
Good catch, will fix in another CL.
| |
203 """ | 204 """ |
204 if 'charging_config' not in self._cache: | 205 if 'charging_config' not in self._cache: |
205 for c in _CONTROL_CHARGING_COMMANDS: | 206 for c in _CONTROL_CHARGING_COMMANDS: |
206 if self._device.FileExists(c['witness_file']): | 207 if self._device.FileExists(c['witness_file']): |
207 self._cache['charging_config'] = c | 208 self._cache['charging_config'] = c |
208 break | 209 break |
209 else: | 210 else: |
210 raise device_errors.CommandFailedError( | 211 raise device_errors.CommandFailedError( |
211 'Unable to find charging commands.') | 212 'Unable to find charging commands.') |
212 | 213 |
(...skipping 13 matching lines...) Expand all Loading... | |
226 """ Resets battery data and makes device appear like it is not | 227 """ Resets battery data and makes device appear like it is not |
227 charging so that it will collect power data since last charge. | 228 charging so that it will collect power data since last charge. |
228 | 229 |
229 Args: | 230 Args: |
230 timeout: timeout in seconds | 231 timeout: timeout in seconds |
231 retries: number of retries | 232 retries: number of retries |
232 | 233 |
233 Raises: | 234 Raises: |
234 device_errors.CommandFailedError: When resetting batterystats fails to | 235 device_errors.CommandFailedError: When resetting batterystats fails to |
235 reset power values. | 236 reset power values. |
237 device_errors.DeviceVersionError: If device is not L or higher. | |
236 """ | 238 """ |
237 def battery_updates_disabled(): | 239 def battery_updates_disabled(): |
238 return self.GetCharging() is False | 240 return self.GetCharging() is False |
239 | 241 |
242 if (self._device.build_version_sdk < | |
243 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | |
244 raise device_errors.DeviceVersionError('Device must be L or higher.') | |
245 | |
240 self._device.RunShellCommand( | 246 self._device.RunShellCommand( |
241 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True) | 247 ['dumpsys', 'battery', 'reset'], check_return=True) |
242 self._device.RunShellCommand( | 248 self._device.RunShellCommand( |
243 ['dumpsys', 'batterystats', '--reset'], check_return=True) | 249 ['dumpsys', 'batterystats', '--reset'], check_return=True) |
244 battery_data = self._device.RunShellCommand( | 250 battery_data = self._device.RunShellCommand( |
245 ['dumpsys', 'batterystats', '--charged', '--checkin'], | 251 ['dumpsys', 'batterystats', '--charged', '--checkin'], |
246 check_return=True) | 252 check_return=True) |
247 ROW_TYPE_INDEX = 3 | 253 ROW_TYPE_INDEX = 3 |
248 PWI_POWER_INDEX = 5 | 254 PWI_POWER_INDEX = 5 |
249 for line in battery_data: | 255 for line in battery_data: |
250 l = line.split(',') | 256 l = line.split(',') |
251 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi' | 257 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi' |
252 and l[PWI_POWER_INDEX] != 0): | 258 and l[PWI_POWER_INDEX] != 0): |
253 raise device_errors.CommandFailedError( | 259 raise device_errors.CommandFailedError( |
254 'Non-zero pmi value found after reset.') | 260 'Non-zero pmi value found after reset.') |
261 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'ac', '0'], | |
262 check_return=True) | |
255 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], | 263 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], |
256 check_return=True) | 264 check_return=True) |
257 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1) | 265 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1) |
258 | 266 |
259 # TODO(rnephew): Make private when all use cases can use the context manager. | 267 # TODO(rnephew): Make private when all use cases can use the context manager. |
260 def EnableBatteryUpdates(self, timeout=None, retries=None): | 268 def EnableBatteryUpdates(self, timeout=None, retries=None): |
261 """ Restarts device charging so that dumpsys no longer collects power data. | 269 """ Restarts device charging so that dumpsys no longer collects power data. |
262 | 270 |
263 Args: | 271 Args: |
264 timeout: timeout in seconds | 272 timeout: timeout in seconds |
265 retries: number of retries | 273 retries: number of retries |
266 """ | 274 """ |
jbudorick
2015/04/08 18:35:23
This needs a Raises section with DeviceVersionErro
| |
267 def battery_updates_enabled(): | 275 def battery_updates_enabled(): |
268 return self.GetCharging() is True | 276 return self.GetCharging() is True |
269 | 277 |
270 self._device.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '1'], | 278 if (self._device.build_version_sdk < |
271 check_return=True) | 279 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
280 raise device_errors.DeviceVersionError('Device must be L or higher.') | |
281 | |
272 self._device.RunShellCommand(['dumpsys', 'battery', 'reset'], | 282 self._device.RunShellCommand(['dumpsys', 'battery', 'reset'], |
273 check_return=True) | 283 check_return=True) |
274 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1) | 284 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1) |
275 | 285 |
276 @contextlib.contextmanager | 286 @contextlib.contextmanager |
277 def BatteryMeasurement(self, timeout=None, retries=None): | 287 def BatteryMeasurement(self, timeout=None, retries=None): |
278 """Context manager that enables battery data collection. It makes | 288 """Context manager that enables battery data collection. It makes |
279 the device appear to stop charging so that dumpsys will start collecting | 289 the device appear to stop charging so that dumpsys will start collecting |
280 power data since last charge. Once the with block is exited, charging is | 290 power data since last charge. Once the with block is exited, charging is |
281 resumed and power data since last charge is no longer collected. | 291 resumed and power data since last charge is no longer collected. |
282 | 292 |
283 Only for devices L and higher. | 293 Only for devices L and higher. |
284 | 294 |
285 Example usage: | 295 Example usage: |
286 with BatteryMeasurement(): | 296 with BatteryMeasurement(): |
287 browser_actions() | 297 browser_actions() |
288 get_power_data() # report usage within this block | 298 get_power_data() # report usage within this block |
289 after_measurements() # Anything that runs after power | 299 after_measurements() # Anything that runs after power |
290 # measurements are collected | 300 # measurements are collected |
291 | 301 |
292 Args: | 302 Args: |
293 timeout: timeout in seconds | 303 timeout: timeout in seconds |
294 retries: number of retries | 304 retries: number of retries |
295 | 305 |
296 Raises: | 306 Raises: |
297 device_errors.CommandFailedError: If device is not L or higher. | 307 device_errors.DeviceVersionError: If device is not L or higher. |
298 """ | 308 """ |
299 if (self._device.build_version_sdk < | 309 if (self._device.build_version_sdk < |
300 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | 310 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
301 raise device_errors.DeviceVersionError('Device must be L or higher.') | 311 raise device_errors.DeviceVersionError('Device must be L or higher.') |
302 try: | 312 try: |
303 self.DisableBatteryUpdates(timeout=timeout, retries=retries) | 313 self.DisableBatteryUpdates(timeout=timeout, retries=retries) |
304 yield | 314 yield |
305 finally: | 315 finally: |
306 self.EnableBatteryUpdates(timeout=timeout, retries=retries) | 316 self.EnableBatteryUpdates(timeout=timeout, retries=retries) |
307 | 317 |
(...skipping 10 matching lines...) Expand all Loading... | |
318 battery_level = self.GetBatteryInfo().get('level') | 328 battery_level = self.GetBatteryInfo().get('level') |
319 if battery_level is None: | 329 if battery_level is None: |
320 logging.warning('Unable to find current battery level.') | 330 logging.warning('Unable to find current battery level.') |
321 battery_level = 100 | 331 battery_level = 100 |
322 else: | 332 else: |
323 logging.info('current battery level: %s', battery_level) | 333 logging.info('current battery level: %s', battery_level) |
324 battery_level = int(battery_level) | 334 battery_level = int(battery_level) |
325 return battery_level >= level | 335 return battery_level >= level |
326 | 336 |
327 timeout_retry.WaitFor(device_charged, wait_period=wait_period) | 337 timeout_retry.WaitFor(device_charged, wait_period=wait_period) |
OLD | NEW |