|
| 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