Home | History | Annotate | Download | only in dirlist
      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.dirlist;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.database.MatrixCursor;
     22 import android.provider.DocumentsContract.Document;
     23 import android.support.v7.widget.RecyclerView;
     24 import android.test.AndroidTestCase;
     25 import android.test.suitebuilder.annotation.SmallTest;
     26 import android.view.ViewGroup;
     27 
     28 import com.android.documentsui.DirectoryResult;
     29 import com.android.documentsui.RootCursorWrapper;
     30 import com.android.documentsui.State;
     31 
     32 @SmallTest
     33 public class SectionBreakDocumentsAdapterWrapperTest extends AndroidTestCase {
     34 
     35     private static final String AUTHORITY = "test_authority";
     36     private static final String[] NAMES = new String[] {
     37             "4",
     38             "foo",
     39             "1",
     40             "bar",
     41             "*(Ljifl;a",
     42             "0",
     43             "baz",
     44             "2",
     45             "3",
     46             "%$%VD"
     47     };
     48 
     49     private TestModel mModel;
     50     private SectionBreakDocumentsAdapterWrapper mAdapter;
     51 
     52     public void setUp() {
     53 
     54         final Context testContext = TestContext.createStorageTestContext(getContext(), AUTHORITY);
     55         DocumentsAdapter.Environment env = new TestEnvironment(testContext);
     56 
     57         mModel = new TestModel(AUTHORITY);
     58         mAdapter = new SectionBreakDocumentsAdapterWrapper(
     59             env,
     60             new ModelBackedDocumentsAdapter(
     61                 env, new IconHelper(testContext, State.MODE_GRID)));
     62 
     63         mModel.addUpdateListener(mAdapter);
     64     }
     65 
     66     // Tests that the item count is correct for a directory containing only subdirs.
     67     public void testItemCount_allDirs() {
     68         MatrixCursor c = new MatrixCursor(TestModel.COLUMNS);
     69 
     70         for (int i = 0; i < 5; ++i) {
     71             MatrixCursor.RowBuilder row = c.newRow();
     72             row.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY);
     73             row.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
     74             row.add(Document.COLUMN_SIZE, i);
     75             row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
     76         }
     77         DirectoryResult r = new DirectoryResult();
     78         r.cursor = c;
     79         r.sortOrder = State.SORT_ORDER_SIZE;
     80         mModel.update(r);
     81 
     82         assertEquals(mModel.getItemCount(), mAdapter.getItemCount());
     83     }
     84 
     85     // Tests that the item count is correct for a directory containing only files.
     86     public void testItemCount_allFiles() {
     87         mModel.update(NAMES);
     88         assertEquals(mModel.getItemCount(), mAdapter.getItemCount());
     89     }
     90 
     91     // Tests that the item count is correct for a directory containing files and subdirs.
     92     public void testItemCount_mixed() {
     93         MatrixCursor c = new MatrixCursor(TestModel.COLUMNS);
     94 
     95         for (int i = 0; i < 5; ++i) {
     96             MatrixCursor.RowBuilder row = c.newRow();
     97             row.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY);
     98             row.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
     99             row.add(Document.COLUMN_SIZE, i);
    100             String mimeType =(i < 2) ? Document.MIME_TYPE_DIR : "text/*";
    101             row.add(Document.COLUMN_MIME_TYPE, mimeType);
    102         }
    103         DirectoryResult r = new DirectoryResult();
    104         r.cursor = c;
    105         r.sortOrder = State.SORT_ORDER_SIZE;
    106         mModel.update(r);
    107 
    108         assertEquals(mModel.getItemCount() + 1, mAdapter.getItemCount());
    109     }
    110 
    111     private final class TestEnvironment implements DocumentsAdapter.Environment {
    112         private final Context testContext;
    113 
    114         private TestEnvironment(Context testContext) {
    115             this.testContext = testContext;
    116         }
    117 
    118         @Override
    119         public boolean isSelected(String id) {
    120             return false;
    121         }
    122 
    123         @Override
    124         public boolean isDocumentEnabled(String mimeType, int flags) {
    125             return true;
    126         }
    127 
    128         @Override
    129         public void initDocumentHolder(DocumentHolder holder) {}
    130 
    131         @Override
    132         public Model getModel() {
    133             return mModel;
    134         }
    135 
    136         @Override
    137         public State getDisplayState() {
    138             return null;
    139         }
    140 
    141         @Override
    142         public Context getContext() {
    143             return testContext;
    144         }
    145 
    146         @Override
    147         public int getColumnCount() {
    148             return 4;
    149         }
    150 
    151         @Override
    152         public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {}
    153     }
    154 
    155     private static class DummyListener implements Model.UpdateListener {
    156         public void onModelUpdate(Model model) {}
    157         public void onModelUpdateFailed(Exception e) {}
    158     }
    159 
    160     private static class DummyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    161         public int getItemCount() { return 0; }
    162         public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}
    163         public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    164             return null;
    165         }
    166     }
    167 }
    168