Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a CursorLoader with Robolectric & Mockito

Given I'm developing a simple ListFragment (in this case, it reads a list of Artists from the MediaStore, but will also read data from a different source later) like this:

@EFragment
public class ArtistsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final String TAG = ArtistsFragment.class.getName();
    private SimpleCursorAdapter mAdapter;

    Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;

    CursorLoader mCursorLoader;

    @AfterViews
    void setupView() {
        mAdapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1, null,
                new String[]{MediaStore.Audio.Artists.ARTIST}, // lists path of files
                new int[]{android.R.id.text1}, 0);

        setListAdapter(mAdapter);

        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (mCursorLoader == null) {
            mCursorLoader = new CursorLoader(getActivity(), uri, new String[]{MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST},
                    null, null, MediaStore.Audio.Artists.ARTIST + " ASC");
        } else {
            System.out.println("mCursorLoader.count: " + mCursorLoader.loadInBackground().getCount());            
        }
        return mCursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        setListShown(true);
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }
}

I want to use Robolectric + Mockito + awaitility to proof the Fragment behaves properly on various conditions (e.g. empty list or invalid data etc). My test class looks like this:

@RunWith(RobolectricTestRunner.class)
public class ArtistsFragmentTest {
    @Test
    public void shouldNotBeNull() {
        final ArtistsFragment myFragment = ArtistsFragment_.builder().build();
        assertNotNull(myFragment);

        // Create a mock cursor.
        final Cursor mc = getSampleArtistCursor();
        when(mc.getCount()).thenReturn(1);
        when(mc.getInt(0)).thenReturn(1);
        when(mc.getString(1)).thenReturn("Sample Title");

        myFragment.mCursorLoader = mock(CursorLoader.class);
        when(myFragment.mCursorLoader.loadInBackground()).thenReturn(mc);

        startFragment(myFragment);

        assertNotNull(myFragment.getListView());
        await().atMost(5, TimeUnit.SECONDS).until(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return myFragment.getListAdapter().getCount();
            }
        }, equalTo(1));

        System.out.println(myFragment.getListAdapter().getCount());
    }

    private Cursor getSampleArtistCursor() {
        return new CursorWrapper(mock(MockCursor.class));
    }
}

Then when running this test in IntelliJ or maven the test will fail, the adapter will always return a count of zero.

The System.out.println statement in onCreateLoader however returns 1. Do I need to take special care for Mockito in background threads? (the loadInBackground method runs on a worker thread).

like image 697
Sebastian Roth Avatar asked Jul 05 '13 10:07

Sebastian Roth


2 Answers

I've just gotten Loader tests working in my code. In my case I found it more direct to test the loader itself rather than try to route it through the Fragment code.

I wound up using a slightly modified version of the code from this post: https://groups.google.com/forum/#!msg/robolectric/xY5MF399jA8/V5PnUfh1D-wJ

First, I had to implement some shadow classes because Robolectric2 doesn't include shadow code for the AsyncTaskLoader or Loader classes. If you've never added a shadow class know that it's important you put these in the correct package. Both of these shadows should live in android.support.v4.content.

ShadowLoader

@Implements(Loader.class)
public class ShadowLoader<D> {

// //////////////////////
// Constants

// //////////////////////
// Fields
protected boolean reset;

// //////////////////////
// Constructors

// //////////////////////
// Getter & Setter

// //////////////////////
// Methods from SuperClass/Interfaces

@Implementation
public void reset() {
    reset = true;
}

@Implementation
public boolean isReset() {
    return reset;
}
// //////////////////////
// Methods

// //////////////////////
// Inner and Anonymous Classes
}

ShadowAsyncTaskLoader

@Implements(AsyncTaskLoader.class)
public class ShadowAsyncTaskLoader<D> extends ShadowLoader {

@RealObject
private AsyncTaskLoader<D> asyncTaskLoader;

@Implementation
void executePendingTask() {
    new AsyncTask<Void, Void, D>() {
        @Override
        protected D doInBackground(Void... params) {
            return (D) asyncTaskLoader.loadInBackground();
        }

        @Override
        protected void onPostExecute(D data) {
            updateLastLoadCompleteTimeField();
            asyncTaskLoader.deliverResult(data);
        }

        @Override
        protected void onCancelled(D data) {
            updateLastLoadCompleteTimeField();
            asyncTaskLoader.onCanceled(data);

            executePendingTask();
        }
    }.execute((Void)null);
}


public void setReset(boolean reset) {
    this.reset = reset;
}

private void updateLastLoadCompleteTimeField() {
    try {
        Field mLastLoadCompleteTimeField = AsyncTaskLoader.class.getDeclaredField("mLastLoadCompleteTime");
        if(!mLastLoadCompleteTimeField.isAccessible()) {
            mLastLoadCompleteTimeField.setAccessible(true);
        }
        mLastLoadCompleteTimeField.set(asyncTaskLoader, SystemClock.uptimeMillis());

    } catch(NoSuchFieldException e) {
        e.printStackTrace();
    } catch(IllegalAccessException e) {
        e.printStackTrace();
    }
}
}

Then, depending on your configuration you can add an annotation to use the custom classes

@Config( shadows = { ShadowAsyncTaskLoader.class, ShadowLoader.class})

At this point calling loader.onStartLoading() caused the loader to run as expected without having to hack any wait commands into my test cases.

Hope this helps. I haven't tried using the LoaderManager with this method of testing, so I can't verify that it works through that call.

Note: the reason I added ShadowLoader is because I was finding isReset() was returning true when I didn't expect it to.

like image 196
Travis Castillo Avatar answered Nov 15 '22 20:11

Travis Castillo


The solution is to use:

Robolectric.flushForegroundThreadScheduler();

(Robolectric 3.0)

This will run all tasks immediately, including the CursorLoader.

like image 21
Sebastian Roth Avatar answered Nov 15 '22 20:11

Sebastian Roth