Skip to content

Commit

Permalink
fix: calls this.opts for createBaseADB (#908)
Browse files Browse the repository at this point in the history
* fix: calls this.opts for createBaseADB

* test: fix test

* fix: review

* fix wrong lint
  • Loading branch information
KazuCocoa authored Jan 26, 2024
1 parent 74480ae commit 8ed666a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
7 changes: 3 additions & 4 deletions lib/commands/device/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,12 @@ export async function getDeviceInfoFromCaps() {

/**
* @this {AndroidDriver}
* @property {(AndroidDriverOpts & {emPort?: number})?} [opts=null]
* @returns {Promise<import('appium-adb').ADB>}
*/
export async function createADB(opts = null) {
export async function createADB() {
// @ts-expect-error do not put arbitrary properties on opts
const {udid, emPort} = opts ?? {};
const adb = await createBaseADB(opts);
const {udid, emPort} = this.opts;
const adb = await createBaseADB(this.opts);
adb.setDeviceId(udid ?? '');
if (emPort) {
adb.setEmulatorPort(emPort);
Expand Down
52 changes: 34 additions & 18 deletions test/unit/commands/device-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import ADB from 'appium-adb';
import _ from 'lodash';
import { AndroidDriver } from '../../../lib/driver';
import { prepareAvdArgs, prepareEmulator } from '../../../lib/commands/device/utils';
import {AndroidDriver} from '../../../lib/driver';
import {prepareAvdArgs, prepareEmulator} from '../../../lib/commands/device/utils';
import * as deviceUtils from '../../../lib/commands/device/utils';
import * as geolocationHelpers from '../../../lib/commands/geolocation';
import * as keyboardHelpers from '../../../lib/commands/keyboard';


chai.use(chaiAsPromised);

describe('Device Helpers', function () {
Expand Down Expand Up @@ -71,7 +70,8 @@ describe('Device Helpers', function () {
});
it('should launch avd if one is not running', async function () {
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(adb, 'launchAVD')
sandbox
.stub(adb, 'launchAVD')
.withArgs('foo@bar', {
args: [],
env: undefined,
Expand All @@ -91,9 +91,10 @@ describe('Device Helpers', function () {
k1: 'v1',
k2: 'v2',
},
};;
};
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(adb, 'launchAVD')
sandbox
.stub(adb, 'launchAVD')
.withArgs('foobar', {
args: ['--arg1', 'value 1', '--arg2', 'value 2'],
env: {
Expand All @@ -112,9 +113,10 @@ describe('Device Helpers', function () {
driver.opts = {
avd: 'foobar',
avdArgs: ['--arg1', 'value 1', '--arg2', 'value 2'],
};;
};
sandbox.stub(adb, 'getRunningAVDWithRetry').withArgs('foobar').throws();
sandbox.stub(adb, 'launchAVD')
sandbox
.stub(adb, 'launchAVD')
.withArgs('foobar', {
args: ['--arg1', 'value 1', '--arg2', 'value 2'],
env: undefined,
Expand All @@ -127,7 +129,7 @@ describe('Device Helpers', function () {
await prepareEmulator.bind(driver)(adb);
});
it('should kill emulator if avdArgs contains -wipe-data', async function () {
driver.opts = {avd: 'foo@bar', avdArgs: '-wipe-data'};;
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();
Expand Down Expand Up @@ -326,7 +328,7 @@ describe('Device Helpers', function () {
ADB.createADB.restore();
});
it('should create adb and set device id and emulator port', async function () {
await driver.createADB({
driver.opts = {
udid: '111222',
emPort: '111',
adbPort: '222',
Expand All @@ -342,7 +344,8 @@ describe('Device Helpers', function () {
remoteAppsCacheLimit: 5,
buildToolsVersion: '1.2.3',
allowOfflineDevices: true,
});
};
await driver.createADB();
ADB.createADB.calledWithExactly({
adbPort: '222',
suppressKillServer: true,
Expand Down Expand Up @@ -376,10 +379,14 @@ describe('Device Helpers', function () {
sandbox.stub(driver.adb, 'waitForDevice').throws();
sandbox.stub(driver.adb, 'startLogcat').onFirstCall();
sandbox.stub(deviceUtils, 'pushSettingsApp').onFirstCall();
sandbox.stub(driver, 'ensureDeviceLocale')
sandbox
.stub(driver, 'ensureDeviceLocale')
.withArgs(driver.opts.language, driver.opts.locale, driver.opts.localeScript)
.onFirstCall();
sandbox.stub(geolocationHelpers, 'setMockLocationApp').withArgs('io.appium.settings').onFirstCall();
sandbox
.stub(geolocationHelpers, 'setMockLocationApp')
.withArgs('io.appium.settings')
.onFirstCall();
await driver.initDevice();
});
it('should init device without locale and language', async function () {
Expand All @@ -390,7 +397,10 @@ describe('Device Helpers', function () {
sandbox.stub(driver.adb, 'startLogcat').onFirstCall();
sandbox.stub(deviceUtils, 'pushSettingsApp').onFirstCall();
sandbox.stub(driver, 'ensureDeviceLocale').throws();
sandbox.stub(geolocationHelpers, 'setMockLocationApp').withArgs('io.appium.settings').onFirstCall();
sandbox
.stub(geolocationHelpers, 'setMockLocationApp')
.withArgs('io.appium.settings')
.onFirstCall();
await driver.initDevice();
});
it('should init device with either locale or language', async function () {
Expand All @@ -400,10 +410,14 @@ describe('Device Helpers', function () {
sandbox.stub(driver.adb, 'waitForDevice').throws();
sandbox.stub(driver.adb, 'startLogcat').onFirstCall();
sandbox.stub(deviceUtils, 'pushSettingsApp').onFirstCall();
sandbox.stub(driver, 'ensureDeviceLocale')
sandbox
.stub(driver, 'ensureDeviceLocale')
.withArgs(driver.opts.language, driver.opts.locale, driver.opts.localeScript)
.onFirstCall();
sandbox.stub(geolocationHelpers, 'setMockLocationApp').withArgs('io.appium.settings').onFirstCall();
sandbox
.stub(geolocationHelpers, 'setMockLocationApp')
.withArgs('io.appium.settings')
.onFirstCall();
await driver.initDevice();
});
it('should not install mock location on emulator', async function () {
Expand Down Expand Up @@ -437,9 +451,11 @@ describe('Device Helpers', function () {
sandbox.stub(driver.adb, 'startLogcat').throws();
sandbox.stub(deviceUtils, 'pushSettingsApp').onFirstCall();
sandbox.stub(driver, 'ensureDeviceLocale').throws();
sandbox.stub(geolocationHelpers, 'setMockLocationApp').withArgs('io.appium.settings').onFirstCall();
sandbox
.stub(geolocationHelpers, 'setMockLocationApp')
.withArgs('io.appium.settings')
.onFirstCall();
await driver.initDevice();
});
});

});

0 comments on commit 8ed666a

Please sign in to comment.