diff options
author | perezju <perezju@chromium.org> | 2015-08-27 02:24:18 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-27 09:24:58 +0000 |
commit | 8f88a850e01a8cc6706ac39f4f5dfa87f6f376b3 (patch) | |
tree | eeb85c1fd9639ee26848359bf6d3bbbb01dad226 | |
parent | a4114ee96a42575ae7de8847fb79d867cd3ef11e (diff) | |
download | chromium_src-8f88a850e01a8cc6706ac39f4f5dfa87f6f376b3.zip chromium_src-8f88a850e01a8cc6706ac39f4f5dfa87f6f376b3.tar.gz chromium_src-8f88a850e01a8cc6706ac39f4f5dfa87f6f376b3.tar.bz2 |
[Android] Add DeviceUtils.GetApplicationVersion
Provide a method to get the version name of a package installed on a
device.
BUG=
Review URL: https://codereview.chromium.org/1315603002
Cr-Commit-Position: refs/heads/master@{#345827}
-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): |