title: Handling options populated by AutoCompleteTextView using espresso
body: <p>I am trying to write espresso tests for an android app and need to know how to automate the section that uses <strong>AutoCompleteTextView</strong>. The code my application uses is mentioned below:</p>
  
  <p><strong><em>activity_main.xml</em></strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  
  &lt;TextView
      android:id="@+id/textView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginTop="15dp"
      android:text="Name a fruit from (Apple Banana Cherry Date Grape Kiwi Mango Pear)" /&gt;
  
  &lt;AutoCompleteTextView
      android:id="@+id/autoCompleteTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/textView"
      android:layout_marginLeft="36dp"
      android:layout_marginTop="17dp"
      android:ems="10"
      android:text=""&gt;
  
      &lt;requestFocus /&gt;
  &lt;/AutoCompleteTextView&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong><em>MainActivity.java</em></strong></p>
  
  <pre><code>import android.app.Activity;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.widget.ArrayAdapter;
  import android.widget.AutoCompleteTextView;
  
  public class MainActivity extends Activity {
  String[] fruits = {"Apple", "Apples2", "Apples3", "Date", "Grape", "Kiwi", "Mango", "Pear"};
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      //Creating the instance of ArrayAdapter containing list of fruit names
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;
              (this, android.R.layout.select_dialog_item, fruits);
      //Getting the instance of AutoCompleteTextView
      AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
      actv.setThreshold(1);//will start working from first character
      actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
      actv.setTextColor(Color.RED);
  
  }
  }
  </code></pre>
  
  <p>I have tried a few solutions with no luck. I was able to fetch the count using the following code</p>
  
  <pre><code>public class AutoCompleteTextViewItemCountAssertion implements ViewAssertion {
  private final int expectedCount;
  
  public AutoCompleteTextViewItemCountAssertion(int expectedCount) {
      this.expectedCount = expectedCount;
  }
  
  @Override
  public void check(View view, NoMatchingViewException noViewFoundException) {
      if (noViewFoundException != null) {
          throw noViewFoundException;
      }
      AutoCompleteTextView tv = (AutoCompleteTextView) view;
      assertThat(tv.getAdapter().getCount(),is(expectedCount));
  }
  }
  </code></pre>
  
  <p>However, I want to be able to click a particular option as well as get text for option at various positions</p>
  
  <p>Tried Following solutions as well, but did not help:</p>
  
  <pre><code>onData(equalTo("ITEM")).inRoot(RootMatchers.isPlatformPopup()).perform(click());
  onData(instanceOf("Whatever your arrayadapter contains".class)).inRoot(RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).perform(ViewActions.click());
  onData(anything()).atPosition(1).perform(click());
  </code></pre>