Test that PngSuite corrupt test images fail.

This commit is contained in:
akwizgran
2019-12-12 10:27:57 +00:00
parent 1e2ccd96a7
commit 6d742c554f
3 changed files with 103 additions and 52 deletions

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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<Pair<String, Boolean>> data() throws IOException {
List<Pair<String, Boolean>> 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<String, Boolean> 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
}
}
}
}