List<LoadData<?>> getLoadData() { if (!isLoadDataSet) { isLoadDataSet = true; loadData.clear(); List<ModelLoader<Object, ?>> modelLoaders = glideContext.getRegistry().getModelLoaders(model); int size = modelLoaders.size(); for (int i = 0; i < size; i++) { ModelLoader<Object, ?> modelLoader = modelLoaders.get(i); LoadData<?> current = modelLoader.buildLoadData(model, width, height, options); if (current != null) { loadData.add(current); } } } return loadData; }
@SuppressWarnings("unchecked") private <T, Z> void registerFailFactory(Class<T> failModel, Class<Z> failResource) throws Exception { DataFetcher<Z> failFetcher = mock(DataFetcher.class); doAnswer(new Util.CallDataReady<>(null)) .when(failFetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); when(failFetcher.getDataClass()).thenReturn(failResource); ModelLoader<T, Z> failLoader = mock(ModelLoader.class); when(failLoader.buildLoadData(isA(failModel), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), failFetcher)); when(failLoader.handles(isA(failModel))).thenReturn(true); ModelLoaderFactory<T, Z> failFactory = mock(ModelLoaderFactory.class); when(failFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(failLoader); Glide.get(getContext()).getRegistry().prepend(failModel, failResource, failFactory); }
@SuppressWarnings("unchecked") private <T> ModelLoader<T, InputStream> mockStreamModelLoader(final Class<T> modelClass) { ModelLoader<T, InputStream> modelLoader = mock(ModelLoader.class); DataFetcher<InputStream> fetcher = mock(DataFetcher.class); try { doAnswer(new Util.CallDataReady<>(new ByteArrayInputStream(new byte[0]))).when(fetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); } catch (Exception e) { // Do nothing. } when(fetcher.getDataClass()).thenReturn(InputStream.class); when(modelLoader.buildLoadData(isA(modelClass), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); when(modelLoader.handles(isA(modelClass))).thenReturn(true); return modelLoader; }
private static <X, Y> void registerMockModelLoader(Class<X> modelClass, Class<Y> dataClass, Y loadedData, Registry registry) { DataFetcher<Y> mockStreamFetcher = mock(DataFetcher.class); when(mockStreamFetcher.getDataClass()).thenReturn(dataClass); try { doAnswer(new Util.CallDataReady<>(loadedData)) .when(mockStreamFetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); } catch (Exception e) { throw new RuntimeException(e); } ModelLoader<X, Y> mockUrlLoader = mock(ModelLoader.class); when(mockUrlLoader.buildLoadData(isA(modelClass), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), mockStreamFetcher)); when(mockUrlLoader.handles(isA(modelClass))).thenReturn(true); ModelLoaderFactory<X, Y> mockUrlLoaderFactory = mock(ModelLoaderFactory.class); when(mockUrlLoaderFactory.build(isA(MultiModelLoaderFactory.class))) .thenReturn(mockUrlLoader); registry.replace(modelClass, dataClass, mockUrlLoaderFactory); }
@Test public void testBuildsNewUrlIfNotPresentInCache() { int width = 10; int height = 11; urlLoader.resultUrl = "fakeUrl"; when(wrapped.buildLoadData(any(GlideUrl.class), eq(width), eq(height), eq(options))) .thenAnswer(new Answer<ModelLoader.LoadData<InputStream>>() { @Override public ModelLoader.LoadData<InputStream> answer(InvocationOnMock invocationOnMock) throws Throwable { GlideUrl glideUrl = (GlideUrl) invocationOnMock.getArguments()[0]; assertEquals(urlLoader.resultUrl, glideUrl.toStringUrl()); return new ModelLoader.LoadData<>(mock(Key.class), fetcher); } }); assertEquals(fetcher, urlLoader.buildLoadData(new GlideUrl(urlLoader.resultUrl), width, height, options).fetcher); }
@SuppressWarnings("unchecked") private <T, Z> void registerFailFactory(Class<T> failModel, Class<Z> failResource) { DataFetcher<Z> failFetcher = mock(DataFetcher.class); doAnswer(new Util.CallDataReady<>(null)) .when(failFetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); when(failFetcher.getDataClass()).thenReturn(failResource); ModelLoader<T, Z> failLoader = mock(ModelLoader.class); when(failLoader.buildLoadData(isA(failModel), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), failFetcher)); when(failLoader.handles(isA(failModel))).thenReturn(true); ModelLoaderFactory<T, Z> failFactory = mock(ModelLoaderFactory.class); when(failFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(failLoader); Glide.get(context).getRegistry().prepend(failModel, failResource, failFactory); }
@Test public void testBuildsNewUrlIfNotPresentInCache() { int width = 10; int height = 11; urlLoader.resultUrl = "fakeUrl"; when(wrapped.buildLoadData(any(GlideUrl.class), eq(width), eq(height), eq(options))) .thenAnswer(new Answer<ModelLoader.LoadData<InputStream>>() { @Override public ModelLoader.LoadData<InputStream> answer(InvocationOnMock invocationOnMock) throws Throwable { GlideUrl glideUrl = (GlideUrl) invocationOnMock.getArguments()[0]; assertEquals(urlLoader.resultUrl, glideUrl.toStringUrl()); return new ModelLoader.LoadData<>(mock(Key.class), fetcher); } }); assertEquals( fetcher, Preconditions.checkNotNull( urlLoader.buildLoadData( new GlideUrl(urlLoader.resultUrl), width, height, options)).fetcher); }
@Test public void testDoesNotInteractWithModelCacheIfNull() { TestLoader urlLoader = new TestLoader(wrapped, null); urlLoader.resultUrl = "fakeUrl"; int width = 456; int height = 789; when(wrapped.buildLoadData(any(GlideUrl.class), eq(width), eq(height), eq(options))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); assertEquals( fetcher, Preconditions.checkNotNull( urlLoader.buildLoadData(new Object(), width, height, options)).fetcher); }
@Override public void onSizeReady(int width, int height) { if (isCancelled) { return; } width = Math.round(sizeMultiplier * width); height = Math.round(sizeMultiplier * height); ResourceDecoder<InputStream, Z> cacheDecoder = loadProvider.getCacheDecoder(); ResourceDecoder<T, Z> decoder = loadProvider.getSourceDecoder(); ResourceEncoder <Z> encoder = loadProvider.getEncoder(); ResourceTranscoder<Z, R> transcoder = loadProvider.getTranscoder(); ModelLoader<A, T> modelLoader = loadProvider.getModelLoader(); final String id = modelLoader.getId(model); final DataFetcher<T> dataFetcher = modelLoader.getResourceFetcher(model, width, height); loadedFromMemoryCache = true; loadStatus = engine.load(id, width, height, cacheDecoder, dataFetcher, decoder, transformation, encoder, transcoder, priority, isMemoryCacheable, this); loadedFromMemoryCache = resource != null; }
@Override public boolean startNext() { while (modelLoaders == null || !hasNextModelLoader()) { sourceIdIndex++; if (sourceIdIndex >= cacheKeys.size()) { return false; } Key sourceId = cacheKeys.get(sourceIdIndex); Key originalKey = new DataCacheKey(sourceId, helper.getSignature()); cacheFile = helper.getDiskCache().get(originalKey); if (cacheFile != null) { this.sourceKey = sourceId; modelLoaders = helper.getModelLoaders(cacheFile); modelLoaderIndex = 0; } } loadData = null; boolean started = false; while (!started && hasNextModelLoader()) { ModelLoader<File, ?> modelLoader = modelLoaders.get(modelLoaderIndex++); loadData = modelLoader.buildLoadData(cacheFile, helper.getWidth(), helper.getHeight(), helper.getOptions()); if (loadData != null && helper.hasLoadPath(loadData.fetcher.getDataClass())) { started = true; loadData.fetcher.loadData(helper.getPriority(), this); } } return started; }
public <Model> List<ModelLoader<Model, ?>> getModelLoaders(Model model) { List<ModelLoader<Model, ?>> result = modelLoaderRegistry.getModelLoaders(model); if (result.isEmpty()) { throw new NoModelLoaderAvailableException(model); } return result; }
@SuppressWarnings("unchecked") private <T> void registerMockStreamModelLoader(final Class<T> modelClass) { ModelLoader<T, InputStream> modelLoader = mockStreamModelLoader(modelClass); ModelLoaderFactory<T, InputStream> modelLoaderFactory = mock(ModelLoaderFactory.class); when(modelLoaderFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(modelLoader); Glide.get(RuntimeEnvironment.application).getRegistry() .prepend(modelClass, InputStream.class, modelLoaderFactory); }
@Test public void testReturnsUrlFromCacheIfPresent() { Object model = new Object(); int width = 100; int height = 200; GlideUrl expectedUrl = mock(GlideUrl.class); when(modelCache.get(eq(model), eq(width), eq(height))).thenReturn(expectedUrl); when(wrapped.buildLoadData(eq(expectedUrl), eq(width), eq(height), eq(options))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); assertEquals(fetcher, urlLoader.buildLoadData(model, width, height, options).fetcher); }
@Test public void testDoesNotInteractWithModelCacheIfNull() { TestLoader urlLoader = new TestLoader(wrapped, null); urlLoader.resultUrl = "fakeUrl"; int width = 456; int height = 789; when(wrapped.buildLoadData(any(GlideUrl.class), eq(width), eq(height), eq(options))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); assertEquals(fetcher, urlLoader.buildLoadData(new Object(), width, height, options).fetcher); }
@SuppressWarnings("unchecked") private <T> void registerMockStreamModelLoader(final Class<T> modelClass) { ModelLoader<T, InputStream> modelLoader = mockStreamModelLoader(modelClass); ModelLoaderFactory<T, InputStream> modelLoaderFactory = mock(ModelLoaderFactory.class); when(modelLoaderFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(modelLoader); Glide.get(context).getRegistry() .prepend(modelClass, InputStream.class, modelLoaderFactory); }
@Test public void testReturnsUrlFromCacheIfPresent() { Object model = new Object(); int width = 100; int height = 200; GlideUrl expectedUrl = mock(GlideUrl.class); when(modelCache.get(eq(model), eq(width), eq(height))).thenReturn(expectedUrl); when(wrapped.buildLoadData(eq(expectedUrl), eq(width), eq(height), eq(options))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); assertEquals( fetcher, Preconditions.checkNotNull(urlLoader.buildLoadData(model, width, height, options)).fetcher); }
public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader<GlideUrl, InputStream> urlLoader, int width, int height) { this.context = context; this.lastFMRestClient = lastFMRestClient; this.model = model; this.urlLoader = urlLoader; this.width = width; this.height = height; }
GenericTranscodeRequest(Context context, Glide glide, A model, ModelLoader<A, T> modelLoader, Class<T> dataClass, Class<Z> resourceClass) { super(context, model, build(glide, modelLoader, dataClass, resourceClass, (ResourceTranscoder<Z, Z>) null), resourceClass, glide); this.context = context; this.model = model; this.glide = glide; this.modelLoader = modelLoader; this.dataClass = dataClass; this.resourceClass = resourceClass; }
private static <A, T, Z, R> LoadProvider<A, T, Z, R> build(Glide glide, ModelLoader<A, T> modelLoader, Class<T> dataClass, Class<Z> resourceClass, ResourceTranscoder<Z, R> transcoder) { if (transcoder == null) { transcoder = UnitTranscoder.get(); } return new FixedLoadProvider<A, T, Z, R>(modelLoader, transcoder, glide.buildDataProvider(dataClass, resourceClass)); }
private static <A, Z, R> FixedLoadProvider<A, ImageVideoWrapper, Z, R> buildProvider(Glide glide, ModelLoader<A, InputStream> streamModelLoader, ModelLoader<A, ParcelFileDescriptor> fileDescriptorModelLoader, Class<Z> resourceClass, Class<R> transcodedClass, ResourceTranscoder<Z, R> transcoder) { return streamModelLoader == null && fileDescriptorModelLoader == null ? null : new FixedLoadProvider<A, ImageVideoWrapper, Z, R>( new ImageVideoModelLoader<A>(streamModelLoader, fileDescriptorModelLoader), transcoder != null ? transcoder : glide.buildTranscoder(resourceClass, transcodedClass), glide.buildDataProvider(ImageVideoWrapper.class, resourceClass)); }
DrawableTypeRequest(A model, ModelLoader<A, InputStream> streamModelLoader, ModelLoader<A, ParcelFileDescriptor> fileDescriptorModelLoader, Context context, Glide glide) { super(context, model, buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, GifBitmapWrapper.class, Drawable.class, null), glide); this.model = model; this.streamModelLoader = streamModelLoader; this.fileDescriptorModelLoader = fileDescriptorModelLoader; this.context = context; this.glide = glide; }
private static <A, R> FixedLoadProvider<A, ImageVideoWrapper, Bitmap, R> buildProvider(Glide glide, ModelLoader<A, InputStream> streamModelLoader, ModelLoader<A, ParcelFileDescriptor> fileDescriptorModelLoader, Class<R> transcodedClass, ResourceTranscoder<Bitmap, R> transcoder) { return streamModelLoader == null && fileDescriptorModelLoader == null ? null : new FixedLoadProvider<A, ImageVideoWrapper, Bitmap, R>( new ImageVideoModelLoader<A>(streamModelLoader, fileDescriptorModelLoader), transcoder != null ? transcoder : glide.buildTranscoder(Bitmap.class, transcodedClass), glide.buildDataProvider(ImageVideoWrapper.class, Bitmap.class)); }
BitmapTypeRequest(Context context, A model, ModelLoader<A, InputStream> streamModelLoader, ModelLoader<A, ParcelFileDescriptor> fileDescriptorModelLoader, Glide glide) { super(context, model, buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, Bitmap.class, null), Bitmap.class, glide); this.context = context; this.model = model; this.streamModelLoader = streamModelLoader; this.fileDescriptorModelLoader = fileDescriptorModelLoader; this.glide = glide; }
public FixedLoadProvider(ModelLoader<A, T> modelLoader, ResourceTranscoder<Z, R> transcoder, DataLoadProvider<T, Z> dataLoadProvider) { if (modelLoader == null) { throw new NullPointerException("ModelLoader must not be null"); } this.modelLoader = modelLoader; if (transcoder == null) { throw new NullPointerException("Transcoder must not be null"); } this.transcoder = transcoder; if (dataLoadProvider == null) { throw new NullPointerException("DataLoadProvider must not be null"); } this.dataLoadProvider = dataLoadProvider; }
private GenericTypeRequest(Context context, Glide glide, A model, ModelLoader<A, T> modelLoader, Class<T> dataClass) { this.context = context; this.glide = glide; this.model = model; this.modelLoader = modelLoader; this.dataClass = dataClass; }