blob: c517377bc14e1b47c4040bb481964d7797a6b00e [file] [log] [blame]
"""Android device related methods which extend from UI Automator.
Android device class is defined in `mobly.controller`.
"""
from mobly import utils
from mobly.controllers import android_device
def is_apk_installed(
device: android_device.AndroidDevice, package_name: str
) -> bool:
"""Checks if given package is already installed on the Android device.
Args:
device: An AndroidDevice object.
package_name: The Android app package name.
Raises:
adb.AdbError: When executing adb command fails.
Returns:
True if package is installed. False otherwise.
"""
out = device.adb.shell(['pm', 'list', 'package'])
return bool(utils.grep('^package:%s$' % package_name, out))
def install_apk(device: android_device.AndroidDevice, apk_path: str) -> None:
"""Installs required apk snippet to the given Android device.
Args:
device: An AndroidDevice object.
apk_path: The absolute file path where the apk is located.
"""
device.adb.install(['-r', '-g', apk_path])
def uninstall_apk(
device: android_device.AndroidDevice, package_name: str
) -> None:
"""Uninstalls required apk snippet on given Android device.
Args:
device: An AndroidDevice object.
package_name: The Android app package name.
"""
device.adb.uninstall(package_name)
def is_apk_version_correct(
device: android_device.AndroidDevice, package_name: str, version: str
) -> bool:
"""Checks if the version of package is given version on the Android device.
Args:
device: An AndroidDevice object.
package_name: The Android app package name.
version: The specific version of app package.
Returns:
True if the version of package matches given version. False otherwise.
Raises:
adb.AdbError: When executing adb command fails.
"""
grep_version_name = utils.grep(
'versionName', device.adb.shell(['dumpsys', 'package', package_name])
)
return (
bool(grep_version_name) and grep_version_name[0].split('=')[1] == version
)