Skip to content

Commit

Permalink
feat: Add mobile helpers to handle services (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jun 1, 2020
1 parent 6906d1f commit 82d86e4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/commands/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ extensions.executeMobile = async function executeMobile (mobileCommand, opts = {
listSms: 'mobileListSms',

deleteFile: 'mobileDeleteFile',

startService: 'mobileStartService',
stopService: 'mobileStopService',
};

if (!_.has(mobileCommandsMapping, mobileCommand)) {
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import shellCmds from './shell';
import emuConsoleCmds from './emu-console';
import fileActionsCmds from './file-actions';
import appManagementCmds from './app-management';
import servicesCmds from './services';
import logCmds from './log';


Expand All @@ -33,6 +34,7 @@ Object.assign(
networkCmds,
coverageCmds,
recordscreenCmds,
servicesCmds,
screenStreamCmds,
performanceCmds,
executeCmds,
Expand Down
80 changes: 80 additions & 0 deletions lib/commands/services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import _ from 'lodash';

const commands = {};

function requireOptions (opts, requiredKeys = []) {
const missingKeys = _.difference(requiredKeys, _.keys(opts));
if (!_.isEmpty(missingKeys)) {
throw new Error(`The following options are required: ${missingKeys}`);
}
return opts;
}

/**
* @typedef {Object} StartServiceOptions
* @property {!string} intent - The name of the service intent to start, for example
* `com.some.package.name/.YourServiceSubClassName`. This option is mandatory.
* @property {string|number} user ['current'] - The user ID for which the service is started.
* The `current` user id is used by default
* @property {boolean} foreground [false] - Set it to `true` if your service must be
* started as foreground service.
*/

/**
* Starts the given service intent.
*
* @param {StartServiceOptions} opts
* @returns {string} The command output
* @throws {Error} If there was a failure while starting the service
* or required options are missing
*/
commands.mobileStartService = async function mobileStartService (opts = {}) {
const {
intent,
user,
foreground,
} = requireOptions(opts, ['intent']);
const cmd = [
'am', foreground ? 'start-foreground-service' : 'start-service',
];
if (user) {
cmd.push('--user', user);
}
cmd.push(intent);
return await this.adb.shell(cmd);
};

/**
* @typedef {Object} StopServiceOptions
* @property {!string} intent - The name of the service intent to stop, for example
* `com.some.package.name/.YourServiceSubClassName`. This option is mandatory.
* @property {string|number} user ['current'] - The user ID for which the service is running.
* The `current` user id is used by default
*/

/**
* Stops the given service intent.
*
* @param {StopServiceOptions} opts
* @returns {string} The command output
* @throws {Error} If there was a failure while stopping the service
* or required options are missing
*/
commands.mobileStopService = async function mobileStopService (opts = {}) {
const {
intent,
user,
} = requireOptions(opts, ['intent']);
const cmd = [
'am', 'stop-service',
];
if (user) {
cmd.push('--user', user);
}
cmd.push(intent);
return await this.adb.shell(cmd);
};


export { commands };
export default commands;

0 comments on commit 82d86e4

Please sign in to comment.