I have an activity that uses RxLoader to fetch data from server upon activity creation, and populates a list view. I also have test code that tries to click the first row of the list view after it's populated.
Because I am using RxLoader, I have to write a customized IdlingResource for Espresso. However, the problem is that the test finishes (fails) before my listview is populated.
Here is my Activity code:
RxLoaderManager.get(this).create(
"my_loader",
mRestService.loadData(),
new RxLoaderObserver<MyData>() {
@Override
public void onNext(MyData data) {
// populate listview with data here
...
}
});
Here is my IdlingResource implementation:
public class IdlingApiServiceWrapper implements IdlingResource {
public Observable<MyData> loadData(){
counter.incrementAndGet();
Observable<MyData> observable = api.loadData().finallyDo(new Action0() {
@Override
public void call() {
counter.decrementAndGet();
notifyIdle();
}
});
return observable;
} }
Problem is that: When I run the test code, the method "counter.decrementAndGet()" is always called before the "onNext()" method in my activity. My test fails because it expects to listview to be populated.
Apparently I am doing something wrong. But what am I doing wrong?
UPDATE:
Here is a cleaner code snippet to illustrate the problem:
RxLoaderManager.get(this).create(
"my_loader",
myObservable.finallyDo(new Action0() {
@Override
public void call() {
//log
}
}),
new RxLoaderObserver<MyData>() {
@Override
public void onNext(MyData data) {
// do something here
}
}).start();
In the above code, the code snippet within finallyDo() gets called before the "onNext()" call of the observer. Is finallyDo() supposed to be called after the "onNext()"?
Record UI interactionsClick Run > Record Espresso Test. In the Select Deployment Target window, choose the device on which you want to record the test. If necessary, create a new Android Virtual Device. Click OK.
One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class.
The most used assertion is the matches() assertion. It uses a ViewMatcher object to assert the state of the currently selected view. onView(...). check(matches(withText("Hello!")))
Evan Tatarka pointed me to the right direction: https://github.com/evant/rxloader/issues/14
Here is my final solution:
public class IdlingApiServiceWrapper implements IdlingResource {
public Observable<MyData> loadData() {
counter.incrementAndGet();
Observable<MyData> observable = api.loadData().finallyDo(new Action0() {
@Override
public void call() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
counter.decrementAndGet();
notifyIdle();
}
});
}
});
return observable;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With