packages/apps/Settings
Revisão | 07bb0280f64162d3f84e70fbfa7d74deb4e264fb (tree) |
---|---|
Hora | 2021-01-12 08:08:40 |
Autor | Alisher Alikhodjaev <alisher@goog...> |
Commiter | Android (Google) Code Review |
Merge "Fix robotests failure in connectivity settings" into rvc-qpr-dev
@@ -34,6 +34,7 @@ import androidx.test.core.content.pm.ApplicationInfoBuilder; | ||
34 | 34 | |
35 | 35 | import com.android.settings.R; |
36 | 36 | import com.android.settings.nfc.NfcPreferenceController; |
37 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
37 | 38 | |
38 | 39 | import org.junit.Before; |
39 | 40 | import org.junit.Test; |
@@ -43,7 +44,7 @@ import org.robolectric.RobolectricTestRunner; | ||
43 | 44 | import org.robolectric.RuntimeEnvironment; |
44 | 45 | import org.robolectric.Shadows; |
45 | 46 | import org.robolectric.annotation.Config; |
46 | -import org.robolectric.shadows.ShadowNfcAdapter; | |
47 | +import org.robolectric.shadow.api.Shadow; | |
47 | 48 | import org.robolectric.shadows.ShadowPackageManager; |
48 | 49 | import org.robolectric.util.ReflectionHelpers; |
49 | 50 |
@@ -70,7 +71,7 @@ public class AdvancedConnectedDeviceControllerTest { | ||
70 | 71 | mContentResolver = mContext.getContentResolver(); |
71 | 72 | mNfcController = new NfcPreferenceController(mContext, |
72 | 73 | NfcPreferenceController.KEY_TOGGLE_NFC); |
73 | - mShadowNfcAdapter = Shadows.shadowOf(NfcAdapter.getNfcAdapter(mContext)); | |
74 | + mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext)); | |
74 | 75 | mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager()); |
75 | 76 | } |
76 | 77 |
@@ -18,7 +18,6 @@ package com.android.settings.connecteddevice; | ||
18 | 18 | import static com.google.common.truth.Truth.assertThat; |
19 | 19 | |
20 | 20 | import static org.mockito.Mockito.spy; |
21 | -import static org.mockito.Mockito.when; | |
22 | 21 | |
23 | 22 | import android.content.Context; |
24 | 23 | import android.nfc.NfcAdapter; |
@@ -26,35 +25,38 @@ import android.provider.SearchIndexableResource; | ||
26 | 25 | |
27 | 26 | import com.android.settings.nfc.AndroidBeamPreferenceController; |
28 | 27 | import com.android.settings.testutils.shadow.ShadowConnectivityManager; |
28 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
29 | 29 | import com.android.settings.testutils.shadow.ShadowUserManager; |
30 | 30 | import com.android.settingslib.drawer.CategoryKey; |
31 | 31 | |
32 | 32 | import org.junit.Before; |
33 | 33 | import org.junit.Test; |
34 | 34 | import org.junit.runner.RunWith; |
35 | -import org.mockito.Mock; | |
36 | 35 | import org.mockito.MockitoAnnotations; |
37 | 36 | import org.robolectric.RobolectricTestRunner; |
38 | 37 | import org.robolectric.RuntimeEnvironment; |
39 | 38 | import org.robolectric.annotation.Config; |
39 | +import org.robolectric.shadow.api.Shadow; | |
40 | 40 | |
41 | 41 | import java.util.List; |
42 | 42 | |
43 | 43 | @RunWith(RobolectricTestRunner.class) |
44 | 44 | @Config(shadows = {ShadowUserManager.class, |
45 | - ShadowConnectivityManager.class}) | |
45 | + ShadowConnectivityManager.class, ShadowNfcAdapter.class}) | |
46 | 46 | public class AdvancedConnectedDeviceDashboardFragmentTest { |
47 | 47 | |
48 | 48 | private AdvancedConnectedDeviceDashboardFragment mFragment; |
49 | 49 | |
50 | - @Mock | |
51 | - private NfcAdapter mNfcAdapter; | |
50 | + private Context mContext; | |
51 | + private ShadowNfcAdapter mShadowNfcAdapter; | |
52 | 52 | |
53 | 53 | @Before |
54 | 54 | public void setUp() { |
55 | 55 | MockitoAnnotations.initMocks(this); |
56 | 56 | |
57 | + mContext = spy(RuntimeEnvironment.application); | |
57 | 58 | mFragment = new AdvancedConnectedDeviceDashboardFragment(); |
59 | + mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext)); | |
58 | 60 | } |
59 | 61 | |
60 | 62 | @Test |
@@ -79,13 +81,10 @@ public class AdvancedConnectedDeviceDashboardFragmentTest { | ||
79 | 81 | |
80 | 82 | @Test |
81 | 83 | public void testSearchIndexProvider_correctNonIndexables() { |
82 | - Context context = spy(RuntimeEnvironment.application); | |
83 | - when(context.getApplicationContext()).thenReturn(context); | |
84 | - when(NfcAdapter.getDefaultAdapter(context)).thenReturn(mNfcAdapter); | |
85 | - when(mNfcAdapter.isSecureNfcSupported()).thenReturn(true); | |
84 | + mShadowNfcAdapter.setSecureNfcSupported(true); | |
86 | 85 | final List<String> niks = |
87 | 86 | AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER |
88 | - .getNonIndexableKeys(context); | |
87 | + .getNonIndexableKeys(mContext); | |
89 | 88 | |
90 | 89 | assertThat(niks).contains(AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS); |
91 | 90 | } |
@@ -29,6 +29,7 @@ import android.nfc.NfcManager; | ||
29 | 29 | import android.os.UserManager; |
30 | 30 | |
31 | 31 | import com.android.settings.R; |
32 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
32 | 33 | |
33 | 34 | import org.junit.Before; |
34 | 35 | import org.junit.Test; |
@@ -37,9 +38,12 @@ import org.mockito.Mock; | ||
37 | 38 | import org.mockito.MockitoAnnotations; |
38 | 39 | import org.robolectric.RobolectricTestRunner; |
39 | 40 | import org.robolectric.RuntimeEnvironment; |
41 | +import org.robolectric.annotation.Config; | |
42 | +import org.robolectric.shadow.api.Shadow; | |
40 | 43 | import org.robolectric.util.ReflectionHelpers; |
41 | 44 | |
42 | 45 | @RunWith(RobolectricTestRunner.class) |
46 | +@Config(shadows = ShadowNfcAdapter.class) | |
43 | 47 | public class NfcAndPaymentFragmentControllerTest { |
44 | 48 | private NfcAndPaymentFragmentController mController; |
45 | 49 | private Context mContext; |
@@ -50,29 +54,28 @@ public class NfcAndPaymentFragmentControllerTest { | ||
50 | 54 | private UserManager mUserManager; |
51 | 55 | @Mock |
52 | 56 | private NfcManager mNfcManager; |
53 | - @Mock | |
54 | - private NfcAdapter mNfcAdapter; | |
57 | + | |
58 | + private ShadowNfcAdapter mShadowNfcAdapter; | |
55 | 59 | |
56 | 60 | @Before |
57 | 61 | public void setUp() { |
58 | 62 | MockitoAnnotations.initMocks(this); |
59 | 63 | mContext = spy(RuntimeEnvironment.application); |
64 | + mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext)); | |
60 | 65 | |
61 | 66 | when(mContext.getApplicationContext()).thenReturn(mContext); |
62 | 67 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
63 | 68 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
64 | 69 | when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mNfcManager); |
65 | - when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter); | |
66 | 70 | |
67 | 71 | mController = new NfcAndPaymentFragmentController(mContext, "fakeKey"); |
68 | - ReflectionHelpers.setField(mController, "mNfcAdapter", mNfcAdapter); | |
69 | 72 | } |
70 | 73 | |
71 | 74 | @Test |
72 | 75 | public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() { |
73 | 76 | when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); |
74 | 77 | when(mUserManager.isAdminUser()).thenReturn(true); |
75 | - when(mNfcAdapter.isEnabled()).thenReturn(true); | |
78 | + mShadowNfcAdapter.setEnabled(true); | |
76 | 79 | |
77 | 80 | assertThat(mController.getAvailabilityStatus()) |
78 | 81 | .isEqualTo(NfcAndPaymentFragmentController.AVAILABLE); |
@@ -87,14 +90,14 @@ public class NfcAndPaymentFragmentControllerTest { | ||
87 | 90 | |
88 | 91 | @Test |
89 | 92 | public void getSummary_nfcOn_shouldProvideOnSummary() { |
90 | - when(mNfcAdapter.isEnabled()).thenReturn(true); | |
93 | + mShadowNfcAdapter.setEnabled(true); | |
91 | 94 | assertThat(mController.getSummary().toString()).contains( |
92 | 95 | mContext.getString(R.string.switch_on_text)); |
93 | 96 | } |
94 | 97 | |
95 | 98 | @Test |
96 | 99 | public void getSummary_nfcOff_shouldProvideOffSummary() { |
97 | - when(mNfcAdapter.isEnabled()).thenReturn(false); | |
100 | + mShadowNfcAdapter.setEnabled(false); | |
98 | 101 | assertThat(mController.getSummary().toString()).contains( |
99 | 102 | mContext.getString(R.string.switch_off_text)); |
100 | 103 | } |
@@ -22,9 +22,12 @@ import static org.mockito.Mockito.spy; | ||
22 | 22 | import static org.mockito.Mockito.when; |
23 | 23 | |
24 | 24 | import android.content.Context; |
25 | +import android.content.pm.PackageManager; | |
25 | 26 | import android.nfc.NfcAdapter; |
26 | 27 | import android.provider.SearchIndexableResource; |
27 | 28 | |
29 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
30 | + | |
28 | 31 | import org.junit.Before; |
29 | 32 | import org.junit.Test; |
30 | 33 | import org.junit.runner.RunWith; |
@@ -32,16 +35,20 @@ import org.mockito.Mock; | ||
32 | 35 | import org.mockito.MockitoAnnotations; |
33 | 36 | import org.robolectric.RobolectricTestRunner; |
34 | 37 | import org.robolectric.RuntimeEnvironment; |
38 | +import org.robolectric.annotation.Config; | |
39 | +import org.robolectric.shadow.api.Shadow; | |
35 | 40 | |
36 | 41 | import java.util.List; |
37 | 42 | |
38 | 43 | @RunWith(RobolectricTestRunner.class) |
44 | +@Config(shadows = ShadowNfcAdapter.class) | |
39 | 45 | public class NfcAndPaymentFragmentTest { |
46 | + @Mock | |
47 | + private PackageManager mPackageManager; | |
48 | + | |
40 | 49 | private NfcAndPaymentFragment mFragment; |
41 | 50 | private Context mContext; |
42 | - | |
43 | - @Mock | |
44 | - private NfcAdapter mNfcAdapter; | |
51 | + private ShadowNfcAdapter mShadowNfcAdapter; | |
45 | 52 | |
46 | 53 | @Before |
47 | 54 | public void setUp() { |
@@ -49,6 +56,9 @@ public class NfcAndPaymentFragmentTest { | ||
49 | 56 | |
50 | 57 | mFragment = new NfcAndPaymentFragment(); |
51 | 58 | mContext = spy(RuntimeEnvironment.application); |
59 | + mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext)); | |
60 | + | |
61 | + when(mContext.getPackageManager()).thenReturn(mPackageManager); | |
52 | 62 | } |
53 | 63 | |
54 | 64 | @Test |
@@ -64,8 +74,10 @@ public class NfcAndPaymentFragmentTest { | ||
64 | 74 | @Test |
65 | 75 | public void searchIndexProvider_shouldIndexValidItems() { |
66 | 76 | when(mContext.getApplicationContext()).thenReturn(mContext); |
67 | - when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter); | |
68 | - when(mNfcAdapter.isSecureNfcSupported()).thenReturn(true); | |
77 | + when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); | |
78 | + when(mPackageManager.hasSystemFeature( | |
79 | + PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)).thenReturn(true); | |
80 | + mShadowNfcAdapter.setSecureNfcSupported(true); | |
69 | 81 | |
70 | 82 | final List<String> niks = NfcAndPaymentFragment.SEARCH_INDEX_DATA_PROVIDER |
71 | 83 | .getNonIndexableKeys(mContext); |
@@ -31,6 +31,7 @@ import android.provider.Settings; | ||
31 | 31 | |
32 | 32 | import androidx.preference.PreferenceScreen; |
33 | 33 | |
34 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
34 | 35 | import com.android.settingslib.RestrictedLockUtilsInternal; |
35 | 36 | import com.android.settingslib.RestrictedPreference; |
36 | 37 |
@@ -41,19 +42,20 @@ import org.mockito.Mock; | ||
41 | 42 | import org.mockito.MockitoAnnotations; |
42 | 43 | import org.robolectric.RobolectricTestRunner; |
43 | 44 | import org.robolectric.RuntimeEnvironment; |
45 | +import org.robolectric.annotation.Config; | |
46 | +import org.robolectric.shadow.api.Shadow; | |
44 | 47 | import org.robolectric.util.ReflectionHelpers; |
45 | 48 | |
46 | 49 | import java.util.ArrayList; |
47 | 50 | import java.util.List; |
48 | 51 | |
49 | 52 | @RunWith(RobolectricTestRunner.class) |
53 | +@Config(shadows = ShadowNfcAdapter.class) | |
50 | 54 | public class AndroidBeamPreferenceControllerTest { |
51 | 55 | |
52 | 56 | Context mContext; |
53 | 57 | @Mock |
54 | - private NfcAdapter mNfcAdapter; | |
55 | - @Mock | |
56 | - NfcManager mManager; | |
58 | + NfcManager mNfcManager; | |
57 | 59 | @Mock |
58 | 60 | private UserManager mUserManager; |
59 | 61 | @Mock |
@@ -63,18 +65,19 @@ public class AndroidBeamPreferenceControllerTest { | ||
63 | 65 | |
64 | 66 | private RestrictedPreference mAndroidBeamPreference; |
65 | 67 | private AndroidBeamPreferenceController mAndroidBeamController; |
68 | + private ShadowNfcAdapter mShadowNfcAdapter; | |
66 | 69 | |
67 | 70 | @Before |
68 | 71 | public void setUp() { |
69 | 72 | MockitoAnnotations.initMocks(this); |
70 | 73 | mContext = spy(RuntimeEnvironment.application); |
74 | + mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext)); | |
71 | 75 | |
72 | 76 | when(mContext.getApplicationContext()).thenReturn(mContext); |
73 | 77 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
74 | - when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager); | |
78 | + when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mNfcManager); | |
75 | 79 | when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, |
76 | 80 | UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(false); |
77 | - when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter); | |
78 | 81 | |
79 | 82 | mAndroidBeamController = new AndroidBeamPreferenceController(mContext, |
80 | 83 | AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS); |
@@ -95,13 +98,13 @@ public class AndroidBeamPreferenceControllerTest { | ||
95 | 98 | |
96 | 99 | @Test |
97 | 100 | public void isAvailable_hasNfc_shouldReturnTrue() { |
98 | - when(mNfcAdapter.isEnabled()).thenReturn(true); | |
101 | + mShadowNfcAdapter.setEnabled(true); | |
99 | 102 | assertThat(mAndroidBeamController.isAvailable()).isTrue(); |
100 | 103 | } |
101 | 104 | |
102 | 105 | @Test |
103 | 106 | public void isAvailable_noNfcFeature_shouldReturnFalse() { |
104 | - when(mNfcAdapter.isEnabled()).thenReturn(true); | |
107 | + mShadowNfcAdapter.setEnabled(true); | |
105 | 108 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)).thenReturn(false); |
106 | 109 | assertThat(mAndroidBeamController.isAvailable()).isFalse(); |
107 | 110 | } |
@@ -114,7 +117,7 @@ public class AndroidBeamPreferenceControllerTest { | ||
114 | 117 | |
115 | 118 | @Test |
116 | 119 | public void isBeamEnable_disAllowBeam_shouldReturnFalse() { |
117 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF); | |
120 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF); | |
118 | 121 | |
119 | 122 | when(RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, |
120 | 123 | UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true); |
@@ -125,7 +128,7 @@ public class AndroidBeamPreferenceControllerTest { | ||
125 | 128 | |
126 | 129 | @Test |
127 | 130 | public void isBeamEnable_nfcStateOn_shouldReturnTrue() { |
128 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON); | |
131 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_ON); | |
129 | 132 | try { |
130 | 133 | mAndroidBeamController.onResume(); |
131 | 134 | } catch (NullPointerException e) { |
@@ -137,22 +140,22 @@ public class AndroidBeamPreferenceControllerTest { | ||
137 | 140 | |
138 | 141 | @Test |
139 | 142 | public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() { |
140 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF); | |
143 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF); | |
141 | 144 | mAndroidBeamController.onResume(); |
142 | 145 | assertThat(mAndroidBeamPreference.isEnabled()).isFalse(); |
143 | 146 | |
144 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON); | |
147 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_ON); | |
145 | 148 | mAndroidBeamController.onResume(); |
146 | 149 | assertThat(mAndroidBeamPreference.isEnabled()).isFalse(); |
147 | 150 | |
148 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF); | |
151 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_OFF); | |
149 | 152 | mAndroidBeamController.onResume(); |
150 | 153 | assertThat(mAndroidBeamPreference.isEnabled()).isFalse(); |
151 | 154 | } |
152 | 155 | |
153 | 156 | @Test |
154 | 157 | public void updateNonIndexableKeys_available_shouldNotUpdate() { |
155 | - when(mNfcAdapter.isEnabled()).thenReturn(true); | |
158 | + mShadowNfcAdapter.setEnabled(true); | |
156 | 159 | final List<String> keys = new ArrayList<>(); |
157 | 160 | |
158 | 161 | mAndroidBeamController.updateNonIndexableKeys(keys); |
@@ -29,6 +29,7 @@ import androidx.preference.PreferenceScreen; | ||
29 | 29 | |
30 | 30 | import com.android.settings.R; |
31 | 31 | import com.android.settings.nfc.PaymentBackend.PaymentAppInfo; |
32 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
32 | 33 | |
33 | 34 | import org.junit.Before; |
34 | 35 | import org.junit.Test; |
@@ -37,10 +38,12 @@ import org.mockito.Mock; | ||
37 | 38 | import org.mockito.MockitoAnnotations; |
38 | 39 | import org.robolectric.RobolectricTestRunner; |
39 | 40 | import org.robolectric.RuntimeEnvironment; |
41 | +import org.robolectric.annotation.Config; | |
40 | 42 | |
41 | 43 | import java.util.ArrayList; |
42 | 44 | |
43 | 45 | @RunWith(RobolectricTestRunner.class) |
46 | +@Config(shadows = ShadowNfcAdapter.class) | |
44 | 47 | public class NfcPaymentPreferenceControllerTest { |
45 | 48 | |
46 | 49 | private static final String PREF_KEY = PaymentSettingsTest.PAYMENT_KEY; |
@@ -35,6 +35,7 @@ import androidx.preference.SwitchPreference; | ||
35 | 35 | |
36 | 36 | import com.android.settings.nfc.NfcPreferenceController.NfcSliceWorker; |
37 | 37 | import com.android.settings.nfc.NfcPreferenceController.NfcSliceWorker.NfcUpdateReceiver; |
38 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
38 | 39 | |
39 | 40 | import org.junit.Before; |
40 | 41 | import org.junit.Test; |
@@ -43,17 +44,18 @@ import org.mockito.Mock; | ||
43 | 44 | import org.mockito.MockitoAnnotations; |
44 | 45 | import org.robolectric.RobolectricTestRunner; |
45 | 46 | import org.robolectric.RuntimeEnvironment; |
47 | +import org.robolectric.annotation.Config; | |
48 | +import org.robolectric.shadow.api.Shadow; | |
46 | 49 | import org.robolectric.util.ReflectionHelpers; |
47 | 50 | |
48 | 51 | import java.util.ArrayList; |
49 | 52 | import java.util.List; |
50 | 53 | |
51 | 54 | @RunWith(RobolectricTestRunner.class) |
55 | +@Config(shadows = ShadowNfcAdapter.class) | |
52 | 56 | public class NfcPreferenceControllerTest { |
53 | 57 | |
54 | 58 | @Mock |
55 | - private NfcAdapter mNfcAdapter; | |
56 | - @Mock | |
57 | 59 | NfcManager mManager; |
58 | 60 | @Mock |
59 | 61 | private UserManager mUserManager; |
@@ -63,16 +65,19 @@ public class NfcPreferenceControllerTest { | ||
63 | 65 | private Context mContext; |
64 | 66 | private SwitchPreference mNfcPreference; |
65 | 67 | private NfcPreferenceController mNfcController; |
68 | + private ShadowNfcAdapter mShadowNfcAdapter; | |
69 | + private NfcAdapter mNfcAdapter; | |
66 | 70 | |
67 | 71 | @Before |
68 | 72 | public void setUp() { |
69 | 73 | MockitoAnnotations.initMocks(this); |
70 | 74 | mContext = spy(RuntimeEnvironment.application); |
75 | + mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext)); | |
76 | + mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext); | |
71 | 77 | |
72 | 78 | when(mContext.getApplicationContext()).thenReturn(mContext); |
73 | 79 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
74 | 80 | when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager); |
75 | - when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter); | |
76 | 81 | |
77 | 82 | mNfcController = new NfcPreferenceController(mContext, |
78 | 83 | NfcPreferenceController.KEY_TOGGLE_NFC); |
@@ -83,7 +88,7 @@ public class NfcPreferenceControllerTest { | ||
83 | 88 | |
84 | 89 | @Test |
85 | 90 | public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() { |
86 | - when(mNfcAdapter.isEnabled()).thenReturn(true); | |
91 | + mShadowNfcAdapter.setEnabled(true); | |
87 | 92 | assertThat(mNfcController.getAvailabilityStatus()) |
88 | 93 | .isEqualTo(NfcPreferenceController.AVAILABLE); |
89 | 94 | } |
@@ -98,11 +103,11 @@ public class NfcPreferenceControllerTest { | ||
98 | 103 | @Test |
99 | 104 | public void isNfcEnable_nfcStateNotTurning_shouldReturnTrue() { |
100 | 105 | mNfcController.displayPreference(mScreen); |
101 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON); | |
106 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_ON); | |
102 | 107 | mNfcController.onResume(); |
103 | 108 | assertThat(mNfcPreference.isEnabled()).isTrue(); |
104 | 109 | |
105 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF); | |
110 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF); | |
106 | 111 | mNfcController.onResume(); |
107 | 112 | assertThat(mNfcPreference.isEnabled()).isTrue(); |
108 | 113 | } |
@@ -110,11 +115,11 @@ public class NfcPreferenceControllerTest { | ||
110 | 115 | @Test |
111 | 116 | public void isNfcEnable_nfcStateTurning_shouldReturnFalse() { |
112 | 117 | mNfcController.displayPreference(mScreen); |
113 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON); | |
118 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_ON); | |
114 | 119 | mNfcController.onResume(); |
115 | 120 | assertThat(mNfcPreference.isEnabled()).isFalse(); |
116 | 121 | |
117 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF); | |
122 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_OFF); | |
118 | 123 | mNfcController.onResume(); |
119 | 124 | assertThat(mNfcPreference.isEnabled()).isFalse(); |
120 | 125 | } |
@@ -122,29 +127,29 @@ public class NfcPreferenceControllerTest { | ||
122 | 127 | @Test |
123 | 128 | public void isNfcChecked_nfcStateOn_shouldReturnTrue() { |
124 | 129 | mNfcController.displayPreference(mScreen); |
125 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON); | |
130 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_ON); | |
126 | 131 | mNfcController.onResume(); |
127 | 132 | assertThat(mNfcPreference.isChecked()).isTrue(); |
128 | 133 | |
129 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON); | |
134 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_ON); | |
130 | 135 | mNfcController.onResume(); |
131 | 136 | assertThat(mNfcPreference.isChecked()).isTrue(); |
132 | 137 | } |
133 | 138 | |
134 | 139 | @Test |
135 | 140 | public void isNfcChecked_nfcStateOff_shouldReturnFalse() { |
136 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF); | |
141 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_OFF); | |
137 | 142 | mNfcController.onResume(); |
138 | 143 | assertThat(mNfcPreference.isChecked()).isFalse(); |
139 | 144 | |
140 | - when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF); | |
145 | + mShadowNfcAdapter.setAdapterState(NfcAdapter.STATE_TURNING_OFF); | |
141 | 146 | mNfcController.onResume(); |
142 | 147 | assertThat(mNfcPreference.isChecked()).isFalse(); |
143 | 148 | } |
144 | 149 | |
145 | 150 | @Test |
146 | 151 | public void updateNonIndexableKeys_available_shouldNotUpdate() { |
147 | - when(mNfcAdapter.isEnabled()).thenReturn(true); | |
152 | + mShadowNfcAdapter.setEnabled(true); | |
148 | 153 | final List<String> keys = new ArrayList<>(); |
149 | 154 | |
150 | 155 | mNfcController.updateNonIndexableKeys(keys); |
@@ -167,7 +172,7 @@ public class NfcPreferenceControllerTest { | ||
167 | 172 | mNfcController.setChecked(true); |
168 | 173 | mNfcController.onResume(); |
169 | 174 | |
170 | - verify(mNfcAdapter).enable(); | |
175 | + assertThat(mNfcAdapter.isEnabled()).isTrue(); | |
171 | 176 | } |
172 | 177 | |
173 | 178 | @Test |
@@ -175,7 +180,7 @@ public class NfcPreferenceControllerTest { | ||
175 | 180 | mNfcController.setChecked(false); |
176 | 181 | mNfcController.onResume(); |
177 | 182 | |
178 | - verify(mNfcAdapter).disable(); | |
183 | + assertThat(mNfcAdapter.isEnabled()).isFalse(); | |
179 | 184 | } |
180 | 185 | |
181 | 186 | @Test |
@@ -33,6 +33,8 @@ import androidx.preference.Preference; | ||
33 | 33 | import androidx.preference.PreferenceManager; |
34 | 34 | import androidx.preference.PreferenceScreen; |
35 | 35 | |
36 | +import com.android.settings.testutils.shadow.ShadowNfcAdapter; | |
37 | + | |
36 | 38 | import org.junit.Before; |
37 | 39 | import org.junit.Test; |
38 | 40 | import org.junit.runner.RunWith; |
@@ -48,7 +50,7 @@ import java.util.ArrayList; | ||
48 | 50 | import java.util.List; |
49 | 51 | |
50 | 52 | @RunWith(RobolectricTestRunner.class) |
51 | -@Config(shadows = PaymentSettingsTest.ShadowPaymentBackend.class) | |
53 | +@Config(shadows = {PaymentSettingsTest.ShadowPaymentBackend.class, ShadowNfcAdapter.class}) | |
52 | 54 | public class PaymentSettingsTest { |
53 | 55 | |
54 | 56 | static final String PAYMENT_KEY = "nfc_payment"; |
@@ -16,6 +16,8 @@ | ||
16 | 16 | |
17 | 17 | package com.android.settings.testutils.shadow; |
18 | 18 | |
19 | +import static org.robolectric.shadow.api.Shadow.newInstanceOf; | |
20 | + | |
19 | 21 | import android.app.Activity; |
20 | 22 | import android.content.Context; |
21 | 23 | import android.nfc.NfcAdapter; |
@@ -24,16 +26,18 @@ import android.os.Bundle; | ||
24 | 26 | import org.robolectric.annotation.Implementation; |
25 | 27 | import org.robolectric.annotation.Implements; |
26 | 28 | import org.robolectric.annotation.Resetter; |
27 | -import org.robolectric.util.ReflectionHelpers; | |
28 | -import org.robolectric.util.ReflectionHelpers.ClassParameter; | |
29 | 29 | |
30 | 30 | /** |
31 | 31 | * Shadow of {@link NfcAdapter}. |
32 | 32 | */ |
33 | -@Implements(NfcAdapter.class) | |
34 | -public class ShadowNfcAdapter { | |
33 | +@Implements(value = NfcAdapter.class) | |
34 | +public class ShadowNfcAdapter extends org.robolectric.shadows.ShadowNfcAdapter { | |
35 | 35 | private static boolean sReaderModeEnabled; |
36 | + private static Object sNfcAdapter = newInstanceOf("android.nfc.NfcAdapter"); | |
37 | + | |
36 | 38 | private boolean mIsNfcEnabled = false; |
39 | + private int mState = NfcAdapter.STATE_ON; | |
40 | + private boolean mIsSecureNfcSupported = false; | |
37 | 41 | |
38 | 42 | @Implementation |
39 | 43 | protected void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback, |
@@ -43,8 +47,7 @@ public class ShadowNfcAdapter { | ||
43 | 47 | |
44 | 48 | @Implementation |
45 | 49 | protected static NfcAdapter getDefaultAdapter(Context context) { |
46 | - return ReflectionHelpers.callConstructor( | |
47 | - NfcAdapter.class, ClassParameter.from(Context.class, context)); | |
50 | + return (NfcAdapter) sNfcAdapter; | |
48 | 51 | } |
49 | 52 | |
50 | 53 | @Implementation |
@@ -52,6 +55,28 @@ public class ShadowNfcAdapter { | ||
52 | 55 | return mIsNfcEnabled; |
53 | 56 | } |
54 | 57 | |
58 | + public void setEnabled(boolean enable) { | |
59 | + mIsNfcEnabled = enable; | |
60 | + } | |
61 | + | |
62 | + @Implementation | |
63 | + protected int getAdapterState() { | |
64 | + return mState; | |
65 | + } | |
66 | + | |
67 | + public void setAdapterState(int state) { | |
68 | + this.mState = state; | |
69 | + } | |
70 | + | |
71 | + @Implementation | |
72 | + protected boolean isSecureNfcSupported() { | |
73 | + return mIsSecureNfcSupported; | |
74 | + } | |
75 | + | |
76 | + public void setSecureNfcSupported(boolean supported) { | |
77 | + this.mIsSecureNfcSupported = supported; | |
78 | + } | |
79 | + | |
55 | 80 | @Implementation |
56 | 81 | protected boolean enable() { |
57 | 82 | mIsNfcEnabled = true; |