Skip to content

Instantly share code, notes, and snippets.

@danylovolokh
Created January 3, 2016 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danylovolokh/3685faf7e663a3dc8c86 to your computer and use it in GitHub Desktop.
Save danylovolokh/3685faf7e663a3dc8c86 to your computer and use it in GitHub Desktop.
@RunWith(AndroidJUnit4.class)
public class TestAsyncLogin {
private static final String TAG = TestAsyncLogin.class.getSimpleName();
@Rule
public ActivityTestRule<LoginTestActivity> mActivityRule = new ActivityTestRule<>(LoginTestActivity.class);
/**
* This method uses {@link LoginTestActivity} and does few things:
*
* 1. Writes "UserLogin" into login field
* 2. Writes "UserPassword" into password field
* 3. Presses "Perform login" button
*
* 4. Waits for {@link com.volokh.danylo.testasyncronousandroid.activity.LoginTestActivity.LoginTestCallback#onHandleResponseCalled(LoginResponse)}
* and validates that user is logged in successfully.
*
* NOTE: It makes synchronization on local variable but in our case it doesn't matter.
*
* @throws Exception
*/
@Test
public void testLoginViaService() throws Exception {
/** 1.*/
onView(withId(R.id.login)).perform(typeText("UserLogin"));
/** 2.*/
onView(withId(R.id.password)).perform(typeText("UserPassword"));
final Object syncObject = new Object();
LoginTestActivity loginActivity = mActivityRule.getActivity();
loginActivity.setLoginCallback(new LoginTestActivity.LoginTestCallback() {
@Override
public void onHandleResponseCalled(LoginResponse loginResponse) {
Log.v(TAG, "onHandleResponseCalled in thread " + Thread.currentThread().getId());
assertTrue(loginResponse.isLoggedIn);
synchronized (syncObject){
syncObject.notify();
}
}
});
/** 3.*/
onView(withId(R.id.perform_login)).perform(click());
/** 4.
* Here is a synchronization on local variable but it's ok.
*/
synchronized (syncObject){
syncObject.wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment