|
| 1 | +package cc.istarx.espressodemo; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Instrumentation; |
| 5 | +import android.content.Intent; |
| 6 | +import android.support.test.espresso.intent.matcher.BundleMatchers; |
| 7 | +import android.support.test.espresso.intent.rule.IntentsTestRule; |
| 8 | +import android.support.test.espresso.matcher.BoundedMatcher; |
| 9 | +import android.support.test.rule.ActivityTestRule; |
| 10 | +import android.support.test.runner.AndroidJUnit4; |
| 11 | + |
| 12 | +import org.hamcrest.Description; |
| 13 | +import org.hamcrest.Matcher; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static android.support.test.espresso.Espresso.onView; |
| 21 | +import static android.support.test.espresso.action.ViewActions.click; |
| 22 | +import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard; |
| 23 | +import static android.support.test.espresso.action.ViewActions.typeText; |
| 24 | +import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 25 | +import static android.support.test.espresso.intent.Intents.intended; |
| 26 | +import static android.support.test.espresso.intent.Intents.intending; |
| 27 | +import static android.support.test.espresso.intent.matcher.BundleMatchers.hasEntry; |
| 28 | +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction; |
| 29 | +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtras; |
| 30 | +import static android.support.test.espresso.intent.matcher.IntentMatchers.toPackage; |
| 31 | +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 32 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 33 | +import static android.support.test.espresso.matcher.ViewMatchers.withText; |
| 34 | +import static android.support.test.internal.util.Checks.checkNotNull; |
| 35 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 36 | +import static org.hamcrest.CoreMatchers.is; |
| 37 | +import static org.hamcrest.CoreMatchers.not; |
| 38 | +import static org.hamcrest.Matchers.allOf; |
| 39 | + |
| 40 | +/** |
| 41 | + * Instrumented test, which will execute on an Android device. |
| 42 | + * |
| 43 | + * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> |
| 44 | + */ |
| 45 | +@RunWith(AndroidJUnit4.class) |
| 46 | +public class ExampleInstrumentedTest { |
| 47 | + |
| 48 | + private static final String EMAIL = "[email protected]"; |
| 49 | + private static final String PASSWORD = "istarx.cc"; |
| 50 | + |
| 51 | + // @Rule |
| 52 | + // public ActivityTestRule<EspressoDemoActivity> rule = new ActivityTestRule<>(EspressoDemoActivity.class); |
| 53 | + @Rule |
| 54 | + public IntentsTestRule<EspressoDemoActivity> intentsTestRule = |
| 55 | + new IntentsTestRule<>(EspressoDemoActivity.class); |
| 56 | + |
| 57 | + |
| 58 | + public static Matcher<Object> mapValueMatcher(final String expectedText) { |
| 59 | + checkNotNull(expectedText); |
| 60 | + return new BoundedMatcher<Object, Map>(Map.class) { |
| 61 | + @Override |
| 62 | + public void describeTo(Description description) { |
| 63 | + description.appendText("with item content"); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected boolean matchesSafely(Map item) { |
| 68 | + return hasEntry(equalTo("espresso"), is(expectedText)).matches(item); |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void espressoTestDemo() { |
| 75 | + // onView(withId(R.id.text_view)) |
| 76 | + // .perform(scrollTo(), click(), typeText("Test Text"), closeSoftKeyboard()) |
| 77 | + // .check(matches(allOf(isDisplayed(), withText("Expected Text")))); |
| 78 | + |
| 79 | + onView(withId(R.id.email)).perform(typeText(EMAIL)); |
| 80 | + onView(withId(R.id.password)).perform(typeText(PASSWORD), closeSoftKeyboard()); |
| 81 | + onView(withId(R.id.login)).perform(click()); |
| 82 | + |
| 83 | + String expectedString = String.format("Email is: %s\nPassword is: %s", EMAIL, PASSWORD); |
| 84 | + onView(withId(R.id.intent_content)).check(matches(allOf(isDisplayed(), withText(expectedString)))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void onDataTest() { |
| 89 | + // onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("espresso"), is("fast")))).perform(click()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void espressoIntentTest() { |
| 94 | + onView(withId(R.id.email)).perform(typeText(EMAIL)); |
| 95 | + onView(withId(R.id.password)).perform(typeText(PASSWORD), closeSoftKeyboard()); |
| 96 | + onView(withId(R.id.login)).perform(click()); |
| 97 | + |
| 98 | + intended(allOf( |
| 99 | + toPackage("cc.istarx.espressodemo"), |
| 100 | + not(hasAction("cc.istarx.test.action")), |
| 101 | + hasExtras(allOf( |
| 102 | + hasEntry(equalTo("email"), equalTo(EMAIL)), |
| 103 | + hasEntry(equalTo("password"), equalTo(PASSWORD)) |
| 104 | + )))); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void intendingTest() { |
| 109 | + Intent resultData = new Intent(); |
| 110 | + String testStr = "Intending test string for test..."; |
| 111 | + resultData.putExtra("test", testStr); |
| 112 | + Instrumentation.ActivityResult result = |
| 113 | + new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData); |
| 114 | + |
| 115 | + intending(toPackage("cc.istarx.espressodemo")).respondWith(result); |
| 116 | + |
| 117 | + onView(withId(R.id.intending_button)).perform(click()); |
| 118 | + onView(withId(R.id.result_text)).check(matches(allOf(isDisplayed(),withText(testStr)))); |
| 119 | + } |
| 120 | +} |
0 commit comments