Fix TetherSettings crash issue

- When the user is a guest user, the UI will remove all preferences to restrict setting changes. If the ViewModel updates the UI in this situation, it will cause Settings to crash.
  - Avoid to setup ViewModel when UI is restricted

Bug: 284435378
Test: Manual test
atest -c TetherSettingsTest

Change-Id: I52d4ea717c34eacc9cc2321e3950dc89408049f8
This commit is contained in:
Weng Su
2023-05-31 20:34:30 +08:00
parent bbf55cafb9
commit 086d062cb2
2 changed files with 51 additions and 18 deletions

View File

@@ -45,6 +45,7 @@ import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.TetheringManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.FeatureFlagUtils;
@@ -54,6 +55,7 @@ import androidx.preference.Preference;
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;
@@ -66,6 +68,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.ArrayList;
import java.util.List;
@@ -92,7 +97,7 @@ public class TetherSettingsTest {
@Mock
private Preference mDataSaverFooter;
TetherSettings mTetherSettings;
private MockTetherSettings mTetherSettings;
@Before
public void setUp() throws Exception {
@@ -114,7 +119,7 @@ public class TetherSettingsTest {
when(mTetheringManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
when(mTetheringManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
mTetherSettings = spy(new TetherSettings());
mTetherSettings = spy(new MockTetherSettings());
mTetherSettings.mContext = mContext;
mTetherSettings.mWifiTetherPreferenceController = mWifiTetherPreferenceController;
mTetherSettings.mUsbTether = mUsbTether;
@@ -123,6 +128,16 @@ public class TetherSettingsTest {
mTetherSettings.mDataSaverFooter = mDataSaverFooter;
}
@Test
@Config(shadows = ShadowRestrictedSettingsFragment.class)
public void onCreate_isUiRestricted_doNotSetupViewModel() {
when(mTetherSettings.isUiRestricted()).thenReturn(true);
mTetherSettings.onCreate(null);
verify(mTetherSettings, never()).setupViewModel();
}
@Test
public void testTetherNonIndexableKeys_tetherAvailable_keysNotReturned() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
@@ -431,4 +446,19 @@ public class TetherSettingsTest {
mTetherSettings.registerReceiver();
updateOnlyBluetoothState(mTetherSettings);
}
private static class MockTetherSettings extends TetherSettings {
@Override
public boolean isUiRestricted() {
return false;
}
}
@Implements(RestrictedSettingsFragment.class)
public static final class ShadowRestrictedSettingsFragment {
@Implementation
public void onCreate(Bundle icicle) {
// do nothing
}
}
}