Skip to content

Commit 5ac71ab

Browse files
Add handlers for gsm, network, power and sms command (#834)
* Add handlers for gsm, network, power and sms command * fixed review comments * make codacy happy * update as per review comments * Fixed review comments
1 parent 30b4e38 commit 5ac71ab

File tree

10 files changed

+285
-2
lines changed

10 files changed

+285
-2
lines changed

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ public class MobileCommand {
9191
protected static final String GET_SETTINGS;
9292
protected static final String SET_SETTINGS;
9393
protected static final String GET_CURRENT_PACKAGE;
94+
protected static final String SEND_SMS;
95+
protected static final String GSM_CALL;
96+
protected static final String GSM_SIGNAL;
97+
protected static final String GSM_VOICE;
98+
protected static final String NETWORK_SPEED;
99+
protected static final String POWER_CAPACITY;
100+
protected static final String POWER_AC_STATE;
94101

95102
public static final Map<String, CommandInfo> commandRepository;
96103

@@ -149,6 +156,13 @@ public class MobileCommand {
149156
GET_SETTINGS = "getSettings";
150157
SET_SETTINGS = "setSettings";
151158
GET_CURRENT_PACKAGE = "getCurrentPackage";
159+
SEND_SMS = "sendSMS";
160+
GSM_CALL = "gsmCall";
161+
GSM_SIGNAL = "gsmSignal";
162+
GSM_VOICE = "gsmVoice";
163+
NETWORK_SPEED = "networkSpeed";
164+
POWER_CAPACITY = "powerCapacity";
165+
POWER_AC_STATE = "powerAC";
152166

153167
commandRepository = new HashMap<>();
154168
commandRepository.put(RESET, postC("/session/:sessionId/appium/app/reset"));
@@ -217,6 +231,13 @@ public class MobileCommand {
217231
commandRepository.put(UNLOCK, postC("/session/:sessionId/appium/device/unlock"));
218232
commandRepository.put(REPLACE_VALUE, postC("/session/:sessionId/appium/element/:id/replace_value"));
219233
commandRepository.put(GET_CURRENT_PACKAGE, getC("/session/:sessionId/appium/device/current_package"));
234+
commandRepository.put(SEND_SMS, postC("/session/:sessionId/appium/device/send_sms"));
235+
commandRepository.put(GSM_CALL, postC("/session/:sessionId/appium/device/gsm_call"));
236+
commandRepository.put(GSM_SIGNAL, postC("/session/:sessionId/appium/device/gsm_signal"));
237+
commandRepository.put(GSM_VOICE, postC("/session/:sessionId/appium/device/gsm_voice"));
238+
commandRepository.put(NETWORK_SPEED, postC("/session/:sessionId/appium/device/network_speed"));
239+
commandRepository.put(POWER_CAPACITY, postC("/session/:sessionId/appium/device/power_capacity"));
240+
commandRepository.put(POWER_AC_STATE, postC("/session/:sessionId/appium/device/power_ac"));
220241
}
221242

222243
/**

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public class AndroidDriver<T extends WebElement>
5151
extends AppiumDriver<T>
5252
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
5353
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
54-
HasSupportedPerformanceDataType, AuthenticatesByFinger, CanRecordScreen {
54+
HasSupportedPerformanceDataType, AuthenticatesByFinger,
55+
CanRecordScreen, SupportsSpecialEmulatorCommands {
5556

5657
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
5758

@@ -175,5 +176,4 @@ public void openNotifications() {
175176
public void toggleLocationServices() {
176177
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
177178
}
178-
179179
}

src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,109 @@ public class AndroidMobileCommandHelper extends MobileCommand {
291291
return new AbstractMap.SimpleEntry<>(
292292
REPLACE_VALUE, prepareArguments(parameters, values));
293293
}
294+
295+
/**
296+
* This method forms a {@link Map} of parameters for the element
297+
* value replacement. It is used against input elements
298+
*
299+
* @param phoneNumber The phone number of message sender
300+
* @param message The message content
301+
*
302+
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
303+
*/
304+
public static Map.Entry<String, Map<String, ?>> sendSMSCommand(
305+
String phoneNumber, String message) {
306+
ImmutableMap<String, ?> parameters = ImmutableMap
307+
.<String, Object>builder().put("phoneNumber", phoneNumber)
308+
.put("message", message)
309+
.build();
310+
311+
return new AbstractMap.SimpleEntry<>(SEND_SMS, parameters);
312+
}
313+
314+
/**
315+
* This method forms a {@link Map} of parameters for the element
316+
* value replacement. It is used against input elements
317+
*
318+
* @param phoneNumber The phone number of message sender
319+
* @param gsmCallActions One of available GSM call actions
320+
*
321+
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
322+
*/
323+
public static Map.Entry<String, Map<String, ?>> gsmCallCommand(
324+
String phoneNumber, GsmCallActions gsmCallActions) {
325+
String[] parameters = new String[] {"phoneNumber", "action"};
326+
Object[] values = new Object[]{phoneNumber, gsmCallActions.name().toLowerCase()};
327+
return new AbstractMap.SimpleEntry<>(GSM_CALL, prepareArguments(parameters, values));
328+
}
329+
330+
/**
331+
* This method forms a {@link Map} of parameters for the element
332+
* value replacement. It is used against input elements
333+
*
334+
* @param gsmSignalStrength One of available GSM signal strength
335+
*
336+
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
337+
*/
338+
public static Map.Entry<String, Map<String, ?>> gsmSignalStrengthCommand(
339+
GsmSignalStrength gsmSignalStrength) {
340+
return new AbstractMap.SimpleEntry<>(GSM_SIGNAL,
341+
prepareArguments("signalStrengh", gsmSignalStrength.ordinal()));
342+
}
343+
344+
/**
345+
* This method forms a {@link Map} of parameters for the element
346+
* value replacement. It is used against input elements
347+
*
348+
* @param gsmVoiceState One of available GSM voice state
349+
*
350+
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
351+
*/
352+
public static Map.Entry<String, Map<String, ?>> gsmVoiceCommand(
353+
GsmVoiceState gsmVoiceState) {
354+
return new AbstractMap.SimpleEntry<>(GSM_VOICE,
355+
prepareArguments("state", gsmVoiceState.name().toLowerCase()));
356+
}
357+
358+
/**
359+
* This method forms a {@link Map} of parameters for the element
360+
* value replacement. It is used against input elements
361+
*
362+
* @param networkSpeed One of possible NETWORK_SPEED values
363+
*
364+
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
365+
*/
366+
public static Map.Entry<String, Map<String, ?>> networkSpeedCommand(
367+
NetworkSpeed networkSpeed) {
368+
return new AbstractMap.SimpleEntry<>(NETWORK_SPEED,
369+
prepareArguments("netspeed", networkSpeed.name().toLowerCase()));
370+
}
371+
372+
/**
373+
* This method forms a {@link Map} of parameters for the element
374+
* value replacement. It is used against input elements
375+
*
376+
* @param percent A number in range [0, 4]
377+
*
378+
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
379+
*/
380+
public static Map.Entry<String, Map<String, ?>> powerCapacityCommand(
381+
int percent) {
382+
return new AbstractMap.SimpleEntry<>(POWER_CAPACITY,
383+
prepareArguments("percent", percent));
384+
}
385+
386+
/**
387+
* This method forms a {@link Map} of parameters for the element
388+
* value replacement. It is used against input elements
389+
*
390+
* @param powerACState One of available power AC state
391+
*
392+
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
393+
*/
394+
public static Map.Entry<String, Map<String, ?>> powerACCommand(
395+
PowerACState powerACState) {
396+
return new AbstractMap.SimpleEntry<>(POWER_AC_STATE,
397+
prepareArguments("state", powerACState.name().toLowerCase()));
398+
}
294399
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.appium.java_client.android;
2+
3+
public enum GsmCallActions {
4+
CALL, ACCEPT, CANCEL, HOLD
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.appium.java_client.android;
2+
3+
public enum GsmSignalStrength {
4+
NONE_OR_UNKNOWN, POOR, MODERATE, GOOD, GREAT
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.appium.java_client.android;
2+
3+
public enum GsmVoiceState {
4+
ON, OFF, DENIED, SEARCHING, ROAMING, HOME, UNREGISTERED
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.appium.java_client.android;
2+
3+
public enum NetworkSpeed {
4+
GSM, SCSD, GPRS, EDGE, UMTS, HSDPA, LTE, EVDO, FULL
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.appium.java_client.android;
2+
3+
public enum PowerACState {
4+
ON, OFF
5+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.appium.java_client.android;
2+
3+
import static io.appium.java_client.android.AndroidMobileCommandHelper.gsmCallCommand;
4+
import static io.appium.java_client.android.AndroidMobileCommandHelper.gsmSignalStrengthCommand;
5+
import static io.appium.java_client.android.AndroidMobileCommandHelper.gsmVoiceCommand;
6+
import static io.appium.java_client.android.AndroidMobileCommandHelper.networkSpeedCommand;
7+
import static io.appium.java_client.android.AndroidMobileCommandHelper.powerACCommand;
8+
import static io.appium.java_client.android.AndroidMobileCommandHelper.powerCapacityCommand;
9+
import static io.appium.java_client.android.AndroidMobileCommandHelper.sendSMSCommand;
10+
11+
import io.appium.java_client.CommandExecutionHelper;
12+
import io.appium.java_client.ExecutesMethod;
13+
14+
public interface SupportsSpecialEmulatorCommands extends ExecutesMethod {
15+
16+
/**
17+
* Emulate send SMS event on the connected emulator.
18+
*
19+
* @param phoneNumber The phone number of message sender.
20+
* @param message The message content.
21+
*/
22+
default void sendSMS(String phoneNumber, String message) {
23+
CommandExecutionHelper.execute(this, sendSMSCommand(phoneNumber, message));
24+
}
25+
26+
/**
27+
* Emulate GSM call event on the connected emulator.
28+
*
29+
* @param phoneNumber The phone number of the caller.
30+
* @param gsmCallActions One of available {@link GsmCallActions} values.
31+
*/
32+
default void makeGsmCall(String phoneNumber, GsmCallActions gsmCallActions) {
33+
CommandExecutionHelper.execute(this, gsmCallCommand(phoneNumber, gsmCallActions));
34+
}
35+
36+
/**
37+
* Emulate GSM signal strength change event on the connected emulator.
38+
*
39+
* @param gsmSignalStrength One of available {@link GsmSignalStrength} values.
40+
*/
41+
default void setGsmSignalStrength(GsmSignalStrength gsmSignalStrength) {
42+
CommandExecutionHelper.execute(this, gsmSignalStrengthCommand(gsmSignalStrength));
43+
}
44+
45+
/**
46+
* Emulate GSM voice event on the connected emulator.
47+
*
48+
* @param gsmVoiceState One of available {@link GsmVoiceState} values.
49+
*/
50+
default void setGsmVoice(GsmVoiceState gsmVoiceState) {
51+
CommandExecutionHelper.execute(this, gsmVoiceCommand(gsmVoiceState));
52+
}
53+
54+
/**
55+
* Emulate network speed change event on the connected emulator.
56+
*
57+
* @param networkSpeed One of available {@link NetworkSpeed} values.
58+
*/
59+
default void setNetworkSpeed(NetworkSpeed networkSpeed) {
60+
CommandExecutionHelper.execute(this, networkSpeedCommand(networkSpeed));
61+
}
62+
63+
/**
64+
* Emulate power capacity change on the connected emulator.
65+
*
66+
* @param percent Percentage value in range [0, 100].
67+
*/
68+
default void setPowerCapacity(int percent) {
69+
CommandExecutionHelper.execute(this, powerCapacityCommand(percent));
70+
}
71+
72+
/**
73+
* Emulate power state change on the connected emulator.
74+
*
75+
* @param powerACState One of available {@link PowerACState} values.
76+
*/
77+
default void setPowerAC(PowerACState powerACState) {
78+
CommandExecutionHelper.execute(this, powerACCommand(powerACState));
79+
}
80+
81+
}

src/test/java/io/appium/java_client/android/AndroidDriverTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.assertNotNull;
2525
import static org.junit.Assert.assertThat;
2626
import static org.junit.Assert.assertTrue;
27+
import static org.junit.Assert.fail;
2728

2829
import io.appium.java_client.appmanagement.ApplicationState;
2930
import org.apache.commons.codec.binary.Base64;
@@ -41,6 +42,56 @@
4142

4243
public class AndroidDriverTest extends BaseAndroidTest {
4344

45+
@Test public void sendSMSTest() {
46+
try {
47+
driver.sendSMS("11111111", "call");
48+
} catch (Exception e) {
49+
fail("method works only in emulators");
50+
}
51+
}
52+
53+
@Test public void gsmCallTest() {
54+
try {
55+
driver.makeGsmCall("11111111", GsmCallActions.CALL);
56+
driver.makeGsmCall("11111111", GsmCallActions.ACCEPT);
57+
} catch (Exception e) {
58+
fail("method works only in emulators");
59+
}
60+
}
61+
62+
@Test public void gsmSignalStrengthTest() {
63+
try {
64+
driver.setGsmSignalStrength(GsmSignalStrength.GREAT);
65+
} catch (Exception e) {
66+
fail("method works only in emulators");
67+
}
68+
}
69+
70+
@Test public void gsmVoiceTest() {
71+
try {
72+
driver.setGsmVoice(GsmVoiceState.OFF);
73+
} catch (Exception e) {
74+
fail("method works only in emulators");
75+
}
76+
}
77+
78+
@Test public void networkSpeedTest() {
79+
try {
80+
driver.setNetworkSpeed(NetworkSpeed.EDGE);
81+
} catch (Exception e) {
82+
fail("method works only in emulators");
83+
}
84+
}
85+
86+
@Test public void powerTest() {
87+
try {
88+
driver.setPowerCapacity(100);
89+
driver.setPowerAC(PowerACState.OFF);
90+
} catch (Exception e) {
91+
fail("method works only in emulators");
92+
}
93+
}
94+
4495
@Test public void getDeviceTimeTest() {
4596
String time = driver.getDeviceTime();
4697
assertTrue(time.length() == 28);

0 commit comments

Comments
 (0)