Skip to content

Commit

Permalink
Add isInitialized visible for testing method (#5163)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
paulsowden authored Jun 8, 2023
1 parent 2de5856 commit 26ecf64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 26ecf64

Please sign in to comment.