forked from appium/java-client
/
AndroidMobileCommandHelper.java
320 lines (291 loc) · 14.5 KB
/
AndroidMobileCommandHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.appium.java_client.android;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.collect.ImmutableMap;
import io.appium.java_client.MobileCommand;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.internal.HasIdentity;
import java.util.AbstractMap;
import java.util.Map;
/**
* This util class helps to prepare parameters of Android-specific JSONWP
* commands.
*/
public class AndroidMobileCommandHelper extends MobileCommand {
/**
* This method forms a {@link java.util.Map} of parameters for the
* getting of the current activity.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> currentActivityCommand() {
return new AbstractMap.SimpleEntry<>(
CURRENT_ACTIVITY, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* getting of the current package.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> currentPackageCommand() {
return new AbstractMap.SimpleEntry<>(
GET_CURRENT_PACKAGE, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* ending of the test coverage.
*
* @param intent intent to broadcast.
* @param path path to .ec file.
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> endTestCoverageCommand(String intent,
String path) {
String[] parameters = new String[] {"intent", "path"};
Object[] values = new Object[] {intent, path};
return new AbstractMap.SimpleEntry<>(
END_TEST_COVERAGE, prepareArguments(parameters, values));
}
/**
* returns the information type of the system state which is supported to read
* as like cpu, memory, network traffic, and battery.
* @return output - array like below
* [cpuinfo, batteryinfo, networkinfo, memoryinfo]
*
*/
public static Map.Entry<String, Map<String, ?>> getSupportedPerformanceDataTypesCommand() {
return new AbstractMap.SimpleEntry<>(
GET_SUPPORTED_PERFORMANCE_DATA_TYPES, ImmutableMap.<String, Object>of());
}
/**
* returns the resource usage information of the application. the resource is one of the system state
* which means cpu, memory, network traffic, and battery.
*
* @param packageName the package name of the application
* @param dataType the type of system state which wants to read.
* It should be one of the supported performance data types,
* the return value of the function "getSupportedPerformanceDataTypes"
* @param dataReadTimeout the number of attempts to read
* @return table of the performance data, The first line of the table represents the type of data.
* The remaining lines represent the values of the data.
* in case of battery info : [[power], [23]]
* in case of memory info :
* [[totalPrivateDirty, nativePrivateDirty, dalvikPrivateDirty, eglPrivateDirty, glPrivateDirty,
* totalPss, nativePss, dalvikPss, eglPss, glPss, nativeHeapAllocatedSize, nativeHeapSize],
* [18360, 8296, 6132, null, null, 42588, 8406, 7024, null, null, 26519, 10344]]
* in case of network info :
* [[bucketStart, activeTime, rxBytes, rxPackets, txBytes, txPackets, operations, bucketDuration,],
* [1478091600000, null, 1099075, 610947, 928, 114362, 769, 0, 3600000],
* [1478095200000, null, 1306300, 405997, 509, 46359, 370, 0, 3600000]]
* in case of network info :
* [[st, activeTime, rb, rp, tb, tp, op, bucketDuration],
* [1478088000, null, null, 32115296, 34291, 2956805, 25705, 0, 3600],
* [1478091600, null, null, 2714683, 11821, 1420564, 12650, 0, 3600],
* [1478095200, null, null, 10079213, 19962, 2487705, 20015, 0, 3600],
* [1478098800, null, null, 4444433, 10227, 1430356, 10493, 0, 3600]]
* in case of cpu info : [[user, kernel], [0.9, 1.3]]
* @throws Exception if the performance data type is not supported, thows Error
*/
public static Map.Entry<String, Map<String, ?>> getPerformanceDataCommand(
String packageName, String dataType, int dataReadTimeout) throws Exception {
String[] parameters = new String[] {"packageName", "dataType", "dataReadTimeout"};
Object[] values = new Object[] {packageName, dataType, dataReadTimeout};
return new AbstractMap.SimpleEntry<>(
GET_PERFORMANCE_DATA, prepareArguments(parameters, values));
}
/**
* This method forms a {@link java.util.Map} of parameters to
* Retrieve the display density of the Android device.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> getDisplayDensityCommand() {
return new AbstractMap.SimpleEntry<>(
GET_DISPLAY_DENSITY, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* getting of a network connection value.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> getNetworkConnectionCommand() {
return new AbstractMap.SimpleEntry<>(
GET_NETWORK_CONNECTION, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters to
* Retrieve visibility and bounds information of the status and navigation bars.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> getSystemBarsCommand() {
return new AbstractMap.SimpleEntry<>(
GET_SYSTEM_BARS, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* checking of the keyboard state (is it shown or not).
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> isKeyboardShownCommand() {
return new AbstractMap.SimpleEntry<>(
IS_KEYBOARD_SHOWN, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* checking of the device state (is it locked or not).
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> isLockedCommand() {
return new AbstractMap.SimpleEntry<>(
IS_LOCKED, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* notification opening.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> openNotificationsCommand() {
return new AbstractMap.SimpleEntry<>(
OPEN_NOTIFICATIONS, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* file pushing
*
* @param remotePath Path to file to write data to on remote device
* @param base64Data Base64 encoded byte array of data to write to remote device
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> pushFileCommandCommand(String remotePath,
byte[] base64Data) {
String[] parameters = new String[] {"path", "data"};
Object[] values = new Object[] {remotePath, base64Data};
return new AbstractMap.SimpleEntry<>(PUSH_FILE, prepareArguments(parameters, values));
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* setting of device network connection.
*
* @param connection The bitmask of the desired connection
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> setConnectionCommand(Connection connection) {
String[] parameters = new String[] {"name", "parameters"};
Object[] values =
new Object[] {"network_connection", ImmutableMap.of("type", connection.bitMask)};
return new AbstractMap.SimpleEntry<>(
SET_NETWORK_CONNECTION, prepareArguments(parameters, values));
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* activity starting.
*
* @param appPackage The package containing the activity. [Required]
* @param appActivity The activity to start. [Required]
* @param appWaitPackage Automation will begin after this package starts. [Optional]
* @param appWaitActivity Automation will begin after this activity starts. [Optional]
* @param intentAction Intent action which will be used to start activity [Optional]
* @param intentCategory Intent category which will be used to start activity [Optional]
* @param intentFlags Flags that will be used to start activity [Optional]
* @param optionalIntentArguments Additional intent arguments that will be used to
* start activity [Optional]
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
* @throws IllegalArgumentException when any required argument is empty
*/
public static Map.Entry<String, Map<String, ?>> startActivityCommand(String appPackage,
String appActivity, String appWaitPackage, String appWaitActivity,
String intentAction, String intentCategory, String intentFlags,
String optionalIntentArguments, boolean stopApp) throws IllegalArgumentException {
checkArgument((!StringUtils.isBlank(appPackage)
&& !StringUtils.isBlank(appActivity)),
String.format("'%s' and '%s' are required.", "appPackage", "appActivity"));
String targetWaitPackage = !StringUtils.isBlank(appWaitPackage) ? appWaitPackage : "";
String targetWaitActivity = !StringUtils.isBlank(appWaitActivity) ? appWaitActivity : "";
String targetIntentAction = !StringUtils.isBlank(intentAction) ? intentAction : "";
String targetIntentCategory = !StringUtils.isBlank(intentCategory) ? intentCategory : "";
String targetIntentFlags = !StringUtils.isBlank(intentFlags) ? intentFlags : "";
String targetOptionalIntentArguments = !StringUtils.isBlank(optionalIntentArguments)
? optionalIntentArguments : "";
ImmutableMap<String, ?> parameters = ImmutableMap
.<String, Object>builder().put("appPackage", appPackage)
.put("appActivity", appActivity)
.put("appWaitPackage", targetWaitPackage)
.put("appWaitActivity", targetWaitActivity)
.put("dontStopAppOnReset", !stopApp)
.put("intentAction", targetIntentAction)
.put("intentCategory", targetIntentCategory)
.put("intentFlags", targetIntentFlags)
.put("optionalIntentArguments", targetOptionalIntentArguments)
.build();
return new AbstractMap.SimpleEntry<>(START_ACTIVITY, parameters);
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* toggling of location services.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> toggleLocationServicesCommand() {
return new AbstractMap.SimpleEntry<>(TOGGLE_LOCATION_SERVICES, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the
* device unlocking.
*
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> unlockCommand() {
return new AbstractMap.SimpleEntry<>(UNLOCK, ImmutableMap.<String, Object>of());
}
/**
* This method forms a {@link java.util.Map} of parameters for the element
* value replacement. It is used against input elements
*
* @param hasIdentityObject an instance which contains an element ID
* @param value a new value
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> replaceElementValueCommand(
HasIdentity hasIdentityObject, String value) {
String[] parameters = new String[] {"id", "value"};
Object[] values =
new Object[] {hasIdentityObject.getId(), value};
return new AbstractMap.SimpleEntry<>(
REPLACE_VALUE, prepareArguments(parameters, values));
}
}