Bug 992357 - e. Add tests for NativeJSObject array support; r=blassey

This commit is contained in:
Jim Chen 2014-05-16 18:25:29 -04:00
Родитель fab1a1c57d
Коммит b01bf0ce76
2 изменённых файлов: 137 добавлений и 9 удалений

Просмотреть файл

@ -13,6 +13,7 @@ import org.mozilla.gecko.util.ThreadUtils;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -108,6 +109,17 @@ public class testEventDispatcher extends UITest
fAssertSame("optObject returns fallback value if nonexistent",
null, message.optObject("nonexistent_object", null));
final NativeJSObject[] objectArray = message.getObjectArray("objectArray");
fAssertNotNull("Native object array should exist", objectArray);
fAssertEquals("Native object array has correct length", 2, objectArray.length);
fAssertSame("Native object array index 0 has correct value", null, objectArray[0]);
fAssertNotSame("Native object array index 1 has correct value", null, objectArray[1]);
checkNativeJSObject(objectArray[1]);
fAssertNotSame("optObjectArray returns existent value",
null, message.optObjectArray("objectArray", null));
fAssertSame("optObjectArray returns fallback value if nonexistent",
null, message.optObjectArray("nonexistent_objectArray", null));
final Bundle bundle = message.toBundle();
checkBundle(bundle);
checkBundle(bundle.getBundle("object"));
@ -116,6 +128,17 @@ public class testEventDispatcher extends UITest
fAssertSame("optBundle returns fallback value if property does not exist",
null, message.optBundle("nonexistent_object", null));
final Bundle[] bundleArray = message.getBundleArray("objectArray");
fAssertNotNull("Native bundle array should exist", bundleArray);
fAssertEquals("Native bundle array has correct length", 2, bundleArray.length);
fAssertSame("Native bundle array index 0 has correct value", null, bundleArray[0]);
fAssertNotSame("Native bundle array index 1 has correct value", null, bundleArray[1]);
checkBundle(bundleArray[1]);
fAssertNotSame("optBundleArray returns existent value",
null, message.optBundleArray("objectArray", null));
fAssertSame("optBundleArray returns fallback value if nonexistent",
null, message.optBundleArray("nonexistent_objectArray", null));
} else if (NATIVE_RESPONSE_EVENT.equals(event)) {
final String response = message.getString("response");
if ("success".equals(response)) {
@ -138,6 +161,30 @@ public class testEventDispatcher extends UITest
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"));
final boolean[] booleanArray = bundle.getBooleanArray("booleanArray");
fAssertNotNull("Bundle boolean array should exist", booleanArray);
fAssertEquals("Bundle boolean array has correct length", 2, booleanArray.length);
fAssertEquals("Bundle boolean array index 0 has correct value", false, booleanArray[0]);
fAssertEquals("Bundle boolean array index 1 has correct value", true, booleanArray[1]);
final int[] intArray = bundle.getIntArray("intArray");
fAssertNotNull("Bundle int array should exist", intArray);
fAssertEquals("Bundle int array has correct length", 2, intArray.length);
fAssertEquals("Bundle int array index 0 has correct value", 2, intArray[0]);
fAssertEquals("Bundle int array index 1 has correct value", 3, intArray[1]);
final double[] doubleArray = bundle.getDoubleArray("doubleArray");
fAssertNotNull("Bundle double array should exist", doubleArray);
fAssertEquals("Bundle double array has correct length", 2, doubleArray.length);
fAssertEquals("Bundle double array index 0 has correct value", 1.5, doubleArray[0]);
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]);
}
private void checkJSONObject(final JSONObject object) throws JSONException {
@ -145,6 +192,38 @@ public class testEventDispatcher extends UITest
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"));
final JSONArray booleanArray = object.getJSONArray("booleanArray");
fAssertNotNull("JSON boolean array should exist", booleanArray);
fAssertEquals("JSON boolean array has correct length", 2, booleanArray.length());
fAssertEquals("JSON boolean array index 0 has correct value",
false, booleanArray.getBoolean(0));
fAssertEquals("JSON boolean array index 1 has correct value",
true, booleanArray.getBoolean(1));
final JSONArray intArray = object.getJSONArray("intArray");
fAssertNotNull("JSON int array should exist", intArray);
fAssertEquals("JSON int array has correct length", 2, intArray.length());
fAssertEquals("JSON int array index 0 has correct value",
2, intArray.getInt(0));
fAssertEquals("JSON int array index 1 has correct value",
3, intArray.getInt(1));
final JSONArray doubleArray = object.getJSONArray("doubleArray");
fAssertNotNull("JSON double array should exist", doubleArray);
fAssertEquals("JSON double array has correct length", 2, doubleArray.length());
fAssertEquals("JSON double array index 0 has correct value",
1.5, doubleArray.getDouble(0));
fAssertEquals("JSON double array index 1 has correct value",
2.5, doubleArray.getDouble(1));
final JSONArray stringArray = object.getJSONArray("stringArray");
fAssertNotNull("JSON string array should exist", stringArray);
fAssertEquals("JSON string array has correct length", 2, stringArray.length());
fAssertEquals("JSON string array index 0 has correct value",
"bar", stringArray.getString(0));
fAssertEquals("JSON string array index 1 has correct value",
"baz", stringArray.getString(1));
}
private void checkNativeJSObject(final NativeJSObject object) {
@ -154,23 +233,66 @@ public class testEventDispatcher extends UITest
true, object.optBoolean("boolean", false));
fAssertEquals("optBoolean returns fallback value if nonexistent",
false, object.optBoolean("nonexistent_boolean", false));
fAssertEquals("Native int has correct value",
1, object.getInt("int"));
fAssertEquals("optInt returns existent value",
1, object.optInt("int", 0));
fAssertEquals("optInt returns fallback value if nonexistent",
0, object.optInt("nonexistent_int", 0));
fAssertEquals("Native double has correct value",
0.5, object.getDouble("double"));
fAssertEquals("optDouble returns existent value",
0.5, object.optDouble("double", -0.5));
fAssertEquals("optDouble returns fallback value if nonexistent",
-0.5, object.optDouble("nonexistent_double", -0.5));
fAssertEquals("Native string has correct value",
"foo", object.getString("string"));
fAssertEquals("optDouble returns existent value",
"foo", object.optString("string", "bar"));
fAssertEquals("optDouble returns fallback value if nonexistent",
"bar", object.optString("nonexistent_string", "bar"));
final boolean[] booleanArray = object.getBooleanArray("booleanArray");
fAssertNotNull("Native boolean array should exist", booleanArray);
fAssertEquals("Native boolean array has correct length", 2, booleanArray.length);
fAssertEquals("Native boolean array index 0 has correct value", false, booleanArray[0]);
fAssertEquals("Native boolean array index 1 has correct value", true, booleanArray[1]);
fAssertNotSame("optBooleanArray returns existent value",
null, object.optBooleanArray("booleanArray", null));
fAssertSame("optBooleanArray returns fallback value if nonexistent",
null, object.optBooleanArray("nonexistent_booleanArray", null));
final int[] intArray = object.getIntArray("intArray");
fAssertNotNull("Native int array should exist", intArray);
fAssertEquals("Native int array has correct length", 2, intArray.length);
fAssertEquals("Native int array index 0 has correct value", 2, intArray[0]);
fAssertEquals("Native int array index 1 has correct value", 3, intArray[1]);
fAssertNotSame("optIntArray returns existent value",
null, object.optIntArray("intArray", null));
fAssertSame("optIntArray returns fallback value if nonexistent",
null, object.optIntArray("nonexistent_intArray", null));
final double[] doubleArray = object.getDoubleArray("doubleArray");
fAssertNotNull("Native double array should exist", doubleArray);
fAssertEquals("Native double array has correct length", 2, doubleArray.length);
fAssertEquals("Native double array index 0 has correct value", 1.5, doubleArray[0]);
fAssertEquals("Native double array index 1 has correct value", 2.5, doubleArray[1]);
fAssertNotSame("optDoubleArray returns existent value",
null, object.optDoubleArray("doubleArray", null));
fAssertSame("optDoubleArray returns fallback value if nonexistent",
null, object.optDoubleArray("nonexistent_doubleArray", null));
final String[] stringArray = object.getStringArray("stringArray");
fAssertNotNull("Native string array should exist", stringArray);
fAssertEquals("Native string array has correct length", 2, stringArray.length);
fAssertEquals("Native string array index 0 has correct value", "bar", stringArray[0]);
fAssertEquals("Native string array index 1 has correct value", "baz", stringArray[1]);
fAssertNotSame("optStringArray returns existent value",
null, object.optStringArray("stringArray", null));
fAssertSame("optStringArray returns fallback value if nonexistent",
null, object.optStringArray("nonexistent_stringArray", null));
}
}

Просмотреть файл

@ -8,19 +8,25 @@ do_register_cleanup(() => {
do_test_pending();
function send_test_message(type) {
sendMessageToJava({
type: type,
let innerObject = {
boolean: true,
booleanArray: [false, true],
int: 1,
intArray: [2, 3],
double: 0.5,
doubleArray: [1.5, 2.5],
string: "foo",
object: {
boolean: true,
int: 1,
double: 0.5,
string: "foo",
},
});
stringArray: ["bar", "baz"],
}
// Make a copy
let outerObject = JSON.parse(JSON.stringify(innerObject));
outerObject.type = type;
outerObject.object = innerObject;
outerObject.objectArray = [null, innerObject];
sendMessageToJava(outerObject);
}
function send_message_for_response(type, response) {