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

Uninstall if installed version is older or newer than new one #358

Merged
merged 6 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
40 changes: 38 additions & 2 deletions lib/espresso-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const TEST_MANIFEST_PATH = path.resolve(__dirname, '..', '..', 'espresso-server'
const TEST_APK_PKG = 'io.appium.espressoserver.test';
const REQUIRED_PARAMS = ['adb', 'tmpDir', 'host', 'systemPort', 'devicePort', 'appPackage', 'forceEspressoRebuild'];
const ESPRESSO_SERVER_LAUNCH_TIMEOUT = 30000;
const ESPRESSO_SERVER_INSTALL_RETRIES = 20;
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
const TARGET_PACKAGE_CONTAINER = '/data/local/tmp/espresso.apppackage';

class EspressoRunner {
Expand Down Expand Up @@ -40,8 +41,43 @@ class EspressoRunner {
return previousAppPackage !== this.appPackage;
}

async installServer () {
await this.adb.installOrUpgrade(this.modServerPath, TEST_APK_PKG);
/**
* Installs Espresso server apk on to the device or emulator.
*
* @param {number} installTimeout - Installation timeout
*/
async installServer (installTimeout = ESPRESSO_SERVER_INSTALL_RETRIES * 1000) {
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
const appState = await this.adb.getApplicationInstallState(this.modServerPath, TEST_APK_PKG);

const shouldUninstallApp = [
this.adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
this.adb.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED
].includes(appState);
const shouldInstallApp = shouldUninstallApp || [
this.adb.APP_INSTALL_STATE.NOT_INSTALLED
].includes(appState);

if (shouldUninstallApp) {
logger.info(`Uninstalling Espresso Test Server apk from the target device (pkg: '${TEST_APK_PKG}')`);
try {
await this.adb.uninstallApk(TEST_APK_PKG);
} catch (err) {
logger.warn(`Error uninstalling '${TEST_APK_PKG}': ${err.message}`);
}
}

if (shouldInstallApp) {
logger.info(`Installing Espresso Test Server apk from the target device (path: '${this.modServerPath}')`);
try {
await this.adb.install(this.modServerPath, {
replace: false,
timeout: installTimeout,
});
} catch (err) {
logger.errorAndThrow(`Cannot install '${this.modServerPath}' because of '${err.message}'`);
}
}

logger.info(`Installed Espresso Test Server apk '${this.modServerPath}' (pkg: '${TEST_APK_PKG}')`);
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
127 changes: 126 additions & 1 deletion test/unit/espresso-runner-specs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { EspressoRunner, REQUIRED_PARAMS } from '../../lib/espresso-runner';

import { ADB } from 'appium-adb';
import sinon from 'sinon';

chai.should();
chai.use(chaiAsPromised);
const expect = chai.expect;
let sandbox = sinon.createSandbox();

describe('espresso-runner', function () {
function getOpts (params) {
Expand All @@ -29,4 +31,127 @@ describe('espresso-runner', function () {
runConstructorTest(opts, REQUIRED_PARAMS[i]);
}
});

describe('installServer', function () {
const adbCmd = new ADB();
let uninstallCount = -1;
let installCount = -1;
const commonStub = {
APP_INSTALL_STATE: {
NEWER_VERSION_INSTALLED: adbCmd.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
OLDER_VERSION_INSTALLED: adbCmd.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
NOT_INSTALLED: adbCmd.APP_INSTALL_STATE.NOT_INSTALLED,
},
uninstallApk: () => {
uninstallCount += 1;
return uninstallCount;
},
install: () => {
installCount += 1;
return installCount;
}
};

afterEach(function () {
sandbox.restore();
});

it('should install newer server', async function () {
sandbox.stub(ADB, 'createADB').callsFake(function () {
uninstallCount = -1;
installCount = -1;
return Object.assign(
commonStub,
{
getApplicationInstallState: () => adbCmd.APP_INSTALL_STATE.NEWER_VERSION_INSTALLED,
}
);
});

const adb = ADB.createADB();
const espresso = new EspressoRunner({
adb, tmpDir: 'tmp', host: 'localhost',
systemPort: 4724, devicePort: 6790, appPackage: 'io.appium.example',
forceEspressoRebuild: false
});

await espresso.installServer();
espresso.adb.uninstallApk().should.eql(1);
espresso.adb.install().should.eql(1);
});

it('should install older server', async function () {
sandbox.stub(ADB, 'createADB').callsFake(function () {
uninstallCount = -1;
installCount = -1;
return Object.assign(
commonStub,
{
getApplicationInstallState: () => adbCmd.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
}
);
});

const adb = ADB.createADB();
const espresso = new EspressoRunner({
adb, tmpDir: 'tmp', host: 'localhost',
systemPort: 4724, devicePort: 6790, appPackage: 'io.appium.example',
forceEspressoRebuild: false
});

await espresso.installServer();
espresso.adb.uninstallApk().should.eql(1);
espresso.adb.install().should.eql(1);
});

it('should install from no server', async function () {
sandbox.stub(ADB, 'createADB').callsFake(function () {
uninstallCount = -1;
installCount = -1;
return Object.assign(
commonStub,
{
getApplicationInstallState: () => adbCmd.APP_INSTALL_STATE.NOT_INSTALLED
}
);
});

const adb = ADB.createADB();
const espresso = new EspressoRunner({
adb, tmpDir: 'tmp', host: 'localhost',
systemPort: 4724, devicePort: 6790, appPackage: 'io.appium.example',
forceEspressoRebuild: false
});

await espresso.installServer();
espresso.adb.uninstallApk().should.eql(0);
espresso.adb.install().should.eql(1);
});

it('should raise an error when it fails to install an apk', async function () {
sandbox.stub(ADB, 'createADB').callsFake(function () {
uninstallCount = -1;
installCount = -1;
return Object.assign(
commonStub,
{
getApplicationInstallState: () => adbCmd.APP_INSTALL_STATE.NOT_INSTALLED,
install: () => {
throw new Error('error happened');
}
}
);
});

const adb = ADB.createADB();
const espresso = new EspressoRunner({
adb, tmpDir: 'tmp', host: 'localhost',
systemPort: 4724, devicePort: 6790, appPackage: 'io.appium.example',
forceEspressoRebuild: false
});

await espresso.installServer().should.eventually.to.be.rejectedWith(/error happened/i);
espresso.adb.uninstallApk().should.eql(0);
});
});
});