#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"); return; } catch (java.lang.NoSuchFieldException ex) { mAsserter.is(false, true, "Error getting field"); return; } 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(); } }