Bug 1319496 - 2. Add more GeckoBundle tests in testEventDispatcher; r=snorp
authorJim Chen <nchen@mozilla.com>
Tue, 29 Nov 2016 12:25:53 -0500
changeset 324718 15407fdf9c663cfd195b28920da5a2d9084eba49
parent 324717 7cf56f4e7c0de931e35f56d7220e4b99d0dd3fdf
child 324719 10a6565daede03fcb1b5d4c7bfbfa499dfec404c
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerssnorp
bugs1319496
milestone53.0a1
Bug 1319496 - 2. Add more GeckoBundle tests in testEventDispatcher; r=snorp Add more tests for edge cases in testEventDispatcher, such as null arrays, nonexistent values, and object arrays containing only nulls.
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testEventDispatcher.java
mobile/android/tests/browser/robocop/testEventDispatcher.js
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testEventDispatcher.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testEventDispatcher.java
@@ -206,18 +206,42 @@ public class testEventDispatcher extends
         } else {
             fFail("Event type should be valid: " + event);
         }
 
         if (UI_EVENT.equals(event) || BACKGROUND_EVENT.equals(event)) {
             checkBundle(message);
             checkBundle(message.getBundle("object"));
             fAssertSame("Bundle null object has correct value", null, message.getBundle("nullObject"));
+            fAssertSame("Bundle nonexistent object returns null",
+                    null, message.getBundle("nonexistentObject"));
 
-            // XXX add objectArray check when we remove NativeJSObject
+            final GeckoBundle[] objectArray = message.getBundleArray("objectArray");
+            fAssertNotNull("Bundle object array should exist", objectArray);
+            fAssertEquals("Bundle object array has correct length", 2, objectArray.length);
+            fAssertSame("Bundle object array index 0 has correct value", null, objectArray[0]);
+            checkBundle(objectArray[1]);
+
+            final GeckoBundle[] objectArrayOfNull = message.getBundleArray("objectArrayOfNull");
+            fAssertNotNull("Bundle object array of null should exist", objectArrayOfNull);
+            fAssertEquals("Bundle object array of null has correct length", 2, objectArrayOfNull.length);
+            fAssertSame("Bundle object array of null index 0 has correct value",
+                    null, objectArrayOfNull[0]);
+            fAssertSame("Bundle object array of null index 1 has correct value",
+                    null, objectArrayOfNull[1]);
+
+            final GeckoBundle[] emptyObjectArray = message.getBundleArray("emptyObjectArray");
+            fAssertNotNull("Bundle empty object array should exist", emptyObjectArray);
+            fAssertEquals("Bundle empty object array has correct length", 0, emptyObjectArray.length);
+
+            fAssertSame("Bundle null object array exists",
+                    null, message.getBundleArray("nullObjectArray"));
+
+            fAssertSame("Bundle nonexistent object array returns null",
+                    null, message.getBundleArray("nonexistentObjectArray"));
 
         } else if (UI_RESPONSE_EVENT.equals(event) || BACKGROUND_RESPONSE_EVENT.equals(event)) {
             final String response = message.getString("response");
             if ("success".equals(response)) {
                 callback.sendSuccess(response);
             } else if ("error".equals(response)) {
                 callback.sendError(response);
             } else {
@@ -385,16 +409,34 @@ public class testEventDispatcher extends
     }
 
     private void checkBundle(final GeckoBundle bundle) {
         fAssertEquals("Bundle boolean has correct value", true, bundle.getBoolean("boolean"));
         fAssertEquals("Bundle int has correct value", 1, bundle.getInt("int"));
         fAssertEquals("Bundle double has correct value", 0.5, bundle.getDouble("double"));
         fAssertEquals("Bundle string has correct value", "foo", bundle.getString("string"));
 
+        fAssertEquals("Bundle default boolean has correct value",
+                true, bundle.getBoolean("nonexistentBoolean", true));
+        fAssertEquals("Bundle default int has correct value",
+                1, bundle.getInt("nonexistentInt", 1));
+        fAssertEquals("Bundle default double has correct value",
+                0.5, bundle.getDouble("nonexistentDouble", 0.5));
+        fAssertEquals("Bundle default string has correct value",
+                "foo", bundle.getString("nonexistentString", "foo"));
+
+        fAssertEquals("Bundle nonexistent boolean returns false",
+                false, bundle.getBoolean("nonexistentBoolean"));
+        fAssertEquals("Bundle nonexistent int returns 0",
+                0, bundle.getInt("nonexistentInt"));
+        fAssertEquals("Bundle nonexistent double returns 0.0",
+                0.0, bundle.getDouble("nonexistentDouble"));
+        fAssertSame("Bundle nonexistent string returns null",
+                null, bundle.getString("nonexistentString"));
+
         fAssertSame("Bundle null string has correct value", null, bundle.getString("nullString"));
         fAssertEquals("Bundle empty string has correct value", "", bundle.getString("emptyString"));
         fAssertEquals("Bundle default null string is correct", "foo",
                       bundle.getString("nullString", "foo"));
         fAssertEquals("Bundle default empty string is correct", "",
                       bundle.getString("emptyString", "foo"));
 
         final boolean[] booleanArray = bundle.getBooleanArray("booleanArray");
@@ -416,32 +458,58 @@ public class testEventDispatcher extends
         fAssertEquals("Bundle double array index 1 has correct value", 2.5, doubleArray[1]);
 
         final String[] stringArray = bundle.getStringArray("stringArray");
         fAssertNotNull("Bundle string array should exist", stringArray);
         fAssertEquals("Bundle string array has correct length", 2, stringArray.length);
         fAssertEquals("Bundle string array index 0 has correct value", "bar", stringArray[0]);
         fAssertEquals("Bundle string array index 1 has correct value", "baz", stringArray[1]);
 
+        final String[] stringArrayOfNull = bundle.getStringArray("stringArrayOfNull");
+        fAssertNotNull("Bundle string array of null should exist", stringArrayOfNull);
+        fAssertEquals("Bundle string array of null has correct length", 2, stringArrayOfNull.length);
+        fAssertSame("Bundle string array of null index 0 has correct value",
+                null, stringArrayOfNull[0]);
+        fAssertSame("Bundle string array of null index 1 has correct value",
+                null, stringArrayOfNull[1]);
+
         final boolean[] emptyBooleanArray = bundle.getBooleanArray("emptyBooleanArray");
         fAssertNotNull("Bundle empty boolean array should exist", emptyBooleanArray);
         fAssertEquals("Bundle empty boolean array has correct length", 0, emptyBooleanArray.length);
 
         final int[] emptyIntArray = bundle.getIntArray("emptyIntArray");
         fAssertNotNull("Bundle empty int array should exist", emptyIntArray);
         fAssertEquals("Bundle empty int array has correct length", 0, emptyIntArray.length);
 
         final double[] emptyDoubleArray = bundle.getDoubleArray("emptyDoubleArray");
         fAssertNotNull("Bundle empty double array should exist", emptyDoubleArray);
         fAssertEquals("Bundle empty double array has correct length", 0, emptyDoubleArray.length);
 
         final String[] emptyStringArray = bundle.getStringArray("emptyStringArray");
         fAssertNotNull("Bundle empty String array should exist", emptyStringArray);
         fAssertEquals("Bundle empty String array has correct length", 0, emptyStringArray.length);
 
+        fAssertSame("Bundle null boolean array exists",
+                null, bundle.getBooleanArray("nullBooleanArray"));
+        fAssertSame("Bundle null int array exists",
+                null, bundle.getIntArray("nullIntArray"));
+        fAssertSame("Bundle null double array exists",
+                null, bundle.getDoubleArray("nullDoubleArray"));
+        fAssertSame("Bundle null string array exists",
+                null, bundle.getStringArray("nullStringArray"));
+
+        fAssertSame("Bundle nonexistent boolean array returns null",
+                null, bundle.getBooleanArray("nonexistentBooleanArray"));
+        fAssertSame("Bundle nonexistent int array returns null",
+                null, bundle.getIntArray("nonexistentIntArray"));
+        fAssertSame("Bundle nonexistent double array returns null",
+                null, bundle.getDoubleArray("nonexistentDoubleArray"));
+        fAssertSame("Bundle nonexistent string array returns null",
+                null, bundle.getStringArray("nonexistentStringArray"));
+
         // XXX add mixedArray check when we remove NativeJSObject
     }
 
     private void checkJSONObject(final JSONObject object) throws JSONException {
         fAssertEquals("JSON boolean has correct value", true, object.getBoolean("boolean"));
         fAssertEquals("JSON int has correct value", 1, object.getInt("int"));
         fAssertEquals("JSON double has correct value", 0.5, object.getDouble("double"));
         fAssertEquals("JSON string has correct value", "foo", object.getString("string"));
@@ -575,33 +643,41 @@ public class testEventDispatcher extends
 
         bundle.putDouble("double", 0.5);
         bundle.putDoubleArray("doubleArray", new double[] {1.5, 2.5});
 
         bundle.putString("string", "foo");
         bundle.putString("nullString", null);
         bundle.putString("emptyString", "");
         bundle.putStringArray("stringArray", new String[] {"bar", "baz"});
+        bundle.putStringArray("stringArrayOfNull", new String[2]);
 
         bundle.putBooleanArray("emptyBooleanArray", new boolean[0]);
         bundle.putIntArray("emptyIntArray", new int[0]);
         bundle.putDoubleArray("emptyDoubleArray", new double[0]);
         bundle.putStringArray("emptyStringArray", new String[0]);
 
+        bundle.putBooleanArray("nullBooleanArray", (boolean[]) null);
+        bundle.putIntArray("nullIntArray", (int[]) null);
+        bundle.putDoubleArray("nullDoubleArray", (double[]) null);
+        bundle.putStringArray("nullStringArray", (String[]) null);
+
         return bundle;
     }
 
     private static GeckoBundle createBundle() {
         final GeckoBundle outer = createInnerBundle();
         final GeckoBundle inner = createInnerBundle();
 
         outer.putBundle("object", inner);
         outer.putBundle("nullObject", null);
         outer.putBundleArray("objectArray", new GeckoBundle[] {null, inner});
+        outer.putBundleArray("objectArrayOfNull", new GeckoBundle[2]);
         outer.putBundleArray("emptyObjectArray", new GeckoBundle[0]);
+        outer.putBundleArray("nullObjectArray", (GeckoBundle[]) null);
 
         return outer;
     }
 
     public void dispatchMessage(final String scope, final String type) {
         getDispatcher(scope).dispatch(type, createBundle());
     }
 
--- a/mobile/android/tests/browser/robocop/testEventDispatcher.js
+++ b/mobile/android/tests/browser/robocop/testEventDispatcher.js
@@ -16,31 +16,38 @@ function get_test_message() {
     intArray: [2, 3],
     double: 0.5,
     doubleArray: [1.5, 2.5],
     null: null,
     string: "foo",
     nullString: null,
     emptyString: "",
     stringArray: ["bar", "baz"],
+    stringArrayOfNull: [null, null],
     emptyBooleanArray: [],
     emptyIntArray: [],
     emptyDoubleArray: [],
     emptyStringArray: [],
+    nullBooleanArray: null,
+    nullIntArray: null,
+    nullDoubleArray: null,
+    nullStringArray: null,
     // XXX enable when we remove NativeJSObject
     // mixedArray: [1, 1.5],
   };
 
   // Make a copy
   let outerObject = JSON.parse(JSON.stringify(innerObject));
 
   outerObject.object = innerObject;
   outerObject.nullObject = null;
   outerObject.objectArray = [null, innerObject];
+  outerObject.objectArrayOfNull = [null, null];
   outerObject.emptyObjectArray = [];
+  outerObject.nullObjectArray = null;
   return outerObject;
 }
 
 function send_test_message(type) {
   let outerObject = get_test_message();
   outerObject.type = type;
 
   Messaging.sendRequest(outerObject);
@@ -99,35 +106,50 @@ let listener = {
     do_check_eq(obj.string, "foo");
     do_check_eq(obj.nullString, null);
     do_check_eq(obj.emptyString, "");
 
     do_check_eq(obj.stringArray.length, 2);
     do_check_eq(obj.stringArray[0], "bar");
     do_check_eq(obj.stringArray[1], "baz");
 
+    do_check_eq(obj.stringArrayOfNull.length, 2);
+    do_check_eq(obj.stringArrayOfNull[0], null);
+    do_check_eq(obj.stringArrayOfNull[1], null);
+
     do_check_eq(obj.emptyBooleanArray.length, 0);
     do_check_eq(obj.emptyIntArray.length, 0);
     do_check_eq(obj.emptyDoubleArray.length, 0);
     do_check_eq(obj.emptyStringArray.length, 0);
+
+    do_check_eq(obj.nullBooleanArray, null);
+    do_check_eq(obj.nullIntArray, null);
+    do_check_eq(obj.nullDoubleArray, null);
+    do_check_eq(obj.nullStringArray, null);
   },
 
   onEvent: function (event, data, callback) {
     do_check_eq(event, this._type);
     this._callCount++;
 
     this._checkObject(data);
 
     this._checkObject(data.object);
     do_check_eq(data.nullObject, null);
 
     do_check_eq(data.objectArray.length, 2);
     do_check_eq(data.objectArray[0], null);
     this._checkObject(data.objectArray[1]);
+
+    do_check_eq(data.objectArrayOfNull.length, 2);
+    do_check_eq(data.objectArrayOfNull[0], null);
+    do_check_eq(data.objectArrayOfNull[1], null);
+
     do_check_eq(data.emptyObjectArray.length, 0);
+    do_check_eq(data.nullObjectArray, null);
   }
 };
 
 let callbackListener = {
   onEvent: function (event, data, callback) {
     do_check_eq(event, this._type);
     this._callCount++;