@Test public void startActivity_shouldDelegateToStartActivityForResult() { TranscriptActivity activity = Robolectric.setupActivity(TranscriptActivity.class); activity.startActivity(new Intent().setType("image/*")); shadowOf(activity).receiveResult(new Intent().setType("image/*"), Activity.RESULT_OK, new Intent().setData(Uri.parse("content:foo"))); assertThat(activity.transcript) .containsExactly( "onActivityResult called with requestCode -1, resultCode -1, intent data content:foo"); }
@Test public void startActivityForResultAndReceiveResult_shouldSendResponsesBackToActivity() throws Exception { TranscriptActivity activity = Robolectric.setupActivity(TranscriptActivity.class); activity.startActivityForResult(new Intent().setType("audio/*"), 123); activity.startActivityForResult(new Intent().setType("image/*"), 456); shadowOf(activity).receiveResult(new Intent().setType("image/*"), Activity.RESULT_OK, new Intent().setData(Uri.parse("content:foo"))); assertThat(activity.transcript) .containsExactly( "onActivityResult called with requestCode 456, resultCode -1, intent data content:foo"); }
@Test public void startActivityForResultAndReceiveResult_whenNoIntentMatches_shouldThrowException() throws Exception { ThrowOnResultActivity activity = Robolectric.buildActivity(ThrowOnResultActivity.class).get(); activity.startActivityForResult(new Intent().setType("audio/*"), 123); activity.startActivityForResult(new Intent().setType("image/*"), 456); Intent requestIntent = new Intent().setType("video/*"); try { shadowOf(activity).receiveResult(requestIntent, Activity.RESULT_OK, new Intent().setData(Uri.parse("content:foo"))); fail(); } catch (Exception e) { assertThat(e.getMessage()).startsWith("No intent matches " + requestIntent); } }
/** * Initiate and complete a barcode capture, either in success * or in failure */ private void captureBarcodeWithResult(final Activity activity, final int buttonId, final boolean success) throws IOException { // Start image capture final Button captureButton = activity.findViewById(buttonId); captureButton.performClick(); ShadowActivity.IntentForResult intentForResult = shadowOf(activity).peekNextStartedActivityForResult(); assertNotNull(intentForResult); Intent intent = intentForResult.intent; assertNotNull(intent); String action = intent.getAction(); assertNotNull(action); assertEquals(Intents.Scan.ACTION, action); Bundle bundle = intent.getExtras(); assertNotNull(bundle); Intent resultIntent = new Intent(intent); Bundle resultBuddle = new Bundle(); resultBuddle.putString(Intents.Scan.RESULT, BARCODE_DATA); resultBuddle.putString(Intents.Scan.RESULT_FORMAT, BARCODE_TYPE); resultIntent.putExtras(resultBuddle); // Respond to image capture, success shadowOf(activity).receiveResult( intent, success ? Activity.RESULT_OK : Activity.RESULT_CANCELED, resultIntent); }
shadowOf(activity).receiveResult( intent, success ? Activity.RESULT_OK : Activity.RESULT_CANCELED,
shadowOf(activity).receiveResult( intent, Activity.RESULT_OK,
shadowOf(activity).receiveResult(intent,