Selecting an entry in Spinner

261 views
Skip to first unread message

sourc...@gmail.com

unread,
Nov 7, 2015, 8:04:01 AM11/7/15
to Espresso Framework
Hi,
I'm trying to select an option entry (of custom objects) from a Spinner with espresso UI testing. However do not seem to be able to get it to work in any way.

The code I use is shown below,
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout_root);

DropDownView dropDownView;
final ArrayAdapter<MyValue> adapter;

dropDownView = new DropDownView(this);
dropDownView.setId(R.id.dropdown_view);

dropDownView.setHint("title");

MyValueList list = new MyValueList();
list.addEntry("First", "1");
list.addEntry("Second", "2");
list.addEntry("Third", "3");
list.addEntry("Fourth", "4");
list.addEntry("Fifth", "5");
list.addEntry("Sixth", "6");
list.addEntry("Seven", "7");
list.addEntry("Eight", "8");
list.addEntry("Nine", "9");
list.addEntry("Ten", "10");
list.addEntry("Eleven", "11");
list.addEntry("Twelve", "12");
list.addEntry("Thirteen", "13");
list.addEntry("Fourteen", "14");
list.addEntry("Fifteen", "15");
list.addEntry("Sixteen", "16");
list.addEntry("Seventeen", "17");
list.addEntry("Eighteen", "18");
list.addEntry("Nineteen", "19");
list.addEntry("Twenty", "20");
list.addEntry("twentyone", "21");

adapter = new ArrayAdapter<>(this,
R.layout.dropdown_selected_item,
list.getList());
adapter.setDropDownViewResource(R.layout.dropdown_item);
dropDownView.setAdapter(adapter);

dropDownView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("MySpinnerTest", "current = " + adapter.getItem(position));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

linearLayout.addView(dropDownView);
}

public class MyValue {
String name;
String code;

public MyValue(String name, String code) {
this.name = name;
this.code = code;
}

@Override
public String toString() {
return name;
}
}

public class MyValueList {
List<MyValue> myValueList;
public MyValueList() {
myValueList = new ArrayList<>();
}
public void addEntry(String name, String code) {
myValueList.add(new MyValue(name, code));
}
public ArrayList<MyValue> getList() {
return (ArrayList<MyValue>) myValueList;
}
}

}

where DropDownView is a compound view with a textview (for title) and a spinner,
public class DropDownView extends LinearLayoutCompat {

private TextView mTitleTextView;
private Spinner mSpinnerView;

public DropDownView(Context context) {
this(context, null, 0);
}
public DropDownView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DropDownView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

setOrientation(VERTICAL);

mTitleTextView = new TextView(context);
mTitleTextView.setId(R.id.dropdown_title_view);
mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
mTitleTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSpinnerView.performClick();
}
});

mSpinnerView = new Spinner(context);
mSpinnerView.setId(R.id.dropdown_spinner_view);

addView(mTitleTextView);
addView(mSpinnerView);
}
public void setHint(CharSequence hint) {
mTitleTextView.setText(hint);
}
public void setAdapter(SpinnerAdapter adapter) {
mSpinnerView.setAdapter(adapter);
}
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener l) {
mSpinnerView.setOnItemSelectedListener(l);
}
}

I have tried with the below but get an Exception ("android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'is assignable from class: class android.widget.AdapterView'.")
onView(withId(R.id.dropdown_view)).perform(click());
onData(withSpinnerText("Twenty")).perform(click());

Could anyone please help with how I could click on the entry (Twenty) in this list? Any help appreciated.
Reply all
Reply to author
Forward
0 new messages