Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not check if app is installed if we know it is #727

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ const RESOLVER_ACTIVITY_NAME = 'android/com.android.internal.app.ResolverActivit
apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg) {
log.debug(`Getting install status for ${pkg}`);
let isInstalled;
try {
const stdout = await this.shell(['pm', 'path', pkg]);
isInstalled = /^package:/m.test(stdout);
} catch (ign) {
isInstalled = false;
if (await this.getApiLevel() < 26) {
try {
const stdout = await this.shell(['pm', 'path', pkg]);
isInstalled = /^package:/m.test(stdout);
} catch (ign) {
isInstalled = false;
}
} else {
const stdout = await this.shell(['cmd', 'package', 'list', 'packages']);
isInstalled = new RegExp(`^package:${_.escapeRegExp(pkg)}$`, 'm').test(stdout);
}
log.debug(`'${pkg}' is${!isInstalled ? ' not' : ''} installed`);
return isInstalled;
Expand Down Expand Up @@ -390,6 +395,8 @@ apkUtilsMethods.waitForNotActivity = async function waitForNotActivity (pkg, act
* app is uninstalled.
* @property {boolean} [keepData] - Set to true in order to keep the
* application data and cache folders after uninstall.
* @property {boolean} [skipInstallCheck] Whether to check if the app is installed prior to
* uninstalling it. By default this is checked.
*/

/**
Expand All @@ -403,7 +410,7 @@ apkUtilsMethods.waitForNotActivity = async function waitForNotActivity (pkg, act
*/
apkUtilsMethods.uninstallApk = async function uninstallApk (pkg, options = {}) {
log.debug(`Uninstalling ${pkg}`);
if (!await this.isAppInstalled(pkg)) {
if (!options.skipInstallCheck && !await this.isAppInstalled(pkg)) {
log.info(`${pkg} was not uninstalled, because it was not present on the device`);
return false;
}
Expand Down Expand Up @@ -796,7 +803,7 @@ apkUtilsMethods.installOrUpgrade = async function installOrUpgrade (appPath, pkg
const appState = await this.getApplicationInstallState(appPath, pkg);
let wasUninstalled = false;
const uninstallPackage = async () => {
if (!await this.uninstallApk(/** @type {string} */ (pkg))) {
if (!await this.uninstallApk(/** @type {string} */ (pkg), {skipInstallCheck: true})) {
throw new Error(`'${pkg}' package cannot be uninstalled`);
}
wasUninstalled = true;
Expand Down
34 changes: 28 additions & 6 deletions test/unit/apk-utils-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,42 @@ describe('Apk-utils', withMocks({adb, fs, teen_process}, function (mocks) {
});

describe('isAppInstalled', function () {
it('should parse correctly and return true', async function () {
it('should parse correctly and return true for older versions', async function () {
const pkg = 'dummy.package';
mocks.adb.expects('getApiLevel')
.returns(25);
mocks.adb.expects('shell')
.twice().withExactArgs(['pm', 'path', pkg])
.once().withExactArgs(['pm', 'path', pkg])
.returns(`package:/system/priv-app/TeleService/TeleService.apk`);
(await adb.isAppInstalled(pkg)).should.be.true;
});
it('should parse correctly and return false', async function () {
it('should parse correctly and return false for older versions', async function () {
const pkg = 'dummy.package';
mocks.adb.expects('getApiLevel')
.returns(25);
mocks.adb.expects('shell')
.once().withExactArgs(['pm', 'path', pkg])
.throws();
(await adb.isAppInstalled(pkg)).should.be.false;
});
it('should parse correctly and return true for newer versions', async function () {
const pkg = 'dummy.package';
mocks.adb.expects('getApiLevel')
.returns(26);
mocks.adb.expects('shell')
.once().withExactArgs(['cmd', 'package', 'list', 'packages'])
.returns(`package:dummy.package\npackage:other.package\n`);
(await adb.isAppInstalled(pkg)).should.be.true;
});
it('should parse correctly and return false for newer versions', async function () {
const pkg = 'dummy.package';
mocks.adb.expects('getApiLevel')
.returns(26);
mocks.adb.expects('shell')
.once().withExactArgs(['cmd', 'package', 'list', 'packages'])
.returns(`package:dummy.package1`);
(await adb.isAppInstalled(pkg)).should.be.false;
});
});

describe('getFocusedPackageAndActivity', function () {
Expand Down Expand Up @@ -824,7 +846,7 @@ describe('Apk-utils', withMocks({adb, fs, teen_process}, function (mocks) {
isInstalled: true,
});
mocks.adb.expects('install').withArgs(apkPath, {replace: true}).once().throws();
mocks.adb.expects('uninstallApk').withExactArgs(pkgId).once().returns(true);
mocks.adb.expects('uninstallApk').withArgs(pkgId).once().returns(true);
mocks.adb.expects('install').withArgs(apkPath, {replace: false}).once().returns(true);
await adb.installOrUpgrade(apkPath);
});
Expand All @@ -838,7 +860,7 @@ describe('Apk-utils', withMocks({adb, fs, teen_process}, function (mocks) {
versionCode: 1,
isInstalled: true,
});
mocks.adb.expects('uninstallApk').withExactArgs(pkgId).once().returns(true);
mocks.adb.expects('uninstallApk').withArgs(pkgId).once().returns(true);
mocks.adb.expects('install').withArgs(apkPath).twice().throws();
await adb.installOrUpgrade(apkPath).should.be.rejected;
});
Expand All @@ -852,7 +874,7 @@ describe('Apk-utils', withMocks({adb, fs, teen_process}, function (mocks) {
versionCode: 1,
isInstalled: true,
});
mocks.adb.expects('uninstallApk').withExactArgs(pkgId).once().returns(false);
mocks.adb.expects('uninstallApk').withArgs(pkgId).once().returns(false);
mocks.adb.expects('install').withArgs(apkPath).once().throws();
await adb.installOrUpgrade(apkPath).should.be.rejected;
});
Expand Down