1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-03-21 01:59:13 +00:00

Tidy up WA UiAutomator bass classes

Minor code maintenance for base classes BaseUiAutomation and
UxPerfUiAutomation. Changes include:

- Prefer static calls for UiDevice methods where possible
- Rename inconsistently named variables
- Avoid long wrapped lines where possible
- Remove unused imports
- Fix whitespace around operators
This commit is contained in:
John Richardson 2016-06-01 10:55:10 +01:00
parent 6b65e9ac86
commit 4b2417a996
15 changed files with 99 additions and 78 deletions

View File

@ -31,11 +31,13 @@ import android.graphics.Rect;
import com.android.uiautomator.core.UiObject; import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException; import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase; import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class BaseUiAutomation extends UiAutomatorTestCase { public class BaseUiAutomation extends UiAutomatorTestCase {
public long waitTimeout = TimeUnit.SECONDS.toMillis(4); public long timeout = TimeUnit.SECONDS.toMillis(4);
public enum ScreenOrientation { RIGHT, NATURAL, LEFT }; public enum ScreenOrientation { RIGHT, NATURAL, LEFT };
public static final int CLICK_REPEAT_INTERVAL_MINIMUM = 5; public static final int CLICK_REPEAT_INTERVAL_MINIMUM = 5;
@ -52,11 +54,11 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
public boolean takeScreenshot(String name) { public boolean takeScreenshot(String name) {
Bundle params = getParams(); Bundle params = getParams();
String png_dir = params.getString("workdir"); String pngDir = params.getString("workdir");
try { try {
return getUiDevice().takeScreenshot(new File(png_dir, name + ".png")); return getUiDevice().takeScreenshot(new File(pngDir, name + ".png"));
} catch(NoSuchMethodError e) { } catch (NoSuchMethodError e) {
return true; return true;
} }
} }
@ -67,9 +69,9 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
public void waitText(String text, int second) throws UiObjectNotFoundException { public void waitText(String text, int second) throws UiObjectNotFoundException {
UiSelector selector = new UiSelector(); UiSelector selector = new UiSelector();
UiObject text_obj = new UiObject(selector.text(text) UiObject textObj = new UiObject(selector.text(text)
.className("android.widget.TextView")); .className("android.widget.TextView"));
waitObject(text_obj, second); waitObject(textObj, second);
} }
public void waitObject(UiObject obj) throws UiObjectNotFoundException { public void waitObject(UiObject obj) throws UiObjectNotFoundException {
@ -77,7 +79,7 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
} }
public void waitObject(UiObject obj, int second) throws UiObjectNotFoundException { public void waitObject(UiObject obj, int second) throws UiObjectNotFoundException {
if (! obj.waitForExists(second * 1000)){ if (!obj.waitForExists(second * 1000)) {
throw new UiObjectNotFoundException("UiObject is not found: " throw new UiObjectNotFoundException("UiObject is not found: "
+ obj.getSelector().toString()); + obj.getSelector().toString());
} }
@ -99,10 +101,10 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
boolean found = false; boolean found = false;
while ((currentTime - startTime) < timeout){ while ((currentTime - startTime) < timeout) {
sleep(2); // poll every two seconds sleep(2); // poll every two seconds
while((line=reader.readLine())!=null) { while ((line = reader.readLine()) != null) {
if (line.contains(searchText)) { if (line.contains(searchText)) {
found = true; found = true;
break; break;
@ -118,74 +120,75 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
process.destroy(); process.destroy();
if ((currentTime - startTime) >= timeout) { if ((currentTime - startTime) >= timeout) {
throw new TimeoutException("Timed out waiting for Logcat text \"%s\"".format(searchText)); throw new TimeoutException(String.format("Timed out waiting for Logcat text \"%s\"",
searchText));
} }
} }
public UiObject getUiObjectByResourceId(String resourceId, String className) throws Exception { public UiObject getUiObjectByResourceId(String resourceId, String className) throws Exception {
UiObject object = new UiObject(new UiSelector().resourceId(resourceId) UiObject object = new UiObject(new UiSelector().resourceId(resourceId)
.className(className)); .className(className));
if (!object.waitForExists(waitTimeout)) { if (!object.waitForExists(timeout)) {
throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"", throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"",
resourceId, className)); resourceId, className));
}; }
return object; return object;
} }
public UiObject getUiObjectByDescription(String description, String className) throws Exception { public UiObject getUiObjectByDescription(String description, String className) throws Exception {
UiObject object = new UiObject(new UiSelector().descriptionContains(description) UiObject object = new UiObject(new UiSelector().descriptionContains(description)
.className(className)); .className(className));
if (!object.waitForExists(waitTimeout)) { if (!object.waitForExists(timeout)) {
throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"", throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"",
description, className)); description, className));
}; }
return object; return object;
} }
public UiObject getUiObjectByText(String text, String className) throws Exception { public UiObject getUiObjectByText(String text, String className) throws Exception {
UiObject object = new UiObject(new UiSelector().textContains(text) UiObject object = new UiObject(new UiSelector().textContains(text)
.className(className)); .className(className));
if (!object.waitForExists(waitTimeout)) { if (!object.waitForExists(timeout)) {
throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"", throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"",
text, className)); text, className));
}; }
return object; return object;
} }
public void pressEnter() { public void pressEnter() {
getUiDevice().getInstance().pressEnter(); UiDevice.getInstance().pressEnter();
} }
public void pressBack() { public void pressBack() {
getUiDevice().getInstance().pressBack(); UiDevice.getInstance().pressBack();
} }
public int getDisplayHeight () { public int getDisplayHeight() {
return getUiDevice().getInstance().getDisplayHeight(); return UiDevice.getInstance().getDisplayHeight();
} }
public int getDisplayWidth () { public int getDisplayWidth() {
return getUiDevice().getInstance().getDisplayWidth(); return UiDevice.getInstance().getDisplayWidth();
} }
public int getDisplayCentreWidth () { public int getDisplayCentreWidth() {
return getDisplayWidth() / 2; return getDisplayWidth() / 2;
} }
public int getDisplayCentreHeight () { public int getDisplayCentreHeight() {
return getDisplayHeight() / 2; return getDisplayHeight() / 2;
} }
public void tapDisplayCentre () { public void tapDisplayCentre() {
tapDisplay(getDisplayCentreWidth(), getDisplayCentreHeight()); tapDisplay(getDisplayCentreWidth(), getDisplayCentreHeight());
} }
public void tapDisplay (int x, int y) { public void tapDisplay(int x, int y) {
getUiDevice().getInstance().click(x, y); UiDevice.getInstance().click(x, y);
} }
public void uiDeviceSwipeUp (int steps) { public void uiDeviceSwipeUp(int steps) {
getUiDevice().getInstance().swipe( UiDevice.getInstance().swipe(
getDisplayCentreWidth(), getDisplayCentreWidth(),
(getDisplayCentreHeight() / 2), (getDisplayCentreHeight() / 2),
getDisplayCentreWidth(), getDisplayCentreWidth(),
@ -193,8 +196,8 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
steps); steps);
} }
public void uiDeviceSwipeDown (int steps) { public void uiDeviceSwipeDown(int steps) {
getUiDevice().getInstance().swipe( UiDevice.getInstance().swipe(
getDisplayCentreWidth(), getDisplayCentreWidth(),
(getDisplayCentreHeight() + (getDisplayCentreHeight() / 2)), (getDisplayCentreHeight() + (getDisplayCentreHeight() / 2)),
getDisplayCentreWidth(), getDisplayCentreWidth(),
@ -202,8 +205,8 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
steps); steps);
} }
public void uiDeviceSwipeLeft (int steps) { public void uiDeviceSwipeLeft(int steps) {
getUiDevice().getInstance().swipe( UiDevice.getInstance().swipe(
(getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)), (getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)),
getDisplayCentreHeight(), getDisplayCentreHeight(),
(getDisplayCentreWidth() / 2), (getDisplayCentreWidth() / 2),
@ -211,8 +214,8 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
steps); steps);
} }
public void uiDeviceSwipeRight (int steps) { public void uiDeviceSwipeRight(int steps) {
getUiDevice().getInstance().swipe( UiDevice.getInstance().swipe(
(getDisplayCentreWidth() / 2), (getDisplayCentreWidth() / 2),
getDisplayCentreHeight(), getDisplayCentreHeight(),
(getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)), (getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)),
@ -224,16 +227,20 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
final int FINGER_TOUCH_HALF_WIDTH = 20; final int FINGER_TOUCH_HALF_WIDTH = 20;
// Make value between 1 and 100 // Make value between 1 and 100
percent = (percent < 0) ? 1 : (percent > 100) ? 100 : percent; int nPercent = (percent < 0) ? 1 : (percent > 100) ? 100 : percent;
float percentage = percent / 100f; float percentage = nPercent / 100f;
Rect rect = view.getVisibleBounds(); Rect rect = view.getVisibleBounds();
if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2)
if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2) {
throw new IllegalStateException("Object width is too small for operation"); throw new IllegalStateException("Object width is too small for operation");
}
// Start at the top-center and bottom-center of the control // Start at the top-center and bottom-center of the control
Point startPoint1 = new Point(rect.centerX(), rect.centerY() + (int) ((rect.height() / 2) * percentage)); Point startPoint1 = new Point(rect.centerX(), rect.centerY()
Point startPoint2 = new Point(rect.centerX(), rect.centerY() - (int) ((rect.height() / 2) * percentage)); + (int) ((rect.height() / 2) * percentage));
Point startPoint2 = new Point(rect.centerX(), rect.centerY()
- (int) ((rect.height() / 2) * percentage));
// End at the same point at the center of the control // End at the same point at the center of the control
Point endPoint1 = new Point(rect.centerX(), rect.centerY() + FINGER_TOUCH_HALF_WIDTH); Point endPoint1 = new Point(rect.centerX(), rect.centerY() + FINGER_TOUCH_HALF_WIDTH);
@ -246,20 +253,24 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
final int FINGER_TOUCH_HALF_WIDTH = 20; final int FINGER_TOUCH_HALF_WIDTH = 20;
// Make value between 1 and 100 // Make value between 1 and 100
percent = (percent < 0) ? 1 : (percent > 100) ? 100 : percent; int nPercent = (percent < 0) ? 1 : (percent > 100) ? 100 : percent;
float percentage = percent / 100f; float percentage = nPercent / 100f;
Rect rect = view.getVisibleBounds(); Rect rect = view.getVisibleBounds();
if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2)
if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2) {
throw new IllegalStateException("Object width is too small for operation"); throw new IllegalStateException("Object width is too small for operation");
}
// Start from the same point at the center of the control // Start from the same point at the center of the control
Point startPoint1 = new Point(rect.centerX(), rect.centerY() + FINGER_TOUCH_HALF_WIDTH); Point startPoint1 = new Point(rect.centerX(), rect.centerY() + FINGER_TOUCH_HALF_WIDTH);
Point startPoint2 = new Point(rect.centerX(), rect.centerY() - FINGER_TOUCH_HALF_WIDTH); Point startPoint2 = new Point(rect.centerX(), rect.centerY() - FINGER_TOUCH_HALF_WIDTH);
// End at the top-center and bottom-center of the control // End at the top-center and bottom-center of the control
Point endPoint1 = new Point(rect.centerX(), rect.centerY() + (int) ((rect.height() / 2) * percentage)); Point endPoint1 = new Point(rect.centerX(), rect.centerY()
Point endPoint2 = new Point(rect.centerX(), rect.centerY() - (int) ((rect.height() / 2) * percentage)); + (int) ((rect.height() / 2) * percentage));
Point endPoint2 = new Point(rect.centerX(), rect.centerY()
- (int) ((rect.height() / 2) * percentage));
view.performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, steps); view.performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, steps);
} }
@ -286,7 +297,7 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
public void uiDevicePerformLongClick(UiObject view, int steps) throws Exception { public void uiDevicePerformLongClick(UiObject view, int steps) throws Exception {
Rect rect = view.getBounds(); Rect rect = view.getBounds();
getUiDevice().getInstance().swipe(rect.centerX(), rect.centerY(), UiDevice.getInstance().swipe(rect.centerX(), rect.centerY(),
rect.centerX(), rect.centerY(), steps); rect.centerX(), rect.centerY(), steps);
} }
@ -299,11 +310,13 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
} }
public void repeatClickUiObject(UiObject view, int repeatCount, int intervalInMillis) throws Exception { public void repeatClickUiObject(UiObject view, int repeatCount, int intervalInMillis) throws Exception {
int repeatInterval = intervalInMillis > CLICK_REPEAT_INTERVAL_MINIMUM ? intervalInMillis : CLICK_REPEAT_INTERVAL_DEFAULT; int repeatInterval = intervalInMillis > CLICK_REPEAT_INTERVAL_MINIMUM
? intervalInMillis : CLICK_REPEAT_INTERVAL_DEFAULT;
if (repeatCount < 1 || !view.isClickable()) { if (repeatCount < 1 || !view.isClickable()) {
return; return;
} }
while (repeatCount-- > 0) {
for (int i = 0; i < repeatCount; ++i) {
view.click(); view.click();
SystemClock.sleep(repeatInterval); // in order to register as separate click SystemClock.sleep(repeatInterval); // in order to register as separate click
} }
@ -323,18 +336,23 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
public UiObject clickUiObject(FindByCriteria criteria, String matching, String clazz, boolean wait) throws Exception { public UiObject clickUiObject(FindByCriteria criteria, String matching, String clazz, boolean wait) throws Exception {
UiObject view; UiObject view;
switch (criteria) { switch (criteria) {
case BY_ID: case BY_ID:
view = clazz == null ? getUiObjectByResourceId(matching) : getUiObjectByResourceId(matching, clazz); view = (clazz == null)
? getUiObjectByResourceId(matching) : getUiObjectByResourceId(matching, clazz);
break; break;
case BY_DESC: case BY_DESC:
view = clazz == null ? getUiObjectByDescription(matching) : getUiObjectByDescription(matching, clazz); view = (clazz == null)
? getUiObjectByDescription(matching) : getUiObjectByDescription(matching, clazz);
break; break;
case BY_TEXT: case BY_TEXT:
default: default:
view = clazz == null ? getUiObjectByText(matching) : getUiObjectByText(matching, clazz); view = (clazz == null)
? getUiObjectByText(matching) : getUiObjectByText(matching, clazz);
break; break;
} }
if (wait) { if (wait) {
view.clickAndWaitForNewWindow(); view.clickAndWaitForNewWindow();
} else { } else {
@ -345,26 +363,28 @@ public class BaseUiAutomation extends UiAutomatorTestCase {
public UiObject getUiObjectByText(String text) throws Exception { public UiObject getUiObjectByText(String text) throws Exception {
UiObject object = new UiObject(new UiSelector().textContains(text)); UiObject object = new UiObject(new UiSelector().textContains(text));
if (!object.waitForExists(waitTimeout)) {
if (!object.waitForExists(timeout)) {
throw new UiObjectNotFoundException("Could not find view with text: " + text); throw new UiObjectNotFoundException("Could not find view with text: " + text);
}; }
return object; return object;
} }
public UiObject getUiObjectByDescription(String desc) throws Exception { public UiObject getUiObjectByDescription(String desc) throws Exception {
UiObject object = new UiObject(new UiSelector().descriptionContains(desc)); UiObject object = new UiObject(new UiSelector().descriptionContains(desc));
if (!object.waitForExists(waitTimeout)) {
if (!object.waitForExists(timeout)) {
throw new UiObjectNotFoundException("Could not find view with description: " + desc); throw new UiObjectNotFoundException("Could not find view with description: " + desc);
}; }
return object; return object;
} }
public UiObject getUiObjectByResourceId(String id) throws Exception { public UiObject getUiObjectByResourceId(String id) throws Exception {
UiObject object = new UiObject(new UiSelector().resourceId(id)); UiObject object = new UiObject(new UiSelector().resourceId(id));
if (!object.waitForExists(waitTimeout)) {
if (!object.waitForExists(timeout)) {
throw new UiObjectNotFoundException("Could not find view with resource ID: " + id); throw new UiObjectNotFoundException("Could not find view with resource ID: " + id);
}; }
return object; return object;
} }
} }

View File

@ -16,18 +16,13 @@
package com.arm.wlauto.uiauto; package com.arm.wlauto.uiauto;
import android.os.Build; import android.os.Build;
import android.os.SystemClock;
import android.os.Bundle; import android.os.Bundle;
import android.util.Pair; import android.util.Pair;
import com.android.uiautomator.core.UiObject; import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.core.UiSelector;
import com.arm.wlauto.uiauto.BaseUiAutomation;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
@ -45,7 +40,6 @@ import java.util.Map.Entry;
public class UxPerfUiAutomation extends BaseUiAutomation { public class UxPerfUiAutomation extends BaseUiAutomation {
private Logger logger = Logger.getLogger(UxPerfUiAutomation.class.getName()); private Logger logger = Logger.getLogger(UxPerfUiAutomation.class.getName());
public long timeout = TimeUnit.SECONDS.toMillis(4);
public enum Direction { UP, DOWN, LEFT, RIGHT, NULL }; public enum Direction { UP, DOWN, LEFT, RIGHT, NULL };
public enum GestureType { UIDEVICE_SWIPE, UIOBJECT_SWIPE, PINCH }; public enum GestureType { UIDEVICE_SWIPE, UIOBJECT_SWIPE, PINCH };
@ -56,7 +50,7 @@ public class UxPerfUiAutomation extends BaseUiAutomation {
private long endTime = 0; private long endTime = 0;
private long duration = 0; private long duration = 0;
public void start(){ public void start() {
this.startTime = System.currentTimeMillis(); this.startTime = System.currentTimeMillis();
} }
@ -152,8 +146,7 @@ public class UxPerfUiAutomation extends BaseUiAutomation {
return executeCommand(command, false); return executeCommand(command, false);
} }
public Pair<Integer, String> executeCommand(List<String> command, boolean readOutput) public Pair<Integer, String> executeCommand(List<String> command, boolean readOutput) {
{
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
ProcessBuilder processBuilder = new ProcessBuilder(); ProcessBuilder processBuilder = new ProcessBuilder();
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
@ -210,17 +203,25 @@ public class UxPerfUiAutomation extends BaseUiAutomation {
} }
process.waitFor(); process.waitFor();
if (process.exitValue() != 0) { if (process.exitValue() != 0) {
throw new Exception("Error while taking dumpsys, exitCode=" + throw new Exception("Error while taking dumpsys, exitCode="
process.exitValue()); + process.exitValue());
} }
} catch (Exception exception) { } catch (Exception exception) {
logger.log(Level.SEVERE, "Unable to take a dumpsys", exception); logger.log(Level.SEVERE, "Unable to take a dumpsys", exception);
} finally { } finally {
if (fileWriter != null) { if (fileWriter != null) {
try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); } try {
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
if (bufferedReader != null) { if (bufferedReader != null) {
try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } try {
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
} }
} }
@ -276,7 +277,7 @@ public class UxPerfUiAutomation extends BaseUiAutomation {
} }
public Timer uiObjectPinchTest(UiObject view, PinchType direction, int steps, public Timer uiObjectPinchTest(UiObject view, PinchType direction, int steps,
int percent) throws Exception { int percent) throws Exception {
Timer results = new Timer(); Timer results = new Timer();
results.start(); results.start();
if (direction.equals(PinchType.IN)) { if (direction.equals(PinchType.IN)) {
@ -362,8 +363,8 @@ public class UxPerfUiAutomation extends BaseUiAutomation {
public void stopDumpsysSurfaceFlinger(Bundle parameters, String filename) throws Exception { public void stopDumpsysSurfaceFlinger(Bundle parameters, String filename) throws Exception {
if (Boolean.parseBoolean(parameters.getString("dumpsys_enabled"))) { if (Boolean.parseBoolean(parameters.getString("dumpsys_enabled"))) {
File out_file = new File(parameters.getString("output_dir"), filename); File outFile = new File(parameters.getString("output_dir"), filename);
exitDumpsysSurfaceFlinger(parameters.getString("package"), out_file); exitDumpsysSurfaceFlinger(parameters.getString("package"), outFile);
} }
} }
@ -375,8 +376,8 @@ public class UxPerfUiAutomation extends BaseUiAutomation {
public void stopDumpsysGfxInfo(Bundle parameters, String filename) throws Exception { public void stopDumpsysGfxInfo(Bundle parameters, String filename) throws Exception {
if (Boolean.parseBoolean(parameters.getString("dumpsys_enabled"))) { if (Boolean.parseBoolean(parameters.getString("dumpsys_enabled"))) {
File out_file = new File(parameters.getString("output_dir"), filename); File outFile = new File(parameters.getString("output_dir"), filename);
exitDumpsysGfxInfo(parameters.getString("package"), out_file); exitDumpsysGfxInfo(parameters.getString("package"), outFile);
} }
} }
} }

View File

@ -49,7 +49,7 @@ public class UiAutomation extends UxPerfUiAutomation {
public void runUiAutomation() throws Exception { public void runUiAutomation() throws Exception {
// Override superclass value // Override superclass value
this.waitTimeout = TimeUnit.SECONDS.toMillis(10); this.timeout = TimeUnit.SECONDS.toMillis(10);
// Get Params // Get Params
Bundle parameters = getParams(); Bundle parameters = getParams();