Skip to content

Commit

Permalink
add startUri method so people can navigate directly into a deep view …
Browse files Browse the repository at this point in the history
…with a uri
  • Loading branch information
jlipps committed Dec 17, 2015
1 parent 1bb7e1c commit 4497cff
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ console.log(await adb.getPIDsByName('m.android.phone'));
- checkApkKeystoreMatch
- isAppInstalled
- startApp
- startUri
- getFocusedPackageAndActivity
- waitForActivityOrNot
- waitForActivity
Expand Down
13 changes: 13 additions & 0 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ apkUtilsMethods.isAppInstalled = async function (pkg) {
}
};

apkUtilsMethods.startUri = async function (uri, pkg) {
if (!uri || !pkg) {
log.errorAndThrow("URI and package arguments are required");
}
try {
let args = ["am", "start", "-W", "-a", "android.intent.action.VIEW", "-d",
uri, pkg];
await this.shell(args);
} catch (e) {
log.errorAndThrow(`Error attempting to start URI. Original error: ${e}`);
}
};

apkUtilsMethods.startApp = async function (startAppOptions = {}) {
try {
if (!startAppOptions.activity || !startAppOptions.pkg) {
Expand Down
20 changes: 20 additions & 0 deletions test/functional/apk-utils-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import chaiAsPromised from 'chai-as-promised';
import ADB from '../..';
import path from 'path';
import { rootDir } from '../../lib/helpers.js';
import { retryInterval } from 'asyncbox';

chai.should();
chai.use(chaiAsPromised);

describe('apk utils', function () {
Expand Down Expand Up @@ -31,6 +33,24 @@ describe('apk utils', function () {
await adb.push(contactManagerPath, deviceTempPath);
await adb.installFromDevicePath(deviceTempPath + 'ContactManager.apk');
});
describe('startUri', async () => {
it('should be able to start a uri', async () => {
await adb.goToHome();
let res = await adb.getFocusedPackageAndActivity();
res.appPackage.should.not.equal('com.android.contacts');
await adb.install(contactManagerPath);
await adb.startUri('content://contacts/people', 'com.android.contacts');
await retryInterval(10, 500, async () => {
res = await adb.shell(['dumpsys', 'window', 'windows']);
// depending on apilevel, app might show up as active in one of these
// two dumpsys output formats
let focusRe1 = '(mCurrentFocus.+\\.PeopleActivity)';
let focusRe2 = '(mFocusedApp.+\\.PeopleActivity)';
res.should.match(new RegExp(`${focusRe1}|${focusRe2}`));
});
await adb.goToHome();
});
});
describe('startApp', async () => {
it('should be able to start', async () => {
await adb.install(contactManagerPath);
Expand Down
14 changes: 14 additions & 0 deletions test/unit/apk-utils-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { withMocks } from 'appium-test-support';
chai.use(chaiAsPromised);
const should = chai.should(),
pkg = 'com.example.android.contactmanager',
uri = 'content://contacts/people/1',
act = '.ContactManager',
startAppOptions = {stopApp: true, action: 'action', category: 'cat',
flags: 'flags', pkg: 'pkg', activity: 'act',
Expand Down Expand Up @@ -151,6 +152,19 @@ describe('Apk-utils', () => {
mocks.adb.verify();
});
}));
describe('startUri', withMocks({adb}, (mocks) => {
it('should fail if uri or pkg are not provided', async () => {
await adb.startUri().should.eventually.be.rejectedWith(/arguments are required/);
await adb.startUri('foo').should.eventually.be.rejectedWith(/arguments are required/);
});
it('should build a call to a VIEW intent with the uri', async () => {
mocks.adb.expects('shell')
.once().withExactArgs(['am', 'start', '-W', '-a',
'android.intent.action.VIEW', '-d', uri, pkg]);
await adb.startUri(uri, pkg);
mocks.adb.verify();
});
}));
describe('startApp', withMocks({adb}, (mocks) => {
it('should call getApiLevel and shell with correct arguments', async () => {
mocks.adb.expects('getApiLevel')
Expand Down

0 comments on commit 4497cff

Please sign in to comment.