Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this by writing a special activity whose only purpose is to start the activity you are testing for result and save the result for you to assert correctness on.</p> <p>For example, you could create an activity named <code>ResultReceiverActivity</code>. Give it three methods: <code>getResultCode</code>, <code>getResultData</code>, and <code>getReceivedRequestCode</code>, which can be used to verify that the tested activity returned the right values. You would create a test case that extends <code>ActivityInstrumentationTestCase2</code> and the generic parameter would be <code>ResultReceiverActivity</code>. Calling <code>getActivity</code> will get you the activity instance. </p> <pre><code>public class ReturnedResultTest extends ActivityInstrumentationTestCase2&lt;ResultReceiverActivity&gt; { public void testReturnedResult() { ResultReceiverActivity a = getActivity(); assertEquals(Activity.RESULT_OK, a.getResultCode()); assertEquals("myResult", a.getResultData().getStringExtra("test")); assertEquals(0x9999, a.getReceivedRequestCode()); } } </code></pre> <p><code>ResultReceiverActivity</code> needs to override <code>onActivityResult</code>, of course, and should just store the values of that methods parameter in its fields, like so:</p> <pre><code>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); this.receivedRequestCode = requestCode; this.resultCode = resultCode; this.resultData = data; } </code></pre> <p>Of course, you may want to customize the activity that <code>ResultReceiverActivity</code> starts, and you can easily do that by using <code>getIntent</code> in its <code>onCreate</code> method. In your test case, call setActivityIntent before calling getActivity to set which Intent is used to start the activity.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload