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

79 lines
4.0 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import java.lang.ClassLoader;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 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 {
@Override
protected int getTestType() {
return TEST_MOCHITEST;
}
public void testJarReader() {
try {
ClassLoader classLoader = getActivity().getClassLoader();
Class appConstantsClass = classLoader.loadClass("org.mozilla.gecko.AppConstants");
String omniJarName = (String) appConstantsClass.getField("OMNIJAR_NAME").get(null);
Class gjrClass = classLoader.loadClass("org.mozilla.gecko.util.GeckoJarReader");
Method getStreamMethod = gjrClass.getMethod("getStream", String.class);
String appPath = getActivity().getApplication().getPackageResourcePath();
mAsserter.isnot(appPath, null, "getPackageResourcePath is non-null");
// Test reading a file from a jar url that looks correct.
String url = "jar:file://" + appPath + "!/" + omniJarName;
InputStream stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.isnot(stream, null, "JarReader returned non-null for valid file in valid jar");
// Test looking for an non-existent file in a jar.
url = "jar:file://" + appPath + "!/" + omniJarName;
stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
mAsserter.is(stream, null, "JarReader returned null for non-existent file in valid jar");
// Test looking for a file that doesn't exist in the APK.
url = "jar:file://" + appPath + "!/" + "BAD" + omniJarName;
stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.is(stream, null, "JarReader returned null for valid file in invalid jar file");
// Test looking for an jar with an invalid url.
url = "jar:file://" + appPath + "!" + "!/" + omniJarName;
stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
mAsserter.is(stream, null, "JarReader returned null for bad jar url");
// Test looking for a file that doesn't exist on disk.
url = "jar:file://" + appPath + "BAD" + "!/" + omniJarName;
stream = (InputStream) getStreamMethod.invoke(null, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.is(stream, null, "JarReader returned null for a non-existent APK");
} catch (java.lang.ClassCastException ex) {
mAsserter.is(false, true, "Error getting OMNIJAR_NAME");
} catch (java.lang.NoSuchFieldException ex) {
mAsserter.is(false, true, "Error getting field");
} catch (java.lang.ClassNotFoundException ex) {
mAsserter.is(false, true, "Error getting class");
} catch (java.lang.NoSuchMethodException ex) {
mAsserter.is(false, true, "Error getting method");
} catch (java.lang.IllegalAccessException ex) {
mAsserter.is(false, true, "Error calling method");
} catch (java.lang.reflect.InvocationTargetException ex) {
mAsserter.is(false, true, "Invocation target exception " + ex.getTargetException());
}
// This test completes very quickly. If it completes too soon, the
// minidumps directory may not be created before the process is
// taken down, causing bug 722166.
blockForGeckoReady();
}
private String getData(InputStream stream) {
return new java.util.Scanner(stream).useDelimiter("\\A").next();
}
}