Skip to content

Commit

Permalink
fix: prepareEmulator API (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jan 25, 2024
1 parent 16008c4 commit cc4ff67
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/commands/device/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function getDeviceInfoFromCaps() {

// a specific avd name was given. try to initialize with that
if (this.opts?.avd) {
await prepareEmulator.bind(this)();
await prepareEmulator.bind(this)(adb);
udid = adb.curDeviceId;
emPort = adb.emulatorPort;
} else {
Expand Down
10 changes: 6 additions & 4 deletions lib/commands/device/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ export function prepareAvdArgs() {

/**
* @this {import('../../driver').AndroidDriver}
* @param {ADB} adb
* @returns {Promise<void>}
*/
export async function prepareEmulator() {
export async function prepareEmulator(adb) {
const {
avd,
avdEnv: env,
Expand All @@ -109,7 +110,8 @@ export async function prepareEmulator() {
const avdName = avd.replace('@', '');
let isEmulatorRunning = true;
try {
await this.adb.getRunningAVDWithRetry(avdName, 5000);
// This API implicitly modifies curDeviceId and emulatorPort properties of the adb instance
await adb.getRunningAVDWithRetry(avdName, 5000);
} catch (e) {
this.log.debug(`Emulator '${avdName}' is not running: ${e.message}`);
isEmulatorRunning = false;
Expand All @@ -118,13 +120,13 @@ export async function prepareEmulator() {
if (isEmulatorRunning) {
if (args.includes('-wipe-data')) {
this.log.debug(`Killing '${avdName}' because it needs to be wiped at start.`);
await this.adb.killEmulator(avdName);
await adb.killEmulator(avdName);
} else {
this.log.debug('Not launching AVD because it is already running.');
return;
}
}
await this.adb.launchAVD(avd, {
await adb.launchAVD(avd, {
args,
env,
language,
Expand Down
56 changes: 27 additions & 29 deletions test/unit/commands/device-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ chai.use(chaiAsPromised);
describe('Device Helpers', function () {
/** @type {AndroidDriver} */
let driver;
/** @type {ADB} */
let adb;
let sandbox = sinon.createSandbox();

beforeEach(function () {
const adb = new ADB();
adb = new ADB();
driver = new AndroidDriver();
driver.adb = adb;
});
Expand Down Expand Up @@ -55,22 +57,21 @@ describe('Device Helpers', function () {
});
describe('prepareEmulator', function () {
beforeEach(function () {
const opts = {avd: 'foo@bar', avdArgs: '', language: 'en', locale: 'us'};
driver.opts = opts;
driver.opts = {avd: 'foo@bar', avdArgs: '', language: 'en', locale: 'us'};
});
this.afterEach(function () {
sandbox.verify();
});

it('should not launch avd if one is already running', async function () {
sandbox.stub(driver.adb, 'getRunningAVDWithRetry').withArgs('foobar').returns('foo');
sandbox.stub(driver.adb, 'launchAVD').throws();
sandbox.stub(driver.adb, 'killEmulator').throws();
await prepareEmulator.bind(driver)();
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').returns('foo');
sandbox.stub(adb, 'launchAVD').throws();
sandbox.stub(adb, 'killEmulator').throws();
await prepareEmulator.bind(driver)(adb);
});
it('should launch avd if one is not running', async function () {
sandbox.stub(driver.adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(driver.adb, 'launchAVD')
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(adb, 'launchAVD')
.withArgs('foo@bar', {
args: [],
env: undefined,
Expand All @@ -80,20 +81,19 @@ describe('Device Helpers', function () {
readyTimeout: undefined,
})
.returns('');
await prepareEmulator.bind(driver)();
await prepareEmulator.bind(driver)(adb);
});
it('should parse avd string command line args', async function () {
const opts = {
driver.opts = {
avd: 'foobar',
avdArgs: '--arg1 "value 1" --arg2 "value 2"',
avdEnv: {
k1: 'v1',
k2: 'v2',
},
};
driver.opts = opts;
sandbox.stub(driver.adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(driver.adb, 'launchAVD')
};;
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(adb, 'launchAVD')
.withArgs('foobar', {
args: ['--arg1', 'value 1', '--arg2', 'value 2'],
env: {
Expand All @@ -106,16 +106,15 @@ describe('Device Helpers', function () {
readyTimeout: undefined,
})
.returns('');
await prepareEmulator.bind(driver)();
await prepareEmulator.bind(driver)(adb);
});
it('should parse avd array command line args', async function () {
const opts = {
driver.opts = {
avd: 'foobar',
avdArgs: ['--arg1', 'value 1', '--arg2', 'value 2'],
};
driver.opts = opts;
sandbox.stub(driver.adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(driver.adb, 'launchAVD')
};;
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(adb, 'launchAVD')
.withArgs('foobar', {
args: ['--arg1', 'value 1', '--arg2', 'value 2'],
env: undefined,
Expand All @@ -125,19 +124,18 @@ describe('Device Helpers', function () {
readyTimeout: undefined,
})
.returns('');
await prepareEmulator.bind(driver)();
await prepareEmulator.bind(driver)(adb);
});
it('should kill emulator if avdArgs contains -wipe-data', async function () {
const opts = {avd: 'foo@bar', avdArgs: '-wipe-data'};
driver.opts = opts;
sandbox.stub(driver.adb, 'getRunningAVDWithRetry').withArgs('foobar').returns('foo');
sandbox.stub(driver.adb, 'killEmulator').withArgs('foobar').onFirstCall();
sandbox.stub(driver.adb, 'launchAVD').onFirstCall();
await prepareEmulator.bind(driver)();
driver.opts = {avd: 'foo@bar', avdArgs: '-wipe-data'};;
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').returns('foo');
sandbox.stub(adb, 'killEmulator').withArgs('foobar').onFirstCall();
sandbox.stub(adb, 'launchAVD').onFirstCall();
await prepareEmulator.bind(driver)(adb);
});
it('should fail if avd name is not specified', async function () {
driver.opts = {};
await prepareEmulator.bind(driver)().should.eventually.be.rejected;
await prepareEmulator.bind(driver)(adb).should.eventually.be.rejected;
});
});
describe('prepareAvdArgs', function () {
Expand Down

0 comments on commit cc4ff67

Please sign in to comment.