1 /* 2 * Copyright (C) 2015 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 com.android.documentsui.dirlist; 18 19 import static org.junit.Assert.assertTrue; 20 21 import androidx.recyclerview.selection.SelectionTracker; 22 import androidx.recyclerview.widget.RecyclerView; 23 import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver; 24 import android.view.ViewGroup; 25 26 import com.android.documentsui.Model.Update; 27 import com.android.documentsui.base.EventListener; 28 import com.android.documentsui.testing.TestEventListener; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** 34 * A skeletal {@link DocumentsAdapter} test double. 35 */ 36 public class TestDocumentsAdapter extends DocumentsAdapter { 37 38 final TestEventListener<Update> mModelListener = new TestEventListener<>(); 39 List<String> mModelIds = new ArrayList<>(); 40 private final AdapterDataObserver mAdapterObserver; 41 private final List<Integer> mSelectionChanged = new ArrayList<>(); 42 TestDocumentsAdapter(List<String> modelIds)43 public TestDocumentsAdapter(List<String> modelIds) { 44 mModelIds = modelIds; 45 46 mAdapterObserver = new RecyclerView.AdapterDataObserver() { 47 48 @Override 49 public void onChanged() { 50 } 51 52 @Override 53 public void onItemRangeChanged(int startPosition, int itemCount, Object payload) { 54 if (SelectionTracker.SELECTION_CHANGED_MARKER.equals(payload)) { 55 int last = startPosition + itemCount; 56 for (int i = startPosition; i < last; i++) { 57 mSelectionChanged.add(i); 58 } 59 } 60 } 61 62 @Override 63 public void onItemRangeInserted(int startPosition, int itemCount) { 64 } 65 66 @Override 67 public void onItemRangeRemoved(int startPosition, int itemCount) { 68 } 69 70 @Override 71 public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { 72 throw new UnsupportedOperationException(); 73 } 74 }; 75 76 registerAdapterDataObserver(mAdapterObserver); 77 } 78 resetSelectionChanged()79 public void resetSelectionChanged() { 80 mSelectionChanged.clear(); 81 } 82 assertSelectionChanged(int position)83 public void assertSelectionChanged(int position) { 84 assertTrue(mSelectionChanged.contains(position)); 85 } 86 87 @Override getModelUpdateListener()88 EventListener<Update> getModelUpdateListener() { 89 return mModelListener; 90 } 91 92 @Override getStableIds()93 public List<String> getStableIds() { 94 return mModelIds; 95 } 96 97 @Override getPosition(String id)98 public int getPosition(String id) { 99 return mModelIds.indexOf(id); 100 } 101 102 @Override getStableId(int position)103 public String getStableId(int position) { 104 return mModelIds.get(position); 105 } 106 107 @Override onCreateViewHolder(ViewGroup parent, int viewType)108 public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) { 109 throw new UnsupportedOperationException(); 110 } 111 112 @Override onBindViewHolder(DocumentHolder holder, int position)113 public void onBindViewHolder(DocumentHolder holder, int position) { 114 throw new UnsupportedOperationException(); 115 } 116 117 @Override getAdapterPosition(String docId)118 public int getAdapterPosition(String docId) { 119 return mModelIds.indexOf(docId); 120 } 121 122 @Override getItemCount()123 public int getItemCount() { 124 return mModelIds.size(); 125 } 126 updateTestModelIds(List<String> modelIds)127 public void updateTestModelIds(List<String> modelIds) { 128 mModelIds = modelIds; 129 130 notifyDataSetChanged(); 131 } 132 } 133