diff --git a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AbstractAttachmentCreationTaskTest.java b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AbstractAttachmentCreationTaskTest.java new file mode 100644 index 000000000..7f2a9ca14 --- /dev/null +++ b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AbstractAttachmentCreationTaskTest.java @@ -0,0 +1,49 @@ +package org.briarproject.briar.android.attachment; + +import android.content.res.AssetManager; + +import org.junit.Before; + +import java.io.IOException; +import java.io.InputStream; + +import static androidx.test.InstrumentationRegistry.getContext; +import static androidx.test.core.app.ApplicationProvider.getApplicationContext; + +abstract class AbstractAttachmentCreationTaskTest { + + private final ImageHelper imageHelper = new ImageHelperImpl(); + private final ImageSizeCalculator imageSizeCalculator = + new ImageSizeCalculator(imageHelper); + + private AttachmentCreationTask task; + + @Before + @SuppressWarnings("ConstantConditions") // add real objects when needed + public void setUp() { + task = new AttachmentCreationTask(null, + getApplicationContext().getContentResolver(), null, + imageSizeCalculator, null, null, true); + } + + void testCompress(String filename, String contentType) + throws IOException { + InputStream is = getAssetInputStream(filename); + task.compressImage(is, contentType); + } + + private InputStream getAssetInputStream(String name) throws IOException { + return getAssetManager().open(name); + } + + static String[] getAssetFiles(String path) throws IOException { + return getAssetManager().list(path); + } + + private static AssetManager getAssetManager() { + // pm.getResourcesForApplication(packageName).getAssets() did not work + //noinspection deprecation + return getContext().getAssets(); + } + +} diff --git a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentCreationTaskTest.java b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentCreationTaskTest.java index eed0e19e3..227dad0d5 100644 --- a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentCreationTaskTest.java +++ b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentCreationTaskTest.java @@ -1,36 +1,23 @@ package org.briarproject.briar.android.attachment; -import android.content.res.AssetManager; - import org.junit.Test; import org.junit.runner.RunWith; -import java.io.IOException; -import java.io.InputStream; import java.util.logging.Logger; import androidx.test.ext.junit.runners.AndroidJUnit4; import static android.os.Build.VERSION.SDK_INT; -import static androidx.test.InstrumentationRegistry.getContext; -import static androidx.test.core.app.ApplicationProvider.getApplicationContext; import static java.util.logging.Logger.getLogger; import static org.junit.Assume.assumeTrue; @RunWith(AndroidJUnit4.class) -public class AttachmentCreationTaskTest { +public class AttachmentCreationTaskTest + extends AbstractAttachmentCreationTaskTest { private static Logger LOG = getLogger(AttachmentCreationTaskTest.class.getName()); - private final ImageHelper imageHelper = new ImageHelperImpl(); - private final ImageSizeCalculator imageSizeCalculator = - new ImageSizeCalculator(imageHelper); - @SuppressWarnings("ConstantConditions") // add real objects when needed - private final AttachmentCreationTask task = new AttachmentCreationTask(null, - getApplicationContext().getContentResolver(), null, - imageSizeCalculator, null, null, true); - @Test public void testCompress100x100Png() throws Exception { testCompress("red-100x100.png", "image/png"); @@ -76,41 +63,4 @@ public class AttachmentCreationTaskTest { assumeTrue(SDK_INT >= 24); testCompress("error_large.gif", "image/gif"); } - - private void testCompress(String filename, String contentType) - throws Exception { - InputStream is = getAssetInputStream(filename); - task.compressImage(is, contentType); - } - - @Test - public void testPngSuiteCompress() throws Exception { - for (String file : getAssetFiles("PngSuite")) { - if (file.endsWith(".png")) { - InputStream is = getAssetInputStream("PngSuite/" + file); - try { - task.compressImage(is, "image/png"); - LOG.warning("PASS: " + file); - } catch (IOException e) { - LOG.warning("ERROR: " + file); - } - } - } - } - - private InputStream getAssetInputStream(String name) throws IOException { - LOG.info("getAssetInputStream: " + name); - return getAssetManager().open(name); - } - - private String[] getAssetFiles(String path) throws IOException { - return getAssetManager().list(path); - } - - private AssetManager getAssetManager() { - // pm.getResourcesForApplication(packageName).getAssets() did not work - //noinspection deprecation - return getContext().getAssets(); - } - } diff --git a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/PngSuiteAttachmentCreationTaskTest.java b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/PngSuiteAttachmentCreationTaskTest.java new file mode 100644 index 000000000..86e86f780 --- /dev/null +++ b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/PngSuiteAttachmentCreationTaskTest.java @@ -0,0 +1,52 @@ +package org.briarproject.briar.android.attachment; + +import org.briarproject.bramble.api.Pair; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.fail; + +@RunWith(Parameterized.class) +public class PngSuiteAttachmentCreationTaskTest + extends AbstractAttachmentCreationTaskTest { + + @Parameters + public static Iterable> data() throws IOException { + List> data = new ArrayList<>(); + for (String file : getAssetFiles("PngSuite")) { + if (file.endsWith(".png")) { + boolean shouldPass = !file.startsWith("x"); + data.add(new Pair<>("PngSuite/" + file, shouldPass)); + } + } + return data; + } + + private final String filename; + private final boolean shouldPass; + + public PngSuiteAttachmentCreationTaskTest(Pair data) { + filename = data.getFirst(); + shouldPass = data.getSecond(); + } + + @Test + public void testPngSuiteCompress() throws Exception { + if (shouldPass) { + testCompress(filename, "image/png"); + } else { + try { + testCompress(filename, "image/png"); + fail(); + } catch (IOException expected) { + // Expected + } + } + } +}