Skip to content

Commit

Permalink
Merge d3475db into 63f9e37
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Mokhnach committed Oct 2, 2018
2 parents 63f9e37 + d3475db commit c1da392
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 85 deletions.
74 changes: 25 additions & 49 deletions lib/android-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import logger from './logger';
import { fs } from 'appium-support';
import { path as unicodeIMEPath } from 'appium-android-ime';
import { path as settingsApkPath } from 'io.appium.settings';
import { path as unlockApkPath } from 'appium-unlock';
import Bootstrap from './bootstrap';
import B from 'bluebird';

Expand Down Expand Up @@ -47,9 +46,8 @@ const CHROME_BROWSER_PACKAGE_ACTIVITY = {
},
};
const SETTINGS_HELPER_PKG_ID = 'io.appium.settings';
const SETTINGS_HELPER_PKG_ACTIVITY = ".Settings";
const UNLOCK_HELPER_PKG_ID = 'io.appium.unlock';
const UNLOCK_HELPER_PKG_ACTIVITY = ".Unlock";
const SETTINGS_HELPER_MAIN_ACTIVITY = '.Settings';
const SETTINGS_HELPER_UNLOCK_ACTIVITY = '.Unlock';
const UNICODE_IME_PKG_ID = 'io.appium.android.ime';

let helpers = {};
Expand Down Expand Up @@ -108,8 +106,14 @@ helpers.getJavaVersion = async function (logVersion = true) {
};

