Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 based on adb. | 5 """Provides a variety of device interactions based on adb. |
| 6 | 6 |
| 7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
| 8 """ | 8 """ |
| 9 # pylint: disable=unused-argument | 9 # pylint: disable=unused-argument |
| 10 | 10 |
| 11 import collections | 11 import collections |
| 12 import contextlib | |
| 12 import itertools | 13 import itertools |
| 13 import logging | 14 import logging |
| 14 import multiprocessing | 15 import multiprocessing |
| 15 import os | 16 import os |
| 16 import re | 17 import re |
| 17 import sys | 18 import sys |
| 18 import tempfile | 19 import tempfile |
| 19 import time | 20 import time |
| 20 import zipfile | 21 import zipfile |
| 21 | 22 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 'enable_command': ( | 63 'enable_command': ( |
| 63 'echo 0x4A > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' | 64 'echo 0x4A > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' |
| 64 'echo 1 > /sys/class/power_supply/usb/online'), | 65 'echo 1 > /sys/class/power_supply/usb/online'), |
| 65 'disable_command': ( | 66 'disable_command': ( |
| 66 'echo 0xCA > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' | 67 'echo 0xCA > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' |
| 67 'chmod 644 /sys/class/power_supply/usb/online && ' | 68 'chmod 644 /sys/class/power_supply/usb/online && ' |
| 68 'echo 0 > /sys/class/power_supply/usb/online'), | 69 'echo 0 > /sys/class/power_supply/usb/online'), |
| 69 }, | 70 }, |
| 70 ] | 71 ] |
| 71 | 72 |
| 72 | |
|
perezju
2015/03/17 12:52:19
nit: this line went missing
| |
| 73 @decorators.WithExplicitTimeoutAndRetries( | 73 @decorators.WithExplicitTimeoutAndRetries( |
| 74 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 74 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
| 75 def GetAVDs(): | 75 def GetAVDs(): |
| 76 """Returns a list of Android Virtual Devices. | 76 """Returns a list of Android Virtual Devices. |
| 77 | 77 |
| 78 Returns: | 78 Returns: |
| 79 A list containing the configured AVDs. | 79 A list containing the configured AVDs. |
| 80 """ | 80 """ |
| 81 lines = cmd_helper.GetCmdOutput([ | 81 lines = cmd_helper.GetCmdOutput([ |
| 82 os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android'), | 82 os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android'), |
| (...skipping 1324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1407 timeout: timeout in seconds | 1407 timeout: timeout in seconds |
| 1408 retries: number of retries | 1408 retries: number of retries |
| 1409 Returns: | 1409 Returns: |
| 1410 A dict containing various battery information as reported by dumpsys | 1410 A dict containing various battery information as reported by dumpsys |
| 1411 battery. | 1411 battery. |
| 1412 """ | 1412 """ |
| 1413 result = {} | 1413 result = {} |
| 1414 # Skip the first line, which is just a header. | 1414 # Skip the first line, which is just a header. |
| 1415 for line in self.RunShellCommand( | 1415 for line in self.RunShellCommand( |
| 1416 ['dumpsys', 'battery'], check_return=True)[1:]: | 1416 ['dumpsys', 'battery'], check_return=True)[1:]: |
| 1417 k, v = line.split(': ', 1) | 1417 # If usb charging has been disabled, an extra line of header exists. |
| 1418 result[k.strip()] = v.strip() | 1418 if 'UPDATES STOPPED' in line: |
| 1419 logging.warning('Dumpsys battery not receiving updates. ' | |
| 1420 'Run dumpsys battery reset if this is in error.') | |
| 1421 elif ':' not in line: | |
| 1422 logging.warning('Unknown line found in dumpsys battery.') | |
| 1423 logging.warning(line) | |
| 1424 else: | |
| 1425 k, v = line.split(': ', 1) | |
| 1426 result[k.strip()] = v.strip() | |
| 1419 return result | 1427 return result |
| 1420 | 1428 |
| 1421 @decorators.WithTimeoutAndRetriesFromInstance() | 1429 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1422 def GetCharging(self, timeout=None, retries=None): | 1430 def GetCharging(self, timeout=None, retries=None): |
| 1423 """Gets the charging state of the device. | 1431 """Gets the charging state of the device. |
| 1424 | 1432 |
| 1425 Args: | 1433 Args: |
| 1426 timeout: timeout in seconds | 1434 timeout: timeout in seconds |
| 1427 retries: number of retries | 1435 retries: number of retries |
| 1428 Returns: | 1436 Returns: |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1459 else: | 1467 else: |
| 1460 command = self._cache['charging_config']['disable_command'] | 1468 command = self._cache['charging_config']['disable_command'] |
| 1461 | 1469 |
| 1462 def set_and_verify_charging(): | 1470 def set_and_verify_charging(): |
| 1463 self.RunShellCommand(command, check_return=True) | 1471 self.RunShellCommand(command, check_return=True) |
| 1464 return self.GetCharging() == enabled | 1472 return self.GetCharging() == enabled |
| 1465 | 1473 |
| 1466 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1) | 1474 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1) |
| 1467 | 1475 |
| 1468 @decorators.WithTimeoutAndRetriesFromInstance() | 1476 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1477 def _DisableBatteryUpdates(self, timeout=None, retries=None): | |
| 1478 def battery_updates_disabled(): | |
| 1479 return self.GetCharging() is False | |
| 1480 | |
| 1481 self.RunShellCommand( | |
| 1482 ['dumpsys', 'batterystats', '--reset'], check_return=True) | |
| 1483 battery_data = self.RunShellCommand( | |
| 1484 ['dumpsys', 'batterystats', '--charged', | |
| 1485 '--checkin'], check_return=True) | |
| 1486 for line in battery_data: | |
| 1487 l = line.split(',') | |
| 1488 if l[3] == 'pwi' and l[1] != 0: | |
| 1489 raise device_errors.CommandFailedError( | |
| 1490 'Non-zero pmi value found after reset.') | |
| 1491 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], | |
| 1492 check_return=True) | |
| 1493 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1) | |
| 1494 | |
| 1495 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 1496 def _EnableBatteryUpdates(self, timeout=None, retries=None): | |
| 1497 def battery_updates_enabled(): | |
| 1498 return self.GetCharging() is True | |
| 1499 | |
| 1500 self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True) | |
| 1501 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1) | |
| 1502 | |
| 1503 @contextlib.contextmanager | |
| 1504 def BatteryMeasurement(self, timeout=None, retries=None): | |
| 1505 """Context manager that enables battery data collection when in. | |
| 1506 Only for devices L and higher. | |
| 1507 | |
| 1508 Example usage: | |
| 1509 with BatteryMeasurement(): | |
| 1510 browser_actions() | |
| 1511 get_power_data() # report usage within this block | |
| 1512 after_measurements() # Anything that runs after power | |
| 1513 # measurements are collected | |
|
perezju
2015/03/17 12:52:19
nit: I think these comments could be improved a bi
rnephew (Wrong account)
2015/03/17 19:58:42
Done.
| |
| 1514 | |
| 1515 Args: | |
| 1516 timeout: timeout in seconds | |
| 1517 retries: number of retries | |
| 1518 """ | |
| 1519 if self.build_version_sdk < constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP: | |
| 1520 raise device_errors.CommandFailedError('Device must be L or higher.') | |
| 1521 try: | |
| 1522 self._DisableBatteryUpdates(timeout=timeout, retries=retries) | |
| 1523 yield | |
| 1524 finally: | |
| 1525 self._EnableBatteryUpdates(timeout=timeout, retries=retries) | |
| 1526 | |
| 1527 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 1469 def GetDevicePieWrapper(self, timeout=None, retries=None): | 1528 def GetDevicePieWrapper(self, timeout=None, retries=None): |
| 1470 """Gets the absolute path to the run_pie wrapper on the device. | 1529 """Gets the absolute path to the run_pie wrapper on the device. |
| 1471 | 1530 |
| 1472 We have to build our device executables to be PIE, but they need to be able | 1531 We have to build our device executables to be PIE, but they need to be able |
| 1473 to run on versions of android that don't support PIE (i.e. ICS and below). | 1532 to run on versions of android that don't support PIE (i.e. ICS and below). |
| 1474 To do so, we push a wrapper to the device that lets older android versions | 1533 To do so, we push a wrapper to the device that lets older android versions |
| 1475 run PIE executables. This method pushes that wrapper to the device if | 1534 run PIE executables. This method pushes that wrapper to the device if |
| 1476 necessary and returns the path to it. | 1535 necessary and returns the path to it. |
| 1477 | 1536 |
| 1478 This is exposed publicly to allow clients to write scripts using run_pie | 1537 This is exposed publicly to allow clients to write scripts using run_pie |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1519 """ | 1578 """ |
| 1520 if not devices: | 1579 if not devices: |
| 1521 devices = adb_wrapper.AdbWrapper.GetDevices() | 1580 devices = adb_wrapper.AdbWrapper.GetDevices() |
| 1522 if not devices: | 1581 if not devices: |
| 1523 raise device_errors.NoDevicesError() | 1582 raise device_errors.NoDevicesError() |
| 1524 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1583 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
| 1525 if async: | 1584 if async: |
| 1526 return parallelizer.Parallelizer(devices) | 1585 return parallelizer.Parallelizer(devices) |
| 1527 else: | 1586 else: |
| 1528 return parallelizer.SyncParallelizer(devices) | 1587 return parallelizer.SyncParallelizer(devices) |
| OLD | NEW |