title: How to stub Intent.createChooser Intent using Espresso
body: <p><strong>Problem</strong></p>
  
  <p>I have an image inside my app, and am sharing it to any other app that can handle image sharing, and the feature is working perectlty.</p>
  
  <p>I am writing an Espresso UI test to intercept the intent and ensure it has the correct action and extras, but cannot seem to get it to work.</p>
  
  <p><strong>Code</strong></p>
  
  <p>Here is the code when creating the intent:</p>
  
  <pre><code>Intent intent = new Intent(Intent.ACTION_SEND);
  intent.putExtra(Intent.EXTRA_STREAM, uri);
  intent.setType(MediaType.PNG.toString());
  startActivity(Intent.createChooser(intent, "send");
  </code></pre>
  
  <p>and here is my attempt to match the Intent in my test, but fails to find a match:</p>
  
  <pre><code>Intents.init();
  launchActivity(MyFragment.newIntent(getTargetContext());
  
  Matcher&lt;Intent&gt; expectedIntent = allOf(
      hasAction(Intent.ACTION_CHOOSER),
      hasExtra(
          Intent.ACTION_SEND,
          hasExtra(Intent.EXTRA_STREAM, EXPECTED_SHARE_URI) // Expected URI has been copied from the extras 'uriString' value when debugging
      )
  );
  intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null));
  MyScreen.clickShareButton(); // performs click on the share button
  intended(expectedIntent);
  Intents.release();
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <p><code>IntentMatcher: (has action: is "android.intent.action.CHOOSER" and has extras: has bundle with: key: is "android.intent.extra.STREAM" value: is "[my uri appears here]")</code></p>
  
  <p><strong>Additional info</strong></p>
  
  <p>When debugging, the intent that is created results in an intent with action "android.intent.action.CHOOSER", and has an extra of type Intent, with action "android.intent.action.SEND" and type "image/png", and in turn has an extra, a HierarchicalUri with a uriString.</p>
  
  <p><strong>Summary</strong></p>
  
  <p>Anyone know what I am doing wrong? I can't find a way to tie all this together and create a matcher for this intent. Any help would be greatly appreciated!</p>