blob: faad880770ff02c60415bc2a1d6305917362acb4 [file] [log] [blame]
Ben Kwada858bf2015-12-09 14:33:49 -08001/*
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
17package com.android.documentsui.dirlist;
18
Ben Kwada858bf2015-12-09 14:33:49 -080019import android.content.ContentResolver;
Ben Kwada858bf2015-12-09 14:33:49 -080020import android.content.ContextWrapper;
21import android.database.Cursor;
22import android.database.MatrixCursor;
Tomasz Mikolajewskifb26c762016-03-23 16:41:48 +090023import android.database.MergeCursor;
Ben Kwada858bf2015-12-09 14:33:49 -080024import android.provider.DocumentsContract.Document;
Ben Kwada858bf2015-12-09 14:33:49 -080025import android.test.AndroidTestCase;
Ben Kwada858bf2015-12-09 14:33:49 -080026import android.test.mock.MockContentResolver;
27import android.test.suitebuilder.annotation.SmallTest;
Ben Kwada858bf2015-12-09 14:33:49 -080028
29import com.android.documentsui.DirectoryResult;
Steve McKayd0805062016-09-15 14:30:38 -070030import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070031import com.android.documentsui.base.Shared;
32import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070033import com.android.documentsui.sorting.SortDimension;
34import com.android.documentsui.sorting.SortModel;
35import com.android.documentsui.testing.SortModels;
Steve McKay990f76e2016-09-16 12:36:58 -070036import com.android.documentsui.testing.TestEventListener;
Ben Kwada858bf2015-12-09 14:33:49 -080037
38import java.util.ArrayList;
Ben Kwab8a5e082015-12-07 13:25:27 -080039import java.util.BitSet;
Ben Kwae4338342016-01-08 15:34:47 -080040import java.util.HashSet;
Ben Kwada858bf2015-12-09 14:33:49 -080041import java.util.List;
Ben Kwab8a5e082015-12-07 13:25:27 -080042import java.util.Random;
Ben Kwae4338342016-01-08 15:34:47 -080043import java.util.Set;
Ben Kwada858bf2015-12-09 14:33:49 -080044
45@SmallTest
46public class ModelTest extends AndroidTestCase {
47
48 private static final int ITEM_COUNT = 10;
49 private static final String AUTHORITY = "test_authority";
Steve McKayc5ecf892015-12-22 18:15:31 -080050
Ben Kwada858bf2015-12-09 14:33:49 -080051 private static final String[] COLUMNS = new String[]{
52 RootCursorWrapper.COLUMN_AUTHORITY,
53 Document.COLUMN_DOCUMENT_ID,
Ben Kwab8a5e082015-12-07 13:25:27 -080054 Document.COLUMN_FLAGS,
55 Document.COLUMN_DISPLAY_NAME,
Ben Kwa6280de02015-12-16 19:42:08 -080056 Document.COLUMN_SIZE,
Ben Kwae4338342016-01-08 15:34:47 -080057 Document.COLUMN_LAST_MODIFIED,
Ben Kwa6280de02015-12-16 19:42:08 -080058 Document.COLUMN_MIME_TYPE
Ben Kwada858bf2015-12-09 14:33:49 -080059 };
Ben Kwada858bf2015-12-09 14:33:49 -080060
Steve McKayc5ecf892015-12-22 18:15:31 -080061 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 Kwada858bf2015-12-09 14:33:49 -080075 private Model model;
76 private TestContentProvider provider;
77
Steve McKay9de0da62016-08-25 15:18:23 -070078 @Override
Ben Kwada858bf2015-12-09 14:33:49 -080079 public void setUp() {
80 setupTestContext();
81
Ben Kwab8a5e082015-12-07 13:25:27 -080082 Random rand = new Random();
83
Ben Kwada858bf2015-12-09 14:33:49 -080084 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 Kwab8a5e082015-12-07 13:25:27 -080090 // 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 Kwada858bf2015-12-09 14:33:49 -080094 }
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 McKay97b4be42016-01-20 15:09:35 -0800101 model = new Model();
Steve McKay990f76e2016-09-16 12:36:58 -0700102 // not sure why we add a listener here at all.
103 model.addUpdateListener(new TestEventListener<>());
Ben Kwada858bf2015-12-09 14:33:49 -0800104 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 Mikolajewskifb26c762016-03-23 16:41:48 +0900114 MatrixCursor cIn1 = new MatrixCursor(COLUMNS);
115 MatrixCursor cIn2 = new MatrixCursor(COLUMNS);
Ben Kwada858bf2015-12-09 14:33:49 -0800116
117 // Make two sets of items with the same IDs, under different authorities.
118 final String AUTHORITY0 = "auth0";
119 final String AUTHORITY1 = "auth1";
Tomasz Mikolajewskifb26c762016-03-23 16:41:48 +0900120
Ben Kwada858bf2015-12-09 14:33:49 -0800121 for (int i = 0; i < ITEM_COUNT; ++i) {
Tomasz Mikolajewskifb26c762016-03-23 16:41:48 +0900122 MatrixCursor.RowBuilder row0 = cIn1.newRow();
Ben Kwada858bf2015-12-09 14:33:49 -0800123 row0.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY0);
124 row0.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
125
Tomasz Mikolajewskifb26c762016-03-23 16:41:48 +0900126 MatrixCursor.RowBuilder row1 = cIn2.newRow();
Ben Kwada858bf2015-12-09 14:33:49 -0800127 row1.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY1);
128 row1.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
129 }
130
Tomasz Mikolajewskifb26c762016-03-23 16:41:48 +0900131 Cursor cIn = new MergeCursor(new Cursor[] { cIn1, cIn2 });
132
Ben Kwab8a5e082015-12-07 13:25:27 -0800133 // 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 Kwada858bf2015-12-09 14:33:49 -0800158 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800159
160 assertEquals(ITEM_COUNT, b0.cardinality());
161 assertEquals(ITEM_COUNT, b1.cardinality());
Ben Kwada858bf2015-12-09 14:33:49 -0800162 }
163
164 // Tests the base case for Model.getItem.
165 public void testGetItem() {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900166 String[] ids = model.getModelIds();
167 assertEquals(ITEM_COUNT, ids.length);
Ben Kwada858bf2015-12-09 14:33:49 -0800168 for (int i = 0; i < ITEM_COUNT; ++i) {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900169 Cursor c = model.getItem(ids[i]);
Ben Kwada858bf2015-12-09 14:33:49 -0800170 assertEquals(i, c.getPosition());
171 }
172 }
Ben Kwada858bf2015-12-09 14:33:49 -0800173 private void setupTestContext() {
174 final MockContentResolver resolver = new MockContentResolver();
Steve McKay9de0da62016-08-25 15:18:23 -0700175 new ContextWrapper(getContext()) {
Ben Kwada858bf2015-12-09 14:33:49 -0800176 @Override
177 public ContentResolver getContentResolver() {
178 return resolver;
179 }
180 };
181 provider = new TestContentProvider();
182 resolver.addProvider(AUTHORITY, provider);
183 }
Ben Kwada858bf2015-12-09 14:33:49 -0800184}