Skip to content

Commit

Permalink
feat: Add mobile wrappers to geolocation APIs (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Sep 23, 2024
1 parent 328fd39 commit 839545d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/commands/device/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import semver from 'semver';
import _ from 'lodash';
import B from 'bluebird';
import {resetMockLocation, setMockLocationApp} from '../geolocation';
import {setMockLocationApp} from '../geolocation';
import {SETTINGS_HELPER_ID} from 'io.appium.settings';
import {hideKeyboardCompletely, initUnicodeKeyboard} from '../keyboard';
import {
Expand Down Expand Up @@ -235,7 +235,7 @@ export async function initDevice() {
if (mockLocationApp || _.isUndefined(mockLocationApp)) {
await setMockLocationApp.bind(this)(mockLocationApp || SETTINGS_HELPER_ID);
} else {
await resetMockLocation.bind(this)();
await this.mobileResetGeolocation();
}
})());
}
Expand Down
4 changes: 4 additions & 0 deletions lib/commands/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export function mobileCommandsMapping() {

getCurrentActivity: 'getCurrentActivity',
getCurrentPackage: 'getCurrentPackage',

setGeolocation: 'mobileSetGeolocation',
getGeolocation: 'mobileGetGeolocation',
resetGeolocation: 'mobileResetGeolocation',
};
}

Expand Down
29 changes: 28 additions & 1 deletion lib/commands/geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export async function setGeoLocation(location) {
}
}

/**
* @this {import('../driver').AndroidDriver}
* @param {import('@appium/types').Location} opts
*/
export async function mobileSetGeolocation(opts) {
await this.settingsApp.setGeoLocation(opts, this.isEmulator());
}

/**
* Sends an async request to refresh the GPS cache.
*
Expand Down Expand Up @@ -64,6 +72,14 @@ export async function getGeoLocation() {
};
}

/**
* @this {import('../driver').AndroidDriver}
* @returns {Promise<import('@appium/types').Location>}
*/
export async function mobileGetGeolocation() {
return await this.getGeoLocation();
}

/**
* @this {import('../driver').AndroidDriver}
* @returns {Promise<boolean>}
Expand All @@ -86,6 +102,17 @@ export async function toggleLocationServices() {
await this.adb.toggleGPSLocationProvider(!isGpsEnabled);
}

/**
* @this {import('../driver').AndroidDriver}
* @returns {Promise<void>}
*/
export async function mobileResetGeolocation() {
if (this.isEmulator()) {
throw new Error('Geolocation reset does not work on emulators');
}
await resetMockLocation.bind(this);
}

// #region Internal helpers

/**
Expand Down Expand Up @@ -133,7 +160,7 @@ export async function setMockLocationApp(appId) {
* @this {import('../driver').AndroidDriver}
* @returns {Promise<void>}
*/
export async function resetMockLocation() {
async function resetMockLocation() {
try {
if ((await this.adb.getApiLevel()) < 23) {
await this.adb.shell(['settings', 'put', 'secure', 'mock_location', '0']);
Expand Down
6 changes: 6 additions & 0 deletions lib/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ import {
mobileRefreshGpsCache,
toggleLocationServices,
isLocationServicesEnabled,
mobileGetGeolocation,
mobileSetGeolocation,
mobileResetGeolocation,
} from './commands/geolocation';
import {
performActions,
Expand Down Expand Up @@ -453,6 +456,9 @@ class AndroidDriver
mobileRefreshGpsCache = mobileRefreshGpsCache;
toggleLocationServices = toggleLocationServices;
isLocationServicesEnabled = isLocationServicesEnabled;
mobileGetGeolocation = mobileGetGeolocation;
mobileSetGeolocation = mobileSetGeolocation;
mobileResetGeolocation = mobileResetGeolocation;

performActions = performActions;

Expand Down

0 comments on commit 839545d

Please sign in to comment.