Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui.dirlist; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.database.Cursor; |
| 21 | import android.support.v7.widget.RecyclerView; |
| 22 | import android.test.AndroidTestCase; |
| 23 | import android.test.suitebuilder.annotation.SmallTest; |
Steve McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 24 | import android.util.SparseArray; |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 25 | import android.view.ViewGroup; |
| 26 | |
| 27 | import com.android.documentsui.State; |
| 28 | |
| 29 | import java.util.List; |
| 30 | |
| 31 | @SmallTest |
| 32 | public 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 McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 48 | private TestModel mModel; |
| 49 | private ModelBackedDocumentsAdapter mAdapter; |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 50 | |
| 51 | public void setUp() { |
| 52 | |
| 53 | final Context testContext = TestContext.createStorageTestContext(getContext(), AUTHORITY); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 54 | mModel = new TestModel(AUTHORITY); |
Steve McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 55 | mModel.update(NAMES); |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 56 | |
| 57 | DocumentsAdapter.Environment env = new TestEnvironment(testContext); |
| 58 | |
Steve McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 59 | mAdapter = new ModelBackedDocumentsAdapter( |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 60 | env, new IconHelper(testContext, State.MODE_GRID)); |
Steve McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 61 | mAdapter.onModelUpdate(mModel); |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Tests that the item count is correct. |
| 65 | public void testItemCount() { |
Steve McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 66 | assertEquals(mModel.getItemCount(), mAdapter.getItemCount()); |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Tests that the item count is correct. |
| 70 | public void testHide_ItemCount() { |
Tomasz Mikolajewski | c05d98f | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 71 | String[] ids = mModel.getModelIds(); |
| 72 | mAdapter.hide(ids[0], ids[1]); |
Steve McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 73 | assertEquals(mModel.getItemCount() - 2, mAdapter.getItemCount()); |
| 74 | } |
| 75 | |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 76 | 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 McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 98 | return mModel; |
Steve McKay | ef16f5f | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 99 | } |
| 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 | } |