Last active
September 28, 2016 08:13
-
-
Save maydin/246b7b3bb528ce4f9d546c8269e2ba86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@LargeTest | |
@RunWith(AndroidJUnit4.class) | |
public class MainActivityTest { | |
@Rule | |
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class); | |
@Test | |
public void mainActivityTest() { | |
ViewInteraction textView = onView( | |
allOf(withId(R.id.textview), withText("Hello World"), | |
childAtPosition( | |
allOf(withId(R.id.text_switcher), | |
childAtPosition( | |
IsInstanceOf.<View>instanceOf(android.widget.RelativeLayout.class), | |
1)), | |
0), | |
isDisplayed())); | |
textView.check(matches(withText("Hello World"))); | |
ViewInteraction appCompatButton = onView( | |
allOf(withId(R.id.button), withText("Click"), isDisplayed())); | |
appCompatButton.perform(click()); | |
try { | |
Thread.sleep(300); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
ViewInteraction textView2 = onView( | |
allOf(withId(R.id.textview), withText("Bye World"), | |
childAtPosition( | |
allOf(withId(R.id.text_switcher), | |
childAtPosition( | |
IsInstanceOf.<View>instanceOf(android.widget.RelativeLayout.class), | |
1)), | |
0), | |
isDisplayed())); | |
textView2.check(matches(withText("Bye World"))); | |
} | |
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)); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment