diff options
-rw-r--r-- | tools/telemetry/telemetry/core/platform/android_device.py | 49 | ||||
-rw-r--r-- | tools/telemetry/telemetry/core/platform/android_device_unittest.py | 176 |
2 files changed, 127 insertions, 98 deletions
diff --git a/tools/telemetry/telemetry/core/platform/android_device.py b/tools/telemetry/telemetry/core/platform/android_device.py index 2584b09..b573252 100644 --- a/tools/telemetry/telemetry/core/platform/android_device.py +++ b/tools/telemetry/telemetry/core/platform/android_device.py @@ -3,15 +3,14 @@ # found in the LICENSE file. import logging import os +import re +import subprocess +import sys from telemetry.core.platform import device from telemetry.core.platform.profiler import monsoon from telemetry.core import util - -util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android') -from pylib import constants # pylint: disable=import-error -from pylib.device import device_errors # pylint: disable=import-error -from pylib.device import device_utils # pylint: disable=import-error +from telemetry.internal.backends import adb_commands class AndroidDevice(device.Device): @@ -43,10 +42,9 @@ class AndroidDevice(device.Device): def enable_performance_mode(self): return self._enable_performance_mode -def GetDeviceSerials(): - device_serials = [d.adb.GetDeviceSerial() - for d in device_utils.DeviceUtils.HealthyDevices()] +def GetDeviceSerials(): + device_serials = adb_commands.GetAttachedDevices() # The monsoon provides power for the device, so for devices with no # real battery, we need to turn them on after the monsoon enables voltage # output to the device. @@ -67,9 +65,8 @@ The Monsoon's power output has been enabled. Please now ensure that: Waiting for device... """) - util.WaitFor(device_utils.DeviceUtils.HealthyDevices, 600) - device_serials = [d.adb.GetDeviceSerial() - for d in device_utils.DeviceUtils.HealthyDevices()] + util.WaitFor(adb_commands.GetAttachedDevices, 600) + device_serials = adb_commands.GetAttachedDevices() except IOError: return [] return device_serials @@ -101,15 +98,33 @@ def GetDevice(finder_options): def CanDiscoverDevices(): """Returns true if devices are discoverable via adb.""" - adb_path = constants.GetAdbPath() - if os.path.isabs(adb_path) and not os.path.exists(adb_path): + if not adb_commands.IsAndroidSupported(): + logging.info( + 'Android build commands unavailable on this machine. ' + 'Have you installed Android build dependencies?') return False try: - device_utils.DeviceUtils.HealthyDevices() + with open(os.devnull, 'w') as devnull: + adb_process = subprocess.Popen( + ['adb', 'devices'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, + stdin=devnull) + stdout = adb_process.communicate()[0] + if re.search(re.escape('????????????\tno permissions'), stdout) != None: + logging.warn('adb devices gave a permissions error. ' + 'Consider running adb as root:') + logging.warn(' adb kill-server') + logging.warn(' sudo `which adb` devices\n\n') return True - except (device_errors.CommandFailedError, device_errors.CommandTimeoutError, - OSError): - return False + except OSError: + pass + chromium_adb_path = os.path.join( + util.GetChromiumSrcDir(), 'third_party', 'android_tools', 'sdk', + 'platform-tools', 'adb') + if sys.platform.startswith('linux') and os.path.exists(chromium_adb_path): + os.environ['PATH'] = os.pathsep.join( + [os.path.dirname(chromium_adb_path), os.environ['PATH']]) + return True + return False def FindAllAvailableDevices(_): diff --git a/tools/telemetry/telemetry/core/platform/android_device_unittest.py b/tools/telemetry/telemetry/core/platform/android_device_unittest.py index d83ee3d..b8a61dd 100644 --- a/tools/telemetry/telemetry/core/platform/android_device_unittest.py +++ b/tools/telemetry/telemetry/core/platform/android_device_unittest.py @@ -5,121 +5,135 @@ import unittest from telemetry.core import browser_options -from telemetry.core import util from telemetry.core.platform import android_device +from telemetry.core.platform import android_platform_backend from telemetry.unittest_util import system_stub -util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android') -from pylib.device import device_utils # pylint: disable=import-error -util.AddDirToPythonPath(util.GetTelemetryDir(), 'third_party', 'mock') -import mock # pylint: disable=import-error - - -class _BaseAndroidDeviceTest(unittest.TestCase): +class AndroidDeviceTest(unittest.TestCase): def setUp(self): - self._healthy_device_patcher = mock.patch( - 'pylib.device.device_utils.DeviceUtils.HealthyDevices') - self._healthy_device_mock = self._healthy_device_patcher.start() self._android_device_stub = system_stub.Override( - android_device, ['subprocess', 'logging']) + android_device, ['adb_commands']) - def tearDown(self): - self._healthy_device_patcher.stop() - self._android_device_stub.Restore() - - -class AndroidDeviceTest(_BaseAndroidDeviceTest): def testGetAllAttachedAndroidDevices(self): - self._healthy_device_mock.return_value = [ - device_utils.DeviceUtils('01'), device_utils.DeviceUtils('02')] + self._android_device_stub.adb_commands.attached_devices = [ + '01', '02'] self.assertEquals( set(['01', '02']), set(device.device_id for device in - android_device.AndroidDevice.GetAllConnectedDevices())) + android_device.AndroidDevice.GetAllConnectedDevices() + )) + + def tearDown(self): + self._android_device_stub.Restore() + + +class GetDeviceTest(unittest.TestCase): + def setUp(self): + self._android_device_stub = system_stub.Override( + android_device, ['adb_commands', 'os', 'subprocess', 'logging']) + self._apb_stub = system_stub.Override( + android_platform_backend, ['adb_commands']) + + def tearDown(self): + self._android_device_stub.Restore() + self._apb_stub.Restore() def testNoAdbReturnsNone(self): finder_options = browser_options.BrowserFinderOptions() - with ( - mock.patch('os.path.isabs', return_value=True)), ( - mock.patch('os.path.exists', return_value=False)): - self.assertEquals([], self._android_device_stub.logging.warnings) - self.assertIsNone(android_device.GetDevice(finder_options)) + + def NoAdb(*_1, **_2): + raise OSError('not found') + self._android_device_stub.subprocess.Popen = NoAdb + + self.assertEquals([], self._android_device_stub.logging.warnings) + self.assertIsNone(android_device.GetDevice(finder_options)) def testAdbNoDevicesReturnsNone(self): finder_options = browser_options.BrowserFinderOptions() - with mock.patch('os.path.isabs', return_value=False): - self._healthy_device_mock.return_value = [] - self.assertEquals([], self._android_device_stub.logging.warnings) - self.assertIsNone(android_device.GetDevice(finder_options)) + self.assertEquals([], self._android_device_stub.logging.warnings) + self.assertIsNone(android_device.GetDevice(finder_options)) + + def testAdbPermissionsErrorReturnsNone(self): + finder_options = browser_options.BrowserFinderOptions() + self._android_device_stub.subprocess.Popen.communicate_result = ( + 'List of devices attached\n????????????\tno permissions\n', + '* daemon not running. starting it now on port 5037 *\n' + '* daemon started successfully *\n') + device = android_device.GetDevice(finder_options) + self.assertEquals([ + 'adb devices gave a permissions error. Consider running adb as root:', + ' adb kill-server', + ' sudo `which adb` devices\n\n'], + self._android_device_stub.logging.warnings) + self.assertIsNone(device) def testAdbTwoDevicesReturnsNone(self): finder_options = browser_options.BrowserFinderOptions() - with mock.patch('os.path.isabs', return_value=False): - self._healthy_device_mock.return_value = [ - device_utils.DeviceUtils('015d14fec128220c'), - device_utils.DeviceUtils('015d14fec128220d')] - device = android_device.GetDevice(finder_options) - self.assertEquals([ - 'Multiple devices attached. Please specify one of the following:\n' - ' --device=015d14fec128220c\n' - ' --device=015d14fec128220d'], - self._android_device_stub.logging.warnings) - self.assertIsNone(device) + self._android_device_stub.adb_commands.attached_devices = [ + '015d14fec128220c', '015d14fec128220d'] + device = android_device.GetDevice(finder_options) + self.assertEquals([ + 'Multiple devices attached. Please specify one of the following:\n' + ' --device=015d14fec128220c\n' + ' --device=015d14fec128220d'], + self._android_device_stub.logging.warnings) + self.assertIsNone(device) def testAdbPickOneDeviceReturnsDeviceInstance(self): finder_options = browser_options.BrowserFinderOptions() finder_options.device = '555d14fecddddddd' # pick one - with mock.patch('os.path.isabs', return_value=False): - self._healthy_device_mock.return_value = [ - device_utils.DeviceUtils('015d14fec128220c'), - device_utils.DeviceUtils('555d14fecddddddd')] - device = android_device.GetDevice(finder_options) - self.assertEquals([], self._android_device_stub.logging.warnings) - self.assertEquals('555d14fecddddddd', device.device_id) + self._android_device_stub.adb_commands.attached_devices = [ + '015d14fec128220c', '555d14fecddddddd'] + device = android_device.GetDevice(finder_options) + self.assertEquals([], self._android_device_stub.logging.warnings) + self.assertEquals('555d14fecddddddd', device.device_id) def testAdbOneDeviceReturnsDeviceInstance(self): finder_options = browser_options.BrowserFinderOptions() - with mock.patch('os.path.isabs', return_value=False): - self._healthy_device_mock.return_value = [ - device_utils.DeviceUtils('015d14fec128220c')] - device = android_device.GetDevice(finder_options) - self.assertEquals([], self._android_device_stub.logging.warnings) - self.assertEquals('015d14fec128220c', device.device_id) + self._android_device_stub.adb_commands.attached_devices = ( + ['015d14fec128220c']) + device = android_device.GetDevice(finder_options) + self.assertEquals([], self._android_device_stub.logging.warnings) + self.assertEquals('015d14fec128220c', device.device_id) -class FindAllAvailableDevicesTest(_BaseAndroidDeviceTest): +class FindAllAvailableDevicesTest(unittest.TestCase): + def setUp(self): + self._android_device_stub = system_stub.Override( + android_device, ['adb_commands', 'os', 'subprocess', 'logging']) + self._apb_stub = system_stub.Override( + android_platform_backend, ['adb_commands']) + + def tearDown(self): + self._android_device_stub.Restore() + self._apb_stub.Restore() + def testAdbNoDeviceReturnsEmptyList(self): finder_options = browser_options.BrowserFinderOptions() - with mock.patch('os.path.isabs', return_value=False): - self._healthy_device_mock.return_value = [] - devices = android_device.FindAllAvailableDevices(finder_options) - self.assertEquals([], self._android_device_stub.logging.warnings) - self.assertIsNotNone(devices) - self.assertEquals(len(devices), 0) + devices = android_device.FindAllAvailableDevices(finder_options) + self.assertEquals([], self._android_device_stub.logging.warnings) + self.assertIsNotNone(devices) + self.assertEquals(len(devices), 0) def testAdbOneDeviceReturnsListWithOneDeviceInstance(self): finder_options = browser_options.BrowserFinderOptions() - with mock.patch('os.path.isabs', return_value=False): - self._healthy_device_mock.return_value = [ - device_utils.DeviceUtils('015d14fec128220c')] - devices = android_device.FindAllAvailableDevices(finder_options) - self.assertEquals([], self._android_device_stub.logging.warnings) - self.assertIsNotNone(devices) - self.assertEquals(len(devices), 1) - self.assertEquals('015d14fec128220c', devices[0].device_id) + self._android_device_stub.adb_commands.attached_devices = ( + ['015d14fec128220c']) + devices = android_device.FindAllAvailableDevices(finder_options) + self.assertEquals([], self._android_device_stub.logging.warnings) + self.assertIsNotNone(devices) + self.assertEquals(len(devices), 1) + self.assertEquals('015d14fec128220c', devices[0].device_id) def testAdbMultipleDevicesReturnsListWithAllDeviceInstances(self): finder_options = browser_options.BrowserFinderOptions() - with mock.patch('os.path.isabs', return_value=False): - self._healthy_device_mock.return_value = [ - device_utils.DeviceUtils('015d14fec128220c'), - device_utils.DeviceUtils('015d14fec128220d'), - device_utils.DeviceUtils('015d14fec128220e')] - devices = android_device.FindAllAvailableDevices(finder_options) - self.assertEquals([], self._android_device_stub.logging.warnings) - self.assertIsNotNone(devices) - self.assertEquals(len(devices), 3) - self.assertEquals(devices[0].guid, '015d14fec128220c') - self.assertEquals(devices[1].guid, '015d14fec128220d') - self.assertEquals(devices[2].guid, '015d14fec128220e') + self._android_device_stub.adb_commands.attached_devices = [ + '015d14fec128220c', '015d14fec128220d', '015d14fec128220e'] + devices = android_device.FindAllAvailableDevices(finder_options) + self.assertEquals([], self._android_device_stub.logging.warnings) + self.assertIsNotNone(devices) + self.assertEquals(len(devices), 3) + self.assertEquals(devices[0].guid, '015d14fec128220c') + self.assertEquals(devices[1].guid, '015d14fec128220d') + self.assertEquals(devices[2].guid, '015d14fec128220e') |