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: I1abe4410169e305a0d6106e24c54e7f2e763fc91 Merged-In: I7c9c48ea7f1ad01a02524beabf9d30baa3db891f Fixes: 67997761 Fixes: 74887543 Test: RunSettingsRoboTests
This commit is contained in:
@@ -55,7 +55,8 @@ public class AdvancedConnectedDeviceControllerTest {
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mContentResolver = mContext.getContentResolver();
|
||||
mNfcController = new NfcPreferenceController(mContext);
|
||||
mNfcController = new NfcPreferenceController(mContext,
|
||||
NfcPreferenceController.KEY_TOGGLE_NFC);
|
||||
mShadowNfcAdapter = shadowOf(ShadowNfcAdapter.getNfcAdapter(mContext));
|
||||
}
|
||||
|
||||
|
@@ -134,6 +134,11 @@ public class BasePreferenceControllerTest {
|
||||
assertThat(mPreferenceController.getSliceType()).isEqualTo(SliceData.SliceType.INTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasAsyncUpdate_shouldReturnFalse() {
|
||||
assertThat(mPreferenceController.hasAsyncUpdate()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void settingAvailable_disabledOnDisplayPreference_preferenceEnabled() {
|
||||
final PreferenceScreen screen = mock(PreferenceScreen.class);
|
||||
|
@@ -72,7 +72,8 @@ public class AndroidBeamPreferenceControllerTest {
|
||||
UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(false);
|
||||
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
|
||||
|
||||
mAndroidBeamController = new AndroidBeamPreferenceController(mContext);
|
||||
mAndroidBeamController = new AndroidBeamPreferenceController(mContext,
|
||||
AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
|
||||
mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application);
|
||||
when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn(
|
||||
mAndroidBeamPreference);
|
||||
|
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.nfc;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.nfc.NfcAdapter;
|
||||
import android.provider.Settings;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowNfcAdapter.class})
|
||||
public class NfcAirplaneModeObserverTest {
|
||||
|
||||
Context mContext;
|
||||
private NfcAdapter mNfcAdapter;
|
||||
private SwitchPreference mNfcPreference;
|
||||
private NfcAirplaneModeObserver mNfcAirplaneModeObserver;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ShadowApplication.getInstance().getApplicationContext();
|
||||
mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext);
|
||||
|
||||
mNfcPreference = new SwitchPreference(RuntimeEnvironment.application);
|
||||
|
||||
mNfcAirplaneModeObserver = new NfcAirplaneModeObserver(mContext, mNfcAdapter,
|
||||
(Preference) mNfcPreference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void NfcAirplaneModeObserver_airplaneOn_shouldDisableNfc() {
|
||||
ReflectionHelpers.setField(mNfcAirplaneModeObserver,
|
||||
"mAirplaneMode", 0);
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.AIRPLANE_MODE_ON, 1);
|
||||
|
||||
mNfcAirplaneModeObserver.onChange(false,
|
||||
NfcAirplaneModeObserver.AIRPLANE_MODE_URI);
|
||||
|
||||
assertThat(mNfcAdapter.isEnabled()).isFalse();
|
||||
assertThat(mNfcPreference.isEnabled()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void NfcAirplaneModeObserver_airplaneOff_shouldEnableNfc() {
|
||||
ReflectionHelpers.setField(mNfcAirplaneModeObserver,
|
||||
"mAirplaneMode", 1);
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.AIRPLANE_MODE_ON, 0);
|
||||
|
||||
mNfcAirplaneModeObserver.onChange(false,
|
||||
NfcAirplaneModeObserver.AIRPLANE_MODE_URI);
|
||||
|
||||
assertThat(mNfcAdapter.isEnabled()).isTrue();
|
||||
assertThat(mNfcPreference.isEnabled()).isTrue();
|
||||
}
|
||||
}
|
@@ -19,6 +19,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,8 +27,8 @@ import android.nfc.NfcAdapter;
|
||||
import android.nfc.NfcManager;
|
||||
import android.os.UserManager;
|
||||
import android.provider.Settings;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
@@ -68,8 +69,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(),
|
||||
@@ -83,15 +86,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
|
||||
@@ -157,4 +162,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();
|
||||
}
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@@ -125,6 +126,60 @@ public class SliceBroadcastReceiverTest {
|
||||
assertThat(valuePair.second).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toggleUpdate_synchronously_notifyChange_should_be_called() {
|
||||
// Monitor the ContentResolver
|
||||
final ContentResolver resolver = spy(mContext.getContentResolver());
|
||||
doReturn(resolver).when(mContext).getContentResolver();
|
||||
|
||||
final String key = "key";
|
||||
mSearchFeatureProvider.getSearchIndexableResources().getProviderValues().clear();
|
||||
insertSpecialCase(key);
|
||||
|
||||
FakeToggleController fakeToggleController = new FakeToggleController(mContext, key);
|
||||
fakeToggleController.setChecked(true);
|
||||
// Set the toggle setting update synchronously.
|
||||
fakeToggleController.setAsyncUpdate(false);
|
||||
Intent intent = new Intent(SettingsSliceProvider.ACTION_TOGGLE_CHANGED);
|
||||
intent.putExtra(SettingsSliceProvider.EXTRA_SLICE_KEY, key);
|
||||
|
||||
assertThat(fakeToggleController.isChecked()).isTrue();
|
||||
|
||||
// Toggle setting
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
|
||||
assertThat(fakeToggleController.isChecked()).isFalse();
|
||||
|
||||
final Uri expectedUri = SliceBuilderUtils.getUri(
|
||||
SettingsSlicesContract.PATH_SETTING_ACTION + "/" + key, false);
|
||||
verify(resolver).notifyChange(eq(expectedUri), eq(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toggleUpdate_asynchronously_notifyChange_should_not_be_called() {
|
||||
// Monitor the ContentResolver
|
||||
final ContentResolver resolver = spy(mContext.getContentResolver());
|
||||
doReturn(resolver).when(mContext).getContentResolver();
|
||||
|
||||
final String key = "key";
|
||||
mSearchFeatureProvider.getSearchIndexableResources().getProviderValues().clear();
|
||||
insertSpecialCase(key);
|
||||
|
||||
FakeToggleController fakeToggleController = new FakeToggleController(mContext, key);
|
||||
fakeToggleController.setChecked(true);
|
||||
// Set the toggle setting update asynchronously.
|
||||
fakeToggleController.setAsyncUpdate(true);
|
||||
Intent intent = new Intent(SettingsSliceProvider.ACTION_TOGGLE_CHANGED);
|
||||
intent.putExtra(SettingsSliceProvider.EXTRA_SLICE_KEY, key);
|
||||
|
||||
assertThat(fakeToggleController.isChecked()).isTrue();
|
||||
|
||||
// Toggle setting
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
|
||||
verify(resolver, never()).notifyChange(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_sliderChanged() {
|
||||
final String key = "key";
|
||||
|
@@ -36,6 +36,8 @@ public class FakeToggleController extends TogglePreferenceController {
|
||||
private final int ON = 1;
|
||||
private final int OFF = 0;
|
||||
|
||||
private boolean mIsAsyncUpdate = false;
|
||||
|
||||
public FakeToggleController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
}
|
||||
@@ -67,4 +69,13 @@ public class FakeToggleController extends TogglePreferenceController {
|
||||
public boolean isSliceable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAsyncUpdate() {
|
||||
return mIsAsyncUpdate;
|
||||
}
|
||||
|
||||
public void setAsyncUpdate(boolean isAsyncUpdate) {
|
||||
mIsAsyncUpdate = isAsyncUpdate;
|
||||
}
|
||||
}
|
||||
|
@@ -33,6 +33,7 @@ import org.robolectric.util.ReflectionHelpers.ClassParameter;
|
||||
@Implements(NfcAdapter.class)
|
||||
public class ShadowNfcAdapter {
|
||||
private static boolean sReaderModeEnabled;
|
||||
private boolean mIsNfcEnabled = false;
|
||||
|
||||
@Implementation
|
||||
public void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback, int flags,
|
||||
@@ -46,6 +47,23 @@ public class ShadowNfcAdapter {
|
||||
NfcAdapter.class, ClassParameter.from(Context.class, context));
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public boolean isEnabled() {
|
||||
return mIsNfcEnabled;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public boolean enable() {
|
||||
mIsNfcEnabled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public boolean disable() {
|
||||
mIsNfcEnabled = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isReaderModeEnabled() {
|
||||
return sReaderModeEnabled;
|
||||
}
|
||||
|
Reference in New Issue
Block a user