### Question:
ShareModule extends ReactContextBaseJavaModule { @ReactMethod public void share(ReadableMap content, String dialogTitle, Promise promise) { if (content == null) { promise.reject(ERROR_INVALID_CONTENT, "Content cannot be null"); return; } try { Intent intent = new Intent(Intent.ACTION_SEND); intent.setTypeAndNormalize("text/plain"); if (content.hasKey("title")) { intent.putExtra(Intent.EXTRA_SUBJECT, content.getString("title")); } if (content.hasKey("message")) { intent.putExtra(Intent.EXTRA_TEXT, content.getString("message")); } Intent chooser = Intent.createChooser(intent, dialogTitle); chooser.addCategory(Intent.CATEGORY_DEFAULT); Activity currentActivity = getCurrentActivity(); if (currentActivity != null) { currentActivity.startActivity(chooser); } else { getReactApplicationContext().startActivity(chooser); } WritableMap result = Arguments.createMap(); result.putString("action", ACTION_SHARED); promise.resolve(result); } catch (Exception e) { promise.reject(ERROR_UNABLE_TO_OPEN_DIALOG, "Failed to open share dialog"); } } ShareModule(ReactApplicationContext reactContext); @Override String getName(); @ReactMethod void share(ReadableMap content, String dialogTitle, Promise promise);  }### Answer:
@Test public void testShareDialog() { final String title = "Title"; final String message = "Message"; final String dialogTitle = "Dialog Title"; JavaOnlyMap content = new JavaOnlyMap(); content.putString("title", title); content.putString("message", message); final SimplePromise promise = new SimplePromise(); mShareModule.share(content, dialogTitle, promise); final Intent chooserIntent = ((ShadowApplication) ShadowExtractor.extract(RuntimeEnvironment.application)).getNextStartedActivity(); assertNotNull("Dialog was not displayed", chooserIntent); assertEquals(Intent.ACTION_CHOOSER, chooserIntent.getAction()); assertEquals( dialogTitle, chooserIntent .getExtras() .get(Intent.EXTRA_TITLE) ); final Intent contentIntent = (Intent) chooserIntent .getExtras() .get(Intent.EXTRA_INTENT); assertNotNull("Intent was not built correctly", contentIntent); assertEquals(Intent.ACTION_SEND, contentIntent.getAction()); assertEquals( title, contentIntent .getExtras() .get(Intent.EXTRA_SUBJECT) ); assertEquals( message, contentIntent .getExtras() .get(Intent.EXTRA_TEXT) ); assertEquals(1, promise.getResolved()); }
@Test public void testShareDialog() { final String title = "Title"; final String message = "Message"; final String dialogTitle = "Dialog Title"; JavaOnlyMap content = new JavaOnlyMap(); content.putString("title", title); content.putString("message", message); final SimplePromise promise = new SimplePromise(); mShareModule.share(content, dialogTitle, promise); final Intent chooserIntent = ((ShadowApplication)ShadowExtractor.extract(RuntimeEnvironment.application)).getNextStartedActivity(); assertNotNull("Dialog was not displayed", chooserIntent); assertEquals(Intent.ACTION_CHOOSER, chooserIntent.getAction()); assertEquals(dialogTitle, chooserIntent.getExtras().get(Intent.EXTRA_TITLE)); final Intent contentIntent = (Intent)chooserIntent.getExtras().get(Intent.EXTRA_INTENT); assertNotNull("Intent was not built correctly", contentIntent); assertEquals(Intent.ACTION_SEND, contentIntent.getAction()); assertEquals(title, contentIntent.getExtras().get(Intent.EXTRA_SUBJECT)); assertEquals(message, contentIntent.getExtras().get(Intent.EXTRA_TEXT)); assertEquals(1, promise.getResolved()); }
@Test public void testInvalidContent() { final String dialogTitle = "Dialog Title"; final SimplePromise promise = new SimplePromise(); mShareModule.share(null, dialogTitle, promise); assertEquals(1, promise.getRejected()); assertEquals(ShareModule.ERROR_INVALID_CONTENT, promise.getErrorCode()); }