List<String> test(DownsampleStrategy strategy) throws IOException { List<String> results = new ArrayList<>(); for (Api api : apis) { results.addAll(api.test(sourceWidth, sourceHeight, targetWidth, targetHeight, strategy)); } return results; } }
new Tester(DownsampleStrategy.FIT_CENTER) .setTargetDimensions(100, 100) .givenSquareImageWithDimensionOf(100, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(200, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(400, onAllApisAndAllFormatsExpect(100, 100)) .givenImageWithDimensionsOf(300, 300, atAndAbove(KITKAT).with(allFormats().expect(100, 100)), below(KITKAT).with(allFormats().expect(150, 150))) .givenImageWithDimensionsOf(799, 100, atAndAbove(KITKAT).with(allFormats().expect(100, 13)), below(KITKAT) .with( formats(JPEG) .expect(199, 25))) .givenImageWithDimensionsOf(800, 100, atAndAbove(KITKAT).with(allFormats().expect(100, 13)), below(KITKAT).with(allFormats().expect(200, 25))) .givenImageWithDimensionsOf(801, 100, atAndAbove(VERSION_CODES.N) .with( formats(JPEG, WEBP) formats(PNG) .expect(100, 12)), apis(KITKAT, VERSION_CODES.M) .with( formats(JPEG)
new Tester(DownsampleStrategy.AT_MOST) .setTargetDimensions(100, 100) .givenSquareImageWithDimensionOf(100, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(200, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(400, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(300, onAllApisAndAllFormatsExpect(75, 75)) .givenImageWithDimensionsOf(799, 100, atAndAbove(VERSION_CODES.N) .with( formats(JPEG, WEBP) formats(PNG) .expect(99, 12)), below(VERSION_CODES.N) .with( formats(JPEG) .expect(99, 12))) .givenImageWithDimensionsOf(800, 100, atAndAbove(VERSION_CODES.N) .with( formats(JPEG, WEBP) formats(PNG) .expect(100, 12)), below(VERSION_CODES.N) .with( formats(JPEG)
new Tester(DownsampleStrategy.CENTER_INSIDE) .setTargetDimensions(100, 100) .givenSquareImageWithDimensionOf(100, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(400, onAllApisAndAllFormatsExpect(100, 100)) .givenImageWithDimensionsOf(300, 300, atAndAbove(KITKAT).with(allFormats().expect(100, 100)), below(KITKAT).with(allFormats().expect(150, 150))) .givenImageWithDimensionsOf(799, 100, atAndAbove(KITKAT).with(allFormats().expect(100, 13)), below(KITKAT) .with( formats(JPEG) .expect(199, 25))) .givenImageWithDimensionsOf(800, 100, atAndAbove(KITKAT).with(allFormats().expect(100, 13)), below(KITKAT).with(allFormats().expect(200, 25))) .givenImageWithDimensionsOf(801, 100, atAndAbove(VERSION_CODES.N) .with( formats(JPEG, WEBP) formats(PNG) .expect(100, 12)), apis(KITKAT, VERSION_CODES.M) .with( formats(JPEG) formats(PNG, WEBP)
@Test public void calculateScaling_withCenterOutside() throws IOException { new Tester(DownsampleStrategy.CENTER_OUTSIDE) .setTargetDimensions(100, 100) .givenSquareImageWithDimensionOf(100, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(200, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(400, onAllApisAndAllFormatsExpect(100, 100)) .givenImageWithDimensionsOf(300, 300, atAndAbove(KITKAT).with(allFormats().expect(100, 100)), below(KITKAT).with(allFormats().expect(150, 150))) .givenImageWithDimensionsOf(799, 100, onAllApisAndAllFormatsExpect(799, 100)) .givenImageWithDimensionsOf(800, 100, onAllApisAndAllFormatsExpect(800, 100)) .givenImageWithDimensionsOf(801, 100, onAllApisAndAllFormatsExpect(801, 100)) .givenImageWithDimensionsOf(100, 800, onAllApisAndAllFormatsExpect(100, 800)) .givenImageWithDimensionsOf(87, 78, atAndAbove(KITKAT).with(allFormats().expect(112, 100)), below(KITKAT).with(allFormats().expect(87, 78))) // Upscaling .setTargetDimensions(500, 500) .givenSquareImageWithDimensionOf(200, atAndAbove(KITKAT).with(allFormats().expect(500, 500)), below(KITKAT).with(allFormats().expect(200, 200))) .givenSquareImageWithDimensionOf(450, atAndAbove(KITKAT).with(allFormats().expect(500, 500)), below(KITKAT).with(allFormats().expect(450, 450))) .givenImageWithDimensionsOf(200, 450, atAndAbove(KITKAT).with(allFormats().expect(500, 1125)), below(KITKAT).with(allFormats().expect(200, 450))) .run(); }
@Test public void calculateScaling_withNone() throws IOException { new Tester(DownsampleStrategy.NONE) .setTargetDimensions(100, 100) .givenSquareImageWithDimensionOf(100, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(200, onAllApisAndAllFormatsExpect(200, 200)) .givenSquareImageWithDimensionOf(400, onAllApisAndAllFormatsExpect(400, 400)) .givenSquareImageWithDimensionOf(300, onAllApisAndAllFormatsExpect(300, 300)) .givenImageWithDimensionsOf(799, 100, onAllApisAndAllFormatsExpect(799, 100)) .givenImageWithDimensionsOf(800, 100, onAllApisAndAllFormatsExpect(800, 100)) .givenImageWithDimensionsOf(801, 100, onAllApisAndAllFormatsExpect(801, 100)) .givenImageWithDimensionsOf(100, 800, onAllApisAndAllFormatsExpect(100, 800)) .givenImageWithDimensionsOf(87, 78, onAllApisAndAllFormatsExpect(87, 78)) .setTargetDimensions(500, 500) .givenSquareImageWithDimensionOf(200, onAllApisAndAllFormatsExpect(200, 200)) .givenSquareImageWithDimensionOf(450, onAllApisAndAllFormatsExpect(450, 450)) .givenImageWithDimensionsOf(200, 450, onAllApisAndAllFormatsExpect(200, 450)) .run(); }
@Test public void calculateScaling_withAtLeast() throws IOException { new Tester(DownsampleStrategy.AT_LEAST) .setTargetDimensions(100, 100) .givenSquareImageWithDimensionOf(100, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(200, onAllApisAndAllFormatsExpect(100, 100)) .givenSquareImageWithDimensionOf(300, onAllApisAndAllFormatsExpect(150, 150)) .givenImageWithDimensionsOf(799, 100, onAllApisAndAllFormatsExpect(799, 100)) .givenImageWithDimensionsOf(800, 100, onAllApisAndAllFormatsExpect(800, 100)) .givenImageWithDimensionsOf(801, 100, onAllApisAndAllFormatsExpect(801, 100)) .givenImageWithDimensionsOf(100, 800, onAllApisAndAllFormatsExpect(100, 800)) .givenImageWithDimensionsOf(87, 78, onAllApisAndAllFormatsExpect(87, 78)) // Upscaling .setTargetDimensions(500, 500) .givenSquareImageWithDimensionOf(200, onAllApisAndAllFormatsExpect(200, 200)) .givenSquareImageWithDimensionOf(450, onAllApisAndAllFormatsExpect(450, 450)) .givenImageWithDimensionsOf(200, 450, onAllApisAndAllFormatsExpect(200, 450)) .run(); }
static Api onAllApisAndAllFormatsExpect(int width, int height) { return allApis().with(allFormats().expect(width, height)); }
Api with(Formats... formats) { return new Api(minVersion, maxVersion, formats); } }