Remove unused SwitchBar

Only used in tests, so also migrate the tests.

Bug: 306658346
Test: manual - on bluetooth
Test: m RunSettingsRoboTests
Change-Id: Id2b010c1d9c32abe21dcc8765e9446fc4f7a5b25
This commit is contained in:
Chaohui Wang
2023-10-25 10:37:20 +08:00
parent 03a064fcd1
commit e752f7214b
24 changed files with 59 additions and 855 deletions

View File

@@ -39,8 +39,6 @@ import android.view.View;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedSwitchPreference;
@@ -80,6 +78,7 @@ public class BluetoothEnablerTest {
private SwitchWidgetController.OnSwitchChangeListener mCallback;
private Context mContext;
@Mock
private SwitchWidgetController mSwitchController;
private BluetoothEnabler mBluetoothEnabler;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@@ -90,7 +89,6 @@ public class BluetoothEnablerTest {
mContext = spy(RuntimeEnvironment.application);
mRestrictedSwitchPreference = new RestrictedSwitchPreference(mContext);
mSwitchController = spy(new SwitchBarController(new SwitchBar(mContext)));
mBluetoothEnabler = new BluetoothEnabler(
mContext,
mSwitchController,
@@ -215,7 +213,7 @@ public class BluetoothEnablerTest {
verify(mSwitchController, never()).setChecked(anyBoolean());
mBluetoothEnabler.resume(mContext);
verify(mSwitchController, never()).setChecked(false);
verify(mSwitchController).setChecked(true);
when(mSwitchController.isChecked()).thenReturn(true);
// Now simulate bluetooth being turned off via an event.
BroadcastReceiver receiver = captor.getValue();

View File

@@ -29,8 +29,7 @@ import androidx.lifecycle.LifecycleOwner;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.testutils.shadow.ShadowWirelessDebuggingPreferenceController;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.After;
@@ -49,23 +48,22 @@ import org.robolectric.util.ReflectionHelpers;
public class WirelessDebuggingEnablerTest {
@Mock
private SwitchBar mSwitchBar;
private SwitchWidgetController mSwitchWidgetController;
@Mock
private WirelessDebuggingEnabler.OnEnabledListener mListener;
private WirelessDebuggingEnabler mWirelessDebuggingEnabler;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mLifecycleOwner = () -> mLifecycle;
LifecycleOwner mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mWirelessDebuggingEnabler = spy(new WirelessDebuggingEnabler(
mContext, new SwitchBarController(mSwitchBar), mListener, mLifecycle));
mContext, mSwitchWidgetController, mListener, mLifecycle));
}
@After
@@ -75,14 +73,14 @@ public class WirelessDebuggingEnablerTest {
@Test
public void onCreation_shouldShowSwitchBar() {
verify(mSwitchBar).show();
verify(mSwitchWidgetController).setupView();
}
@Test
public void teardownSwitchController_shouldHideSwitchBar() {
mWirelessDebuggingEnabler.teardownSwitchController();
verify(mSwitchBar).hide();
verify(mSwitchWidgetController).teardownView();
}
@Test
@@ -92,7 +90,7 @@ public class WirelessDebuggingEnablerTest {
Global.ADB_WIFI_ENABLED, 0 /* setting disabled */);
mWirelessDebuggingEnabler.onResume();
verify(mSwitchBar).setChecked(false);
verify(mSwitchWidgetController).setChecked(false);
verify(mListener).onEnabled(false);
Global.putInt(mContext.getContentResolver(),
@@ -101,7 +99,7 @@ public class WirelessDebuggingEnablerTest {
ReflectionHelpers.getField(mWirelessDebuggingEnabler, "mSettingsObserver");
observer.onChange(true, Global.getUriFor(Global.ADB_WIFI_ENABLED));
verify(mSwitchBar).setChecked(true);
verify(mSwitchWidgetController).setChecked(true);
// Should also get a callback
verify(mListener).onEnabled(true);
}
@@ -112,7 +110,7 @@ public class WirelessDebuggingEnablerTest {
Global.ADB_WIFI_ENABLED, 1 /* setting enabled */);
mWirelessDebuggingEnabler.onResume();
verify(mSwitchBar).setChecked(true);
verify(mSwitchWidgetController).setChecked(true);
verify(mListener).onEnabled(true);
Global.putInt(mContext.getContentResolver(),
@@ -121,7 +119,7 @@ public class WirelessDebuggingEnablerTest {
ReflectionHelpers.getField(mWirelessDebuggingEnabler, "mSettingsObserver");
observer.onChange(true, Global.getUriFor(Global.ADB_WIFI_ENABLED));
verify(mSwitchBar).setChecked(false);
verify(mSwitchWidgetController).setChecked(false);
// Should also get a callback
verify(mListener).onEnabled(false);
}
@@ -133,7 +131,7 @@ public class WirelessDebuggingEnablerTest {
Global.ADB_WIFI_ENABLED, 0 /* setting disabled */);
mWirelessDebuggingEnabler.onResume();
verify(mSwitchBar).setChecked(false);
verify(mSwitchWidgetController).setChecked(false);
verify(mListener).onEnabled(false);
mWirelessDebuggingEnabler.onSwitchToggled(true);
@@ -149,7 +147,7 @@ public class WirelessDebuggingEnablerTest {
Global.ADB_WIFI_ENABLED, 0 /* setting disabled */);
mWirelessDebuggingEnabler.onResume();
verify(mSwitchBar).setChecked(false);
verify(mSwitchWidgetController).setChecked(false);
verify(mListener).onEnabled(false);
mWirelessDebuggingEnabler.onSwitchToggled(true);
@@ -165,7 +163,7 @@ public class WirelessDebuggingEnablerTest {
Global.ADB_WIFI_ENABLED, 1 /* setting disabled */);
mWirelessDebuggingEnabler.onResume();
verify(mSwitchBar).setChecked(true);
verify(mSwitchWidgetController).setChecked(true);
verify(mListener).onEnabled(true);
mWirelessDebuggingEnabler.onSwitchToggled(false);
@@ -181,7 +179,7 @@ public class WirelessDebuggingEnablerTest {
Global.ADB_WIFI_ENABLED, 1 /* setting disabled */);
mWirelessDebuggingEnabler.onResume();
verify(mSwitchBar).setChecked(true);
verify(mSwitchWidgetController).setChecked(true);
verify(mListener).onEnabled(true);
mWirelessDebuggingEnabler.onSwitchToggled(false);

View File

@@ -27,8 +27,6 @@ import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import com.android.settings.widget.SwitchWidgetController;
import org.junit.Before;
@@ -42,8 +40,6 @@ import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class GraphicsDriverGlobalSwitchBarControllerTest {
@Mock
private SwitchBar mSwitchBar;
@Mock
private SwitchWidgetController mSwitchWidgetController;
@Mock
@@ -65,9 +61,9 @@ public class GraphicsDriverGlobalSwitchBarControllerTest {
Settings.Global.putInt(
mResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, UPDATABLE_DRIVER_DEFAULT);
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mContext, mSwitchWidgetController);
verify(mSwitchBar).setChecked(true);
verify(mSwitchWidgetController).setChecked(true);
}
@Test
@@ -75,34 +71,33 @@ public class GraphicsDriverGlobalSwitchBarControllerTest {
Settings.Global.putInt(mResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS,
UPDATABLE_DRIVER_OFF);
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mContext, mSwitchWidgetController);
verify(mSwitchBar).setChecked(false);
verify(mSwitchWidgetController).setChecked(false);
}
@Test
public void constructor_developmentSettingsEnabled_shouldEnableSwitchBar() {
Settings.Global.putInt(mResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mContext, mSwitchWidgetController);
verify(mSwitchBar).setEnabled(true);
verify(mSwitchWidgetController).setEnabled(true);
}
@Test
public void constructor_developmentSettingsDisabled_shouldDisableSwitchBar() {
Settings.Global.putInt(mResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mContext, mSwitchWidgetController);
verify(mSwitchBar).setEnabled(false);
verify(mSwitchWidgetController).setEnabled(false);
}
@Test
public void onStart_shouldStartListeningAndRegister() {
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mController.mSwitchWidgetController = mSwitchWidgetController;
mContext, mSwitchWidgetController);
mController.mGraphicsDriverContentObserver = mGraphicsDriverContentObserver;
mController.onStart();
@@ -113,8 +108,7 @@ public class GraphicsDriverGlobalSwitchBarControllerTest {
@Test
public void onStop_shouldStopListeningAndUnregister() {
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mController.mSwitchWidgetController = mSwitchWidgetController;
mContext, mSwitchWidgetController);
mController.mGraphicsDriverContentObserver = mGraphicsDriverContentObserver;
mController.onStop();
@@ -127,7 +121,7 @@ public class GraphicsDriverGlobalSwitchBarControllerTest {
Settings.Global.putInt(mResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS,
UPDATABLE_DRIVER_OFF);
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mContext, mSwitchWidgetController);
mController.onSwitchToggled(true);
assertThat(Settings.Global.getInt(
@@ -141,7 +135,7 @@ public class GraphicsDriverGlobalSwitchBarControllerTest {
Settings.Global.putInt(
mResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, UPDATABLE_DRIVER_DEFAULT);
mController = new GraphicsDriverGlobalSwitchBarController(
mContext, new SwitchBarController(mSwitchBar));
mContext, mSwitchWidgetController);
mController.onSwitchToggled(false);
assertThat(Settings.Global.getInt(

View File

@@ -33,6 +33,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -51,8 +52,6 @@ import android.net.wifi.WifiManager;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.network.TetherEnabler.OnTetherStateUpdateListener;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import com.android.settings.widget.SwitchWidgetController;
import org.junit.Before;
@@ -82,8 +81,8 @@ public class TetherEnablerTest {
@Mock
private BluetoothAdapter mBluetoothAdapter;
private SwitchBar mSwitchBar;
private TetherEnabler mEnabler;
@Mock
private SwitchWidgetController mSwitchWidgetController;
private static final String[] USB_TETHERED = {"usb"};
@@ -93,8 +92,6 @@ public class TetherEnablerTest {
Context context = spy(ApplicationProvider.getApplicationContext());
AtomicReference<BluetoothPan> panReference = spy(AtomicReference.class);
mSwitchBar = spy(new SwitchBar(context));
mSwitchWidgetController = spy(new SwitchBarController(mSwitchBar));
when(context.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
@@ -115,7 +112,8 @@ public class TetherEnablerTest {
when(mTetheringManager.getTetherableUsbRegexs()).thenReturn(USB_TETHERED);
mEnabler.onStart();
assertThat(mSwitchBar.isChecked()).isTrue();
verify(mSwitchWidgetController).setChecked(true);
}
@Test
@@ -150,26 +148,29 @@ public class TetherEnablerTest {
mEnabler.onStart();
mEnabler.startTethering(TetheringManager.TETHERING_WIFI);
clearInvocations(mSwitchWidgetController);
when(mTetheringManager.getTetheredIfaces()).thenReturn(new String[0]);
mEnabler.mOnStartTetheringCallback.onTetheringFailed();
assertThat(mSwitchBar.isChecked()).isFalse();
assertThat(mSwitchBar.isEnabled()).isTrue();
verify(mSwitchWidgetController).setEnabled(true);
verify(mSwitchWidgetController).setChecked(false);
}
@Test
public void onDataSaverChanged_setsEnabledCorrectly() {
mSwitchBar.setEnabled(true);
mSwitchWidgetController.setEnabled(true);
// try to turn data saver on
clearInvocations(mSwitchWidgetController);
when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(true);
mEnabler.onDataSaverChanged(true);
assertThat(mSwitchBar.isEnabled()).isFalse();
verify(mSwitchWidgetController).setEnabled(false);
// lets turn data saver off again
clearInvocations(mSwitchWidgetController);
when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(false);
mEnabler.onDataSaverChanged(false);
assertThat(mSwitchBar.isEnabled()).isTrue();
verify(mSwitchWidgetController).setEnabled(true);
}
@Test
@@ -250,7 +251,7 @@ public class TetherEnablerTest {
ReflectionHelpers.setField(mEnabler, "mDataSaverEnabled", false);
mEnabler.updateState(null/*tethered*/);
verify(mSwitchBar).setEnabled(true);
verify(mSwitchWidgetController).setEnabled(true);
}
@Test
@@ -263,7 +264,7 @@ public class TetherEnablerTest {
ReflectionHelpers.setField(mEnabler, "mDataSaverEnabled", false);
mEnabler.updateState(null/*tethered*/);
verify(mSwitchBar).setEnabled(true);
verify(mSwitchWidgetController).setEnabled(true);
}
@Test

View File

@@ -17,8 +17,8 @@
package com.android.settings.users;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
@@ -26,8 +26,7 @@ import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import com.android.settings.widget.SwitchWidgetController;
import org.junit.After;
import org.junit.Before;
@@ -43,14 +42,14 @@ public class MultiUserSwitchBarControllerTest {
private Context mContext;
private ShadowUserManager mUserManager;
private SwitchBarController mSwitchBarController;
private SwitchWidgetController mSwitchWidgetController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mUserManager = ShadowUserManager.getShadow();
mSwitchBarController = spy(new SwitchBarController(new SwitchBar(mContext)));
mSwitchWidgetController = mock(SwitchWidgetController.class);
mUserManager.setSupportsMultipleUsers(true);
}
@@ -65,9 +64,9 @@ public class MultiUserSwitchBarControllerTest {
UserManager.DISALLOW_USER_SWITCH, true);
final MultiUserSwitchBarController controller = new MultiUserSwitchBarController(mContext,
mSwitchBarController, null);
mSwitchWidgetController, null);
verify(mSwitchBarController).setDisabledByAdmin(any());
verify(mSwitchWidgetController).setDisabledByAdmin(any());
}
@Test
@@ -76,8 +75,8 @@ public class MultiUserSwitchBarControllerTest {
UserManager.DISALLOW_USER_SWITCH, false);
final MultiUserSwitchBarController controller = new MultiUserSwitchBarController(mContext,
mSwitchBarController, null);
mSwitchWidgetController, null);
verify(mSwitchBarController, never()).setDisabledByAdmin(any());
verify(mSwitchWidgetController, never()).setDisabledByAdmin(any());
}
}

View File

@@ -1,123 +0,0 @@
/*
* Copyright (C) 2018 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.widget;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.RuntimeEnvironment.application;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class SwitchBarTest {
private static final int COLOR_BACKGROUND = 1;
private static final int COLOR_BACKGROUND_ACTIVATED = 2;
private Context mContext;
private SwitchBar mBar;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mBar = new SwitchBar(application, Robolectric.buildAttributeSet()
.addAttribute(R.attr.switchBarBackgroundColor, String.valueOf(COLOR_BACKGROUND))
.addAttribute(R.attr.switchBarBackgroundActivatedColor,
String.valueOf(COLOR_BACKGROUND_ACTIVATED))
.build());
}
@Test
public void cycleChecked_defaultLabel_shouldUpdateTextAndBackground() {
final int defaultOnText = R.string.switch_on_text;
final int defaultOffText = R.string.switch_off_text;
assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
.isEqualTo(mContext.getString(defaultOffText));
mBar.setChecked(true);
assertThat(mBar.getBackground()).isInstanceOf(ColorDrawable.class);
assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
.isEqualTo(mContext.getString(defaultOnText));
}
@Test
public void cycleChecked_customLabel_shouldUpdateTextAndBackground() {
final int onText = R.string.main_clear_progress_text;
final int offText = R.string.clear_user_data_text;
mBar.setSwitchBarText(onText, offText);
assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
.isEqualTo(mContext.getString(offText));
mBar.setChecked(true);
assertThat(mBar.getBackground()).isInstanceOf(ColorDrawable.class);
assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
.isEqualTo(mContext.getString(onText));
}
@Test
public void setCheck_customLabelWithStringType_shouldUpdateTextAndBackground() {
final String onText = mContext.getString(
R.string.accessibility_service_primary_switch_title);
final String offText = mContext.getString(
R.string.accessibility_service_primary_switch_title);
final TextView switchBarTextView = ((TextView) mBar.findViewById(R.id.switch_text));
mBar.setSwitchBarText(onText, offText);
assertThat(switchBarTextView.getText()).isEqualTo(offText);
mBar.setChecked(true);
assertThat(mBar.getBackground()).isInstanceOf(ColorDrawable.class);
assertThat(switchBarTextView.getText()).isEqualTo(onText);
}
@Test
public void disabledByAdmin_shouldDelegateToRestrictedIcon() {
mBar.setDisabledByAdmin(new EnforcedAdmin());
assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.restricted_icon);
}
@Test
public void notDisabledByAdmin_shouldDelegateToSwitch() {
mBar.setDisabledByAdmin(null);
assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.switch_widget);
}
@Test
public void performClick_shouldIsCheckedValueChange() {
boolean isChecked = mBar.isChecked();
mBar.performClick();
assertThat(mBar.isChecked()).isEqualTo(!isChecked);
}
}