Skip to content

Commit

Permalink
fix: Update GPS toggle logic since Android 11 (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Apr 29, 2023
1 parent 3069c61 commit 6251cf0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
25 changes: 19 additions & 6 deletions lib/tools/adb-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,21 @@ methods.getReqPermissions = async function getReqPermissions (pkg, cmdOutput = n
/**
* Retrieve the list of location providers for the device under test.
*
* @return {Array.<String>} The list of available location providers or an empty list.
* @return {Promise<string[]>} The list of available location providers or an empty list.
*/
methods.getLocationProviders = async function getLocationProviders () {
let stdout = await this.getSetting('secure', 'location_providers_allowed');
return stdout.trim().split(',')
.map((p) => p.trim())
.filter(Boolean);
if (await this.getApiLevel() < 31) {
// https://stackoverflow.com/questions/70939503/settings-secure-location-providers-allowed-returns-null-in-android-12
const stdout = await this.getSetting('secure', 'location_providers_allowed');
return stdout.trim().split(',')
.map((p) => p.trim())
.filter(Boolean);
}

// To emulate the legacy behavior
return _.includes(await this.shell(['cmd', 'location', 'is-location-enabled']), 'true')
? ['gps']
: [];
};

/**
Expand All @@ -474,7 +482,12 @@ methods.getLocationProviders = async function getLocationProviders () {
* @param {boolean} enabled - Whether to enable (true) or disable (false) the GPS provider.
*/
methods.toggleGPSLocationProvider = async function toggleGPSLocationProvider (enabled) {
await this.setSetting('secure', 'location_providers_allowed', `${enabled ? '+' : '-'}gps`);
if (await this.getApiLevel() < 31) {
// https://stackoverflow.com/questions/70939503/settings-secure-location-providers-allowed-returns-null-in-android-12
await this.setSetting('secure', 'location_providers_allowed', `${enabled ? '+' : '-'}gps`);
return;
}
await this.shell(['cmd', 'location', 'set-location-enabled', enabled ? 'true' : 'false']);
};

/**
Expand Down
15 changes: 14 additions & 1 deletion test/unit/adb-commands-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('adb commands', withMocks({adb, logcat, teen_process, net}, function (m
});
describe('getLocationProviders', function () {
it('should call shell with correct args and return empty location_providers_allowed', async function () {
mocks.adb.expects('getApiLevel').once().returns(27);
mocks.adb.expects('getSetting')
.once().withExactArgs('secure', 'location_providers_allowed')
.returns('');
Expand All @@ -110,6 +111,7 @@ describe('adb commands', withMocks({adb, logcat, teen_process, net}, function (m
providers.length.should.equal(0);
});
it('should return one location_providers_allowed', async function () {
mocks.adb.expects('getApiLevel').once().returns(27);
mocks.adb.expects('getSetting')
.once().withExactArgs('secure', 'location_providers_allowed')
.returns('gps');
Expand All @@ -119,6 +121,7 @@ describe('adb commands', withMocks({adb, logcat, teen_process, net}, function (m
providers.should.include('gps');
});
it('should return both location_providers_allowed', async function () {
mocks.adb.expects('getApiLevel').once().returns(27);
mocks.adb.expects('getSetting')
.once().withExactArgs('secure', 'location_providers_allowed')
.returns('gps ,wifi');
Expand All @@ -130,14 +133,24 @@ describe('adb commands', withMocks({adb, logcat, teen_process, net}, function (m
});
});
describe('toggleGPSLocationProvider', function () {
it('should call shell with correct args on gps enabled', async function () {
it('should call shell with correct args on gps enabled on API below 31', async function () {
mocks.adb.expects('getApiLevel').atLeast(1).returns(27);
mocks.adb.expects('setSetting')
.withExactArgs('secure', 'location_providers_allowed', '+gps');
mocks.adb.expects('setSetting')
.withExactArgs('secure', 'location_providers_allowed', '-gps');
await adb.toggleGPSLocationProvider(true);
await adb.toggleGPSLocationProvider(false);
});
it('should call shell with correct args on gps enabled on API above 30', async function () {
mocks.adb.expects('getApiLevel').atLeast(1).returns(31);
mocks.adb.expects('shell')
.withExactArgs(['cmd', 'location', 'set-location-enabled', 'true']);
mocks.adb.expects('shell')
.withExactArgs(['cmd', 'location', 'set-location-enabled', 'false']);
await adb.toggleGPSLocationProvider(true);
await adb.toggleGPSLocationProvider(false);
});
});
describe('getDeviceSysLocale', function () {
it('should call shell with correct args', async function () {
Expand Down

0 comments on commit 6251cf0

Please sign in to comment.