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.v7.widget.RecyclerView;
21 import android.view.View;
22 
23 import com.android.documentsui.dirlist.TestDocumentsAdapter;
24 
25 import org.mockito.Mockito;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 
31 public class TestRecyclerView extends RecyclerView {
32 
33     private List<RecyclerView.ViewHolder> holders = new ArrayList<>();
34     private TestDocumentsAdapter adapter;
35 
TestRecyclerView(Context context)36     public TestRecyclerView(Context context) {
37         super(context);
38     }
39 
40     @Override
findViewHolderForAdapterPosition(int position)41     public ViewHolder findViewHolderForAdapterPosition(int position) {
42         return holders.get(position);
43     }
44 
45     @Override
addOnScrollListener(OnScrollListener listener)46     public void addOnScrollListener(OnScrollListener listener) {
47     }
48 
49     @Override
smoothScrollToPosition(int position)50     public void smoothScrollToPosition(int position) {
51     }
52 
53     @Override
getAdapter()54     public RecyclerView.Adapter getAdapter() {
55         return adapter;
56     }
57 
setItems(List<String> modelIds)58     public void setItems(List<String> modelIds) {
59         holders = new ArrayList<>();
60         for (String modelId: modelIds) {
61             holders.add(new TestViewHolder(Views.createTestView()));
62         }
63         adapter.updateTestModelIds(modelIds);
64     }
65 
create(List<String> modelIds)66     public static TestRecyclerView create(List<String> modelIds) {
67         final TestRecyclerView view = Mockito.mock(TestRecyclerView.class,
68                 Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
69         view.holders = new ArrayList<>();
70         for (String modelId: modelIds) {
71             view.holders.add(new TestViewHolder(Views.createTestView()));
72         }
73         view.adapter = new TestDocumentsAdapter(modelIds);
74         return view;
75     }
76 
assertItemViewFocused(int pos)77     public void assertItemViewFocused(int pos) {
78         Mockito.verify(holders.get(pos).itemView).requestFocus();
79     }
80 
81     private static class TestViewHolder extends RecyclerView.ViewHolder {
TestViewHolder(View itemView)82         public TestViewHolder(View itemView) {
83             super(itemView);
84         }
85     }
86 }
87