Fix broken test in WifiEnterpriseRestrictionUtils
- The original design would result in NPE if the user did not have any restrictions. - Refine to use UserManager#hasUserRestriction instead of UserManager#getUserRestrictions. Bug: 229350205 Test: manual test make RunSettingsRoboTests ROBOTEST_FILTER=WifiSettingsTest make RunSettingsLibRoboTests \ ROBOTEST_FILTER=WifiEnterpriseRestrictionUtilsTest Change-Id: Iec60e3a1c2778fc89596b3c4d28a34604adfdfb8
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -37,13 +36,9 @@ 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.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_TETHERING)) return true;
|
||||
Log.w(TAG, "Wi-Fi Tethering isn't available due to user restriction.");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,13 +48,9 @@ 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.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_DIRECT)) return true;
|
||||
Log.w(TAG, "Wi-Fi Direct isn't available due to user restriction.");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,13 +60,9 @@ 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.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +85,7 @@ public class WifiEnterpriseRestrictionUtils {
|
||||
return userManager.hasUserRestriction(restrictionKey);
|
||||
}
|
||||
|
||||
@ChecksSdkIntAtLeast(api=Build.VERSION_CODES.TIRAMISU)
|
||||
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU)
|
||||
private static boolean isAtLeastT() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
@@ -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"))
|
||||
|
||||
Reference in New Issue
Block a user