Skip to content

Commit

Permalink
Revert "[ChipGroup] Fix ChipGroup.getCheckedChipIds() returns wrong s…
Browse files Browse the repository at this point in the history
…tate"

This reverts commit ba9803a.
  • Loading branch information
pekingme committed May 25, 2022
1 parent ba9803a commit bc9595b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 63 deletions.
26 changes: 7 additions & 19 deletions lib/java/com/google/android/material/chip/Chip.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import android.view.ViewParent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.CompoundButton;
import androidx.annotation.AnimatorRes;
import androidx.annotation.BoolRes;
import androidx.annotation.CallSuper;
Expand Down Expand Up @@ -152,7 +151,6 @@ public class Chip extends AppCompatCheckBox
@Nullable private RippleDrawable ripple;

@Nullable private OnClickListener onCloseIconClickListener;
@Nullable private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
@Nullable private MaterialCheckable.OnCheckedChangeListener<Chip> onCheckedChangeListenerInternal;
private boolean deferredCheckedValue;
private boolean closeIconPressed;
Expand Down Expand Up @@ -253,16 +251,6 @@ public Chip(Context context, AttributeSet attrs, int defStyleAttr) {
setMinHeight(minTouchTargetSize);
}
lastLayoutDirection = ViewCompat.getLayoutDirection(this);

super.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
if (onCheckedChangeListenerInternal != null) {
onCheckedChangeListenerInternal.onCheckedChanged(Chip.this, isChecked);
}
if (onCheckedChangeListener != null) {
onCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
}
});
}

@Override
Expand Down Expand Up @@ -719,15 +707,15 @@ public void setChecked(boolean checked) {
// Defer the setChecked() call until after initialization.
deferredCheckedValue = checked;
} else if (chipDrawable.isCheckable()) {
boolean wasChecked = isChecked();
super.setChecked(checked);
}
}

@Override
public void setOnCheckedChangeListener(
@Nullable CompoundButton.OnCheckedChangeListener listener) {
// Do not call super here - the wrapped listener set in the constructor will call the listener.
onCheckedChangeListener = listener;
if (wasChecked != checked) {
if (onCheckedChangeListenerInternal != null) {
onCheckedChangeListenerInternal.onCheckedChanged(this, checked);
}
}
}
}

/** Register a callback to be invoked when the close icon is clicked. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
*/
package com.google.android.material.chip;

import android.widget.CompoundButton.OnCheckedChangeListener;
import com.google.android.material.test.R;
import com.google.android.material.R;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat;
Expand Down Expand Up @@ -199,41 +196,6 @@ public void onCheckedChanged(ChipGroup group, List<Integer> checkedIds) {
assertThat(checkedIds).contains(second.getId());
}

@Test
public void multipleSelection_chipListener() {
chipgroup.setSingleSelection(false);

Chip first = (Chip) chipgroup.getChildAt(0);
first.setOnCheckedChangeListener(
new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChipCheckedStateChanged(buttonView, isChecked);
}
});

Chip second = (Chip) chipgroup.getChildAt(1);
second.setOnCheckedChangeListener(
new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChipCheckedStateChanged(buttonView, isChecked);
}
});

first.performClick();
getInstrumentation().waitForIdleSync();

assertThat(checkedChangeCallCount).isEqualTo(1);
assertThat(checkedIds).containsExactly(first.getId());

second.performClick();
getInstrumentation().waitForIdleSync();

assertThat(checkedChangeCallCount).isEqualTo(2);
assertThat(checkedIds).containsExactly(first.getId(), second.getId());
}

@Test
public void multiSelection_withSelectionRequired_unSelectsIfTwo() {
chipgroup.setSingleSelection(false);
Expand Down Expand Up @@ -298,9 +260,4 @@ public void isNotSingleLine_initializesAccessibilityNodeInfo() {
assertEquals(1, itemInfo.getRowIndex());
assertTrue(itemInfo.isSelected());
}

private void onChipCheckedStateChanged(CompoundButton chip, boolean checked) {
checkedChangeCallCount++;
checkedIds = chipgroup.getCheckedChipIds();
}
}

0 comments on commit bc9595b

Please sign in to comment.