Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 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.car.settings.users; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.when; |
| 22 | |
| 23 | import android.car.userlib.CarUserManagerHelper; |
| 24 | import android.content.pm.UserInfo; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 25 | import android.os.Process; |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 27 | import android.os.UserManager; |
| 28 | import android.view.View; |
| 29 | import android.widget.Button; |
| 30 | |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 31 | import com.android.car.settings.R; |
kwaky | fc2851b | 2019-03-01 14:56:56 -0800 | [diff] [blame] | 32 | import com.android.car.settings.common.ConfirmationDialogFragment; |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 33 | import com.android.car.settings.testutils.BaseTestActivity; |
| 34 | import com.android.car.settings.testutils.ShadowCarUserManagerHelper; |
| 35 | import com.android.car.settings.testutils.ShadowUserIconProvider; |
| 36 | import com.android.car.settings.testutils.ShadowUserManager; |
| 37 | |
| 38 | import org.junit.After; |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | import org.robolectric.Robolectric; |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 45 | import org.robolectric.RobolectricTestRunner; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 46 | import org.robolectric.RuntimeEnvironment; |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 47 | import org.robolectric.annotation.Config; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 48 | import org.robolectric.shadow.api.Shadow; |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 49 | |
| 50 | import java.util.Arrays; |
| 51 | |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 52 | @RunWith(RobolectricTestRunner.class) |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 53 | @Config(shadows = {ShadowUserManager.class, ShadowCarUserManagerHelper.class, |
| 54 | ShadowUserIconProvider.class}) |
| 55 | public class UserDetailsBaseFragmentTest { |
| 56 | |
| 57 | /* |
| 58 | * This class needs to be public and static in order for it to be recreated from instance |
| 59 | * state if necessary. |
| 60 | */ |
| 61 | public static class TestUserDetailsBaseFragment extends UserDetailsBaseFragment { |
| 62 | |
| 63 | @Override |
| 64 | protected String getTitleText() { |
| 65 | return "test_title"; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected int getPreferenceScreenResId() { |
Heemin Seog | 8a470d7 | 2018-12-12 16:19:03 -0800 | [diff] [blame] | 70 | return R.xml.test_user_details_base_fragment; |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | private BaseTestActivity mTestActivity; |
| 75 | private UserDetailsBaseFragment mUserDetailsBaseFragment; |
| 76 | @Mock |
| 77 | private CarUserManagerHelper mCarUserManagerHelper; |
| 78 | @Mock |
| 79 | private UserManager mUserManager; |
| 80 | |
| 81 | private Button mRemoveUserButton; |
| 82 | |
| 83 | @Before |
| 84 | public void setUpTestActivity() { |
| 85 | MockitoAnnotations.initMocks(this); |
| 86 | ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper); |
| 87 | ShadowUserManager.setInstance(mUserManager); |
| 88 | |
| 89 | mTestActivity = Robolectric.setupActivity(BaseTestActivity.class); |
| 90 | } |
| 91 | |
| 92 | @After |
| 93 | public void tearDown() { |
| 94 | ShadowCarUserManagerHelper.reset(); |
| 95 | ShadowUserManager.reset(); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testRemoveUserButtonVisible_whenAllowedToRemoveUsers() { |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 100 | getShadowUserManager().setUserRestriction( |
| 101 | Process.myUserHandle(), UserManager.DISALLOW_REMOVE_USER, false); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 102 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false); |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 103 | createUserDetailsBaseFragment(/*userId=*/1); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 104 | |
| 105 | assertThat(mRemoveUserButton.getVisibility()).isEqualTo(View.VISIBLE); |
| 106 | } |
| 107 | |
| 108 | @Test |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 109 | public void testRemoveUserButtonHidden_whenNotAllowedToRemoveUsers() { |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 110 | getShadowUserManager().setUserRestriction( |
| 111 | Process.myUserHandle(), UserManager.DISALLOW_REMOVE_USER, true); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 112 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false); |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 113 | createUserDetailsBaseFragment(/*userId=*/1); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 114 | |
| 115 | assertThat(mRemoveUserButton.getVisibility()).isEqualTo(View.GONE); |
| 116 | } |
| 117 | |
| 118 | @Test |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 119 | public void testRemoveUserButtonHidden_whenUserIsSystemUser() { |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 120 | getShadowUserManager().setUserRestriction( |
| 121 | Process.myUserHandle(), UserManager.DISALLOW_REMOVE_USER, false); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 122 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false); |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 123 | createUserDetailsBaseFragment(UserHandle.USER_SYSTEM); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 124 | |
| 125 | assertThat(mRemoveUserButton.getVisibility()).isEqualTo(View.GONE); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testRemoveUserButtonHidden_demoUser() { |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 130 | getShadowUserManager().setUserRestriction( |
| 131 | Process.myUserHandle(), UserManager.DISALLOW_REMOVE_USER, false); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 132 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(true); |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 133 | createUserDetailsBaseFragment(/*userId=*/1); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 134 | |
| 135 | assertThat(mRemoveUserButton.getVisibility()).isEqualTo(View.GONE); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testRemoveUserButtonClick_createsRemovalDialog() { |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 140 | getShadowUserManager().setUserRestriction( |
| 141 | Process.myUserHandle(), UserManager.DISALLOW_REMOVE_USER, false); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 142 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false); |
| 143 | when(mCarUserManagerHelper.getAllPersistentUsers()).thenReturn( |
| 144 | Arrays.asList(new UserInfo())); |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 145 | createUserDetailsBaseFragment(/*userId=*/1); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 146 | mRemoveUserButton.performClick(); |
| 147 | |
kwaky | fc2851b | 2019-03-01 14:56:56 -0800 | [diff] [blame] | 148 | assertThat(mUserDetailsBaseFragment.findDialogByTag( |
| 149 | ConfirmationDialogFragment.TAG)).isNotNull(); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 152 | private void createUserDetailsBaseFragment(int userId) { |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 153 | UserInfo testUser = new UserInfo(); |
Anthony Hugh | 095c352 | 2019-07-08 15:13:28 -0700 | [diff] [blame] | 154 | testUser.id = userId; |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 155 | // Use UserDetailsFragment, since we cannot test an abstract class. |
| 156 | mUserDetailsBaseFragment = UserDetailsBaseFragment.addUserIdToFragmentArguments( |
| 157 | new TestUserDetailsBaseFragment(), testUser.id); |
| 158 | when(mUserManager.getUserInfo(testUser.id)).thenReturn(testUser); |
| 159 | mTestActivity.launchFragment(mUserDetailsBaseFragment); |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 160 | mRemoveUserButton = (Button) mTestActivity.findViewById(R.id.action_button1); |
| 161 | } |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame^] | 162 | |
| 163 | private ShadowUserManager getShadowUserManager() { |
| 164 | return Shadow.extract(RuntimeEnvironment.application.getSystemService(UserManager.class)); |
| 165 | } |
Heemin Seog | 9d5e907 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 166 | } |