gecko/mobile/android/base/tests/testJarReader.java.in

72 lines
3.3 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import @ANDROID_PACKAGE_NAME@.*;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.Bitmap;
import java.lang.ClassLoader;
import java.util.ArrayList;
import java.io.Writer;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Method;
import java.io.StringWriter;
/**
* A basic jar reader test. Tests reading a png from fennec's apk, as well
* as loading some invalid jar urls.
*/
public class testJarReader extends BaseTest {
public void testJarReader() {
setTestType("mochitest");
try {
ClassLoader classLoader = getActivity().getClassLoader();
Class gjrClass = classLoader.loadClass("org.mozilla.gecko.GeckoJarReader");
Method getStreamMethod = gjrClass.getMethod("getStream", String.class);
String appPath = getActivity().getApplication().getPackageResourcePath();
// Test reading a file from a jar url
String url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "!/omni.ja";
InputStream stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.isnot(stream, null, "JarReader returned null for invalid jar file");
// Test looking for a jar that doesn't exist
url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "2!/omni.ja";
stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.is(stream, null, "JarReader returned null for invalid jar file");
// Test looking for an non-existant file in a jar
url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "2!/omni.ja";
stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistant_file.png");
mAsserter.is(stream, null, "JarReader returned null for non-existant file in jar");
// Test looking for an jar with an invalid url
url = "jar:file://" + getActivity().getApplication().getPackageResourcePath() + "2!!/omni.ja";
stream = (InputStream)getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistant_file.png");
mAsserter.is(stream, null, "JarReader returned null for bar jar url");
} catch (java.lang.ClassNotFoundException ex) {
mAsserter.is(false, true, "Error getting class");
return;
} catch (java.lang.NoSuchMethodException ex) {
mAsserter.is(false, true, "Error getting method");
return;
} catch (java.lang.IllegalAccessException ex) {
mAsserter.is(false, true, "Error calling method");
return;
} catch (java.lang.reflect.InvocationTargetException ex) {
mAsserter.is(false, true, "Invocation target exception " + ex.getTargetException());
return;
}
}
private String getData(InputStream stream) {
return new java.util.Scanner(stream).useDelimiter("\\A").next();
}
public void tearDown() throws Exception {
super.tearDown();
}
}