result = applyTransformations(picasso, data, transformations, result); bitmap = result.getBitmap(); if (bitmap != null) {
@Test public void crashingOnTransformationThrows() { Transformation badTransformation = new Transformation() { @Override public RequestHandler.Result transform(RequestHandler.Result source) { throw new NullPointerException("hello"); } @Override public String key() { return "test"; } }; List<Transformation> transformations = Collections.singletonList(badTransformation); Bitmap original = Bitmap.createBitmap(10, 10, ARGB_8888); RequestHandler.Result result = new RequestHandler.Result(original, MEMORY, 0); Request data = new Request.Builder(URI_1).build(); try { BitmapHunter.applyTransformations(picasso, data, transformations, result); fail("Expected exception to be thrown."); } catch (RuntimeException e) { assertThat(e).hasMessageThat().isEqualTo("Transformation " + badTransformation.key() + " crashed with exception."); } }
@Test public void doesNotRecycleOriginalTransformationThrows() { Transformation badTransformation = new Transformation() { @Override public RequestHandler.Result transform(RequestHandler.Result source) { // Should recycle source. return new RequestHandler.Result(Bitmap.createBitmap(10, 10, ARGB_8888), MEMORY, 0); } @Override public String key() { return "test"; } }; List<Transformation> transformations = Collections.singletonList(badTransformation); Bitmap original = Bitmap.createBitmap(10, 10, ARGB_8888); RequestHandler.Result result = new RequestHandler.Result(original, MEMORY, 0); Request data = new Request.Builder(URI_1).build(); try { BitmapHunter.applyTransformations(picasso, data, transformations, result); fail("Expected exception to be thrown."); } catch (RuntimeException e) { assertThat(e).hasMessageThat().isEqualTo("Transformation " + badTransformation.key() + " mutated input Bitmap but failed to recycle the original."); } }
@Test public void nullResultFromTransformationThrows() { Transformation badTransformation = new Transformation() { @Override public RequestHandler.Result transform(RequestHandler.Result source) { return null; } @Override public String key() { return "test"; } }; List<Transformation> transformations = Collections.singletonList(badTransformation); Bitmap original = Bitmap.createBitmap(10, 10, ARGB_8888); RequestHandler.Result result = new RequestHandler.Result(original, MEMORY, 0); Request data = new Request.Builder(URI_1).build(); try { BitmapHunter.applyTransformations(picasso, data, transformations, result); fail("Expected exception to be thrown."); } catch (RuntimeException e) { assertThat(e).hasMessageThat().contains( "Transformation " + badTransformation.key() + " returned null"); } }
@Test public void recycledOriginalTransformationThrows() { Transformation badTransformation = new Transformation() { @Override public RequestHandler.Result transform(RequestHandler.Result source) { source.getBitmap().recycle(); return source; } @Override public String key() { return "test"; } }; List<Transformation> transformations = Collections.singletonList(badTransformation); Bitmap original = Bitmap.createBitmap(10, 10, ARGB_8888); RequestHandler.Result result = new RequestHandler.Result(original, MEMORY, 0); Request data = new Request.Builder(URI_1).build(); try { BitmapHunter.applyTransformations(picasso, data, transformations, result); fail("Expected exception to be thrown."); } catch (RuntimeException e) { assertThat(e).hasMessageThat().isEqualTo("Transformation " + badTransformation.key() + " returned input Bitmap but recycled it."); } }