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

feat: remove idevicelocation #1006

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 6 additions & 28 deletions lib/commands/location.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { exec } from 'teen_process';
import { fs, util } from 'appium-support';
import { services } from 'appium-ios-device';
import { util } from 'appium-support';
import log from '../logger';
import _ from 'lodash';

const IDEVICELOCATION = 'idevicelocation';
const MINUS_MARKER = '--';

let commands = {};

function formatLocationArg (value) {
value = `${value}`.trim();
// Negative coordinate values should be properly formatted
return value.startsWith('-') ? [MINUS_MARKER, value] : [value];
}

commands.setGeoLocation = async function setGeoLocation (location) {
let {latitude, longitude} = location;

Expand All @@ -26,25 +16,13 @@ commands.setGeoLocation = async function setGeoLocation (location) {
return;
}

const service = await services.startSimulateLocationService(this.opts.udid);
try {
await fs.which(IDEVICELOCATION);
} catch (e) {
log.errorAndThrow(`${IDEVICELOCATION} doesn't exist on the host. ` +
'Check https://github.com/JonGabilondoAngulo/idevicelocation on how to install the tool.');
}
let args = [];
args.push(...formatLocationArg(latitude));
args.push(...formatLocationArg(longitude));
if (args.includes(MINUS_MARKER) && _.countBy(args)[MINUS_MARKER] > 1) {
// Move -- marker at the start of the args array if there is more than one occurrence
args = [MINUS_MARKER, ...(_.without(args, MINUS_MARKER))];
}
args = ['-u', this.opts.udid, ...args];
log.debug(`Executing ${IDEVICELOCATION} with args ${JSON.stringify(args)}`);
try {
await exec(IDEVICELOCATION, args);
service.setLocation(latitude, longitude);
umutuzgur marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
log.errorAndThrow(`Can't set the location on device '${this.opts.udid}'. Original error: ${e.message}`);
} finally {
service.close();
}
};

Expand Down
46 changes: 18 additions & 28 deletions test/unit/commands/location-specs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import sinon from 'sinon';
import XCUITestDriver from '../../..';
import { fs } from 'appium-support';
import * as teenProcess from 'teen_process';
import { services } from 'appium-ios-device';

describe('location commands', function () {
const udid = '1234';
const toolName = 'idevicelocation';

const driver = new XCUITestDriver();
const proxySpy = sinon.stub(driver, 'proxyCommand');
Expand All @@ -15,17 +13,19 @@ describe('location commands', function () {
});

describe('setLocation', function () {
let execStub;
let fsWhichStub;
let startSimulateLocationServiceStub;
let setLocationStub;

beforeEach(function () {
execStub = sinon.stub(teenProcess, 'exec');
fsWhichStub = sinon.stub(fs, 'which');
startSimulateLocationServiceStub = sinon.stub(services, 'startSimulateLocationService');
let mockService = { setLocation () {}, close () {} };
setLocationStub = sinon.stub(mockService, 'setLocation');
startSimulateLocationServiceStub.returns(mockService);
});

afterEach(function () {
execStub.restore();
fsWhichStub.restore();
startSimulateLocationServiceStub.restore();
setLocationStub.restore();
});

it('should fail when location object is wrong', async function () {
Expand All @@ -38,30 +38,20 @@ describe('location commands', function () {
driver.opts.realDevice = true;
});

it('should use idevicelocation to set a location', async function () {
fsWhichStub.returns(toolName);
await driver.setGeoLocation({latitude: '1.234', longitude: '2.789'});
it('should use location service to set a location', async function () {
await driver.setGeoLocation({latitude: 1.234, longitude: 2.789});

execStub.calledOnce.should.be.true;
execStub.firstCall.args[0].should.eql(toolName);
execStub.firstCall.args[1].should.eql(['-u', udid, '1.234', '2.789']);
startSimulateLocationServiceStub.calledOnce.should.be.true;
startSimulateLocationServiceStub.firstCall.args[0].should.eql(udid);
setLocationStub.args[0].should.eql([1.234, 2.789]);
});

it('should use idevicelocation to set a location with negative values', async function () {
fsWhichStub.returns(toolName);
it('should use location service to set a location with negative values', async function () {
await driver.setGeoLocation({latitude: 1.234, longitude: -2});

execStub.calledOnce.should.be.true;
execStub.firstCall.args[0].should.eql(toolName);
execStub.firstCall.args[1].should.eql(['-u', udid, '1.234', '--', '-2']);
});

it('should fail when idevicelocation doesnt exist on the host', async function () {
fsWhichStub.throws();
await driver.setGeoLocation({
latitude: '1.234',
longitude: '2.789'}
).should.be.rejectedWith(`idevicelocation doesn't exist on the host`);
startSimulateLocationServiceStub.calledOnce.should.be.true;
startSimulateLocationServiceStub.firstCall.args[0].should.eql(udid);
setLocationStub.args[0].should.eql([1.234, -2]);
});
});

Expand Down