blob: dd6ef3c81a78ba24547f41741489853647aab351 [file] [log] [blame]
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +00001/*
2 * Copyright (C) 2020 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;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.content.Context;
Kelvin Kwan339c7bc2020-03-02 14:14:06 +000022import android.net.Uri;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000023import android.os.UserHandle;
24import android.view.LayoutInflater;
25import android.view.View;
26
27import androidx.test.InstrumentationRegistry;
28
Kelvin Kwan339c7bc2020-03-02 14:14:06 +000029import com.android.documentsui.base.DocumentInfo;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000030import com.android.documentsui.base.RootInfo;
31import com.android.documentsui.base.State;
32import com.android.documentsui.base.UserId;
33import com.android.documentsui.testing.TestEnv;
34import com.android.documentsui.testing.TestProvidersAccess;
35
36import com.google.android.collect.Lists;
37import com.google.android.material.tabs.TabLayout;
38
39import org.junit.Before;
40import org.junit.Test;
41
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000042import java.util.Collections;
Kelvin Kwan339c7bc2020-03-02 14:14:06 +000043import java.util.List;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000044
45public class ProfileTabsTest {
46
47 private final UserId systemUser = UserId.of(UserHandle.SYSTEM);
48 private final UserId managedUser = UserId.of(100);
49
50 private ProfileTabs mProfileTabs;
51
52 private Context mContext;
53 private TabLayout mTabLayout;
54 private TestEnvironment mTestEnv;
55 private State mState;
56 private TestUserIdManager mTestUserIdManager;
Kelvin Kwan339c7bc2020-03-02 14:14:06 +000057 private TestCommonAddons mTestCommonAddons;
58 private boolean mIsListenerInvoked;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000059
60 @Before
61 public void setUp() {
62 TestEnv.create();
63 mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
64 mContext.setTheme(R.style.DocumentsTheme);
65 mContext.getTheme().applyStyle(R.style.DocumentsDefaultTheme, false);
66 LayoutInflater layoutInflater = LayoutInflater.from(mContext);
67 mState = new State();
Kelvin Kwan218e63b2020-03-27 17:35:51 +000068 mState.supportsCrossProfile = true;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000069 mState.stack.changeRoot(TestProvidersAccess.DOWNLOADS);
70 mState.stack.push(TestEnv.FOLDER_0);
71 View view = layoutInflater.inflate(R.layout.directory_header, null);
72
73 mTabLayout = view.findViewById(R.id.tabs);
74 mTestEnv = new TestEnvironment();
Alex Kershaw7f2d6dd2020-03-24 18:04:56 +000075 mTestEnv.isSearchExpanded = false;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000076
77 mTestUserIdManager = new TestUserIdManager();
Kelvin Kwan339c7bc2020-03-02 14:14:06 +000078 mTestCommonAddons = new TestCommonAddons();
79 mTestCommonAddons.mCurrentRoot = TestProvidersAccess.DOWNLOADS;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +000080 }
81
82 @Test
83 public void testUpdateView_singleUser_shouldHide() {
84 initializeWithUsers(systemUser);
85
86 assertThat(mTabLayout.getVisibility()).isEqualTo(View.GONE);
87 assertThat(mTabLayout.getTabCount()).isEqualTo(0);
88 }
89
90 @Test
91 public void testUpdateView_twoUsers_shouldShow() {
92 initializeWithUsers(systemUser, managedUser);
93
94 assertThat(mTabLayout.getVisibility()).isEqualTo(View.VISIBLE);
95 assertThat(mTabLayout.getTabCount()).isEqualTo(2);
96
97 TabLayout.Tab tab1 = mTabLayout.getTabAt(0);
98 assertThat(tab1.getTag()).isEqualTo(systemUser);
99 assertThat(tab1.getText()).isEqualTo(mContext.getString(R.string.personal_tab));
100
101 TabLayout.Tab tab2 = mTabLayout.getTabAt(1);
102 assertThat(tab2.getTag()).isEqualTo(managedUser);
103 assertThat(tab2.getText()).isEqualTo(mContext.getString(R.string.work_tab));
104 }
105
106 @Test
Kelvin Kwan218e63b2020-03-27 17:35:51 +0000107 public void testUpdateView_twoUsers_doesNotSupportCrossProfile_shouldHide() {
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000108 initializeWithUsers(systemUser, managedUser);
109
Kelvin Kwan218e63b2020-03-27 17:35:51 +0000110 mState.supportsCrossProfile = false;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000111 mProfileTabs.updateView();
112
113 assertThat(mTabLayout.getVisibility()).isEqualTo(View.GONE);
114 }
115
116 @Test
117 public void testUpdateView_twoUsers_subFolder_shouldHide() {
118 initializeWithUsers(systemUser, managedUser);
119
120 // Push 1 more folder. Now the stack has size of 2.
121 mState.stack.push(TestEnv.FOLDER_1);
122
123 mProfileTabs.updateView();
124 assertThat(mTabLayout.getVisibility()).isEqualTo(View.GONE);
125 assertThat(mTabLayout.getTabCount()).isEqualTo(2);
126 }
127
128 @Test
129 public void testUpdateView_twoUsers_recents_subFolder_shouldHide() {
130 initializeWithUsers(systemUser, managedUser);
131
132 mState.stack.changeRoot(TestProvidersAccess.RECENTS);
133 // This(stack of size 2 in Recents) may not happen in real world.
134 mState.stack.push((TestEnv.FOLDER_0));
135
136 mProfileTabs.updateView();
137 assertThat(mTabLayout.getVisibility()).isEqualTo(View.GONE);
138 assertThat(mTabLayout.getTabCount()).isEqualTo(2);
139 }
140
141 @Test
142 public void testUpdateView_twoUsers_thirdParty_shouldHide() {
143 initializeWithUsers(systemUser, managedUser);
144
145 mState.stack.changeRoot(TestProvidersAccess.PICKLES);
146 mState.stack.push((TestEnv.FOLDER_0));
147
148 mProfileTabs.updateView();
149 assertThat(mTabLayout.getVisibility()).isEqualTo(View.GONE);
150 assertThat(mTabLayout.getTabCount()).isEqualTo(2);
151 }
152
153 @Test
154 public void testUpdateView_twoUsers_isSearching_shouldHide() {
Alex Kershaw7f2d6dd2020-03-24 18:04:56 +0000155 mTestEnv.isSearchExpanded = true;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000156 initializeWithUsers(systemUser, managedUser);
157
158 assertThat(mTabLayout.getVisibility()).isEqualTo(View.GONE);
159 assertThat(mTabLayout.getTabCount()).isEqualTo(2);
160 }
161
162 @Test
163 public void testUpdateView_getSelectedUser_afterUsersChanged() {
164 initializeWithUsers(systemUser, managedUser);
165 mProfileTabs.updateView();
166 mTabLayout.selectTab(mTabLayout.getTabAt(1));
167 assertThat(mTabLayout.getVisibility()).isEqualTo(View.VISIBLE);
168 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(managedUser);
169
170 mTestUserIdManager.userIds = Collections.singletonList(systemUser);
171 mProfileTabs.updateView();
172 assertThat(mTabLayout.getVisibility()).isEqualTo(View.GONE);
173 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(systemUser);
174 }
175
176 @Test
Kelvin Kwan339c7bc2020-03-02 14:14:06 +0000177 public void testUpdateView_afterCurrentRootChanged_shouldChangeSelectedUser() {
178 initializeWithUsers(systemUser, managedUser);
179 mProfileTabs.updateView();
180
181 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(systemUser);
182
183 RootInfo newRoot = RootInfo.copyRootInfo(mTestCommonAddons.mCurrentRoot);
184 newRoot.userId = managedUser;
185 mTestCommonAddons.mCurrentRoot = newRoot;
186 mProfileTabs.updateView();
187
188 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(managedUser);
189 // updating view should not trigger listener callback.
190 assertThat(mIsListenerInvoked).isFalse();
191 }
192
193 @Test
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000194 public void testgetSelectedUser_twoUsers() {
195 initializeWithUsers(systemUser, managedUser);
196
197 mTabLayout.selectTab(mTabLayout.getTabAt(0));
198 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(systemUser);
199
200 mTabLayout.selectTab(mTabLayout.getTabAt(1));
201 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(managedUser);
Kelvin Kwan339c7bc2020-03-02 14:14:06 +0000202 assertThat(mIsListenerInvoked).isTrue();
203 }
204
205 @Test
206 public void testReselectedUser_doesNotInvokeListener() {
207 initializeWithUsers(systemUser, managedUser);
208
209 assertThat(mTabLayout.getSelectedTabPosition()).isAtLeast(0);
210 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(systemUser);
211
212 mTabLayout.selectTab(mTabLayout.getTabAt(0));
213 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(systemUser);
214 assertThat(mIsListenerInvoked).isFalse();
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000215 }
216
217 @Test
218 public void testgetSelectedUser_singleUsers() {
219 initializeWithUsers(systemUser);
220
221 assertThat(mProfileTabs.getSelectedUser()).isEqualTo(systemUser);
222 }
223
224 private void initializeWithUsers(UserId... userIds) {
225 mTestUserIdManager.userIds = Lists.newArrayList(userIds);
226 for (UserId userId : userIds) {
227 if (userId.isSystem()) {
228 mTestUserIdManager.systemUser = userId;
229 } else {
230 mTestUserIdManager.managedUser = userId;
231 }
232 }
233
Kelvin Kwan339c7bc2020-03-02 14:14:06 +0000234 mProfileTabs = new ProfileTabs(mTabLayout, mState, mTestUserIdManager, mTestEnv,
235 mTestCommonAddons);
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000236 mProfileTabs.updateView();
Kelvin Kwan339c7bc2020-03-02 14:14:06 +0000237 mProfileTabs.setListener(userId -> mIsListenerInvoked = true);
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000238 }
239
240 /**
241 * A test implementation of {@link NavigationViewManager.Environment}.
242 */
243 private static class TestEnvironment implements NavigationViewManager.Environment {
244
Alex Kershaw7f2d6dd2020-03-24 18:04:56 +0000245 public boolean isSearchExpanded = false;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000246
247 @Override
248 public RootInfo getCurrentRoot() {
249 throw new UnsupportedOperationException("not implemented");
250 }
251
252 @Override
253 public String getDrawerTitle() {
254 throw new UnsupportedOperationException("not implemented");
255 }
256
257 @Override
258 public void refreshCurrentRootAndDirectory(int animation) {
259 throw new UnsupportedOperationException("not implemented");
260 }
261
262 @Override
263 public boolean isSearchExpanded() {
Alex Kershaw7f2d6dd2020-03-24 18:04:56 +0000264 return isSearchExpanded;
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000265 }
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000266 }
Kelvin Kwan339c7bc2020-03-02 14:14:06 +0000267
268 private static class TestCommonAddons implements AbstractActionHandler.CommonAddons {
269
270 private RootInfo mCurrentRoot;
271
272 @Override
273 public void restoreRootAndDirectory() {
274 throw new UnsupportedOperationException("not implemented");
275 }
276
277 @Override
278 public void refreshCurrentRootAndDirectory(int anim) {
279 throw new UnsupportedOperationException("not implemented");
280 }
281
282 @Override
283 public void onRootPicked(RootInfo root) {
284 throw new UnsupportedOperationException("not implemented");
285 }
286
287 @Override
288 public void onDocumentsPicked(List<DocumentInfo> docs) {
289 throw new UnsupportedOperationException("not implemented");
290 }
291
292 @Override
293 public void onDocumentPicked(DocumentInfo doc) {
294 throw new UnsupportedOperationException("not implemented");
295 }
296
297 @Override
298 public RootInfo getCurrentRoot() {
299 return mCurrentRoot;
300 }
301
302 @Override
303 public DocumentInfo getCurrentDirectory() {
304 throw new UnsupportedOperationException("not implemented");
305 }
306
307 @Override
308 public UserId getSelectedUser() {
309 throw new UnsupportedOperationException("not implemented");
310 }
311
312 @Override
313 public boolean isInRecents() {
314 throw new UnsupportedOperationException("not implemented");
315 }
316
317 @Override
318 public void setRootsDrawerOpen(boolean open) {
319 throw new UnsupportedOperationException("not implemented");
320 }
321
322 @Override
Zemiao Zhu7da7b0f2020-02-18 17:15:28 -0800323 public void setRootsDrawerLocked(boolean locked) {
324 throw new UnsupportedOperationException("not implemented");
325 }
326
327 @Override
Kelvin Kwan339c7bc2020-03-02 14:14:06 +0000328 public void updateNavigator() {
329 throw new UnsupportedOperationException("not implemented");
330 }
331
332 @Override
333 public void notifyDirectoryNavigated(Uri docUri) {
334 throw new UnsupportedOperationException("not implemented");
335 }
336 }
Kelvin Kwan1b9f85b2020-02-13 15:22:38 +0000337}
338