Merge "Adapt new EthernetManager APIs in Settings." am: 3340b8e3c3

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Settings/+/1968461

Change-Id: Icc3b8d9fd42ee9712cfc81c74ae886576695ee2a
This commit is contained in:
Xiao Ma
2022-02-24 05:22:10 +00:00
committed by Automerger Merge Worker
4 changed files with 77 additions and 41 deletions

View File

@@ -33,8 +33,10 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.EthernetManager;
import android.net.TetheringManager;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.FeatureFlagUtils;
@@ -43,7 +45,6 @@ import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.FeatureFlags;
import com.android.settings.testutils.shadow.ShadowWifiManager;
import com.android.settings.wifi.tether.WifiTetherAutoOffPreferenceController;
import com.android.settings.wifi.tether.WifiTetherSecurityPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -55,14 +56,12 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowWifiManager.class})
public class AllInOneTetherSettingsTest {
private static final String[] WIFI_REGEXS = {"wifi_regexs"};
private static final String[] USB_REGEXS = {"usb_regexs"};
@@ -72,6 +71,8 @@ public class AllInOneTetherSettingsTest {
private Context mContext;
private AllInOneTetherSettings mAllInOneTetherSettings;
@Mock
private WifiManager mWifiManager;
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
@@ -84,16 +85,20 @@ public class AllInOneTetherSettingsTest {
private PreferenceScreen mPreferenceScreen;
@Mock
private PreferenceGroup mWifiTetherGroup;
@Mock
private EthernetManager mEthernetManager;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
MockitoAnnotations.initMocks(this);
doReturn(mWifiManager).when(mContext).getSystemService(WifiManager.class);
doReturn(mConnectivityManager)
.when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
doReturn(mTetheringManager)
.when(mContext).getSystemService(Context.TETHERING_SERVICE);
doReturn(mEthernetManager).when(mContext).getSystemService(EthernetManager.class);
doReturn(WIFI_REGEXS).when(mTetheringManager).getTetherableWifiRegexs();
doReturn(USB_REGEXS).when(mTetheringManager).getTetherableUsbRegexs();
doReturn(BT_REGEXS).when(mTetheringManager).getTetherableBluetoothRegexs();

View File

@@ -19,6 +19,7 @@ package com.android.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -64,10 +65,9 @@ public class EthernetTetherPreferenceControllerTest {
mPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(mTetheringManager.getTetherableIfaces()).thenReturn(new String[]{ETHERNET_REGEX});
when(mContext.getSystemService(Context.ETHERNET_SERVICE)).thenReturn(mEthernetManager);
when(mContext.getSystemService(EthernetManager.class)).thenReturn(mEthernetManager);
mController = new EthernetTetherPreferenceController(mContext, "ethernet");
mController.setTetherEnabler(mTetherEnabler);
ReflectionHelpers.setField(mController, "mEthernetRegex", ETHERNET_REGEX);
ReflectionHelpers.setField(mController, "mPreference", mPreference);
}
@@ -75,7 +75,8 @@ public class EthernetTetherPreferenceControllerTest {
public void lifecycle_shouldRegisterReceiverOnStart() {
mController.onStart();
verify(mEthernetManager).addListener(eq(mController.mEthernetListener));
verify(mEthernetManager).addInterfaceStateListener(any(),
eq(mController.mEthernetListener));
}
@Test
@@ -93,11 +94,10 @@ public class EthernetTetherPreferenceControllerTest {
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onStart();
EthernetManager.Listener listener = mController.mEthernetListener;
EthernetManager.InterfaceStateListener listener = mController.mEthernetListener;
mController.onStop();
verify(mEthernetManager).removeListener(eq(listener));
assertThat(mController.mEthernetListener).isNull();
verify(mEthernetManager).removeInterfaceStateListener(eq(listener));
}
@Test
@@ -108,8 +108,11 @@ public class EthernetTetherPreferenceControllerTest {
@Test
public void shouldShow_noEthernetInterface() {
ReflectionHelpers.setField(mController, "mEthernetRegex", "");
assertThat(mController.shouldShow()).isFalse();
when(mContext.getSystemService(EthernetManager.class)).thenReturn(null);
final EthernetTetherPreferenceController controller =
new EthernetTetherPreferenceController(mContext, "ethernet");
assertThat(controller.shouldShow()).isFalse();
}
@Test