Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.content.Context; |
Kelvin Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 22 | import android.net.Uri; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 23 | import android.os.UserHandle; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | |
| 27 | import androidx.test.InstrumentationRegistry; |
| 28 | |
Kelvin Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 29 | import com.android.documentsui.base.DocumentInfo; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 30 | import com.android.documentsui.base.RootInfo; |
| 31 | import com.android.documentsui.base.State; |
| 32 | import com.android.documentsui.base.UserId; |
| 33 | import com.android.documentsui.testing.TestEnv; |
| 34 | import com.android.documentsui.testing.TestProvidersAccess; |
| 35 | |
| 36 | import com.google.android.collect.Lists; |
| 37 | import com.google.android.material.tabs.TabLayout; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 42 | import java.util.Collections; |
Kelvin Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 43 | import java.util.List; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 44 | |
| 45 | public 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 Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 57 | private TestCommonAddons mTestCommonAddons; |
| 58 | private boolean mIsListenerInvoked; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 59 | |
| 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 Kwan | 218e63b | 2020-03-27 17:35:51 +0000 | [diff] [blame] | 68 | mState.supportsCrossProfile = true; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 69 | 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 Kershaw | 7f2d6dd | 2020-03-24 18:04:56 +0000 | [diff] [blame] | 75 | mTestEnv.isSearchExpanded = false; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 76 | |
| 77 | mTestUserIdManager = new TestUserIdManager(); |
Kelvin Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 78 | mTestCommonAddons = new TestCommonAddons(); |
| 79 | mTestCommonAddons.mCurrentRoot = TestProvidersAccess.DOWNLOADS; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 80 | } |
| 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 Kwan | 218e63b | 2020-03-27 17:35:51 +0000 | [diff] [blame] | 107 | public void testUpdateView_twoUsers_doesNotSupportCrossProfile_shouldHide() { |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 108 | initializeWithUsers(systemUser, managedUser); |
| 109 | |
Kelvin Kwan | 218e63b | 2020-03-27 17:35:51 +0000 | [diff] [blame] | 110 | mState.supportsCrossProfile = false; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 111 | 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 Kershaw | 7f2d6dd | 2020-03-24 18:04:56 +0000 | [diff] [blame] | 155 | mTestEnv.isSearchExpanded = true; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 156 | 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 Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 177 | 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 Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 194 | 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 Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 202 | 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 Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 215 | } |
| 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 Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 234 | mProfileTabs = new ProfileTabs(mTabLayout, mState, mTestUserIdManager, mTestEnv, |
| 235 | mTestCommonAddons); |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 236 | mProfileTabs.updateView(); |
Kelvin Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 237 | mProfileTabs.setListener(userId -> mIsListenerInvoked = true); |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * A test implementation of {@link NavigationViewManager.Environment}. |
| 242 | */ |
| 243 | private static class TestEnvironment implements NavigationViewManager.Environment { |
| 244 | |
Alex Kershaw | 7f2d6dd | 2020-03-24 18:04:56 +0000 | [diff] [blame] | 245 | public boolean isSearchExpanded = false; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 246 | |
| 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 Kershaw | 7f2d6dd | 2020-03-24 18:04:56 +0000 | [diff] [blame] | 264 | return isSearchExpanded; |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 265 | } |
Kelvin Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 266 | } |
Kelvin Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 267 | |
| 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 Zhu | 7da7b0f | 2020-02-18 17:15:28 -0800 | [diff] [blame] | 323 | public void setRootsDrawerLocked(boolean locked) { |
| 324 | throw new UnsupportedOperationException("not implemented"); |
| 325 | } |
| 326 | |
| 327 | @Override |
Kelvin Kwan | 339c7bc | 2020-03-02 14:14:06 +0000 | [diff] [blame] | 328 | 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 Kwan | 1b9f85b | 2020-02-13 15:22:38 +0000 | [diff] [blame] | 337 | } |
| 338 | |