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