Skip to content

Commit

Permalink
Add setExecutor shadow API
Browse files Browse the repository at this point in the history
  • Loading branch information
brettchabot committed Apr 1, 2019
1 parent 075f699 commit 1e2c79c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
package org.robolectric.shadows.support.v4;


import static org.robolectric.util.reflector.Reflector.reflector;

import android.support.v4.content.AsyncTaskLoader;
import java.util.concurrent.Executor;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.ForType;

@Implements(
value = AsyncTaskLoader.class,
shadowPicker = ShadowBaseAsyncTaskLoader.Picker.class,
// TODO: turn off shadowOf generation. Figure out why this is needed
isInAndroidSdk = false)
public class ShadowRealisticAsyncTaskLoader<D> extends ShadowBaseAsyncTaskLoader {}
public class ShadowRealisticAsyncTaskLoader<D> extends ShadowBaseAsyncTaskLoader {

@RealObject private AsyncTaskLoader<D> realObject;

/**
* Allows overriding background executor used by the AsyncLoader.
*
* Its recommended to switch to androidx's AsyncTaskLoader, which provides an overriddable
* getExecutor method.
*/
public void setExecutor(Executor executor) {
reflector(ReflectorAsyncTaskLoader.class, realObject).setExecutor(executor);
}

/** Accessor interface for {@link AsyncTaskLoader}'s internals. */
@ForType(AsyncTaskLoader.class)
private interface ReflectorAsyncTaskLoader {
@Accessor("mExecutor")
void setExecutor(Executor executor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,44 @@
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.annotation.LooperMode.Mode.PAUSED;
import static org.robolectric.shadows.ShadowBaseLooper.shadowMainLooper;
import static org.robolectric.shadows.support.v4.Shadows.shadowOf;

import android.support.v4.content.AsyncTaskLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.util.concurrent.PausedExecutorService;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.TestRunnerWithManifest;

@RunWith(TestRunnerWithManifest.class)
@LooperMode(PAUSED)
// TODO DO NOT SUBMIT
@Config(sdk = 28)
public class ShadowRealisticAsyncTaskLoaderTest {
private final List<String> taskRecord = new ArrayList<>();
private TestLoader testLoader;
private PausedExecutorService pausedBackgroundExecutor;

@Before
public void setUp() {
pausedBackgroundExecutor = new PausedExecutorService();
testLoader = new TestLoader(42);
ShadowRealisticAsyncTaskLoader shadowLoader = Shadow.extract(testLoader);
shadowLoader.setExecutor(pausedBackgroundExecutor);
}

@Test
public void forceLoad_shouldEnqueueWorkOnSchedulers() {
new TestLoader(42).forceLoad();
public void forceLoad_shouldEnqueueWork() {
testLoader.forceLoad();
assertThat(taskRecord).isEmpty();

// Robolectric.flushBackgroundThreadScheduler();
pausedBackgroundExecutor.runAll();
assertThat(taskRecord).containsExactly("loadInBackground");
taskRecord.clear();

Expand All @@ -37,11 +50,10 @@ public void forceLoad_shouldEnqueueWorkOnSchedulers() {

@Test
public void forceLoad_multipleLoads() {
TestLoader testLoader = new TestLoader(42);
testLoader.forceLoad();
assertThat(taskRecord).isEmpty();

// Robolectric.flushBackgroundThreadScheduler();
pausedBackgroundExecutor.runAll();
assertThat(taskRecord).containsExactly("loadInBackground");
taskRecord.clear();

Expand All @@ -52,7 +64,7 @@ public void forceLoad_multipleLoads() {
taskRecord.clear();
testLoader.forceLoad();

// Robolectric.flushBackgroundThreadScheduler();
pausedBackgroundExecutor.runAll();
assertThat(taskRecord).containsExactly("loadInBackground");
taskRecord.clear();

Expand Down

0 comments on commit 1e2c79c

Please sign in to comment.