To add restriction check for Wi-Fi Tethering

Bug: 203168953
Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=WifiEnterpriseRestrictionUtilsTest
Change-Id: I49f3197ce79c2ef60b73b6ff079a8aa7f86356cc
This commit is contained in:
changbetty
2021-12-15 15:13:38 +00:00
parent 6a1ecf7c45
commit 53184b7523
2 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
import androidx.annotation.ChecksSdkIntAtLeast;
/* Utility class is to confirm the Wi-Fi function is available by enterprise restriction */
public class WifiEnterpriseRestrictionUtils {
private static final String TAG = "WifiEntResUtils";
/**
* Confirm Wi-Fi tethering is available according to whether user restriction is set
*
* @param context A context
* @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;
}
@ChecksSdkIntAtLeast(api=Build.VERSION_CODES.TIRAMISU)
private static boolean isAtLeastT() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU;
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.wifi;
import static com.google.common.truth.Truth.assertThat;
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.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class WifiEnterpriseRestrictionUtilsTest {
private Context mContext;
@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);
}
@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);
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);
assertThat(WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(mContext)).isTrue();
}
}