Skip to content

Commit f1493aa

Browse files
committed
fix: Cannot find Firefox package on device with work profile
1 parent ac17631 commit f1493aa

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

src/util/adb.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,31 @@ export default class ADBUtils {
8686
return devices.map((dev) => dev.id);
8787
}
8888

89+
async getCurrentUser(deviceId) {
90+
log.debug(`Retrieving current user on ${deviceId}`);
91+
92+
const currentUser = await this.runShellCommand(deviceId, [
93+
'am',
94+
'get-current-user',
95+
]);
96+
97+
const userId = parseInt(currentUser.trim());
98+
if (isNaN(userId)) {
99+
throw new WebExtError(`Unable to retrieve current user on ${deviceId}`);
100+
}
101+
return userId;
102+
}
103+
89104
async discoverInstalledFirefoxAPKs(deviceId, firefoxApk) {
90-
log.debug(`Listing installed Firefox APKs on ${deviceId}`);
105+
const userId = await this.getCurrentUser(deviceId);
91106

107+
log.debug(`Listing installed Firefox APKs on ${deviceId}`);
92108
const pmList = await this.runShellCommand(deviceId, [
93109
'pm',
94110
'list',
95111
'packages',
112+
'--user',
113+
`${userId}`,
96114
]);
97115

98116
return pmList

tests/unit/test-util/test.adb.js

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,49 @@ describe('utils/adb', () => {
223223
testFn: (adbUtils) => adbUtils.discoverInstalledFirefoxAPKs('device1'),
224224
});
225225

226+
sinon.assert.calledOnce(adb.fakeADBDevice.shell);
227+
});
228+
229+
it('retrieves current user', async () => {
230+
const adb = getFakeADBKit({
231+
adbkitUtil: {
232+
readAll: sinon.spy(() => {
233+
return Promise.resolve(Buffer.from('123\n'));
234+
}),
235+
},
236+
});
237+
238+
const adbUtils = new ADBUtils({ adb });
239+
const promise = adbUtils.getCurrentUser();
240+
226241
sinon.assert.calledOnce(adb.fakeADBDevice.shell);
227242
sinon.assert.calledWith(adb.fakeADBDevice.shell, [
228-
'pm',
229-
'list',
230-
'packages',
243+
'am',
244+
'get-current-user',
231245
]);
246+
const result = await assert.isFulfilled(promise);
247+
assert.equal(result, 123);
248+
});
249+
250+
it('rejects invalid get-current-user output', async () => {
251+
const adb = getFakeADBKit({
252+
adbkitUtil: {
253+
readAll: sinon.spy(() => {
254+
return Promise.resolve(Buffer.from('No user'));
255+
}),
256+
},
257+
});
258+
259+
const adbUtils = new ADBUtils({ adb });
260+
const promise = adbUtils.getCurrentUser();
261+
262+
sinon.assert.calledOnce(adb.fakeADBDevice.shell);
263+
sinon.assert.calledWith(adb.fakeADBDevice.shell, [
264+
'am',
265+
'get-current-user',
266+
]);
267+
await assert.isRejected(promise, WebExtError,
268+
/Unable to retrieve current user/);
232269
});
233270

234271
it('resolves the array of the installed firefox APKs', async () => {
@@ -242,11 +279,27 @@ describe('utils/adb', () => {
242279
}),
243280
},
244281
});
282+
283+
const stubGetCurrentUser = sinon.stub(
284+
ADBUtils.prototype,
285+
'getCurrentUser',
286+
);
287+
stubGetCurrentUser.resolves(0);
288+
245289
const adbUtils = new ADBUtils({ adb });
246290

247291
const promise = adbUtils.discoverInstalledFirefoxAPKs('device1');
248292
const packages = await assert.isFulfilled(promise);
293+
sinon.assert.calledOnce(stubGetCurrentUser);
294+
sinon.assert.calledWith(stubGetCurrentUser, 'device1');
249295
sinon.assert.calledOnce(adb.fakeADBDevice.shell);
296+
sinon.assert.calledWith(adb.fakeADBDevice.shell, [
297+
'pm',
298+
'list',
299+
'packages',
300+
'--user',
301+
'0',
302+
]);
250303
sinon.assert.calledOnce(adb.util.readAll);
251304
assert.deepEqual(packages, ['org.mozilla.fennec', 'org.mozilla.firefox']);
252305
});

0 commit comments

Comments
 (0)