Android Open Source - android-marvin Find 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  w  w  w .j  a v a  2s  .  c  o  m
import android.app.Activity;
import android.app.Instrumentation;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import de.akquinet.android.marvin.monitor.ExtendedActivityMonitor;

public interface FindViewAction {
    ViewAction withText(String regex);

    ViewAction withId(int id);
    
    ViewAction root();
}

class FindViewActionImpl<T extends Activity> extends BaseActionImpl implements FindViewAction  {
    private final T activity;
    
    public T getActivity() {
        return activity;
    }

    public FindViewActionImpl(Instrumentation instrumentation,
                              ExtendedActivityMonitor activityMonitor, T activity) {
        super(instrumentation, activityMonitor);

        this.activity = activity;
    }

    @Override
    public ViewAction withText(final String regex) {
        View rootView = activity.getWindow().getDecorView();

        View theView = findView(rootView, new ViewFilter() {
            @Override
            public boolean accept(View view) {
                return view instanceof TextView
                        && ((TextView) view).getText().toString()
                        .matches(regex);
            }
        });

        return new ViewActionImpl<T>(this, theView);
    }

    @Override
    public ViewAction withId(int id) {
        return new ViewActionImpl<T>(this, activity.findViewById(id));
    }

    @Override
    public ViewAction root() {
        return new ViewActionImpl<T>(this, activity.getWindow().getDecorView());
    }

    public ViewAction with(ViewFilter filter) {
        View rootView = activity.getWindow().getDecorView();

        View theView = findView(rootView, filter);
        return new ViewActionImpl<T>(this, theView);
    }

    private View findView(View view, ViewFilter filter) {
        if (filter.accept(view)) {
            return view;
        }

        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                View childView = findView(group.getChildAt(i), filter);
                if (childView != null) {
                    return childView;
                }
            }
        }

        return null;
    }
}




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