Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 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; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.Context; |
| 21 | import android.content.ContextWrapper; |
| 22 | import android.database.Cursor; |
| 23 | import android.database.MatrixCursor; |
| 24 | import android.provider.DocumentsContract.Document; |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 25 | import android.support.v7.widget.RecyclerView; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 26 | import android.test.AndroidTestCase; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 27 | import android.test.mock.MockContentResolver; |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 29 | |
| 30 | import com.android.documentsui.DirectoryFragment.Model; |
| 31 | import com.android.documentsui.MultiSelectManager.Selection; |
| 32 | import com.android.documentsui.model.DocumentInfo; |
| 33 | |
| 34 | import java.util.List; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 35 | |
| 36 | public class DirectoryFragmentModelTest extends AndroidTestCase { |
| 37 | |
| 38 | private static final int ITEM_COUNT = 5; |
| 39 | private static final String[] COLUMNS = new String[]{ |
| 40 | Document.COLUMN_DOCUMENT_ID |
| 41 | }; |
| 42 | private static Cursor cursor; |
| 43 | |
| 44 | private Context mContext; |
| 45 | private Model model; |
| 46 | |
| 47 | public void setUp() { |
| 48 | setupTestContext(); |
| 49 | |
| 50 | MatrixCursor c = new MatrixCursor(COLUMNS); |
| 51 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 52 | MatrixCursor.RowBuilder row = c.newRow(); |
| 53 | row.add(COLUMNS[0], i); |
| 54 | } |
| 55 | cursor = c; |
| 56 | |
| 57 | DirectoryResult r = new DirectoryResult(); |
| 58 | r.cursor = cursor; |
| 59 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 60 | // Instantiate the model with a dummy view adapter and listener that (for now) do nothing. |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 61 | model = new Model(mContext, new DummyAdapter()); |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 62 | model.addUpdateListener(new DummyListener()); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 63 | model.update(r); |
| 64 | } |
| 65 | |
| 66 | // Tests that the item count is correct. |
| 67 | public void testItemCount() { |
| 68 | assertEquals(ITEM_COUNT, model.getItemCount()); |
| 69 | } |
| 70 | |
| 71 | // Tests that the item count is correct after a deletion. |
| 72 | public void testItemCount_WithDeletion() { |
| 73 | // Simulate deleting 2 files. |
| 74 | delete(2, 4); |
| 75 | |
| 76 | assertEquals(ITEM_COUNT - 2, model.getItemCount()); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Tests that the item count is correct after a deletion is undone. |
| 80 | public void testItemCount_WithUndoneDeletion() { |
| 81 | // Simulate deleting 2 files. |
| 82 | delete(0, 3); |
| 83 | |
| 84 | // Undo the deletion |
| 85 | model.undoDeletion(); |
| 86 | assertEquals(ITEM_COUNT, model.getItemCount()); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Tests that the right things are marked for deletion. |
| 90 | public void testMarkForDeletion() { |
| 91 | delete(1, 3); |
| 92 | |
| 93 | List<DocumentInfo> docs = model.getDocumentsMarkedForDeletion(); |
| 94 | assertEquals(2, docs.size()); |
| 95 | assertEquals("1", docs.get(0).documentId); |
| 96 | assertEquals("3", docs.get(1).documentId); |
| 97 | } |
| 98 | |
| 99 | // Tests the base case for Model.getItem. |
| 100 | public void testGetItem() { |
| 101 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 102 | Cursor c = model.getItem(i); |
| 103 | assertEquals(i, c.getPosition()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Tests that Model.getItem returns the right items after a deletion. |
| 108 | public void testGetItem_WithDeletion() { |
| 109 | // Simulate deleting 2 files. |
| 110 | delete(2, 3); |
| 111 | |
| 112 | List<DocumentInfo> docs = getDocumentInfo(0, 1, 2); |
| 113 | assertEquals("0", docs.get(0).documentId); |
| 114 | assertEquals("1", docs.get(1).documentId); |
| 115 | assertEquals("4", docs.get(2).documentId); |
| 116 | } |
| 117 | |
| 118 | // Tests that Model.getItem returns the right items after a deletion is undone. |
| 119 | public void testGetItem_WithCancelledDeletion() { |
| 120 | delete(0, 1); |
| 121 | model.undoDeletion(); |
| 122 | |
| 123 | // Test that all documents are accounted for, in the right position. |
| 124 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 125 | assertEquals(Integer.toString(i), getDocumentInfo(i).get(0).documentId); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private void setupTestContext() { |
| 130 | final MockContentResolver resolver = new MockContentResolver(); |
| 131 | mContext = new ContextWrapper(getContext()) { |
| 132 | @Override |
| 133 | public ContentResolver getContentResolver() { |
| 134 | return resolver; |
| 135 | } |
| 136 | }; |
| 137 | } |
| 138 | |
Steve McKay | 68a8bb8 | 2015-10-21 14:38:54 -0700 | [diff] [blame^] | 139 | private void delete(int... positions) { |
| 140 | model.markForDeletion(new Selection(positions)); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Steve McKay | 68a8bb8 | 2015-10-21 14:38:54 -0700 | [diff] [blame^] | 143 | private List<DocumentInfo> getDocumentInfo(int... positions) { |
| 144 | return model.getDocuments(new Selection(positions)); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 145 | } |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 146 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 147 | private static class DummyListener extends Model.UpdateListener { |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 148 | public void onModelUpdate(Model model) {} |
| 149 | public void onModelUpdateFailed(Exception e) {} |
Ben Kwa | 83cedf2 | 2015-09-11 15:15:45 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Ben Kwa | 379e176 | 2015-09-21 10:49:52 -0700 | [diff] [blame] | 152 | private static class DummyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
| 153 | public int getItemCount() { return 0; } |
| 154 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {} |
| 155 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 156 | return null; |
| 157 | } |
| 158 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame] | 159 | } |