|
15 | 15 | */ |
16 | 16 | package com.google.android.material.chip; |
17 | 17 |
|
18 | | -import com.google.android.material.R; |
| 18 | +import android.widget.CompoundButton.OnCheckedChangeListener; |
| 19 | +import com.google.android.material.test.R; |
19 | 20 |
|
| 21 | +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
20 | 22 | import static com.google.common.truth.Truth.assertThat; |
21 | 23 | import static org.junit.Assert.assertEquals; |
22 | 24 | import static org.junit.Assert.assertTrue; |
23 | 25 |
|
24 | 26 | import android.content.Context; |
25 | 27 | import androidx.appcompat.app.AppCompatActivity; |
26 | 28 | import android.view.View; |
| 29 | +import android.widget.CompoundButton; |
27 | 30 | import androidx.core.view.ViewCompat; |
28 | 31 | import androidx.core.view.accessibility.AccessibilityNodeInfoCompat; |
29 | 32 | import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat; |
@@ -196,6 +199,41 @@ public void onCheckedChanged(ChipGroup group, List<Integer> checkedIds) { |
196 | 199 | assertThat(checkedIds).contains(second.getId()); |
197 | 200 | } |
198 | 201 |
|
| 202 | + @Test |
| 203 | + public void multipleSelection_chipListener() { |
| 204 | + chipgroup.setSingleSelection(false); |
| 205 | + |
| 206 | + Chip first = (Chip) chipgroup.getChildAt(0); |
| 207 | + first.setOnCheckedChangeListener( |
| 208 | + new OnCheckedChangeListener() { |
| 209 | + @Override |
| 210 | + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 211 | + onChipCheckedStateChanged(buttonView, isChecked); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + Chip second = (Chip) chipgroup.getChildAt(1); |
| 216 | + second.setOnCheckedChangeListener( |
| 217 | + new OnCheckedChangeListener() { |
| 218 | + @Override |
| 219 | + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 220 | + onChipCheckedStateChanged(buttonView, isChecked); |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + first.performClick(); |
| 225 | + getInstrumentation().waitForIdleSync(); |
| 226 | + |
| 227 | + assertThat(checkedChangeCallCount).isEqualTo(1); |
| 228 | + assertThat(checkedIds).containsExactly(first.getId()); |
| 229 | + |
| 230 | + second.performClick(); |
| 231 | + getInstrumentation().waitForIdleSync(); |
| 232 | + |
| 233 | + assertThat(checkedChangeCallCount).isEqualTo(2); |
| 234 | + assertThat(checkedIds).containsExactly(first.getId(), second.getId()); |
| 235 | + } |
| 236 | + |
199 | 237 | @Test |
200 | 238 | public void multiSelection_withSelectionRequired_unSelectsIfTwo() { |
201 | 239 | chipgroup.setSingleSelection(false); |
@@ -260,4 +298,9 @@ public void isNotSingleLine_initializesAccessibilityNodeInfo() { |
260 | 298 | assertEquals(1, itemInfo.getRowIndex()); |
261 | 299 | assertTrue(itemInfo.isSelected()); |
262 | 300 | } |
| 301 | + |
| 302 | + private void onChipCheckedStateChanged(CompoundButton chip, boolean checked) { |
| 303 | + checkedChangeCallCount++; |
| 304 | + checkedIds = chipgroup.getCheckedChipIds(); |
| 305 | + } |
263 | 306 | } |
0 commit comments