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");
}
});
}
Aggregations