mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-22 11:16:06 +08:00
Add Share module
Summary: revision of https://github.com/facebook/react-native/pull/5476 It has only one method `shareTextContent` and next will be`shareBinaryContent`. In Android, Promise can't receive a result, because `startActivityForResult` is not working with `Intent.ACTION_SEND`. Maybe we can use `createChooser(Intent target, CharSequence title, IntentSender sender)` which requires API level 22. Closes https://github.com/facebook/react-native/pull/5904 Differential Revision: D3612889 fbshipit-source-id: 0e7aaf34b076a99089cc76bd649e6da067d9a760
This commit is contained in:
committed by
Facebook Github Bot 6
parent
c21d3a1029
commit
3b35732800
@@ -14,6 +14,7 @@ deps = [
|
||||
react_native_target('java/com/facebook/react/common:common'),
|
||||
react_native_target('java/com/facebook/react/modules/core:core'),
|
||||
react_native_target('java/com/facebook/react/modules/datepicker:datepicker'),
|
||||
react_native_target('java/com/facebook/react/modules/share:share'),
|
||||
react_native_target('java/com/facebook/react/modules/systeminfo:systeminfo'),
|
||||
react_native_target('java/com/facebook/react/modules/timepicker:timepicker'),
|
||||
react_native_target('java/com/facebook/react/touch:touch'),
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* 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 java.util.List;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Instrumentation.ActivityMonitor;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.IntentFilter.MalformedMimeTypeException;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
|
||||
import com.facebook.react.bridge.BaseJavaModule;
|
||||
import com.facebook.react.testing.ReactInstanceSpecForTest;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.JavaScriptModule;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.bridge.WritableNativeMap;
|
||||
import com.facebook.react.modules.share.ShareModule;
|
||||
import com.facebook.react.testing.ReactAppInstrumentationTestCase;
|
||||
|
||||
/**
|
||||
* Test case for {@link ShareModule}.
|
||||
*/
|
||||
public class ShareTestCase extends ReactAppInstrumentationTestCase {
|
||||
|
||||
private static interface ShareTestModule extends JavaScriptModule {
|
||||
public void showShareDialog(WritableMap content, WritableMap options);
|
||||
}
|
||||
|
||||
private static class ShareRecordingModule extends BaseJavaModule {
|
||||
|
||||
private int mOpened = 0;
|
||||
private int mErrors = 0;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ShareRecordingModule";
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void recordOpened() {
|
||||
mOpened++;
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void recordError() {
|
||||
mErrors++;
|
||||
}
|
||||
|
||||
public int getOpened() {
|
||||
return mOpened;
|
||||
}
|
||||
|
||||
public int getErrors() {
|
||||
return mErrors;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final ShareRecordingModule mRecordingModule = new ShareRecordingModule();
|
||||
|
||||
@Override
|
||||
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
|
||||
return super.createReactInstanceSpecForTest()
|
||||
.addNativeModule(mRecordingModule)
|
||||
.addJSModule(ShareTestModule.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getReactApplicationKeyUnderTest() {
|
||||
return "ShareTestApp";
|
||||
}
|
||||
|
||||
private ShareTestModule getTestModule() {
|
||||
return getReactContext().getCatalystInstance().getJSModule(ShareTestModule.class);
|
||||
}
|
||||
|
||||
public void testShowBasicShareDialog() {
|
||||
final WritableMap content = new WritableNativeMap();
|
||||
content.putString("message", "Hello, ReactNative!");
|
||||
final WritableMap options = new WritableNativeMap();
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CHOOSER);
|
||||
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
ActivityMonitor monitor = getInstrumentation().addMonitor(intentFilter, null, true);
|
||||
|
||||
getTestModule().showShareDialog(content, options);
|
||||
|
||||
waitForBridgeAndUIIdle();
|
||||
getInstrumentation().waitForIdleSync();
|
||||
|
||||
assertEquals(1, monitor.getHits());
|
||||
assertEquals(1, mRecordingModule.getOpened());
|
||||
assertEquals(0, mRecordingModule.getErrors());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
41
ReactAndroid/src/androidTest/js/ShareTestModule.js
Normal file
41
ReactAndroid/src/androidTest/js/ShareTestModule.js
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* 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 ShareTestModule
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var BatchedBridge = require('BatchedBridge');
|
||||
var React = require('React');
|
||||
var RecordingModule = require('NativeModules').ShareRecordingModule;
|
||||
var Share = require('Share');
|
||||
var View = require('View');
|
||||
|
||||
var ShareTestApp = React.createClass({
|
||||
render: function() {
|
||||
return (<View />);
|
||||
},
|
||||
});
|
||||
|
||||
var ShareTestModule = {
|
||||
ShareTestApp: ShareTestApp,
|
||||
showShareDialog: function(content, options) {
|
||||
Share.share(content, options).then(
|
||||
() => RecordingModule.recordOpened(),
|
||||
({code, message}) => RecordingModule.recordError()
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
BatchedBridge.registerCallableModule(
|
||||
'ShareTestModule',
|
||||
ShareTestModule
|
||||
);
|
||||
|
||||
module.exports = ShareTestModule;
|
||||
@@ -25,6 +25,7 @@ require('DatePickerDialogTestModule');
|
||||
require('MeasureLayoutTestModule');
|
||||
require('PickerAndroidTestModule');
|
||||
require('ScrollViewTestModule');
|
||||
require('ShareTestModule');
|
||||
require('SwipeRefreshLayoutTestModule');
|
||||
require('TextInputTestModule');
|
||||
require('TimePickerDialogTestModule');
|
||||
@@ -74,6 +75,10 @@ var apps = [
|
||||
appKey: 'ScrollViewTestApp',
|
||||
component: () => require('ScrollViewTestModule').ScrollViewTestApp,
|
||||
},
|
||||
{
|
||||
appKey: 'ShareTestApp',
|
||||
component: () => require('ShareTestModule').ShareTestApp,
|
||||
},
|
||||
{
|
||||
appKey: 'SubviewsClippingTestApp',
|
||||
component: () => require('SubviewsClippingTestModule').App,
|
||||
|
||||
Reference in New Issue
Block a user