DialogModule extends ReactContextBaseJavaModule implements LifecycleEventListener { @ReactMethod public void showAlert( ReadableMap options, Callback errorCallback, Callback actionCallback) { FragmentManagerHelper fragmentManagerHelper = getFragmentManagerHelper(); if (fragmentManagerHelper == null) { errorCallback.invoke("Tried to show an alert while not attached to an Activity"); return; } final Bundle args = new Bundle(); if (options.hasKey(KEY_TITLE)) { args.putString(AlertFragment.ARG_TITLE, options.getString(KEY_TITLE)); } if (options.hasKey(KEY_MESSAGE)) { args.putString(AlertFragment.ARG_MESSAGE, options.getString(KEY_MESSAGE)); } if (options.hasKey(KEY_BUTTON_POSITIVE)) { args.putString(AlertFragment.ARG_BUTTON_POSITIVE, options.getString(KEY_BUTTON_POSITIVE)); } if (options.hasKey(KEY_BUTTON_NEGATIVE)) { args.putString(AlertFragment.ARG_BUTTON_NEGATIVE, options.getString(KEY_BUTTON_NEGATIVE)); } if (options.hasKey(KEY_BUTTON_NEUTRAL)) { args.putString(AlertFragment.ARG_BUTTON_NEUTRAL, options.getString(KEY_BUTTON_NEUTRAL)); } if (options.hasKey(KEY_ITEMS)) { ReadableArray items = options.getArray(KEY_ITEMS); CharSequence[] itemsArray = new CharSequence[items.size()]; for (int i = 0; i < items.size(); i ++) { itemsArray[i] = items.getString(i); } args.putCharSequenceArray(AlertFragment.ARG_ITEMS, itemsArray); } fragmentManagerHelper.showNewAlert(mIsInForeground, args, actionCallback); } DialogModule(ReactApplicationContext reactContext); @Override String getName(); @Override Map<String, Object> getConstants(); @Override void initialize(); @Override void onHostPause(); @Override void onHostDestroy(); @Override void onHostResume(); @ReactMethod void showAlert(
      ReadableMap options,
      Callback errorCallback,
      Callback actionCallback);  }
@Test public void testCallbackPositive() { final JavaOnlyMap options = new JavaOnlyMap(); options.putString("buttonPositive", "OK"); final SimpleCallback actionCallback = new SimpleCallback(); mDialogModule.showAlert(options, null, actionCallback); final AlertDialog dialog = (AlertDialog) getFragment().getDialog(); dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertEquals(1, actionCallback.getCalls()); assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.getArgs()[0]); assertEquals(DialogInterface.BUTTON_POSITIVE, actionCallback.getArgs()[1]); }
@Test public void testCallbackNegative() { final JavaOnlyMap options = new JavaOnlyMap(); options.putString("buttonNegative", "Cancel"); final SimpleCallback actionCallback = new SimpleCallback(); mDialogModule.showAlert(options, null, actionCallback); final AlertDialog dialog = (AlertDialog) getFragment().getDialog(); dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); assertEquals(1, actionCallback.getCalls()); assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.getArgs()[0]); assertEquals(DialogInterface.BUTTON_NEGATIVE, actionCallback.getArgs()[1]); }
@Test public void testCallbackNeutral() { final JavaOnlyMap options = new JavaOnlyMap(); options.putString("buttonNeutral", "Later"); final SimpleCallback actionCallback = new SimpleCallback(); mDialogModule.showAlert(options, null, actionCallback); final AlertDialog dialog = (AlertDialog) getFragment().getDialog(); dialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick(); assertEquals(1, actionCallback.getCalls()); assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.getArgs()[0]); assertEquals(DialogInterface.BUTTON_NEUTRAL, actionCallback.getArgs()[1]); }
@Test public void testCallbackDismiss() { final JavaOnlyMap options = new JavaOnlyMap(); final SimpleCallback actionCallback = new SimpleCallback(); mDialogModule.showAlert(options, null, actionCallback); getFragment().getDialog().dismiss(); assertEquals(1, actionCallback.getCalls()); assertEquals(DialogModule.ACTION_DISMISSED, actionCallback.getArgs()[0]); }
@Test public void testAllOptions() { final JavaOnlyMap options = new JavaOnlyMap(); options.putString("title", "Title"); options.putString("message", "Message"); options.putString("buttonPositive", "OK"); options.putString("buttonNegative", "Cancel"); options.putString("buttonNeutral", "Later"); mDialogModule.showAlert(options, null, null); final AlertFragment fragment = getFragment(); assertNotNull("Fragment was not displayed", fragment); final AlertDialog dialog = (AlertDialog) fragment.getDialog(); assertEquals("OK", dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText().toString()); assertEquals("Cancel", dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText().toString()); assertEquals("Later", dialog.getButton(DialogInterface.BUTTON_NEUTRAL).getText().toString()); }