static BitmapHunter mockHunter(String key, RequestHandler.Result result, Action action) { Request data = new Request.Builder(URI_1).build(); BitmapHunter hunter = mock(BitmapHunter.class); when(hunter.getKey()).thenReturn(key); when(hunter.getResult()).thenReturn(result); when(hunter.getData()).thenReturn(data); when(hunter.getAction()).thenReturn(action); Picasso picasso = mockPicasso(); when(hunter.getPicasso()).thenReturn(picasso); return hunter; }
@Test public void centerInsideResultMatchesTargetSizeWhileDesiredWidthIs0() { Request request = new Request.Builder(URI_1).resize(0, 642).centerInside().build(); Bitmap source = Bitmap.createBitmap(640, 640, ARGB_8888); Bitmap result = transformResult(request, source, 0); assertThat(result.getWidth()).isEqualTo(642); assertThat(result.getHeight()).isEqualTo(642); }
@Test public void centerCropResultMatchesTargetSizeWhileDesiredHeightIs0() { Request request = new Request.Builder(URI_1).resize(1080, 0).centerCrop().build(); Bitmap source = Bitmap.createBitmap(640, 640, ARGB_8888); Bitmap result = transformResult(request, source, 0); assertThat(result.getWidth()).isEqualTo(1080); assertThat(result.getHeight()).isEqualTo(1080); }
@Test public void onlyScaleDownOriginalBiggerWidthIs0() { Bitmap source = Bitmap.createBitmap(50, 50, ARGB_8888); Request data = new Request.Builder(URI_1).resize(0, 40).onlyScaleDown().build(); Bitmap result = transformResult(data, source, 0); ShadowBitmap shadowBitmap = shadowOf(result); assertThat(shadowBitmap.getCreatedFromBitmap()).isSameAs(source); Matrix matrix = shadowBitmap.getCreatedFromMatrix(); ShadowMatrix shadowMatrix = shadowOf(matrix); assertThat(shadowMatrix.getPreOperations()).containsExactly("scale 0.8 0.8"); }
@Test public void centerCropResultMatchesTargetSize() { Request request = new Request.Builder(URI_1).resize(1080, 642).centerCrop().build(); Bitmap source = Bitmap.createBitmap(640, 640, ARGB_8888); Bitmap result = transformResult(request, source, 0); assertThat(result.getWidth()).isEqualTo(1080); assertThat(result.getHeight()).isEqualTo(642); }
@Test public void keepsAspectRationWhileResizingWhenDesiredHeightIs0() { Request request = new Request.Builder(URI_1).resize(0, 10).build(); Bitmap source = Bitmap.createBitmap(40, 20, ARGB_8888); Bitmap result = transformResult(request, source, 0); ShadowBitmap shadowBitmap = shadowOf(result); Matrix matrix = shadowBitmap.getCreatedFromMatrix(); ShadowMatrix shadowMatrix = shadowOf(matrix); assertThat(shadowMatrix.getPreOperations()).containsExactly("scale 0.5 0.5"); }
@Test public void rotation180Sizing() { Request data = new Request.Builder(URI_1).rotate(180).resize(5, 10).build(); Bitmap source = Bitmap.createBitmap(10, 10, ARGB_8888); Bitmap result = transformResult(data, source, 0); ShadowBitmap shadowBitmap = shadowOf(result); assertThat(shadowBitmap.getCreatedFromBitmap()).isSameAs(source); Matrix matrix = shadowBitmap.getCreatedFromMatrix(); ShadowMatrix shadowMatrix = shadowOf(matrix); assertThat(shadowMatrix.getPreOperations()).contains("scale 0.5 1.0"); }
@Test public void exifRotation() { Request data = new Request.Builder(URI_1).rotate(-45).build(); Bitmap source = Bitmap.createBitmap(10, 10, ARGB_8888); Bitmap result = transformResult(data, source, ORIENTATION_ROTATE_90); ShadowBitmap shadowBitmap = shadowOf(result); assertThat(shadowBitmap.getCreatedFromBitmap()).isSameAs(source); Matrix matrix = shadowBitmap.getCreatedFromMatrix(); ShadowMatrix shadowMatrix = shadowOf(matrix); assertThat(shadowMatrix.getPreOperations()).containsExactly("rotate 90.0"); }
@Test public void simpleResizeWithCenterInsideAndEncryption() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerInside().build(); Request output = secureTransformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); assertThat(output.centerInside).isFalse(); String expected = Thumbor.create(HOST, KEY).buildImage(IMAGE).resize(50, 50).fitIn().toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); } }
@Test public void simpleResizeWithCenterCrop() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerCrop().build(); Request output = transformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); assertThat(output.centerCrop).isFalse(); String expected = Thumbor.create(HOST).buildImage(IMAGE).resize(50, 50).toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Test public void simpleResize() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).build(); Request output = transformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); String expected = Thumbor.create(HOST).buildImage(IMAGE).resize(50, 50).toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Test public void nonResizedRequestsAreNotTransformed() { Request input = new Request.Builder(IMAGE_URI).build(); Request output = transformer.transformRequest(input); assertThat(output).isSameAs(input); }
@Test public void onlyScaleDownOriginalSmallerHeightIs0() { Bitmap source = Bitmap.createBitmap(50, 50, ARGB_8888); Request data = new Request.Builder(URI_1).resize(60, 0).onlyScaleDown().build(); Bitmap result = transformResult(data, source, 0); assertThat(result).isSameAs(source); ShadowBitmap shadowBitmap = shadowOf(result); assertThat(shadowBitmap.getCreatedFromBitmap()).isNull(); }
@Test public void onlyScaleDownOriginalSmallerWidthIs0() { Bitmap source = Bitmap.createBitmap(50, 50, ARGB_8888); Request data = new Request.Builder(URI_1).resize(0, 60).onlyScaleDown().build(); Bitmap result = transformResult(data, source, 0); assertThat(result).isSameAs(source); ShadowBitmap shadowBitmap = shadowOf(result); assertThat(shadowBitmap.getCreatedFromBitmap()).isNull(); }
@Test public void exifTransverse() { Request data = new Request.Builder(URI_1).rotate(-45).build(); Bitmap source = Bitmap.createBitmap(10, 10, ARGB_8888); Bitmap result = transformResult(data, source, ORIENTATION_TRANSVERSE); ShadowBitmap shadowBitmap = shadowOf(result); assertThat(shadowBitmap.getCreatedFromBitmap()).isSameAs(source); Matrix matrix = shadowBitmap.getCreatedFromMatrix(); ShadowMatrix shadowMatrix = shadowOf(matrix); assertThat(shadowMatrix.getPostOperations()).containsExactly("scale -1.0 1.0"); assertThat(shadowMatrix.getPreOperations()).containsExactly("rotate 270.0"); }
@Test public void exifRotationSizing() { Request data = new Request.Builder(URI_1).resize(5, 10).build(); Bitmap source = Bitmap.createBitmap(10, 10, ARGB_8888); Bitmap result = transformResult(data, source, ORIENTATION_ROTATE_90); ShadowBitmap shadowBitmap = shadowOf(result); assertThat(shadowBitmap.getCreatedFromBitmap()).isSameAs(source); Matrix matrix = shadowBitmap.getCreatedFromMatrix(); ShadowMatrix shadowMatrix = shadowOf(matrix); assertThat(shadowMatrix.getPreOperations()).contains("scale 1.0 0.5"); }
@Test public void performSubmitWithFetchAction() { String pausedTag = "pausedTag"; dispatcher.pausedTags.add(pausedTag); assertThat(dispatcher.pausedActions).isEmpty(); FetchAction fetchAction1 = new FetchAction(mockPicasso(), new Request.Builder(URI_1).tag(pausedTag).build(), null); FetchAction fetchAction2 = new FetchAction(mockPicasso(), new Request.Builder(URI_1).tag(pausedTag).build(), null); dispatcher.performSubmit(fetchAction1); dispatcher.performSubmit(fetchAction2); assertThat(dispatcher.pausedActions).hasSize(2); }
@Test public void ensureBuilderIsCleared() { new Request.Builder(RESOURCE_ID_URI).build(); assertThat(Utils.MAIN_THREAD_KEY_BUILDER.length()).isEqualTo(0); new Request.Builder(URI_1).build(); assertThat(Utils.MAIN_THREAD_KEY_BUILDER.length()).isEqualTo(0); }
@Test public void calculateInSampleSizeKeepAspectRatioWithHeight() { final BitmapFactory.Options options = new BitmapFactory.Options(); Request data = new Request.Builder(URI_1).resize(0, 100).build(); calculateInSampleSize(data.targetWidth, data.targetHeight, 800, 200, options, data); assertThat(options.inSampleSize).isEqualTo(2); }
@Test public void bitmapConfig() { for (Bitmap.Config config : Bitmap.Config.values()) { Request data = new Request.Builder(URI_1).config(config).build(); Request copy = data.newBuilder().build(); assertThat(createBitmapOptions(data).inPreferredConfig).isSameAs(config); assertThat(createBitmapOptions(copy).inPreferredConfig).isSameAs(config); } }