Loading packages/SettingsLib/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtils.java +10 −23 Original line number Diff line number Diff line Loading @@ -18,7 +18,6 @@ package com.android.settingslib.wifi; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.UserManager; import android.util.Log; Loading @@ -37,14 +36,10 @@ public class WifiEnterpriseRestrictionUtils { * @return whether the device is permitted to use Wi-Fi Tethering */ public static boolean isWifiTetheringAllowed(Context context) { final UserManager userManager = context.getSystemService(UserManager.class); final Bundle restrictions = userManager.getUserRestrictions(); if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)) { Log.i(TAG, "Wi-Fi Tethering isn't available due to user restriction."); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_TETHERING)) return true; Log.w(TAG, "Wi-Fi Tethering isn't available due to user restriction."); return false; } return true; } /** * Confirm Wi-Fi Direct is allowed according to whether user restriction is set Loading @@ -53,14 +48,10 @@ public class WifiEnterpriseRestrictionUtils { * @return whether the device is permitted to use Wi-Fi Direct */ public static boolean isWifiDirectAllowed(Context context) { final UserManager userManager = context.getSystemService(UserManager.class); final Bundle restrictions = userManager.getUserRestrictions(); if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)) { Log.i(TAG, "Wi-Fi Direct isn't available due to user restriction."); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_DIRECT)) return true; Log.w(TAG, "Wi-Fi Direct isn't available due to user restriction."); return false; } return true; } /** * Confirm Wi-Fi Config is allowed to add according to whether user restriction is set Loading @@ -69,14 +60,10 @@ public class WifiEnterpriseRestrictionUtils { * @return whether the device is permitted to add new Wi-Fi config */ public static boolean isAddWifiConfigAllowed(Context context) { final UserManager userManager = context.getSystemService(UserManager.class); final Bundle restrictions = userManager.getUserRestrictions(); if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)) { Log.i(TAG, "Wi-Fi Add network isn't available due to user restriction."); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_ADD_WIFI_CONFIG)) return true; Log.w(TAG, "Wi-Fi Add network isn't available due to user restriction."); return false; } return true; } /** * Confirm Wi-Fi state is allowed to change to whether user restriction is set Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtilsTest.java +31 −63 Original line number Diff line number Diff line Loading @@ -15,122 +15,91 @@ */ package com.android.settingslib.wifi; import static android.os.UserManager.DISALLOW_ADD_WIFI_CONFIG; import static android.os.UserManager.DISALLOW_CHANGE_WIFI_STATE; import static android.os.UserManager.DISALLOW_WIFI_DIRECT; import static android.os.UserManager.DISALLOW_WIFI_TETHERING; import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.UserManager; import androidx.test.core.app.ApplicationProvider; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.Spy; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.robolectric.RobolectricTestRunner; import org.robolectric.util.ReflectionHelpers; @RunWith(RobolectricTestRunner.class) public class WifiEnterpriseRestrictionUtilsTest { private Context mContext; static final String SDK_INT = "SDK_INT"; static final int VERSION_CODES_S = Build.VERSION_CODES.S; static final int VERSION_CODES_T = Build.VERSION_CODES.TIRAMISU; @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); @Spy Context mContext = ApplicationProvider.getApplicationContext(); @Mock private UserManager mUserManager; @Mock private Bundle mBundle; @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = spy(ApplicationProvider.getApplicationContext()); when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); when(mUserManager.getUserRestrictions()).thenReturn(mBundle); ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_T); } @Test public void isWifiTetheringAllowed_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue(); } @Test public void isWifiTetheringAllowed_setSDKForTAndDisallowForRestriction_shouldReturnFalse() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(true); public void isWifiTetheringAllowed_hasDisallowRestriction_shouldReturnFalse() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_TETHERING)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isFalse(); } @Test public void isWifiTetheringAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(false); public void isWifiTetheringAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_TETHERING)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue(); } @Test public void isWifiDirectAllowed_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isTrue(); } @Test public void isWifiDirectAllowed_setSDKForTAndDisallowForRestriction_shouldReturnFalse() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)).thenReturn(true); public void isWifiDirectAllowed_hasDisallowRestriction_shouldReturnFalse() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_DIRECT)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isFalse(); } @Test public void isWifiDirectAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)).thenReturn(false); public void isWifiDirectAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_DIRECT)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isTrue(); } @Test public void isAddWifiConfigAllowed_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mBundle.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isTrue(); } @Test public void isAddWifiConfigAllowed_setSDKForTAndDisallowForRestriction_shouldReturnFalse() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)).thenReturn(true); public void isAddWifiConfigAllowed_hasDisallowRestriction_shouldReturnFalse() { when(mUserManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isFalse(); } @Test public void isAddWifiConfigAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)).thenReturn(false); public void isAddWifiConfigAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isTrue(); } Loading @@ -143,7 +112,7 @@ public class WifiEnterpriseRestrictionUtilsTest { } @Test public void isChangeWifiStateAllowed_hasNoDisallowRestriction_shouldReturnTrue() { public void isChangeWifiStateAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_CHANGE_WIFI_STATE)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed(mContext)).isTrue(); Loading @@ -151,7 +120,7 @@ public class WifiEnterpriseRestrictionUtilsTest { @Test public void hasUserRestrictionFromT_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_S); assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key")) .isFalse(); Loading @@ -159,8 +128,7 @@ public class WifiEnterpriseRestrictionUtilsTest { @Test public void hasUserRestrictionFromT_setSDKForT_shouldReturnHasUserRestriction() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_T); when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key")) Loading Loading
packages/SettingsLib/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtils.java +10 −23 Original line number Diff line number Diff line Loading @@ -18,7 +18,6 @@ package com.android.settingslib.wifi; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.UserManager; import android.util.Log; Loading @@ -37,14 +36,10 @@ public class WifiEnterpriseRestrictionUtils { * @return whether the device is permitted to use Wi-Fi Tethering */ public static boolean isWifiTetheringAllowed(Context context) { final UserManager userManager = context.getSystemService(UserManager.class); final Bundle restrictions = userManager.getUserRestrictions(); if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)) { Log.i(TAG, "Wi-Fi Tethering isn't available due to user restriction."); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_TETHERING)) return true; Log.w(TAG, "Wi-Fi Tethering isn't available due to user restriction."); return false; } return true; } /** * Confirm Wi-Fi Direct is allowed according to whether user restriction is set Loading @@ -53,14 +48,10 @@ public class WifiEnterpriseRestrictionUtils { * @return whether the device is permitted to use Wi-Fi Direct */ public static boolean isWifiDirectAllowed(Context context) { final UserManager userManager = context.getSystemService(UserManager.class); final Bundle restrictions = userManager.getUserRestrictions(); if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)) { Log.i(TAG, "Wi-Fi Direct isn't available due to user restriction."); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_DIRECT)) return true; Log.w(TAG, "Wi-Fi Direct isn't available due to user restriction."); return false; } return true; } /** * Confirm Wi-Fi Config is allowed to add according to whether user restriction is set Loading @@ -69,14 +60,10 @@ public class WifiEnterpriseRestrictionUtils { * @return whether the device is permitted to add new Wi-Fi config */ public static boolean isAddWifiConfigAllowed(Context context) { final UserManager userManager = context.getSystemService(UserManager.class); final Bundle restrictions = userManager.getUserRestrictions(); if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)) { Log.i(TAG, "Wi-Fi Add network isn't available due to user restriction."); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_ADD_WIFI_CONFIG)) return true; Log.w(TAG, "Wi-Fi Add network isn't available due to user restriction."); return false; } return true; } /** * Confirm Wi-Fi state is allowed to change to whether user restriction is set Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/wifi/WifiEnterpriseRestrictionUtilsTest.java +31 −63 Original line number Diff line number Diff line Loading @@ -15,122 +15,91 @@ */ package com.android.settingslib.wifi; import static android.os.UserManager.DISALLOW_ADD_WIFI_CONFIG; import static android.os.UserManager.DISALLOW_CHANGE_WIFI_STATE; import static android.os.UserManager.DISALLOW_WIFI_DIRECT; import static android.os.UserManager.DISALLOW_WIFI_TETHERING; import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.UserManager; import androidx.test.core.app.ApplicationProvider; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.Spy; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.robolectric.RobolectricTestRunner; import org.robolectric.util.ReflectionHelpers; @RunWith(RobolectricTestRunner.class) public class WifiEnterpriseRestrictionUtilsTest { private Context mContext; static final String SDK_INT = "SDK_INT"; static final int VERSION_CODES_S = Build.VERSION_CODES.S; static final int VERSION_CODES_T = Build.VERSION_CODES.TIRAMISU; @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); @Spy Context mContext = ApplicationProvider.getApplicationContext(); @Mock private UserManager mUserManager; @Mock private Bundle mBundle; @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = spy(ApplicationProvider.getApplicationContext()); when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); when(mUserManager.getUserRestrictions()).thenReturn(mBundle); ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_T); } @Test public void isWifiTetheringAllowed_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue(); } @Test public void isWifiTetheringAllowed_setSDKForTAndDisallowForRestriction_shouldReturnFalse() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(true); public void isWifiTetheringAllowed_hasDisallowRestriction_shouldReturnFalse() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_TETHERING)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isFalse(); } @Test public void isWifiTetheringAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(false); public void isWifiTetheringAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_TETHERING)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue(); } @Test public void isWifiDirectAllowed_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isTrue(); } @Test public void isWifiDirectAllowed_setSDKForTAndDisallowForRestriction_shouldReturnFalse() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)).thenReturn(true); public void isWifiDirectAllowed_hasDisallowRestriction_shouldReturnFalse() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_DIRECT)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isFalse(); } @Test public void isWifiDirectAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)).thenReturn(false); public void isWifiDirectAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_WIFI_DIRECT)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isTrue(); } @Test public void isAddWifiConfigAllowed_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mBundle.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isTrue(); } @Test public void isAddWifiConfigAllowed_setSDKForTAndDisallowForRestriction_shouldReturnFalse() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)).thenReturn(true); public void isAddWifiConfigAllowed_hasDisallowRestriction_shouldReturnFalse() { when(mUserManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)).thenReturn(true); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isFalse(); } @Test public void isAddWifiConfigAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); when(mBundle.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)).thenReturn(false); public void isAddWifiConfigAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isTrue(); } Loading @@ -143,7 +112,7 @@ public class WifiEnterpriseRestrictionUtilsTest { } @Test public void isChangeWifiStateAllowed_hasNoDisallowRestriction_shouldReturnTrue() { public void isChangeWifiStateAllowed_noDisallowRestriction_shouldReturnTrue() { when(mUserManager.hasUserRestriction(DISALLOW_CHANGE_WIFI_STATE)).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed(mContext)).isTrue(); Loading @@ -151,7 +120,7 @@ public class WifiEnterpriseRestrictionUtilsTest { @Test public void hasUserRestrictionFromT_setSDKForS_shouldReturnTrue() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_S); assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key")) .isFalse(); Loading @@ -159,8 +128,7 @@ public class WifiEnterpriseRestrictionUtilsTest { @Test public void hasUserRestrictionFromT_setSDKForT_shouldReturnHasUserRestriction() { ReflectionHelpers.setStaticField( Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU); ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_T); when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key")) Loading