Skip to content

Commit

Permalink
feat: let pass user for isAppInstalled (#739)
Browse files Browse the repository at this point in the history
* feat: let pass user for isAppInstalled

* giev for pm path
  • Loading branch information
KazuCocoa authored May 8, 2024
1 parent 210e8af commit 90d1fc6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,44 @@ const REMOTE_CACHE_ROOT = '/data/local/tmp/appium_cache';
const RESOLVER_ACTIVITY_NAME = 'android/com.android.internal.app.ResolverActivity';


/**
* @typedef {Object} IsAppInstalledOptions
* @property {string} [user] - The user id
*/

/**
* Check whether the particular package is present on the device under test.
*
* @this {import('../adb.js').ADB}
* @param {string} pkg - The name of the package to check.
* @param {IsAppInstalledOptions} [opts={}]
* @return {Promise<boolean>} True if the package is installed.
*/
apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg) {
apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg, opts = {}) {
const {
user
} = opts;

log.debug(`Getting install status for ${pkg}`);
let isInstalled;
if (await this.getApiLevel() < 26) {
try {
const stdout = await this.shell(['pm', 'path', pkg]);
const cmd = ['pm', 'path'];
if (util.hasValue(user)) {
cmd.push('--user', user);
}
cmd.push(pkg);
const stdout = await this.shell(cmd);
isInstalled = /^package:/m.test(stdout);
} catch (ign) {
isInstalled = false;
}
} else {
const stdout = await this.shell(['cmd', 'package', 'list', 'packages']);
const cmd = ['cmd', 'package', 'list', 'packages'];
if (util.hasValue(user)) {
cmd.push('--user', user);
}
const stdout = await this.shell(cmd);
isInstalled = new RegExp(`^package:${_.escapeRegExp(pkg)}$`, 'm').test(stdout);
}
log.debug(`'${pkg}' is${!isInstalled ? ' not' : ''} installed`);
Expand Down
37 changes: 37 additions & 0 deletions test/unit/apk-utils-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,43 @@ describe('Apk-utils', withMocks({adb, fs, teen_process}, function (mocks) {
.returns(`package:dummy.package1`);
(await adb.isAppInstalled(pkg)).should.be.false;
});
it('should parse correctly and return true for older versions with user', async function () {
const pkg = 'dummy.package';
mocks.adb.expects('getApiLevel')
.returns(25);
mocks.adb.expects('shell')
.once().withExactArgs(['pm', 'path', '--user', '1', pkg])
.returns(`package:/system/priv-app/TeleService/TeleService.apk with user`);
(await adb.isAppInstalled(pkg, {user: '1'})).should.be.true;
});
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', '--user', '1', pkg])
.throws();
(await adb.isAppInstalled(pkg, {user: '1'})).should.be.false;
});

it('should parse correctly and return true for newer versions with user', async function () {
const pkg = 'dummy.package';
mocks.adb.expects('getApiLevel')
.returns(26);
mocks.adb.expects('shell')
.once().withExactArgs(['cmd', 'package', 'list', 'packages', '--user', '1'])
.returns(`package:dummy.package\npackage:other.package\n`);
(await adb.isAppInstalled(pkg, {user: '1'})).should.be.true;
});
it('should parse correctly and return false for newer versions with user', async function () {
const pkg = 'dummy.package';
mocks.adb.expects('getApiLevel')
.returns(26);
mocks.adb.expects('shell')
.once().withExactArgs(['cmd', 'package', 'list', 'packages', '--user', '1'])
.returns(`package:dummy.package1`);
(await adb.isAppInstalled(pkg, {user: '1'})).should.be.false;
});
});

describe('getFocusedPackageAndActivity', function () {
Expand Down

0 comments on commit 90d1fc6

Please sign in to comment.