• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.support.test.InstrumentationRegistry;
21 import android.support.v7.widget.RecyclerView;
22 import android.view.View;
23 
24 import com.android.documentsui.dirlist.TestDocumentsAdapter;
25 
26 import org.mockito.Mockito;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 
32 public class TestRecyclerView extends RecyclerView {
33 
34     private List<RecyclerView.ViewHolder> holders = 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
findViewHolderForAdapterPosition(int position)43     public ViewHolder findViewHolderForAdapterPosition(int position) {
44         return holders.get(position);
45     }
46 
47     @Override
addOnScrollListener(OnScrollListener listener)48     public void addOnScrollListener(OnScrollListener listener) {
49     }
50 
51     @Override
smoothScrollToPosition(int position)52     public void smoothScrollToPosition(int position) {
53     }
54 
55     @Override
getAdapter()56     public RecyclerView.Adapter getAdapter() {
57         return adapter;
58     }
59 
60     @Override
setLayoutManager(LayoutManager manager)61     public void setLayoutManager(LayoutManager manager) {
62         mLayoutManager = manager;
63     }
64 
65     @Override
getLayoutManager()66     public RecyclerView.LayoutManager getLayoutManager() {
67         return mLayoutManager;
68     }
69 
setItems(List<String> modelIds)70     public void setItems(List<String> modelIds) {
71         holders = new ArrayList<>();
72         for (String modelId: modelIds) {
73             holders.add(new TestViewHolder(Views.createTestView()));
74         }
75         adapter.updateTestModelIds(modelIds);
76     }
77 
create(List<String> modelIds)78     public static TestRecyclerView create(List<String> modelIds) {
79         final TestRecyclerView view =
80                 new TestRecyclerView(InstrumentationRegistry.getTargetContext());
81         view.holders = new ArrayList<>();
82         for (String modelId: modelIds) {
83             view.holders.add(new TestViewHolder(Views.createTestView()));
84         }
85         view.adapter = new TestDocumentsAdapter(modelIds);
86         return view;
87     }
88 
assertItemViewFocused(int pos)89     public void assertItemViewFocused(int pos) {
90         Mockito.verify(holders.get(pos).itemView).requestFocus();
91     }
92 
93     private static class TestViewHolder extends RecyclerView.ViewHolder {
TestViewHolder(View itemView)94         public TestViewHolder(View itemView) {
95             super(itemView);
96         }
97     }
98 }
99