Search in sources :

Example 1 with LoaderSet

use of com.stanfy.enroscar.content.loader.LoaderSet in project enroscar by stanfy.

the class LoaderSetTest method shouldWaitFor3Requests.

@Ignore
@Test
public void shouldWaitFor3Requests() throws Throwable {
    // enqueue
    getWebServer().enqueue(new MockResponse().setBody("R1"));
    getWebServer().enqueue(new MockResponse().setBody("R2"));
    getWebServer().enqueue(new MockResponse().setBody("R3"));
    final URL url = getWebServer().getUrl("/");
    final Fragment fragment = createFragment();
    final LoaderManager loaderManager = fragment.getLoaderManager();
    assertThat(loaderManager).isNotNull();
    final CountDownLatch waiter = new CountDownLatch(1);
    // describe loader
    final LoaderSet set = LoaderSet.build(getApplication()).withManager(loaderManager).withCallbacks(new LoaderSet.SetCallbacksAdapter<ResponseData<String>>() {

        @Override
        public Loader<ResponseData<String>> onCreateLoader(final int id, final Bundle args) {
            return new MyRequestBuilder<String>(getApplication()) {
            }.setUrl(url.toString()).setFormat("string").getLoader();
        }
    }, 1).withCallbacks(new LoaderSet.SetCallbacksAdapter<ResponseData<String>>() {

        @Override
        public Loader<ResponseData<String>> onCreateLoader(final int id, final Bundle args) {
            return new MyRequestBuilder<String>(getApplication()) {
            }.setUrl(url.toString()).setFormat("string").getLoader();
        }
    }, 2, 3).create();
    final LoaderSet.LoaderSetCallback callbacks = new LoaderSet.LoaderSetCallback() {

        @Override
        public void onLoadFinished(final Object[] data) {
            Log.i("ChainTest", Arrays.toString(data));
            waiter.countDown();
        }
    };
    set.init(null, callbacks);
    // TODO finish it
    waitAndAssert(new Waiter<Object[]>() {

        @Override
        public Object[] waitForData() {
            try {
                waiter.await(2, TimeUnit.SECONDS);
                return LoaderSetAccess.getResults(set);
            } catch (final InterruptedException e) {
                return null;
            }
        }
    }, new Asserter<Object[]>() {

        @SuppressWarnings("unchecked")
        @Override
        public void makeAssertions(final Object[] data) throws Exception {
            assertThat(data.length).isEqualTo(3);
            assertThat(((ResponseData<String>) data[0]).getModel()).isEqualTo("R1");
            assertThat(((ResponseData<String>) data[1]).getModel()).isEqualTo("R2");
            assertThat(((ResponseData<String>) data[2]).getModel()).isEqualTo("R3");
        }
    });
}
Also used : Loader(android.support.v4.content.Loader) Fragment(android.support.v4.app.Fragment) URL(java.net.URL) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) Bundle(android.os.Bundle) ResponseData(com.stanfy.enroscar.content.loader.ResponseData) CountDownLatch(java.util.concurrent.CountDownLatch) LoaderManager(android.support.v4.app.LoaderManager) LoaderSet(com.stanfy.enroscar.content.loader.LoaderSet) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Bundle (android.os.Bundle)1 Fragment (android.support.v4.app.Fragment)1 LoaderManager (android.support.v4.app.LoaderManager)1 Loader (android.support.v4.content.Loader)1 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)1 LoaderSet (com.stanfy.enroscar.content.loader.LoaderSet)1 ResponseData (com.stanfy.enroscar.content.loader.ResponseData)1 URL (java.net.URL)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1