Home | History | Annotate | Download | only in dirlist
      1 /*
      2  * Copyright (C) 2015 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.dirlist;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.support.v7.widget.RecyclerView;
     22 import android.test.AndroidTestCase;
     23 import android.test.suitebuilder.annotation.SmallTest;
     24 import android.util.SparseArray;
     25 import android.view.ViewGroup;
     26 
     27 import com.android.documentsui.State;
     28 
     29 import java.util.List;
     30 
     31 @SmallTest
     32 public class ModelBackedDocumentsAdapterTest extends AndroidTestCase {
     33 
     34     private static final String AUTHORITY = "test_authority";
     35     private static final String[] NAMES = new String[] {
     36             "4",
     37             "foo",
     38             "1",
     39             "bar",
     40             "*(Ljifl;a",
     41             "0",
     42             "baz",
     43             "2",
     44             "3",
     45             "%$%VD"
     46     };
     47 
     48     private TestModel mModel;
     49     private ModelBackedDocumentsAdapter mAdapter;
     50 
     51     public void setUp() {
     52 
     53         final Context testContext = TestContext.createStorageTestContext(getContext(), AUTHORITY);
     54         mModel = new TestModel(AUTHORITY);
     55         mModel.update(NAMES);
     56 
     57         DocumentsAdapter.Environment env = new TestEnvironment(testContext);
     58 
     59         mAdapter = new ModelBackedDocumentsAdapter(
     60                 env, new IconHelper(testContext, State.MODE_GRID));
     61         mAdapter.onModelUpdate(mModel);
     62     }
     63 
     64     // Tests that the item count is correct.
     65     public void testItemCount() {
     66         assertEquals(mModel.getItemCount(), mAdapter.getItemCount());
     67     }
     68 
     69     // Tests that the item count is correct.
     70     public void testHide_ItemCount() {
     71         String[] ids = mModel.getModelIds();
     72         mAdapter.hide(ids[0], ids[1]);
     73         assertEquals(mModel.getItemCount() - 2, mAdapter.getItemCount());
     74     }
     75 
     76     private final class TestEnvironment implements DocumentsAdapter.Environment {
     77         private final Context testContext;
     78 
     79         private TestEnvironment(Context testContext) {
     80             this.testContext = testContext;
     81         }
     82 
     83         @Override
     84         public boolean isSelected(String id) {
     85             return false;
     86         }
     87 
     88         @Override
     89         public boolean isDocumentEnabled(String mimeType, int flags) {
     90             return true;
     91         }
     92 
     93         @Override
     94         public void initDocumentHolder(DocumentHolder holder) {}
     95 
     96         @Override
     97         public Model getModel() {
     98             return mModel;
     99         }
    100 
    101         @Override
    102         public State getDisplayState() {
    103             return null;
    104         }
    105 
    106         @Override
    107         public Context getContext() {
    108             return testContext;
    109         }
    110 
    111         @Override
    112         public int getColumnCount() {
    113             return 4;
    114         }
    115 
    116         @Override
    117         public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {}
    118     }
    119 
    120     private static class DummyListener implements Model.UpdateListener {
    121         public void onModelUpdate(Model model) {}
    122         public void onModelUpdateFailed(Exception e) {}
    123     }
    124 
    125     private static class DummyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    126         public int getItemCount() { return 0; }
    127         public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}
    128         public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    129             return null;
    130         }
    131     }
    132 }
    133