title: Check if a custom progress DialogFragment is shown or not using Espresso?
body: <p>I have a custom progress dialog with a progressbar and a message displayed during network calls.(For e.g. Logging in..., Fetching data etc., ).</p>
  
  <p>I want to write a test to verify the dialogfragment with given text is displayed or not.</p>
  
  <p><strong>CustomProgressDialog.java</strong></p>
  
  <pre><code>public class CustomProgressDialog extends DialogFragment {
  
    private static final String KEY_MESSAGE = "message";
    public static final String TAG_PROGRESS_DIALOG = "progress_dialog";
  
    @BindView(R.id.progressbar) ProgressBar mProgressBar;
    @BindView(R.id.progress_textview) TextView mProgressTextView;
  
    private Unbinder mUnbinder;
  
    public static CustomProgressDialog start(String progressMessage) {
      CustomProgressDialog dialog = new CustomProgressDialog();
  
      Bundle bundle = new Bundle();
      bundle.putString(KEY_MESSAGE, progressMessage);
      dialog.setArguments(bundle);
      return dialog;
    }
  
    @Override public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setRetainInstance(true);
      setCancelable(false);
    }
  
    @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
      LayoutInflater inflater = getActivity().getLayoutInflater();
  
      Bundle bundle = getArguments();
      String mProgressMessage;
      if (bundle != null &amp;&amp; bundle.containsKey(KEY_MESSAGE)) {
        mProgressMessage = bundle.getString(KEY_MESSAGE);
      } else {
        mProgressMessage = getActivity().getString(R.string.progress_loading);
      }
  
      View view = inflater.inflate(R.layout.dialog_custom_progress, null);
      mUnbinder = ButterKnife.bind(this, view);
      mProgressTextView.setText(mProgressMessage);
  
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      builder.setView(view);
  
      return builder.create();
    }
  
  
    /**
     * Helper method to show a progress dialog with a message.
     */
    public static void showDialog(FragmentActivity activity, String message) {
      showDialog(activity, message, TAG_PROGRESS_DIALOG);
    }
  
    /**
     * Helper method to show a progress dialog with a message.
     */
    public static void showDialog(FragmentActivity activity, String message, String tag) {
      CustomProgressDialog progressDialog = CustomProgressDialog.start(message);
      progressDialog.show(activity.getSupportFragmentManager(), tag);
    }
  
    public static void hideDialog(FragmentActivity activity) {
      hideDialog(activity, TAG_PROGRESS_DIALOG);
    }
  
    /**
     * Helper method to hide a progress dialog.
     */
    public static void hideDialog(FragmentActivity activity, String tag) {
      FragmentManager fragmentManager = activity.getSupportFragmentManager();
      DialogFragment dialogFragment = (DialogFragment) fragmentManager.findFragmentByTag(tag);
      if (dialogFragment != null) {
        Timber.d("Gonna dismiss the dialog.");
        dialogFragment.dismiss();
      }
    }
  
    @Override public void onDestroyView() {
      if (getDialog() != null &amp;&amp; getRetainInstance()) {
        getDialog().setDismissMessage(null);
        mUnbinder.unbind();
      }
      super.onDestroyView();
    }
  }
  </code></pre>
  
  <p><strong>dialog_custom_progress.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/root_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/white"
      android:layout_marginTop="@dimen/spacing_small"
      android:layout_marginBottom="@dimen/spacing_small"
      android:padding="@dimen/spacing_xlarge"
      &gt;
  
    &lt;ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:indeterminate="true"
        android:indeterminateTint="@color/primary_dark"
        android:indeterminateTintMode="src_in"
        /&gt;
  
    &lt;TextView
        android:id="@+id/progress_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="left"
        android:layout_marginLeft="@dimen/spacing_medium"
        android:layout_toRightOf="@+id/progressbar"
        android:maxLines="3"
        tools:text="@string/placeholder_progress_text"
        style="@style/ProgressTextStyle"
        /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>LoginFragment.java</strong></p>
  
  <pre><code>private void doLogin() {
    //blah.. blah ..
    CustomProgressDialog.showDialog(getActivity(),
          getActivity().getString(R.string.progress_logging_in));
  }
  </code></pre>
  
  <p><strong>Sample testcase</strong></p>
  
  <pre><code> @Test public void checkProgressBar_displayedWhileLoggingIn() throws Exception {
      // GIVEN
      ...... 
  
      // WHEN
      onView(LOGIN_BUTTON).perform(click());
  
      // THEN
      // TODO check for progress bar is displayed.
      //onView(withText(R.string.progress_logging_in)).check(matches(isDisplayed()));                
      onView(withId(R.id.progress_textview)).check(matches(isDisplayed()));
  
    }
  </code></pre>
  
  <p>I'd got the following error for the above testcase:</p>
  
  <pre><code>android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.custom.android.internal.debug:id/progress_textview
  </code></pre>
  
  <p>Although <code>progress_textview</code> is displayed on the screen.</p>