package com.lukekorth.deviceautomator;

import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiScrollable;
import android.support.test.uiautomator.UiSelector;

/**
 * A collection of actions for use with {@link DeviceAutomator}.
 */
public abstract class AutomatorAction {

    /**
     * Performs a click on the ui element specified in
     * {@link DeviceAutomator#onDevice(UiObjectMatcher)}.
     *
     * @return
     */
    public static AutomatorAction click() {
        return new AutomatorAction() {
            @Override
            void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException {
                object.click();
            }
        };
    }

    /**
     * Checks or unchecks the ui element based on the check param if the element is checkable.
     *
     * @param check {@code true} to check the element, {@code false} to uncheck the element.
     * @return
     */
    public static AutomatorAction check(final boolean check) {
        return new AutomatorAction() {
            @Override
            void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException {
                if (object.isCheckable() &&
                        ((check && !object.isChecked()) || (!check && object.isChecked()))) {
                    object.click();
                }
            }
        };
    }

    /**
     * Sets the text on the ui element specified in
     * {@link DeviceAutomator#onDevice(UiObjectMatcher)}.
     *
     * @return
     */
    public static AutomatorAction setText(final String text) {
        return new AutomatorAction() {
            @Override
            void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException {
                object.setText(text);
            }
        };
    }

    /**
     * Clears the text field on the ui element specified in
     * {@link DeviceAutomator#onDevice(UiObjectMatcher)}.
     *
     * @return
     */
    public static AutomatorAction clearTextField() {
        return new AutomatorAction() {
            @Override
            void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException {
                object.clearTextField();
            }
        };
    }

    /**
     * Performs the swipe right action on the UiObject. The swipe gesture can be performed over any surface.
     * The targeted UI element does not need to be scrollable.
     *
     * @param steps indicates the number of injected move steps into the system. Steps are injected about 5ms apart.
     *              So a 100 steps may take about 1/2 second to complete.
     * @return
     */
    public static AutomatorAction swipeRight(final int steps) {
        return new AutomatorAction() {
            @Override
            void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException {
                object.swipeRight(steps);
            }
        };
    }

    /**
     * Scrolls a {@link UiScrollable} until the given text is displayed on the screen.
     *
     * @param text the text to scroll to.
     * @return
     */
    public static AutomatorAction scrollTextIntoView(final String text) {
        return new AutomatorAction() {
            @Override
            void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException {
                new UiScrollable(selector).scrollTextIntoView(text);
            }
        };
    }

    void perform(UiSelector selector, UiObject object) {
        try {
            wrappedPerform(selector, object);
        } catch (UiObjectNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    abstract void wrappedPerform(UiSelector selector, UiObject object) throws UiObjectNotFoundException;
}

@@ -631,32 +631,39 @@
 ide%0A            
+public 
 void wrappedPerf
@@ -731,32 +731,32 @@
 oundException %7B%0A
-
                 
@@ -1157,32 +1157,39 @@
 ide%0A            
+public 
 void wrappedPerf
@@ -1765,32 +1765,39 @@
 ide%0A            
+public 
 void wrappedPerf
@@ -2217,32 +2217,39 @@
 ide%0A            
+public 
 void wrappedPerf
@@ -2928,32 +2928,39 @@
 ide%0A            
+public 
 void wrappedPerf
@@ -3420,24 +3420,31 @@
             
+public 
 void wrapped
@@ -3850,20 +3850,27 @@
 %0A    %7D%0A%0A
-
     
+public 
 abstract

Make AutomatorAction method public to enable custom actions