Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso testing with RxLoader

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()"?

like image 210
Hua H. Avatar asked Dec 10 '14 21:12

Hua H.


People also ask

How do you do the Espresso test?

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.

How do you check Espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class.

How do you assert Espresso?

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!")))


1 Answers

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;
}

}

like image 111
Hua H. Avatar answered Oct 05 '22 02:10

Hua H.