Update SharedPreference values based on tethering state

TetherEnabler need to update tethering state of each tethering
interfaces to make sure other preferences that depends on these
SharedPreferences be consistent with UI.

Bug: 148968321
Test: TetherEnablerTest; CodeInspectionTest. Manully test, built and
flashed to crosshatch device.

Change-Id: Ie0be7748adf20e6fb0ff5489795b0ca0664b6323
This commit is contained in:
Zhen Zhang
2020-02-26 15:53:34 -08:00
parent 91d2dbe2d4
commit 0c02d33818
2 changed files with 85 additions and 16 deletions

View File

@@ -69,6 +69,7 @@ public class TetherEnablerTest {
private SwitchBar mSwitchBar;
private TetherEnabler mEnabler;
private SwitchWidgetController mSwitchWidgetController;
private static final String[] USB_TETHERED = {"usb"};
@Before
public void setUp() {
@@ -84,9 +85,13 @@ public class TetherEnablerTest {
when(context.getSystemService(Context.NETWORK_POLICY_SERVICE)).thenReturn(
mNetworkPolicyManager);
when(mConnectivityManager.getTetherableIfaces()).thenReturn(new String[0]);
when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[0]);
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
panReference.set(mBluetoothPan);
when(context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class);
when(mSharedPreferences.edit()).thenReturn(editor);
mEnabler = spy(new TetherEnabler(context, mSwitchWidgetController, panReference));
}
@@ -103,7 +108,8 @@ public class TetherEnablerTest {
@Test
public void lifecycle_onStart_setCheckedCorrectly() {
when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[]{""});
when(mConnectivityManager.getTetheredIfaces()).thenReturn(USB_TETHERED);
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(USB_TETHERED);
mEnabler.onStart();
assertThat(mSwitchBar.isChecked()).isTrue();
@@ -188,7 +194,7 @@ public class TetherEnablerTest {
mSwitchWidgetController.setListener(mEnabler);
mSwitchWidgetController.startListening();
mEnabler.updateState(null/*tethered*/);
mEnabler.updateState(null /* tethered */);
verify(mEnabler, never()).onSwitchToggled(anyBoolean());
}
@@ -211,4 +217,28 @@ public class TetherEnablerTest {
mEnabler.updateState(new String[]{""});
verify(mSwitchBar).setEnabled(true);
}
@Test
public void updateState_onSharedPreferencesChangeNeverCalled() {
mSharedPreferences.registerOnSharedPreferenceChangeListener(mEnabler);
mSwitchWidgetController.setListener(mEnabler);
mSwitchWidgetController.startListening();
mEnabler.updateState(null /* tethered */);
verify(mEnabler, never()).onSharedPreferenceChanged(eq(mSharedPreferences), any());
verify(mEnabler, never()).onSharedPreferenceChanged(eq(mSharedPreferences), any());
}
@Test
public void updateState_setSharedPreferencesOnlyWhenNeeded() {
mSwitchWidgetController.setListener(mEnabler);
mSwitchWidgetController.startListening();
mEnabler.updateState(null /* tethered */);
verify(mSharedPreferences, never()).edit();
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(USB_TETHERED);
mSharedPreferences.registerOnSharedPreferenceChangeListener(mEnabler);
mEnabler.updateState(USB_TETHERED);
}
}