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)
throws IOException {
InputStream is = getAssetInputStream(filename);
InputStream is = getAssetManager().open(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() {
static AssetManager getAssetManager() {
// pm.getResourcesForApplication(packageName).getAssets() did not work
//noinspection deprecation
return getContext().getAssets();

View File

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