Refactor nfc preference controller

- Remove BaseNfcPreferenceController.
- NfcPreferenceController inherit from TogglePreferenceController.
- AndroidBeamPreferenceController inherit from BasePreferenceController.
- Override getIntentFilter in NfcPreferenceController to listen changes.
- Add an API (hasAsyncUpdate) into BasePreferenceController to
distinguish the setting which is updated asynchronously.

Change-Id: I7c9c48ea7f1ad01a02524beabf9d30baa3db891f
Fixes: 67997761
Fixes: 74887543
Test: RunSettingsRoboTests
This commit is contained in:
Chihhang Chuang
2018-03-29 17:26:04 +08:00
parent 8f36eb8cfb
commit 3b387a028a
19 changed files with 474 additions and 214 deletions

View File

@@ -18,6 +18,7 @@ package com.android.settings.nfc;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
@@ -26,6 +27,9 @@ import android.nfc.NfcManager;
import android.os.UserManager;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
@@ -39,9 +43,6 @@ import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
@RunWith(SettingsRobolectricTestRunner.class)
public class NfcPreferenceControllerTest {
@@ -69,8 +70,10 @@ public class NfcPreferenceControllerTest {
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
mNfcController = new NfcPreferenceController(mContext);
mNfcController = new NfcPreferenceController(mContext,
NfcPreferenceController.KEY_TOGGLE_NFC);
mNfcPreference = new SwitchPreference(RuntimeEnvironment.application);
when(mScreen.findPreference(mNfcController.getPreferenceKey())).thenReturn(mNfcPreference);
Settings.Global.putString(mContext.getContentResolver(),
@@ -84,15 +87,17 @@ public class NfcPreferenceControllerTest {
}
@Test
public void isAvailable_hasNfc_shouldReturnTrue() {
public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
when(mNfcAdapter.isEnabled()).thenReturn(true);
assertThat(mNfcController.isAvailable()).isTrue();
assertThat(mNfcController.getAvailabilityStatus())
.isEqualTo(NfcPreferenceController.AVAILABLE);
}
@Test
public void isAvailable_noNfcAdapter_shouldReturnFalse() {
public void getAvailabilityStatus_noNfcAdapter_shouldReturnDisabledUnsupported() {
ReflectionHelpers.setField(mNfcController, "mNfcAdapter", null);
assertThat(mNfcController.isAvailable()).isFalse();
assertThat(mNfcController.getAvailabilityStatus())
.isEqualTo(NfcPreferenceController.UNSUPPORTED_ON_DEVICE);
}
@Test
@@ -158,4 +163,46 @@ public class NfcPreferenceControllerTest {
assertThat(keys).hasSize(1);
}
@Test
public void setChecked_True_nfcShouldEnable() {
mNfcController.setChecked(true);
mNfcController.onResume();
verify(mNfcAdapter).enable();
}
@Test
public void setChecked_False_nfcShouldDisable() {
mNfcController.setChecked(false);
mNfcController.onResume();
verify(mNfcAdapter).disable();
}
@Test
public void hasAsyncUpdate_shouldReturnTrue() {
assertThat(mNfcController.hasAsyncUpdate()).isTrue();
}
@Test
public void isToggleableInAirplaneMode_containNfc_shouldReturnTrue() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
Settings.Global.RADIO_NFC);
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 1);
assertThat(NfcPreferenceController.isToggleableInAirplaneMode(mContext)).isTrue();
}
@Test
public void isToggleableInAirplaneMode_withoutNfc_shouldReturnFalse() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
"null");
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 1);
assertThat(NfcPreferenceController.isToggleableInAirplaneMode(mContext)).isFalse();
}
}