1 /*
2  * Copyright (C) 2018 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.queries;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.provider.DocumentsContract;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.LinearLayout;
29 
30 import androidx.test.filters.SmallTest;
31 import androidx.test.platform.app.InstrumentationRegistry;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import com.android.documentsui.R;
35 import com.android.documentsui.base.MimeTypes;
36 import com.android.documentsui.base.Shared;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.Set;
45 
46 @RunWith(AndroidJUnit4.class)
47 @SmallTest
48 public final class SearchChipViewManagerTest {
49 
50     private static final String LARGE_FILES_CHIP_MIME_TYPE = "";
51     private static final String FROM_THIS_WEEK_CHIP_MIME_TYPE = "";
52     private static final String[] TEST_MIME_TYPES =
53             new String[]{"image/*", "video/*"};
54     private static final String[] TEST_OTHER_TYPES =
55             new String[]{LARGE_FILES_CHIP_MIME_TYPE, FROM_THIS_WEEK_CHIP_MIME_TYPE};
56     private static int CHIP_TYPE = 1000;
57 
58     private Context mContext;
59     private SearchChipViewManager mSearchChipViewManager;
60     private LinearLayout mChipGroup;
61 
62     @Before
setUp()63     public void setUp() {
64         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
65         mContext.setTheme(com.android.documentsui.R.style.DocumentsTheme);
66         mContext.getTheme().applyStyle(R.style.DocumentsDefaultTheme, false);
67         mChipGroup = spy(new LinearLayout(mContext));
68 
69         mSearchChipViewManager = new SearchChipViewManager(mChipGroup);
70         mSearchChipViewManager.initChipSets(new String[] {"*/*"});
71     }
72 
73     @Test
testInitChipSets_hasCorrectChipCount()74     public void testInitChipSets_hasCorrectChipCount() throws Exception {
75         mSearchChipViewManager.initChipSets(TEST_MIME_TYPES);
76         mSearchChipViewManager.updateChips(new String[] {"*/*"});
77 
78         int totalChipLength = TEST_MIME_TYPES.length + TEST_OTHER_TYPES.length;
79         assertThat(mChipGroup.getChildCount()).isEqualTo(totalChipLength);
80     }
81 
82     @Test
testUpdateChips_hasCorrectChipCount()83     public void testUpdateChips_hasCorrectChipCount() throws Exception {
84         mSearchChipViewManager.updateChips(TEST_MIME_TYPES);
85 
86         int totalChipLength = TEST_MIME_TYPES.length + TEST_OTHER_TYPES.length;
87         assertThat(mChipGroup.getChildCount()).isEqualTo(totalChipLength);
88     }
89 
90     @Test
testUpdateChips_withSingleMimeType_hasCorrectChipCount()91     public void testUpdateChips_withSingleMimeType_hasCorrectChipCount() throws Exception {
92         mSearchChipViewManager.updateChips(new String[]{"image/*"});
93 
94         assertThat(mChipGroup.getChildCount()).isEqualTo(TEST_OTHER_TYPES.length);
95     }
96 
97     @Test
testGetCheckedChipMimeTypes_hasCorrectValue()98     public void testGetCheckedChipMimeTypes_hasCorrectValue() throws Exception {
99         mSearchChipViewManager.mCheckedChipItems = getFakeSearchChipDataList();
100 
101         String[] checkedMimeTypes =
102                 mSearchChipViewManager.getCheckedChipQueryArgs()
103                         .getStringArray(DocumentsContract.QUERY_ARG_MIME_TYPES);
104 
105         assertThat(MimeTypes.mimeMatches(TEST_MIME_TYPES, checkedMimeTypes[0])).isTrue();
106         assertThat(MimeTypes.mimeMatches(TEST_MIME_TYPES, checkedMimeTypes[1])).isTrue();
107     }
108 
109     @Test
testRestoreCheckedChipItems_hasCorrectValue()110     public void testRestoreCheckedChipItems_hasCorrectValue() throws Exception {
111         Bundle bundle = new Bundle();
112         bundle.putIntArray(Shared.EXTRA_QUERY_CHIPS, new int[]{2});
113 
114         mSearchChipViewManager.restoreCheckedChipItems(bundle);
115 
116         assertThat(mSearchChipViewManager.mCheckedChipItems.size()).isEqualTo(1);
117         Iterator<SearchChipData> iterator = mSearchChipViewManager.mCheckedChipItems.iterator();
118         assertThat(iterator.next().getChipType()).isEqualTo(2);
119     }
120 
121     @Test
testSaveInstanceState_hasCorrectValue()122     public void testSaveInstanceState_hasCorrectValue() throws Exception {
123         mSearchChipViewManager.mCheckedChipItems = getFakeSearchChipDataList();
124         Bundle bundle = new Bundle();
125 
126         mSearchChipViewManager.onSaveInstanceState(bundle);
127 
128         final int[] chipTypes = bundle.getIntArray(Shared.EXTRA_QUERY_CHIPS);
129         assertThat(chipTypes.length).isEqualTo(1);
130         assertThat(chipTypes[0]).isEqualTo(CHIP_TYPE);
131     }
132 
133     @Test
testBindMirrorGroup_sameValue()134     public void testBindMirrorGroup_sameValue() throws Exception {
135         mSearchChipViewManager.updateChips(new String[] {"*/*"});
136 
137         ViewGroup mirror = spy(new LinearLayout(mContext));
138         mSearchChipViewManager.bindMirrorGroup(mirror);
139 
140         assertThat(View.VISIBLE).isEqualTo(mirror.getVisibility());
141         assertThat(mChipGroup.getChildCount()).isEqualTo(mirror.getChildCount());
142         assertThat(mChipGroup.getChildAt(0).getTag()).isEqualTo(mirror.getChildAt(0).getTag());
143     }
144 
145     @Test
testBindMirrorGroup_showRow()146     public void testBindMirrorGroup_showRow() throws Exception {
147         mSearchChipViewManager.updateChips(new String[] {"image/*"});
148 
149         ViewGroup mirror = spy(new LinearLayout(mContext));
150         mSearchChipViewManager.bindMirrorGroup(mirror);
151 
152         assertThat(View.VISIBLE).isEqualTo(mirror.getVisibility());
153     }
154 
getFakeSearchChipDataList()155     private static Set<SearchChipData> getFakeSearchChipDataList() {
156         final Set<SearchChipData> chipDataList = new HashSet<>();
157         chipDataList.add(new SearchChipData(CHIP_TYPE, 0 /* titleRes */, TEST_MIME_TYPES));
158         return chipDataList;
159     }
160 }
161