使用自定义arrayAdapter和Espresso进行仪器测试微调器

时间:2016-12-05 12:51:35

标签: java android android-espresso

所以我正在尝试为一个包含一些微调器的活动编写一些仪器测试。这些微调器使用自定义arrayAdapter,因此我可以将它们插入某些模型类的ArrayLists中。

一个微调器的适配器:

/**
* Custom spinner adapter for car selection.
*
* Source: http://stackoverflow.com/questions/1625249/android-how-to-bind-spinner-to-custom-object-list
*/

public class MyCarSpinnerAdapter extends ArrayAdapter<Car> {

// Your sent context
private Context context;
// Your custom values for the spinner (User)
private ArrayList<Car> values;

public MyCarSpinnerAdapter(
        Context context,
        int textViewResourceId,
        ArrayList<Car> values
) {
    super(context, textViewResourceId, values);
    this.context = context;
    this.values = values;
}

public int getCount(){
    return values.size();
}

public Car getItem(int position){
    return values.get(position);
}

// And the "magic" goes here
// This is for the "passive" state of the spinner
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // I created a dynamic TextView here, but you can reference
    // your own custom layout for each spinner item
    TextView label = new TextView(this.context);
    label.setTextColor(Color.BLACK);
    // Then you can get the current item using the values array
    // (Users array) and the current position. You can NOW reference
    // each method you has created in your bean object (User class)
    label.setText(
            values.get(position).getManufacturer()
                    + " "
                    + this.values.get(position).getModel()
    );

    // And finally return your dynamic (or custom) view for each spinner item
    return label;
}

// And here is when the "chooser" is popped up
// Normally is the same view, but you can customize it if you want
@Override
public View getDropDownView(int position, View convertView,
                            ViewGroup parent) {
    TextView label = new TextView(context);
    label.setTextColor(Color.BLACK);
    label.setText(
            this.values.get(position).getManufacturer()
                    + " "
                    + this.values.get(position).getModel()
    );

    return label;
}

@Override
public final String toString() {
    return "this is the my car spinner adapter string";
}
}

测试:

/**
 * Initial values testing.
 */
@Test
public void initialSettingsTest() {
    ViewInteraction carSpinner = onView(withId(R.id.MyCarActivity_ManufacturerSpinner));
    ViewInteraction batSpinner = onView(withId(R.id.MyCarActivity_BatterySpinner));
    ViewInteraction tireSpinner = onView(withId(R.id.MyCarActivity_TireSpinner));

    carSpinner.check(matches(withSpinnerText("Ford Magic")));
    batSpinner.check(matches(withSpinnerText("10.0 kwh")));
    tireSpinner.check(matches(withSpinnerText("1.0 roll")));
}

结果:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Ford Magic"' doesn't match the selected view.
Expected: with text: is "Ford Magic"
Got: "AppCompatSpinner{id=2131492994, res-name=MyCarActivity_ManufacturerSpinner, visibility=VISIBLE, width=618, height=72, 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=462.0, y=147.0, child-count=1}"

造成这种情况的原因是什么?我怎样才能测试微调器中的值是否符合我的期望值?

0 个答案:

没有答案