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 29 matching lines...) Expand all Loading... |
1458 command = self._cache['charging_config']['enable_command'] | 1466 command = self._cache['charging_config']['enable_command'] |
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 |
| 1476 # TODO(rnephew): Make private when all use cases can use the context manager. |
| 1477 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1478 def DisableBatteryUpdates(self, timeout=None, retries=None): |
| 1479 """ Resets battery data and makes device appear like it is not |
| 1480 charging so that it will collect power data since last charge. |
| 1481 |
| 1482 Args: |
| 1483 timeout: timeout in seconds |
| 1484 retries: number of retries |
| 1485 """ |
| 1486 def battery_updates_disabled(): |
| 1487 return self.GetCharging() is False |
| 1488 |
| 1489 self.RunShellCommand( |
| 1490 ['dumpsys', 'batterystats', '--reset'], check_return=True) |
| 1491 battery_data = self.RunShellCommand( |
| 1492 ['dumpsys', 'batterystats', '--charged', '--checkin'], |
| 1493 check_return=True) |
| 1494 ROW_TYPE_INDEX = 3 |
| 1495 PWI_POWER_INDEX = 5 |
| 1496 for line in battery_data: |
| 1497 l = line.split(',') |
| 1498 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi' |
| 1499 and l[PWI_POWER_INDEX] != 0): |
| 1500 raise device_errors.CommandFailedError( |
| 1501 'Non-zero pmi value found after reset.') |
| 1502 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], |
| 1503 check_return=True) |
| 1504 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1) |
| 1505 |
| 1506 # TODO(rnephew): Make private when all use cases can use the context manager. |
| 1507 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1508 def EnableBatteryUpdates(self, timeout=None, retries=None): |
| 1509 """ Restarts device charging so that dumpsys no longer collects power data. |
| 1510 |
| 1511 Args: |
| 1512 timeout: timeout in seconds |
| 1513 retries: number of retries |
| 1514 """ |
| 1515 def battery_updates_enabled(): |
| 1516 return self.GetCharging() is True |
| 1517 |
| 1518 self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True) |
| 1519 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1) |
| 1520 |
| 1521 @contextlib.contextmanager |
| 1522 def BatteryMeasurement(self, timeout=None, retries=None): |
| 1523 """Context manager that enables battery data collection. It makes |
| 1524 the device appear to stop charging so that dumpsys will start collecting |
| 1525 power data since last charge. Once the with block is exited, charging is |
| 1526 resumed and power data since last charge is no longer collected. |
| 1527 |
| 1528 Only for devices L and higher. |
| 1529 |
| 1530 Example usage: |
| 1531 with BatteryMeasurement(): |
| 1532 browser_actions() |
| 1533 get_power_data() # report usage within this block |
| 1534 after_measurements() # Anything that runs after power |
| 1535 # measurements are collected |
| 1536 |
| 1537 Args: |
| 1538 timeout: timeout in seconds |
| 1539 retries: number of retries |
| 1540 """ |
| 1541 if self.build_version_sdk < constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP: |
| 1542 raise device_errors.CommandFailedError('Device must be L or higher.') |
| 1543 try: |
| 1544 self.DisableBatteryUpdates(timeout=timeout, retries=retries) |
| 1545 yield |
| 1546 finally: |
| 1547 self.EnableBatteryUpdates(timeout=timeout, retries=retries) |
| 1548 |
1468 @decorators.WithTimeoutAndRetriesFromInstance() | 1549 @decorators.WithTimeoutAndRetriesFromInstance() |
1469 def GetDevicePieWrapper(self, timeout=None, retries=None): | 1550 def GetDevicePieWrapper(self, timeout=None, retries=None): |
1470 """Gets the absolute path to the run_pie wrapper on the device. | 1551 """Gets the absolute path to the run_pie wrapper on the device. |
1471 | 1552 |
1472 We have to build our device executables to be PIE, but they need to be able | 1553 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). | 1554 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 | 1555 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 | 1556 run PIE executables. This method pushes that wrapper to the device if |
1476 necessary and returns the path to it. | 1557 necessary and returns the path to it. |
1477 | 1558 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1519 """ | 1600 """ |
1520 if not devices: | 1601 if not devices: |
1521 devices = adb_wrapper.AdbWrapper.GetDevices() | 1602 devices = adb_wrapper.AdbWrapper.GetDevices() |
1522 if not devices: | 1603 if not devices: |
1523 raise device_errors.NoDevicesError() | 1604 raise device_errors.NoDevicesError() |
1524 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1605 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1525 if async: | 1606 if async: |
1526 return parallelizer.Parallelizer(devices) | 1607 return parallelizer.Parallelizer(devices) |
1527 else: | 1608 else: |
1528 return parallelizer.SyncParallelizer(devices) | 1609 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |