gecko-dev/mobile/android/base/tests/testJarReader.java.in
Brian Nicholson fe44f36220 Bug 850487 - More code cleanup (@Overrides and unused imports). r=kats
--HG--
extra : rebase_source : 376574e0c41b91c16a6be335584a4a61768bb4a9
2013-03-13 13:20:57 -07:00

64 lines
3.0 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import java.lang.ClassLoader;
import java.io.InputStream;
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 gjrClass = classLoader.loadClass("org.mozilla.gecko.util.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();
}
}