Migrate PaymentSettings to DashboardFragment

- Move preference related logic to controllers.
- Add some test cases for controllers.

Test: manual
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.nfc
      make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.core
      atest SettingsGatewayTest UniquePreferenceTest
Change-Id: I061a194c170f63fab51974f26c24be43d67d6f6f
This commit is contained in:
chiujason
2018-04-12 15:52:00 +08:00
committed by Fan Zhang
parent 8efbe6e255
commit ff244c0e40
14 changed files with 833 additions and 407 deletions

View File

@@ -17,7 +17,6 @@
package com.android.settings.nfc;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -27,7 +26,6 @@ import android.nfc.NfcManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedLockUtils;
@@ -44,6 +42,8 @@ import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
public class AndroidBeamPreferenceControllerTest {

View File

@@ -0,0 +1,158 @@
/*
* 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 static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import androidx.preference.DropDownPreference;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
public class NfcForegroundPreferenceControllerTest {
private static final String PREF_KEY = PaymentSettingsTest.FOREGROUND_KEY;
@Mock
private PaymentBackend mPaymentBackend;
@Mock
private PreferenceScreen mScreen;
@Mock
private PackageManager mManager;
private Context mContext;
private DropDownPreference mPreference;
private NfcForegroundPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getPackageManager()).thenReturn(mManager);
mController = new NfcForegroundPreferenceController(mContext, PREF_KEY);
mPreference = new DropDownPreference(mContext);
when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
}
@Test
public void getAvailabilityStatus_noNFC_DISABLED() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(NfcForegroundPreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_noPaymentBackend_DISABLED() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(NfcForegroundPreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_noPaymentApps_DISABLED() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
mController.setPaymentBackend(mPaymentBackend);
when(mPaymentBackend.getPaymentAppInfos()).thenReturn(null);
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(NfcForegroundPreferenceController.AVAILABLE);
when(mPaymentBackend.getPaymentAppInfos()).thenReturn(new ArrayList<>());
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(NfcForegroundPreferenceController.AVAILABLE);
}
private void initPaymentApps() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
mController.setPaymentBackend(mPaymentBackend);
final ArrayList<PaymentBackend.PaymentAppInfo> appInfos = new ArrayList<>();
appInfos.add(new PaymentBackend.PaymentAppInfo());
when(mPaymentBackend.getPaymentAppInfos()).thenReturn(appInfos);
}
@Test
public void getAvailabilityStatus_hasPaymentApps_AVAILABLE() {
initPaymentApps();
assertThat(mController.getAvailabilityStatus())
.isEqualTo(NfcForegroundPreferenceController.AVAILABLE);
}
@Test
public void onStart_shouldRegisterCallback() {
mController.setPaymentBackend(mPaymentBackend);
mController.onStart();
verify(mPaymentBackend).registerCallback(mController);
}
@Test
public void onStop_shouldUnregisterCallback() {
mController.setPaymentBackend(mPaymentBackend);
mController.onStart();
mController.onStop();
verify(mPaymentBackend).unregisterCallback(mController);
}
@Test
public void changeOptions_shouldUpdateEntryAndSummary() {
initPaymentApps();
mController.displayPreference(mScreen);
mController.onPaymentAppsChanged();
final CharSequence favorDefault = mContext.getText(R.string.nfc_payment_favor_default);
final CharSequence favorOpen = mContext.getText(R.string.nfc_payment_favor_open);
assertThat(mPreference.getEntry()).isEqualTo(favorDefault);
assertThat(mPreference.getSummary()).isEqualTo(favorDefault);
mPreference.setValueIndex(0);
mPreference.callChangeListener(mPreference.getEntryValues()[0]);
verify(mPaymentBackend).setForegroundMode(true);
assertThat(mPreference.getEntry()).isEqualTo(favorOpen);
assertThat(mPreference.getSummary()).isEqualTo(favorOpen);
mPreference.setValueIndex(1);
mPreference.callChangeListener(mPreference.getEntryValues()[1]);
verify(mPaymentBackend).setForegroundMode(false);
assertThat(mPreference.getEntry()).isEqualTo(favorDefault);
assertThat(mPreference.getSummary()).isEqualTo(favorDefault);
}
}

View File

@@ -1,79 +0,0 @@
/*
* 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class NfcForegroundPreferenceTest {
@Mock
private PaymentBackend mPaymentBackend;
private Context mContext;
private PreferenceScreen mScreen;
private NfcForegroundPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mScreen = spy(new PreferenceScreen(mContext, null));
when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
when(mPaymentBackend.isForegroundMode()).thenReturn(false);
mPreference = new NfcForegroundPreference(mContext, mPaymentBackend);
mScreen.addPreference(mPreference);
}
@Test
public void testTogglingMode() {
String nfc_payment_favor_default = mContext.getString(R.string.nfc_payment_favor_default);
String nfc_payment_favor_open = mContext.getString(R.string.nfc_payment_favor_open);
assertThat(mPreference.getEntry()).isEqualTo(nfc_payment_favor_default);
assertThat(mPreference.getSummary()).isEqualTo(nfc_payment_favor_default);
mPreference.setValueIndex(0);
mPreference.callChangeListener(mPreference.getEntryValues()[0]);
verify(mPaymentBackend).setForegroundMode(true);
assertThat(mPreference.getEntry()).isEqualTo(nfc_payment_favor_open);
assertThat(mPreference.getSummary()).isEqualTo(nfc_payment_favor_open);
mPreference.setValueIndex(1);
mPreference.callChangeListener(mPreference.getEntryValues()[1]);
verify(mPaymentBackend).setForegroundMode(false);
assertThat(mPreference.getEntry()).isEqualTo(nfc_payment_favor_default);
assertThat(mPreference.getSummary()).isEqualTo(nfc_payment_favor_default);
}
}

View File

@@ -0,0 +1,150 @@
/*
* 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 static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import com.android.settings.R;
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
public class NfcPaymentPreferenceControllerTest {
private static final String PREF_KEY = PaymentSettingsTest.PAYMENT_KEY;
@Mock
private PaymentBackend mPaymentBackend;
@Mock
private PreferenceScreen mScreen;
@Mock
private PackageManager mManager;
private Context mContext;
private NfcPaymentPreference mPreference;
private NfcPaymentPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getPackageManager()).thenReturn(mManager);
mController = new NfcPaymentPreferenceController(mContext, PREF_KEY);
mPreference = spy(new NfcPaymentPreference(mContext, null));
when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
}
@Test
public void getAvailabilityStatus_noNFC_DISABLED() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_noPaymentApps_DISABLED() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
mController.setPaymentBackend(mPaymentBackend);
when(mPaymentBackend.getPaymentAppInfos()).thenReturn(null);
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
when(mPaymentBackend.getPaymentAppInfos()).thenReturn(new ArrayList<>());
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(NfcPaymentPreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_hasPaymentApps_AVAILABLE() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
mController.setPaymentBackend(mPaymentBackend);
final ArrayList<PaymentAppInfo> appInfos = new ArrayList<>();
appInfos.add(new PaymentAppInfo());
when(mPaymentBackend.getPaymentAppInfos()).thenReturn(appInfos);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(NfcPaymentPreferenceController.AVAILABLE);
}
@Test
public void onStart_shouldRegisterCallback() {
mController.setPaymentBackend(mPaymentBackend);
mController.onStart();
verify(mPaymentBackend).registerCallback(mController);
}
@Test
public void onStop_shouldUnregisterCallback() {
mController.setPaymentBackend(mPaymentBackend);
mController.onStart();
mController.onStop();
verify(mPaymentBackend).unregisterCallback(mController);
}
@Test
public void displayPreference_shouldInitialize() {
mController.setPaymentBackend(mPaymentBackend);
mController.displayPreference(mScreen);
verify(mPreference).initialize(mController);
}
@Test
public void onPaymentAppsChanged_shouldRefreshSummary() {
mController.setPaymentBackend(mPaymentBackend);
mController.displayPreference(mScreen);
when(mPaymentBackend.getDefaultApp()).thenReturn(null);
mController.onPaymentAppsChanged();
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getText(R.string.nfc_payment_default_not_set));
final PaymentAppInfo appInfo = new PaymentAppInfo();
appInfo.label = "test label";
when(mPaymentBackend.getDefaultApp()).thenReturn(appInfo);
mController.onPaymentAppsChanged();
assertThat(mPreference.getSummary()).isEqualTo(appInfo.label);
}
}

View File

@@ -17,7 +17,6 @@
package com.android.settings.nfc;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -26,8 +25,6 @@ import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.os.UserManager;
import android.provider.Settings;
import androidx.preference.SwitchPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -42,10 +39,13 @@ 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 {
Context mContext;
@Mock
private NfcAdapter mNfcAdapter;
@Mock
@@ -55,6 +55,7 @@ public class NfcPreferenceControllerTest {
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private SwitchPreference mNfcPreference;
private NfcPreferenceController mNfcController;

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.when;
import android.content.Context;
@@ -30,42 +31,66 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
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;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = PaymentSettingsTest.ShadowPaymentBackend.class)
public class PaymentSettingsTest {
@Mock
Context mContext;
static final String PAYMENT_KEY = "nfc_payment";
static final String FOREGROUND_KEY = "nfc_foreground";
private Context mContext;
@Mock
private PackageManager mManager;
private PaymentSettings mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = new PaymentSettings();
mContext = spy(RuntimeEnvironment.application);
when(mContext.getPackageManager()).thenReturn(mManager);
}
@Test
public void testNonIndexableKey_NoNFC_KeyAdded() {
public void getNonIndexableKey_NoNFC_AllKeysAdded() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
final List<String> niks =
PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(niks).contains(PaymentSettings.PAYMENT_KEY);
PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(niks).contains(PAYMENT_KEY);
assertThat(niks).contains(FOREGROUND_KEY);
}
@Test
public void testNonIndexableKey_NFC_NoKeyAdded() {
public void getNonIndexableKey_NFC_ForegroundKeyAdded() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
final List<String> niks =
PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(niks).isEmpty();
PaymentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(niks).contains(FOREGROUND_KEY);
}
@Implements(PaymentBackend.class)
public static class ShadowPaymentBackend {
private ArrayList<PaymentBackend.PaymentAppInfo> mAppInfos;
public void __constructor__(Context context) {
mAppInfos = new ArrayList<>();
mAppInfos.add(new PaymentBackend.PaymentAppInfo());
}
@Implementation
public List<PaymentBackend.PaymentAppInfo> getPaymentAppInfos() {
return mAppInfos;
}
}
}