Home | History | Annotate | Download | only in testing
      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 
     38     public TestRecyclerView(Context context) {
     39         super(context);
     40     }
     41 
     42     @Override
     43     public ViewHolder findViewHolderForAdapterPosition(int position) {
     44         return holders.get(position);
     45     }
     46 
     47     @Override
     48     public void addOnScrollListener(OnScrollListener listener) {
     49     }
     50 
     51     @Override
     52     public void smoothScrollToPosition(int position) {
     53     }
     54 
     55     @Override
     56     public RecyclerView.Adapter getAdapter() {
     57         return adapter;
     58     }
     59 
     60     @Override
     61     public void setLayoutManager(LayoutManager manager) {
     62         mLayoutManager = manager;
     63     }
     64 
     65     @Override
     66     public RecyclerView.LayoutManager getLayoutManager() {
     67         return mLayoutManager;
     68     }
     69 
     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 
     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 
     89     public void assertItemViewFocused(int pos) {
     90         Mockito.verify(holders.get(pos).itemView).requestFocus();
     91     }
     92 
     93     private static class TestViewHolder extends RecyclerView.ViewHolder {
     94         public TestViewHolder(View itemView) {
     95             super(itemView);
     96         }
     97     }
     98 }
     99