blob: 7ab51ba278c11dce79b29892bf645b1988c961c2 [file] [log] [blame]
Steve McKayef16f5f2015-12-22 18:15:31 -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
19import android.content.Context;
20import android.database.Cursor;
21import android.support.v7.widget.RecyclerView;
22import android.test.AndroidTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
Steve McKay955e46d2016-01-05 12:53:35 -080024import android.util.SparseArray;
Steve McKayef16f5f2015-12-22 18:15:31 -080025import android.view.ViewGroup;
26
27import com.android.documentsui.State;
28
29import java.util.List;
30
31@SmallTest
32public class ModelBackedDocumentsAdapterTest extends AndroidTestCase {
33
34 private static final String AUTHORITY = "test_authority";
35 private static final String[] NAMES = new String[] {
36 "4",
37 "foo",
38 "1",
39 "bar",
40 "*(Ljifl;a",
41 "0",
42 "baz",
43 "2",
44 "3",
45 "%$%VD"
46 };
47
Steve McKay955e46d2016-01-05 12:53:35 -080048 private TestModel mModel;
49 private ModelBackedDocumentsAdapter mAdapter;
Steve McKayef16f5f2015-12-22 18:15:31 -080050
51 public void setUp() {
52
53 final Context testContext = TestContext.createStorageTestContext(getContext(), AUTHORITY);
Steve McKay35645432016-01-20 15:09:35 -080054 mModel = new TestModel(AUTHORITY);
Steve McKay955e46d2016-01-05 12:53:35 -080055 mModel.update(NAMES);
Steve McKayef16f5f2015-12-22 18:15:31 -080056
57 DocumentsAdapter.Environment env = new TestEnvironment(testContext);
58
Steve McKay955e46d2016-01-05 12:53:35 -080059 mAdapter = new ModelBackedDocumentsAdapter(
Steve McKayef16f5f2015-12-22 18:15:31 -080060 env, new IconHelper(testContext, State.MODE_GRID));
Steve McKay955e46d2016-01-05 12:53:35 -080061 mAdapter.onModelUpdate(mModel);
Steve McKayef16f5f2015-12-22 18:15:31 -080062 }
63
64 // Tests that the item count is correct.
65 public void testItemCount() {
Steve McKay955e46d2016-01-05 12:53:35 -080066 assertEquals(mModel.getItemCount(), mAdapter.getItemCount());
Steve McKayef16f5f2015-12-22 18:15:31 -080067 }
68
69 // Tests that the item count is correct.
70 public void testHide_ItemCount() {
Tomasz Mikolajewskic05d98f2016-03-07 18:01:45 +090071 String[] ids = mModel.getModelIds();
72 mAdapter.hide(ids[0], ids[1]);
Steve McKay955e46d2016-01-05 12:53:35 -080073 assertEquals(mModel.getItemCount() - 2, mAdapter.getItemCount());
74 }
75
Steve McKayef16f5f2015-12-22 18:15:31 -080076 private final class TestEnvironment implements DocumentsAdapter.Environment {
77 private final Context testContext;
78
79 private TestEnvironment(Context testContext) {
80 this.testContext = testContext;
81 }
82
83 @Override
84 public boolean isSelected(String id) {
85 return false;
86 }
87
88 @Override
89 public boolean isDocumentEnabled(String mimeType, int flags) {
90 return true;
91 }
92
93 @Override
94 public void initDocumentHolder(DocumentHolder holder) {}
95
96 @Override
97 public Model getModel() {
Steve McKay955e46d2016-01-05 12:53:35 -080098 return mModel;
Steve McKayef16f5f2015-12-22 18:15:31 -080099 }
100
101 @Override
102 public State getDisplayState() {
103 return null;
104 }
105
106 @Override
107 public Context getContext() {
108 return testContext;
109 }
110
111 @Override
112 public int getColumnCount() {
113 return 4;
114 }
115
116 @Override
117 public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {}
118 }
119
120 private static class DummyListener implements Model.UpdateListener {
121 public void onModelUpdate(Model model) {}
122 public void onModelUpdateFailed(Exception e) {}
123 }
124
125 private static class DummyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
126 public int getItemCount() { return 0; }
127 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}
128 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
129 return null;
130 }
131 }
132}