Clean up AllInOneTetherSettings

This project is not on-going.

Clean up to help improve latency.

Bug: 311604902
Test: manual - on Network & internet page
Test: robo test
Change-Id: I6addb92e5587206661d1b64bdc56473a85ba9c9f
This commit is contained in:
Chaohui Wang
2023-11-30 12:03:51 +08:00
parent f04ee0dfab
commit 0664d3cc45
29 changed files with 8 additions and 2774 deletions

View File

@@ -1,240 +0,0 @@
/*
* Copyright (C) 2020 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.settings;
import static com.android.settings.AllInOneTetherSettings.BLUETOOTH_TETHER_KEY;
import static com.android.settings.AllInOneTetherSettings.ETHERNET_TETHER_KEY;
import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_DEFAULT;
import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_MAX;
import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_WITH_SECURITY_NON;
import static com.android.settings.AllInOneTetherSettings.USB_TETHER_KEY;
import static com.android.settings.AllInOneTetherSettings.WIFI_TETHER_DISABLE_KEY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
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;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.FeatureFlags;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.wifi.repository.WifiHotspotRepository;
import com.android.settings.wifi.tether.WifiTetherAutoOffPreferenceController;
import com.android.settings.wifi.tether.WifiTetherSecurityPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
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.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class AllInOneTetherSettingsTest {
private static final String[] WIFI_REGEXS = {"wifi_regexs"};
private static final String[] USB_REGEXS = {"usb_regexs"};
private static final String[] BT_REGEXS = {"bt_regexs"};
private static final String[] ETHERNET_REGEXS = {"ethernet_regexs"};
private Context mContext;
private AllInOneTetherSettings mAllInOneTetherSettings;
@Mock
private WifiManager mWifiManager;
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private TetheringManager mTetheringManager;
@Mock
private UserManager mUserManager;
@Mock
private WifiTetherSecurityPreferenceController mSecurityPreferenceController;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private PreferenceGroup mWifiTetherGroup;
@Mock
private EthernetManager mEthernetManager;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
MockitoAnnotations.initMocks(this);
when(FakeFeatureFactory.setupForTest().getWifiFeatureProvider().getWifiHotspotRepository())
.thenReturn(mock(WifiHotspotRepository.class));
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();
doReturn(ETHERNET_REGEXS).when(mTetheringManager).getTetherableIfaces();
doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
// Assume the feature is enabled for most test cases.
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, true);
mAllInOneTetherSettings = spy(new AllInOneTetherSettings());
doReturn(mPreferenceScreen).when(mAllInOneTetherSettings).getPreferenceScreen();
ReflectionHelpers.setField(mAllInOneTetherSettings, "mLifecycle", mock(Lifecycle.class));
ReflectionHelpers.setField(mAllInOneTetherSettings, "mSecurityPreferenceController",
mSecurityPreferenceController);
ReflectionHelpers.setField(mAllInOneTetherSettings, "mWifiTetherGroup", mWifiTetherGroup);
}
@Test
public void getNonIndexableKeys_tetherAvailable_featureEnabled_keysReturnedCorrectly() {
// To let TetherUtil.isTetherAvailable return true, select one of the combinations
setupIsTetherAvailable(true);
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, true);
final List<String> niks =
AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
assertThat(niks).doesNotContain(
AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY);
assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY);
assertThat(niks).doesNotContain(USB_TETHER_KEY);
assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY);
// This key should be returned because it's not visible by default.
assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
}
@Test
public void getNonIndexableKeys_tetherAvailable_featureDisabled_keysReturned() {
setupIsTetherAvailable(true);
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
final List<String> niks =
AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY);
assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
assertThat(niks).contains(BLUETOOTH_TETHER_KEY);
assertThat(niks).contains(USB_TETHER_KEY);
assertThat(niks).contains(ETHERNET_TETHER_KEY);
}
@Test
public void getNonIndexableKeys_tetherNotAvailable_keysReturned() {
// To let TetherUtil.isTetherAvailable return false, select one of the combinations
setupIsTetherAvailable(false);
final List<String> niks =
AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY);
assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY);
assertThat(niks).doesNotContain(USB_TETHER_KEY);
assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY);
}
@Test
public void getPreferenceControllers_notEmpty() {
assertThat(AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER
.getPreferenceControllers(mContext)).isNotEmpty();
}
@Test
public void createPreferenceControllers_hasAutoOffPreference() {
assertThat(mAllInOneTetherSettings.createPreferenceControllers(mContext)
.stream()
.filter(controller -> controller instanceof WifiTetherAutoOffPreferenceController)
.count())
.isEqualTo(1);
}
@Test
public void getInitialChildCount_withSecurity() {
when(mSecurityPreferenceController.getSecurityType())
.thenReturn(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo(
EXPANDED_CHILD_COUNT_DEFAULT);
}
@Test
public void getInitialChildCount_withoutSecurity() {
when(mSecurityPreferenceController.getSecurityType())
.thenReturn(SoftApConfiguration.SECURITY_TYPE_OPEN);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo(
EXPANDED_CHILD_COUNT_WITH_SECURITY_NON);
}
@Test
public void getInitialExpandedChildCount_expandAllChild() {
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isNotEqualTo(EXPANDED_CHILD_COUNT_MAX);
ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", false);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isEqualTo(EXPANDED_CHILD_COUNT_MAX);
ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", true);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isEqualTo(EXPANDED_CHILD_COUNT_MAX);
}
private void setupIsTetherAvailable(boolean returnValue) {
when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
// For RestrictedLockUtils.checkIfRestrictionEnforced
final int userId = UserHandle.myUserId();
List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
when(mUserManager.getUserRestrictionSources(
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
.thenReturn(enforcingUsers);
// For RestrictedLockUtils.hasBaseUserRestriction
when(mUserManager.hasBaseUserRestriction(
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
.thenReturn(!returnValue);
}
}

View File

@@ -1,187 +0,0 @@
/*
* Copyright (C) 2020 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.settings.network;
import static com.android.settings.network.TetherEnabler.TETHERING_BLUETOOTH_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_ETHERNET_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_OFF;
import static com.android.settings.network.TetherEnabler.TETHERING_USB_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_WIFI_ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settingslib.PrimarySwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
@RunWith(ParameterizedRobolectricTestRunner.class)
public class AllInOneTetherPreferenceControllerTest {
@ParameterizedRobolectricTestRunner.Parameters(name = "TetherState: {0}")
public static List params() {
return Arrays.asList(new Object[][] {
{TETHERING_OFF, R.string.tether_settings_summary_off},
{TETHERING_WIFI_ON, R.string.tether_settings_summary_hotspot_only},
{TETHERING_USB_ON, R.string.tether_settings_summary_usb_tethering_only},
{TETHERING_BLUETOOTH_ON, R.string.tether_settings_summary_bluetooth_tethering_only},
{TETHERING_ETHERNET_ON, R.string.tether_settings_summary_ethernet_tethering_only},
{
TETHERING_WIFI_ON | TETHERING_USB_ON,
R.string.tether_settings_summary_hotspot_and_usb
},
{
TETHERING_WIFI_ON | TETHERING_BLUETOOTH_ON,
R.string.tether_settings_summary_hotspot_and_bluetooth
},
{
TETHERING_WIFI_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_hotspot_and_ethernet
},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON,
R.string.tether_settings_summary_usb_and_bluetooth
},
{
TETHERING_USB_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_usb_and_ethernet
},
{
TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_bluetooth_and_ethernet
},
{
TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_BLUETOOTH_ON,
R.string.tether_settings_summary_hotspot_and_usb_and_bluetooth
},
{
TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_hotspot_and_usb_and_ethernet
},
{
TETHERING_WIFI_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_hotspot_and_bluetooth_and_ethernet
},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_usb_and_bluetooth_and_ethernet
},
{
TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_BLUETOOTH_ON
| TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_all
}
});
}
private Context mContext;
@Mock
private BluetoothAdapter mBluetoothAdapter;
@Mock
private PrimarySwitchPreference mPreference;
private static final String PREF_KEY = "tether";
private AllInOneTetherPreferenceController mController;
private final int mTetherState;
private final int mSummaryResId;
public AllInOneTetherPreferenceControllerTest(int tetherState, int summaryResId) {
mTetherState = tetherState;
mSummaryResId = summaryResId;
}
@Before
public void setUp() {
mContext = spy(ApplicationProvider.getApplicationContext());
MockitoAnnotations.initMocks(this);
doReturn(null).when(mContext)
.getSystemService(Context.DEVICE_POLICY_SERVICE);
mController = spy(new AllInOneTetherPreferenceController(mContext, /* key= */ "test"));
ReflectionHelpers.setField(mController, "mContext", mContext);
ReflectionHelpers.setField(mController, "mBluetoothAdapter", mBluetoothAdapter);
ReflectionHelpers.setField(mController, "mPreferenceKey", PREF_KEY);
PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference(PREF_KEY)).thenReturn(mPreference);
doReturn(mController.AVAILABLE).when(mController).getAvailabilityStatus();
mController.displayPreference(screen);
}
@Test
public void onCreate_shouldInitBluetoothPan() {
when(mBluetoothAdapter.getState()).thenReturn(BluetoothAdapter.STATE_ON);
mController.onCreate();
verify(mBluetoothAdapter).getState();
verify(mBluetoothAdapter).getProfileProxy(mContext, mController.mBtProfileServiceListener,
BluetoothProfile.PAN);
}
@Test
public void onCreate_shouldNotInitBluetoothPanWhenBluetoothOff() {
when(mBluetoothAdapter.getState()).thenReturn(BluetoothAdapter.STATE_OFF);
mController.onCreate();
verify(mBluetoothAdapter).getState();
verifyNoMoreInteractions(mBluetoothAdapter);
}
@Test
public void goThroughLifecycle_shouldDestroyBluetoothProfile() {
final BluetoothPan pan = mock(BluetoothPan.class);
final AtomicReference<BluetoothPan> panRef =
ReflectionHelpers.getField(mController, "mBluetoothPan");
panRef.set(pan);
mController.onDestroy();
verify(mBluetoothAdapter).closeProfileProxy(BluetoothProfile.PAN, pan);
}
@Test
public void getSummary_afterTetherStateChanged() {
mController.onTetherStateUpdated(mTetherState);
assertThat(mController.getSummary()).isEqualTo(mContext.getString(mSummaryResId));
verify(mController).updateState(mPreference);
verify(mPreference).setSummary(mContext.getString(mSummaryResId));
}
}

