package com.mumux.androidtesting.actions; import com.mumux.androidtesting.actions.impl.*; import org.jetbrains.annotations.Nullable; import org.json.JSONArray; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ActionParser { // region Actions private final static List<Action> actions = new ArrayList<>(); static { // System actions.add(new PlaneAction()); actions.add(new DataAction()); actions.add(new WifiAction()); actions.add(new BackAction()); actions.add(new HomeAction()); actions.add(new KillAppAction()); actions.add(new StartAction()); // UI actions.add(new PressAction()); actions.add(new InputAction()); actions.add(new KeyAction()); // Test actions.add(new AssertExistAction()); actions.add(new TempoAction()); } // endregion // region getAction @Nullable public static Action getAction(String name) { for (Action action : actions) { if (name.equals(action.getName())) { return action.deepcopy(); } } return null; } // endregion // region getJson public static JSONArray getJson() { JSONArray jsonArray = new JSONArray(); for (Action action : actions) { jsonArray.put(action.toJson()); } return jsonArray; } // endregion // region parsing static List<String> parseTokens(String line) { line = line.split("#")[0].trim(); List<String> args = new ArrayList<>(); Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(line); while (m.find()) { String value = m.group(1); if (value.startsWith("\"")) { value = value.substring(1, value.length() - 1); } args.add(value); } return args; } public static Action parseAction(String line) throws ActionParseException { List<String> tokens = parseTokens(line); String name = tokens.remove(0); Action action = getAction(name); if (action == null) { throw new ActionParseException("Action " + name + " not found"); } action.parseValues(tokens.toArray(new String[]{})); return action; } // endregion }