Android Open Source - android-marvin View Action






From Project

Back to project page android-marvin.

License

The source code is released under:

Apache License

If you think the Android project android-marvin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.akquinet.android.marvin.actions;
//from  ww w . j ava2s .  c om
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;


public interface ViewAction {
    View get();

    void click();

    void setText(String text);
}


class ViewActionImpl<T extends Activity> extends BaseActionImpl implements ViewAction {
    private final T activity;
    private final View view;

    public ViewActionImpl(ActivityActionImpl<T> activityAction, View view) {
        super(activityAction.instrumentation, activityAction.activityMonitor);

        this.activity = activityAction.activity;
        this.view = view;
    }

    public ViewActionImpl(ActivityActionImpl<T> activityAction, int viewId) {
        super(activityAction.instrumentation, activityAction.activityMonitor);

        this.activity = activityAction.activity;
        this.view = activityAction.activity.findViewById(viewId);
    }

    public ViewActionImpl(FindViewActionImpl<T> activityAction, View view) {
        super(activityAction.instrumentation, activityAction.activityMonitor);

        this.activity = activityAction.getActivity();
        this.view = view;
    }

    public ViewActionImpl(FindViewActionImpl<T> activityAction, int viewId) {
        super(activityAction.instrumentation, activityAction.activityMonitor);

        this.activity = activityAction.getActivity();
        this.view = activityAction.getActivity().findViewById(viewId);
    }

    public View get() {
        return view;
    }

    @Override
    public void click() {
        Assert.assertNotNull("View should exist but does not.", view);

        actionPerformed();

        final List<Boolean> monitor = new ArrayList<Boolean>();

        // Call the OnClickListener of the view in UI thread
        activity.runOnUiThread(new Runnable() {
            public void run() {
                boolean listenerRegistered = view.performClick();
                synchronized (monitor) {
                    monitor.add(listenerRegistered);
                    monitor.notifyAll();
                }
            }
        });

        synchronized (monitor) {
            try {
                while (monitor.isEmpty()) {
                    monitor.wait();
                }
            }
            catch (InterruptedException e) {
            }
        }

        if (monitor.size() > 0 && Boolean.FALSE.equals(monitor.get(0))) {
            /*
             * There was no OnClickListener registered. But there may be a view
             * above/below our view that has a listener. It would have gotten
             * invoked on a user click, so we perform a click at the coordinates
             * of our view to reach the same behavior.
             */
            int[] location = new int[2];
            view.getLocationOnScreen(location);

            click(location[0], location[1]);
        }

        instrumentation.waitForIdleSync();
    }

    @Override
    public void setText(final String text) {
        Assert.assertNotNull("View should exist but does not.", view);

        if (!(view instanceof TextView)) {
            Assert.fail("View is no TextView");
        }

        actionPerformed();

        activity.runOnUiThread(new Runnable() {
            public void run() {
                ((TextView) view).setText(text);
            }
        });

        instrumentation.waitForIdleSync();
    }
}




Java Source Code List

de.akquinet.android.marvin.ActivityTestCase.java
de.akquinet.android.marvin.AndroidMatchers.java
de.akquinet.android.marvin.AndroidTestCase.java
de.akquinet.android.marvin.ServiceTestCase.java
de.akquinet.android.marvin.actions.ActionFactory.java
de.akquinet.android.marvin.actions.ActivityAction.java
de.akquinet.android.marvin.actions.AwaitAction.java
de.akquinet.android.marvin.actions.BaseAction.java
de.akquinet.android.marvin.actions.FindViewAction.java
de.akquinet.android.marvin.actions.PerformAction.java
de.akquinet.android.marvin.actions.ViewAction.java
de.akquinet.android.marvin.actions.ViewFilter.java
de.akquinet.android.marvin.matchers.Condition.java
de.akquinet.android.marvin.matchers.HasText.java
de.akquinet.android.marvin.matchers.IsEnabled.java
de.akquinet.android.marvin.matchers.IsOnScreen.java
de.akquinet.android.marvin.matchers.IsVisible.java
de.akquinet.android.marvin.matchers.ViewGroupComparison.java
de.akquinet.android.marvin.matchers.util.WaitForConditionUtil.java
de.akquinet.android.marvin.monitor.ExtendedActivityMonitor.java
de.akquinet.android.marvin.monitor.StartedActivity.java
de.akquinet.android.marvin.util.MarvinConnectivityUtil.java
de.akquinet.android.marvin.util.TemporaryServiceConnection.java