Created
October 1, 2016 15:35
-
-
Save wingyplus/ddd32852ea27b71be1a0c6981ca710a1 to your computer and use it in GitHub Desktop.
For blog in medium
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
package io.github.wingyplus.calculator; | |
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.onView; | |
import static android.support.test.espresso.action.ViewActions.click; | |
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard; | |
import static android.support.test.espresso.action.ViewActions.replaceText; | |
import static android.support.test.espresso.assertion.ViewAssertions.matches; | |
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; | |
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; | |
@LargeTest | |
@RunWith(AndroidJUnit4.class) | |
public class MainActivityTest { | |
@Rule | |
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class); | |
@Test | |
public void mainActivityTest() { | |
ViewInteraction appCompatEditText = onView( | |
allOf(withId(R.id.etLeftNumber), isDisplayed())); | |
appCompatEditText.perform(replaceText("20"), closeSoftKeyboard()); | |
ViewInteraction appCompatEditText2 = onView( | |
allOf(withId(R.id.etRightNumber), isDisplayed())); | |
appCompatEditText2.perform(replaceText("30"), closeSoftKeyboard()); | |
ViewInteraction appCompatButton = onView( | |
allOf(withId(R.id.btnCalculate), withText("Calculate"), | |
withParent(allOf(withId(R.id.activity_main), | |
withParent(withId(android.R.id.content)))), | |
isDisplayed())); | |
appCompatButton.perform(click()); | |
ViewInteraction textView = onView( | |
allOf(withId(R.id.tvResult), withText("50"), | |
childAtPosition( | |
childAtPosition( | |
withId(R.id.activity_main), | |
0), | |
3), | |
isDisplayed())); | |
textView.check(matches(withText("50"))); | |
} | |
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