blob: d44a4678d989641e9d6c6b8177702be3b76afd7a [file] [log] [blame]
Ben Kwab4a93ac2016-01-08 08:41:34 -08001/*
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
17package com.android.documentsui.dirlist;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.database.MatrixCursor;
22import android.provider.DocumentsContract.Document;
23import android.support.v7.widget.RecyclerView;
24import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
Ben Kwab4a93ac2016-01-08 08:41:34 -080026import android.view.ViewGroup;
27
28import com.android.documentsui.DirectoryResult;
Steve McKayd9caa6a2016-09-15 16:36:45 -070029import com.android.documentsui.base.State;
30import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070031import com.android.documentsui.testing.SortModels;
Ben Kwab4a93ac2016-01-08 08:41:34 -080032
Ben Kwab4a93ac2016-01-08 08:41:34 -080033@SmallTest
34public 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 McKay97b4be42016-01-20 15:09:35 -080058 mModel = new TestModel(AUTHORITY);
Ben Kwab4a93ac2016-01-08 08:41:34 -080059 mAdapter = new SectionBreakDocumentsAdapterWrapper(
60 env,
61 new ModelBackedDocumentsAdapter(
62 env, new IconHelper(testContext, State.MODE_GRID)));
63
Steve McKay990f76e2016-09-16 12:36:58 -070064 mModel.addUpdateListener(mAdapter.getModelUpdateListener());
Ben Kwab4a93ac2016-01-08 08:41:34 -080065 }
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 Kwab4a93ac2016-01-08 08:41:34 -080080 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 Kwab4a93ac2016-01-08 08:41:34 -0800105 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 Kwab4a93ac2016-01-08 08:41:34 -0800154 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}