Add a switchbar to turn off multi-user feature entirely
Change-Id: Ibf88bf37899af5065c68aeb2337acd4ee48bc13c Fixes: 72319180 Test: robotest on new controller. Manual test on UserSettings fragment.
This commit is contained in:
@@ -47,6 +47,8 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
|
||||
private final Set<Integer> mManagedProfiles = new HashSet<>();
|
||||
private boolean mIsQuietModeEnabled = false;
|
||||
private int[] profileIdsForUser;
|
||||
private boolean mUserSwitchEnabled;
|
||||
|
||||
|
||||
@Resetter
|
||||
public void reset() {
|
||||
@@ -56,6 +58,7 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
|
||||
mRestrictionSources.clear();
|
||||
mManagedProfiles.clear();
|
||||
mIsQuietModeEnabled = false;
|
||||
mUserSwitchEnabled = false;
|
||||
}
|
||||
|
||||
public void setUserInfo(int userHandle, UserInfo userInfo) {
|
||||
@@ -136,4 +139,13 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
|
||||
public void setProfileIdsWithDisabled(int[] profileIds) {
|
||||
profileIdsForUser = profileIds;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public boolean isUserSwitcherEnabled() {
|
||||
return mUserSwitchEnabled;
|
||||
}
|
||||
|
||||
public void setUserSwitcherEnabled(boolean userSwitchEnabled) {
|
||||
mUserSwitchEnabled = userSwitchEnabled;
|
||||
}
|
||||
}
|
||||
|
@@ -17,58 +17,59 @@ package com.android.settings.users;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import android.content.Context;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.UserManager;
|
||||
import android.provider.Settings.Global;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowUserManager.class})
|
||||
public class AddUserWhenLockedPreferenceControllerTest {
|
||||
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private UserInfo mUserInfo;
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private UserManager mUserManager;
|
||||
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private Lifecycle mLifecycle;
|
||||
private Context mContext;
|
||||
private ShadowUserManager mUserManager;
|
||||
private AddUserWhenLockedPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ShadowApplication shadowContext = ShadowApplication.getInstance();
|
||||
shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
|
||||
mUserManager = ShadowUserManager.getShadow();
|
||||
mContext = shadowContext.getApplicationContext();
|
||||
mLifecycleOwner = () -> mLifecycle;
|
||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||
mController = new AddUserWhenLockedPreferenceController(mContext, "fake_key", mLifecycle);
|
||||
mController = new AddUserWhenLockedPreferenceController(mContext, "fake_key");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mUserManager.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPref_NotAdmin_shouldNotDisplay() {
|
||||
when(mUserManager.getUserInfo(anyInt())).thenReturn(mUserInfo);
|
||||
mUserManager.setUserInfo(0, mUserInfo);
|
||||
when(mUserInfo.isAdmin()).thenReturn(false);
|
||||
final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class);
|
||||
when(preference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
|
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.users;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class MultiUserFooterPreferenceControllerTest {
|
||||
|
||||
private Context mContext;
|
||||
private MultiUserFooterPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = new MultiUserFooterPreferenceController(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_multiUserOff_shouldReturnEnabled() {
|
||||
mController.mUserCaps.mEnabled = true;
|
||||
mController.mUserCaps.mUserSwitcherEnabled = false;
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_multiUserOn_shouldReturnDisabled() {
|
||||
mController.mUserCaps.mEnabled = true;
|
||||
mController.mUserCaps.mUserSwitcherEnabled = true;
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
|
||||
}
|
||||
}
|
@@ -17,36 +17,43 @@
|
||||
package com.android.settings.users;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowUserManager.class})
|
||||
public class UserCapabilitiesTest {
|
||||
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
private ShadowUserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mUserManager = ShadowUserManager.getShadow();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mUserManager.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disallowUserSwitchWhenRestrictionIsSet() {
|
||||
when(mUserManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH)).thenReturn(true);
|
||||
public void disallowUserSwitch_restrictionIsSet_true() {
|
||||
mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
|
||||
UserManager.DISALLOW_USER_SWITCH, true);
|
||||
|
||||
UserCapabilities userCapabilities = UserCapabilities.create(mContext);
|
||||
userCapabilities.updateAddUserCapabilities(mContext);
|
||||
@@ -55,12 +62,33 @@ public class UserCapabilitiesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowUserSwitchWhenRestrictionIsNotSet() {
|
||||
when(mUserManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH)).thenReturn(false);
|
||||
public void disallowUserSwitch_restrictionIsNotSet_false() {
|
||||
mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
|
||||
UserManager.DISALLOW_USER_SWITCH, false);
|
||||
|
||||
UserCapabilities userCapabilities = UserCapabilities.create(mContext);
|
||||
userCapabilities.updateAddUserCapabilities(mContext);
|
||||
|
||||
assertThat(userCapabilities.mDisallowSwitchUser).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userSwitchEnabled_off() {
|
||||
mUserManager.setUserSwitcherEnabled(false);
|
||||
|
||||
final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
|
||||
userCapabilities.updateAddUserCapabilities(mContext);
|
||||
|
||||
assertThat(userCapabilities.mUserSwitcherEnabled).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userSwitchEnabled_on() {
|
||||
mUserManager.setUserSwitcherEnabled(true);
|
||||
|
||||
final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
|
||||
userCapabilities.updateAddUserCapabilities(mContext);
|
||||
|
||||
assertThat(userCapabilities.mUserSwitcherEnabled).isTrue();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user