Not call MainSwitchBar.getSwitch()
This is private implement details, will be removed in future. Should call MainSwitchBar's isChecked() and setChecked() to ensure main switch bar's style is set correctly. Bug: 306658427 Test: manual - on wifi tether Test: manual - on developer options Test: m RunSettingsRoboTests Change-Id: I292ffbcf73da0721fc206e3dac7610a0aeb20047
This commit is contained in:
@@ -33,13 +33,11 @@ public class AudioSharingSwitchBarController
|
|||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final SettingsMainSwitchBar mSwitchBar;
|
private final SettingsMainSwitchBar mSwitchBar;
|
||||||
private final Switch mSwitch;
|
|
||||||
|
|
||||||
AudioSharingSwitchBarController(Context context, SettingsMainSwitchBar switchBar) {
|
AudioSharingSwitchBarController(Context context, SettingsMainSwitchBar switchBar) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mSwitchBar = switchBar;
|
mSwitchBar = switchBar;
|
||||||
mSwitch = mSwitchBar.getSwitch();
|
mSwitchBar.setChecked(false);
|
||||||
mSwitch.setChecked(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -334,9 +334,6 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||||
if (switchView != mSwitchBar.getSwitch()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final boolean developmentEnabledState =
|
final boolean developmentEnabledState =
|
||||||
DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(getContext());
|
DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(getContext());
|
||||||
if (isChecked != developmentEnabledState) {
|
if (isChecked != developmentEnabledState) {
|
||||||
|
@@ -54,7 +54,6 @@ public class WifiTetherSwitchBarController implements
|
|||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final SettingsMainSwitchBar mSwitchBar;
|
private final SettingsMainSwitchBar mSwitchBar;
|
||||||
private final Switch mSwitch;
|
|
||||||
private final ConnectivityManager mConnectivityManager;
|
private final ConnectivityManager mConnectivityManager;
|
||||||
private final WifiManager mWifiManager;
|
private final WifiManager mWifiManager;
|
||||||
|
|
||||||
@@ -78,7 +77,6 @@ public class WifiTetherSwitchBarController implements
|
|||||||
WifiTetherSwitchBarController(Context context, SettingsMainSwitchBar switchBar) {
|
WifiTetherSwitchBarController(Context context, SettingsMainSwitchBar switchBar) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mSwitchBar = switchBar;
|
mSwitchBar = switchBar;
|
||||||
mSwitch = mSwitchBar.getSwitch();
|
|
||||||
mDataSaverBackend = new DataSaverBackend(context);
|
mDataSaverBackend = new DataSaverBackend(context);
|
||||||
mConnectivityManager =
|
mConnectivityManager =
|
||||||
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
@@ -153,8 +151,8 @@ public class WifiTetherSwitchBarController implements
|
|||||||
if (state == WIFI_AP_STATE_ENABLING || state == WIFI_AP_STATE_DISABLING) return;
|
if (state == WIFI_AP_STATE_ENABLING || state == WIFI_AP_STATE_DISABLING) return;
|
||||||
|
|
||||||
final boolean shouldBeChecked = (state == WIFI_AP_STATE_ENABLED);
|
final boolean shouldBeChecked = (state == WIFI_AP_STATE_ENABLED);
|
||||||
if (mSwitch.isChecked() != shouldBeChecked) {
|
if (mSwitchBar.isChecked() != shouldBeChecked) {
|
||||||
mSwitch.setChecked(shouldBeChecked);
|
mSwitchBar.setChecked(shouldBeChecked);
|
||||||
}
|
}
|
||||||
updateWifiSwitch();
|
updateWifiSwitch();
|
||||||
}
|
}
|
||||||
|
@@ -27,7 +27,6 @@ import static org.mockito.Mockito.when;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.widget.Switch;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
@@ -65,7 +64,6 @@ import java.util.List;
|
|||||||
})
|
})
|
||||||
public class DevelopmentSettingsDashboardFragmentTest {
|
public class DevelopmentSettingsDashboardFragmentTest {
|
||||||
|
|
||||||
private Switch mSwitch;
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private ShadowUserManager mShadowUserManager;
|
private ShadowUserManager mShadowUserManager;
|
||||||
private DevelopmentSettingsDashboardFragment mDashboard;
|
private DevelopmentSettingsDashboardFragment mDashboard;
|
||||||
@@ -75,7 +73,6 @@ public class DevelopmentSettingsDashboardFragmentTest {
|
|||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
SettingsMainSwitchBar switchBar = new SettingsMainSwitchBar(mContext);
|
SettingsMainSwitchBar switchBar = new SettingsMainSwitchBar(mContext);
|
||||||
mSwitch = switchBar.getSwitch();
|
|
||||||
mDashboard = spy(new DevelopmentSettingsDashboardFragment());
|
mDashboard = spy(new DevelopmentSettingsDashboardFragment());
|
||||||
ReflectionHelpers.setField(mDashboard, "mSwitchBar", switchBar);
|
ReflectionHelpers.setField(mDashboard, "mSwitchBar", switchBar);
|
||||||
mShadowUserManager = Shadow.extract(mContext.getSystemService(Context.USER_SERVICE));
|
mShadowUserManager = Shadow.extract(mContext.getSystemService(Context.USER_SERVICE));
|
||||||
@@ -163,7 +160,7 @@ public class DevelopmentSettingsDashboardFragmentTest {
|
|||||||
Settings.Global.putInt(mContext.getContentResolver(),
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
|
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
|
||||||
|
|
||||||
mDashboard.onSwitchChanged(mSwitch, false /* isChecked */);
|
mDashboard.onSwitchChanged(null, false /* isChecked */);
|
||||||
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isFalse();
|
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +172,7 @@ public class DevelopmentSettingsDashboardFragmentTest {
|
|||||||
Settings.Global.putInt(mContext.getContentResolver(),
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
|
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
|
||||||
|
|
||||||
mDashboard.onSwitchChanged(mSwitch, true /* isChecked */);
|
mDashboard.onSwitchChanged(null, true /* isChecked */);
|
||||||
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isTrue();
|
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +184,7 @@ public class DevelopmentSettingsDashboardFragmentTest {
|
|||||||
Settings.Global.putInt(mContext.getContentResolver(),
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
|
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
|
||||||
|
|
||||||
mDashboard.onSwitchChanged(mSwitch, false /* isChecked */);
|
mDashboard.onSwitchChanged(null, false /* isChecked */);
|
||||||
|
|
||||||
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isFalse();
|
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isFalse();
|
||||||
assertThat(DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)).isFalse();
|
assertThat(DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)).isFalse();
|
||||||
@@ -206,7 +203,7 @@ public class DevelopmentSettingsDashboardFragmentTest {
|
|||||||
Settings.Global.putInt(mContext.getContentResolver(),
|
Settings.Global.putInt(mContext.getContentResolver(),
|
||||||
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
|
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
|
||||||
|
|
||||||
mDashboard.onSwitchChanged(mSwitch, false /* isChecked */);
|
mDashboard.onSwitchChanged(null, false /* isChecked */);
|
||||||
|
|
||||||
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||||
assertThat(dialog).isNotNull();
|
assertThat(dialog).isNotNull();
|
||||||
|
@@ -19,45 +19,42 @@ package com.android.settings.widget;
|
|||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.widget.CompoundButton;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
import com.android.settingslib.RestrictedLockUtils;
|
|
||||||
|
import com.android.settingslib.RestrictedLockUtils;
|
||||||
|
import com.android.settingslib.widget.mainswitch.R;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class SettingsMainSwitchBarTest {
|
public class SettingsMainSwitchBarTest {
|
||||||
|
|
||||||
private SettingsMainSwitchBar mMainSwitchBar;
|
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||||
|
private final SettingsMainSwitchBar mMainSwitchBar = new SettingsMainSwitchBar(mContext);
|
||||||
|
|
||||||
@Before
|
private final TextView mTitle = mMainSwitchBar.findViewById(R.id.switch_text);
|
||||||
public void setUp() {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
private final CompoundButton mSwitchWidget =
|
||||||
final Context context = RuntimeEnvironment.application;
|
mMainSwitchBar.findViewById(android.R.id.switch_widget);
|
||||||
mMainSwitchBar = new SettingsMainSwitchBar(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void disabledByAdmin_shouldBeDisabled() {
|
public void disabledByAdmin_shouldBeDisabled() {
|
||||||
mMainSwitchBar.setDisabledByAdmin(new RestrictedLockUtils.EnforcedAdmin());
|
mMainSwitchBar.setDisabledByAdmin(new RestrictedLockUtils.EnforcedAdmin());
|
||||||
|
|
||||||
TextView title = (TextView) mMainSwitchBar.findViewById(R.id.switch_text);
|
assertThat(mTitle.isEnabled()).isFalse();
|
||||||
assertThat(title.isEnabled()).isFalse();
|
assertThat(mSwitchWidget.isEnabled()).isFalse();
|
||||||
assertThat(mMainSwitchBar.getSwitch().isEnabled()).isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void disabledByAdmin_setNull_shouldBeEnabled() {
|
public void disabledByAdmin_setNull_shouldBeEnabled() {
|
||||||
mMainSwitchBar.setDisabledByAdmin(null);
|
mMainSwitchBar.setDisabledByAdmin(null);
|
||||||
|
|
||||||
TextView title = (TextView) mMainSwitchBar.findViewById(R.id.switch_text);
|
assertThat(mTitle.isEnabled()).isTrue();
|
||||||
assertThat(title.isEnabled()).isTrue();
|
assertThat(mSwitchWidget.isEnabled()).isTrue();
|
||||||
assertThat(mMainSwitchBar.getSwitch().isEnabled()).isTrue();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user