Skip to content

Commit

Permalink
Add mock utility methods for Glide's various builder classes
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 307088222
  • Loading branch information
sjudd authored and glide-copybara-robot committed Apr 17, 2020
1 parent 00d8fce commit 1caeff4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions mocks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
implementation project(':library')
implementation "androidx.annotation:annotation:${ANDROID_X_VERSION}"
implementation "com.google.guava:guava:${GUAVA_VERSION}"
implementation "org.mockito:mockito-core:${MOCKITO_VERSION}"
}

android {
Expand Down
4 changes: 4 additions & 0 deletions mocks/lint.xml
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 mocks/src/main/java/com/bumptech/glide/mocks/AnswerSelf.java
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);
}
}
}
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>());
}
}

0 comments on commit 1caeff4

Please sign in to comment.