| 
 | 1 | +package com.facebook.react.bridge;  | 
 | 2 | + | 
 | 3 | +import android.content.Context;  | 
 | 4 | +import android.content.Intent;  | 
 | 5 | +import android.os.Bundle;  | 
 | 6 | +import android.os.Parcel;  | 
 | 7 | +import android.os.Parcelable;  | 
 | 8 | + | 
 | 9 | +import androidx.annotation.NonNull;  | 
 | 10 | +import androidx.test.platform.app.InstrumentationRegistry;  | 
 | 11 | +import androidx.test.runner.AndroidJUnit4;  | 
 | 12 | + | 
 | 13 | +import com.facebook.soloader.SoLoader;  | 
 | 14 | + | 
 | 15 | +import org.junit.Before;  | 
 | 16 | +import org.junit.Test;  | 
 | 17 | +import org.junit.runner.RunWith;  | 
 | 18 | + | 
 | 19 | +import static com.facebook.react.bridge.Arguments.fromBundle;  | 
 | 20 | +import static org.junit.Assert.assertEquals;  | 
 | 21 | +import static org.junit.Assert.assertNotNull;  | 
 | 22 | + | 
 | 23 | +@RunWith(AndroidJUnit4.class)  | 
 | 24 | +public class ArgumentsTest {  | 
 | 25 | + | 
 | 26 | +  @Before  | 
 | 27 | +  public void setUp() {  | 
 | 28 | +    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();  | 
 | 29 | +    SoLoader.init(context, false);  | 
 | 30 | +  }  | 
 | 31 | + | 
 | 32 | +  @Test  | 
 | 33 | +  public void testFromBundle() {  | 
 | 34 | +    verifyBundle(createBundle());  | 
 | 35 | +  }  | 
 | 36 | + | 
 | 37 | +  /**  | 
 | 38 | +   * When passing a bundle via {@link Intent} extras, it gets parceled and unparceled.  | 
 | 39 | +   * Any array of bundles will return as an array of {@link Parcelable} instead. This test  | 
 | 40 | +   * verifies that {@link Arguments#fromArray} handles this situation correctly.  | 
 | 41 | +   */  | 
 | 42 | +  @Test  | 
 | 43 | +  public void testFromMarshaledBundle() {  | 
 | 44 | +    verifyBundle(marshalAndUnmarshalBundle(createBundle()));  | 
 | 45 | +  }  | 
 | 46 | + | 
 | 47 | +  private void verifyBundle(@NonNull Bundle bundle) {  | 
 | 48 | +    WritableMap map = fromBundle(bundle);  | 
 | 49 | +    assertNotNull(map);  | 
 | 50 | + | 
 | 51 | +    assertEquals(ReadableType.Array, map.getType("children"));  | 
 | 52 | +    ReadableArray children = map.getArray("children");  | 
 | 53 | +    assertNotNull(children);  | 
 | 54 | +    assertEquals(1, children.size());  | 
 | 55 | + | 
 | 56 | +    assertEquals(ReadableType.Map, children.getType(0));  | 
 | 57 | +    ReadableMap child = children.getMap(0);  | 
 | 58 | +    assertNotNull(child);  | 
 | 59 | +    assertEquals("exampleChild", child.getString("exampleChildKey"));  | 
 | 60 | +  }  | 
 | 61 | + | 
 | 62 | +  @NonNull  | 
 | 63 | +  private Bundle marshalAndUnmarshalBundle(@NonNull Bundle bundle) {  | 
 | 64 | +    Parcel parcel = null;  | 
 | 65 | +    try {  | 
 | 66 | +      parcel = Parcel.obtain();  | 
 | 67 | +      bundle.writeToParcel(parcel, 0);  | 
 | 68 | + | 
 | 69 | +      byte[] bytes = parcel.marshall();  | 
 | 70 | +      parcel.unmarshall(bytes, 0, bytes.length);  | 
 | 71 | +      parcel.setDataPosition(0);  | 
 | 72 | +      return Bundle.CREATOR.createFromParcel(parcel);  | 
 | 73 | +    } finally {  | 
 | 74 | +      if (parcel != null) {  | 
 | 75 | +        parcel.recycle();  | 
 | 76 | +      }  | 
 | 77 | +    }  | 
 | 78 | +  }  | 
 | 79 | + | 
 | 80 | +  @NonNull  | 
 | 81 | +  private Bundle createBundle() {  | 
 | 82 | +    Bundle bundle = new Bundle();  | 
 | 83 | +    Bundle[] children = new Bundle[1];  | 
 | 84 | +    children[0] = new Bundle();  | 
 | 85 | +    children[0].putString("exampleChildKey", "exampleChild");  | 
 | 86 | +    bundle.putSerializable("children", children);  | 
 | 87 | +    return bundle;  | 
 | 88 | +  }  | 
 | 89 | +}  | 
0 commit comments