Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [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.dirlist; |
| 18 | |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 19 | import android.content.ContentResolver; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 20 | import android.content.ContextWrapper; |
| 21 | import android.database.Cursor; |
| 22 | import android.database.MatrixCursor; |
Tomasz Mikolajewski | fb26c76 | 2016-03-23 16:41:48 +0900 | [diff] [blame] | 23 | import android.database.MergeCursor; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 24 | import android.provider.DocumentsContract.Document; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 25 | import android.test.AndroidTestCase; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 26 | import android.test.mock.MockContentResolver; |
| 27 | import android.test.suitebuilder.annotation.SmallTest; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 28 | |
| 29 | import com.android.documentsui.DirectoryResult; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 30 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 31 | import com.android.documentsui.base.Shared; |
| 32 | import com.android.documentsui.roots.RootCursorWrapper; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 33 | import com.android.documentsui.sorting.SortDimension; |
| 34 | import com.android.documentsui.sorting.SortModel; |
| 35 | import com.android.documentsui.testing.SortModels; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 36 | import com.android.documentsui.testing.TestEventListener; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 37 | |
| 38 | import java.util.ArrayList; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 39 | import java.util.BitSet; |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 40 | import java.util.HashSet; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 41 | import java.util.List; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 42 | import java.util.Random; |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 43 | import java.util.Set; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 44 | |
| 45 | @SmallTest |
| 46 | public class ModelTest extends AndroidTestCase { |
| 47 | |
| 48 | private static final int ITEM_COUNT = 10; |
| 49 | private static final String AUTHORITY = "test_authority"; |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 50 | |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 51 | private static final String[] COLUMNS = new String[]{ |
| 52 | RootCursorWrapper.COLUMN_AUTHORITY, |
| 53 | Document.COLUMN_DOCUMENT_ID, |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 54 | Document.COLUMN_FLAGS, |
| 55 | Document.COLUMN_DISPLAY_NAME, |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 56 | Document.COLUMN_SIZE, |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 57 | Document.COLUMN_LAST_MODIFIED, |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 58 | Document.COLUMN_MIME_TYPE |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 59 | }; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 60 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 61 | private static final String[] NAMES = new String[] { |
| 62 | "4", |
| 63 | "foo", |
| 64 | "1", |
| 65 | "bar", |
| 66 | "*(Ljifl;a", |
| 67 | "0", |
| 68 | "baz", |
| 69 | "2", |
| 70 | "3", |
| 71 | "%$%VD" |
| 72 | }; |
| 73 | |
| 74 | private Cursor cursor; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 75 | private Model model; |
| 76 | private TestContentProvider provider; |
| 77 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 78 | @Override |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 79 | public void setUp() { |
| 80 | setupTestContext(); |
| 81 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 82 | Random rand = new Random(); |
| 83 | |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 84 | MatrixCursor c = new MatrixCursor(COLUMNS); |
| 85 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 86 | MatrixCursor.RowBuilder row = c.newRow(); |
| 87 | row.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY); |
| 88 | row.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i)); |
| 89 | row.add(Document.COLUMN_FLAGS, Document.FLAG_SUPPORTS_DELETE); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 90 | // Generate random document names and sizes. This forces the model's internal sort code |
| 91 | // to actually do something. |
| 92 | row.add(Document.COLUMN_DISPLAY_NAME, NAMES[i]); |
| 93 | row.add(Document.COLUMN_SIZE, rand.nextInt()); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 94 | } |
| 95 | cursor = c; |
| 96 | |
| 97 | DirectoryResult r = new DirectoryResult(); |
| 98 | r.cursor = cursor; |
| 99 | |
| 100 | // Instantiate the model with a dummy view adapter and listener that (for now) do nothing. |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 101 | model = new Model(); |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 102 | // not sure why we add a listener here at all. |
| 103 | model.addUpdateListener(new TestEventListener<>()); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 104 | model.update(r); |
| 105 | } |
| 106 | |
| 107 | // Tests that the item count is correct. |
| 108 | public void testItemCount() { |
| 109 | assertEquals(ITEM_COUNT, model.getItemCount()); |
| 110 | } |
| 111 | |
| 112 | // Tests multiple authorities with clashing document IDs. |
| 113 | public void testModelIdIsUnique() { |
Tomasz Mikolajewski | fb26c76 | 2016-03-23 16:41:48 +0900 | [diff] [blame] | 114 | MatrixCursor cIn1 = new MatrixCursor(COLUMNS); |
| 115 | MatrixCursor cIn2 = new MatrixCursor(COLUMNS); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 116 | |
| 117 | // Make two sets of items with the same IDs, under different authorities. |
| 118 | final String AUTHORITY0 = "auth0"; |
| 119 | final String AUTHORITY1 = "auth1"; |
Tomasz Mikolajewski | fb26c76 | 2016-03-23 16:41:48 +0900 | [diff] [blame] | 120 | |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 121 | for (int i = 0; i < ITEM_COUNT; ++i) { |
Tomasz Mikolajewski | fb26c76 | 2016-03-23 16:41:48 +0900 | [diff] [blame] | 122 | MatrixCursor.RowBuilder row0 = cIn1.newRow(); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 123 | row0.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY0); |
| 124 | row0.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i)); |
| 125 | |
Tomasz Mikolajewski | fb26c76 | 2016-03-23 16:41:48 +0900 | [diff] [blame] | 126 | MatrixCursor.RowBuilder row1 = cIn2.newRow(); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 127 | row1.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY1); |
| 128 | row1.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i)); |
| 129 | } |
| 130 | |
Tomasz Mikolajewski | fb26c76 | 2016-03-23 16:41:48 +0900 | [diff] [blame] | 131 | Cursor cIn = new MergeCursor(new Cursor[] { cIn1, cIn2 }); |
| 132 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 133 | // Update the model, then make sure it contains all the expected items. |
| 134 | DirectoryResult r = new DirectoryResult(); |
| 135 | r.cursor = cIn; |
| 136 | model.update(r); |
| 137 | |
| 138 | assertEquals(ITEM_COUNT * 2, model.getItemCount()); |
| 139 | BitSet b0 = new BitSet(ITEM_COUNT); |
| 140 | BitSet b1 = new BitSet(ITEM_COUNT); |
| 141 | |
| 142 | for (String id: model.getModelIds()) { |
| 143 | Cursor cOut = model.getItem(id); |
| 144 | String authority = |
| 145 | DocumentInfo.getCursorString(cOut, RootCursorWrapper.COLUMN_AUTHORITY); |
| 146 | String docId = DocumentInfo.getCursorString(cOut, Document.COLUMN_DOCUMENT_ID); |
| 147 | |
| 148 | switch (authority) { |
| 149 | case AUTHORITY0: |
| 150 | b0.set(Integer.parseInt(docId)); |
| 151 | break; |
| 152 | case AUTHORITY1: |
| 153 | b1.set(Integer.parseInt(docId)); |
| 154 | break; |
| 155 | default: |
| 156 | fail("Unrecognized authority string"); |
| 157 | } |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 158 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 159 | |
| 160 | assertEquals(ITEM_COUNT, b0.cardinality()); |
| 161 | assertEquals(ITEM_COUNT, b1.cardinality()); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Tests the base case for Model.getItem. |
| 165 | public void testGetItem() { |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 166 | String[] ids = model.getModelIds(); |
| 167 | assertEquals(ITEM_COUNT, ids.length); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 168 | for (int i = 0; i < ITEM_COUNT; ++i) { |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 169 | Cursor c = model.getItem(ids[i]); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 170 | assertEquals(i, c.getPosition()); |
| 171 | } |
| 172 | } |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 173 | private void setupTestContext() { |
| 174 | final MockContentResolver resolver = new MockContentResolver(); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 175 | new ContextWrapper(getContext()) { |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 176 | @Override |
| 177 | public ContentResolver getContentResolver() { |
| 178 | return resolver; |
| 179 | } |
| 180 | }; |
| 181 | provider = new TestContentProvider(); |
| 182 | resolver.addProvider(AUTHORITY, provider); |
| 183 | } |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 184 | } |