-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mock utility methods for Glide's various builder classes
PiperOrigin-RevId: 307088222
- Loading branch information
1 parent
00d8fce
commit 1caeff4
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<lint> | ||
<issue id="InvalidPackage" severity="ignore" /> | ||
</lint> |
26 changes: 26 additions & 0 deletions
26
mocks/src/main/java/com/bumptech/glide/mocks/AnswerSelf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.bumptech.glide.mocks; | ||
|
||
import static org.mockito.Mockito.RETURNS_DEFAULTS; | ||
|
||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
/** | ||
* Useful default when mocking {@link com.bumptech.glide.request.RequestOptions} or {@link | ||
* com.bumptech.glide.RequestBuilder}. | ||
* | ||
* @param <T> The type of the options and/or builder. | ||
*/ | ||
final class AnswerSelf<T> implements Answer<T> { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T answer(InvocationOnMock invocation) throws Throwable { | ||
Object mock = invocation.getMock(); | ||
if (invocation.getMethod().getReturnType().isInstance(mock)) { | ||
return (T) mock; | ||
} else { | ||
return (T) RETURNS_DEFAULTS.answer(invocation); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
mocks/src/main/java/com/bumptech/glide/mocks/MockGlideBuilders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.bumptech.glide.mocks; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
import com.bumptech.glide.RequestBuilder; | ||
import com.bumptech.glide.request.RequestOptions; | ||
|
||
/** | ||
* Mocks for various builder patterns in Glide to make testing easier. | ||
* | ||
* <p>All methods share the same behavior. Any method on the builder that returns the builder itself | ||
* will default to returning the mock rather than null. Any method on the builder that returns | ||
* anything other than the builder will return Mockito's standard default return value. | ||
*/ | ||
public final class MockGlideBuilders { | ||
|
||
private MockGlideBuilders() {} | ||
|
||
/** Creates a new {@link RequestBuilder} instance with a matching resource type. */ | ||
@SuppressWarnings("unchecked") | ||
public static <T> RequestBuilder<T> mockRequestBuilder() { | ||
return (RequestBuilder<T>) mockGlideRequest(RequestBuilder.class); | ||
} | ||
|
||
/** Creates a new instance of a generated {@code GlideRequest} class for an application. */ | ||
// The suppressions allow callers to get a typed class without warnings in their test code. | ||
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"}) | ||
public static <T, Y extends RequestBuilder<T>> Y mockGlideRequest(Class<?> glideRequest) { | ||
return (Y) mock(glideRequest, new AnswerSelf<Y>()); | ||
} | ||
|
||
/** Creates a new {@link RequestOptions} instance. */ | ||
public static RequestOptions mockRequestOptions() { | ||
return mockGlideOptions(RequestOptions.class); | ||
} | ||
|
||
/** Creates a new instance of a generated {@code GlideOptions} class for an application. */ | ||
public static <T extends RequestOptions> T mockGlideOptions(Class<T> glideOptionsClass) { | ||
return mock(glideOptionsClass, new AnswerSelf<T>()); | ||
} | ||
} |