Skip to content

Instantly share code, notes, and snippets.

@mcassiano
Created February 11, 2018 17:09
Show Gist options
  • Save mcassiano/1e3110af3bbaa77e2e0b73b22af990d9 to your computer and use it in GitHub Desktop.
Save mcassiano/1e3110af3bbaa77e2e0b73b22af990d9 to your computer and use it in GitHub Desktop.
public class IdlingToaster extends Toaster {
private HashSet<Toast> toasts = new HashSet<>();
@Override
public Toast makeText(Context context, CharSequence text, int duration) {
Toast toast = Toast.makeText(context, text, LENGTH_SHORT);
toast.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
toasts.add(toast);
}
@Override
public void onViewDetachedFromWindow(View view) {
toasts.remove(toast);
}
});
return toast;
}
public void cancel() {
for (Toast t : toasts)
t.cancel();
toasts.clear();
}
}
@Test
public void render_shouldShowCurrentPlace() throws InterruptedException {
// do your setup
toaster.cancel();
}
public class Toaster {
public Toast makeText(Context context, CharSequence text, int duration) {
return Toast.makeText(context, text, duration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment