下面列出了怎么用com.facebook.react.bridge.JavaOnlyArray的API类实例代码及写法,或者点击链接到github查看源代码。
public void collectViewUpdates(JavaOnlyMap propsMap) {
List<JavaOnlyMap> transforms = new ArrayList<>(mTransformConfigs.size());
for (TransformConfig transformConfig : mTransformConfigs) {
double value;
if (transformConfig instanceof AnimatedTransformConfig) {
int nodeTag = ((AnimatedTransformConfig) transformConfig).mNodeTag;
AnimatedNode node = mNativeAnimatedNodesManager.getNodeById(nodeTag);
if (node == null) {
throw new IllegalArgumentException("Mapped style node does not exists");
} else if (node instanceof ValueAnimatedNode) {
value = ((ValueAnimatedNode) node).getValue();
} else {
throw new IllegalArgumentException("Unsupported type of node used as a transform child " +
"node " + node.getClass());
}
} else {
value = ((StaticTransformConfig) transformConfig).mValue;
}
transforms.add(JavaOnlyMap.of(transformConfig.mProperty, value));
}
propsMap.putArray("transform", JavaOnlyArray.from(transforms));
}
@Test
public void testPausingAndResuming() {
mTiming.onHostResume();
mTiming.createTimer(41, 1, 0, true);
stepChoreographerFrame();
verify(mJSTimersMock).callTimers(JavaOnlyArray.of(41));
reset(mJSTimersMock);
mTiming.onHostPause();
stepChoreographerFrame();
verifyNoMoreInteractions(mJSTimersMock);
reset(mJSTimersMock);
mTiming.onHostResume();
stepChoreographerFrame();
verify(mJSTimersMock).callTimers(JavaOnlyArray.of(41));
}
@Test
public void testHeadlessJsTaskInForeground() {
mTiming.onHostResume();
mTiming.onHeadlessJsTaskStart(42);
mTiming.createTimer(41, 1, 0, true);
stepChoreographerFrame();
verify(mJSTimersMock).callTimers(JavaOnlyArray.of(41));
reset(mJSTimersMock);
mTiming.onHeadlessJsTaskFinish(42);
stepChoreographerFrame();
verify(mJSTimersMock).callTimers(JavaOnlyArray.of(41));
reset(mJSTimersMock);
mTiming.onHostPause();
verifyNoMoreInteractions(mJSTimersMock);
}
@Test
public void testHeadlessJsTaskIntertwine() {
mTiming.onHostResume();
mTiming.onHeadlessJsTaskStart(42);
mTiming.createTimer(41, 1, 0, true);
mTiming.onHostPause();
stepChoreographerFrame();
verify(mJSTimersMock).callTimers(JavaOnlyArray.of(41));
reset(mJSTimersMock);
mTiming.onHostResume();
mTiming.onHeadlessJsTaskFinish(42);
stepChoreographerFrame();
verify(mJSTimersMock).callTimers(JavaOnlyArray.of(41));
reset(mJSTimersMock);
mTiming.onHostPause();
stepChoreographerFrame();
verifyNoMoreInteractions(mJSTimersMock);
}
@Test
public void testDeleteViews() {
UIManagerModule uiManager = getUIManagerModule();
TestMoveDeleteHierarchy hierarchy = createMoveDeleteHierarchy(uiManager);
View expectedViewAt0 = hierarchy.nativeRootView.getChildAt(1);
View expectedViewAt1 = hierarchy.nativeRootView.getChildAt(2);
uiManager.manageChildren(
hierarchy.rootView,
null,
null,
null,
null,
JavaOnlyArray.of(0, 3));
uiManager.onBatchComplete();
executePendingFrameCallbacks();
assertChildrenAreExactly(
hierarchy.nativeRootView,
expectedViewAt0,
expectedViewAt1);
}
@Test
public void testMoveAndDeleteViews() {
UIManagerModule uiManager = getUIManagerModule();
TestMoveDeleteHierarchy hierarchy = createMoveDeleteHierarchy(uiManager);
View expectedViewAt0 = hierarchy.nativeRootView.getChildAt(0);
View expectedViewAt1 = hierarchy.nativeRootView.getChildAt(3);
View expectedViewAt2 = hierarchy.nativeRootView.getChildAt(2);
uiManager.manageChildren(
hierarchy.rootView,
JavaOnlyArray.of(3),
JavaOnlyArray.of(1),
null,
null,
JavaOnlyArray.of(1));
uiManager.onBatchComplete();
executePendingFrameCallbacks();
assertChildrenAreExactly(
hierarchy.nativeRootView,
expectedViewAt0,
expectedViewAt1,
expectedViewAt2);
}
@Test(expected = IllegalViewOperationException.class)
public void testMoveAndDeleteRemoveViewsDuplicateRemove() {
UIManagerModule uiManager = getUIManagerModule();
TestMoveDeleteHierarchy hierarchy = createMoveDeleteHierarchy(uiManager);
uiManager.manageChildren(
hierarchy.rootView,
JavaOnlyArray.of(3),
JavaOnlyArray.of(1),
null,
null,
JavaOnlyArray.of(3));
uiManager.onBatchComplete();
executePendingFrameCallbacks();
}
@Test(expected = IllegalViewOperationException.class)
public void testDuplicateRemove() {
UIManagerModule uiManager = getUIManagerModule();
TestMoveDeleteHierarchy hierarchy = createMoveDeleteHierarchy(uiManager);
uiManager.manageChildren(
hierarchy.rootView,
null,
null,
null,
null,
JavaOnlyArray.of(3, 3));
uiManager.onBatchComplete();
executePendingFrameCallbacks();
}
@Test
public void testDeleteViewsWithChildren() {
UIManagerModule uiManager = getUIManagerModule();
TestMoveDeleteHierarchy hierarchy = createMoveDeleteHierarchy(uiManager);
View expectedViewAt0 = hierarchy.nativeRootView.getChildAt(0);
View expectedViewAt1 = hierarchy.nativeRootView.getChildAt(2);
View expectedViewAt2 = hierarchy.nativeRootView.getChildAt(3);
uiManager.manageChildren(
hierarchy.rootView,
null,
null,
null,
null,
JavaOnlyArray.of(1));
uiManager.onBatchComplete();
executePendingFrameCallbacks();
assertChildrenAreExactly(
hierarchy.nativeRootView,
expectedViewAt0,
expectedViewAt1,
expectedViewAt2);
}
@Test
public void testNodeValueListenerIfNotListening() {
int nodeId = 1;
createSimpleAnimatedViewWithOpacity(1000, 0d);
JavaOnlyArray frames = JavaOnlyArray.of(0d, 0.2d, 0.4d, 0.6d, 0.8d, 1d);
Callback animationCallback = mock(Callback.class);
AnimatedNodeValueListener valueListener = mock(AnimatedNodeValueListener.class);
mNativeAnimatedNodesManager.startListeningToAnimatedNodeValue(nodeId, valueListener);
mNativeAnimatedNodesManager.startAnimatingNode(
1,
nodeId,
JavaOnlyMap.of("type", "frames", "frames", frames, "toValue", 1d),
animationCallback);
mNativeAnimatedNodesManager.runUpdates(nextFrameTime());
verify(valueListener).onValueUpdate(eq(0d));
mNativeAnimatedNodesManager.stopListeningToAnimatedNodeValue(nodeId);
reset(valueListener);
mNativeAnimatedNodesManager.runUpdates(nextFrameTime());
verifyNoMoreInteractions(valueListener);
}
@Test
public void testNativeAnimatedEventDoUpdate() {
int viewTag = 1000;
createSimpleAnimatedViewWithOpacity(viewTag, 0d);
mNativeAnimatedNodesManager.addAnimatedEventToView(viewTag, "topScroll", JavaOnlyMap.of(
"animatedValueTag", 1,
"nativeEventPath", JavaOnlyArray.of("contentOffset", "y")));
mNativeAnimatedNodesManager.onEventDispatch(createScrollEvent(viewTag, 10));
ArgumentCaptor<ReactStylesDiffMap> stylesCaptor =
ArgumentCaptor.forClass(ReactStylesDiffMap.class);
reset(mUIImplementationMock);
mNativeAnimatedNodesManager.runUpdates(nextFrameTime());
verify(mUIImplementationMock).synchronouslyUpdateViewOnUIThread(eq(viewTag), stylesCaptor.capture());
assertThat(stylesCaptor.getValue().getDouble("opacity", Double.NaN)).isEqualTo(10);
}
@Test
public void testNativeAnimatedEventDoNotUpdate() {
int viewTag = 1000;
createSimpleAnimatedViewWithOpacity(viewTag, 0d);
mNativeAnimatedNodesManager.addAnimatedEventToView(viewTag, "otherEvent", JavaOnlyMap.of(
"animatedValueTag", 1,
"nativeEventPath", JavaOnlyArray.of("contentOffset", "y")));
mNativeAnimatedNodesManager.addAnimatedEventToView(999, "topScroll", JavaOnlyMap.of(
"animatedValueTag", 1,
"nativeEventPath", JavaOnlyArray.of("contentOffset", "y")));
mNativeAnimatedNodesManager.onEventDispatch(createScrollEvent(viewTag, 10));
ArgumentCaptor<ReactStylesDiffMap> stylesCaptor =
ArgumentCaptor.forClass(ReactStylesDiffMap.class);
reset(mUIImplementationMock);
mNativeAnimatedNodesManager.runUpdates(nextFrameTime());
verify(mUIImplementationMock).synchronouslyUpdateViewOnUIThread(eq(viewTag), stylesCaptor.capture());
assertThat(stylesCaptor.getValue().getDouble("opacity", Double.NaN)).isEqualTo(0);
}
@Test
public void parsesArrays() throws Exception {
JavaOnlyArray input = new JavaOnlyArray();
input.pushString("Hello");
input.pushInt(123);
input.pushDouble(123.456);
input.pushBoolean(true);
input.pushArray(new JavaOnlyArray());
input.pushMap(new JavaOnlyMap());
input.pushNull();
JSONArray result = new JSONParser().parse(input);
assertThat(result.length()).isEqualTo(6);
assertThat(result.get(0)).isEqualTo("Hello");
assertThat(result.get(1)).isEqualTo(123);
assertThat(result.get(2)).isEqualTo(123.456);
assertThat(result.get(3)).isEqualTo(true);
assertThat(result.getJSONArray(4).length()).isZero();
assertThat(result.getJSONObject(5).keys()).isEmpty();
}
@Test
public void givenInvocationEvent$setInvocationEvents_whenQuery_thenShouldCallNativeApiWithArgs() {
// given
PowerMockito.mockStatic(BugReporting.class);
final Map<String, Object> args = new HashMap<>();
ArgsRegistry.registerInstabugInvocationEventsArgs(args);
final String[] keysArray = args.keySet().toArray(new String[0]);
JavaOnlyArray actualArray = new JavaOnlyArray();
for (String key : keysArray) {
actualArray.pushString(key);
}
// when
bugReportingModule.setInvocationEvents(actualArray);
// then
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
BugReporting.setInvocationEvents(args.values().toArray(new InstabugInvocationEvent[0]));
}
@Test
public void givenOptions$setOptions_whenQuery_thenShouldCallNativeApiWithArgs() {
// given
PowerMockito.mockStatic(BugReporting.class);
final Map<String, Object> args = new HashMap<>();
ArgsRegistry.registerInvocationOptionsArgs(args);
final String[] keysArray = args.keySet().toArray(new String[0]);
JavaOnlyArray actualArray = new JavaOnlyArray();
actualArray.pushString(keysArray[0]);
actualArray.pushString(keysArray[1]);
// when
bugReportingModule.setOptions(actualArray);
// then
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
int option1 = (int) args.get(keysArray[0]);
int option2 = (int) args.get(keysArray[1]);
BugReporting.setOptions(option1);
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
BugReporting.setOptions(option2);
}
@Test
public void givenArray$setReportTypes_whenQuery_thenShouldCallNativeApiWithEnumArgs() {
// given
PowerMockito.mockStatic(BugReporting.class);
final Map<String, Object> args = new HashMap<>();
ArgsRegistry.registerInstabugReportTypesArgs(args);
final String[] keysArray = args.keySet().toArray(new String[0]);
JavaOnlyArray actualArray = new JavaOnlyArray();
actualArray.pushString(keysArray[0]);
actualArray.pushString(keysArray[1]);
// when
bugReportingModule.setReportTypes(actualArray);
// then
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
int type1 = (int) args.get(keysArray[0]);
int type2 = (int) args.get(keysArray[1]);
BugReporting.setReportTypes(type1, type2);
}
@Test
public void givenArgs$showBugReportingWithReportTypeAndOptions_whenQuery_thenShouldCallNativeApiWithEnums() {
// given
PowerMockito.mockStatic(BugReporting.class);
final Map<String, Object> optionsArgs = new HashMap<>();
final Map<String, Object> reportTypeArgs = new HashMap<>();
ArgsRegistry.registerInvocationOptionsArgs(optionsArgs);
ArgsRegistry.registerInstabugReportTypesArgs(reportTypeArgs);
final String[] keysArray = optionsArgs.keySet().toArray(new String[0]);
final String[] reportTypeKeys = reportTypeArgs.keySet().toArray(new String[0]);
JavaOnlyArray actualArray = new JavaOnlyArray();
actualArray.pushString(keysArray[0]);
actualArray.pushString(keysArray[1]);
// when
bugReportingModule.show(reportTypeKeys[0], actualArray);
// then
int option1 = (int) optionsArgs.get(keysArray[0]);
int option2 = (int) optionsArgs.get(keysArray[1]);
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
BugReporting.setOptions(option1);
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
BugReporting.setOptions(option2);
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
BugReporting.show((int) reportTypeArgs.get(reportTypeKeys[0]));
}
/********Feature Requests*********/
@Test
public void givenArgs$setEmailFieldRequiredForFeatureRequests_whenQuery_thenShouldCallNativeApi() {
// given
PowerMockito.mockStatic(FeatureRequests.class);
PowerMockito.mockStatic(Arguments.class);
// when
PowerMockito.when(Arguments.createArray()).thenReturn(new JavaOnlyArray());
ReadableArray actionTypes = Arguments.createArray();
((WritableArray) actionTypes).pushString("requestNewFeature");
((WritableArray) actionTypes).pushString("addCommentToFeature");
featureRequestsModule.setEmailFieldRequiredForFeatureRequests(true, actionTypes );
int[] parsedActionTypes = new int[2];
parsedActionTypes[0] = ActionType.REQUEST_NEW_FEATURE;
parsedActionTypes[1] = ActionType.ADD_COMMENT_TO_FEATURE;
// then
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
FeatureRequests.setEmailFieldRequired(true, parsedActionTypes);
}
@Test
public void givenCallback$getTags_whenQuery_thenShouldCallNativeApiAndInvokeCallback() {
// given
PowerMockito.mockStatic(Instabug.class);
PowerMockito.mockStatic(Arguments.class);
Callback callback = mock(Callback.class);
// when
ArrayList<String> tags = new ArrayList<>();
tags.add("tag1");
tags.add("tag2");
PowerMockito.when(Instabug.getTags()).thenReturn(tags);
PowerMockito.when(Arguments.createArray()).thenReturn(new JavaOnlyArray());
rnModule.getTags(callback);
// then
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
Instabug.getTags();
WritableArray expectedArray = new JavaOnlyArray();
expectedArray.pushString("tag1");
expectedArray.pushString("tag2");
verify(callback).invoke(expectedArray);
}
@Test
public void given$getAvailableSurveys_whenQuery_thenShouldCallNativeApiAndInvokeCallback() throws Exception {
// given
PowerMockito.mockStatic(Surveys.class);
PowerMockito.mockStatic(SystemClock.class);
PowerMockito.mockStatic(Arguments.class);
Callback callback = mock(Callback.class);
JSONArray json = mock(JSONArray.class);
// when
PowerMockito.whenNew(JSONArray.class).withAnyArguments().thenReturn(json);
PowerMockito.when(Arguments.createArray()).thenReturn(new JavaOnlyArray());
surveysModule.getAvailableSurveys(callback);
// then
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
Surveys.getAvailableSurveys();
verify(callback).invoke(any());
}
@Test
public void testPropsApplied() {
UIManagerModule uiManager = getUIManagerModule();
ReactRootView rootView = new ReactRootView(RuntimeEnvironment.application);
rootView.setLayoutParams(new ReactRootView.LayoutParams(100, 100));
int rootTag = uiManager.addRootView(rootView);
int textInputTag = rootTag + 1;
final String hintStr = "placeholder text";
uiManager.createView(
textInputTag,
ReactTextInputManager.REACT_CLASS,
rootTag,
JavaOnlyMap.of(
ViewProps.FONT_SIZE, 13.37, ViewProps.HEIGHT, 20.0, "placeholder", hintStr));
uiManager.manageChildren(
rootTag,
null,
null,
JavaOnlyArray.of(textInputTag),
JavaOnlyArray.of(0),
null);
uiManager.onBatchComplete();
executePendingChoreographerCallbacks();
EditText editText = (EditText) rootView.getChildAt(0);
assertThat(editText.getHint()).isEqualTo(hintStr);
assertThat(editText.getTextSize()).isEqualTo((float) Math.ceil(13.37));
assertThat(editText.getHeight()).isEqualTo(20);
}
private ReactRootView createText(
UIManagerModule uiManager,
JavaOnlyMap textProps,
JavaOnlyMap rawTextProps) {
ReactRootView rootView = new ReactRootView(RuntimeEnvironment.application);
int rootTag = uiManager.addRootView(rootView);
int textTag = rootTag + 1;
int rawTextTag = textTag + 1;
uiManager.createView(
textTag,
ReactTextViewManager.REACT_CLASS,
rootTag,
textProps);
uiManager.createView(
rawTextTag,
ReactRawTextManager.REACT_CLASS,
rootTag,
rawTextProps);
uiManager.manageChildren(
textTag,
null,
null,
JavaOnlyArray.of(rawTextTag),
JavaOnlyArray.of(0),
null);
uiManager.manageChildren(
rootTag,
null,
null,
JavaOnlyArray.of(textTag),
JavaOnlyArray.of(0),
null);
uiManager.onBatchComplete();
executePendingFrameCallbacks();
return rootView;
}
@Test
public void testBorderColor() {
ReactImageManager viewManager = new ReactImageManager();
ReactImageView view = viewManager.createViewInstance(mThemeContext);
viewManager.updateProperties(
view,
buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg"))));
viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 0, 255, 255)));
int borderColor = view.getHierarchy().getRoundingParams().getBorderColor();
assertEquals(0, Color.alpha(borderColor));
assertEquals(0, Color.red(borderColor));
assertEquals(255, Color.green(borderColor));
assertEquals(255, Color.blue(borderColor));
viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 255, 50, 128)));
borderColor = view.getHierarchy().getRoundingParams().getBorderColor();
assertEquals(0, Color.alpha(borderColor));
assertEquals(255, Color.red(borderColor));
assertEquals(50, Color.green(borderColor));
assertEquals(128, Color.blue(borderColor));
viewManager.updateProperties(view, buildStyles("borderColor", null));
borderColor = view.getHierarchy().getRoundingParams().getBorderColor();
assertEquals(0, Color.alpha(borderColor));
assertEquals(0, Color.red(borderColor));
assertEquals(0, Color.green(borderColor));
assertEquals(0, Color.blue(borderColor));
}
@Test
public void testRoundedCorners() {
ReactImageManager viewManager = new ReactImageManager();
ReactImageView view = viewManager.createViewInstance(mThemeContext);
viewManager.updateProperties(
view,
buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg"))));
// We can't easily verify if rounded corner was honored or not, this tests simply verifies
// we're not crashing..
viewManager.updateProperties(view, buildStyles("borderRadius", (double) 10));
viewManager.updateProperties(view, buildStyles("borderRadius", (double) 0));
viewManager.updateProperties(view, buildStyles("borderRadius", null));
}
@Test
public void testGetWithoutHeaders() throws Exception {
OkHttpClient httpClient = mock(OkHttpClient.class);
when(httpClient.newCall(any(Request.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Call callMock = mock(Call.class);
return callMock;
}
});
OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
when(clientBuilder.build()).thenReturn(httpClient);
when(httpClient.newBuilder()).thenReturn(clientBuilder);
NetworkingModule networkingModule =
new NetworkingModule(mock(ReactApplicationContext.class), "", httpClient);
networkingModule.sendRequest(
"GET",
"http://somedomain/foo",
/* requestId */ 0,
/* headers */ JavaOnlyArray.of(),
/* body */ null,
/* responseType */ "text",
/* useIncrementalUpdates*/ true,
/* timeout */ 0,
/* withCredentials */ false);
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
verify(httpClient).newCall(argumentCaptor.capture());
assertThat(argumentCaptor.getValue().url().toString()).isEqualTo("http://somedomain/foo");
// We set the User-Agent header by default
assertThat(argumentCaptor.getValue().headers().size()).isEqualTo(1);
assertThat(argumentCaptor.getValue().method()).isEqualTo("GET");
}
@Test
public void testFailGetWithInvalidHeadersStruct() throws Exception {
RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class);
ReactApplicationContext context = mock(ReactApplicationContext.class);
when(context.getJSModule(any(Class.class))).thenReturn(emitter);
OkHttpClient httpClient = mock(OkHttpClient.class);
OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
when(clientBuilder.build()).thenReturn(httpClient);
when(httpClient.newBuilder()).thenReturn(clientBuilder);
NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient);
List<JavaOnlyArray> invalidHeaders = Arrays.asList(JavaOnlyArray.of("foo"));
mockEvents();
networkingModule.sendRequest(
"GET",
"http://somedoman/foo",
/* requestId */ 0,
/* headers */ JavaOnlyArray.from(invalidHeaders),
/* body */ null,
/* responseType */ "text",
/* useIncrementalUpdates*/ true,
/* timeout */ 0,
/* withCredentials */ false);
verifyErrorEmit(emitter, 0);
}
@Test
public void testFailPostWithoutContentType() throws Exception {
RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class);
ReactApplicationContext context = mock(ReactApplicationContext.class);
when(context.getJSModule(any(Class.class))).thenReturn(emitter);
OkHttpClient httpClient = mock(OkHttpClient.class);
OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
when(clientBuilder.build()).thenReturn(httpClient);
when(httpClient.newBuilder()).thenReturn(clientBuilder);
NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient);
JavaOnlyMap body = new JavaOnlyMap();
body.putString("string", "This is request body");
mockEvents();
networkingModule.sendRequest(
"POST",
"http://somedomain/bar",
0,
JavaOnlyArray.of(),
body,
/* responseType */ "text",
/* useIncrementalUpdates*/ true,
/* timeout */ 0,
/* withCredentials */ false);
verifyErrorEmit(emitter, 0);
}
@Test
public void testFailInvalidUrl() throws Exception {
RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class);
ReactApplicationContext context = mock(ReactApplicationContext.class);
when(context.getJSModule(any(Class.class))).thenReturn(emitter);
OkHttpClient httpClient = mock(OkHttpClient.class);
OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
when(clientBuilder.build()).thenReturn(httpClient);
when(httpClient.newBuilder()).thenReturn(clientBuilder);
NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient);
mockEvents();
networkingModule.sendRequest(
"GET",
"aaa",
/* requestId */ 0,
/* headers */ JavaOnlyArray.of(),
/* body */ null,
/* responseType */ "text",
/* useIncrementalUpdates*/ true,
/* timeout */ 0,
/* withCredentials */ false);
verifyErrorEmit(emitter, 0);
}
@Test
public void testHeaders() throws Exception {
OkHttpClient httpClient = mock(OkHttpClient.class);
when(httpClient.newCall(any(Request.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Call callMock = mock(Call.class);
return callMock;
}
});
OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
when(clientBuilder.build()).thenReturn(httpClient);
when(httpClient.newBuilder()).thenReturn(clientBuilder);
NetworkingModule networkingModule =
new NetworkingModule(mock(ReactApplicationContext.class), "", httpClient);
List<JavaOnlyArray> headers = Arrays.asList(
JavaOnlyArray.of("Accept", "text/plain"),
JavaOnlyArray.of("User-Agent", "React test agent/1.0"));
networkingModule.sendRequest(
"GET",
"http://someurl/baz",
0,
JavaOnlyArray.from(headers),
null,
/* responseType */ "text",
/* useIncrementalUpdates*/ true,
/* timeout */ 0,
/* withCredentials */ false);
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
verify(httpClient).newCall(argumentCaptor.capture());
Headers requestHeaders = argumentCaptor.getValue().headers();
assertThat(requestHeaders.size()).isEqualTo(2);
assertThat(requestHeaders.get("Accept")).isEqualTo("text/plain");
assertThat(requestHeaders.get("User-Agent")).isEqualTo("React test agent/1.0");
}
@Test
public void testSimpleTimer() {
mTiming.onHostResume();
mTiming.createTimer(1, 1, 0, false);
stepChoreographerFrame();
verify(mJSTimersMock).callTimers(JavaOnlyArray.of(1));
reset(mJSTimersMock);
stepChoreographerFrame();
verifyNoMoreInteractions(mJSTimersMock);
}