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 java.util.HashSet; 26 import java.util.Set; 27 28 public class TestSelectionObserver<K> extends SelectionObserver<K> { 29 30 private final Set<K> mSelected = new HashSet<>(); 31 private boolean mSelectionChanged = false; 32 private boolean mSelectionReset = false; 33 private boolean mSelectionRestored = false; 34 35 public void reset() { 36 mSelected.clear(); 37 mSelectionChanged = false; 38 mSelectionReset = false; 39 } 40 41 @Override 42 public void onItemStateChanged(K key, boolean selected) { 43 if (selected) { 44 assertNotSelected(key); 45 mSelected.add(key); 46 } else { 47 assertSelected(key); 48 mSelected.remove(key); 49 } 50 } 51 52 @Override 53 public void onSelectionRefresh() { 54 mSelectionReset = true; 55 mSelected.clear(); 56 } 57 58 @Override 59 public void onSelectionChanged() { 60 mSelectionChanged = true; 61 } 62 63 @Override 64 public void onSelectionRestored() { 65 mSelectionRestored = true; 66 } 67 68 void assertNoSelection() { 69 assertTrue(mSelected.isEmpty()); 70 } 71 72 void assertSelectionSize(int expected) { 73 assertEquals(expected, mSelected.size()); 74 } 75 76 void assertSelected(K key) { 77 assertTrue(key + " is not selected.", mSelected.contains(key)); 78 } 79 80 void assertNotSelected(K key) { 81 assertFalse(key + " is already selected", mSelected.contains(key)); 82 } 83 84 public void assertSelectionChanged() { 85 assertTrue(mSelectionChanged); 86 } 87 88 public void assertSelectionUnchanged() { 89 assertFalse(mSelectionChanged); 90 } 91 92 public void assertSelectionReset() { 93 assertTrue(mSelectionReset); 94 } 95 96 public void assertSelectionRestored() { 97 assertTrue(mSelectionRestored); 98 } 99 } 100