Skip to content

Commit

Permalink
Merge pull request #77 from Jonahss/fixAdbSetup
Browse files Browse the repository at this point in the history
adjust ADB setup. fixes #5949
  • Loading branch information
Jonahss committed Jan 6, 2016
2 parents 788b18d + 8a4f863 commit 5dc6826
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 35 deletions.
41 changes: 28 additions & 13 deletions lib/android-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 'appium-android-bootstrap';
import ADB from 'appium-adb';

const REMOTE_TEMP_PATH = "/data/local/tmp";
const REMOTE_INSTALL_TIMEOUT = 90000; // milliseconds
Expand Down Expand Up @@ -76,23 +77,41 @@ helpers.ensureDeviceLocale = async function (adb, language, locale) {
}
};

helpers.getActiveDevice = async function (adb, udid) {
helpers.getDeviceInfoFromCaps = async function (opts = {}) {
// we can create a standalone ADB instance here, so there is no dependency
// on instantiating on earlier (at this point, we have no udid)
// we can only use this ADB object for commands that would not be confused
// if multiple devices are connected
let adb = await ADB.createADB({javaVersion: opts.javaVersion});
let udid = opts.udid;
let emPort = null;

logger.info("Retrieving device list");
let devices = await adb.getDevicesWithRetry();
let deviceId = null, emPort = null;
if (udid) {
if (!_.contains(_.pluck(devices, 'udid'), udid)) {
logger.errorAndThrow(`Device ${udid} was not in the list ` +
`of connected devices`);
}
deviceId = udid;
emPort = adb.getPortFromEmulatorString(deviceId);
emPort = adb.getPortFromEmulatorString(udid);
} else {
deviceId = devices[0].udid;
emPort = adb.getPortFromEmulatorString(deviceId);
udid = devices[0].udid;
emPort = adb.getPortFromEmulatorString(udid);
}
logger.info(`Using device: ${udid}`);
return {udid, emPort};
};

// returns a new adb instance with deviceId set
helpers.createADB = async function (javaVersion, udid, emPort) {
let adb = await ADB.createADB({javaVersion});

adb.setDeviceId(udid);
if (emPort) {
adb.setEmulatorPort(emPort);
}
logger.info(`Found device: ${deviceId}`);
return {deviceId, emPort};

return adb;
};

helpers.getLaunchInfo = async function (adb, opts) {
Expand Down Expand Up @@ -307,13 +326,9 @@ helpers.initDevice = async function (adb, opts) {
if (opts.avd) {
await helpers.prepareEmulator(adb, opts);
}
let {deviceId, emPort} = await helpers.getActiveDevice(adb, opts.udid);
adb.setDeviceId(deviceId);
if (emPort) {
adb.setEmulatorPort(emPort);
}

await adb.waitForDevice();

await helpers.ensureDeviceLocale(adb, opts.language, opts.locale);
await adb.startLogcat();
let defaultIME;
Expand Down
10 changes: 7 additions & 3 deletions lib/driver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseDriver, DeviceSettings } from 'appium-base-driver';
import ADB from 'appium-adb';
import Chromedriver from 'appium-chromedriver';
import desiredConstraints from './desired-caps';
import commands from './commands/index';
Expand Down Expand Up @@ -81,7 +80,7 @@ class AndroidDriver extends BaseDriver {
if (!this.opts.javaVersion) {
this.opts.javaVersion = await helpers.getJavaVersion();
}

this.curContext = this.defaultContextName();

if (this.isChromeSession) {
Expand All @@ -92,8 +91,13 @@ class AndroidDriver extends BaseDriver {
log.info(`Chrome-type package and activity are ${pkg} and ${activity}`);
}

// get device udid for this session
let {udid, emPort} = await helpers.getDeviceInfoFromCaps(this.opts);
this.opts.udid = udid;
this.opts.emPort = emPort;

// set up an instance of ADB
this.adb = await ADB.createADB({javaVersion: this.opts.javaVersion});
this.adb = await helpers.createADB(this.opts.javaVersion, this.opts.udid, this.opts.emPort);

if (this.helpers.isPackageOrBundle(this.opts.app)){
// user provided package instead of app for 'app' capability, massage options
Expand Down
59 changes: 41 additions & 18 deletions test/unit/android-helper-specs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import helpers from '../../lib/android-helpers';
import ADB from 'appium-adb';
import { withMocks } from 'appium-test-support';
Expand Down Expand Up @@ -93,29 +94,51 @@ describe('Android Helpers', () => {
mocks.adb.verify();
});
}));
describe('getActiveDevice', withMocks({adb}, (mocks) => {

describe('getDeviceInfoFromCaps', () => {
before(() => {
sinon.stub(ADB, 'createADB', async () => {
return {
getDevicesWithRetry: async () => {
return [
{udid: 'emulator-1234'},
{udid: 'rotalume-1337'}
];
},
getPortFromEmulatorString: () => {
return 1234;
}
};
});
});

after(() => {
ADB.createADB.restore();
});

it('should throw error when udid not in list', async () => {
mocks.adb.expects('getDevicesWithRetry').withExactArgs()
.returns(["foo"]);
await helpers.getActiveDevice(adb, "bar").should.eventually
.be.rejectedWith("bar");
mocks.adb.verify();
let caps = {
udid: 'foomulator'
};

await helpers.getDeviceInfoFromCaps(caps).should.be.rejectedWith('foomulator');
});
it('should get deviceId and emPort when udid is present', async () => {
mocks.adb.expects('getDevicesWithRetry').withExactArgs()
.returns([{udid: 'emulator-1234'}]);
(await helpers.getActiveDevice(adb, "emulator-1234")).should.deep
.equal({ deviceId: 'emulator-1234', emPort: 1234 });
mocks.adb.verify();
let caps = {
udid: 'emulator-1234'
};

let {udid, emPort} = await helpers.getDeviceInfoFromCaps(caps);
udid.should.equal('emulator-1234');
emPort.should.equal(1234);
});
it('should get first deviceId and emPort', async () => {
mocks.adb.expects('getDevicesWithRetry').withExactArgs()
.returns([{udid: 'emulator-1234'}, {udid: 'emulator2-2345'}]);
(await helpers.getActiveDevice(adb)).should.deep
.equal({ deviceId: 'emulator-1234', emPort: 1234 });
mocks.adb.verify();
it('should get first deviceId and emPort by default', async () => {
let {udid, emPort} = await helpers.getDeviceInfoFromCaps();
udid.should.equal('emulator-1234');
emPort.should.equal(1234);
});
}));
});

describe('getLaunchInfoFromManifest', withMocks({adb}, (mocks) => {
it('should return when no app present', async () => {
mocks.adb.expects('packageAndLaunchActivityFromManifest').never();
Expand Down
16 changes: 15 additions & 1 deletion test/unit/driver-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ describe('driver', () => {
sandbox.stub(driver, 'checkAppPresent');
sandbox.stub(driver, 'checkPackagePresent');
sandbox.stub(driver, 'startAndroidSession');
sandbox.stub(ADB, 'createADB');
sandbox.stub(ADB, 'createADB', async () => {
return {
getDevicesWithRetry: async () => {
return [
{udid: 'emulator-1234'},
{udid: 'rotalume-1337'}
];
},
getPortFromEmulatorString: () => {
return 1234;
},
setDeviceId: () => {},
setEmulatorPort: () => {}
};
});
});
afterEach(() => {
sandbox.restore();
Expand Down

0 comments on commit 5dc6826

Please sign in to comment.