helpers.prepareEmulator = async function (adb, opts) {
let {avd, avdArgs, language, locale, avdLaunchTimeout,
avdReadyTimeout} = opts;
let {
avd,
avdArgs,
language,
locale,
avdLaunchTimeout,
avdReadyTimeout,
} = opts;
if (!avd) {
throw new Error("Cannot launch AVD without AVD name");
}
Expand Down Expand Up @@ -185,8 +189,7 @@ helpers.getDeviceInfoFromCaps = async function (opts = {}) {
// udid was given, lets try to init with that device
if (udid) {
if (!_.includes(_.map(devices, 'udid'), udid)) {
logger.errorAndThrow(`Device ${udid} was not in the list ` +
`of connected devices`);
logger.errorAndThrow(`Device ${udid} was not in the list of connected devices`);
}
emPort = adb.getPortFromEmulatorString(udid);
} else if (opts.platformVersion) {
Expand Down Expand Up @@ -219,8 +222,7 @@ helpers.getDeviceInfoFromCaps = async function (opts = {}) {
// we couldn't find anything! quit
if (!udid) {
logger.errorAndThrow(`Unable to find an active device or emulator ` +
`with OS ${opts.platformVersion}. The following ` +
`are available: ` + availDevicesStr.join(', '));
`with OS ${opts.platformVersion}. The following are available: ` + availDevicesStr.join(', '));
}

emPort = adb.getPortFromEmulatorString(udid);
Expand Down Expand Up @@ -464,7 +466,7 @@ helpers.pushSettingsApp = async function (adb, throwError = false) {
// there is no need to continue if the application is still running
if (await adb.processExists(SETTINGS_HELPER_PKG_ID)) {
logger.debug(`${SETTINGS_HELPER_PKG_ID} is already running. ` +
`There is no need to reset its permissions.`);
`There is no need to reset its permissions.`);
return;
}

Expand All @@ -474,7 +476,7 @@ helpers.pushSettingsApp = async function (adb, throwError = false) {
try {
await adb.startApp({
pkg: SETTINGS_HELPER_PKG_ID,
activity: SETTINGS_HELPER_PKG_ACTIVITY,
activity: SETTINGS_HELPER_MAIN_ACTIVITY,
action: "android.intent.action.MAIN",
category: "android.intent.category.LAUNCHER",
flags: "0x10200000",
Expand All @@ -488,12 +490,6 @@ helpers.pushSettingsApp = async function (adb, throwError = false) {
}
};

helpers.pushUnlock = async function (adb) {
logger.debug("Pushing unlock helper app to device...");

await helpers.installHelperApp(adb, unlockApkPath, UNLOCK_HELPER_PKG_ID, 'Unlock');
};

/**
* Extracts string.xml and converts it to string.json and pushes
* it to /data/local/tmp/string.json on for use of bootstrap
Expand Down Expand Up @@ -564,25 +560,6 @@ helpers.unlockWithUIAutomation = async function (driver, adb, unlockCapabilities
helpers.unlockWithHelperApp = async function (adb) {
logger.info("Unlocking screen");

try {
await adb.forceStop(UNLOCK_HELPER_PKG_ID);
} catch (e) {
// Sometimes we can see the below error, but we can ignore it.
// [W3C] Encountered internal error running command: Error: Error executing adbExec. Original error: 'Command 'adb -P 5037 -s emulator-5554 shell am force-stop io.appium.unlock' timed out after 20000ms'; Stderr: ''; Code: 'null'
logger.warn(`An error in unlockWithHelperApp: ${e.message}`);
}

let startOpts = {
pkg: UNLOCK_HELPER_PKG_ID,
activity: UNLOCK_HELPER_PKG_ACTIVITY,
action: "android.intent.action.MAIN",
category: "android.intent.category.LAUNCHER",
flags: "0x10200000",
stopApp: false,
retry: false,
waitDuration: 1000
};

// Unlock succeed with a couple of retries.
let firstRun = true;
await retry(3, async function () {
Expand All @@ -601,10 +578,15 @@ helpers.unlockWithHelperApp = async function (adb) {
}
}

logger.info(`Launching ${UNLOCK_HELPER_PKG_ID}`);

// The command takes too much time so we should not call the command over twice continuously.
await adb.startApp(startOpts);
logger.info(`Launching ${SETTINGS_HELPER_UNLOCK_ACTIVITY}`);
await adb.shell([
'am', 'start',
'-n', `${SETTINGS_HELPER_PKG_ID}/${SETTINGS_HELPER_UNLOCK_ACTIVITY}`,
'-c', 'android.intent.category.LAUNCHER',
'-a', 'android.intent.action.MAIN',
'-f', '0x10200000',
]);
await B.delay(1000);
});
};

Expand Down Expand Up @@ -643,14 +625,9 @@ helpers.initDevice = async function (adb, opts) {

await helpers.ensureDeviceLocale(adb, opts.language, opts.locale);
await adb.startLogcat();
let defaultIME;
if (opts.unicodeKeyboard) {
defaultIME = await helpers.initUnicodeKeyboard(adb);
return await helpers.initUnicodeKeyboard(adb);
}
if (_.isUndefined(opts.unlockType) && !opts.skipUnlock) {
await helpers.pushUnlock(adb);
}
return defaultIME;
};

helpers.removeNullProperties = function (obj) {
Expand All @@ -674,8 +651,7 @@ helpers.isChromeBrowser = function (browser) {
};

helpers.getChromePkg = function (browser) {
return CHROME_BROWSER_PACKAGE_ACTIVITY[browser.toLowerCase()] ||
CHROME_BROWSER_PACKAGE_ACTIVITY.default;
return CHROME_BROWSER_PACKAGE_ACTIVITY[browser.toLowerCase()] || CHROME_BROWSER_PACKAGE_ACTIVITY.default;
};

helpers.removeAllSessionWebSocketHandlers = async function (server, sessionId) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@
"appium-base-driver": "^3.0.0",
"appium-chromedriver": "^4.0.0",
"appium-support": "^2.13.0",
"appium-unlock": "^2.0.0",
"asyncbox": "^2.0.4",
"babel-runtime": "=5.8.24",
"bluebird": "^3.4.7",
"io.appium.settings": "^2.5.0",
"io.appium.settings": "^2.7.0",
"jimp": "^0.2.24",
"lodash": "^4.17.4",
"moment": "^2.22.2",
Expand Down
18 changes: 0 additions & 18 deletions test/functional/android-helper-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ADB from 'appium-adb';
import { app } from './desired';
import { MOCHA_TIMEOUT } from './helpers';
import { exec } from 'teen_process';
import { path as unlockApkPath } from 'appium-unlock';
import path from 'path';


Expand Down Expand Up @@ -76,21 +75,4 @@ describe('android-helpers e2e', function () {
await helpers.pushSettingsApp(adb, true);
});
});
describe('pushUnlock', function () {
const unlockPkg = 'appium-unlock';
const unlockBundle = 'io.appium.unlock';
it('should be able to upgrade from unlock v0.0.1 to latest', async function () {
await adb.uninstallApk(unlockBundle);

// get and install old version of settings app
await exec('npm', ['install', `${unlockPkg}@0.0.1`]);
await adb.install(unlockApkPath);

// get latest version of settings app
await exec('npm', ['uninstall', unlockPkg]);
await exec('npm', ['install', unlockPkg]);

await helpers.pushUnlock(adb);
});
});
});
22 changes: 6 additions & 16 deletions test/unit/android-helper-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ADB from 'appium-adb';
import { withMocks } from 'appium-test-support';
import * as teen_process from 'teen_process';
import { fs } from 'appium-support';
import { path as unlockApkPath } from 'appium-unlock';
import unlocker from '../../lib/unlock-helpers';
import _ from 'lodash';
import B from 'bluebird';
Expand Down Expand Up @@ -331,8 +330,12 @@ describe('Android Helpers', function () {
it('should return package and launch activity from manifest', async function () {
mocks.adb.expects('packageAndLaunchActivityFromManifest').withExactArgs('foo')
.returns({apkPackage: 'pkg', apkActivity: 'ack'});
const result = {appPackage: 'pkg', appWaitPackage: 'pkg',
appActivity: 'ack', appWaitActivity: 'ack'};
const result = {
appPackage: 'pkg',
appWaitPackage: 'pkg',
appActivity: 'ack',
appWaitActivity: 'ack',
};
(await helpers.getLaunchInfo(adb, {app: "foo"})).should.deep
.equal(result);
mocks.adb.verify();
Expand Down Expand Up @@ -507,14 +510,6 @@ describe('Android Helpers', function () {
mocks.adb.verify();
});
}));
describe('pushUnlock', withMocks({adb}, (mocks) => {
it('should install unlockApp', async function () {
mocks.adb.expects('installOrUpgrade').withArgs(unlockApkPath, 'io.appium.unlock').once()
.returns('');
await helpers.pushUnlock(adb);
mocks.adb.verify();
});
}));
describe('pushStrings', withMocks({adb, fs}, (mocks) => {
it('should return {} because of no app, no package and no app in the target device', async function () {
const opts = {tmpDir: '/tmp_dir', appPackage: 'pkg'};
Expand Down Expand Up @@ -630,7 +625,6 @@ describe('Android Helpers', function () {
mocks.helpers.expects('pushSettingsApp').once();
mocks.helpers.expects('ensureDeviceLocale').withExactArgs(adb, opts.language, opts.locale).once();
mocks.helpers.expects('setMockLocationApp').withExactArgs(adb, 'io.appium.settings').once();
mocks.helpers.expects('pushUnlock').withExactArgs(adb).once();
await helpers.initDevice(adb, opts);
mocks.helpers.verify();
mocks.adb.verify();
Expand All @@ -642,7 +636,6 @@ describe('Android Helpers', function () {
mocks.helpers.expects('pushSettingsApp').once();
mocks.helpers.expects('ensureDeviceLocale').withArgs(adb).once();
mocks.helpers.expects('setMockLocationApp').never();
mocks.helpers.expects('pushUnlock').withExactArgs(adb).once();
await helpers.initDevice(adb, opts);
mocks.helpers.verify();
mocks.adb.verify();
Expand All @@ -655,7 +648,6 @@ describe('Android Helpers', function () {
mocks.helpers.expects('ensureDeviceLocale').once();
mocks.helpers.expects('setMockLocationApp').once();
mocks.helpers.expects('initUnicodeKeyboard').withExactArgs(adb).once().returns("defaultIME");
mocks.helpers.expects('pushUnlock').withExactArgs(adb).once();
await helpers.initDevice(adb, opts).should.become("defaultIME");
mocks.helpers.verify();
mocks.adb.verify();
Expand All @@ -668,7 +660,6 @@ describe('Android Helpers', function () {
mocks.helpers.expects('ensureDeviceLocale').once();
mocks.helpers.expects('setMockLocationApp').once();
mocks.helpers.expects('initUnicodeKeyboard').never();
mocks.helpers.expects('pushUnlock').withExactArgs(adb).once();
should.not.exist(await helpers.initDevice(adb, opts));
mocks.helpers.verify();
mocks.adb.verify();
Expand All @@ -681,7 +672,6 @@ describe('Android Helpers', function () {
mocks.helpers.expects('ensureDeviceLocale').once();
mocks.helpers.expects('setMockLocationApp').once();
mocks.helpers.expects('initUnicodeKeyboard').never();
mocks.helpers.expects('pushUnlock').never();
await helpers.initDevice(adb, opts);
mocks.helpers.verify();
mocks.adb.verify();
Expand Down

0 comments on commit c1da392

Please sign in to comment.