diff --git a/glide/build.gradle b/glide/build.gradle index 7c70fd95b9..5704cfa644 100644 --- a/glide/build.gradle +++ b/glide/build.gradle @@ -16,6 +16,7 @@ static def getAndroidPathsForJavadoc() { ':integration:okhttp3', ':integration:recyclerview', ':integration:volley', + ':mocks', ] } diff --git a/instrumentation/build.gradle b/instrumentation/build.gradle index 1fa7b93948..0cebef0125 100644 --- a/instrumentation/build.gradle +++ b/instrumentation/build.gradle @@ -10,6 +10,7 @@ dependencies { implementation project(":library") androidTestImplementation project(':library') + androidTestImplementation project(':mocks') androidTestImplementation "org.mockito:mockito-android:${MOCKITO_ANDROID_VERSION}" androidTestImplementation "androidx.test.ext:junit:${ANDROIDX_TEST_VERSION}" androidTestImplementation "androidx.test:rules:${ANDROIDX_TEST_VERSION}" diff --git a/library/test/build.gradle b/library/test/build.gradle index 41f86f4410..49349392c6 100644 --- a/library/test/build.gradle +++ b/library/test/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'com.android.library' dependencies { testImplementation "androidx.appcompat:appcompat:${ANDROID_X_VERSION}" testImplementation project(':library') + testImplementation project(':mocks') testImplementation project(':testutil') testImplementation 'com.google.guava:guava-testlib:18.0' testImplementation "com.google.truth:truth:${TRUTH_VERSION}" diff --git a/library/test/src/test/java/com/bumptech/glide/load/engine/executor/MockGlideExecutor.java b/library/test/src/test/java/com/bumptech/glide/load/engine/executor/MockGlideExecutor.java deleted file mode 100644 index ab35c4e64f..0000000000 --- a/library/test/src/test/java/com/bumptech/glide/load/engine/executor/MockGlideExecutor.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.bumptech.glide.load.engine.executor; - -import android.os.StrictMode; -import androidx.annotation.NonNull; -import androidx.annotation.VisibleForTesting; -import com.google.common.util.concurrent.ForwardingExecutorService; -import com.google.common.util.concurrent.MoreExecutors; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; - -/** Creates mock {@link GlideExecutor}s. */ -@VisibleForTesting -public final class MockGlideExecutor { - private MockGlideExecutor() { - // Utility class. - } - - // Public API. - @SuppressWarnings("WeakerAccess") - public static GlideExecutor newTestExecutor(ExecutorService executorService) { - return new GlideExecutor(executorService); - } - - public static GlideExecutor newMainThreadExecutor() { - return newTestExecutor(new DirectExecutorService()); - } - - /** @deprecated Use {@link #newMainThreadExecutor} instead. */ - @Deprecated - public static GlideExecutor newMainThreadUnlimitedExecutor() { - return newMainThreadExecutor(); - } - - /** - * DirectExecutorService that enforces StrictMode and converts ExecutionExceptions into - * RuntimeExceptions. - */ - private static final class DirectExecutorService extends ForwardingExecutorService { - private static final StrictMode.ThreadPolicy THREAD_POLICY = - new StrictMode.ThreadPolicy.Builder().detectNetwork().penaltyDeath().build(); - - private final ExecutorService delegate; - - DirectExecutorService() { - delegate = MoreExecutors.newDirectExecutorService(); - } - - @Override - protected ExecutorService delegate() { - return delegate; - } - - @NonNull - @Override - public Future submit(@NonNull Runnable task, @NonNull T result) { - return getUninterruptibly(super.submit(task, result)); - } - - @NonNull - @Override - public Future submit(@NonNull Callable task) { - return getUninterruptibly(super.submit(task)); - } - - @NonNull - @Override - public Future submit(@NonNull Runnable task) { - return getUninterruptibly(super.submit(task)); - } - - @Override - public void execute(@NonNull final Runnable command) { - delegate.execute( - new Runnable() { - @Override - public void run() { - StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy(); - StrictMode.setThreadPolicy(THREAD_POLICY); - try { - command.run(); - } finally { - StrictMode.setThreadPolicy(oldPolicy); - } - } - }); - } - - private Future getUninterruptibly(Future future) { - boolean interrupted = false; - try { - while (!future.isDone()) { - try { - future.get(); - } catch (ExecutionException e) { - throw new RuntimeException(e); - } catch (InterruptedException e) { - interrupted = true; - } - } - } finally { - if (interrupted) { - Thread.currentThread().interrupt(); - } - } - return future; - } - } -} diff --git a/mocks/build.gradle b/mocks/build.gradle new file mode 100644 index 0000000000..65fc4b5bc2 --- /dev/null +++ b/mocks/build.gradle @@ -0,0 +1,25 @@ +apply plugin: 'com.android.library' + +dependencies { + implementation project(':library') + implementation "androidx.annotation:annotation:${ANDROID_X_VERSION}" + implementation "com.google.guava:guava:${GUAVA_VERSION}" +} + +android { + compileSdkVersion COMPILE_SDK_VERSION as int + + defaultConfig { + minSdkVersion MIN_SDK_VERSION as int + targetSdkVersion TARGET_SDK_VERSION as int + + versionName = VERSION_NAME as String + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_7 + targetCompatibility JavaVersion.VERSION_1_7 + } +} + +apply from: "${rootProject.projectDir}/scripts/upload.gradle" diff --git a/mocks/gradle.properties b/mocks/gradle.properties new file mode 100644 index 0000000000..fb7be66647 --- /dev/null +++ b/mocks/gradle.properties @@ -0,0 +1,7 @@ +POM_NAME=Glide mocks +POM_ARTIFACT_ID=mocks +POM_PACKAGING=aar +POM_DESCRIPTION=A set of mocks to ease testing with Glide + +JAR_PREFIX=glide- +JAR_POSTFIX= diff --git a/mocks/src/main/AndroidManifest.xml b/mocks/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..35b3d02288 --- /dev/null +++ b/mocks/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + diff --git a/instrumentation/src/androidTest/java/com/bumptech/glide/load/engine/executor/MockGlideExecutor.java b/mocks/src/main/java/com/bumptech/glide/load/engine/executor/MockGlideExecutor.java similarity index 100% rename from instrumentation/src/androidTest/java/com/bumptech/glide/load/engine/executor/MockGlideExecutor.java rename to mocks/src/main/java/com/bumptech/glide/load/engine/executor/MockGlideExecutor.java diff --git a/settings.gradle b/settings.gradle index a1fab3b677..dd8bb128b4 100644 --- a/settings.gradle +++ b/settings.gradle @@ -27,5 +27,6 @@ include ':integration:okhttp3' include ':integration:recyclerview' include ':integration:volley' include ':testutil' +include ':mocks' rootProject.name = 'glide-parent'