From cc4ff6705517acf1eaf82d6457be1a77f6e764ad Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 25 Jan 2024 13:28:09 +0100 Subject: [PATCH] fix: prepareEmulator API (#906) --- lib/commands/device/common.js | 2 +- lib/commands/device/utils.js | 10 +++--- test/unit/commands/device-specs.js | 56 ++++++++++++++---------------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/commands/device/common.js b/lib/commands/device/common.js index 48c9f98b..15d2b9d9 100644 --- a/lib/commands/device/common.js +++ b/lib/commands/device/common.js @@ -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 { diff --git a/lib/commands/device/utils.js b/lib/commands/device/utils.js index 811716d7..5a2f5192 100644 --- a/lib/commands/device/utils.js +++ b/lib/commands/device/utils.js @@ -91,9 +91,10 @@ export function prepareAvdArgs() { /** * @this {import('../../driver').AndroidDriver} + * @param {ADB} adb * @returns {Promise} */ -export async function prepareEmulator() { +export async function prepareEmulator(adb) { const { avd, avdEnv: env, @@ -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; @@ -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, diff --git a/test/unit/commands/device-specs.js b/test/unit/commands/device-specs.js index 7e4b106a..09c2fbc7 100644 --- a/test/unit/commands/device-specs.js +++ b/test/unit/commands/device-specs.js @@ -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; }); @@ -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, @@ -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: { @@ -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, @@ -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 () {