From 26ecf64550a07d1b8f958a8e3ef192028b4892ca Mon Sep 17 00:00:00 2001 From: Paul Sowden <106676+paulsowden@users.noreply.github.com> Date: Wed, 7 Jun 2023 20:17:07 -0700 Subject: [PATCH] Add `isInitialized` visible for testing method (#5163) Glide provides an `init` method for test authors and rule writers to initialize Glide with custom executors but due to the transparent way it self initializes and tears down when re-initialized it makes it hard to assert the state of Glide. One easy way to misconfigure Glide when using dependency injection is for something to obtain an instance before the test has called `init`, in this case the instance that was read will be torn down but will typically fail in mysterious ways; Glide doesn't throw an exception when a torn down instance is attempted to be loaded from, usually the request will be rejected by the executors because they've been shutdown. Adding `isInitialized` allows test authors and rule writers to assert that Glide has not been initialized before the test initializes Glide. --- library/src/main/java/com/bumptech/glide/Glide.java | 5 +++++ .../com/bumptech/glide/InitializeGlideTest.java | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/library/src/main/java/com/bumptech/glide/Glide.java b/library/src/main/java/com/bumptech/glide/Glide.java index 8db27f10c2..612dde5718 100644 --- a/library/src/main/java/com/bumptech/glide/Glide.java +++ b/library/src/main/java/com/bumptech/glide/Glide.java @@ -183,6 +183,11 @@ public static void init(@NonNull Context context, @NonNull GlideBuilder builder) } } + @VisibleForTesting + public static synchronized boolean isInitialized() { + return glide != null; + } + /** * Allows hardware Bitmaps to be used prior to the first frame in the app being drawn as soon as * this method is called. diff --git a/library/test/src/test/java/com/bumptech/glide/InitializeGlideTest.java b/library/test/src/test/java/com/bumptech/glide/InitializeGlideTest.java index b51e69c531..7a5f7d13fc 100644 --- a/library/test/src/test/java/com/bumptech/glide/InitializeGlideTest.java +++ b/library/test/src/test/java/com/bumptech/glide/InitializeGlideTest.java @@ -1,5 +1,6 @@ package com.bumptech.glide; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import android.content.Context; @@ -69,4 +70,16 @@ public void run() throws Throwable { // Make sure the second exception isn't hidden by some Glide initialization related exception. assertThrows(TestException.class, initializeGlide); } + + @Test + public void isInitialized_whenNotInitialized_returnsFalse() { + assertThat(Glide.isInitialized()).isFalse(); + } + + @Test + public void isInitialized_whenInitialized_returnsTrue() { + Glide.get(context); + + assertThat(Glide.isInitialized()).isTrue(); + } }