Like the other answer suggests, the countingIdlingResource does not really apply for your use case.
What I always do is add an interface - let's call this one ProgressListener
- as a field of the activity / fragment that has a resource to be waited on (asynchronous background work, longer networking sessions, etc.) and a method to notify it everytime the progress is shown or dismissed.
I'm assuming you have your credentials validation logic and the call to the Parse API in the LoginActivity
, which will then call an intent to the MainActivity
if successful.
public class LoginActivity extends AppCompatActivity {
private ProgressListener mListener;
...
public interface ProgressListener {
public void onProgressShown();
public void onProgressDismissed();
}
public void setProgressListener(ProgressListener progressListener) {
mListener = progressListener;
}
...
public void onLoginButtonClicked (View view) {
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
// validate credentials for blanks and so on
// show progress and call parse login in background method
showProgress();
ParseUser.logInInBackground(username,password, new LogInCallback() {
@Override
public void done(ParseUser parseUser, ParseException e) {
dismissProgress();
if (e == null){
// Success!, continue to MainActivity via intent
Intent intent = new Intent (LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
// login failed dialog or similar.
}
}
});
}
private void showProgress() {
// show the progress and notify the listener
...
notifyListener(mListener);
}
private void dismissProgress() {
// hide the progress and notify the listener
...
notifyListener(mListener);
}
public boolean isInProgress() {
// return true if progress is visible
}
private void notifyListener(ProgressListener listener) {
if (listener == null){
return;
}
if (isInProgress()){
listener.onProgressShown();
}
else {
listener.onProgressDismissed();
}
}
}
Then, simply implement the IdlingResource class and override its methods to communicate when the resource goes from busy to idle through its ResourceCallBack
public class ProgressIdlingResource implements IdlingResource {
private ResourceCallback resourceCallback;
private LoginActivity loginActivity;
private LoginActivity.ProgressListener progressListener;
public ProgressIdlingResource(LoginActivity activity){
loginActivity = activity;
progressListener = new LoginActivity.ProgressListener() {
@Override
public void onProgressShown() {
}
@Override
public void onProgressDismissed() {
if (resourceCallback == null){
return ;
}
//Called when the resource goes from busy to idle.
resourceCallback.onTransitionToIdle();
}
};
loginActivity.setProgressListener (progressListener);
}
@Override
public String getName() {
return "My idling resource";
}
@Override
public boolean isIdleNow() {
// the resource becomes idle when the progress has been dismissed
return !loginActivity.isInProgress();
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
}
Last step is to register your custom idling resource in the test's setUp()
method:
Espresso.registerIdlingResources(new ProgressIdlingResource((LoginActivity) getActivity()));
And that's it! Now espresso will wait for your login process to complete and then continue with all the other tests.
Please let me know if I wasn't clear enough or if that is exactly what you needed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…