title: How to test ActionMenuItemView's icon in Espresso
body: <p>I have a button in action bar, for which the icon is changed depending of a boolean. I would like to check which drawable resource is used.</p>
  
  <p>Here is the code where the icon is changed:</p>
  
  <pre><code>@Override
  public void onPrepareOptionsMenu(Menu menu) {
      super.onPrepareOptionsMenu(menu);
      MenuItem item = menu.findItem(R.id.menu_favorite);
      if(mIsFavorite)
          item.setIcon(R.drawable.ab_icon_on);
      else
          item.setIcon(R.drawable.ab_icon_off);
  }
  </code></pre>
  
  <p>When icon needs to be changed, the menu is invalidated:</p>
  
  <pre><code>// request menu update
  supportInvalidateOptionsMenu();
  </code></pre>
  
  <p>Finally, my espresso code where I would like to check the result:</p>
  
  <pre><code>@Test
  public void action_setUnsetFavorite() {
      // check favorite off
      onView(withImageDrawable(R.drawable.ab_icon_off))
              .check(matches(isDisplayed()));
  
      // click favorite button
      onView(withId(R.id.menu_favorite))
              .perform(click());
  
      // check favorite on
      onView(withImageDrawable(R.drawable.ab_icon_on))
              .check(matches(isDisplayed()));
  </code></pre>
  
  <p>Please note that I am using a custom matcher found <a href="https://gist.github.com/frankiesardo/7490059" rel="nofollow">here</a>.</p>