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 17package androidx.recyclerview.selection.testing; 18 19import static org.junit.Assert.assertTrue; 20 21import android.view.ViewGroup; 22 23import androidx.recyclerview.selection.SelectionTracker; 24import androidx.recyclerview.widget.RecyclerView; 25import androidx.recyclerview.widget.RecyclerView.Adapter; 26import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver; 27 28import java.util.ArrayList; 29import java.util.Collections; 30import java.util.List; 31 32public class TestAdapter<K> extends Adapter<TestHolder> { 33 34 private final List<K> mItems = new ArrayList<>(); 35 private final List<Integer> mNotifiedOfSelection = new ArrayList<>(); 36 private final AdapterDataObserver mAdapterObserver; 37 38 public TestAdapter() { 39 this(Collections.EMPTY_LIST); 40 } 41 42 public TestAdapter(List<K> items) { 43 mItems.addAll(items); 44 mAdapterObserver = new RecyclerView.AdapterDataObserver() { 45 46 @Override 47 public void onChanged() { 48 } 49 50 @Override 51 public void onItemRangeChanged(int startPosition, int itemCount, Object payload) { 52 if (SelectionTracker.SELECTION_CHANGED_MARKER.equals(payload)) { 53 int last = startPosition + itemCount; 54 for (int i = startPosition; i < last; i++) { 55 mNotifiedOfSelection.add(i); 56 } 57 } 58 } 59 60 @Override 61 public void onItemRangeInserted(int startPosition, int itemCount) { 62 } 63 64 @Override 65 public void onItemRangeRemoved(int startPosition, int itemCount) { 66 } 67 68 @Override 69 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { 70 throw new UnsupportedOperationException(); 71 } 72 }; 73 74 registerAdapterDataObserver(mAdapterObserver); 75 } 76 77 @Override 78 public TestHolder onCreateViewHolder(ViewGroup parent, int viewType) { 79 return new TestHolder(parent); 80 } 81 82 @Override 83 public void onBindViewHolder(TestHolder holder, int position) { 84 throw new UnsupportedOperationException(); 85 } 86 87 @Override 88 public int getItemCount() { 89 return mItems.size(); 90 } 91 92 public void updateTestModelIds(List<K> items) { 93 mItems.clear(); 94 mItems.addAll(items); 95 96 notifyDataSetChanged(); 97 } 98 99 public int getPosition(K key) { 100 return mItems.indexOf(key); 101 } 102 103 public K getSelectionKey(int position) { 104 return mItems.get(position); 105 } 106 107 108 public void resetSelectionNotifications() { 109 mNotifiedOfSelection.clear(); 110 } 111 112 public void assertNotifiedOfSelectionChange(int position) { 113 assertTrue(mNotifiedOfSelection.contains(position)); 114 } 115 116 public static List<String> createItemList(int num) { 117 List<String> items = new ArrayList<>(num); 118 for (int i = 0; i < num; ++i) { 119 items.add(Integer.toString(i)); 120 } 121 return items; 122 } 123} 124