package com.amplify.honeydew_server; import android.util.Log; import com.amplify.honeydew_server.actions.*; import com.android.uiautomator.core.UiDevice; import java.lang.reflect.*; import java.util.*; import static com.google.common.collect.Sets.newHashSet; public class ActionsExecutor { private final UiDevice uiDevice; private final HashMap actions; public ActionsExecutor(UiDevice uiDevice) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { this.uiDevice = uiDevice; actions = new HashMap(); for (Class actionClass : allActionClasses()) { Constructor constructor = actionClass.getConstructor(UiDevice.class); Action action = constructor.newInstance(getUiDevice()); Log.d(getClass().getName(), String.format("Registering action: %s", action.name())); actions.put(action.name(), action); } } public Result execute(Command command) { String actionName = command.getAction(); try { Action action = actions.get(actionName); if (action == null) { return new Result("Action: " + actionName + " does not exists"); } return action.execute(command.getArguments()); } catch (Exception e) { return new Result("Exception, on calling " + actionName, e); } } private static Set> allActionClasses() { Set> actionClasses = newHashSet(); actionClasses.add(LaunchApp.class); actionClasses.add(LaunchHome.class); actionClasses.add(PressBack.class); actionClasses.add(PressEnter.class); actionClasses.add(WakeUp.class); actionClasses.add(IsTextPresent.class); actionClasses.add(IsButtonPresent.class); actionClasses.add(IsElementWithNestedTextPresent.class); actionClasses.add(IsChildCountEqualTo.class); actionClasses.add(Click.class); actionClasses.add(ClickAndWaitForNewWindow.class); actionClasses.add(LongClick.class); actionClasses.add(SetText.class); actionClasses.add(SetTextByLabel.class); actionClasses.add(SetTextByIndex.class); actionClasses.add(DumpWindowHierarchy.class); actionClasses.add(SelectMenuInSettings.class); actionClasses.add(IsOptionInSettingsMenuEnabled.class); actionClasses.add(IsOptionInSettingsMenuDisabled.class); actionClasses.add(HasSettingsMenuItem.class); actionClasses.add(SelectFromAppsList.class); actionClasses.add(Unlock.class); actionClasses.add(ScrollToTextByIndex.class); return actionClasses; } private UiDevice getUiDevice() { return uiDevice; } }