1 /* 2 * Copyright (C) 2019 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.settings.users; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.never; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 import android.content.pm.UserInfo; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 29 import com.android.settings.testutils.shadow.ShadowUserManager; 30 import com.android.settings.widget.SwitchWidgetController; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.annotation.Config; 39 40 @RunWith(RobolectricTestRunner.class) 41 @Config(shadows = {ShadowUserManager.class}) 42 public class MultiUserSwitchBarControllerTest { 43 44 private Context mContext; 45 private ShadowUserManager mUserManager; 46 private SwitchWidgetController mSwitchWidgetController; 47 48 49 @Before setUp()50 public void setUp() { 51 mContext = RuntimeEnvironment.application; 52 mUserManager = ShadowUserManager.getShadow(); 53 mSwitchWidgetController = mock(SwitchWidgetController.class); 54 mUserManager.setSupportsMultipleUsers(true); 55 } 56 57 @After tearDown()58 public void tearDown() { 59 ShadowUserManager.reset(); 60 } 61 62 @Test onStart_disallowUserSwitch_shouldSetDisabledByAdmin()63 public void onStart_disallowUserSwitch_shouldSetDisabledByAdmin() { 64 mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), 65 UserManager.DISALLOW_USER_SWITCH, true); 66 67 final MultiUserSwitchBarController controller = new MultiUserSwitchBarController(mContext, 68 mSwitchWidgetController, null); 69 70 verify(mSwitchWidgetController).setDisabledByAdmin(any()); 71 } 72 73 @Test onStart_allowUserSwitch_shouldNotSetDisabledByAdmin()74 public void onStart_allowUserSwitch_shouldNotSetDisabledByAdmin() { 75 mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), 76 UserManager.DISALLOW_USER_SWITCH, false); 77 78 final MultiUserSwitchBarController controller = new MultiUserSwitchBarController(mContext, 79 mSwitchWidgetController, null); 80 81 verify(mSwitchWidgetController, never()).setDisabledByAdmin(any()); 82 } 83 84 @Test onStart_userIsNotMain_shouldNotBeEnabled()85 public void onStart_userIsNotMain_shouldNotBeEnabled() { 86 mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), 87 UserManager.DISALLOW_USER_SWITCH, false); 88 mUserManager.addUser(10, "Test", UserInfo.FLAG_ADMIN); 89 mUserManager.switchUser(10); 90 new MultiUserSwitchBarController(mContext, mSwitchWidgetController, null); 91 92 verify(mSwitchWidgetController, never()).setDisabledByAdmin(any()); 93 verify(mSwitchWidgetController).setEnabled(false); 94 } 95 96 @Test onStart_userIsMain_shouldBeEnabled()97 public void onStart_userIsMain_shouldBeEnabled() { 98 mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()), 99 UserManager.DISALLOW_USER_SWITCH, false); 100 new MultiUserSwitchBarController(mContext, mSwitchWidgetController, null); 101 102 verify(mSwitchWidgetController, never()).setDisabledByAdmin(any()); 103 verify(mSwitchWidgetController).setEnabled(true); 104 } 105 } 106