package com.mumux.androidtesting.actions.impl; import com.android.uiautomator.testrunner.UiAutomatorTestCase; import com.mumux.androidtesting.actions.Action; import com.mumux.androidtesting.actions.argument.ActionArgument; import com.mumux.androidtesting.actions.argument.ArgumentType; import java.io.IOException; import static com.mumux.androidtesting.actions.impl.ActionUtils.getString; public class KeyAction extends Action { private String key = null; public KeyAction() { super("KEY", false, "Send Key Event", Category.UI, null); } @Override public String run(UiAutomatorTestCase uiAutomatorTestCase, Runtime runtime) { try { //ProcessBuilder pb = new ProcessBuilder("am start " + application); ProcessBuilder pb = new ProcessBuilder("input", "keyevent", key); Process p = pb.start(); String error = getString(p.getErrorStream()); if (!error.isEmpty() && error.startsWith("Error")) { return error; } } catch (IOException e) { return e.getMessage(); } return null; } @Override public ActionArgument[] getArguments() { ActionArgument applicationArgument = new ActionArgument("key", ArgumentType.STRING); applicationArgument.setValue(key); return new ActionArgument[]{applicationArgument}; } @Override public void setValues(Object[] values) { key = values[0].toString(); } }