package ; import android.support.test.espresso.intent.rule.IntentsTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static android.support.test.espresso.intent.Intents.intended; import static android.support.test.espresso.intent.matcher.ComponentNameMatchers.hasShortClassName; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; import static .ScreenRobot.withRobot; import static org.junit.Assert.assertNotNull; @RunWith( AndroidJUnit4.class ) @LargeTest public class LoginActivityTest { private static final String TEST_EMAIL = "annyce@mycompany.com"; private static final String TEST_PASSWORD = "123456"; private static final String PACKAGE_NAME = "my.package"; @Rule public IntentsTestRule activityRule = new IntentsTestRule<>( LoginActivity.class ); @Test public void shouldHaveUsernameAndPasswordHeader () { withRobot( LoginScreenRobot.class ) .checkAreHeadersDisplayed(); } @Test public void shouldNotAllowLoginWhenInvalid () { withRobot( LoginScreenRobot.class ) .provideActivityContext( activityRule.getActivity() ) .callLogin( "", "" ) .checkIsInErrorState(); } @Test public void shouldProceedLoginWhenValid () { withRobot( LoginScreenRobot.class ) .callLogin( TEST_EMAIL, TEST_PASSWORD ) .checkIsLoggedIn(); } public static class LoginScreenRobot extends ScreenRobot { public LoginScreenRobot checkIsLoggedIn () { intended( hasComponent( hasShortClassName( PACKAGE_NAME ) ) ); return this; } public LoginScreenRobot checkIsInErrorState () { return checkDialogWithTextIsDisplayed( R.string.login_error ); } public LoginScreenRobot checkAreHeadersDisplayed () { return checkIsDisplayed( R.id.login_username_header, R.id.login_password_header ); } public LoginScreenRobot callLogin (String username, String password) { return enterTextIntoView ( R.id.login_username_entry, username ) .enterTextIntoView ( R.id.login_password_entry, password ) .clickOkOnView( R.id.login_button ); } } }