-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support invoking and returning arbitrary types from Java sync hooks
Reviewed By: mhorowitz Differential Revision: D4409900 fbshipit-source-id: 347e33c442b32f64355d343308c218c15cf5a70f
- Loading branch information
1 parent
cfb9028
commit f8c72f5
Showing
13 changed files
with
318 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...androidTest/java/com/facebook/react/tests/CatalystNativeJavaToJSReturnValuesTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,163 @@ | |||
/** | |||
* Copyright (c) 2014-present, Facebook, Inc. | |||
* All rights reserved. | |||
* This source code is licensed under the BSD-style license found in the | |||
* LICENSE file in the root directory of this source tree. An additional grant | |||
* of patent rights can be found in the PATENTS file in the same directory. | |||
*/ | |||
|
|||
package com.facebook.react.tests; | |||
|
|||
import java.util.ArrayList; | |||
|
|||
import com.facebook.react.bridge.BaseJavaModule; | |||
import com.facebook.react.bridge.CatalystInstance; | |||
import com.facebook.react.bridge.JavaScriptModule; | |||
import com.facebook.react.bridge.ReactMethod; | |||
import com.facebook.react.bridge.WritableArray; | |||
import com.facebook.react.bridge.WritableMap; | |||
import com.facebook.react.bridge.WritableNativeArray; | |||
import com.facebook.react.bridge.WritableNativeMap; | |||
import com.facebook.react.module.annotations.ReactModule; | |||
import com.facebook.react.testing.AssertModule; | |||
import com.facebook.react.testing.FakeWebSocketModule; | |||
import com.facebook.react.testing.ReactIntegrationTestCase; | |||
import com.facebook.react.testing.ReactTestHelper; | |||
import com.facebook.react.uimanager.UIImplementationProvider; | |||
import com.facebook.react.uimanager.UIManagerModule; | |||
import com.facebook.react.uimanager.ViewManager; | |||
|
|||
/** | |||
* Test marshalling return values from Java to JS | |||
*/ | |||
public class CatalystNativeJavaToJSReturnValuesTestCase extends ReactIntegrationTestCase { | |||
|
|||
private interface TestJavaToJSReturnValuesModule extends JavaScriptModule { | |||
void callMethod(String methodName, String expectedReturnType, String expectedJSON); | |||
void triggerException(); | |||
} | |||
|
|||
@ReactModule(name = "TestModule") | |||
private static class TestModule extends BaseJavaModule { | |||
@Override | |||
public String getName() { | |||
return "TestModule"; | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
boolean getBoolean() { | |||
return true; | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
Boolean getBoxedBoolean() { | |||
return Boolean.valueOf(true); | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
int getInt() { | |||
return 42; | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
Integer getBoxedInt() { | |||
return Integer.valueOf(42); | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
double getDouble() { | |||
return 3.14159; | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
Double getBoxedDouble() { | |||
return Double.valueOf(3.14159); | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
String getString() { | |||
return "Hello world!"; | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
WritableArray getArray() { | |||
WritableArray arr = new WritableNativeArray(); | |||
arr.pushString("a"); | |||
arr.pushBoolean(true); | |||
return arr; | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
WritableMap getMap() { | |||
WritableMap map = new WritableNativeMap(); | |||
map.putBoolean("a", true); | |||
map.putBoolean("b", false); | |||
return map; | |||
} | |||
|
|||
@ReactMethod(isBlockingSynchronousMethod = true) | |||
boolean triggerException() { | |||
throw new RuntimeException("Exception triggered"); | |||
} | |||
} | |||
|
|||
private AssertModule mAssertModule; | |||
private CatalystInstance mInstance; | |||
|
|||
@Override | |||
protected void setUp() throws Exception { | |||
super.setUp(); | |||
|
|||
final UIManagerModule mUIManager = new UIManagerModule( | |||
getContext(), | |||
new ArrayList<ViewManager>(), | |||
new UIImplementationProvider(), | |||
false); | |||
|
|||
mAssertModule = new AssertModule(); | |||
|
|||
mInstance = ReactTestHelper.catalystInstanceBuilder(this) | |||
.addNativeModule(mAssertModule) | |||
.addNativeModule(new FakeWebSocketModule()) | |||
.addJSModule(TestJavaToJSReturnValuesModule.class) | |||
.addNativeModule(mUIManager) | |||
.addNativeModule(new TestModule()) | |||
.build(); | |||
} | |||
|
|||
public void testGetPrimitives() { | |||
TestJavaToJSReturnValuesModule m = mInstance.getJSModule(TestJavaToJSReturnValuesModule.class); | |||
|
|||
// jboolean is actually an unsigned char, so we don't get JS booleans | |||
m.callMethod("getBoolean", "number", "1"); | |||
m.callMethod("getBoxedBoolean", "number", "1"); | |||
|
|||
m.callMethod("getInt", "number", "42"); | |||
m.callMethod("getBoxedInt", "number", "42"); | |||
|
|||
m.callMethod("getDouble", "number", "3.14159"); | |||
m.callMethod("getBoxedDouble", "number", "3.14159"); | |||
|
|||
waitForBridgeAndUIIdle(); | |||
mAssertModule.verifyAssertsAndReset(); | |||
} | |||
|
|||
public void testObjectTypes() { | |||
TestJavaToJSReturnValuesModule m = mInstance.getJSModule(TestJavaToJSReturnValuesModule.class); | |||
|
|||
m.callMethod("getString", "string", "\"Hello world!\""); | |||
m.callMethod("getArray", "object", "[\"a\",true]"); | |||
m.callMethod("getMap", "object", "{\"b\":false,\"a\":true}"); | |||
|
|||
waitForBridgeAndUIIdle(); | |||
mAssertModule.verifyAssertsAndReset(); | |||
} | |||
|
|||
public void testThrowsException() { | |||
TestJavaToJSReturnValuesModule m = mInstance.getJSModule(TestJavaToJSReturnValuesModule.class); | |||
m.triggerException(); | |||
|
|||
waitForBridgeAndUIIdle(); | |||
mAssertModule.verifyAssertsAndReset(); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
ReactAndroid/src/androidTest/js/TestJavaToJSReturnValuesModule.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,40 @@ | |||
/** | |||
* Copyright (c) 2013-present, Facebook, Inc. | |||
* All rights reserved. | |||
* | |||
* This source code is licensed under the BSD-style license found in the | |||
* LICENSE file in the root directory of this source tree. An additional grant | |||
* of patent rights can be found in the PATENTS file in the same directory. | |||
* | |||
* @providesModule TestJavaToJSReturnValuesModule | |||
*/ | |||
|
|||
'use strict'; | |||
|
|||
const BatchedBridge = require('BatchedBridge'); | |||
|
|||
const {assertEquals, assertTrue} = require('Asserts'); | |||
const {TestModule} = require('NativeModules'); | |||
|
|||
var TestJavaToJSReturnValuesModule = { | |||
callMethod: function(methodName, expectedType, expectedJSON) { | |||
const result = TestModule[methodName](); | |||
assertEquals(expectedType, typeof result); | |||
assertEquals(expectedJSON, JSON.stringify(result)); | |||
}, | |||
|
|||
triggerException: function() { | |||
try { | |||
TestModule.triggerException(); | |||
} catch (ex) { | |||
assertTrue(ex.message.indexOf('Exception triggered') !== -1); | |||
} | |||
} | |||
}; | |||
|
|||
BatchedBridge.registerCallableModule( | |||
'TestJavaToJSReturnValuesModule', | |||
TestJavaToJSReturnValuesModule | |||
); | |||
|
|||
module.exports = TestJavaToJSReturnValuesModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.