Merge "Fix robotests failure in connectivity settings" into rvc-qpr-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
07bb0280f6
@@ -34,6 +34,7 @@ import androidx.test.core.content.pm.ApplicationInfoBuilder;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.nfc.NfcPreferenceController;
|
import com.android.settings.nfc.NfcPreferenceController;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -43,7 +44,7 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.Shadows;
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.shadows.ShadowNfcAdapter;
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.shadows.ShadowPackageManager;
|
import org.robolectric.shadows.ShadowPackageManager;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
@@ -70,7 +71,7 @@ public class AdvancedConnectedDeviceControllerTest {
|
|||||||
mContentResolver = mContext.getContentResolver();
|
mContentResolver = mContext.getContentResolver();
|
||||||
mNfcController = new NfcPreferenceController(mContext,
|
mNfcController = new NfcPreferenceController(mContext,
|
||||||
NfcPreferenceController.KEY_TOGGLE_NFC);
|
NfcPreferenceController.KEY_TOGGLE_NFC);
|
||||||
mShadowNfcAdapter = Shadows.shadowOf(NfcAdapter.getNfcAdapter(mContext));
|
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
|
||||||
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
|
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,7 +18,6 @@ package com.android.settings.connecteddevice;
|
|||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
@@ -26,35 +25,38 @@ import android.provider.SearchIndexableResource;
|
|||||||
|
|
||||||
import com.android.settings.nfc.AndroidBeamPreferenceController;
|
import com.android.settings.nfc.AndroidBeamPreferenceController;
|
||||||
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
|
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
import com.android.settings.testutils.shadow.ShadowUserManager;
|
import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||||
import com.android.settingslib.drawer.CategoryKey;
|
import com.android.settingslib.drawer.CategoryKey;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = {ShadowUserManager.class,
|
@Config(shadows = {ShadowUserManager.class,
|
||||||
ShadowConnectivityManager.class})
|
ShadowConnectivityManager.class, ShadowNfcAdapter.class})
|
||||||
public class AdvancedConnectedDeviceDashboardFragmentTest {
|
public class AdvancedConnectedDeviceDashboardFragmentTest {
|
||||||
|
|
||||||
private AdvancedConnectedDeviceDashboardFragment mFragment;
|
private AdvancedConnectedDeviceDashboardFragment mFragment;
|
||||||
|
|
||||||
@Mock
|
private Context mContext;
|
||||||
private NfcAdapter mNfcAdapter;
|
private ShadowNfcAdapter mShadowNfcAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mFragment = new AdvancedConnectedDeviceDashboardFragment();
|
mFragment = new AdvancedConnectedDeviceDashboardFragment();
|
||||||
|
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -79,13 +81,10 @@ public class AdvancedConnectedDeviceDashboardFragmentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSearchIndexProvider_correctNonIndexables() {
|
public void testSearchIndexProvider_correctNonIndexables() {
|
||||||
Context context = spy(RuntimeEnvironment.application);
|
mShadowNfcAdapter.setSecureNfcSupported(true);
|
||||||
when(context.getApplicationContext()).thenReturn(context);
|
|
||||||
when(NfcAdapter.getDefaultAdapter(context)).thenReturn(mNfcAdapter);
|
|
||||||
when(mNfcAdapter.isSecureNfcSupported()).thenReturn(true);
|
|
||||||
final List<String> niks =
|
final List<String> niks =
|
||||||
AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
|
AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
|
||||||
.getNonIndexableKeys(context);
|
.getNonIndexableKeys(mContext);
|
||||||
|
|
||||||
assertThat(niks).contains(AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
|
assertThat(niks).contains(AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
|
||||||
}
|
}
|
||||||
|
@@ -29,6 +29,7 @@ import android.nfc.NfcManager;
|
|||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -37,9 +38,12 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(shadows = ShadowNfcAdapter.class)
|
||||||
public class NfcAndPaymentFragmentControllerTest {
|
public class NfcAndPaymentFragmentControllerTest {
|
||||||
private NfcAndPaymentFragmentController mController;
|
private NfcAndPaymentFragmentController mController;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@@ -50,29 +54,28 @@ public class NfcAndPaymentFragmentControllerTest {
|
|||||||
private UserManager mUserManager;
|
private UserManager mUserManager;
|
||||||
@Mock
|
@Mock
|
||||||
private NfcManager mNfcManager;
|
private NfcManager mNfcManager;
|
||||||
@Mock
|
|
||||||
private NfcAdapter mNfcAdapter;
|
private ShadowNfcAdapter mShadowNfcAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
|
||||||
|
|
||||||
when(mContext.getApplicationContext()).thenReturn(mContext);
|
when(mContext.getApplicationContext()).thenReturn(mContext);
|
||||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||||
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mNfcManager);
|
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mNfcManager);
|
||||||
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
|
|
||||||
|
|
||||||
mController = new NfcAndPaymentFragmentController(mContext, "fakeKey");
|
mController = new NfcAndPaymentFragmentController(mContext, "fakeKey");
|
||||||
ReflectionHelpers.setField(mController, "mNfcAdapter", mNfcAdapter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
|
public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
|
||||||
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
|
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
|
||||||
when(mUserManager.isAdminUser()).thenReturn(true);
|
when(mUserManager.isAdminUser()).thenReturn(true);
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(true);
|
mShadowNfcAdapter.setEnabled(true);
|
||||||
|
|
||||||
assertThat(mController.getAvailabilityStatus())
|
assertThat(mController.getAvailabilityStatus())
|
||||||
.isEqualTo(NfcAndPaymentFragmentController.AVAILABLE);
|
.isEqualTo(NfcAndPaymentFragmentController.AVAILABLE);
|
||||||
@@ -87,14 +90,14 @@ public class NfcAndPaymentFragmentControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSummary_nfcOn_shouldProvideOnSummary() {
|
public void getSummary_nfcOn_shouldProvideOnSummary() {
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(true);
|
mShadowNfcAdapter.setEnabled(true);
|
||||||
assertThat(mController.getSummary().toString()).contains(
|
assertThat(mController.getSummary().toString()).contains(
|
||||||
mContext.getString(R.string.switch_on_text));
|
mContext.getString(R.string.switch_on_text));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSummary_nfcOff_shouldProvideOffSummary() {
|
public void getSummary_nfcOff_shouldProvideOffSummary() {
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(false);
|
mShadowNfcAdapter.setEnabled(false);
|
||||||
assertThat(mController.getSummary().toString()).contains(
|
assertThat(mController.getSummary().toString()).contains(
|
||||||
mContext.getString(R.string.switch_off_text));
|
mContext.getString(R.string.switch_off_text));
|
||||||
}
|
}
|
||||||
|
@@ -22,9 +22,12 @@ import static org.mockito.Mockito.spy;
|
|||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
|
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -32,16 +35,20 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(shadows = ShadowNfcAdapter.class)
|
||||||
public class NfcAndPaymentFragmentTest {
|
public class NfcAndPaymentFragmentTest {
|
||||||
|
@Mock
|
||||||
|
private PackageManager mPackageManager;
|
||||||
|
|
||||||
private NfcAndPaymentFragment mFragment;
|
private NfcAndPaymentFragment mFragment;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private ShadowNfcAdapter mShadowNfcAdapter;
|
||||||
@Mock
|
|
||||||
private NfcAdapter mNfcAdapter;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -49,6 +56,9 @@ public class NfcAndPaymentFragmentTest {
|
|||||||
|
|
||||||
mFragment = new NfcAndPaymentFragment();
|
mFragment = new NfcAndPaymentFragment();
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
|
||||||
|
|
||||||
|
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -64,8 +74,10 @@ public class NfcAndPaymentFragmentTest {
|
|||||||
@Test
|
@Test
|
||||||
public void searchIndexProvider_shouldIndexValidItems() {
|
public void searchIndexProvider_shouldIndexValidItems() {
|
||||||
when(mContext.getApplicationContext()).thenReturn(mContext);
|
when(mContext.getApplicationContext()).thenReturn(mContext);
|
||||||
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
|
||||||
when(mNfcAdapter.isSecureNfcSupported()).thenReturn(true);
|
when(mPackageManager.hasSystemFeature(
|
||||||
|
PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)).thenReturn(true);
|
||||||
|
mShadowNfcAdapter.setSecureNfcSupported(true);
|
||||||
|
|
||||||
final List<String> niks = NfcAndPaymentFragment.SEARCH_INDEX_DATA_PROVIDER
|
final List<String> niks = NfcAndPaymentFragment.SEARCH_INDEX_DATA_PROVIDER
|
||||||
.getNonIndexableKeys(mContext);
|
.getNonIndexableKeys(mContext);
|
||||||
|
@@ -31,6 +31,7 @@ import android.provider.Settings;
|
|||||||
|
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
import com.android.settingslib.RestrictedLockUtilsInternal;
|
import com.android.settingslib.RestrictedLockUtilsInternal;
|
||||||
import com.android.settingslib.RestrictedPreference;
|
import com.android.settingslib.RestrictedPreference;
|
||||||
|
|
||||||
@@ -41,19 +42,20 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(shadows = ShadowNfcAdapter.class)
|
||||||
public class AndroidBeamPreferenceControllerTest {
|
public class AndroidBeamPreferenceControllerTest {
|
||||||
|
|
||||||
Context mContext;
|
Context mContext;
|
||||||
@Mock
|
@Mock
|
||||||
private NfcAdapter mNfcAdapter;
|
NfcManager mNfcManager;
|
||||||
@Mock
|
|
||||||
NfcManager mManager;
|
|
||||||
@Mock
|
@Mock
|
||||||
private UserManager mUserManager;
|
private UserManager mUserManager;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -63,18 +65,19 @@ public class AndroidBeamPreferenceControllerTest {
|
|||||||
|
|
||||||
private RestrictedPreference mAndroidBeamPreference;
|
private RestrictedPreference mAndroidBeamPreference;
|
||||||
private AndroidBeamPreferenceController mAndroidBeamController;
|
private AndroidBeamPreferenceController mAndroidBeamController;
|
||||||
|
private ShadowNfcAdapter mShadowNfcAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
|
||||||
|
|
||||||
when(mContext.getApplicationContext()).thenReturn(mContext);
|
when(mContext.getApplicationContext()).thenReturn(mContext);
|
||||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||||
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
|
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mNfcManager);
|
||||||
when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
|
when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
|
||||||
UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(false);
|
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);
|
AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
|
||||||
@@ -95,13 +98,13 @@ public class AndroidBeamPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_hasNfc_shouldReturnTrue() {
|
public void isAvailable_hasNfc_shouldReturnTrue() {
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(true);
|
mShadowNfcAdapter.setEnabled(true);
|
||||||
assertThat(mAndroidBeamController.isAvailable()).isTrue();
|
assertThat(mAndroidBeamController.isAvailable()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_noNfcFeature_shouldReturnFalse() {
|
public void isAvailable_noNfcFeature_shouldReturnFalse() {
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(true);
|
mShadowNfcAdapter.setEnabled(true);
|
||||||
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)).thenReturn(false);
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)).thenReturn(false);
|
||||||
assertThat(mAndroidBeamController.isAvailable()).isFalse();
|
assertThat(mAndroidBeamController.isAvailable()).isFalse();
|
||||||
}
|
}
|
||||||
@@ -114,7 +117,7 @@ public class AndroidBeamPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
|
public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF);
|
||||||
|
|
||||||
when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
|
when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
|
||||||
UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
|
UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
|
||||||
@@ -125,7 +128,7 @@ public class AndroidBeamPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
|
public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_ON);
|
||||||
try {
|
try {
|
||||||
mAndroidBeamController.onResume();
|
mAndroidBeamController.onResume();
|
||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
@@ -137,22 +140,22 @@ public class AndroidBeamPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
|
public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF);
|
||||||
mAndroidBeamController.onResume();
|
mAndroidBeamController.onResume();
|
||||||
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
|
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
|
||||||
|
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_ON);
|
||||||
mAndroidBeamController.onResume();
|
mAndroidBeamController.onResume();
|
||||||
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
|
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
|
||||||
|
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_OFF);
|
||||||
mAndroidBeamController.onResume();
|
mAndroidBeamController.onResume();
|
||||||
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
|
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateNonIndexableKeys_available_shouldNotUpdate() {
|
public void updateNonIndexableKeys_available_shouldNotUpdate() {
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(true);
|
mShadowNfcAdapter.setEnabled(true);
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
|
|
||||||
mAndroidBeamController.updateNonIndexableKeys(keys);
|
mAndroidBeamController.updateNonIndexableKeys(keys);
|
||||||
|
@@ -29,6 +29,7 @@ import androidx.preference.PreferenceScreen;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
|
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -37,10 +38,12 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(shadows = ShadowNfcAdapter.class)
|
||||||
public class NfcPaymentPreferenceControllerTest {
|
public class NfcPaymentPreferenceControllerTest {
|
||||||
|
|
||||||
private static final String PREF_KEY = PaymentSettingsTest.PAYMENT_KEY;
|
private static final String PREF_KEY = PaymentSettingsTest.PAYMENT_KEY;
|
||||||
|
@@ -35,6 +35,7 @@ import androidx.preference.SwitchPreference;
|
|||||||
|
|
||||||
import com.android.settings.nfc.NfcPreferenceController.NfcSliceWorker;
|
import com.android.settings.nfc.NfcPreferenceController.NfcSliceWorker;
|
||||||
import com.android.settings.nfc.NfcPreferenceController.NfcSliceWorker.NfcUpdateReceiver;
|
import com.android.settings.nfc.NfcPreferenceController.NfcSliceWorker.NfcUpdateReceiver;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -43,16 +44,17 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(shadows = ShadowNfcAdapter.class)
|
||||||
public class NfcPreferenceControllerTest {
|
public class NfcPreferenceControllerTest {
|
||||||
|
|
||||||
@Mock
|
|
||||||
private NfcAdapter mNfcAdapter;
|
|
||||||
@Mock
|
@Mock
|
||||||
NfcManager mManager;
|
NfcManager mManager;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -63,16 +65,19 @@ public class NfcPreferenceControllerTest {
|
|||||||
private Context mContext;
|
private Context mContext;
|
||||||
private SwitchPreference mNfcPreference;
|
private SwitchPreference mNfcPreference;
|
||||||
private NfcPreferenceController mNfcController;
|
private NfcPreferenceController mNfcController;
|
||||||
|
private ShadowNfcAdapter mShadowNfcAdapter;
|
||||||
|
private NfcAdapter mNfcAdapter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
|
||||||
|
mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext);
|
||||||
|
|
||||||
when(mContext.getApplicationContext()).thenReturn(mContext);
|
when(mContext.getApplicationContext()).thenReturn(mContext);
|
||||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||||
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
|
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);
|
NfcPreferenceController.KEY_TOGGLE_NFC);
|
||||||
@@ -83,7 +88,7 @@ public class NfcPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
|
public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(true);
|
mShadowNfcAdapter.setEnabled(true);
|
||||||
assertThat(mNfcController.getAvailabilityStatus())
|
assertThat(mNfcController.getAvailabilityStatus())
|
||||||
.isEqualTo(NfcPreferenceController.AVAILABLE);
|
.isEqualTo(NfcPreferenceController.AVAILABLE);
|
||||||
}
|
}
|
||||||
@@ -98,11 +103,11 @@ public class NfcPreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void isNfcEnable_nfcStateNotTurning_shouldReturnTrue() {
|
public void isNfcEnable_nfcStateNotTurning_shouldReturnTrue() {
|
||||||
mNfcController.displayPreference(mScreen);
|
mNfcController.displayPreference(mScreen);
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_ON);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isEnabled()).isTrue();
|
assertThat(mNfcPreference.isEnabled()).isTrue();
|
||||||
|
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isEnabled()).isTrue();
|
assertThat(mNfcPreference.isEnabled()).isTrue();
|
||||||
}
|
}
|
||||||
@@ -110,11 +115,11 @@ public class NfcPreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void isNfcEnable_nfcStateTurning_shouldReturnFalse() {
|
public void isNfcEnable_nfcStateTurning_shouldReturnFalse() {
|
||||||
mNfcController.displayPreference(mScreen);
|
mNfcController.displayPreference(mScreen);
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_ON);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isEnabled()).isFalse();
|
assertThat(mNfcPreference.isEnabled()).isFalse();
|
||||||
|
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_OFF);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isEnabled()).isFalse();
|
assertThat(mNfcPreference.isEnabled()).isFalse();
|
||||||
}
|
}
|
||||||
@@ -122,29 +127,29 @@ public class NfcPreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void isNfcChecked_nfcStateOn_shouldReturnTrue() {
|
public void isNfcChecked_nfcStateOn_shouldReturnTrue() {
|
||||||
mNfcController.displayPreference(mScreen);
|
mNfcController.displayPreference(mScreen);
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_ON);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isChecked()).isTrue();
|
assertThat(mNfcPreference.isChecked()).isTrue();
|
||||||
|
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_ON);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isChecked()).isTrue();
|
assertThat(mNfcPreference.isChecked()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isNfcChecked_nfcStateOff_shouldReturnFalse() {
|
public void isNfcChecked_nfcStateOff_shouldReturnFalse() {
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isChecked()).isFalse();
|
assertThat(mNfcPreference.isChecked()).isFalse();
|
||||||
|
|
||||||
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
|
mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_OFF);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
assertThat(mNfcPreference.isChecked()).isFalse();
|
assertThat(mNfcPreference.isChecked()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateNonIndexableKeys_available_shouldNotUpdate() {
|
public void updateNonIndexableKeys_available_shouldNotUpdate() {
|
||||||
when(mNfcAdapter.isEnabled()).thenReturn(true);
|
mShadowNfcAdapter.setEnabled(true);
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
|
|
||||||
mNfcController.updateNonIndexableKeys(keys);
|
mNfcController.updateNonIndexableKeys(keys);
|
||||||
@@ -167,7 +172,7 @@ public class NfcPreferenceControllerTest {
|
|||||||
mNfcController.setChecked(true);
|
mNfcController.setChecked(true);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
|
|
||||||
verify(mNfcAdapter).enable();
|
assertThat(mNfcAdapter.isEnabled()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -175,7 +180,7 @@ public class NfcPreferenceControllerTest {
|
|||||||
mNfcController.setChecked(false);
|
mNfcController.setChecked(false);
|
||||||
mNfcController.onResume();
|
mNfcController.onResume();
|
||||||
|
|
||||||
verify(mNfcAdapter).disable();
|
assertThat(mNfcAdapter.isEnabled()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -33,6 +33,8 @@ import androidx.preference.Preference;
|
|||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -48,7 +50,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = PaymentSettingsTest.ShadowPaymentBackend.class)
|
@Config(shadows = {PaymentSettingsTest.ShadowPaymentBackend.class, ShadowNfcAdapter.class})
|
||||||
public class PaymentSettingsTest {
|
public class PaymentSettingsTest {
|
||||||
|
|
||||||
static final String PAYMENT_KEY = "nfc_payment";
|
static final String PAYMENT_KEY = "nfc_payment";
|
||||||
|
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.android.settings.testutils.shadow;
|
package com.android.settings.testutils.shadow;
|
||||||
|
|
||||||
|
import static org.robolectric.shadow.api.Shadow.newInstanceOf;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
@@ -24,16 +26,18 @@ import android.os.Bundle;
|
|||||||
import org.robolectric.annotation.Implementation;
|
import org.robolectric.annotation.Implementation;
|
||||||
import org.robolectric.annotation.Implements;
|
import org.robolectric.annotation.Implements;
|
||||||
import org.robolectric.annotation.Resetter;
|
import org.robolectric.annotation.Resetter;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
|
||||||
import org.robolectric.util.ReflectionHelpers.ClassParameter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shadow of {@link NfcAdapter}.
|
* Shadow of {@link NfcAdapter}.
|
||||||
*/
|
*/
|
||||||
@Implements(NfcAdapter.class)
|
@Implements(value = NfcAdapter.class)
|
||||||
public class ShadowNfcAdapter {
|
public class ShadowNfcAdapter extends org.robolectric.shadows.ShadowNfcAdapter {
|
||||||
private static boolean sReaderModeEnabled;
|
private static boolean sReaderModeEnabled;
|
||||||
|
private static Object sNfcAdapter = newInstanceOf("android.nfc.NfcAdapter");
|
||||||
|
|
||||||
private boolean mIsNfcEnabled = false;
|
private boolean mIsNfcEnabled = false;
|
||||||
|
private int mState = NfcAdapter.STATE_ON;
|
||||||
|
private boolean mIsSecureNfcSupported = false;
|
||||||
|
|
||||||
@Implementation
|
@Implementation
|
||||||
protected void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback,
|
protected void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback,
|
||||||
@@ -43,8 +47,7 @@ public class ShadowNfcAdapter {
|
|||||||
|
|
||||||
@Implementation
|
@Implementation
|
||||||
protected static NfcAdapter getDefaultAdapter(Context context) {
|
protected static NfcAdapter getDefaultAdapter(Context context) {
|
||||||
return ReflectionHelpers.callConstructor(
|
return (NfcAdapter) sNfcAdapter;
|
||||||
NfcAdapter.class, ClassParameter.from(Context.class, context));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Implementation
|
@Implementation
|
||||||
@@ -52,6 +55,28 @@ public class ShadowNfcAdapter {
|
|||||||
return mIsNfcEnabled;
|
return mIsNfcEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enable) {
|
||||||
|
mIsNfcEnabled = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
protected int getAdapterState() {
|
||||||
|
return mState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdapterState(int state) {
|
||||||
|
this.mState = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
protected boolean isSecureNfcSupported() {
|
||||||
|
return mIsSecureNfcSupported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecureNfcSupported(boolean supported) {
|
||||||
|
this.mIsSecureNfcSupported = supported;
|
||||||
|
}
|
||||||
|
|
||||||
@Implementation
|
@Implementation
|
||||||
protected boolean enable() {
|
protected boolean enable() {
|
||||||
mIsNfcEnabled = true;
|
mIsNfcEnabled = true;
|
||||||
|
Reference in New Issue
Block a user