View File

@@ -1,132 +0,0 @@
/*
* Copyright (C) 2019 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.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.TetheringManager;
import androidx.preference.SwitchPreference;
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 BluetoothTetherPreferenceControllerTest {
@Mock
private TetheringManager mTetheringManager;
@Mock
private TetherEnabler mTetherEnabler;
private SwitchPreference mSwitchPreference;
private BluetoothTetherPreferenceController mController;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
mSwitchPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(mTetheringManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
mController = new BluetoothTetherPreferenceController(mContext, "BLUETOOTH");
mController.setTetherEnabler(mTetherEnabler);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
}
@Test
public void lifecycle_shouldRegisterReceiverOnStart() {
mController.onStart();
verify(mContext).registerReceiver(
eq(mController.mBluetoothChangeReceiver),
any());
}
@Test
public void lifecycle_shouldAddListenerOnResume() {
mController.onResume();
verify(mTetherEnabler).addListener(mController);
}
@Test
public void lifecycle_shouldRemoveListenrOnPause() {
mController.onPause();
verify(mTetherEnabler).removeListener(mController);
}
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onStart();
mController.onStop();
verify(mContext).unregisterReceiver(
eq(mController.mBluetoothChangeReceiver));
}
@Test
public void shouldShow_noBluetoothTetherable() {
when(mTetheringManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void shouldEnable_transientState() {
ReflectionHelpers.setField(mController, "mBluetoothState",
BluetoothAdapter.STATE_TURNING_OFF);
assertThat(mController.shouldEnable()).isFalse();
}
@Test
public void setChecked_shouldStartBluetoothTethering() {
mController.setChecked(true);
verify(mTetherEnabler).startTethering(TetheringManager.TETHERING_BLUETOOTH);
}
@Test
public void setUnchecked_shouldStopBluetoothTethering() {
mController.setChecked(false);
verify(mTetherEnabler).stopTethering(TetheringManager.TETHERING_BLUETOOTH);
}
@Test
public void switch_shouldCheckedWhenBluetoothTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_BLUETOOTH_ON);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void switch_shouldUnCheckedWhenBluetoothNotTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_OFF);
assertThat(mController.isChecked()).isFalse();
}
}

View File

@@ -1,143 +0,0 @@
/*
* Copyright (C) 2020 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.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;
import android.content.Context;
import android.net.EthernetManager;
import android.net.TetheringManager;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class EthernetTetherPreferenceControllerTest {
@Rule
public MockitoRule mocks = MockitoJUnit.rule();
@Mock
private TetheringManager mTetheringManager;
@Mock
private EthernetManager mEthernetManager;
@Mock
private TetherEnabler mTetherEnabler;
private Context mContext;
private EthernetTetherPreferenceController mController;
private SwitchPreference mPreference;
private static final String ETHERNET_REGEX = "ethernet";
@Before
public void setUp() {
mContext = spy(ApplicationProvider.getApplicationContext());
mPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(mTetheringManager.getTetherableIfaces()).thenReturn(new String[]{ETHERNET_REGEX});
when(mContext.getSystemService(EthernetManager.class)).thenReturn(mEthernetManager);
mController = new EthernetTetherPreferenceController(mContext, "ethernet");
mController.setTetherEnabler(mTetherEnabler);
ReflectionHelpers.setField(mController, "mPreference", mPreference);
}
@Test
@Ignore
public void lifecycle_shouldRegisterReceiverOnStart() {
mController.onStart();
verify(mEthernetManager).addInterfaceStateListener(any(),
eq(mController.mEthernetListener));
}
@Test
public void lifecycle_shouldAddListenerOnResume() {
mController.onResume();
verify(mTetherEnabler).addListener(mController);
}
@Test
public void lifecycle_shouldRemoveListenerOnPause() {
mController.onPause();
verify(mTetherEnabler).removeListener(mController);
}
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onStart();
EthernetManager.InterfaceStateListener listener = mController.mEthernetListener;
mController.onStop();
verify(mEthernetManager).removeInterfaceStateListener(eq(listener));
}
@Test
public void shouldEnable_noTetherable() {
when(mTetheringManager.getTetherableIfaces()).thenReturn(new String[0]);
assertThat(mController.shouldEnable()).isFalse();
}
@Test
public void shouldShow_noEthernetInterface() {
when(mContext.getSystemService(EthernetManager.class)).thenReturn(null);
final EthernetTetherPreferenceController controller =
new EthernetTetherPreferenceController(mContext, "ethernet");
assertThat(controller.shouldShow()).isFalse();
}
@Test
public void setChecked_shouldStartEthernetTethering() {
mController.setChecked(true);
verify(mTetherEnabler).startTethering(TetheringManager.TETHERING_ETHERNET);
}
@Test
public void setUnchecked_shouldStopEthernetTethering() {
mController.setChecked(false);
verify(mTetherEnabler).stopTethering(TetheringManager.TETHERING_ETHERNET);
}
@Test
public void switch_shouldCheckedWhenEthernetTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_ETHERNET_ON);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void switch_shouldUnCheckedWhenEthernetNotTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_OFF);
assertThat(mController.isChecked()).isFalse();
}
}

View File

@@ -1,128 +0,0 @@
/*
* Copyright (C) 2019 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.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.TetheringManager;
import androidx.preference.SwitchPreference;
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 UsbTetherPreferenceControllerTest {
@Mock
private TetheringManager mTetheringManager;
@Mock
private TetherEnabler mTetherEnabler;
private Context mContext;
private UsbTetherPreferenceController mController;
private SwitchPreference mSwitchPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(mTetheringManager.getTetherableUsbRegexs()).thenReturn(new String[]{""});
mController = new UsbTetherPreferenceController(mContext, "USB");
mController.setTetherEnabler(mTetherEnabler);
mSwitchPreference = spy(SwitchPreference.class);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
}
@Test
public void lifecycle_shouldRegisterReceiverOnStart() {
mController.onStart();
verify(mContext).registerReceiver(eq(mController.mUsbChangeReceiver), any());
}
@Test
public void lifecycle_shouldAddListenerOnResume() {
mController.onResume();
verify(mTetherEnabler).addListener(mController);
}
@Test
public void lifecycle_shouldRemoveListenrOnPause() {
mController.onPause();
verify(mTetherEnabler).removeListener(mController);
}
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onStart();
mController.onStop();
verify(mContext).unregisterReceiver(eq(mController.mUsbChangeReceiver));
}
@Test
public void shouldShow_noTetherableUsb() {
when(mTetheringManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
assertThat(mController.shouldShow()).isFalse();
}
@Test
public void shouldEnable_noUsbConnected() {
ReflectionHelpers.setField(mController, "mUsbConnected", false);
assertThat(mController.shouldEnable()).isFalse();
}
@Test
public void setChecked_shouldStartUsbTethering() {
mController.setChecked(true);
verify(mTetherEnabler).startTethering(TetheringManager.TETHERING_USB);
}
@Test
public void setUnchecked_shouldStopUsbTethering() {
mController.setChecked(false);
verify(mTetherEnabler).stopTethering(TetheringManager.TETHERING_USB);
}
@Test
public void switch_shouldCheckedWhenUsbTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_USB_ON);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void switch_shouldUnCheckedWhenUsbNotTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_OFF);
assertThat(mController.isChecked()).isFalse();
}
}

View File

@@ -1,142 +0,0 @@
/*
* Copyright (C) 2019 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.settings.network;
import static com.android.settings.AllInOneTetherSettings.WIFI_TETHER_DISABLE_KEY;
import static com.android.settings.network.TetherEnabler.TETHERING_BLUETOOTH_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_ETHERNET_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_OFF;
import static com.android.settings.network.TetherEnabler.TETHERING_USB_ON;
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.net.TetheringManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
import java.util.Arrays;
import java.util.List;
@RunWith(ParameterizedRobolectricTestRunner.class)
public class WifiTetherDisablePreferenceControllerTest {
@ParameterizedRobolectricTestRunner.Parameters(name = "TetherState: {0}")
public static List params() {
return Arrays.asList(new Object[][] {
{TETHERING_OFF, R.string.summary_placeholder},
{TETHERING_USB_ON, R.string.disable_wifi_hotspot_when_usb_on},
{TETHERING_BLUETOOTH_ON, R.string.disable_wifi_hotspot_when_bluetooth_on},
{TETHERING_ETHERNET_ON, R.string.disable_wifi_hotspot_when_ethernet_on},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON,
R.string.disable_wifi_hotspot_when_usb_and_bluetooth_on
},
{
TETHERING_USB_ON | TETHERING_ETHERNET_ON,
R.string.disable_wifi_hotspot_when_usb_and_ethernet_on
},
{
TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.disable_wifi_hotspot_when_bluetooth_and_ethernet_on
},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.disable_wifi_hotspot_when_usb_and_bluetooth_and_ethernet_on
}
});
}
@Mock
private TetheringManager mTetheringManager;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private TetherEnabler mTetherEnabler;
private SwitchPreference mPreference;
private Context mContext;
private WifiTetherDisablePreferenceController mController;
private final int mTetherState;
private final int mSummaryResId;
public WifiTetherDisablePreferenceControllerTest(int tetherState, int summaryResId) {
mTetherState = tetherState;
mSummaryResId = summaryResId;
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
mPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{""});
mController = new WifiTetherDisablePreferenceController(mContext, WIFI_TETHER_DISABLE_KEY);
mController.setTetherEnabler(mTetherEnabler);
ReflectionHelpers.setField(mController, "mScreen", mPreferenceScreen);
ReflectionHelpers.setField(mController, "mPreference", mPreference);
when(mPreferenceScreen.findPreference(WIFI_TETHER_DISABLE_KEY)).thenReturn(mPreference);
}
@Test
public void shouldShow_noTetherableWifi() {
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[0]);
assertThat(mController.shouldShow()).isFalse();
}
@Test
public void onTetherStateUpdated_visibilityChangeCorrectly() {
int state = TetherEnabler.TETHERING_BLUETOOTH_ON;
mController.onTetherStateUpdated(state);
assertThat(mController.shouldShow()).isTrue();
state |= TetherEnabler.TETHERING_USB_ON;
mController.onTetherStateUpdated(state);
assertThat(mController.shouldShow()).isTrue();
state = TetherEnabler.TETHERING_USB_ON;
mController.onTetherStateUpdated(state);
assertThat(mController.shouldShow()).isTrue();
state = TetherEnabler.TETHERING_OFF;
mController.onTetherStateUpdated(state);
assertThat(mController.shouldShow()).isFalse();
}
@Test
public void getSummary_onTetherStateUpdated() {
mController.onTetherStateUpdated(mTetherState);
assertThat(mController.getSummary()).isEqualTo(mContext.getString(mSummaryResId));
}
}

View File

@@ -48,7 +48,6 @@ import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.FeatureFlagUtils;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
@@ -56,7 +55,6 @@ import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.RestrictedSettingsFragment;
import com.android.settings.core.FeatureFlags;
import com.android.settings.wifi.tether.WifiTetherPreferenceController;
import com.android.settingslib.RestrictedSwitchPreference;
@@ -145,7 +143,6 @@ public class TetherSettingsTest {
@Test
public void testTetherNonIndexableKeys_tetherAvailable_keysNotReturned() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
// To let TetherUtil.isTetherAvailable return true, select one of the combinations
setupIsTetherAvailable(true);
@@ -190,7 +187,6 @@ public class TetherSettingsTest {
@Test
public void testTetherNonIndexableKeys_usbAvailable_usbKeyNotReturned() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
// We can ignore the condition of Utils.isMonkeyRunning()
// In normal case, monkey and robotest should not execute at the same time
when(mTetheringManager.getTetherableUsbRegexs()).thenReturn(new String[]{"fakeRegex"});
@@ -213,7 +209,6 @@ public class TetherSettingsTest {
@Test
public void testTetherNonIndexableKeys_bluetoothAvailable_bluetoothKeyNotReturned() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
when(mTetheringManager.getTetherableBluetoothRegexs())
.thenReturn(new String[]{"fakeRegex"});

View File

@@ -1,171 +0,0 @@
/*
* Copyright (C) 2017 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.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.times;
import static org.mockito.Mockito.verify;
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 androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
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.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class WifiTetherApBandPreferenceControllerTest {
private static final String ALL_BANDS = "5.0 GHz Band preferred";
private static final String TWO_GHZ_STRING = "2.4 GHz Band";
private static final String FIVE_GHZ_STRING = "5.0 GHz Band";
private static final String VAL_2GHZ_STR = "1";
private static final String VAL_5GHZ_STR = "2";
private static final String VAL_2_5_GHZ_STR = "3";
private static final int VAL_2GHZ_INT = 1;
private static final int VAL_5GHZ_INT = 2;
private static final int VAL_2_5_GHZ_INT = 3;
private Context mContext;
@Mock
private TetheringManager mTetheringManager;
@Mock
private WifiManager mWifiManager;
@Mock
private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener;
@Mock
private PreferenceScreen mScreen;
private WifiTetherApBandPreferenceController mController;
private ListPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mPreference = new ListPreference(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources());
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
when(mWifiManager.getSoftApConfiguration()).thenReturn(
new SoftApConfiguration.Builder().build());
mController = new WifiTetherApBandPreferenceController(mContext, mListener);
}
@Test
public void display_5GhzSupported_shouldDisplayFullList() {
when(mWifiManager.getCountryCode()).thenReturn("US");
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
// Create a new instance
mController = new WifiTetherApBandPreferenceController(mContext, mListener);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VAL_2_5_GHZ_STR);
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
}
@Test
public void display_noCountryCode_shouldDisable() {
when(mWifiManager.getCountryCode()).thenReturn(null);
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
mController.displayPreference(mScreen);
assertThat(mPreference.isEnabled()).isFalse();
assertThat(mPreference.getSummary())
.isEqualTo(RuntimeEnvironment.application.getString(R.string.wifi_ap_choose_2G));
}
@Test
public void display_5GhzNotSupported_shouldDisable() {
when(mWifiManager.getCountryCode()).thenReturn("US");
when(mWifiManager.is5GHzBandSupported()).thenReturn(false);
mController.displayPreference(mScreen);
assertThat(mPreference.isEnabled()).isFalse();
assertThat(mPreference.getSummary())
.isEqualTo(RuntimeEnvironment.application.getString(R.string.wifi_ap_choose_2G));
}
@Test
public void changePreference_With5G_shouldUpdateValue() {
when(mWifiManager.getCountryCode()).thenReturn("US");
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
// Create a new instance to pick the proper value of isDualModeSupported()
mController = new WifiTetherApBandPreferenceController(mContext, mListener);
mController.displayPreference(mScreen);
// 'Auto' option
mController.onPreferenceChange(mPreference, VAL_2_5_GHZ_STR);
assertThat(mController.getBandIndex()).isEqualTo(VAL_2_5_GHZ_INT);
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
verify(mListener, times(1)).onTetherConfigUpdated(mController);
// should revert to the default for 5 Ghz only since this is not supported with this config
mController.onPreferenceChange(mPreference, VAL_5GHZ_STR);
assertThat(mController.getBandIndex()).isEqualTo(VAL_2_5_GHZ_INT);
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);
verify(mListener, times(2)).onTetherConfigUpdated(mController);
// set to 2 Ghz
mController.onPreferenceChange(mPreference, VAL_2GHZ_STR);
assertThat(mController.getBandIndex()).isEqualTo(VAL_2GHZ_INT);
assertThat(mPreference.getSummary()).isEqualTo(TWO_GHZ_STRING);
verify(mListener, times(3)).onTetherConfigUpdated(mController);
}
@Test
public void updateDisplay_shouldUpdateValue() {
when(mWifiManager.getCountryCode()).thenReturn("US");
when(mWifiManager.is5GHzBandSupported()).thenReturn(true);
// Set controller band index to 5GHz and verify is set.
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VAL_5GHZ_STR);
assertThat(mController.getBandIndex()).isEqualTo(VAL_2_5_GHZ_INT);
// Disable 5Ghz band
when(mWifiManager.is5GHzBandSupported()).thenReturn(false);
// Call updateDisplay and verify it's changed.
mController.updateDisplay();
assertThat(mController.getBandIndex()).isEqualTo(VAL_2GHZ_INT);
}
}

View File

@@ -49,7 +49,6 @@ import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserManager;
import android.util.FeatureFlagUtils;
import android.widget.TextView;
import androidx.fragment.app.FragmentActivity;
@@ -60,7 +59,6 @@ import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.core.FeatureFlags;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowFragment;
@@ -144,7 +142,6 @@ public class WifiTetherSettingsTest {
@Before
public void setUp() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
setCanShowWifiHotspotCached(true);
doReturn(mWifiManager).when(mContext).getSystemService(WifiManager.class);
doReturn(mConnectivityManager)