Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/util/adb.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,31 @@ export default class ADBUtils {
return devices.map((dev) => dev.id);
}

async getCurrentUser(deviceId) {
log.debug(`Retrieving current user on ${deviceId}`);

const currentUser = await this.runShellCommand(deviceId, [
'am',
'get-current-user',
]);

const userId = parseInt(currentUser.trim());
if (isNaN(userId)) {
throw new WebExtError(`Unable to retrieve current user on ${deviceId}`);
}
return userId;
}

async discoverInstalledFirefoxAPKs(deviceId, firefoxApk) {
log.debug(`Listing installed Firefox APKs on ${deviceId}`);
const userId = await this.getCurrentUser(deviceId);

log.debug(`Listing installed Firefox APKs on ${deviceId}`);
const pmList = await this.runShellCommand(deviceId, [
'pm',
'list',
'packages',
'--user',
`${userId}`,
]);

return pmList
Expand Down
62 changes: 59 additions & 3 deletions tests/unit/test-util/test.adb.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,52 @@ describe('utils/adb', () => {
testFn: (adbUtils) => adbUtils.discoverInstalledFirefoxAPKs('device1'),
});

sinon.assert.calledOnce(adb.fakeADBDevice.shell);
});

it('retrieves current user', async () => {
const adb = getFakeADBKit({
adbkitUtil: {
readAll: sinon.spy(() => {
return Promise.resolve(Buffer.from('123\n'));
}),
},
});

const adbUtils = new ADBUtils({ adb });
const promise = adbUtils.getCurrentUser();

sinon.assert.calledOnce(adb.fakeADBDevice.shell);
sinon.assert.calledWith(adb.fakeADBDevice.shell, [
'pm',
'list',
'packages',
'am',
'get-current-user',
]);
const result = await assert.isFulfilled(promise);
assert.equal(result, 123);
});

it('rejects invalid get-current-user output', async () => {
const adb = getFakeADBKit({
adbkitUtil: {
readAll: sinon.spy(() => {
return Promise.resolve(Buffer.from('No user'));
}),
},
});

const adbUtils = new ADBUtils({ adb });
const promise = adbUtils.getCurrentUser();

sinon.assert.calledOnce(adb.fakeADBDevice.shell);
sinon.assert.calledWith(adb.fakeADBDevice.shell, [
'am',
'get-current-user',
]);
await assert.isRejected(
promise,
WebExtError,
/Unable to retrieve current user/,
);
});

it('resolves the array of the installed firefox APKs', async () => {
Expand All @@ -242,11 +282,27 @@ describe('utils/adb', () => {
}),
},
});

const stubGetCurrentUser = sinon.stub(
ADBUtils.prototype,
'getCurrentUser',
);
stubGetCurrentUser.resolves(0);

const adbUtils = new ADBUtils({ adb });

const promise = adbUtils.discoverInstalledFirefoxAPKs('device1');
const packages = await assert.isFulfilled(promise);
sinon.assert.calledOnce(stubGetCurrentUser);
sinon.assert.calledWith(stubGetCurrentUser, 'device1');
sinon.assert.calledOnce(adb.fakeADBDevice.shell);
sinon.assert.calledWith(adb.fakeADBDevice.shell, [
'pm',
'list',
'packages',
'--user',
'0',
]);
sinon.assert.calledOnce(adb.util.readAll);
assert.deepEqual(packages, ['org.mozilla.fennec', 'org.mozilla.firefox']);
});
Expand Down