blob: 9f8307d19f5bcd1d8565f0e3c33aafe35c4435b4 [file] [log] [blame]
import android.support.test.espresso.DataInteraction;
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 org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.*;
import static android.support.test.espresso.assertion.ViewAssertions.*;
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static android.support.test.espresso.matcher.ViewMatchers.*;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class MyTest {
@Rule
public ActivityTestRule<MyActivity> mActivityTestRule = new ActivityTestRule<>(MyActivity.class);
@Test
public void myTest() {
ViewInteraction elementInnerClass = onView(
allOf(withId(resourceId1), withContentDescription("content description 1"),
childAtPosition(
allOf(withId(parentResourceId1),
childAtPosition(
allOf(withId(parentResourceId2), withContentDescription("parent content description 1")),
1)),
0),
isDisplayed()));
elementInnerClass.perform(click());
ViewInteraction elementClass = onView(
allOf(childAtPosition(
withClassName(is("ParentClass1")),
1),
isDisplayed()));
elementClass.perform(click());
ViewInteraction gridClass = onView(
allOf(withId(gridResourceId),
childAtPosition(
withId(gridParentResourceId),
0)));
gridClass.perform(actionOnItemAtPosition(5, click()));
ViewInteraction elementClass2 = onView(
allOf(withId(resourceId2), withContentDescription("content description 2"), isDisplayed()));
elementClass2.check(matches(isDisplayed()));
ViewInteraction elementClass3 = onView(
allOf(withId(resourceId2), withContentDescription("content description 2"), isDisplayed()));
elementClass3.perform(longClick());
ViewInteraction elementClass4 = onView(
allOf(withId(resourceId3),
childAtPosition(
allOf(withId(parentResourceId3),
childAtPosition(
withId(parentResourceId4),
0)),
2),
isDisplayed()));
elementClass4.perform(click());
pressBack();
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
ViewInteraction elementClass5 = onView(
allOf(withId(resourceId4), withText("original text"), withContentDescription("content description 3"),
childAtPosition(
allOf(withId(parentResourceId5),
childAtPosition(
allOf(withId(parentResourceId6), withContentDescription("parent content description 2")),
0)),
1),
isDisplayed()));
elementClass5.perform(replaceText("replacement \n\ntext\n"));
ViewInteraction elementClass6 = onView(
allOf(withId(resourceId4), withText("replacement \n\ntext\n"), withContentDescription("content description 3"),
childAtPosition(
allOf(withId(parentResourceId5),
childAtPosition(
allOf(withId(parentResourceId6), withContentDescription("parent content description 2")),
0)),
1),
isDisplayed()));
elementClass6.perform(closeSoftKeyboard());
ViewInteraction elementClass7 = onView(
allOf(withId(resourceId4), withText("replacement \n\ntext\n"), withContentDescription("content description 3"),
childAtPosition(
allOf(withId(parentResourceId5),
childAtPosition(
allOf(withId(parentResourceId6), withContentDescription("parent content description 2")),
0)),
1),
isDisplayed()));
elementClass7.check(matches(withText("replacement \n\ntext\n")));
ViewInteraction elementClass8 = onView(
allOf(withId(resourceId4), withText("replacement \n\ntext\n"), withContentDescription("content description 3"),
childAtPosition(
allOf(withId(parentResourceId5),
childAtPosition(
allOf(withId(parentResourceId6), withContentDescription("parent content description 2")),
0)),
1),
isDisplayed()));
elementClass8.perform(pressImeActionButton());
ViewInteraction elementClass9 = onView(
allOf(withId(resourceId5), withContentDescription("content description 4"),
childAtPosition(
allOf(withId(parentResourceId7), withText("parent text")),
0),
isDisplayed()));
elementClass9.perform(swipeRight());
DataInteraction elementClass10 = onData(anything())
.inAdapterView(withId(list))
.atPosition(3);
elementClass10.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));
}
};
}
}