|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * This source code is licensed under the BSD-style license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 6 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 7 | + */ |
| 8 | + |
| 9 | +package com.facebook.react.tests; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import android.app.Activity; |
| 15 | +import android.app.AlertDialog; |
| 16 | +import android.app.Instrumentation.ActivityMonitor; |
| 17 | +import android.content.DialogInterface; |
| 18 | +import android.content.Intent; |
| 19 | +import android.content.IntentFilter; |
| 20 | +import android.content.IntentFilter.MalformedMimeTypeException; |
| 21 | +import android.support.v4.app.DialogFragment; |
| 22 | + |
| 23 | +import com.facebook.react.bridge.BaseJavaModule; |
| 24 | +import com.facebook.react.testing.ReactInstanceSpecForTest; |
| 25 | +import com.facebook.react.bridge.ReactMethod; |
| 26 | +import com.facebook.react.bridge.JavaScriptModule; |
| 27 | +import com.facebook.react.bridge.WritableMap; |
| 28 | +import com.facebook.react.bridge.WritableNativeMap; |
| 29 | +import com.facebook.react.modules.share.ShareModule; |
| 30 | +import com.facebook.react.testing.ReactAppInstrumentationTestCase; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test case for {@link ShareModule}. |
| 34 | + */ |
| 35 | +public class ShareTestCase extends ReactAppInstrumentationTestCase { |
| 36 | + |
| 37 | + private static interface ShareTestModule extends JavaScriptModule { |
| 38 | + public void showShareDialog(WritableMap content, WritableMap options); |
| 39 | + } |
| 40 | + |
| 41 | + private static class ShareRecordingModule extends BaseJavaModule { |
| 42 | + |
| 43 | + private int mOpened = 0; |
| 44 | + private int mErrors = 0; |
| 45 | + |
| 46 | + @Override |
| 47 | + public String getName() { |
| 48 | + return "ShareRecordingModule"; |
| 49 | + } |
| 50 | + |
| 51 | + @ReactMethod |
| 52 | + public void recordOpened() { |
| 53 | + mOpened++; |
| 54 | + } |
| 55 | + |
| 56 | + @ReactMethod |
| 57 | + public void recordError() { |
| 58 | + mErrors++; |
| 59 | + } |
| 60 | + |
| 61 | + public int getOpened() { |
| 62 | + return mOpened; |
| 63 | + } |
| 64 | + |
| 65 | + public int getErrors() { |
| 66 | + return mErrors; |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + final ShareRecordingModule mRecordingModule = new ShareRecordingModule(); |
| 72 | + |
| 73 | + @Override |
| 74 | + protected ReactInstanceSpecForTest createReactInstanceSpecForTest() { |
| 75 | + return super.createReactInstanceSpecForTest() |
| 76 | + .addNativeModule(mRecordingModule) |
| 77 | + .addJSModule(ShareTestModule.class); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected String getReactApplicationKeyUnderTest() { |
| 82 | + return "ShareTestApp"; |
| 83 | + } |
| 84 | + |
| 85 | + private ShareTestModule getTestModule() { |
| 86 | + return getReactContext().getCatalystInstance().getJSModule(ShareTestModule.class); |
| 87 | + } |
| 88 | + |
| 89 | + public void testShowBasicShareDialog() { |
| 90 | + final WritableMap content = new WritableNativeMap(); |
| 91 | + content.putString("message", "Hello, ReactNative!"); |
| 92 | + final WritableMap options = new WritableNativeMap(); |
| 93 | + |
| 94 | + IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CHOOSER); |
| 95 | + intentFilter.addCategory(Intent.CATEGORY_DEFAULT); |
| 96 | + ActivityMonitor monitor = getInstrumentation().addMonitor(intentFilter, null, true); |
| 97 | + |
| 98 | + getTestModule().showShareDialog(content, options); |
| 99 | + |
| 100 | + waitForBridgeAndUIIdle(); |
| 101 | + getInstrumentation().waitForIdleSync(); |
| 102 | + |
| 103 | + assertEquals(1, monitor.getHits()); |
| 104 | + assertEquals(1, mRecordingModule.getOpened()); |
| 105 | + assertEquals(0, mRecordingModule.getErrors()); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments