1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.recyclerview.selection.testing; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import androidx.recyclerview.selection.SelectionTracker.SelectionObserver; 24 25 import org.jspecify.annotations.NonNull; 26 27 import java.util.HashSet; 28 import java.util.Set; 29 30 public class TestSelectionObserver<K> extends SelectionObserver<K> { 31 32 private final Set<K> mSelected = new HashSet<>(); 33 private boolean mSelectionChanged = false; 34 private boolean mSelectionCleared = false; 35 private boolean mSelectionReset = false; 36 private boolean mSelectionRestored = false; 37 reset()38 public void reset() { 39 mSelected.clear(); 40 mSelectionChanged = false; 41 mSelectionReset = false; 42 } 43 44 @Override onItemStateChanged(@onNull K key, boolean selected)45 public void onItemStateChanged(@NonNull K key, boolean selected) { 46 if (selected) { 47 mSelected.add(key); 48 } else { 49 mSelected.remove(key); 50 } 51 } 52 53 @Override onSelectionCleared()54 protected void onSelectionCleared() { 55 mSelectionCleared = true; 56 } 57 58 @Override onSelectionRefresh()59 public void onSelectionRefresh() { 60 mSelectionReset = true; 61 mSelected.clear(); 62 } 63 64 @Override onSelectionChanged()65 public void onSelectionChanged() { 66 mSelectionChanged = true; 67 } 68 69 @Override onSelectionRestored()70 public void onSelectionRestored() { 71 mSelectionRestored = true; 72 } 73 assertNoSelection()74 void assertNoSelection() { 75 assertTrue(mSelected.isEmpty()); 76 } 77 assertSelectionSize(int expected)78 void assertSelectionSize(int expected) { 79 assertEquals(expected, mSelected.size()); 80 } 81 assertSelected(K key)82 void assertSelected(K key) { 83 assertTrue(key + " is not selected.", mSelected.contains(key)); 84 } 85 assertNotSelected(K key)86 void assertNotSelected(K key) { 87 assertFalse(key + " is already selected", mSelected.contains(key)); 88 } 89 assertSelectionChanged()90 public void assertSelectionChanged() { 91 assertTrue(mSelectionChanged); 92 } 93 wasSelectionCleared()94 public boolean wasSelectionCleared() { 95 return mSelectionCleared; 96 } 97 assertSelectionCleared()98 public void assertSelectionCleared() { 99 assertTrue(mSelectionCleared); 100 } 101 assertSelectionUnchanged()102 public void assertSelectionUnchanged() { 103 assertFalse(mSelectionChanged); 104 } 105 assertSelectionReset()106 public void assertSelectionReset() { 107 assertTrue(mSelectionReset); 108 } 109 assertSelectionRestored()110 public void assertSelectionRestored() { 111 assertTrue(mSelectionRestored); 112 } 113 } 114