Skip to content

Instantly share code, notes, and snippets.

@digitalbuddha digitalbuddha/rxEspresso.java Secret
Last active May 13, 2019

Embed
What would you like to do?
//in test
protected void setUp() throws Exception {
super.setUp();
Espresso.registerIdlingResources(new BetterIdlingResource());
public class BetterIdlingResource implements IdlingResource, BetterExecutionBridge {
private static final boolean isLogged = false;
private IdlingResource.ResourceCallback cb;
private Integer idler = 0;
public BetterIdlingResource() {
try {
RxJavaPlugins.getInstance().registerObservableExecutionHook(new BetterExecutionHook(this));
} catch (Exception e) {
}
}
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public boolean isIdleNow() {
synchronized (idler) {
if (isLogged)
LogUtil.e("LOG", "called isidlenow: " + idler, null);
return idler == 0;
}
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
if (isLogged)
LogUtil.e("LOG", "called register Idle: " + idler, null);
this.cb = resourceCallback;
}
@Override
public void onStart() {
synchronized (idler) {
idler++;
if (isLogged)
LogUtil.e("LOG", "called onstart: " + idler, null);
}
}
@Override
public void onError() {
synchronized (idler) {
idler--;
if (isLogged)
LogUtil.e("LOG", "called onerrror: " + idler, null);
if (idler == 0 && cb != null) {
cb.onTransitionToIdle();
}
}
}
@Override
public void onEnd() {
synchronized (idler) {
idler--;
if (isLogged)
LogUtil.e("LOG", "called onend: " + idler, null);
if (idler == 0 && cb != null) {
cb.onTransitionToIdle();
}
}
}
}
public class BetterExecutionHook extends RxJavaObservableExecutionHook {
BetterExecutionBridge betterExecutionBridge;
public BetterExecutionHook(BetterExecutionBridge betterExecutionBridge) {
this.betterExecutionBridge = betterExecutionBridge;
}
@Override
public <T> Observable.OnSubscribe<T> onCreate(Observable.OnSubscribe<T> f) {
return super.onCreate(f);
}
@Override
public <T> Observable.OnSubscribe<T> onSubscribeStart(Observable<? extends T> observableInstance, Observable.OnSubscribe<T> onSubscribe) {
onSubscribe.call(new Subscriber<T>() {
@Override
public void onCompleted() {
betterExecutionBridge.onEnd();
}
@Override
public void onError(Throwable e) {
betterExecutionBridge.onEnd();
}
@Override
public void onNext(T t) {
}
});
betterExecutionBridge.onStart();
return onSubscribe;
}
@Override
public <T> Subscription onSubscribeReturn(Subscription subscription) {
return subscription;
}
@Override
public <T> Throwable onSubscribeError(Throwable e) {
return e;
}
@Override
public <T, R> Observable.Operator<? extends R, ? super T> onLift(Observable.Operator<? extends R, ? super T> lift) {
return super.onLift(lift);
}
}
public interface BetterExecutionBridge {
void onStart();
void onError();
void onEnd();
}
@markrietveld

This comment has been minimized.

Copy link

commented May 1, 2015

It seems like line 113 should call onError(), not onEnd()

@carlonzo

This comment has been minimized.

Copy link

commented Aug 27, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.