Skip to content

Commit 1caeff4

Browse files
sjuddglide-copybara-robot
authored andcommitted
Add mock utility methods for Glide's various builder classes
PiperOrigin-RevId: 307088222
1 parent 00d8fce commit 1caeff4

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

mocks/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dependencies {
44
implementation project(':library')
55
implementation "androidx.annotation:annotation:${ANDROID_X_VERSION}"
66
implementation "com.google.guava:guava:${GUAVA_VERSION}"
7+
implementation "org.mockito:mockito-core:${MOCKITO_VERSION}"
78
}
89

910
android {

mocks/lint.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
<issue id="InvalidPackage" severity="ignore" />
4+
</lint>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.bumptech.glide.mocks;
2+
3+
import static org.mockito.Mockito.RETURNS_DEFAULTS;
4+
5+
import org.mockito.invocation.InvocationOnMock;
6+
import org.mockito.stubbing.Answer;
7+
8+
/**
9+
* Useful default when mocking {@link com.bumptech.glide.request.RequestOptions} or {@link
10+
* com.bumptech.glide.RequestBuilder}.
11+
*
12+
* @param <T> The type of the options and/or builder.
13+
*/
14+
final class AnswerSelf<T> implements Answer<T> {
15+
16+
@SuppressWarnings("unchecked")
17+
@Override
18+
public T answer(InvocationOnMock invocation) throws Throwable {
19+
Object mock = invocation.getMock();
20+
if (invocation.getMethod().getReturnType().isInstance(mock)) {
21+
return (T) mock;
22+
} else {
23+
return (T) RETURNS_DEFAULTS.answer(invocation);
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.bumptech.glide.mocks;
2+
3+
import static org.mockito.Mockito.mock;
4+
5+
import com.bumptech.glide.RequestBuilder;
6+
import com.bumptech.glide.request.RequestOptions;
7+
8+
/**
9+
* Mocks for various builder patterns in Glide to make testing easier.
10+
*
11+
* <p>All methods share the same behavior. Any method on the builder that returns the builder itself
12+
* will default to returning the mock rather than null. Any method on the builder that returns
13+
* anything other than the builder will return Mockito's standard default return value.
14+
*/
15+
public final class MockGlideBuilders {
16+
17+
private MockGlideBuilders() {}
18+
19+
/** Creates a new {@link RequestBuilder} instance with a matching resource type. */
20+
@SuppressWarnings("unchecked")
21+
public static <T> RequestBuilder<T> mockRequestBuilder() {
22+
return (RequestBuilder<T>) mockGlideRequest(RequestBuilder.class);
23+
}
24+
25+
/** Creates a new instance of a generated {@code GlideRequest} class for an application. */
26+
// The suppressions allow callers to get a typed class without warnings in their test code.
27+
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
28+
public static <T, Y extends RequestBuilder<T>> Y mockGlideRequest(Class<?> glideRequest) {
29+
return (Y) mock(glideRequest, new AnswerSelf<Y>());
30+
}
31+
32+
/** Creates a new {@link RequestOptions} instance. */
33+
public static RequestOptions mockRequestOptions() {
34+
return mockGlideOptions(RequestOptions.class);
35+
}
36+
37+
/** Creates a new instance of a generated {@code GlideOptions} class for an application. */
38+
public static <T extends RequestOptions> T mockGlideOptions(Class<T> glideOptionsClass) {
39+
return mock(glideOptionsClass, new AnswerSelf<T>());
40+
}
41+
}

0 commit comments

Comments
 (0)