1 /* 2 * Copyright (C) 2022 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 com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assume.assumeTrue; 22 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.content.pm.PackageManager; 29 import android.os.SystemProperties; 30 import android.os.UserManager; 31 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settings.testutils.shadow.ShadowDevicePolicyManager; 35 import com.android.settings.testutils.shadow.ShadowUserManager; 36 import com.android.settingslib.RestrictedSwitchPreference; 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.RobolectricTestRunner; 45 import org.robolectric.RuntimeEnvironment; 46 import org.robolectric.annotation.Config; 47 48 @RunWith(RobolectricTestRunner.class) 49 @Config(shadows = { 50 ShadowUserManager.class, 51 ShadowDevicePolicyManager.class 52 }) 53 public class GuestTelephonyPreferenceControllerTest { 54 55 @Mock(answer = RETURNS_DEEP_STUBS) 56 private PreferenceScreen mScreen; 57 58 @Mock(answer = RETURNS_DEEP_STUBS) 59 private Context mContext; 60 private ShadowUserManager mUserManager; 61 private ShadowDevicePolicyManager mDpm; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 mContext = RuntimeEnvironment.application; 67 mUserManager = ShadowUserManager.getShadow(); 68 mUserManager.setSupportsMultipleUsers(true); 69 mDpm = ShadowDevicePolicyManager.getShadow(); 70 } 71 72 @After tearDown()73 public void tearDown() { 74 ShadowUserManager.reset(); 75 } 76 77 @Test displayPref_NotAdmin_shouldNotDisplay()78 public void displayPref_NotAdmin_shouldNotDisplay() { 79 mUserManager.setIsAdminUser(false); 80 81 final GuestTelephonyPreferenceController controller = 82 new GuestTelephonyPreferenceController(mContext, "fake_key"); 83 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 84 85 when(preference.getKey()).thenReturn(controller.getPreferenceKey()); 86 when(mScreen.findPreference(preference.getKey())).thenReturn(preference); 87 88 controller.displayPreference(mScreen); 89 90 verify(preference).setVisible(false); 91 } 92 93 @Test updateState_NotAdmin_shouldNotDisplayPreference()94 public void updateState_NotAdmin_shouldNotDisplayPreference() { 95 mUserManager.setIsAdminUser(false); 96 97 final GuestTelephonyPreferenceController controller = 98 new GuestTelephonyPreferenceController(mContext, "fake_key"); 99 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 100 101 controller.updateState(preference); 102 103 verify(preference).setVisible(false); 104 } 105 106 @Test updateState_Admin_shouldDisplayPreference()107 public void updateState_Admin_shouldDisplayPreference() { 108 assumeTrue("Device does not have telephony feature ", 109 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)); 110 SystemProperties.set("fw.max_users", Long.toBinaryString(4)); 111 mDpm.setDeviceOwner(null); 112 mUserManager.setIsAdminUser(true); 113 mUserManager.setUserSwitcherEnabled(true); 114 mUserManager.setSupportsMultipleUsers(true); 115 mUserManager.setUserTypeEnabled(UserManager.USER_TYPE_FULL_RESTRICTED, true); 116 mUserManager.setUserTypeEnabled(UserManager.USER_TYPE_FULL_SYSTEM, true); 117 mUserManager.setUserTypeEnabled(UserManager.USER_TYPE_FULL_GUEST, true); 118 119 final GuestTelephonyPreferenceController controller = 120 new GuestTelephonyPreferenceController(mContext, "fake_key"); 121 final RestrictedSwitchPreference preference = mock(RestrictedSwitchPreference.class); 122 123 controller.updateState(preference); 124 125 verify(preference).setVisible(true); 126 } 127 128 @Test setChecked_Guest_hasNoCallRestriction()129 public void setChecked_Guest_hasNoCallRestriction() { 130 mUserManager.setIsAdminUser(true); 131 132 final GuestTelephonyPreferenceController controller = 133 new GuestTelephonyPreferenceController(mContext, "fake_key"); 134 135 controller.setChecked(true); 136 137 assertThat(mUserManager.hasGuestUserRestriction("no_outgoing_calls", false)).isTrue(); 138 assertThat(mUserManager.hasGuestUserRestriction("no_sms", true)).isTrue(); 139 } 140 141 @Test setUnchecked_Guest_hasCallRestriction()142 public void setUnchecked_Guest_hasCallRestriction() { 143 mUserManager.setIsAdminUser(true); 144 145 final GuestTelephonyPreferenceController controller = 146 new GuestTelephonyPreferenceController(mContext, "fake_key"); 147 148 controller.setChecked(false); 149 150 assertThat(mUserManager.hasGuestUserRestriction("no_outgoing_calls", true)).isTrue(); 151 assertThat(mUserManager.hasGuestUserRestriction("no_sms", true)).isTrue(); 152 } 153 154 } 155