Bug 994195 - Add tests for RawResource (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 22 Apr 2014 14:53:59 +0100
changeset 180032 d95dd0d913f181ce84520645dbeca589cd2cb087
parent 180031 290e0e14312ffe31ef215fafe77a0e7f084b2e31
child 180033 24fc89305763a65ddaa102ef44861021ca1cbbb5
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersmargaret
bugs994195
milestone31.0a1
Bug 994195 - Add tests for RawResource (r=margaret)
mobile/android/tests/browser/junit3/moz.build
mobile/android/tests/browser/junit3/src/tests/TestRawResource.java
--- a/mobile/android/tests/browser/junit3/moz.build
+++ b/mobile/android/tests/browser/junit3/moz.build
@@ -8,16 +8,17 @@ DEFINES['ANDROID_PACKAGE_NAME'] = CONFIG
 
 jar = add_java_jar('browser-junit3')
 jar.sources += [
     'src/harness/BrowserInstrumentationTestRunner.java',
     'src/harness/BrowserTestListener.java',
     'src/tests/BrowserTestCase.java',
     'src/tests/TestGeckoSharedPrefs.java',
     'src/tests/TestJarReader.java',
+    'src/tests/TestRawResource.java',
     'src/tests/TestTopSitesCursorWrapper.java',
 ]
 jar.generated_sources = [] # None yet -- try to keep it this way.
 jar.javac_flags += ['-Xlint:all,-unchecked']
 
 # Android Eclipse project.
 main = add_android_eclipse_project('BrowserInstrumentationTests', OBJDIR + '/AndroidManifest.xml')
 # The package name doesn't really matter, but it looks nicest if the
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/junit3/src/tests/TestRawResource.java
@@ -0,0 +1,67 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.gecko.browser.tests;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.test.mock.MockContext;
+import android.test.mock.MockResources;
+import android.util.TypedValue;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+
+import org.mozilla.gecko.util.RawResource;
+
+/**
+ * Tests whether RawResource.get() produces the right String
+ * result after reading the returned raw resource's InputStream.
+ */
+public class TestRawResource extends BrowserTestCase {
+    private static final int RAW_RESOURCE_ID = 1;
+    private static final String RAW_CONTENTS = "RAW";
+
+    private static class TestContext extends MockContext {
+        private final Resources resources;
+
+        public TestContext() {
+            resources = new TestResources();
+        }
+
+        @Override
+        public Resources getResources() {
+            return resources;
+        }
+    }
+
+    /**
+     * Browser instrumentation tests can't have access to test-only
+     * resources (bug 994135) yet so we mock the access to resources
+     * for now.
+     */
+    private static class TestResources extends MockResources {
+        @Override
+        public InputStream openRawResource(int id) {
+            if (id == RAW_RESOURCE_ID) {
+                return new ByteArrayInputStream(RAW_CONTENTS.getBytes());
+            }
+
+            return null;
+        }
+    }
+
+    public void testGet() {
+        Context context = new TestContext();
+        String result;
+
+        try {
+            result = RawResource.get(context, RAW_RESOURCE_ID);
+        } catch (IOException e) {
+            result = null;
+        }
+
+        assertEquals(RAW_CONTENTS, result);
+    }
+}
\ No newline at end of file