Merge "Fix broken test in WifiEnterpriseRestrictionUtils" into tm-dev

This commit is contained in:
TreeHugger Robot
2022-04-17 19:52:43 +00:00
committed by Android (Google) Code Review
2 changed files with 41 additions and 86 deletions

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.wifi;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.os.UserManager; import android.os.UserManager;
import android.util.Log; import android.util.Log;
@@ -37,13 +36,9 @@ public class WifiEnterpriseRestrictionUtils {
* @return whether the device is permitted to use Wi-Fi Tethering * @return whether the device is permitted to use Wi-Fi Tethering
*/ */
public static boolean isWifiTetheringAllowed(Context context) { public static boolean isWifiTetheringAllowed(Context context) {
final UserManager userManager = context.getSystemService(UserManager.class); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_TETHERING)) return true;
final Bundle restrictions = userManager.getUserRestrictions(); Log.w(TAG, "Wi-Fi Tethering isn't available due to user restriction.");
if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)) { return false;
Log.i(TAG, "Wi-Fi Tethering isn't available due to user restriction.");
return false;
}
return true;
} }
/** /**
@@ -53,13 +48,9 @@ public class WifiEnterpriseRestrictionUtils {
* @return whether the device is permitted to use Wi-Fi Direct * @return whether the device is permitted to use Wi-Fi Direct
*/ */
public static boolean isWifiDirectAllowed(Context context) { public static boolean isWifiDirectAllowed(Context context) {
final UserManager userManager = context.getSystemService(UserManager.class); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_WIFI_DIRECT)) return true;
final Bundle restrictions = userManager.getUserRestrictions(); Log.w(TAG, "Wi-Fi Direct isn't available due to user restriction.");
if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)) { return false;
Log.i(TAG, "Wi-Fi Direct isn't available due to user restriction.");
return false;
}
return true;
} }
/** /**
@@ -69,13 +60,9 @@ public class WifiEnterpriseRestrictionUtils {
* @return whether the device is permitted to add new Wi-Fi config * @return whether the device is permitted to add new Wi-Fi config
*/ */
public static boolean isAddWifiConfigAllowed(Context context) { public static boolean isAddWifiConfigAllowed(Context context) {
final UserManager userManager = context.getSystemService(UserManager.class); if (!hasUserRestrictionFromT(context, UserManager.DISALLOW_ADD_WIFI_CONFIG)) return true;
final Bundle restrictions = userManager.getUserRestrictions(); Log.w(TAG, "Wi-Fi Add network isn't available due to user restriction.");
if (isAtLeastT() && restrictions.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)) { return false;
Log.i(TAG, "Wi-Fi Add network isn't available due to user restriction.");
return false;
}
return true;
} }
/** /**
@@ -98,7 +85,7 @@ public class WifiEnterpriseRestrictionUtils {
return userManager.hasUserRestriction(restrictionKey); return userManager.hasUserRestriction(restrictionKey);
} }
@ChecksSdkIntAtLeast(api=Build.VERSION_CODES.TIRAMISU) @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU)
private static boolean isAtLeastT() { private static boolean isAtLeastT() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU; return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU;
} }

View File

@@ -15,122 +15,91 @@
*/ */
package com.android.settingslib.wifi; 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_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 com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.os.UserManager; import android.os.UserManager;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; 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.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class WifiEnterpriseRestrictionUtilsTest { 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 @Mock
private UserManager mUserManager; private UserManager mUserManager;
@Mock
private Bundle mBundle;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
when(mUserManager.getUserRestrictions()).thenReturn(mBundle); ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_T);
ReflectionHelpers.setStaticField(
Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);
} }
@Test @Test
public void isWifiTetheringAllowed_setSDKForS_shouldReturnTrue() { public void isWifiTetheringAllowed_hasDisallowRestriction_shouldReturnFalse() {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mUserManager.hasUserRestriction(DISALLOW_WIFI_TETHERING)).thenReturn(true);
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);
assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isFalse(); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isFalse();
} }
@Test @Test
public void isWifiTetheringAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { public void isWifiTetheringAllowed_noDisallowRestriction_shouldReturnTrue() {
ReflectionHelpers.setStaticField( when(mUserManager.hasUserRestriction(DISALLOW_WIFI_TETHERING)).thenReturn(false);
Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);
when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_TETHERING)).thenReturn(false);
assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue(); assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue();
} }
@Test @Test
public void isWifiDirectAllowed_setSDKForS_shouldReturnTrue() { public void isWifiDirectAllowed_hasDisallowRestriction_shouldReturnFalse() {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mUserManager.hasUserRestriction(DISALLOW_WIFI_DIRECT)).thenReturn(true);
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);
assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isFalse(); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isFalse();
} }
@Test @Test
public void isWifiDirectAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { public void isWifiDirectAllowed_noDisallowRestriction_shouldReturnTrue() {
ReflectionHelpers.setStaticField( when(mUserManager.hasUserRestriction(DISALLOW_WIFI_DIRECT)).thenReturn(false);
Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);
when(mBundle.getBoolean(UserManager.DISALLOW_WIFI_DIRECT)).thenReturn(false);
assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isTrue(); assertThat(WifiEnterpriseRestrictionUtils.isWifiDirectAllowed(mContext)).isTrue();
} }
@Test @Test
public void isAddWifiConfigAllowed_setSDKForS_shouldReturnTrue() { public void isAddWifiConfigAllowed_hasDisallowRestriction_shouldReturnFalse() {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.S); when(mUserManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)).thenReturn(true);
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);
assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isFalse(); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isFalse();
} }
@Test @Test
public void isAddWifiConfigAllowed_setSDKForTAndAllowForRestriction_shouldReturnTrue() { public void isAddWifiConfigAllowed_noDisallowRestriction_shouldReturnTrue() {
ReflectionHelpers.setStaticField( when(mUserManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)).thenReturn(false);
Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);
when(mBundle.getBoolean(UserManager.DISALLOW_ADD_WIFI_CONFIG)).thenReturn(false);
assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isTrue(); assertThat(WifiEnterpriseRestrictionUtils.isAddWifiConfigAllowed(mContext)).isTrue();
} }
@@ -143,7 +112,7 @@ public class WifiEnterpriseRestrictionUtilsTest {
} }
@Test @Test
public void isChangeWifiStateAllowed_hasNoDisallowRestriction_shouldReturnTrue() { public void isChangeWifiStateAllowed_noDisallowRestriction_shouldReturnTrue() {
when(mUserManager.hasUserRestriction(DISALLOW_CHANGE_WIFI_STATE)).thenReturn(false); when(mUserManager.hasUserRestriction(DISALLOW_CHANGE_WIFI_STATE)).thenReturn(false);
assertThat(WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed(mContext)).isTrue(); assertThat(WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed(mContext)).isTrue();
@@ -151,7 +120,7 @@ public class WifiEnterpriseRestrictionUtilsTest {
@Test @Test
public void hasUserRestrictionFromT_setSDKForS_shouldReturnTrue() { 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")) assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key"))
.isFalse(); .isFalse();
@@ -159,8 +128,7 @@ public class WifiEnterpriseRestrictionUtilsTest {
@Test @Test
public void hasUserRestrictionFromT_setSDKForT_shouldReturnHasUserRestriction() { public void hasUserRestrictionFromT_setSDKForT_shouldReturnHasUserRestriction() {
ReflectionHelpers.setStaticField( ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, VERSION_CODES_T);
Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.TIRAMISU);
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key")) assertThat(WifiEnterpriseRestrictionUtils.hasUserRestrictionFromT(mContext, "key"))