diff --git a/src/main/java/io/appium/java_client/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index 7a87eb8a4..ed0a530fd 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -48,6 +48,10 @@ public class MobileCommand { protected static final String GET_DEVICE_TIME; protected static final String GET_SESSION; + protected static final String GET_PERFORMANCE_DATA; + protected static final String GET_SUPPORTED_PERFORMANCE_DATA_TYPES; + + protected static final String HIDE_KEYBOARD; protected static final String LOCK; //iOS @@ -92,6 +96,9 @@ public class MobileCommand { GET_DEVICE_TIME = "getDeviceTime"; GET_SESSION = "getSession"; + GET_PERFORMANCE_DATA = "getPerformanceData"; + GET_SUPPORTED_PERFORMANCE_DATA_TYPES = "getSuppportedPerformanceDataTypes"; + HIDE_KEYBOARD = "hideKeyboard"; LOCK = "lock"; SHAKE = "shake"; @@ -136,6 +143,11 @@ public class MobileCommand { commandRepository.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings")); commandRepository.put(GET_DEVICE_TIME, getC("/session/:sessionId/appium/device/system_time")); commandRepository.put(GET_SESSION,getC("/session/:sessionId/")); + commandRepository.put(GET_SUPPORTED_PERFORMANCE_DATA_TYPES, + postC("/session/:sessionId/appium/performanceData/types")); + commandRepository.put(GET_PERFORMANCE_DATA, + postC("/session/:sessionId/appium/getPerformanceData")); + //iOS commandRepository.put(SHAKE, postC("/session/:sessionId/appium/device/shake")); commandRepository.put(TOUCH_ID, postC("/session/:sessionId/appium/simulator/touch_id")); diff --git a/src/main/java/io/appium/java_client/android/AndroidDriver.java b/src/main/java/io/appium/java_client/android/AndroidDriver.java index 8b9bd93df..0b8b8497f 100644 --- a/src/main/java/io/appium/java_client/android/AndroidDriver.java +++ b/src/main/java/io/appium/java_client/android/AndroidDriver.java @@ -47,7 +47,8 @@ public class AndroidDriver extends AppiumDriver implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity, - FindsByAndroidUIAutomator, LocksAndroidDevice, HasSettings, HasDeviceDetails { + FindsByAndroidUIAutomator, LocksAndroidDevice, HasSettings, HasDeviceDetails, + HasSupportedPerformanceDataType { private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID; diff --git a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java index 4684ad7c0..55eae2f9d 100644 --- a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java +++ b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java @@ -63,6 +63,55 @@ public class AndroidMobileCommandHelper extends MobileCommand { 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> getSupportedPerformanceDataTypesCommand() { + return new AbstractMap.SimpleEntry<>( + GET_SUPPORTED_PERFORMANCE_DATA_TYPES, ImmutableMap.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> 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. diff --git a/src/main/java/io/appium/java_client/android/HasSupportedPerformanceDataType.java b/src/main/java/io/appium/java_client/android/HasSupportedPerformanceDataType.java new file mode 100644 index 000000000..e9004daf9 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/HasSupportedPerformanceDataType.java @@ -0,0 +1,61 @@ +package io.appium.java_client.android; + +import static io.appium.java_client.android.AndroidMobileCommandHelper.getPerformanceDataCommand; +import static io.appium.java_client.android.AndroidMobileCommandHelper.getSupportedPerformanceDataTypesCommand; + +import io.appium.java_client.CommandExecutionHelper; +import io.appium.java_client.ExecutesMethod; + +import java.util.List; + +/** + * + */ +public interface HasSupportedPerformanceDataType extends ExecutesMethod { + + /** + * 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] + * + */ + default List getSupportedPerformanceDataTypes() { + return CommandExecutionHelper.execute(this, getSupportedPerformanceDataTypesCommand()); + } + + /** + * 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 + */ + default List> getPerformanceData(String packageName, String dataType, int dataReadTimeout) + throws Exception { + return CommandExecutionHelper.execute(this, + getPerformanceDataCommand(packageName, dataType, dataReadTimeout)); + } +} diff --git a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java index 186bc6e47..9ae72b96c 100644 --- a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java +++ b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java @@ -29,8 +29,11 @@ import org.openqa.selenium.html5.Location; import java.io.File; +import java.util.ArrayList; +import java.util.List; import java.util.Map; + public class AndroidDriverTest extends BaseAndroidTest { @Test public void getDeviceTimeTest() { @@ -140,4 +143,43 @@ public class AndroidDriverTest extends BaseAndroidTest { assertNotNull(driver.getDisplayDensity()); assertNotEquals(0, driver.getSystemBars().size()); } + + @Test public void getSupportedPerformanceDataTypesTest() { + driver.startActivity("io.appium.android.apis", ".ApiDemos"); + + List dataTypes = new ArrayList(); + dataTypes.add("cpuinfo"); + dataTypes.add("memoryinfo"); + dataTypes.add("batteryinfo"); + dataTypes.add("networkinfo"); + + List supportedPerformanceDataTypes = driver.getSupportedPerformanceDataTypes(); + + assertEquals(4, supportedPerformanceDataTypes.size()); + + for ( int i = 0 ; i < supportedPerformanceDataTypes.size() ; ++i) { + assertEquals(dataTypes.get(i), supportedPerformanceDataTypes.get(i)); + } + + + } + + @Test public void getPerformanceDataTest() throws Exception { + driver.startActivity("io.appium.android.apis", ".ApiDemos"); + + List supportedPerformanceDataTypes = driver.getSupportedPerformanceDataTypes(); + + for (int i = 0 ; i < supportedPerformanceDataTypes.size() ; ++ i) { + + String dataType = supportedPerformanceDataTypes.get(i); + + List> valueTable = driver.getPerformanceData("com.example.android.apis", dataType, 60000); + + for ( int j = 1 ; j < valueTable.size() ; ++ j) { + assertEquals(valueTable.subList(0,0).size(), valueTable.subList(j, j).size()); + } + } + + } + }