From 55773106c59b09386e1a7273e1aa90fbfd786de2 Mon Sep 17 00:00:00 2001 From: Umut Uzgur Date: Thu, 11 Jul 2019 16:26:12 +0200 Subject: [PATCH] feat: remove idevicelocation --- lib/commands/location.js | 34 ++++---------------- test/unit/commands/location-specs.js | 46 +++++++++++----------------- 2 files changed, 24 insertions(+), 56 deletions(-) diff --git a/lib/commands/location.js b/lib/commands/location.js index abba3a98c..3453b6ef5 100644 --- a/lib/commands/location.js +++ b/lib/commands/location.js @@ -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; @@ -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); } catch (e) { log.errorAndThrow(`Can't set the location on device '${this.opts.udid}'. Original error: ${e.message}`); + } finally { + service.close(); } }; diff --git a/test/unit/commands/location-specs.js b/test/unit/commands/location-specs.js index 816847f54..d237af60d 100644 --- a/test/unit/commands/location-specs.js +++ b/test/unit/commands/location-specs.js @@ -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'); @@ -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 () { @@ -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]); }); });