|
| 1 | +package com.bumptech.glide.load.model; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.junit.Assert.assertThrows; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import com.bumptech.glide.Registry.NoModelLoaderAvailableException; |
| 10 | +import com.bumptech.glide.util.pool.FactoryPools; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.function.ThrowingRunnable; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.robolectric.RobolectricTestRunner; |
| 16 | + |
| 17 | +@RunWith(RobolectricTestRunner.class) |
| 18 | +public class ModelLoaderRegistryTest { |
| 19 | + private static final String MOCK_MODEL_LOADER_NAME = "MockModelLoader"; |
| 20 | + |
| 21 | + private ModelLoaderRegistry registry; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() { |
| 25 | + registry = new ModelLoaderRegistry(FactoryPools.<Throwable>threadSafeList()); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void getModelLoaders_withNoRegisteredModelLoader_throws() { |
| 30 | + final Object model = new Object(); |
| 31 | + NoModelLoaderAvailableException thrown = |
| 32 | + assertThrows( |
| 33 | + NoModelLoaderAvailableException.class, |
| 34 | + new ThrowingRunnable() { |
| 35 | + @Override |
| 36 | + public void run() { |
| 37 | + registry.getModelLoaders(model); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + assertThat(thrown) |
| 42 | + .hasMessageThat() |
| 43 | + .contains( |
| 44 | + "Failed to find any ModelLoaders registered for model class: " + model.getClass()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void getModelLoaders_withRegisteredModelLoader_thatDoesNotHandleModelInstance_throws() { |
| 49 | + final Object model = new Object(); |
| 50 | + final ModelLoader<Object, Object> modelLoader = mockModelLoader(); |
| 51 | + when(modelLoader.handles(model)).thenReturn(false); |
| 52 | + appendModelLoader(modelLoader); |
| 53 | + |
| 54 | + NoModelLoaderAvailableException thrown = |
| 55 | + assertThrows( |
| 56 | + NoModelLoaderAvailableException.class, |
| 57 | + new ThrowingRunnable() { |
| 58 | + @Override |
| 59 | + public void run() { |
| 60 | + registry.getModelLoaders(model); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + assertThat(thrown) |
| 65 | + .hasMessageThat() |
| 66 | + .contains( |
| 67 | + "Found ModelLoaders for model class: [MockModelLoader], but none that handle this specific model instance: java.lang.Object"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void getModelLoaders_withRegisteredModelLoader_handlesModel_returnsModelLoader() { |
| 72 | + final Object model = new Object(); |
| 73 | + final ModelLoader<Object, Object> modelLoader = mockModelLoader(); |
| 74 | + when(modelLoader.handles(model)).thenReturn(true); |
| 75 | + appendModelLoader(modelLoader); |
| 76 | + |
| 77 | + assertThat(registry.getModelLoaders(model)).containsExactly(modelLoader); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void |
| 82 | + getModelLoaders_withRegisteredModelLoaders_onlyOneHandlesModel_returnsHandlingModelLoader() { |
| 83 | + final Object model = new Object(); |
| 84 | + |
| 85 | + ModelLoader<Object, Object> handlingModelLoader = mockModelLoader(); |
| 86 | + when(handlingModelLoader.handles(model)).thenReturn(true); |
| 87 | + appendModelLoader(handlingModelLoader); |
| 88 | + |
| 89 | + ModelLoader<Object, Object> notHandlingModelLoader = mockModelLoader(); |
| 90 | + when(notHandlingModelLoader.handles(model)).thenReturn(false); |
| 91 | + appendModelLoader(notHandlingModelLoader); |
| 92 | + |
| 93 | + assertThat(registry.getModelLoaders(model)).containsExactly(handlingModelLoader); |
| 94 | + } |
| 95 | + |
| 96 | + private void appendModelLoader(final ModelLoader<Object, Object> modelLoader) { |
| 97 | + registry.append( |
| 98 | + Object.class, |
| 99 | + Object.class, |
| 100 | + new ModelLoaderFactory<Object, Object>() { |
| 101 | + @NonNull |
| 102 | + @Override |
| 103 | + public ModelLoader<Object, Object> build(@NonNull MultiModelLoaderFactory multiFactory) { |
| 104 | + return modelLoader; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void teardown() {} |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + @SuppressWarnings("unchecked") |
| 113 | + private static ModelLoader<Object, Object> mockModelLoader() { |
| 114 | + return mock(ModelLoader.class, MOCK_MODEL_LOADER_NAME); |
| 115 | + } |
| 116 | +} |
0 commit comments