Skip to content

Commit

Permalink
fix: Make package of startUri optional
Browse files Browse the repository at this point in the history
  • Loading branch information
fur0ut0 committed Dec 9, 2024
1 parent b58613b commit 4a8a006
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 8 additions & 6 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,27 @@ apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg, opts = {})
*
* @this {import('../adb.js').ADB}
* @param {string} uri - The name of URI to start.
* @param {string} pkg - The name of the package to start the URI with.
* @param {string?} [pkg=null] - The name of the package to start the URI with.
* @param {StartUriOptions} [opts={}]
*/
apkUtilsMethods.startUri = async function startUri (uri, pkg, opts = {}) {
apkUtilsMethods.startUri = async function startUri (uri, pkg = null, opts = {}) {
const {
waitForLaunch = true,
} = opts;

if (!uri || !pkg) {
throw new Error('URI and package arguments are required');
if (!uri) {
throw new Error('URI argument is required');
}

const args = ['am', 'start'];
if (waitForLaunch) {
args.push('-W');
}
args.push('-a', 'android.intent.action.VIEW',
'-d', escapeShellArg(uri),
pkg);
'-d', escapeShellArg(uri));
if (pkg) {
args.push(pkg);
}

try {
const res = await this.shell(args);
Expand Down
15 changes: 12 additions & 3 deletions test/unit/apk-utils-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,8 @@ describe('Apk-utils', withMocks({adb, fs, teen_process}, function (mocks) {
});
});
describe('startUri', function () {
it('should fail if uri or pkg are not provided', async function () {
await adb.startUri().should.eventually.be.rejectedWith(/arguments are required/);
await adb.startUri('foo').should.eventually.be.rejectedWith(/arguments are required/);
it('should fail if uri is not provided', async function () {
await adb.startUri().should.eventually.be.rejectedWith(/argument is required/);
});
it('should fail if "unable to resolve intent" appears in shell command result', async function () {
mocks.adb.expects('shell')
Expand All @@ -532,6 +531,16 @@ describe('Apk-utils', withMocks({adb, fs, teen_process}, function (mocks) {
await adb.startUri(uri, pkg).should.eventually.be.rejectedWith(/Unable to resolve intent/);
});
it('should build a call to a VIEW intent with the uri', async function () {
mocks.adb.expects('shell')
.once().withExactArgs([
'am', 'start', '-W', '-a',
'android.intent.action.VIEW', '-d', uri
])
.returns('Passable result');

await adb.startUri(uri);
});
it('should build a call to a VIEW intent with the uri and package', async function () {
mocks.adb.expects('shell')
.once().withExactArgs([
'am', 'start', '-W', '-a',
Expand Down

0 comments on commit 4a8a006

Please sign in to comment.