Make tether option preferences listen to own SharedPreferences change

The SharedPreferences value could be changed by other classes. This can
help make UI consistent with shared preferences value.

Bug: 148968321
Test: UsbTetherPreferenceControllerTest;
BluetoothTetherPreferenceControllerTest;
WifiTetherDisablePreferenceControllerTest;
CodeInspectionTest.

Change-Id: I11bdad2729e88c07ea1f046d9e95a6a63b398931
This commit is contained in:
Zhen Zhang
2020-02-26 12:54:20 -08:00
parent 4d59bce21b
commit 084875a104
5 changed files with 134 additions and 28 deletions

View File

@@ -27,8 +27,10 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
@@ -37,13 +39,17 @@ 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 ConnectivityManager mConnectivityManager;
@Mock
private SharedPreferences mSharedPreferences;
private SwitchPreference mSwitchPreference;
private BluetoothTetherPreferenceController mController;
private Context mContext;
@@ -52,10 +58,14 @@ public class BluetoothTetherPreferenceControllerTest {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
mSwitchPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mContext.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
mController = new BluetoothTetherPreferenceController(mContext, BLUETOOTH_TETHER_KEY);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
}
@Test
@@ -84,4 +94,20 @@ public class BluetoothTetherPreferenceControllerTest {
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void switch_shouldCheckedWhenSharedPreferenceIsTrue() {
when(mSharedPreferences.getBoolean(BLUETOOTH_TETHER_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences, BLUETOOTH_TETHER_KEY);
verify(mSwitchPreference).setChecked(true);
}
@Test
public void switch_shouldUnCheckedWhenSharedPreferenceIsFalse() {
when(mSharedPreferences.getBoolean(BLUETOOTH_TETHER_KEY, false)).thenReturn(false);
mController.onSharedPreferenceChanged(mSharedPreferences, BLUETOOTH_TETHER_KEY);
verify(mSwitchPreference).setChecked(false);
}
}

View File

@@ -27,8 +27,10 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
@@ -37,15 +39,19 @@ 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 ConnectivityManager mConnectivityManager;
@Mock
private SharedPreferences mSharedPreferences;
private Context mContext;
private UsbTetherPreferenceController mController;
private SwitchPreference mSwitchPreference;
@Before
public void setUp() {
@@ -55,7 +61,11 @@ public class UsbTetherPreferenceControllerTest {
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{""});
when(mContext.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
mController = new UsbTetherPreferenceController(mContext, USB_TETHER_KEY);
mSwitchPreference = spy(SwitchPreference.class);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
}
@Test
@@ -81,4 +91,20 @@ public class UsbTetherPreferenceControllerTest {
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void switch_shouldCheckedWhenSharedPreferencesIsTrue() {
when(mSharedPreferences.getBoolean(USB_TETHER_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences, USB_TETHER_KEY);
verify(mSwitchPreference).setChecked(true);
}
@Test
public void switch_shouldUnCheckedWhenSharedPreferencesIsFalse() {
when(mSharedPreferences.getBoolean(USB_TETHER_KEY, false)).thenReturn(false);
mController.onSharedPreferenceChanged(mSharedPreferences, USB_TETHER_KEY);
verify(mSwitchPreference).setChecked(false);
}
}