@Override public DataFetcher<InputStream> getResourceFetcher(final String model, int i, int i1) { return new DataFetcher<InputStream>() { @Override public InputStream loadData(Priority priority) throws Exception { throw new IOException(); } @Override public void cleanup() { } @Override public String getId() { return model; } @Override public void cancel() { } }; }
private <Data> Resource<R> decodeFromData(DataFetcher<?> fetcher, Data data, DataSource dataSource) throws GlideException { try { if (data == null) { return null; } long startTime = LogTime.getLogTime(); Resource<R> result = decodeFromFetcher(data, dataSource); if (Log.isLoggable(TAG, Log.VERBOSE)) { logWithTimeAndKey("Decoded result " + result, startTime); } return result; } finally { fetcher.cleanup(); } }
@Override public LoadData<Data> buildLoadData(Model model, int width, int height, Options options) { Key sourceKey = null; int size = modelLoaders.size(); List<DataFetcher<Data>> fetchers = new ArrayList<>(size); for (int i = 0; i < size; i++) { ModelLoader<Model, Data> modelLoader = modelLoaders.get(i); if (modelLoader.handles(model)) { LoadData<Data> loadData = modelLoader.buildLoadData(model, width, height, options); if (loadData != null) { sourceKey = loadData.sourceKey; fetchers.add(loadData.fetcher); } } } return !fetchers.isEmpty() ? new LoadData<>(sourceKey, new MultiFetcher<>(fetchers, exceptionListPool)) : null; }
@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); }
@Override public void onDataFetcherReady(Key sourceKey, Object data, DataFetcher<?> fetcher, DataSource dataSource, Key attemptedKey) { this.currentSourceKey = sourceKey; this.currentData = data; this.currentFetcher = fetcher; this.currentDataSource = dataSource; this.currentAttemptingKey = attemptedKey; if (Thread.currentThread() != currentThread) { runReason = RunReason.DECODE_DATA; callback.reschedule(this); } else { TraceCompat.beginSection("DecodeJob.decodeFromRetrievedData"); try { decodeFromRetrievedData(); } finally { TraceCompat.endSection(); } } }
@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); }
@Override public DataFetcher<InputStream> getResourceFetcher(final String model, int width, int height) { return new DataFetcher<InputStream>() { @Override public InputStream loadData(Priority priority) throws Exception { throw new IOException("Forced Glide network failure"); } @Override public void cleanup() { } @Override public String getId() { return model; } @Override public void cancel() { } }; }
@Override public DataFetcher<InputStream> getResourceFetcher(final AppInfo model, int width, int height) { return new DataFetcher<InputStream>() { @Override public InputStream loadData(Priority priority) throws Exception { mCancelled = false; PackageManager pm = mContext.getPackageManager(); if (mCancelled) return null; return drawableToStream(pm.getApplicationInfo(model.packageName, 0).loadIcon(pm)); } @Override public void cleanup() { } @Override public String getId() { return String.valueOf(model.packageName.hashCode()); } @Override public void cancel() { mCancelled = true; } }; }
@Override public DataFetcher<InputStream> getResourceFetcher(final Uri model, int width, int height) { return new DataFetcher<InputStream>() { @Override public InputStream loadData(Priority priority) throws Exception { throw new IOException(); } @Override public void cleanup() { } @Override public String getId() { return model.toString(); } @Override public void cancel() { } }; }
@Override public DataFetcher<InputStream> getResourceFetcher(final String model, int i, int i1) { return new DataFetcher<InputStream>() { @Override public InputStream loadData(Priority priority) { return null; } @Override public void cleanup() { } @Override public String getId() { return model; } @Override public void cancel() { } }; }
@Override public DataFetcher<InputStream> getResourceFetcher(final String model, int width, int height) { return new DataFetcher<InputStream>() { @Override public InputStream loadData(Priority priority) throws Exception { return new NfsFileInputStream(ctx, model); } @Override public void cleanup() { } @Override public String getId() { return model; } @Override public void cancel() { } }; }
@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 DataFetcher<InputStream> getResourceFetcher(T model, int width, int height) { final String id = getId(model); GlideUrl result = null; if (modelCache != null) { result = modelCache.get(id, width, height); } if (result == null) { String stringURL = getUrl(model, width, height); result = new GlideUrl(stringURL); if (modelCache != null) { modelCache.put(id, width, height, result); } } return concreteLoader.getResourceFetcher(result, width, height); }
@Override public DataFetcher<FallbackGlideParams> getResourceFetcher(final FallbackGlideParams model, int width, int height) { return new DataFetcher<FallbackGlideParams>() { @Override public FallbackGlideParams loadData(Priority priority) throws Exception { return model; } @Override public void cleanup() { } @Override public String getId() { return model.getId(); } @Override public void cancel() { } }; }
@Test public void testReturnsInputStreamOnStatusOk() throws Exception { String expected = "fakedata"; mockWebServer.enqueue(new MockResponse().setBody(expected).setResponseCode(200)); DataFetcher<InputStream> fetcher = getFetcher(); fetcher.loadData(Priority.HIGH, callback); waitForResponseLatch.await(); verify(callback).onDataReady(streamCaptor.capture()); assertStreamOf(expected, streamCaptor.getValue()); }
@Override public void onDataFetcherReady(Key sourceKey, Object data, DataFetcher<?> fetcher, DataSource dataSource, Key attemptedKey) { this.currentSourceKey = sourceKey; this.currentData = data; this.currentFetcher = fetcher; this.currentDataSource = dataSource; this.currentAttemptingKey = attemptedKey; if (Thread.currentThread() != currentThread) { runReason = RunReason.DECODE_DATA; callback.reschedule(this); } else { decodeFromRetrievedData(); } }
@Override public void onDataFetcherFailed(Key attemptedKey, Exception e, DataFetcher<?> fetcher, DataSource dataSource) { fetcher.cleanup(); GlideException exception = new GlideException("Fetching data failed", e); exception.setLoggingDetails(attemptedKey, dataSource, fetcher.getDataClass()); exceptions.add(exception); if (Thread.currentThread() != currentThread) { runReason = RunReason.SWITCH_TO_SOURCE_SERVICE; callback.reschedule(this); } else { runGenerators(); } }
@Override public void onDataFetcherReady(Key sourceKey, Object data, DataFetcher<?> fetcher, DataSource dataSource, Key attemptedKey) { // This data fetcher will be loading from a File and provide the wrong data source, so override // with the data source of the original fetcher cb.onDataFetcherReady(sourceKey, data, fetcher, loadData.fetcher.getDataSource(), sourceKey); }
@Override public void cleanup() { exceptionListPool.release(exceptions); exceptions = null; for (DataFetcher<Data> fetcher : fetchers) { fetcher.cleanup(); } }
@SuppressWarnings("unchecked") @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { DataFetcher.DataCallback<T> callback = (DataFetcher.DataCallback<T>) invocationOnMock.getArguments()[1]; callback.onDataReady(data); return null; }
@Override public void onDataFetcherFailed(Key attemptedKey, Exception e, DataFetcher<?> fetcher, DataSource dataSource) { fetcher.cleanup(); GlideException exception = new GlideException("Fetching data failed", e); exception.setLoggingDetails(attemptedKey, dataSource, fetcher.getDataClass()); throwables.add(exception); if (Thread.currentThread() != currentThread) { runReason = RunReason.SWITCH_TO_SOURCE_SERVICE; callback.reschedule(this); } else { runGenerators(); } }
@Override public void cleanup() { if (exceptions != null) { throwableListPool.release(exceptions); } exceptions = null; for (DataFetcher<Data> fetcher : fetchers) { fetcher.cleanup(); } }
@Test public void testCanHandleByteArray() { byte[] data = new byte[10]; DataFetcher<Object> fetcher = Preconditions.checkNotNull(loader.buildLoadData(data, -1, -1, options)).fetcher; assertNotNull(fetcher); }