package org.robolectric.shadows; import org.junit.Test; import org.junit.Before; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.TestRunners; import org.robolectric.RuntimeEnvironment; import android.content.AsyncTaskLoader; import java.util.ArrayList; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @RunWith(TestRunners.MultiApiSelfTest.class) public class ShadowAsyncTaskLoaderTest { private final List<String> transcript = new ArrayList<>(); @Before public void setUp() { Robolectric.getForegroundThreadScheduler().pause(); Robolectric.getBackgroundThreadScheduler().pause(); } @Test public void forceLoad_shouldEnqueueWorkOnSchedulers() { new TestLoader(42).forceLoad(); assertThat(transcript).isEmpty(); Robolectric.flushBackgroundThreadScheduler(); assertThat(transcript).containsExactly("loadInBackground"); transcript.clear(); Robolectric.flushForegroundThreadScheduler(); assertThat(transcript).containsExactly("deliverResult 42"); } public class TestLoader extends AsyncTaskLoader<Integer> { private final Integer data; public TestLoader(Integer data) { super(RuntimeEnvironment.application); this.data = data; } @Override public Integer loadInBackground() { transcript.add("loadInBackground"); return data; } @Override public void deliverResult(Integer data) { transcript.add("deliverResult " + data.toString()); } } }