diff --git a/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java b/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java index 94bd78a9c09..73ff31d4e47 100644 --- a/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java +++ b/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java @@ -46,12 +46,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController private static final String WIFI_TETHER_SETTINGS = "wifi_tether"; - private final TetheringManager mTetheringManager; - private final String[] mWifiRegexs; - private final WifiManager mWifiManager; - private final Lifecycle mLifecycle; - @VisibleForTesting - boolean mIsWifiTetheringAllow; + private boolean mIsWifiTetherable; + private WifiManager mWifiManager; + private boolean mIsWifiTetheringAllow; private int mSoftApState; @VisibleForTesting Preference mPreference; @@ -59,18 +56,32 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController WifiTetherSoftApManager mWifiTetherSoftApManager; public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) { - this(context, lifecycle, true /* initSoftApManager */); + this(context, lifecycle, + context.getSystemService(WifiManager.class), + context.getSystemService(TetheringManager.class), + true /* initSoftApManager */, + WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context)); } @VisibleForTesting - WifiTetherPreferenceController(Context context, Lifecycle lifecycle, - boolean initSoftApManager) { + WifiTetherPreferenceController( + Context context, + Lifecycle lifecycle, + WifiManager wifiManager, + TetheringManager tetheringManager, + boolean initSoftApManager, + boolean isWifiTetheringAllow) { super(context); - mTetheringManager = context.getSystemService(TetheringManager.class); - mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); - mWifiRegexs = mTetheringManager.getTetherableWifiRegexs(); - mIsWifiTetheringAllow = WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context); - mLifecycle = lifecycle; + final String[] wifiRegexs = tetheringManager.getTetherableWifiRegexs(); + if (wifiRegexs != null && wifiRegexs.length != 0) { + mIsWifiTetherable = true; + } + + mIsWifiTetheringAllow = isWifiTetheringAllow; + if (!isWifiTetheringAllow) return; + + mWifiManager = wifiManager; + if (lifecycle != null) { lifecycle.addObserver(this); } @@ -81,9 +92,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController @Override public boolean isAvailable() { - return mWifiRegexs != null - && mWifiRegexs.length != 0 - && !Utils.isMonkeyRunning(); + return mIsWifiTetherable && !Utils.isMonkeyRunning(); } @Override @@ -94,7 +103,10 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController // unavailable return; } - mPreference.setEnabled(mIsWifiTetheringAllow); + if (!mIsWifiTetheringAllow && mPreference.isEnabled()) { + mPreference.setEnabled(false); + mPreference.setSummary(R.string.not_allowed_by_ent); + } } @Override diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java index 7ce2bf3b32c..e8ee7c3b90e 100644 --- a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java @@ -19,30 +19,29 @@ package com.android.settings.wifi.tether; 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.net.TetheringManager; import android.net.wifi.SoftApConfiguration; import android.net.wifi.WifiManager; -import android.os.Bundle; -import android.os.UserManager; -import androidx.lifecycle.LifecycleOwner; import androidx.preference.PreferenceScreen; +import androidx.test.core.app.ApplicationProvider; import com.android.settings.testutils.FakeFeatureFactory; import com.android.settingslib.PrimarySwitchPreference; import com.android.settingslib.core.lifecycle.Lifecycle; 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.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; @@ -56,53 +55,44 @@ public class WifiTetherPreferenceControllerTest { private static final String SSID = "Pixel"; - private Context mContext; + @Rule + public final MockitoRule mMockitoRule = MockitoJUnit.rule(); + @Spy + Context mContext = ApplicationProvider.getApplicationContext(); + @Mock + private Lifecycle mLifecycle; @Mock private TetheringManager mTetheringManager; @Mock private WifiManager mWifiManager; @Mock - private UserManager mUserManager; - @Mock - private Bundle mBundle; - @Mock private PreferenceScreen mScreen; private SoftApConfiguration mSoftApConfiguration; private WifiTetherPreferenceController mController; - private Lifecycle mLifecycle; - private LifecycleOwner mLifecycleOwner; private PrimarySwitchPreference mPreference; @Before public void setUp() { - MockitoAnnotations.initMocks(this); - - mContext = spy(RuntimeEnvironment.application); - mLifecycleOwner = () -> mLifecycle; - mLifecycle = new Lifecycle(mLifecycleOwner); FakeFeatureFactory.setupForTest(); - mPreference = new PrimarySwitchPreference(RuntimeEnvironment.application); + mPreference = new PrimarySwitchPreference(mContext); when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager); when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); - when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); - when(mUserManager.getUserRestrictions()).thenReturn(mBundle); when(mScreen.findPreference(anyString())).thenReturn(mPreference); mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build(); when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration); when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); - mController = new WifiTetherPreferenceController(mContext, mLifecycle, - false /* initSoftApManager */); - mController.mIsWifiTetheringAllow = true; + mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager, + mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */); mController.displayPreference(mScreen); } @Test public void isAvailable_noTetherRegex_shouldReturnFalse() { when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{}); - mController = new WifiTetherPreferenceController(mContext, mLifecycle, - false /* initSoftApManager */); + mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager, + mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */); assertThat(mController.isAvailable()).isFalse(); } @@ -114,16 +104,19 @@ public class WifiTetherPreferenceControllerTest { @Test public void displayPreference_wifiTetheringNotAllowed_shouldDisable() { - mController.mIsWifiTetheringAllow = false; + mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager, + mTetheringManager, false /* initSoftApManager */, false /* isWifiTetheringAllow */); mController.displayPreference(mScreen); assertThat(mPreference.isEnabled()).isFalse(); + assertThat(mPreference.getSummary()).isEqualTo("Not allowed by your organization"); } @Test public void displayPreference_wifiTetheringAllowed_shouldEnable() { - mController.mIsWifiTetheringAllow = true; + mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager, + mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */); mController.displayPreference(mScreen);