Bug 709250 - Tets for jar reader. r=lucasr
authorWes Johnston <wjohnston@mozilla.com>
Thu, 22 Mar 2012 11:12:08 -0700
changeset 93370 7b54766189f3bf6ad26b62742b030a4e10a46dc3
parent 93369 6bf86d4a94ea018218a3fbe5724bc2a067a66ea4
child 93371 7e6750f282cc12fadc7c1235bfc673f65d9d88ff
push idunknown
push userunknown
push dateunknown
reviewerslucasr
bugs709250
milestone14.0a1
Bug 709250 - Tets for jar reader. r=lucasr
mobile/android/base/tests/robocop.ini
mobile/android/base/tests/testJarReader.java.in
--- a/mobile/android/base/tests/robocop.ini
+++ b/mobile/android/base/tests/robocop.ini
@@ -9,13 +9,14 @@
 [testOverscroll]
 [testAxisLocking]
 [testAboutPage]
 [testWebContentContextMenu]
 [testPasswordProvider]
 [testPasswordEncrypt]
 [testFormHistory]
 [testBrowserProvider]
+[testJarReader]
 
 # Used for Talos, please don't use in mochitest
 #[testPan]
 #[testCheck]
 #[testBrowserProviderPerf]
new file mode 100644
--- /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();
+    }
+}