1/*
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.os.Bundle;
22import android.provider.DocumentsContract;
23import android.support.test.filters.MediumTest;
24import android.support.v7.widget.RecyclerView;
25import android.test.AndroidTestCase;
26import android.view.ViewGroup;
27
28import com.android.documentsui.ActionHandler;
29import com.android.documentsui.Model;
30import com.android.documentsui.base.Features;
31import com.android.documentsui.base.State;
32import com.android.documentsui.testing.TestActionHandler;
33import com.android.documentsui.testing.TestEnv;
34import com.android.documentsui.testing.TestFileTypeLookup;
35
36@MediumTest
37public class DirectoryAddonsAdapterTest extends AndroidTestCase {
38
39    private static final String AUTHORITY = "test_authority";
40
41    private TestEnv mEnv;
42    private DirectoryAddonsAdapter mAdapter;
43    private ActionHandler mActionHandler;
44
45    public void setUp() {
46
47        mEnv = TestEnv.create(AUTHORITY);
48        mActionHandler = new TestActionHandler();
49        mEnv.clear();
50
51        final Context testContext = TestContext.createStorageTestContext(getContext(), AUTHORITY);
52        DocumentsAdapter.Environment env = new TestEnvironment(testContext);
53
54        mAdapter = new DirectoryAddonsAdapter(
55            env,
56            new ModelBackedDocumentsAdapter(
57                    env, new IconHelper(testContext, State.MODE_GRID), new TestFileTypeLookup()));
58
59        mEnv.model.addUpdateListener(mAdapter.getModelUpdateListener());
60    }
61
62    // Tests that the item count is correct for a directory containing files and subdirs.
63    public void testItemCount_mixed() {
64        mEnv.reset();  // creates a mix of folders and files for us.
65
66        assertEquals(mEnv.model.getItemCount() + 1, mAdapter.getItemCount());
67    }
68
69    public void testGetPosition() {
70        mEnv.model.createFolder("a");  // id will be "1"...derived from insert position.
71        mEnv.model.createFile("b");  // id will be "2"
72        mEnv.model.update();
73
74        assertEquals(0, mAdapter.getPosition("1"));
75        // adapter inserts a view between item 0 and 1 to force layout
76        // break between folders and files. This is reflected by an offset position.
77        assertEquals(2, mAdapter.getPosition("2"));
78    }
79
80    // Tests that the item count is correct for a directory containing only subdirs.
81    public void testItemCount_allDirs() {
82        String[] names = {"Trader Joe's", "Alphabeta", "Lucky", "Vons", "Gelson's"};
83        for (String name : names) {
84            mEnv.model.createFolder(name);
85        }
86        mEnv.model.update();
87        assertEquals(mEnv.model.getItemCount(), mAdapter.getItemCount());
88    }
89
90    // Tests that the item count is correct for a directory containing only files.
91    public void testItemCount_allFiles() {
92        String[] names = {"123.txt", "234.jpg", "abc.pdf"};
93        for (String name : names) {
94            mEnv.model.createFile(name);
95        }
96        mEnv.model.update();
97        assertEquals(mEnv.model.getItemCount(), mAdapter.getItemCount());
98    }
99
100    public void testAddsInfoMessage_WithDirectoryChildren() {
101        String[] names = {"123.txt", "234.jpg", "abc.pdf"};
102        for (String name : names) {
103            mEnv.model.createFile(name);
104        }
105        Bundle bundle = new Bundle();
106        bundle.putString(DocumentsContract.EXTRA_INFO, "some info");
107        mEnv.model.setCursorExtras(bundle);
108        mEnv.model.update();
109        assertEquals(mEnv.model.getItemCount() + 1, mAdapter.getItemCount());
110        assertHolderType(0, DocumentsAdapter.ITEM_TYPE_HEADER_MESSAGE);
111    }
112
113    public void testItemCount_none() {
114        mEnv.model.update();
115        assertEquals(1, mAdapter.getItemCount());
116        assertHolderType(0, DocumentsAdapter.ITEM_TYPE_INFLATED_MESSAGE);
117    }
118
119    public void testAddsInfoMessage_WithNoItem() {
120        Bundle bundle = new Bundle();
121        bundle.putString(DocumentsContract.EXTRA_INFO, "some info");
122        mEnv.model.setCursorExtras(bundle);
123
124        mEnv.model.update();
125        assertEquals(2, mAdapter.getItemCount());
126        assertHolderType(0, DocumentsAdapter.ITEM_TYPE_HEADER_MESSAGE);
127    }
128
129    public void testAddsErrorMessage_WithNoItem() {
130        Bundle bundle = new Bundle();
131        bundle.putString(DocumentsContract.EXTRA_ERROR, "some error");
132        mEnv.model.setCursorExtras(bundle);
133
134        mEnv.model.update();
135        assertEquals(2, mAdapter.getItemCount());
136        assertHolderType(0, DocumentsAdapter.ITEM_TYPE_HEADER_MESSAGE);
137    }
138
139    private void assertHolderType(int index, int type) {
140        assertTrue(mAdapter.getItemViewType(index) == type);
141    }
142
143    private final class TestEnvironment implements DocumentsAdapter.Environment {
144        private final Context testContext;
145
146        private TestEnvironment(Context testContext) {
147            this.testContext = testContext;
148        }
149
150        @Override
151        public Features getFeatures() {
152            return mEnv.features;
153        }
154
155        @Override
156        public ActionHandler getActionHandler() { return mActionHandler; }
157
158        @Override
159        public boolean isSelected(String id) {
160            return false;
161        }
162
163        @Override
164        public boolean isDocumentEnabled(String mimeType, int flags) {
165            return true;
166        }
167
168        @Override
169        public void initDocumentHolder(DocumentHolder holder) {}
170
171        @Override
172        public Model getModel() {
173            return mEnv.model;
174        }
175
176        @Override
177        public State getDisplayState() {
178            return null;
179        }
180
181        @Override
182        public boolean isInSearchMode() {
183            return false;
184        }
185
186        @Override
187        public Context getContext() {
188            return testContext;
189        }
190
191        @Override
192        public int getColumnCount() {
193            return 4;
194        }
195
196        @Override
197        public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {}
198    }
199
200    private static class DummyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
201        @Override
202        public int getItemCount() { return 0; }
203        @Override
204        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}
205        @Override
206        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
207            return null;
208        }
209    }
210}
211