Simplify code.

This commit is contained in:
akwizgran
2019-12-12 10:33:33 +00:00
parent 6d742c554f
commit 44411ab224
2 changed files with 12 additions and 30 deletions

View File

@@ -28,19 +28,11 @@ abstract class AbstractAttachmentCreationTaskTest {
void testCompress(String filename, String contentType) void testCompress(String filename, String contentType)
throws IOException { throws IOException {
InputStream is = getAssetInputStream(filename); InputStream is = getAssetManager().open(filename);
task.compressImage(is, contentType); task.compressImage(is, contentType);
} }
private InputStream getAssetInputStream(String name) throws IOException { static AssetManager getAssetManager() {
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 // pm.getResourcesForApplication(packageName).getAssets() did not work
//noinspection deprecation //noinspection deprecation
return getContext().getAssets(); return getContext().getAssets();

View File

@@ -1,15 +1,14 @@
package org.briarproject.briar.android.attachment; package org.briarproject.briar.android.attachment;
import org.briarproject.bramble.api.Pair;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; import org.junit.runners.Parameterized.Parameters;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
@@ -17,36 +16,27 @@ public class PngSuiteAttachmentCreationTaskTest
extends AbstractAttachmentCreationTaskTest { extends AbstractAttachmentCreationTaskTest {
@Parameters @Parameters
public static Iterable<Pair<String, Boolean>> data() throws IOException { public static Iterable<String> data() throws IOException {
List<Pair<String, Boolean>> data = new ArrayList<>(); return asList(requireNonNull(getAssetManager().list("PngSuite")));
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 String filename;
private final boolean shouldPass;
public PngSuiteAttachmentCreationTaskTest(Pair<String, Boolean> data) { public PngSuiteAttachmentCreationTaskTest(String filename) {
filename = data.getFirst(); this.filename = filename;
shouldPass = data.getSecond();
} }
@Test @Test
public void testPngSuiteCompress() throws Exception { public void testPngSuiteCompress() throws Exception {
if (shouldPass) { if (filename.startsWith("x")) {
testCompress(filename, "image/png");
} else {
try { try {
testCompress(filename, "image/png"); testCompress("PngSuite/" + filename, "image/png");
fail(); fail();
} catch (IOException expected) { } catch (IOException expected) {
// Expected // Expected
} }
} else {
testCompress("PngSuite/" + filename, "image/png");
} }
} }
} }