|
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,39 @@ 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(new OnCheckedChangeListener() { |
| 208 | + @Override |
| 209 | + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 210 | + onChipCheckedStateChanged(buttonView, isChecked); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + Chip second = (Chip) chipgroup.getChildAt(1); |
| 215 | + second.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 216 | + @Override |
| 217 | + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 218 | + onChipCheckedStateChanged(buttonView, isChecked); |
| 219 | + } |
| 220 | + }); |
| 221 | + |
| 222 | + first.performClick(); |
| 223 | + getInstrumentation().waitForIdleSync(); |
| 224 | + |
| 225 | + assertThat(checkedChangeCallCount).isEqualTo(1); |
| 226 | + assertThat(checkedIds).containsExactly(first.getId()); |
| 227 | + |
| 228 | + second.performClick(); |
| 229 | + getInstrumentation().waitForIdleSync(); |
| 230 | + |
| 231 | + assertThat(checkedChangeCallCount).isEqualTo(2); |
| 232 | + assertThat(checkedIds).containsExactly(first.getId(), second.getId()); |
| 233 | + } |
| 234 | + |
199 | 235 | @Test
|
200 | 236 | public void multiSelection_withSelectionRequired_unSelectsIfTwo() {
|
201 | 237 | chipgroup.setSingleSelection(false);
|
@@ -260,4 +296,9 @@ public void isNotSingleLine_initializesAccessibilityNodeInfo() {
|
260 | 296 | assertEquals(1, itemInfo.getRowIndex());
|
261 | 297 | assertTrue(itemInfo.isSelected());
|
262 | 298 | }
|
| 299 | + |
| 300 | + private void onChipCheckedStateChanged(CompoundButton chip, boolean checked) { |
| 301 | + checkedChangeCallCount++; |
| 302 | + checkedIds = chipgroup.getCheckedChipIds(); |
| 303 | + } |
263 | 304 | }
|
0 commit comments