0

Following the answer here, I try to check whether a certain spinner text is selected. The spinner appears in a dialog, so I tried:

onView(withId(R.id.package_spinner)).inRoot(isDialog()).check(matches(withSpinnerText(containsString("sachet"))));

However this does not work and I get the following error message:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: a string containing "sachet"' doesn't match the selected view. Expected: with text: a string containing "sachet" Got: "AppCompatSpinner{id=2131624039, res-name=package_spinner, visibility=VISIBLE, width=620, height=75, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=75.0, child-count=1}"

Does "is-selected=false" mean, that the spinner it found is not selected? This is running on a real device (not emulator) with API 18. During the Espresso run and also when testing manually, the spinner is correctly set to "sachet". Why does Espresso have a problem with it?

Not sure whether this is relevant, but the spinner is for objects of type:

public class PackageType {
    private int id;
    private String name;

    private final Context ctx;

    public PackageType(Context context) { this.ctx=context; }
    public PackageType(String name, Context context) {
        super();
        this.ctx = context;
        setName(name);
    }

    // setters
    public void setId(int i) { this.id = i; }
    public void setName(String u) {
        this.name = u;
    }


    // getters
    public int getId() { return id; }
    public String getName() { return name; }
}

and the spinner adapter looks like:

class SpinnerPackageTypeAdapter extends ArrayAdapter<PackageType> {
    private final List<PackageType> packageTypes;
    private final Context mContext;

    public SpinnerPackageTypeAdapter(Context context, int resource, List<PackageType> packageTypes) {
        super(context, resource, packageTypes);
        this.mContext = context;
        this.packageTypes = packageTypes;
    }

    public PackageType getItem(int position) { return packageTypes.get(position); }

    public long getItemId(int position) { return position; }

    // this is for the passive state of the spinner
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // use dynamically created TextView, but could reference custom layout
        TextView label = new TextView(mContext);
        label.setTextColor(Color.BLACK);
        label.setTextSize(mContext.getResources().getDimension(R.dimen.list_row_font_size));
        label.setGravity(Gravity.CENTER);
        label.setText(getItem(position).getName());

        return label;
    }

    // this is for the chooser dropped down spinner
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView label = (TextView) View.inflate(mContext,R.layout.row_spinner,null);
        label.setText(getItem(position).getName());
        return label;
    }

}
Community
  • 1
  • 1
user1583209
  • 1,637
  • 3
  • 24
  • 42

1 Answers1

0

I found the problem. In class PackageType I had to override the toString() method as (of course other versions are possible as long as "name" is in there):

@Override
    public String toString() {
        return "PackageType [id=" + id + ", name=" + name + "]";
    }
user1583209
  • 1,637
  • 3
  • 24
  • 42