SavingStreamProvider implements BitmapProvider { @Override public Bitmap load() throws IOException { Bitmap bitmap; try { bitmap = loadBitmap(); } catch (OutOfMemoryError e) { if (request.memoryCache() != null) request.memoryCache().clear(); System.gc(); try { bitmap = loadBitmap(); } catch (OutOfMemoryError e2) { throw e2; } } return bitmap; } SavingStreamProvider(MirageRequest request); @Override Bitmap load(); @Override String id();  }
@Test public void testLoads_doesNotSaveSource() throws IOException { MirageRequest request = mock(MirageRequest.class); UrlFactory factory = mock(UrlFactory.class); InputStream inputStream = mock(InputStream.class); when(factory.getStream(any(Uri.class))).thenReturn(inputStream); when(request.urlFactory()).thenReturn(factory); when(request.uri()).thenReturn(Uri.parse("http: DiskCache diskCache = mock(DiskCache.class); MemoryCache<String, Bitmap> memCache = mock(MemoryCache.class); when(request.diskCache()).thenReturn(diskCache); when(request.memoryCache()).thenReturn(memCache); SavingStreamProvider provider = new SavingStreamProvider(request) { @Override protected InputStream stream() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Bitmap bitmap = Bitmap.createBitmap(400, 600, Bitmap.Config.ARGB_8888); bitmap.compress(Bitmap.CompressFormat.PNG, 0 , bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata); return bs; } }; when(request.provider()).thenReturn(provider); Bitmap bitmap = provider.load(); Assert.assertNotNull(bitmap); verify(diskCache, times(0)).put(anyString(), any(DiskCache.Writer.class)); }
@Test public void testLoads_savesSource() throws IOException { MirageRequest request = mock(MirageRequest.class); UrlFactory factory = mock(UrlFactory.class); InputStream inputStream = mock(InputStream.class); when(factory.getStream(any(Uri.class))).thenReturn(inputStream); when(request.urlFactory()).thenReturn(factory); when(request.uri()).thenReturn(Uri.parse("http: when(request.isRequestShouldSaveSource()).thenReturn(true); when(request.keyMaker()).thenReturn(new SimpleKeyMaker()); DiskCache diskCache = mock(DiskCache.class); MemoryCache<String, Bitmap> memCache = mock(MemoryCache.class); when(request.diskCache()).thenReturn(diskCache); when(request.memoryCache()).thenReturn(memCache); SavingStreamProvider provider = new SavingStreamProvider(request) { @Override protected InputStream stream() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Bitmap bitmap = Bitmap.createBitmap(400, 600, Bitmap.Config.ARGB_8888); bitmap.compress(Bitmap.CompressFormat.PNG, 0 , bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata); return bs; } }; when(request.provider()).thenReturn(provider); Bitmap bitmap = provider.load(); verify(diskCache, times(1)).put(anyString(), any(DiskCache.Writer.class)); verify(diskCache, times(1)).get(anyString()); }
@Test public void testUsesBitmapOptions() throws IOException { MirageRequest request = mock(MirageRequest.class); UrlFactory factory = mock(UrlFactory.class); InputStream inputStream = mock(InputStream.class); when(factory.getStream(any(Uri.class))).thenReturn(inputStream); when(request.urlFactory()).thenReturn(factory); when(request.uri()).thenReturn(Uri.parse("http: when(request.isRequestShouldSaveSource()).thenReturn(true); when(request.keyMaker()).thenReturn(new SimpleKeyMaker()); when(request.isInSampleSizeDynamic()).thenReturn(true); when(request.getResizeTargetDimen()).thenReturn(50); when(request.isResizeSampleUndershoot()).thenReturn(true); DiskCache diskCache = mock(DiskCache.class); MemoryCache<String, Bitmap> memCache = mock(MemoryCache.class); when(request.diskCache()).thenReturn(diskCache); when(request.memoryCache()).thenReturn(memCache); SavingStreamProvider provider = new SavingStreamProvider(request) { @Override protected InputStream stream() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Bitmap bitmap = Bitmap.createBitmap(400, 600, Bitmap.Config.ARGB_8888); bitmap.compress(Bitmap.CompressFormat.PNG, 0 , bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata); return bs; } }; when(request.provider()).thenReturn(provider); provider.load(); verify(request, times(1)).inSampleSize(eq(2)); }