Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 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; |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 26 | import android.view.ViewGroup; |
| 27 | |
| 28 | import com.android.documentsui.DirectoryResult; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 29 | import com.android.documentsui.base.State; |
| 30 | import com.android.documentsui.roots.RootCursorWrapper; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 31 | import com.android.documentsui.testing.SortModels; |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 32 | |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 33 | @SmallTest |
| 34 | public class SectionBreakDocumentsAdapterWrapperTest extends AndroidTestCase { |
| 35 | |
| 36 | private static final String AUTHORITY = "test_authority"; |
| 37 | private static final String[] NAMES = new String[] { |
| 38 | "4", |
| 39 | "foo", |
| 40 | "1", |
| 41 | "bar", |
| 42 | "*(Ljifl;a", |
| 43 | "0", |
| 44 | "baz", |
| 45 | "2", |
| 46 | "3", |
| 47 | "%$%VD" |
| 48 | }; |
| 49 | |
| 50 | private TestModel mModel; |
| 51 | private SectionBreakDocumentsAdapterWrapper mAdapter; |
| 52 | |
| 53 | public void setUp() { |
| 54 | |
| 55 | final Context testContext = TestContext.createStorageTestContext(getContext(), AUTHORITY); |
| 56 | DocumentsAdapter.Environment env = new TestEnvironment(testContext); |
| 57 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 58 | mModel = new TestModel(AUTHORITY); |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 59 | mAdapter = new SectionBreakDocumentsAdapterWrapper( |
| 60 | env, |
| 61 | new ModelBackedDocumentsAdapter( |
| 62 | env, new IconHelper(testContext, State.MODE_GRID))); |
| 63 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 64 | mModel.addUpdateListener(mAdapter.getModelUpdateListener()); |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Tests that the item count is correct for a directory containing only subdirs. |
| 68 | public void testItemCount_allDirs() { |
| 69 | MatrixCursor c = new MatrixCursor(TestModel.COLUMNS); |
| 70 | |
| 71 | for (int i = 0; i < 5; ++i) { |
| 72 | MatrixCursor.RowBuilder row = c.newRow(); |
| 73 | row.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY); |
| 74 | row.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i)); |
| 75 | row.add(Document.COLUMN_SIZE, i); |
| 76 | row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR); |
| 77 | } |
| 78 | DirectoryResult r = new DirectoryResult(); |
| 79 | r.cursor = c; |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 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; |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 105 | mModel.update(r); |
| 106 | |
| 107 | assertEquals(mModel.getItemCount() + 1, mAdapter.getItemCount()); |
| 108 | } |
| 109 | |
| 110 | private final class TestEnvironment implements DocumentsAdapter.Environment { |
| 111 | private final Context testContext; |
| 112 | |
| 113 | private TestEnvironment(Context testContext) { |
| 114 | this.testContext = testContext; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean isSelected(String id) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean isDocumentEnabled(String mimeType, int flags) { |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void initDocumentHolder(DocumentHolder holder) {} |
| 129 | |
| 130 | @Override |
| 131 | public Model getModel() { |
| 132 | return mModel; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public State getDisplayState() { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Context getContext() { |
| 142 | return testContext; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public int getColumnCount() { |
| 147 | return 4; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {} |
| 152 | } |
| 153 | |
Ben Kwa | b4a93ac | 2016-01-08 08:41:34 -0800 | [diff] [blame] | 154 | private static class DummyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
| 155 | public int getItemCount() { return 0; } |
| 156 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {} |
| 157 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 158 | return null; |
| 159 | } |
| 160 | } |
| 161 | } |