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

fix: get connectivity return value #987

Merged
merged 4 commits into from
Mar 14, 2025
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
12 changes: 6 additions & 6 deletions lib/commands/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function mobileSetConnectivity(wifi, data, airplaneMode) {
*/
export async function mobileGetConnectivity(services = SUPPORTED_SERVICE_NAMES) {
const svcs = _.castArray(services);
const unsupportedServices = _.difference(services, SUPPORTED_SERVICE_NAMES);
const unsupportedServices = _.difference(svcs, SUPPORTED_SERVICE_NAMES);
if (!_.isEmpty(unsupportedServices)) {
throw new errors.InvalidArgumentError(
`${util.pluralize(
Expand All @@ -120,11 +120,11 @@ export async function mobileGetConnectivity(services = SUPPORTED_SERVICE_NAMES)
),
};
await B.all(_.values(statePromises));
return {
wifi: Boolean(statePromises.wifi.value()),
data: Boolean(statePromises.data.value()),
airplaneMode: Boolean(statePromises.airplaneMode.value()),
};
return _.reduce(
statePromises,
(state, v, k) => _.isUndefined(v.value()) ? state : {...state, [k]: Boolean(v.value())},
{}
);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ export interface GetConnectivityResult {
/**
* True if wifi is enabled
*/
wifi: boolean;
wifi?: boolean;
/**
* True if mobile data connection is enabled
*/
data: boolean;
data?: boolean;
/**
* True if Airplane Mode is enabled
*/
airplaneMode: boolean;
airplaneMode?: boolean;
}

export type ServiceType = 'wifi' | 'data' | 'airplaneMode';
Expand Down
42 changes: 42 additions & 0 deletions test/unit/commands/network-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ADB} from 'appium-adb';
import {AndroidDriver} from '../../../lib/driver';
import B from 'bluebird';
import { SettingsApp } from 'io.appium.settings';
import { errors } from 'appium/driver';

/** @type {AndroidDriver} */
let driver;
Expand Down Expand Up @@ -118,6 +119,47 @@ describe('Network', function () {
driver.setDataState.calledWithExactly(true).should.be.true;
});
});
describe('mobileGetConnectivity', function () {
it('should raise unsupported services in string', async function () {
await driver.mobileGetConnectivity('bad')
.should.eventually.rejectedWith(errors.InvalidArgumentError);
});
it('should raise unsupported services in array', async function () {
await driver.mobileGetConnectivity(['bad', 'array'])
.should.eventually.rejectedWith(errors.InvalidArgumentError);
});
it('should raise unsupported services with an empty array', async function () {
await driver.mobileGetConnectivity().should.eventually.eql({});
});
it('should return all supported services', async function () {
adb.isWifiOn.returns(true);
adb.isDataOn.returns(true);
adb.isAirplaneModeOn.returns(true);
await driver.mobileGetConnectivity()
.should.eventually.eql({ wifi: true, data: true, airplaneMode: true });
});
it('should return only wifi', async function () {
adb.isWifiOn.returns(true);
adb.isDataOn.returns(true);
adb.isAirplaneModeOn.returns(true);
await driver.mobileGetConnectivity('wifi')
.should.eventually.eql({ wifi: true });
});
it('should return only data', async function () {
adb.isWifiOn.returns(true);
adb.isDataOn.returns(true);
adb.isAirplaneModeOn.returns(true);
await driver.mobileGetConnectivity(['data'])
.should.eventually.eql({ data: true });
});
it('should return only data and airplaneMode', async function () {
adb.isWifiOn.returns(true);
adb.isDataOn.returns(true);
adb.isAirplaneModeOn.returns(false);
await driver.mobileGetConnectivity(['data', 'airplaneMode'])
.should.eventually.eql({ data: true, airplaneMode: false});
});
});
describe('toggleData', function () {
it('should toggle data', async function () {
adb.isDataOn.returns(false);
Expand Down