1 /*
2  * Copyright (C) 2016 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.testing;
18 
19 import android.content.Context;
20 import android.view.View;
21 
22 import androidx.recyclerview.widget.RecyclerView;
23 import androidx.test.InstrumentationRegistry;
24 
25 import com.android.documentsui.dirlist.TestDocumentsAdapter;
26 
27 import org.mockito.Mockito;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 public class TestRecyclerView extends RecyclerView {
33 
34     private List<RecyclerView.ViewHolder> mHolders = new ArrayList<>();
35     private TestDocumentsAdapter adapter;
36     private RecyclerView.LayoutManager mLayoutManager;
37 
TestRecyclerView(Context context)38     public TestRecyclerView(Context context) {
39         super(context);
40     }
41 
42     @Override
getChildViewHolder(View child)43     public ViewHolder getChildViewHolder(View child) {
44         return mHolders.get(0);
45     }
46 
setHolders(List<ViewHolder> holders)47     public void setHolders(List<ViewHolder> holders) {
48         mHolders = holders;
49     }
50 
51     @Override
findViewHolderForAdapterPosition(int position)52     public ViewHolder findViewHolderForAdapterPosition(int position) {
53         return mHolders.get(position);
54     }
55 
56     @Override
addOnScrollListener(OnScrollListener listener)57     public void addOnScrollListener(OnScrollListener listener) {
58     }
59 
60     @Override
smoothScrollToPosition(int position)61     public void smoothScrollToPosition(int position) {
62     }
63 
64     @Override
getAdapter()65     public RecyclerView.Adapter getAdapter() {
66         return adapter;
67     }
68 
69     @Override
setLayoutManager(LayoutManager manager)70     public void setLayoutManager(LayoutManager manager) {
71         mLayoutManager = manager;
72     }
73 
74     @Override
getLayoutManager()75     public RecyclerView.LayoutManager getLayoutManager() {
76         return mLayoutManager;
77     }
78 
setItems(List<String> modelIds)79     public void setItems(List<String> modelIds) {
80         mHolders = new ArrayList<>();
81         for (String modelId: modelIds) {
82             mHolders.add(new TestViewHolder(Views.createTestView()));
83         }
84         adapter.updateTestModelIds(modelIds);
85     }
86 
create(List<String> modelIds)87     public static TestRecyclerView create(List<String> modelIds) {
88         final TestRecyclerView view =
89                 new TestRecyclerView(InstrumentationRegistry.getTargetContext());
90         view.mHolders = new ArrayList<>();
91         for (String modelId: modelIds) {
92             view.mHolders.add(new TestViewHolder(Views.createTestView()));
93         }
94         view.adapter = new TestDocumentsAdapter(modelIds);
95         return view;
96     }
97 
assertItemViewFocused(int pos)98     public void assertItemViewFocused(int pos) {
99         Mockito.verify(mHolders.get(pos).itemView).requestFocus();
100     }
101 
102     private static class TestViewHolder extends RecyclerView.ViewHolder {
TestViewHolder(View itemView)103         public TestViewHolder(View itemView) {
104             super(itemView);
105         }
106     }
107 }
108