Merge "To disable Wi-Fi Direct when user restriction is set"

This commit is contained in:
Betty Chang
2021-12-29 09:55:00 +00:00
committed by Android (Google) Code Review
2 changed files with 37 additions and 38 deletions

View File

@@ -32,8 +32,9 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
@@ -56,12 +57,15 @@ public class WifiP2PPreferenceControllerTest {
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private WifiManager mWifiManager;
@Mock
private UserManager mUserManager;
@Mock
private Bundle mBundle;
@Mock
private PreferenceScreen mScreen;
@Mock
private Preference mWifiDirectPreference;
@Mock
private LocationManager mLocationManager;
private Lifecycle mLifecycle;
private LifecycleOwner mLifecycleOwner;
@@ -74,8 +78,11 @@ public class WifiP2PPreferenceControllerTest {
mLifecycle = new Lifecycle(mLifecycleOwner);
when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
when(mScreen.findPreference(anyString())).thenReturn(mWifiDirectPreference);
when(mContext.getSystemService(eq(Service.LOCATION_SERVICE))).thenReturn(mLocationManager);
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
when(mUserManager.getUserRestrictions()).thenReturn(mBundle);
when(mWifiManager.isWifiEnabled()).thenReturn(true);
mController = new WifiP2pPreferenceController(mContext, mLifecycle, mWifiManager);
mController.mIsWifiDirectAllow = true;
}
@Test
@@ -86,21 +93,19 @@ public class WifiP2PPreferenceControllerTest {
@Test
public void testOnResume_shouldRegisterListener() {
mLifecycle.handleLifecycleEvent(ON_RESUME);
verify(mContext, times(2)).registerReceiver(
any(BroadcastReceiver.class), any(IntentFilter.class));
verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
}
@Test
public void testOnPause_shouldUnregisterListener() {
mLifecycle.handleLifecycleEvent(ON_RESUME);
mLifecycle.handleLifecycleEvent(ON_PAUSE);
verify(mContext, times(2)).unregisterReceiver(any(BroadcastReceiver.class));
verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
}
@Test
public void testWifiStateChange_shouldToggleEnabledState() {
when(mWifiManager.isWifiEnabled()).thenReturn(true);
when(mLocationManager.isLocationEnabled()).thenReturn(true);
//Sets the preferences.
mController.displayPreference(mScreen);
@@ -118,7 +123,6 @@ public class WifiP2PPreferenceControllerTest {
@Test
public void testDisplayPreference_shouldToggleEnabledState() {
when(mWifiManager.isWifiEnabled()).thenReturn(true);
when(mLocationManager.isLocationEnabled()).thenReturn(true);
mController.displayPreference(mScreen);
verify(mWifiDirectPreference).setEnabled(true);
@@ -127,21 +131,25 @@ public class WifiP2PPreferenceControllerTest {
verify(mWifiDirectPreference).setEnabled(false);
when(mWifiManager.isWifiEnabled()).thenReturn(true);
when(mLocationManager.isLocationEnabled()).thenReturn(false);
mController.displayPreference(mScreen);
verify(mWifiDirectPreference, times(2)).setEnabled(false);
verify(mWifiDirectPreference).setEnabled(false);
}
@Test
public void updateState_withLocationDisabled_preferenceShouldBeDisable() {
when(mWifiManager.isWifiEnabled()).thenReturn(true);
when(mLocationManager.isLocationEnabled()).thenReturn(true);
Intent fakeIntent = new Intent();
mController.displayPreference(mScreen);
verify(mWifiDirectPreference).setEnabled(true);
public void displayPreference_wifiDirectNotAllowed_shouldDisable() {
mController.mIsWifiDirectAllow = false;
mController.displayPreference(mScreen);
when(mLocationManager.isLocationEnabled()).thenReturn(false);
mController.mLocationReceiver.onReceive(mContext, fakeIntent);
verify(mWifiDirectPreference).setEnabled(false);
}
@Test
public void displayPreference_wifiDirectNotAllowed_shouldEnable() {
mController.mIsWifiDirectAllow = true;
mController.displayPreference(mScreen);
verify(mWifiDirectPreference).setEnabled(true);
}
}