Merge "Fix broken test in WifiEnterpriseRestrictionUtils" into tm-dev am: 7332464413

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17767847

Change-Id: I464f11d674a19e94b2d694c281f3d00821f0e814
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
TreeHugger Robot
2022-04-17 20:05:28 +00:00
committed by Automerger Merge Worker
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,14 +36,10 @@ 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)) {
Log.i(TAG, "Wi-Fi Tethering isn't available due to user restriction.");
return false; return false;
} }
return true;
}
/** /**
* Confirm Wi-Fi Direct is allowed according to whether user restriction is set * Confirm Wi-Fi Direct is allowed according to whether user restriction is set
@@ -53,14 +48,10 @@ 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)) {
Log.i(TAG, "Wi-Fi Direct isn't available due to user restriction.");
return false; return false;
} }
return true;
}
/** /**
* Confirm Wi-Fi Config is allowed to add according to whether user restriction is set * Confirm Wi-Fi Config is allowed to add according to whether user restriction is set
@@ -69,14 +60,10 @@ 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)) {
Log.i(TAG, "Wi-Fi Add network isn't available due to user restriction.");
return false; return false;
} }
return true;
}
/** /**
* Confirm Wi-Fi state is allowed to change to whether user restriction is set * Confirm Wi-Fi state is allowed to change to whether user restriction is set

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"))