IsCheckedConstraint extends Constraint<T> { @NonNull @Override public Set<Violation> validate() throws UnsupportedException { final Set<Violation> violations = new HashSet<>(); final boolean isChecked; if (object instanceof RadioGroup) { RadioGroup radioGroup = (RadioGroup) object; isChecked = View.NO_ID != radioGroup.getCheckedRadioButtonId(); } else if (object instanceof CompoundButton) { CompoundButton radioButton = (CompoundButton) object; isChecked = radioButton.isChecked(); } else { throw new UnsupportedException(this, object, propertyName); } if (!isChecked) { violations.add(new Violation(propertyName, object, message, ERROR_CODE_IS_NOT_CHECKED)); } return violations; } IsCheckedConstraint(@NonNull T object, @NonNull String propertyName); @NonNull @Override Set<Violation> validate(); void setMessage(String message); static final String ERROR_CODE_IS_NOT_CHECKED; }
@SuppressWarnings("ResourceType") @Test public void isValid() throws Exception { RadioGroup radioGroup = new RadioGroup(RuntimeEnvironment.application); radioGroup.check(1); assertEmpty("is valid with RadioGroup", new IsCheckedConstraint<View>(radioGroup, "radioGroup").validate()); RadioButton radioButton = new RadioButton(RuntimeEnvironment.application); radioButton.setChecked(true); assertEmpty("is valid with RadioButton", new IsCheckedConstraint<View>(radioButton, "radioButton").validate()); }
@SuppressWarnings("ResourceType") @Test public void isInValid() throws Exception { RadioGroup radioGroup = new RadioGroup(RuntimeEnvironment.application); assertNotEmpty("is invalid with RadioGroup", new IsCheckedConstraint<View>(radioGroup, "radioGroup").validate()); RadioButton radioButton = new RadioButton(RuntimeEnvironment.application); assertNotEmpty("is invalid with RadioButton", new IsCheckedConstraint<View>(radioButton, "radioButton").validate()); }