Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a null pointer while converting bundle array to list object. Add … #54

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.walmartlabs.electrode.reactnative.bridge.util;

import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.walmartlabs.electrode.reactnative.bridge.BaseBridgeTestCase;
import com.walmartlabs.electrode.reactnative.bridge.helpers.ArgumentsEx;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class ArgumentsExTests extends BaseBridgeTestCase {

@Test
public void toBundleNull() {
assertEquals(0, ArgumentsEx.toBundle(null).size());
}

@Test
public void toBundleEmpty() {
WritableMap readableMap = Arguments.createMap();
assertEquals(0, ArgumentsEx.toBundle(readableMap).size());
}

@Test
public void toBundleEmptyArray() {
WritableMap readableMap = Arguments.createMap();
readableMap.putArray("test", Arguments.createArray());
assertEquals(0, ArgumentsEx.toBundle(readableMap).size());
}

@Test
public void toBundleNullKeyValue() {
WritableMap readableMap = Arguments.createMap();
readableMap.putArray("test", null);
Bundle outBundle = ArgumentsEx.toBundle(readableMap);
assertEquals(1, outBundle.size());
}

@Test
public void toBundleStringArray() {
final String STRING_ARRAY = "stringArray";
WritableMap readableMap = Arguments.createMap();
WritableArray stringArray = Arguments.createArray();
stringArray.pushString("1");
stringArray.pushString("2");
stringArray.pushString("3");
stringArray.pushString("4");

readableMap.putArray(STRING_ARRAY, stringArray);
Bundle outBundle = ArgumentsEx.toBundle(readableMap);
assertEquals(1, outBundle.size());
assertNotNull(outBundle.getStringArray(STRING_ARRAY));
String[] outArray = outBundle.getStringArray(STRING_ARRAY);
assertEquals(readableMap.getArray(STRING_ARRAY).size(), outArray.length);
}

@Test
public void toBundleBooleanArray() {
final String BOOLEAN_ARRAY = "booleanArray";
WritableMap readableMap = Arguments.createMap();
WritableArray booleanArray = Arguments.createArray();
booleanArray.pushBoolean(true);
booleanArray.pushBoolean(true);
booleanArray.pushBoolean(true);
booleanArray.pushBoolean(true);

readableMap.putArray(BOOLEAN_ARRAY, booleanArray);
Bundle outBundle = ArgumentsEx.toBundle(readableMap);
assertEquals(1, outBundle.size());
assertNotNull(outBundle.getBooleanArray(BOOLEAN_ARRAY));
boolean[] outArray = outBundle.getBooleanArray(BOOLEAN_ARRAY);
ReadableArray expectedArray = readableMap.getArray(BOOLEAN_ARRAY);
assertEquals(expectedArray.size(), outArray.length);

for (int i = 0; i < expectedArray.size(); i++) {
boolean expected = expectedArray.getBoolean(i);
assertEquals(expected, outArray[i]);
}
}

@Test
public void toBundleNumberArray() {
final String NUMBER_ARRAY = "numberArray";
WritableMap readableMap = Arguments.createMap();
WritableArray numberArray = Arguments.createArray();
numberArray.pushInt(1);
numberArray.pushInt(2);
numberArray.pushInt(3);
numberArray.pushInt(4);

readableMap.putArray(NUMBER_ARRAY, numberArray);
Bundle outBundle = ArgumentsEx.toBundle(readableMap);
assertEquals(1, outBundle.size());
double[] outArray = outBundle.getDoubleArray(NUMBER_ARRAY);
assertNotNull(outArray);
ReadableArray expectedArray = readableMap.getArray(NUMBER_ARRAY);
assertEquals(expectedArray.size(), outArray.length);

for (int i = 0; i < expectedArray.size(); i++) {
int expected = expectedArray.getInt(i);
assertEquals(expected, outArray[i], 0);
}
}

@Test
public void toBundleMap() {
final String MAP = "map";
WritableMap readableMap = Arguments.createMap();
WritableMap readableMapData = Arguments.createMap();
readableMapData.putArray("nullKey", null);
readableMapData.putString("string", "some string");
readableMapData.putInt("int", 1);
readableMapData.putBoolean("bool", true);
readableMap.putMap(MAP, readableMapData);
Bundle outBundle = ArgumentsEx.toBundle(readableMap);
assertEquals(1, outBundle.size());

Bundle innerBundle = outBundle.getBundle(MAP);
assertNotNull(innerBundle);
assertNull(innerBundle.get("nullKey"));
assertEquals("some string", innerBundle.getString("string"));
assertEquals(1, innerBundle.getDouble("int"), 0);
assertTrue(innerBundle.getBoolean("bool"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void testModelParcelable() {
add(40);
}};
final Person inputPerson = new Person.Builder("testName", 10).addresses(addressList).siblingsNames(namesList).siblingsAges(agesList).build();
Parcel personParcel = Parcel.obtain();
Parcel personParcel = Parcel.obtain();
inputPerson.writeToParcel(personParcel, inputPerson.describeContents());
personParcel.setDataPosition(0);

Expand All @@ -339,4 +339,29 @@ public void testModelParcelable() {
assertEquals(inputPerson.getName(), outPerson.getName());
}


public void testNullList() {
final Person inputPerson = new Person.Builder("testName", 10).addresses(null).siblingsNames(null).siblingsAges(null).build();
Parcel personParcel = Parcel.obtain();
inputPerson.writeToParcel(personParcel, inputPerson.describeContents());
personParcel.setDataPosition(0);

Person outPerson = Person.CREATOR.createFromParcel(personParcel);
assertNotNull(outPerson);
assertEquals(0, outPerson.getAddressList().size());
assertEquals(0, outPerson.getSiblingsAges().size());
assertEquals(0, outPerson.getSiblingsNames().size());
assertEquals(inputPerson.getName(), outPerson.getName());
}

public void testNullListItem() {
Bundle bundle = new Bundle();
bundle.putString("name", "John");
bundle.putInt("month", 1);
bundle.putString("addressList", null);

Person person = new Person(bundle);
assertNotNull(person);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,24 @@ public static Bundle toBundle(@Nullable ReadableMap readableMap) {
break;
case Array: {
ReadableArray readableArray = readableMap.getArray(key);
switch (readableArray.getType(0)) {
case String:
bundle.putStringArray(key, ArgumentsEx.toStringArray(readableArray));
break;
case Boolean:
bundle.putBooleanArray(key, ArgumentsEx.toBooleanArray(readableArray));
break;
case Number:
// Can be int or double but we just assume double for now
bundle.putDoubleArray(key, ArgumentsEx.toDoubleArray(readableArray));
break;
case Map:
bundle.putParcelableArray(key, ArgumentsEx.toBundleArray(readableArray));
break;
case Array:
throw new UnsupportedOperationException("Arrays of arrays is not supported");
if(readableArray.size() > 0) {
switch (readableArray.getType(0)) {
case String:
bundle.putStringArray(key, ArgumentsEx.toStringArray(readableArray));
break;
case Boolean:
bundle.putBooleanArray(key, ArgumentsEx.toBooleanArray(readableArray));
break;
case Number:
// Can be int or double but we just assume double for now
bundle.putDoubleArray(key, ArgumentsEx.toDoubleArray(readableArray));
break;
case Map:
bundle.putParcelableArray(key, ArgumentsEx.toBundleArray(readableArray));
break;
case Array:
throw new UnsupportedOperationException("Arrays of arrays is not supported");
}
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,14 @@ private static boolean isArray(@NonNull Object obj) {

/**
* @param obj input array
* @param listItemClass Defines the conent type of the list
* @param listItemClass Defines the content type of the list
* @return List<listItemClass>
*/
public static List getList(Object obj, @NonNull Class listItemClass) {
public static List getList(@Nullable Object obj, @NonNull Class listItemClass) {
if (obj == null) {
return new ArrayList<>();
}

if (!isArray(obj)) {
throw new IllegalArgumentException("Should never reach here, expected an array, received: " + obj);
}
Expand Down