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.sidebar;
18 
19 import static junit.framework.Assert.assertTrue;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.admin.DevicePolicyManager;
26 import android.content.Context;
27 import android.content.pm.ResolveInfo;
28 
29 import androidx.test.filters.MediumTest;
30 import androidx.test.platform.app.InstrumentationRegistry;
31 
32 import com.android.documentsui.TestConfigStore;
33 import com.android.documentsui.TestUserManagerState;
34 import com.android.documentsui.base.RootInfo;
35 import com.android.documentsui.base.UserId;
36 import com.android.documentsui.testing.TestEnv;
37 import com.android.documentsui.testing.TestProvidersAccess;
38 import com.android.documentsui.testing.TestResolveInfo;
39 import com.android.modules.utils.build.SdkLevel;
40 
41 import com.google.android.collect.Lists;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.junit.runners.Parameterized;
47 import org.junit.runners.Parameterized.Parameter;
48 import org.junit.runners.Parameterized.Parameters;
49 
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.List;
53 
54 /**
55  * A unit test for RootsFragment.
56  */
57 @RunWith(Parameterized.class)
58 @MediumTest
59 public class RootsFragmentTest {
60 
61     private Context mContext;
62     private DevicePolicyManager mDevicePolicyManager;
63     private RootsFragment mRootsFragment;
64     private TestEnv mEnv;
65     private final TestConfigStore mTestConfigStore = new TestConfigStore();
66     private TestUserManagerState mTestUserManagerState;
67 
68     private static final String[] EXPECTED_SORTED_RESULT = {
69             TestProvidersAccess.RECENTS.title,
70             TestProvidersAccess.IMAGE.title,
71             TestProvidersAccess.VIDEO.title,
72             TestProvidersAccess.AUDIO.title,
73             TestProvidersAccess.DOCUMENT.title,
74             TestProvidersAccess.DOWNLOADS.title,
75             "" /* SpacerItem */,
76             TestProvidersAccess.EXTERNALSTORAGE.title,
77             TestProvidersAccess.HAMMY.title,
78             "" /* SpacerItem */,
79             TestProvidersAccess.INSPECTOR.title,
80             TestProvidersAccess.PICKLES.title};
81 
82     @Parameter(0)
83     public boolean isPrivateSpaceEnabled;
84 
85     /**
86      * Parametrize values for {@code isPrivateSpaceEnabled} to run all the tests twice once with
87      * private space flag enabled and once with it disabled.
88      */
89     @Parameters(name = "privateSpaceEnabled={0}")
data()90     public static Iterable<?> data() {
91         return Lists.newArrayList(true, false);
92     }
93 
94     @Before
setUp()95     public void setUp() {
96         mEnv = TestEnv.create();
97         mEnv.state.configStore = mTestConfigStore;
98 
99         mContext = mock(Context.class);
100         mDevicePolicyManager = mock(DevicePolicyManager.class);
101         when(mContext.getResources()).thenReturn(
102                 InstrumentationRegistry.getInstrumentation().getTargetContext().getResources());
103         when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
104                 .thenReturn(mDevicePolicyManager);
105         when(mContext.getApplicationContext()).thenReturn(
106                 InstrumentationRegistry.getInstrumentation().getTargetContext());
107 
108         if (SdkLevel.isAtLeastS() && isPrivateSpaceEnabled) {
109             mTestConfigStore.enablePrivateSpaceInPhotoPicker();
110             mTestUserManagerState = new TestUserManagerState();
111             mTestUserManagerState.canFrowardToProfileIdMap.put(UserId.DEFAULT_USER, true);
112         }
113         isPrivateSpaceEnabled = SdkLevel.isAtLeastS() && isPrivateSpaceEnabled;
114         mRootsFragment = new RootsFragment();
115     }
116 
117     @Test
testSortLoadResult_WithCorrectOrder()118     public void testSortLoadResult_WithCorrectOrder() {
119         List<Item> items = mRootsFragment.sortLoadResult(
120                 mContext,
121                 mEnv.state,
122                 createFakeRootInfoList(),
123                 null /* excludePackage */, null /* handlerAppIntent */, new TestProvidersAccess(),
124                 UserId.DEFAULT_USER,
125                 Collections.singletonList(UserId.DEFAULT_USER),
126                 /* maybeShowBadge */ false, mTestUserManagerState);
127         assertTrue(assertSortedResult(items));
128     }
129 
130     @Test
testItemComparator_WithCorrectOrder()131     public void testItemComparator_WithCorrectOrder() {
132         final String testPackageName = "com.test1";
133         final String errorTestPackageName = "com.test2";
134         final RootsFragment.ItemComparator comp = new RootsFragment.ItemComparator(testPackageName);
135         final List<Item> rootList = new ArrayList<>();
136         rootList.add(new RootItem(TestProvidersAccess.HAMMY, null /* actionHandler */,
137                 errorTestPackageName, /* maybeShowBadge= */ false));
138         rootList.add(new RootItem(TestProvidersAccess.INSPECTOR, null /* actionHandler */,
139                 errorTestPackageName, /* maybeShowBadge= */ false));
140         rootList.add(new RootItem(TestProvidersAccess.PICKLES, null /* actionHandler */,
141                 testPackageName, /* maybeShowBadge= */ false));
142         Collections.sort(rootList, comp);
143 
144         assertEquals(rootList.get(0).title, TestProvidersAccess.PICKLES.title);
145         assertEquals(rootList.get(1).title, TestProvidersAccess.HAMMY.title);
146         assertEquals(rootList.get(2).title, TestProvidersAccess.INSPECTOR.title);
147     }
148 
149     @Test
testItemComparator_differentItemTypes_WithCorrectOrder()150     public void testItemComparator_differentItemTypes_WithCorrectOrder() {
151         final String testPackageName = "com.test1";
152         final RootsFragment.ItemComparator comp = new RootsFragment.ItemComparator(testPackageName);
153         final List<Item> rootList = new ArrayList<>();
154         rootList.add(new RootItem(TestProvidersAccess.HAMMY, null /* actionHandler */,
155                 testPackageName, /* maybeShowBadge= */ false));
156 
157         final ResolveInfo info = TestResolveInfo.create();
158         info.activityInfo.packageName = testPackageName;
159 
160         rootList.add(new AppItem(info, TestProvidersAccess.PICKLES.title, UserId.DEFAULT_USER,
161                 null /* actionHandler */));
162         rootList.add(new RootAndAppItem(TestProvidersAccess.INSPECTOR, info,
163                 null /* actionHandler */, /* maybeShowBadge= */ false));
164 
165         Collections.sort(rootList, comp);
166 
167         assertEquals(rootList.get(0).title, TestProvidersAccess.HAMMY.title);
168         assertEquals(rootList.get(1).title, TestProvidersAccess.INSPECTOR.title);
169         assertEquals(rootList.get(2).title, TestProvidersAccess.PICKLES.title);
170     }
171 
assertSortedResult(List<Item> items)172     private boolean assertSortedResult(List<Item> items) {
173         for (int i = 0; i < items.size(); i++) {
174             Item item = items.get(i);
175             if (item instanceof RootItem) {
176                 assertEquals(EXPECTED_SORTED_RESULT[i], ((RootItem) item).root.title);
177             } else if (item instanceof SpacerItem) {
178                 assertTrue(EXPECTED_SORTED_RESULT[i].isEmpty());
179             } else {
180                 return false;
181             }
182         }
183         return true;
184     }
185 
createFakeRootInfoList()186     private List<RootInfo> createFakeRootInfoList() {
187         final List<RootInfo> fakeRootInfoList = new ArrayList<>();
188         fakeRootInfoList.add(TestProvidersAccess.PICKLES);
189         fakeRootInfoList.add(TestProvidersAccess.HAMMY);
190         fakeRootInfoList.add(TestProvidersAccess.INSPECTOR);
191         fakeRootInfoList.add(TestProvidersAccess.DOWNLOADS);
192         fakeRootInfoList.add(TestProvidersAccess.AUDIO);
193         fakeRootInfoList.add(TestProvidersAccess.VIDEO);
194         fakeRootInfoList.add(TestProvidersAccess.RECENTS);
195         fakeRootInfoList.add(TestProvidersAccess.IMAGE);
196         fakeRootInfoList.add(TestProvidersAccess.EXTERNALSTORAGE);
197         fakeRootInfoList.add(TestProvidersAccess.DOCUMENT);
198         return fakeRootInfoList;
199     }
200 }
201