* Rules are interceptors which are executed for each test method and will run before * any of your setup code in the {@link Before @Before} method. *
* {@link ActivityTestRule} will create and launch of the activity for you and also expose * the activity under test. To get a reference to the activity you can use * the {@link ActivityTestRule#getActivity()} method. */ @Rule public ActivityTestRule mActivityRule = new ActivityTestRule<>( MainActivity.class); // A valid string with a valid ending private String mStringWithValidEnding; // A valid string from the coffee preparations private String mValidStringToBeTyped; @Before public void initValidStrings() { // Produce a string with valid ending. mStringWithValidEnding = "Random " + MainActivity.VALID_ENDING; // Get one of the available coffee preparations. mValidStringToBeTyped = MainActivity.COFFEE_PREPARATIONS.get(0); } /** * Uses a custom matcher {@link HintMatcher#withHint}, with a {@link String} as the argument. */ @Test public void hint_isDisplayedInEditText() { String hintText = mActivityRule.getActivity().getResources().getString(R.string.hint); onView(withId(R.id.editText)).check(matches(HintMatcher.withHint(hintText))); } /** * Same as above but using a {@link org.hamcrest.Matcher} as the argument. */ @SuppressWarnings("unchecked") @Test public void hint_endsWith() { // This check will probably fail if the app is localized and the language is changed. Avoid // string literals in code! onView(withId(R.id.editText)).check(matches(HintMatcher.withHint(anyOf( endsWith(COFFEE_ENDING), endsWith(COFFEE_INVALID_ENDING))))); } @Test public void editText_canBeTypedInto() { onView(withId(R.id.editText)) .perform(typeText(mValidStringToBeTyped), closeSoftKeyboard()) .check(matches(withText(mValidStringToBeTyped))); } @Test public void validation_resultIsOneOfTheValidStrings() { // Type a valid string and click on the button. onView(withId(R.id.editText)) .perform(typeText(mValidStringToBeTyped), closeSoftKeyboard()); onView(withId(R.id.button)).perform(click()); // Check that the correct sign is displayed. onView(withId(R.id.inputValidationSuccess)).check(matches(isDisplayed())); onView(withId(R.id.inputValidationError)).check(matches(not(isDisplayed()))); } @Test public void validation_resultHasCorrectEnding() { // Type a string with a valid ending and click on the button. onView(withId(R.id.editText)) .perform(typeText(mStringWithValidEnding), closeSoftKeyboard()); onView(withId(R.id.button)).perform(click()); // Check that the correct sign is displayed. onView(withId(R.id.inputValidationSuccess)).check(matches(isDisplayed())); onView(withId(R.id.inputValidationError)).check(matches(not(isDisplayed()))); } @Test public void validation_resultIsIncorrect() { // Type a valid string and click on the button. onView(withId(R.id.editText)) .perform(typeText(INVALID_STRING_TO_BE_TYPED), closeSoftKeyboard()); onView(withId(R.id.button)).perform(click()); // Check that the correct sign is displayed. onView(withId(R.id.inputValidationError)).check(matches(isDisplayed())); onView(withId(R.id.inputValidationSuccess)).check(matches(not(isDisplayed()))); } }