title: Android UI testing with Espresso on an AlertDialog inner views
body: <p>I'm testing a click on an AlertDialog's inner ListView using Espresso but keep getting NoMatchingViewException. The Dialog has a ListView that reads the accounts in the device.</p>
  
  <pre><code> AlertDialog.Builder builder = new AlertDialog.Builder(activity_reference);
      builder.setTitle(R.string.main_dialog_title);
      ListView lv = new ListView(activity_reference);
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;
              (activity_reference, android.R.layout.simple_list_item_1, android.R.id.text1,
                      gUsernameList);
      lv.setAdapter(adapter);
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                Toast.makeText(activity_reference, "You selected :" + gUsernameList.get(position), Toast.LENGTH_LONG).show();
                account_selected = gUsernameList.get(position);
          }
        }
      );
  
      builder.setView(lv);
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
              {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      if (account_selected == null) {
                          return;
                      }
                      dialog.dismiss();
                      Intent myIntent = new Intent(activity_reference, GraphActivity.class);
                      myIntent.putExtra("account_selected", account_selected); //Optional parameters
                      activity_reference.startActivity(myIntent);
                  }
              }
      );
      final Dialog dialog = builder.create();
      dialog.show();
  </code></pre>
  
  <p>And in the test</p>
  
  <pre><code>onView(withText(R.string.test_account)).inRoot(isDialog()).check(matches(isDisplayed()));
  </code></pre>
  
  <p>Am I doing anything wrong ?
  Thanks beforehand.</p>
  
  <p>Edit : I tried using UIAutomator too but gets UIObjectNotFoundException, although it is clearly there in the uiautomatorviewer.</p>