package com.asksven.betterbatterystats; import android.support.test.espresso.ViewInteraction; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.view.WindowManager; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withClassName; import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withParent; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.is; @LargeTest @RunWith(AndroidJUnit4.class) public class ShareActivityTestAll { @Rule public ActivityTestRule<StatsActivity> mActivityTestRule = new ActivityTestRule<>(StatsActivity.class); @Before public void setUp() { final StatsActivity activity = mActivityTestRule.getActivity(); Runnable wakeUpDevice = new Runnable() { public void run() { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }; activity.runOnUiThread(wakeUpDevice); } @Test public void shareActivityTestAll() { ViewInteraction actionMenuItemView = onView( allOf(withId(R.id.share), withContentDescription("Share"), isDisplayed())); actionMenuItemView.perform(click()); ViewInteraction appCompatCheckedTextView = onView( allOf(withId(android.R.id.text1), withText("logcat"), childAtPosition( allOf(withClassName(is("com.android.internal.app.AlertController$RecycleListView")), withParent(withClassName(is("android.widget.LinearLayout")))), 1), isDisplayed())); appCompatCheckedTextView.perform(click()); ViewInteraction appCompatCheckedTextView2 = onView( allOf(withId(android.R.id.text1), withText("dmesg"), childAtPosition( allOf(withClassName(is("com.android.internal.app.AlertController$RecycleListView")), withParent(withClassName(is("android.widget.LinearLayout")))), 2), isDisplayed())); appCompatCheckedTextView2.perform(click()); ViewInteraction editText = onView( allOf(withClassName(is("android.widget.EditText")), isDisplayed())); editText.perform(click()); ViewInteraction appCompatButton = onView( allOf(withId(android.R.id.button3), withText("Save"), isDisplayed())); appCompatButton.perform(click()); } private static Matcher<View> childAtPosition( final Matcher<View> parentMatcher, final int position) { return new TypeSafeMatcher<View>() { @Override public void describeTo(Description description) { description.appendText("Child at position " + position + " in parent "); parentMatcher.describeTo(description); } @Override public boolean matchesSafely(View view) { ViewParent parent = view.getParent(); return parent instanceof ViewGroup && parentMatcher.matches(parent) && view.equals(((ViewGroup) parent).getChildAt(position)); } }; } }