org.robolectric.shadows.ShadowAsyncTaskLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shadows-core-v17 Show documentation
Show all versions of shadows-core-v17 Show documentation
An alternative Android testing framework.
package org.robolectric.shadows;
import android.content.Context;
import android.content.AsyncTaskLoader;
import org.robolectric.util.SimpleFuture;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Implementation;
import java.util.concurrent.Callable;
/**
* Shadow for {@link android.content.AsyncTaskLoader}
*
* @param Return data type.
*/
@Implements(AsyncTaskLoader.class)
public class ShadowAsyncTaskLoader {
@RealObject private AsyncTaskLoader realObject;
private SimpleFuture future;
public void __constructor__(Context context) {
BackgroundWorker worker = new BackgroundWorker();
future = new SimpleFuture(worker) {
@Override
protected void done() {
try {
final D result = get();
ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
@Override
public void run() {
realObject.deliverResult(result);
}
});
} catch (InterruptedException e) {
// Ignore
}
}
};
}
@Implementation
public void onForceLoad() {
ShadowApplication.getInstance().getBackgroundThreadScheduler().post(new Runnable() {
@Override
public void run() {
future.run();
}
});
}
private final class BackgroundWorker implements Callable {
@Override
public D call() throws Exception {
return realObject.loadInBackground();
}
}
}