Skip to content

Commit

Permalink
fix: Cannot find Firefox package on device with work profile (#2857) (#…
Browse files Browse the repository at this point in the history
…3146)

Co-authored-by: lm1 <mielicki@gmail.com>
  • Loading branch information
willdurand and lm1 authored May 27, 2024
1 parent ffcf07d commit e1566a2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
20 changes: 19 additions & 1 deletion src/util/adb.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,34 @@ export default class ADBUtils {
return devices.map((dev) => dev.id);
}

async getCurrentUser(deviceId: string): Promise<number> {
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: string,
firefoxApk?: string
): Promise<Array<string>> {
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
64 changes: 61 additions & 3 deletions tests/unit/test-util/test.adb.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,54 @@ describe('utils/adb', () => {
testFn: (adbUtils) => adbUtils.discoverInstalledFirefoxAPKs('device1'),
});

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

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

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

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 deviceId = '123';
const adb = getFakeADBKit({
adbkitUtil: {
readAll: sinon.spy(() => {
return Promise.resolve(Buffer.from('No user'));
}),
},
});

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

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 @@ -244,11 +286,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

0 comments on commit e1566a2

Please sign in to comment.