package com.mumux.androidtesting.actions;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.mumux.androidtesting.actions.argument.ActionArgument;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public abstract class Action implements Cloneable {
// region Custom Types
public enum Category {
UI, TEST, SYSTEM
}
// endregion
// region Private fields
private String name;
private boolean rootRequired;
private String description;
private Category category;
private String failure;
private boolean optional = false;
// endregion
// region Constructor
protected Action() {
}
public Action(String name, Boolean rootRequired, String description, Category category, String failure) {
this.name = name;
this.rootRequired = rootRequired;
this.description = description;
this.category = category;
this.failure = failure;
}
// endregion
// region Accessors
public String getName() {
return name;
}
public boolean isRootRequired() {
return rootRequired;
}
private String getDescription() {
return description;
}
private Category getCategory() {
return category;
}
// todo - dirty to be refactor
public void parseValues(String... values) throws ActionParseException {
ActionArgument[] arguments = getArguments();
if (arguments.length != values.length) {
throw new ActionParseException("Invalid number of arguments for action " + getName());
}
for (int i = 0; i < arguments.length; i++) {
try {
arguments[i].parseValue(values[i]);
} catch (Exception e) {
throw new ActionParseException(e.getMessage());
}
}
List<Object> vals = new ArrayList<>();
for (ActionArgument argument : arguments) {
vals.add(argument.getValue());
}
setValues(vals.toArray(new Object[0]));
}
public void setValues(Object[] values) {
}
String[] getStringValues() {
List<String> values = new ArrayList<>();
for (ActionArgument argument : getArguments()) {
values.add(argument.getStringValue());
}
return values.toArray(new String[0]);
}
protected ActionArgument[] getArguments() {
return new ActionArgument[]{};
}
void setOptional(boolean optional) {
this.optional = optional;
}
boolean isOptional() {
return optional;
}
Action deepcopy() {
try {
return (Action) this.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
// endregion
// region toJson/toString()
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
try {
if (isRootRequired()) {
jsonObject.put("root", true);
}
if (optional) {
jsonObject.put("optional", true);
}
jsonObject.put("name", getName());
if (getArguments().length > 0) {
JSONArray jsonArray = new JSONArray();
for (ActionArgument argument : getArguments()) {
jsonArray.put(argument.toJson());
}
jsonObject.put("arguments", jsonArray);
}
jsonObject.put("description", getDescription());
jsonObject.put("category", getCategory().toString());
if (failure != null) {
jsonObject.put("failure", failure);
}
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
// as defined in a test
public String toString() {
String s = getName();
for (String arg : getStringValues()) {
s += " ";
if (arg.contains(" ")) {
s += "\"";
}
s += arg;
if (arg.contains(" ")) {
s += "\"";
}
}
if (optional) {
s = "(" + s + ")";
}
return s;
}
// endregion
// return Error String like go
public abstract String run(UiAutomatorTestCase uiAutomatorTestCase, Runtime runtime);
/*
public abstract void run(UiAutomatorTestCase uiAutomatorTestCase) throws ActionException;
// Optional does not forward exceptions
public void runWithRetries(UiAutomatorTestCase uiAutomatorTestCase, int retry) throws ActionException {
while (retry > 0) {
try {
run(uiAutomatorTestCase);
retry = 0;
} catch (ActionException e) {
e.printStackTrace();
if (optional) {
return;
}
if (retry == 1) {
throw e;
}
System.err.println("retry");
retry--;
}
}
}
*/
}