Skip to content

Commit

Permalink
Add method to get/set gps location provider (appium#248)
Browse files Browse the repository at this point in the history
* add method to get location_providers_allowde

* add method to toggle gps locationn provider

* add unit tests

* more functional code

* update unit tests

* code even more functional
  • Loading branch information
vrunoa authored and imurchie committed Aug 7, 2017
1 parent dd999b2 commit e2d7e85
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/tools/adb-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ methods.getReqPermissions = async function (pkg, cmdOutput = null) {
return match[0].match(/android\.permission\.\w+/g) || [];
};

methods.getLocationProviders = async function () {
let stdout = await this.shell(['settings', 'get', 'secure', 'location_providers_allowed']);
return stdout.trim().split(',')
.map((p) => p.trim())
.filter(Boolean);
};

methods.toggleGPSLocationProvider = async function (enabled) {
await this.shell(['settings', 'put', 'secure', 'location_providers_allowed', `${enabled ? "+" : "-"}gps`]);
};

methods.stopAndClear = async function (pkg) {
try {
await this.forceStop(pkg);
Expand Down
43 changes: 43 additions & 0 deletions test/unit/adb-commands-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,49 @@ describe('adb commands', () => {
mocks.adb.verify();
});
}));
describe('getLocationProviders', withMocks({adb}, (mocks) => {
it('should call shell with correct args and return empty location_providers_allowed', async () => {
mocks.adb.expects("shell")
.once().withExactArgs(['settings', 'get', 'secure', 'location_providers_allowed'])
.returns('');
let providers = await adb.getLocationProviders();
providers.should.be.an('array');
providers.length.should.equal(0);
mocks.adb.verify();
});
it('should return one location_providers_allowed', async () => {
mocks.adb.expects("shell")
.once().withExactArgs(['settings', 'get', 'secure', 'location_providers_allowed'])
.returns('gps');
let providers = await adb.getLocationProviders();
providers.should.be.an('array');
providers.length.should.equal(1);
providers.should.include('gps');
mocks.adb.verify();
});
it('should return both location_providers_allowed', async () => {
mocks.adb.expects("shell")
.once().withExactArgs(['settings', 'get', 'secure', 'location_providers_allowed'])
.returns('gps ,wifi');
let providers = await adb.getLocationProviders();
providers.should.be.an('array');
providers.length.should.equal(2);
providers.should.include('gps');
providers.should.include('wifi');
mocks.adb.verify();
});
}));
describe('toggleGPSLocationProvider', withMocks({adb}, (mocks) => {
it('should call shell with correct args on gps enabled', async () => {
mocks.adb.expects("shell")
.withExactArgs(['settings', 'put', 'secure', 'location_providers_allowed', '+gps']);
mocks.adb.expects("shell")
.withExactArgs(['settings', 'put', 'secure', 'location_providers_allowed', '-gps']);
await adb.toggleGPSLocationProvider(true);
await adb.toggleGPSLocationProvider(false);
mocks.adb.verify();
});
}));
describe('setDeviceSysCountry', withMocks({adb}, (mocks) => {
it('should call shell with correct args', async () => {
mocks.adb.expects("shell")
Expand Down

0 comments on commit e2d7e85

Please sign in to comment.