Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring startApp to add callback as a parameter and removing redundant varaibles #14

Merged
merged 1 commit into from
Jun 18, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 24 additions & 32 deletions lib/adb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,46 +1060,38 @@ ADB.prototype.killProcessByPID = function (pid, cb) {
this.shell("kill " + pid, cb);
};

ADB.prototype.startApp = function (startAppOptions) {
var pkg = startAppOptions.pkg;
var activity = startAppOptions.activity;
var action = startAppOptions.action;
var category = startAppOptions.category;
var flags = startAppOptions.flags;
// preventing null wait package
var waitPkg = startAppOptions.waitPkg || pkg;
var waitActivity = startAppOptions.waitActivity || false;
var optionalIntentArguments = startAppOptions.optionalIntentArguments ? " " + startAppOptions.optionalIntentArguments : "";
var retry = startAppOptions.retry;
var stopApp = startAppOptions.stopApp;
var cb = startAppOptions.cb;

if (typeof retry === "undefined") {
retry = true;
}
if (typeof stopApp === "undefined") {
stopApp = true;
}

var stop = stopApp ? "-S" : "";
ADB.prototype.startApp = function (startAppOptions, cb) {
startAppOptions = _.clone(startAppOptions);
// initializing defaults
_.defaults(startAppOptions, {
waitPkg: startAppOptions.pkg,
waitActivity: false,
optionalIntentArguments: false,
retry: true,
stopApp: true
});
// preventing null waitpkg
startAppOptions.waitPkg = startAppOptions.waitPkg || startAppOptions.pkg;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this handled by the default case above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you might want to clone before defaulting to avoid side effects.

startAppOptions = _.clone(startAppOptions);
_default(...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlipps no defaults only handles undefined. Earlier code had a comment for null so thought to handle it explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have cloned startAppOptions but still have null check in place , @jlipps any ideas if we still need the null check, since defaults will only handle undefined, it was explicitly commented about the null check.

startAppOptions.optionalIntentArguments = startAppOptions.optionalIntentArguments ? " " + startAppOptions.optionalIntentArguments : "";
var stop = startAppOptions.stopApp ? "-S" : "";
var cmd = "am start " + stop +
" -a " + action +
" -c " + category +
" -f " + flags +
" -n " + pkg + "/" + activity + optionalIntentArguments;
" -a " + startAppOptions.action +
" -c " + startAppOptions.category +
" -f " + startAppOptions.flags +
" -n " + startAppOptions.pkg + "/" + startAppOptions.activity + startAppOptions.optionalIntentArguments;
this.shell(cmd, function (err, stdout) {
if (err) return cb(err);
if (stdout.indexOf("Error: Activity class") !== -1 &&
stdout.indexOf("does not exist") !== -1) {
if (!activity) {
if (!startAppOptions.activity) {
return cb(new Error("Parameter 'appActivity' is required for launching application"));
}
if (retry && activity[0] !== ".") {
if (startAppOptions.retry && startAppOptions.activity[0] !== ".") {
logger.info("We tried to start an activity that doesn't exist, " +
"retrying with . prepended to activity");
startAppOptions.activity = "." + activity;
startAppOptions.activity = "." + startAppOptions.activity;
startAppOptions.retry = false;
return this.startApp(startAppOptions);
return this.startApp(startAppOptions, cb);
} else {
var msg = "Activity used to start app doesn't exist or cannot be " +
"launched! Make sure it exists and is a launchable activity";
Expand All @@ -1108,8 +1100,8 @@ ADB.prototype.startApp = function (startAppOptions) {
}
}

if (waitActivity) {
this.waitForActivity(waitPkg, waitActivity, cb);
if (startAppOptions.waitActivity) {
this.waitForActivity(startAppOptions.waitPkg, startAppOptions.waitActivity, cb);
} else {
cb();
}
Expand Down