diff options
-rw-r--r-- | build/android/pylib/device/device_utils.py | 22 | ||||
-rwxr-xr-x | build/android/pylib/device/device_utils_test.py | 30 |
2 files changed, 52 insertions, 0 deletions
diff --git a/build/android/pylib/device/device_utils.py b/build/android/pylib/device/device_utils.py index 0ba0570..aab8ec3 100644 --- a/build/android/pylib/device/device_utils.py +++ b/build/android/pylib/device/device_utils.py @@ -386,6 +386,28 @@ class DeviceUtils(object): return apks @decorators.WithTimeoutAndRetriesFromInstance() + def GetApplicationVersion(self, package, timeout=None, retries=None): + """Get the version name of a package installed on the device. + + Args: + package: Name of the package. + + Returns: + A string with the version name or None if the package is not found + on the device. + """ + output = self.RunShellCommand( + ['dumpsys', 'package', package], check_return=True) + if not output: + return None + for line in output: + line = line.strip() + if line.startswith('versionName='): + return line[len('versionName='):] + raise device_errors.CommandFailedError( + 'Version name for %s not found on dumpsys output' % package, str(self)) + + @decorators.WithTimeoutAndRetriesFromInstance() def GetApplicationDataDirectory(self, package, timeout=None, retries=None): """Get the data directory on the device for the given package. diff --git a/build/android/pylib/device/device_utils_test.py b/build/android/pylib/device/device_utils_test.py index 47ae9be..4fcce84 100755 --- a/build/android/pylib/device/device_utils_test.py +++ b/build/android/pylib/device/device_utils_test.py @@ -326,6 +326,36 @@ class DeviceUtils_GetApplicationPathsInternalTest(DeviceUtilsTest): self.device._GetApplicationPathsInternal('android') +class DeviceUtils_GetApplicationVersionTest(DeviceUtilsTest): + + def test_GetApplicationVersion_exists(self): + with self.assertCalls( + (self.call.adb.Shell('dumpsys package com.android.chrome'), + 'Packages:\n' + ' Package [com.android.chrome] (3901ecfb):\n' + ' userId=1234 gids=[123, 456, 789]\n' + ' pkg=Package{1fecf634 com.android.chrome}\n' + ' versionName=45.0.1234.7\n')): + self.assertEquals('45.0.1234.7', + self.device.GetApplicationVersion('com.android.chrome')) + + def test_GetApplicationVersion_notExists(self): + with self.assertCalls( + (self.call.adb.Shell('dumpsys package com.android.chrome'), '')): + self.assertEquals(None, + self.device.GetApplicationVersion('com.android.chrome')) + + def test_GetApplicationVersion_fails(self): + with self.assertCalls( + (self.call.adb.Shell('dumpsys package com.android.chrome'), + 'Packages:\n' + ' Package [com.android.chrome] (3901ecfb):\n' + ' userId=1234 gids=[123, 456, 789]\n' + ' pkg=Package{1fecf634 com.android.chrome}\n')): + with self.assertRaises(device_errors.CommandFailedError): + self.device.GetApplicationVersion('com.android.chrome') + + class DeviceUtilsGetApplicationDataDirectoryTest(DeviceUtilsTest): def testGetApplicationDataDirectory_exists(self): |