SingleChoiceOptionItem extends OptionItem implements AdapterView.OnItemSelectedListener { public SingleChoiceOptionItem setLabels(final String title, final List<String> labels) { if (labels == null) { throw new IllegalArgumentException(getContext().getString(R.string.msdkui_exception_labels_null)); } View.inflate(getContext(), R.layout.single_option_item, this); final TextView titleView = (TextView) findViewById(R.id.single_item_label); if (title == null) { titleView.setVisibility(GONE); } else { titleView.setText(title); } mTitle = title; mLabels = labels; mSpinner = (Spinner) findViewById(R.id.single_item_value); mSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_row_item, labels)); mSpinner.setOnItemSelectedListener(this); return this; } SingleChoiceOptionItem(final Context context);  SingleChoiceOptionItem(final Context context, final AttributeSet attrs);  SingleChoiceOptionItem(final Context context, final AttributeSet attrs, final int defStyleAttr); @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)  SingleChoiceOptionItem(final Context context, final AttributeSet attrs, final int defStyleAttr,
            final int defStyleRes); List<String> getLabels(); String getTitle(); SingleChoiceOptionItem setLabels(final String title, final List<String> labels); void selectLabel(final String label); void selectIndex(final int index); String getSelectedItemLabel(); @Override void onItemSelected(AdapterView<?> parent, View view, int position, long id); @Override void onNothingSelected(AdapterView<?> parent);  }
@Test public void testHideTitleWhenNullTitleProvided() { final List<String> labels = Collections.singletonList(getString(R.string.msdkui_fastest)); mSingleChoiceOptionItem.setLabels(null, labels); View titleView = mSingleChoiceOptionItem.getRootView().findViewById(R.id.single_item_label); assertNotNull(titleView); assertEquals(View.GONE, titleView.getVisibility()); }
@Test public void testCallback() { mSingleChoiceOptionItem.setListener(new OptionItem.OnChangedListener() { @Override public void onChanged(OptionItem item) { mCallbackCalled = true; } }); final List<String> ids = Arrays.asList(getString(R.string.msdkui_fastest), getString(R.string.msdkui_allow_car_pool)); mSingleChoiceOptionItem.setLabels(TITLE, ids); assertThat(mSingleChoiceOptionItem.getChildCount(), equalTo(1)); final View view = mSingleChoiceOptionItem.getChildAt(0); final TextView labelView = (TextView) view.findViewById(R.id.single_item_label); final Spinner selectionView = (Spinner) view.findViewById(R.id.single_item_value); assertNotNull(labelView); assertNotNull(selectionView); selectionView.setSelection(1); assertThat(mCallbackCalled, is(true)); }
@Test(expected = IllegalArgumentException.class) public void testSetNullLabelsException() { mSingleChoiceOptionItem.setLabels(TITLE, null); }