Skip to content

Commit

Permalink
Keep track of args used to start the app, for restart
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed Dec 6, 2017
1 parent fe2364b commit 9242bd8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
48 changes: 35 additions & 13 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,36 @@ commands.background = async function (seconds) {
let {appPackage, appActivity} = await this.adb.getFocusedPackageAndActivity();
await this.adb.goToHome();
await B.delay(seconds * 1000);
return this.adb.startApp({
pkg: this.opts.appPackage,
activity: this.opts.appActivity,
action: this.opts.intentAction,
category: this.opts.intentCategory,
flags: this.opts.intentFlags,
waitPkg: appPackage,
waitActivity: appActivity,
optionalIntentArguments: this.opts.optionalIntentArguments,
stopApp: false
});

let args;
if (this.opts.startActivityArgs && this.opts.startActivityArgs[`${appPackage}/${appActivity}`]) {
// the activity was started with `startActivity`, so use those args to restart
args = this.opts.startActivityArgs[`${appPackage}/${appActivity}`];
} else if (appPackage === this.opts.appPackage && appActivity === this.opts.appActivity) {
// the activity is the original session activity, so use the original args
args = {
pkg: appPackage,
activity: appActivity,
action: this.opts.intentAction,
category: this.opts.intentCategory,
flags: this.opts.intentFlags,
waitPkg: this.opts.appPackage,
waitActivity: this.opts.appActivity,
optionalIntentArguments: this.opts.optionalIntentArguments,
stopApp: false,
};
} else {
// the activity was started some other way, so use defaults
args = {
pkg: appPackage,
activity: appActivity,
waitPkg: appPackage,
waitActivity: appActivity,
stopApp: false
};
}
log.debug(`Bringing application back to foreground with arguments: ${JSON.stringify(args)}`);
return this.adb.startApp(args);
};

commands.getStrings = async function (language) {
Expand Down Expand Up @@ -180,7 +199,7 @@ commands.startActivity = async function (appPackage, appActivity,
dontStopAppOnReset = !!this.opts.dontStopAppOnReset;
}

await this.adb.startApp({
let args = {
pkg: appPackage,
activity: appActivity,
waitPkg: appWaitPackage || appPackage,
Expand All @@ -190,7 +209,10 @@ commands.startActivity = async function (appPackage, appActivity,
flags: intentFlags,
optionalIntentArguments,
stopApp: !dontStopAppOnReset
});
};
this.opts.startActivityArgs = this.opts.startActivityArgs || {};
this.opts.startActivityArgs[`${appPackage}/${appActivity}`] = args;
await this.adb.startApp(args);
};

commands.reset = async function () {
Expand Down
33 changes: 28 additions & 5 deletions test/unit/commands/general-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,40 @@ describe('General', () => {
await driver.installApp('non/existent/app.apk').should.be.rejectedWith(/Could not find/);
});
});
describe('background', () => {
it('should bring app to background', async () => {
describe('background', function () {
it('should bring app to background and back', async function () {
const appPackage = 'wpkg';
const appActivity = 'wacv';
driver.opts = {appPackage, appActivity, intentAction: 'act',
intentCategory: 'cat', intentFlags: 'flgs',
optionalIntentArguments: 'opt'};
let params = {pkg: appPackage, activity: appActivity, action: 'act', category: 'cat',
flags: 'flgs', waitPkg: 'wpkg', waitActivity: 'wacv',
optionalIntentArguments: 'opt', stopApp: false};
sandbox.stub(driver.adb, 'goToHome');
sandbox.stub(driver.adb, 'getFocusedPackageAndActivity')
.returns({appPackage, appActivity});
sandbox.stub(B, 'delay');
sandbox.stub(driver.adb, 'startApp');
await driver.background(10);
driver.adb.getFocusedPackageAndActivity.calledOnce.should.be.true;
driver.adb.goToHome.calledOnce.should.be.true;
B.delay.calledWithExactly(10000).should.be.true;
driver.adb.startApp.calledWithExactly(params).should.be.true;
});
it('should bring app to background and back if started after session init', async function () {
const appPackage = 'newpkg';
const appActivity = 'newacv';
driver.opts = {appPackage: 'pkg', appActivity: 'acv', intentAction: 'act',
intentCategory: 'cat', intentFlags: 'flgs',
optionalIntentArguments: 'opt'};
let params = {pkg: 'pkg', activity: 'acv', action: 'act', category: 'cat',
let params = {pkg: appPackage, activity: appActivity, action: 'act', category: 'cat',
flags: 'flgs', waitPkg: 'wpkg', waitActivity: 'wacv',
optionalIntentArguments: 'opt', stopApp: false};
driver.opts.startActivityArgs = {[`${appPackage}/${appActivity}`]: params};
sandbox.stub(driver.adb, 'goToHome');
sandbox.stub(driver.adb, 'getFocusedPackageAndActivity')
.returns({appPackage: 'wpkg', appActivity: 'wacv'});
.returns({appPackage, appActivity});
sandbox.stub(B, 'delay');
sandbox.stub(driver.adb, 'startApp');
await driver.background(10);
Expand All @@ -203,7 +226,7 @@ describe('General', () => {
B.delay.calledWithExactly(10000).should.be.true;
driver.adb.startApp.calledWithExactly(params).should.be.true;
});
it('should not bring app back if seconds are negative', async () => {
it('should not bring app back if seconds are negative', async function () {
sandbox.stub(driver.adb, 'goToHome');
sandbox.stub(driver.adb, 'startApp');
await driver.background(-1);
Expand Down

0 comments on commit 9242bd8

Please sign in to comment.