Skip to content

Commit

Permalink
[MaterialButtonToggleGroup][a11y] Update accessibility class name
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 504557810
  • Loading branch information
paulfthomas authored and raajkumars committed Jan 26, 2023
1 parent 2cef555 commit 8656683
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/java/com/google/android/material/button/MaterialButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatButton;
import android.text.Layout.Alignment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
Expand Down Expand Up @@ -204,6 +205,7 @@ interface OnPressedChangeListener {
@Nullable private Mode iconTintMode;
@Nullable private ColorStateList iconTint;
@Nullable private Drawable icon;
@Nullable private String accessibilityClassName;

@Px private int iconSize;
@Px private int iconLeft;
Expand Down Expand Up @@ -257,11 +259,18 @@ public MaterialButton(@NonNull Context context, @Nullable AttributeSet attrs, in
}

@NonNull
private String getA11yClassName() {
String getA11yClassName() {
if (!TextUtils.isEmpty(accessibilityClassName)) {
return accessibilityClassName;
}
// Use the platform widget classes so Talkback can recognize this as a button.
return (isCheckable() ? CompoundButton.class : Button.class).getName();
}

void setA11yClassName(@Nullable String className) {
accessibilityClassName = className;
}

@Override
public void onInitializeAccessibilityNodeInfo(@NonNull AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.ToggleButton;
import androidx.annotation.BoolRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -448,6 +450,15 @@ public void setSingleSelection(boolean singleSelection) {
this.singleSelection = singleSelection;
clearChecked();
}
updateChildrenA11yClassName();
}

private void updateChildrenA11yClassName() {
for (int i = 0; i < getChildCount(); i++) {
String className =
singleSelection ? RadioButton.class.getName() : ToggleButton.class.getName();
getChildButton(i).setA11yClassName(className);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View.MeasureSpec;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
Expand Down Expand Up @@ -182,6 +185,30 @@ public void setToggleCheckedStateOnClick() {
assertThat(materialButton.isToggleCheckedStateOnClick()).isFalse();
}

@Test
public void getA11yClassName_whenCheckable() {
MaterialButton materialButton = new MaterialButton(context);
materialButton.setCheckable(true);

assertThat(materialButton.getA11yClassName()).isEqualTo(CompoundButton.class.getName());
}

@Test
public void getA11yClassName_whenNotCheckable() {
MaterialButton materialButton = new MaterialButton(context);
materialButton.setCheckable(false);

assertThat(materialButton.getA11yClassName()).isEqualTo(Button.class.getName());
}

@Test
public void getA11yClassName_whenSetToRadioButton() {
MaterialButton materialButton = new MaterialButton(context);
materialButton.setA11yClassName(RadioButton.class.getName());

assertThat(materialButton.getA11yClassName()).isEqualTo(RadioButton.class.getName());
}

@Test
@Config(minSdk = 23, maxSdk = 28)
public void setIcon_iconNotUpdated_whenPositionChanged() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import android.view.View;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.ToggleButton;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat;
Expand Down Expand Up @@ -347,4 +349,22 @@ public void setEnable_true_enablesChildButtons() {
assertThat(middleChild.isEnabled()).isTrue();
assertThat(lastChild.isEnabled()).isTrue();
}

@Test
public void singleSelection_hasRadioButtonA11yClassName() {
toggleGroup.setSingleSelection(true);
View button1 = toggleGroup.getChildAt(0);

assertThat(((MaterialButton) button1).getA11yClassName())
.isEqualTo(RadioButton.class.getName());
}

@Test
public void multiSelection_hasToggleButtonA11yClassName() {
toggleGroup.setSingleSelection(false);
View button1 = toggleGroup.getChildAt(0);

assertThat(((MaterialButton) button1).getA11yClassName())
.isEqualTo(ToggleButton.class.getName());
}
}

0 comments on commit 8656683

Please sign in to comment.