From 7156b0e7d4e74c4b75b47d2d629a1cd21bce6d66 Mon Sep 17 00:00:00 2001 From: Wes Johnston Date: Thu, 22 Mar 2012 11:12:08 -0700 Subject: [PATCH] Bug 709250 - Tets for jar reader. r=lucasr --- mobile/android/base/tests/robocop.ini | 1 + .../android/base/tests/testJarReader.java.in | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 mobile/android/base/tests/testJarReader.java.in diff --git a/mobile/android/base/tests/robocop.ini b/mobile/android/base/tests/robocop.ini index 0c20f87030a9..7b3e38338fb0 100644 --- a/mobile/android/base/tests/robocop.ini +++ b/mobile/android/base/tests/robocop.ini @@ -14,6 +14,7 @@ [testPasswordEncrypt] [testFormHistory] [testBrowserProvider] +[testJarReader] # Used for Talos, please don't use in mochitest #[testPan] diff --git a/mobile/android/base/tests/testJarReader.java.in b/mobile/android/base/tests/testJarReader.java.in new file mode 100644 index 000000000000..346b625f6bc7 --- /dev/null +++ b/mobile/android/base/tests/testJarReader.java.in @@ -0,0 +1,71 @@ +#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(); + } +}