blob: 36d880a0689208d73c73dcf067758f3fdf8d2556 [file] [log] [blame]
Ben Kwa7461a952015-09-01 11:03:01 -07001/*
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;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.ContextWrapper;
22import android.database.Cursor;
23import android.database.MatrixCursor;
24import android.provider.DocumentsContract.Document;
Ben Kwa379e1762015-09-21 10:49:52 -070025import android.support.v7.widget.RecyclerView;
Ben Kwa7461a952015-09-01 11:03:01 -070026import android.test.AndroidTestCase;
Ben Kwa7461a952015-09-01 11:03:01 -070027import android.test.mock.MockContentResolver;
Ben Kwa379e1762015-09-21 10:49:52 -070028import android.view.ViewGroup;
Ben Kwa7461a952015-09-01 11:03:01 -070029
30import com.android.documentsui.DirectoryFragment.Model;
31import com.android.documentsui.MultiSelectManager.Selection;
32import com.android.documentsui.model.DocumentInfo;
33
34import java.util.List;
Ben Kwa7461a952015-09-01 11:03:01 -070035
36public 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 Kwa379e1762015-09-21 10:49:52 -070060 // Instantiate the model with a dummy view adapter and listener that (for now) do nothing.
Ben Kwaa017bc12015-10-07 14:15:12 -070061 model = new Model(mContext, new DummyAdapter());
Ben Kwa83cedf22015-09-11 15:15:45 -070062 model.addUpdateListener(new DummyListener());
Ben Kwa7461a952015-09-01 11:03:01 -070063 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 Kwa7461a952015-09-01 11:03:01 -070077 }
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 Kwa7461a952015-09-01 11:03:01 -070087 }
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 McKay68a8bb82015-10-21 14:38:54 -0700139 private void delete(int... positions) {
140 model.markForDeletion(new Selection(positions));
Ben Kwa7461a952015-09-01 11:03:01 -0700141 }
142
Steve McKay68a8bb82015-10-21 14:38:54 -0700143 private List<DocumentInfo> getDocumentInfo(int... positions) {
144 return model.getDocuments(new Selection(positions));
Ben Kwa7461a952015-09-01 11:03:01 -0700145 }
Ben Kwa83cedf22015-09-11 15:15:45 -0700146
Ben Kwa379e1762015-09-21 10:49:52 -0700147 private static class DummyListener extends Model.UpdateListener {
Ben Kwa83cedf22015-09-11 15:15:45 -0700148 public void onModelUpdate(Model model) {}
149 public void onModelUpdateFailed(Exception e) {}
Ben Kwa83cedf22015-09-11 15:15:45 -0700150 }
151
Ben Kwa379e1762015-09-21 10:49:52 -0700152 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 Kwa7461a952015-09-01 11:03:01 -0700159}