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 | |
| 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 _ResetBatteryData(self, timeout=None, retries=None): | |
| 1478 def reset_battery_data(): | |
| 1479 self.RunShellCommand(['dumpsys', 'batterystats', '--reset'], | |
| 1480 check_return=True) | |
| 1481 # Test that battery data was reset. | |
| 1482 battery_data = self.RunShellCommand( | |
| 1483 ['dumpsys', 'batterystats', '--charged', | |
| 1484 '--checkin'], check_return=True) | |
| 1485 for line in battery_data: | |
| 1486 l = line.split(',') | |
| 1487 if l[3] == 'pwi' and l[1] != 0: | |
| 1488 return False | |
| 1489 return True | |
| 1490 | |
| 1491 timeout_retry.WaitFor(reset_battery_data, wait_period=1) | |
| 1492 | |
| 1493 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 1494 def _DisableBatteryUpdates(self, timeout=None, retries=None): | |
|
perezju
2015/03/13 10:19:06
Thanks, this is looking a lot better. I'm still a
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
| |
| 1495 def disable_battery_updates(): | |
| 1496 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], | |
| 1497 check_return=True) | |
| 1498 return self.GetCharging() is False | |
| 1499 | |
| 1500 timeout_retry.WaitFor(disable_battery_updates, wait_period=1) | |
| 1501 | |
| 1502 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 1503 def _EnableBatteryUpdates(self, timeout=None, retries=None): | |
| 1504 def enable_battery_updates(): | |
| 1505 self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True) | |
|
perezju
2015/03/13 10:19:07
ditto as above
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
| |
| 1506 return self.GetCharging() is True | |
| 1507 | |
| 1508 timeout_retry.WaitFor(enable_battery_updates, wait_period=1) | |
| 1509 | |
| 1510 @contextlib.contextmanager | |
| 1511 def BatteryMeasurement(self, timeout=None, retries=None): | |
| 1512 """Context manager that enables battery data collection when in. | |
| 1513 Only for devices L and higher. | |
| 1514 | |
|
perezju
2015/03/13 10:19:06
nit: maybe the example would look a bit better wit
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
| |
| 1515 with BatteryMeasurement(): | |
| 1516 browser_actions() | |
| 1517 get_power_data() | |
| 1518 no_longer_collecting_data | |
| 1519 | |
| 1520 Args: | |
| 1521 timeout: timeout in seconds | |
| 1522 retries: number of retries | |
| 1523 """ | |
| 1524 if self.build_version_sdk < constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP: | |
|
perezju
2015/03/13 10:19:07
nit: extra space after '<'
rnephew (Wrong account)
2015/03/16 16:33:40
Done.
| |
| 1525 raise device_errors.CommandFailedError('Device must be L or higher.') | |
| 1526 try: | |
| 1527 self._ResetBatteryData(timeout=timeout, retries=retries) | |
| 1528 self._DisableBatteryUpdates(timeout=timeout, retries=retries) | |
| 1529 yield | |
| 1530 finally: | |
| 1531 self._EnableBatteryUpdates(timeout=timeout, retries=retries) | |
| 1532 | |
| 1533 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 1469 def GetDevicePieWrapper(self, timeout=None, retries=None): | 1534 def GetDevicePieWrapper(self, timeout=None, retries=None): |
| 1470 """Gets the absolute path to the run_pie wrapper on the device. | 1535 """Gets the absolute path to the run_pie wrapper on the device. |
| 1471 | 1536 |
| 1472 We have to build our device executables to be PIE, but they need to be able | 1537 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). | 1538 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 | 1539 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 | 1540 run PIE executables. This method pushes that wrapper to the device if |
| 1476 necessary and returns the path to it. | 1541 necessary and returns the path to it. |
| 1477 | 1542 |
| 1478 This is exposed publicly to allow clients to write scripts using run_pie | 1543 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 """ | 1584 """ |
| 1520 if not devices: | 1585 if not devices: |
| 1521 devices = adb_wrapper.AdbWrapper.GetDevices() | 1586 devices = adb_wrapper.AdbWrapper.GetDevices() |
| 1522 if not devices: | 1587 if not devices: |
| 1523 raise device_errors.NoDevicesError() | 1588 raise device_errors.NoDevicesError() |
| 1524 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1589 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
| 1525 if async: | 1590 if async: |
| 1526 return parallelizer.Parallelizer(devices) | 1591 return parallelizer.Parallelizer(devices) |
| 1527 else: | 1592 else: |
| 1528 return parallelizer.SyncParallelizer(devices) | 1593 return parallelizer.SyncParallelizer(devices) |
| OLD | NEW |