1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.nfc.cardemulation; 18 19 import static org.junit.Assert.assertTrue; 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.ArgumentMatchers.anyBoolean; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.anyLong; 24 import static org.mockito.ArgumentMatchers.anyString; 25 import static org.mockito.ArgumentMatchers.eq; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.verifyNoMoreInteractions; 29 import static org.mockito.Mockito.verifyZeroInteractions; 30 import static org.mockito.Mockito.when; 31 32 import android.app.ActivityManager; 33 import android.content.ComponentName; 34 import android.content.Context; 35 import android.content.res.Resources; 36 import android.nfc.NfcAdapter; 37 import android.nfc.cardemulation.AidGroup; 38 import android.nfc.cardemulation.ApduServiceInfo; 39 import android.nfc.cardemulation.CardEmulation; 40 import android.nfc.cardemulation.NfcFServiceInfo; 41 import android.nfc.cardemulation.PollingFrame; 42 import android.os.Binder; 43 import android.os.PowerManager; 44 import android.os.RemoteException; 45 import android.os.UserHandle; 46 import android.os.UserManager; 47 import android.util.Pair; 48 49 import com.android.dx.mockito.inline.extended.ExtendedMockito; 50 import com.android.nfc.ForegroundUtils; 51 import com.android.nfc.NfcPermissions; 52 import com.android.nfc.NfcService; 53 import com.android.nfc.R; 54 55 import org.junit.After; 56 import org.junit.Assert; 57 import org.junit.Before; 58 import org.junit.Test; 59 import org.mockito.ArgumentCaptor; 60 import org.mockito.Captor; 61 import org.mockito.Mock; 62 import org.mockito.Mockito; 63 import org.mockito.MockitoAnnotations; 64 import org.mockito.MockitoSession; 65 import org.mockito.quality.Strictness; 66 67 import java.util.List; 68 69 public class CardEmulationManagerTest { 70 71 private static final int USER_ID = 0; 72 private static final UserHandle USER_HANDLE = UserHandle.of(USER_ID); 73 private static final byte[] TEST_DATA_1 = new byte[] {(byte) 0xd2}; 74 private static final byte[] TEST_DATA_2 = new byte[] {(byte) 0xd3}; 75 private static final byte[] PROPER_SKIP_DATA_NDF1_HEADER = new byte[] 76 {0x00, (byte) 0xa4, 0x04, 0x00, (byte)0x07, (byte) 0xd2, 0x76, 0x00, 0x00, 77 (byte) 0x85, 0x01, 0x00}; 78 private static final byte[] PROPER_SKIP_DATA_NDF2_HEADER = new byte[] 79 {0x00, (byte) 0xa4, 0x04, 0x00, (byte)0x07, (byte) 0xd2, 0x76, 0x00, 0x00, 80 (byte) 0x85, 0x01, 0x01}; 81 private static final String WALLET_HOLDER_PACKAGE_NAME = "com.android.test.walletroleholder"; 82 private static final List<PollingFrame> POLLING_LOOP_FRAMES = List.of(); 83 private static final List<ApduServiceInfo> UPDATED_SERVICES = List.of(); 84 private static final List<NfcFServiceInfo> UPDATED_NFC_SERVICES = List.of(); 85 private static final ComponentName WALLET_PAYMENT_SERVICE 86 = new ComponentName(WALLET_HOLDER_PACKAGE_NAME, 87 "com.android.test.walletroleholder.WalletRoleHolderApduService"); 88 private static final String PAYMENT_AID_1 = "A000000004101012"; 89 90 @Mock 91 private Context mContext; 92 @Mock 93 private Resources mResources; 94 @Mock 95 private ForegroundUtils mForegroundUtils; 96 @Mock 97 private WalletRoleObserver mWalletRoleObserver; 98 @Mock 99 private RegisteredAidCache mRegisteredAidCache; 100 @Mock 101 private RegisteredT3tIdentifiersCache mRegisteredT3tIdentifiersCache; 102 @Mock 103 private HostEmulationManager mHostEmulationManager; 104 @Mock 105 private HostNfcFEmulationManager mHostNfcFEmulationManager; 106 @Mock 107 private RegisteredServicesCache mRegisteredServicesCache; 108 @Mock 109 private RegisteredNfcFServicesCache mRegisteredNfcFServicesCache; 110 @Mock 111 private PreferredServices mPreferredServices; 112 @Mock 113 private EnabledNfcFServices mEnabledNfcFServices; 114 @Mock 115 private RoutingOptionManager mRoutingOptionManager; 116 @Mock 117 private PowerManager mPowerManager; 118 @Mock 119 private NfcService mNfcService; 120 @Mock 121 private UserManager mUserManager; 122 @Mock 123 private NfcAdapter mNfcAdapter; 124 @Captor 125 private ArgumentCaptor<List<PollingFrame>> mPollingLoopFrameCaptor; 126 @Captor 127 private ArgumentCaptor<byte[]> mDataCaptor; 128 @Captor 129 private ArgumentCaptor<List<ApduServiceInfo>> mServiceListCaptor; 130 @Captor 131 private ArgumentCaptor<List<NfcFServiceInfo>> mNfcServiceListCaptor; 132 private MockitoSession mStaticMockSession; 133 private CardEmulationManager mCardEmulationManager; 134 @Before setUp()135 public void setUp() { 136 mStaticMockSession = ExtendedMockito.mockitoSession() 137 .mockStatic(ActivityManager.class) 138 .mockStatic(NfcPermissions.class) 139 .mockStatic(android.nfc.Flags.class) 140 .strictness(Strictness.LENIENT) 141 .mockStatic(NfcService.class) 142 .mockStatic(Binder.class) 143 .mockStatic(UserHandle.class) 144 .mockStatic(NfcAdapter.class) 145 .startMocking(); 146 MockitoAnnotations.initMocks(this); 147 when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter); 148 when(NfcService.getInstance()).thenReturn(mNfcService); 149 when(ActivityManager.getCurrentUser()).thenReturn(USER_ID); 150 when(UserHandle.getUserHandleForUid(anyInt())).thenReturn(USER_HANDLE); 151 when(mContext.createContextAsUser( 152 any(), anyInt())).thenReturn(mContext); 153 when(mContext.getResources()).thenReturn(mResources); 154 when(mContext.getSystemService(eq(UserManager.class))).thenReturn(mUserManager); 155 mCardEmulationManager = createInstanceWithMockParams(); 156 } 157 158 @After tearDown()159 public void tearDown() { 160 mStaticMockSession.finishMocking(); 161 } 162 163 @Test testConstructor()164 public void testConstructor() { 165 assertConstructorMethodCalls(); 166 } 167 assertConstructorMethodCalls()168 private void assertConstructorMethodCalls() { 169 verify(mRoutingOptionManager).getOffHostRouteEse(); 170 verify(mRoutingOptionManager).getOffHostRouteUicc(); 171 verify(mRegisteredServicesCache).initialize(); 172 verify(mRegisteredNfcFServicesCache).initialize(); 173 verify(mWalletRoleObserver).isWalletRoleFeatureEnabled(); 174 verify(mWalletRoleObserver).getDefaultWalletRoleHolder(eq(USER_ID)); 175 verify(mPreferredServices).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 176 eq(USER_ID)); 177 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 178 eq(USER_ID)); 179 } 180 181 @Test testGetters()182 public void testGetters() { 183 Assert.assertNotNull(mCardEmulationManager.getNfcCardEmulationInterface()); 184 Assert.assertNotNull(mCardEmulationManager.getNfcFCardEmulationInterface()); 185 } 186 187 @Test testPollingLoopDetected()188 public void testPollingLoopDetected() { 189 mCardEmulationManager.onPollingLoopDetected(POLLING_LOOP_FRAMES); 190 191 verify(mHostEmulationManager).onPollingLoopDetected(mPollingLoopFrameCaptor.capture()); 192 Assert.assertEquals(mPollingLoopFrameCaptor.getValue(), POLLING_LOOP_FRAMES); 193 } 194 195 @Test testOnHostCardEmulationActivated_technologyApdu()196 public void testOnHostCardEmulationActivated_technologyApdu() { 197 mCardEmulationManager.onHostCardEmulationActivated(CardEmulationManager.NFC_HCE_APDU); 198 199 verify(mPowerManager).userActivity(anyLong(), eq(PowerManager.USER_ACTIVITY_EVENT_TOUCH), 200 eq(PowerManager.USER_ACTIVITY_FLAG_INDIRECT)); 201 verify(mHostEmulationManager).onHostEmulationActivated(); 202 verify(mPreferredServices).onHostEmulationActivated(); 203 Assert.assertFalse(mCardEmulationManager.mNotSkipAid); 204 verifyZeroInteractions(mHostNfcFEmulationManager); 205 verifyZeroInteractions(mEnabledNfcFServices); 206 } 207 208 @Test testOnHostCardEmulationActivated_technologyNfcf()209 public void testOnHostCardEmulationActivated_technologyNfcf() { 210 mCardEmulationManager.onHostCardEmulationActivated(CardEmulationManager.NFC_HCE_NFCF); 211 212 assertConstructorMethodCalls(); 213 verify(mPowerManager).userActivity(anyLong(), eq(PowerManager.USER_ACTIVITY_EVENT_TOUCH), 214 eq(PowerManager.USER_ACTIVITY_FLAG_INDIRECT)); 215 verify(mHostNfcFEmulationManager).onHostEmulationActivated(); 216 verify(mRegisteredNfcFServicesCache).onHostEmulationActivated(); 217 verify(mEnabledNfcFServices).onHostEmulationActivated(); 218 verifyZeroInteractions(mHostEmulationManager); 219 verifyZeroInteractions(mPreferredServices); 220 } 221 222 @Test testSkipAid_nullData_isFalse()223 public void testSkipAid_nullData_isFalse() { 224 mCardEmulationManager.mNotSkipAid = false; 225 Assert.assertFalse(mCardEmulationManager.isSkipAid(null)); 226 } 227 228 @Test testSkipAid_notSkipTrue_isFalse()229 public void testSkipAid_notSkipTrue_isFalse() { 230 mCardEmulationManager.mNotSkipAid = true; 231 Assert.assertFalse(mCardEmulationManager.isSkipAid(TEST_DATA_1)); 232 } 233 234 @Test testSkipAid_wrongData_isFalse()235 public void testSkipAid_wrongData_isFalse() { 236 mCardEmulationManager.mNotSkipAid = false; 237 Assert.assertFalse(mCardEmulationManager.isSkipAid(TEST_DATA_1)); 238 } 239 240 @Test testSkipAid_ndf1_isTrue()241 public void testSkipAid_ndf1_isTrue() { 242 mCardEmulationManager.mNotSkipAid = false; 243 assertTrue(mCardEmulationManager.isSkipAid(PROPER_SKIP_DATA_NDF1_HEADER)); 244 } 245 246 @Test testSkipAid_ndf2_isTrue()247 public void testSkipAid_ndf2_isTrue() { 248 mCardEmulationManager.mNotSkipAid = false; 249 assertTrue(mCardEmulationManager.isSkipAid(PROPER_SKIP_DATA_NDF2_HEADER)); 250 } 251 252 @Test testOnHostCardEmulationData_technologyApdu_skipData()253 public void testOnHostCardEmulationData_technologyApdu_skipData() { 254 mCardEmulationManager.onHostCardEmulationData(CardEmulationManager.NFC_HCE_APDU, 255 PROPER_SKIP_DATA_NDF1_HEADER); 256 257 verify(mHostEmulationManager).onHostEmulationData(mDataCaptor.capture()); 258 Assert.assertEquals(PROPER_SKIP_DATA_NDF1_HEADER, mDataCaptor.getValue()); 259 verifyZeroInteractions(mHostNfcFEmulationManager); 260 verifyZeroInteractions(mPowerManager); 261 } 262 263 @Test testOnHostCardEmulationData_technologyNfcf_DontSkipData()264 public void testOnHostCardEmulationData_technologyNfcf_DontSkipData() { 265 mCardEmulationManager.onHostCardEmulationData(CardEmulationManager.NFC_HCE_NFCF, 266 PROPER_SKIP_DATA_NDF1_HEADER); 267 268 verify(mHostNfcFEmulationManager).onHostEmulationData(mDataCaptor.capture()); 269 Assert.assertEquals(PROPER_SKIP_DATA_NDF1_HEADER, mDataCaptor.getValue()); 270 verifyZeroInteractions(mHostEmulationManager); 271 verify(mPowerManager).userActivity(anyLong(), eq(PowerManager.USER_ACTIVITY_EVENT_TOUCH), 272 eq(0)); 273 } 274 275 @Test testOnHostCardEmulationDeactivated_technologyApdu()276 public void testOnHostCardEmulationDeactivated_technologyApdu() { 277 mCardEmulationManager.onHostCardEmulationDeactivated(CardEmulationManager.NFC_HCE_APDU); 278 279 assertConstructorMethodCalls(); 280 verify(mHostEmulationManager).onHostEmulationDeactivated(); 281 verify(mPreferredServices).onHostEmulationDeactivated(); 282 verifyZeroInteractions(mHostNfcFEmulationManager); 283 verifyZeroInteractions(mRegisteredNfcFServicesCache); 284 verifyZeroInteractions(mEnabledNfcFServices); 285 } 286 287 @Test testOnHostCardEmulationDeactivated_technologyNfcf()288 public void testOnHostCardEmulationDeactivated_technologyNfcf() { 289 mCardEmulationManager.onHostCardEmulationDeactivated(CardEmulationManager.NFC_HCE_NFCF); 290 291 assertConstructorMethodCalls(); 292 verify(mHostNfcFEmulationManager).onHostEmulationDeactivated(); 293 verify(mRegisteredNfcFServicesCache).onHostEmulationDeactivated(); 294 verify(mEnabledNfcFServices).onHostEmulationDeactivated(); 295 verifyZeroInteractions(mHostEmulationManager); 296 verifyZeroInteractions(mPreferredServices); 297 } 298 299 @Test testOnOffHostAidSelected()300 public void testOnOffHostAidSelected() { 301 mCardEmulationManager.onOffHostAidSelected(); 302 303 assertConstructorMethodCalls(); 304 verify(mHostEmulationManager).onOffHostAidSelected(); 305 } 306 307 @Test testOnUserSwitched()308 public void testOnUserSwitched() { 309 mCardEmulationManager.onUserSwitched(USER_ID); 310 311 assertConstructorMethodCalls(); 312 verify(mWalletRoleObserver).onUserSwitched(eq(USER_ID)); 313 verify(mRegisteredServicesCache).onUserSwitched(); 314 verify(mPreferredServices).onUserSwitched(eq(USER_ID)); 315 verify(mHostNfcFEmulationManager).onUserSwitched(); 316 verify(mRegisteredT3tIdentifiersCache).onUserSwitched(); 317 verify(mEnabledNfcFServices).onUserSwitched(eq(USER_ID)); 318 verify(mRegisteredNfcFServicesCache).onUserSwitched(); 319 } 320 321 @Test testOnManagedProfileChanged()322 public void testOnManagedProfileChanged() { 323 mCardEmulationManager.onManagedProfileChanged(); 324 325 assertConstructorMethodCalls(); 326 verify(mRegisteredServicesCache).onManagedProfileChanged(); 327 verify(mRegisteredNfcFServicesCache).onManagedProfileChanged(); 328 } 329 330 @Test testOnNfcEnabled()331 public void testOnNfcEnabled() { 332 mCardEmulationManager.onNfcEnabled(); 333 334 assertConstructorMethodCalls(); 335 verify(mRegisteredAidCache).onNfcEnabled(); 336 verify(mRegisteredT3tIdentifiersCache).onNfcEnabled(); 337 } 338 339 @Test testOnNfcDisabled()340 public void testOnNfcDisabled() { 341 mCardEmulationManager.onNfcDisabled(); 342 343 assertConstructorMethodCalls(); 344 verify(mRegisteredAidCache).onNfcDisabled(); 345 verify(mHostNfcFEmulationManager).onNfcDisabled(); 346 verify(mRegisteredNfcFServicesCache).onNfcDisabled(); 347 verify(mEnabledNfcFServices).onNfcDisabled(); 348 verify(mRegisteredT3tIdentifiersCache).onNfcDisabled(); 349 } 350 351 @Test testOnSecureNfcToggled()352 public void testOnSecureNfcToggled() { 353 mCardEmulationManager.onSecureNfcToggled(); 354 355 verify(mRegisteredAidCache).onSecureNfcToggled(); 356 verify(mRegisteredT3tIdentifiersCache).onSecureNfcToggled(); 357 } 358 359 @Test testOnServicesUpdated_walletEnabledPollingLoopDisabled()360 public void testOnServicesUpdated_walletEnabledPollingLoopDisabled() { 361 when(mWalletRoleObserver.isWalletRoleFeatureEnabled()).thenReturn(true); 362 when(android.nfc.Flags.nfcReadPollingLoop()).thenReturn(false); 363 364 mCardEmulationManager.onServicesUpdated(USER_ID, UPDATED_SERVICES, false); 365 366 verify(mWalletRoleObserver, times(2)).isWalletRoleFeatureEnabled(); 367 verify(mRegisteredAidCache).onServicesUpdated(eq(USER_ID), mServiceListCaptor.capture()); 368 verify(mPreferredServices).onServicesUpdated(); 369 Assert.assertEquals(UPDATED_SERVICES, mServiceListCaptor.getValue()); 370 verifyZeroInteractions(mHostEmulationManager); 371 verify(mNfcService).onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_UPDATED)); 372 } 373 374 @Test testOnServicesUpdated_walletEnabledPollingLoopEnabled()375 public void testOnServicesUpdated_walletEnabledPollingLoopEnabled() { 376 when(mWalletRoleObserver.isWalletRoleFeatureEnabled()).thenReturn(true); 377 when(android.nfc.Flags.nfcReadPollingLoop()).thenReturn(true); 378 379 mCardEmulationManager.onServicesUpdated(USER_ID, UPDATED_SERVICES, false); 380 381 verify(mWalletRoleObserver, times(2)).isWalletRoleFeatureEnabled(); 382 verify(mRegisteredAidCache).onServicesUpdated(eq(USER_ID), mServiceListCaptor.capture()); 383 verify(mPreferredServices).onServicesUpdated(); 384 verify(mHostEmulationManager).updatePollingLoopFilters(eq(USER_ID), 385 mServiceListCaptor.capture()); 386 verify(mNfcService).onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_UPDATED)); 387 Assert.assertEquals(UPDATED_SERVICES, mServiceListCaptor.getAllValues().getFirst()); 388 Assert.assertEquals(UPDATED_SERVICES, mServiceListCaptor.getAllValues().getLast()); 389 } 390 391 @Test testOnNfcFServicesUpdated()392 public void testOnNfcFServicesUpdated() { 393 mCardEmulationManager.onNfcFServicesUpdated(USER_ID, UPDATED_NFC_SERVICES); 394 395 verify(mRegisteredT3tIdentifiersCache).onServicesUpdated(eq(USER_ID), 396 mNfcServiceListCaptor.capture()); 397 Assert.assertEquals(UPDATED_NFC_SERVICES, mNfcServiceListCaptor.getValue()); 398 } 399 400 @Test testIsServiceRegistered_serviceExists()401 public void testIsServiceRegistered_serviceExists() { 402 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 403 404 assertTrue(mCardEmulationManager 405 .isServiceRegistered(USER_ID, WALLET_PAYMENT_SERVICE)); 406 407 verify(mRegisteredServicesCache, times(2)) 408 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 409 } 410 411 @Test testIsServiceRegistered_serviceDoesNotExists()412 public void testIsServiceRegistered_serviceDoesNotExists() { 413 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 414 415 Assert.assertFalse(mCardEmulationManager 416 .isServiceRegistered(USER_ID, WALLET_PAYMENT_SERVICE)); 417 418 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 419 verify(mRegisteredServicesCache, times(2)) 420 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 421 } 422 423 @Test testIsNfcServiceInstalled_serviceExists()424 public void testIsNfcServiceInstalled_serviceExists() { 425 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 426 427 assertTrue(mCardEmulationManager 428 .isNfcFServiceInstalled(USER_ID, WALLET_PAYMENT_SERVICE)); 429 430 verify(mRegisteredNfcFServicesCache, times(2)) 431 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 432 } 433 434 @Test testIsNfcServiceInstalled_serviceDoesNotExists()435 public void testIsNfcServiceInstalled_serviceDoesNotExists() { 436 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 437 438 Assert.assertFalse(mCardEmulationManager 439 .isNfcFServiceInstalled(USER_ID, WALLET_PAYMENT_SERVICE)); 440 441 verify(mRegisteredNfcFServicesCache).invalidateCache(eq(USER_ID)); 442 verify(mRegisteredNfcFServicesCache, times(2)) 443 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 444 } 445 446 @Test testPackageHasPreferredService()447 public void testPackageHasPreferredService() { 448 when(mPreferredServices.packageHasPreferredService(eq(WALLET_HOLDER_PACKAGE_NAME))) 449 .thenReturn(true); 450 451 assertTrue(mCardEmulationManager 452 .packageHasPreferredService(WALLET_HOLDER_PACKAGE_NAME)); 453 454 verify(mPreferredServices).packageHasPreferredService(eq(WALLET_HOLDER_PACKAGE_NAME)); 455 } 456 457 @Test testCardEmulationIsDefaultServiceForCategory_serviceExistsWalletEnabled()458 public void testCardEmulationIsDefaultServiceForCategory_serviceExistsWalletEnabled() 459 throws RemoteException { 460 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 461 when(mWalletRoleObserver.isWalletRoleFeatureEnabled()).thenReturn(true); 462 when(mWalletRoleObserver.getDefaultWalletRoleHolder(eq(USER_ID))) 463 .thenReturn(WALLET_HOLDER_PACKAGE_NAME); 464 465 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 466 .isDefaultServiceForCategory(USER_ID, WALLET_PAYMENT_SERVICE, 467 CardEmulation.CATEGORY_PAYMENT)); 468 469 ExtendedMockito.verify(() -> { 470 NfcPermissions.validateUserId(USER_ID); 471 }); 472 ExtendedMockito.verify(() -> { 473 NfcPermissions.enforceUserPermissions(mContext); 474 }); 475 verify(mRegisteredServicesCache, times(2)) 476 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 477 } 478 479 @Test testCardEmulationIsDefaultServiceForCategory_serviceDoesNotExists()480 public void testCardEmulationIsDefaultServiceForCategory_serviceDoesNotExists() 481 throws RemoteException { 482 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 483 484 assertConstructorMethodCalls(); 485 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 486 .isDefaultServiceForCategory(USER_ID, WALLET_PAYMENT_SERVICE, 487 CardEmulation.CATEGORY_PAYMENT)); 488 489 ExtendedMockito.verify(() -> { 490 NfcPermissions.validateUserId(USER_ID); 491 }); 492 ExtendedMockito.verify(() -> { 493 NfcPermissions.enforceUserPermissions(mContext); 494 }); 495 verifyZeroInteractions(mWalletRoleObserver); 496 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 497 verify(mRegisteredServicesCache, times(2)) 498 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 499 } 500 501 @Test testCardEmulationIsDefaultServiceForAid_serviceExists()502 public void testCardEmulationIsDefaultServiceForAid_serviceExists() 503 throws RemoteException { 504 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 505 when(mRegisteredAidCache.isDefaultServiceForAid(eq(USER_ID), any(), eq(PAYMENT_AID_1))) 506 .thenReturn(true); 507 508 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 509 .isDefaultServiceForAid(USER_ID, WALLET_PAYMENT_SERVICE, 510 PAYMENT_AID_1)); 511 512 ExtendedMockito.verify(() -> { 513 NfcPermissions.validateUserId(USER_ID); 514 }); 515 ExtendedMockito.verify(() -> { 516 NfcPermissions.validateUserId(USER_ID); 517 }); 518 verify(mRegisteredAidCache).isDefaultServiceForAid(eq(USER_ID), eq(WALLET_PAYMENT_SERVICE), 519 eq(PAYMENT_AID_1)); 520 verify(mRegisteredServicesCache, times(2)) 521 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 522 } 523 524 @Test testCardEmulationIsDefaultServiceForAid_serviceDoesNotExists()525 public void testCardEmulationIsDefaultServiceForAid_serviceDoesNotExists() 526 throws RemoteException { 527 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 528 529 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 530 .isDefaultServiceForAid(USER_ID, WALLET_PAYMENT_SERVICE, 531 PAYMENT_AID_1)); 532 533 ExtendedMockito.verify(() -> { 534 NfcPermissions.validateUserId(USER_ID); 535 }); 536 ExtendedMockito.verify(() -> { 537 NfcPermissions.enforceUserPermissions(mContext); 538 }); 539 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 540 eq(USER_ID)); 541 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 542 verify(mRegisteredServicesCache, times(2)) 543 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 544 verifyZeroInteractions(mRegisteredAidCache); 545 } 546 547 @Test testCardEmulationSetDefaultForNextTap_serviceExists()548 public void testCardEmulationSetDefaultForNextTap_serviceExists() 549 throws RemoteException { 550 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 551 when(mPreferredServices.setDefaultForNextTap(anyInt(), any())).thenReturn(true); 552 553 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 554 .setDefaultForNextTap(USER_ID, WALLET_PAYMENT_SERVICE)); 555 556 ExtendedMockito.verify(() -> { 557 NfcPermissions.validateProfileId(mContext, USER_ID); 558 }); 559 ExtendedMockito.verify(() -> { 560 NfcPermissions.enforceAdminPermissions(mContext); 561 }); 562 verify(mPreferredServices).setDefaultForNextTap(eq(USER_ID), eq(WALLET_PAYMENT_SERVICE)); 563 verify(mRegisteredServicesCache, times(2)) 564 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 565 } 566 567 @Test testCardEmulationSetDefaultForNextTap_serviceDoesNotExists()568 public void testCardEmulationSetDefaultForNextTap_serviceDoesNotExists() 569 throws RemoteException { 570 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 571 572 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 573 .setDefaultForNextTap(USER_ID, WALLET_PAYMENT_SERVICE)); 574 575 ExtendedMockito.verify(() -> { 576 NfcPermissions.validateProfileId(mContext, USER_ID); 577 }); 578 ExtendedMockito.verify(() -> { 579 NfcPermissions.enforceAdminPermissions(mContext); 580 }); 581 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 582 verify(mRegisteredServicesCache, times(2)) 583 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 584 verify(mPreferredServices).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 585 eq(USER_ID)); 586 verifyZeroInteractions(mPreferredServices); 587 } 588 589 @Test testCardEmulationSetShouldDefaultToObserveModeForService_serviceExists()590 public void testCardEmulationSetShouldDefaultToObserveModeForService_serviceExists() 591 throws RemoteException { 592 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 593 when(mRegisteredServicesCache.setShouldDefaultToObserveModeForService(anyInt(), anyInt(), 594 any(), anyBoolean())).thenReturn(true); 595 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), 596 any())).thenReturn(false); 597 598 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 599 .setShouldDefaultToObserveModeForService(USER_ID, WALLET_PAYMENT_SERVICE, 600 true)); 601 602 ExtendedMockito.verify(() -> { 603 NfcPermissions.validateUserId(USER_ID); 604 }); 605 ExtendedMockito.verify(() -> { 606 NfcPermissions.enforceUserPermissions(mContext); 607 }); 608 verify(mRegisteredServicesCache).initialize(); 609 verify(mRegisteredServicesCache) 610 .doesServiceShouldDefaultToObserveMode(anyInt(), any()); 611 verify(mRegisteredServicesCache).setShouldDefaultToObserveModeForService(eq(USER_ID), 612 anyInt(), eq(WALLET_PAYMENT_SERVICE), eq(true)); 613 verify(mRegisteredServicesCache, times(2)) 614 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 615 verifyNoMoreInteractions(mRegisteredServicesCache); 616 } 617 618 @Test testCardEmulationSetShouldDefaultToObserveModeForService_ignoreNoopStateChange()619 public void testCardEmulationSetShouldDefaultToObserveModeForService_ignoreNoopStateChange() 620 throws RemoteException { 621 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 622 when(mRegisteredServicesCache.setShouldDefaultToObserveModeForService(anyInt(), anyInt(), 623 any(), anyBoolean())).thenReturn(true); 624 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), 625 any())).thenReturn(false); 626 627 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 628 .setShouldDefaultToObserveModeForService(USER_ID, WALLET_PAYMENT_SERVICE, 629 true)); 630 631 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), 632 any())).thenReturn(true); 633 634 // Called twice with the same value. Calls to update should be ignored. 635 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 636 .setShouldDefaultToObserveModeForService(USER_ID, WALLET_PAYMENT_SERVICE, 637 true)); 638 639 ExtendedMockito.verify(() -> NfcPermissions.validateUserId(USER_ID), times(2)); 640 ExtendedMockito.verify(() -> NfcPermissions.enforceUserPermissions(mContext), times(2)); 641 verify(mRegisteredServicesCache).initialize(); 642 verify(mRegisteredServicesCache, times(2)) 643 .doesServiceShouldDefaultToObserveMode(anyInt(), any()); 644 verify(mRegisteredServicesCache, times(4)) 645 .hasService(eq(USER_ID), eq(WALLET_PAYMENT_SERVICE)); 646 647 // Importantly this should only be called once. 648 verify(mRegisteredServicesCache, times(1)) 649 .setShouldDefaultToObserveModeForService(eq(USER_ID), anyInt(), 650 eq(WALLET_PAYMENT_SERVICE), eq(true)); 651 verifyNoMoreInteractions(mRegisteredServicesCache); 652 } 653 654 @Test testCardEmulationSetShouldDefaultToObserveModeForService_serviceDoesNotExists()655 public void testCardEmulationSetShouldDefaultToObserveModeForService_serviceDoesNotExists() 656 throws RemoteException { 657 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 658 659 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 660 .setShouldDefaultToObserveModeForService(USER_ID, WALLET_PAYMENT_SERVICE, 661 false)); 662 663 ExtendedMockito.verify(() -> { 664 NfcPermissions.validateUserId(USER_ID); 665 }); 666 ExtendedMockito.verify(() -> { 667 NfcPermissions.enforceUserPermissions(mContext); 668 }); 669 verify(mRegisteredServicesCache).initialize(); 670 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 671 verify(mRegisteredServicesCache, times(2)) 672 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 673 verifyNoMoreInteractions(mRegisteredServicesCache); 674 } 675 676 @Test testCardEmulationRegisterAidGroupForService_serviceExists()677 public void testCardEmulationRegisterAidGroupForService_serviceExists() 678 throws RemoteException { 679 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 680 when(mRegisteredServicesCache.registerAidGroupForService(eq(USER_ID), anyInt(), any(), 681 any())).thenReturn(true); 682 AidGroup aidGroup = Mockito.mock(AidGroup.class); 683 684 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 685 .registerAidGroupForService(USER_ID, WALLET_PAYMENT_SERVICE, aidGroup)); 686 687 ExtendedMockito.verify(() -> { 688 NfcPermissions.validateUserId(USER_ID); 689 }); 690 ExtendedMockito.verify(() -> { 691 NfcPermissions.enforceUserPermissions(mContext); 692 }); 693 verify(mRegisteredServicesCache).initialize(); 694 verify(mRegisteredServicesCache, times(2)) 695 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 696 verify(mRegisteredServicesCache).registerAidGroupForService(eq(USER_ID), anyInt(), 697 eq(WALLET_PAYMENT_SERVICE), eq(aidGroup)); 698 verifyNoMoreInteractions(mRegisteredServicesCache); 699 } 700 701 @Test testCardEmulationRegisterAidGroupForService_serviceDoesNotExists()702 public void testCardEmulationRegisterAidGroupForService_serviceDoesNotExists() 703 throws RemoteException { 704 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 705 when(mRegisteredServicesCache.registerAidGroupForService(eq(USER_ID), anyInt(), any(), 706 any())).thenReturn(true); 707 AidGroup aidGroup = Mockito.mock(AidGroup.class); 708 709 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 710 .registerAidGroupForService(USER_ID, WALLET_PAYMENT_SERVICE, aidGroup)); 711 712 ExtendedMockito.verify(() -> { 713 NfcPermissions.validateUserId(USER_ID); 714 }); 715 ExtendedMockito.verify(() -> { 716 NfcPermissions.enforceUserPermissions(mContext); 717 }); 718 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 719 eq(USER_ID)); 720 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 721 verify(mRegisteredServicesCache, times(2)) 722 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 723 verifyNoMoreInteractions(mRegisteredAidCache); 724 } 725 726 @Test testCardEmulationRegisterPollingLoopFilterForService_serviceExists()727 public void testCardEmulationRegisterPollingLoopFilterForService_serviceExists() 728 throws RemoteException { 729 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 730 when(mRegisteredServicesCache.registerPollingLoopFilterForService(eq(USER_ID), anyInt(), 731 any(), any(),anyBoolean())).thenReturn(true); 732 String pollingLoopFilter = "filter"; 733 734 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 735 .registerPollingLoopFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 736 pollingLoopFilter, true)); 737 738 verify(mRegisteredServicesCache).initialize(); 739 ExtendedMockito.verify(() -> { 740 NfcPermissions.enforceUserPermissions(mContext); 741 }); 742 ExtendedMockito.verify(() -> { 743 NfcPermissions.validateUserId(USER_ID); 744 }); 745 verify(mRegisteredServicesCache, times(2)) 746 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 747 verify(mRegisteredServicesCache).registerPollingLoopFilterForService(eq(USER_ID), 748 anyInt(), eq(WALLET_PAYMENT_SERVICE), eq(pollingLoopFilter), eq(true)); 749 verifyNoMoreInteractions(mRegisteredServicesCache); 750 } 751 752 @Test testCardEmulationRegisterPollingLoopFilterForService_serviceDoesNotExists()753 public void testCardEmulationRegisterPollingLoopFilterForService_serviceDoesNotExists() 754 throws RemoteException { 755 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 756 when(mRegisteredServicesCache.registerPollingLoopFilterForService(eq(USER_ID), anyInt(), 757 any(), any(),anyBoolean())).thenReturn(true); 758 String pollingLoopFilter = "filter"; 759 760 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 761 .registerPollingLoopFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 762 pollingLoopFilter, true)); 763 764 ExtendedMockito.verify(() -> { 765 NfcPermissions.validateUserId(USER_ID); 766 }); 767 ExtendedMockito.verify(() -> { 768 NfcPermissions.enforceUserPermissions(mContext); 769 }); 770 verify(mRegisteredServicesCache).initialize(); 771 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 772 verify(mRegisteredServicesCache, times(2)) 773 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 774 verifyNoMoreInteractions(mRegisteredServicesCache); 775 } 776 777 @Test testCardEmulationRemovePollingLoopFilterForService_serviceExists()778 public void testCardEmulationRemovePollingLoopFilterForService_serviceExists() 779 throws RemoteException { 780 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 781 when(mRegisteredServicesCache.removePollingLoopFilterForService(eq(USER_ID), anyInt(), 782 any(), any())).thenReturn(true); 783 String pollingLoopFilter = "filter"; 784 785 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 786 .removePollingLoopFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 787 pollingLoopFilter)); 788 789 ExtendedMockito.verify(() -> { 790 NfcPermissions.enforceUserPermissions(mContext); 791 }); 792 ExtendedMockito.verify(() -> { 793 NfcPermissions.validateUserId(USER_ID); 794 }); 795 verify(mRegisteredServicesCache).initialize(); 796 verify(mRegisteredServicesCache, times(2)) 797 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 798 verify(mRegisteredServicesCache).removePollingLoopFilterForService(eq(USER_ID), 799 anyInt(), eq(WALLET_PAYMENT_SERVICE), eq(pollingLoopFilter)); 800 verifyNoMoreInteractions(mRegisteredServicesCache); 801 } 802 803 @Test testCardEmulationRemovePollingLoopFilterForService_serviceDoesNotExists()804 public void testCardEmulationRemovePollingLoopFilterForService_serviceDoesNotExists() 805 throws RemoteException { 806 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 807 when(mRegisteredServicesCache.removePollingLoopFilterForService(eq(USER_ID), anyInt(), 808 any(), any())).thenReturn(true); 809 String pollingLoopFilter = "filter"; 810 811 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 812 .removePollingLoopFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 813 pollingLoopFilter)); 814 815 ExtendedMockito.verify(() -> { 816 NfcPermissions.enforceUserPermissions(mContext); 817 }); 818 ExtendedMockito.verify(() -> { 819 NfcPermissions.validateUserId(USER_ID); 820 }); 821 verify(mRegisteredServicesCache).initialize(); 822 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 823 verify(mRegisteredServicesCache, times(2)) 824 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 825 verifyNoMoreInteractions(mRegisteredServicesCache); 826 } 827 828 @Test testCardEmulationRegisterPollingLoopPatternFilterForService_serviceExists()829 public void testCardEmulationRegisterPollingLoopPatternFilterForService_serviceExists() 830 throws RemoteException { 831 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 832 when(mRegisteredServicesCache.registerPollingLoopPatternFilterForService(eq(USER_ID), 833 anyInt(), any(), any(), anyBoolean())).thenReturn(true); 834 String pollingLoopFilter = "filter"; 835 836 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 837 .registerPollingLoopPatternFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 838 pollingLoopFilter, true)); 839 840 ExtendedMockito.verify(() -> { 841 NfcPermissions.enforceUserPermissions(mContext); 842 }); 843 ExtendedMockito.verify(() -> { 844 NfcPermissions.validateUserId(USER_ID); 845 }); 846 verify(mRegisteredServicesCache).initialize(); 847 verify(mRegisteredServicesCache, times(2)) 848 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 849 verify(mRegisteredServicesCache).registerPollingLoopPatternFilterForService(eq(USER_ID), 850 anyInt(), eq(WALLET_PAYMENT_SERVICE), eq(pollingLoopFilter), eq(true)); 851 verifyNoMoreInteractions(mRegisteredServicesCache); 852 } 853 854 @Test testCardEmulationRegisterPollingLoopPatternFilterForService_serviceDoesNotExists()855 public void testCardEmulationRegisterPollingLoopPatternFilterForService_serviceDoesNotExists() 856 throws RemoteException { 857 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 858 when(mRegisteredServicesCache.registerPollingLoopPatternFilterForService(eq(USER_ID), 859 anyInt(), any(), any(), anyBoolean())).thenReturn(true); 860 String pollingLoopFilter = "filter"; 861 862 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 863 .registerPollingLoopPatternFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 864 pollingLoopFilter, true)); 865 866 ExtendedMockito.verify(() -> { 867 NfcPermissions.enforceUserPermissions(mContext); 868 }); 869 ExtendedMockito.verify(() -> { 870 NfcPermissions.validateUserId(USER_ID); 871 }); 872 verify(mRegisteredServicesCache).initialize(); 873 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 874 verify(mRegisteredServicesCache, times(2)) 875 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 876 verifyNoMoreInteractions(mRegisteredServicesCache); 877 } 878 879 @Test testCardEmulationRemovePollingLoopPatternFilterForService_serviceExists()880 public void testCardEmulationRemovePollingLoopPatternFilterForService_serviceExists() 881 throws RemoteException { 882 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 883 when(mRegisteredServicesCache.removePollingLoopPatternFilterForService(eq(USER_ID), 884 anyInt(), any(), any())).thenReturn(true); 885 String pollingLoopFilter = "filter"; 886 887 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 888 .removePollingLoopPatternFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 889 pollingLoopFilter)); 890 891 ExtendedMockito.verify(() -> { 892 NfcPermissions.enforceUserPermissions(mContext); 893 }); 894 ExtendedMockito.verify(() -> { 895 NfcPermissions.validateUserId(USER_ID); 896 }); 897 verify(mRegisteredServicesCache).initialize(); 898 verify(mRegisteredServicesCache, times(2)) 899 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 900 verify(mRegisteredServicesCache).removePollingLoopPatternFilterForService(eq(USER_ID), 901 anyInt(), eq(WALLET_PAYMENT_SERVICE), eq(pollingLoopFilter)); 902 verifyNoMoreInteractions(mRegisteredServicesCache); 903 } 904 905 @Test testCardEmulationRemovePollingLoopPatternFilterForService_serviceDoesNotExists()906 public void testCardEmulationRemovePollingLoopPatternFilterForService_serviceDoesNotExists() 907 throws RemoteException { 908 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 909 when(mRegisteredServicesCache.removePollingLoopPatternFilterForService(eq(USER_ID), 910 anyInt(), any(), any())).thenReturn(true); 911 String pollingLoopFilter = "filter"; 912 913 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 914 .removePollingLoopPatternFilterForService(USER_ID, WALLET_PAYMENT_SERVICE, 915 pollingLoopFilter)); 916 917 ExtendedMockito.verify(() -> { 918 NfcPermissions.enforceUserPermissions(mContext); 919 }); 920 ExtendedMockito.verify(() -> { 921 NfcPermissions.validateUserId(USER_ID); 922 }); 923 verify(mRegisteredServicesCache).initialize(); 924 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 925 verify(mRegisteredServicesCache, times(2)) 926 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 927 verifyNoMoreInteractions(mRegisteredServicesCache); 928 } 929 930 @Test testCardEmulationSetOffHostForService_serviceExists()931 public void testCardEmulationSetOffHostForService_serviceExists() 932 throws RemoteException { 933 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 934 when(mRegisteredServicesCache.setOffHostSecureElement(eq(USER_ID), 935 anyInt(), any(), any())).thenReturn(true); 936 String offhostse = "offhostse"; 937 938 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 939 .setOffHostForService(USER_ID, WALLET_PAYMENT_SERVICE, offhostse)); 940 941 ExtendedMockito.verify(() -> { 942 NfcPermissions.enforceUserPermissions(mContext); 943 }); 944 ExtendedMockito.verify(() -> { 945 NfcPermissions.validateUserId(USER_ID); 946 }); 947 verify(mRegisteredServicesCache).initialize(); 948 verify(mRegisteredServicesCache, times(2)) 949 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 950 verify(mRegisteredServicesCache).setOffHostSecureElement(eq(USER_ID), anyInt(), 951 eq(WALLET_PAYMENT_SERVICE) , eq(offhostse)); 952 verifyNoMoreInteractions(mRegisteredServicesCache); 953 } 954 955 @Test testCardEmulationSetOffHostForService_serviceDoesNotExists()956 public void testCardEmulationSetOffHostForService_serviceDoesNotExists() 957 throws RemoteException { 958 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 959 when(mRegisteredServicesCache.setOffHostSecureElement(eq(USER_ID), 960 anyInt(), any(), any())).thenReturn(true); 961 String offhostse = "offhostse"; 962 963 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 964 .setOffHostForService(USER_ID, WALLET_PAYMENT_SERVICE, offhostse)); 965 966 ExtendedMockito.verify(() -> { 967 NfcPermissions.enforceUserPermissions(mContext); 968 }); 969 ExtendedMockito.verify(() -> { 970 NfcPermissions.validateUserId(USER_ID); 971 }); 972 verify(mRegisteredServicesCache).initialize(); 973 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 974 verify(mRegisteredServicesCache, times(2)) 975 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 976 verifyNoMoreInteractions(mRegisteredServicesCache); 977 } 978 979 @Test testCardEmulationUnsetOffHostForService_serviceExists()980 public void testCardEmulationUnsetOffHostForService_serviceExists() 981 throws RemoteException { 982 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 983 when(mRegisteredServicesCache.resetOffHostSecureElement(eq(USER_ID), 984 anyInt(), any())).thenReturn(true); 985 986 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 987 .unsetOffHostForService(USER_ID, WALLET_PAYMENT_SERVICE)); 988 989 ExtendedMockito.verify(() -> { 990 NfcPermissions.enforceUserPermissions(mContext); 991 }); 992 ExtendedMockito.verify(() -> { 993 NfcPermissions.validateUserId(USER_ID); 994 }); 995 verify(mRegisteredServicesCache).initialize(); 996 verify(mRegisteredServicesCache, times(2)) 997 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 998 verify(mRegisteredServicesCache).resetOffHostSecureElement(eq(USER_ID), anyInt(), 999 eq(WALLET_PAYMENT_SERVICE)); 1000 verifyNoMoreInteractions(mRegisteredServicesCache); 1001 verify(mNfcService).onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_UPDATED)); 1002 } 1003 1004 @Test testCardEmulationUnsetOffHostForService_serviceDoesNotExists()1005 public void testCardEmulationUnsetOffHostForService_serviceDoesNotExists() 1006 throws RemoteException { 1007 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 1008 when(mRegisteredServicesCache.resetOffHostSecureElement(eq(USER_ID), 1009 anyInt(), any())).thenReturn(true); 1010 1011 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1012 .unsetOffHostForService(USER_ID, WALLET_PAYMENT_SERVICE)); 1013 1014 ExtendedMockito.verify(() -> { 1015 NfcPermissions.enforceUserPermissions(mContext); 1016 }); 1017 ExtendedMockito.verify(() -> { 1018 NfcPermissions.validateUserId(USER_ID); 1019 }); 1020 verify(mRegisteredServicesCache).initialize(); 1021 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 1022 verify(mRegisteredServicesCache, times(2)) 1023 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1024 verifyNoMoreInteractions(mRegisteredServicesCache); 1025 } 1026 1027 @Test testCardEmulationGetAidGroupForService_serviceExists()1028 public void testCardEmulationGetAidGroupForService_serviceExists() 1029 throws RemoteException { 1030 AidGroup aidGroup = Mockito.mock(AidGroup.class); 1031 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 1032 when(mRegisteredServicesCache.getAidGroupForService(eq(USER_ID), 1033 anyInt(), any(), eq(CardEmulation.CATEGORY_PAYMENT))).thenReturn(aidGroup); 1034 1035 Assert.assertEquals(mCardEmulationManager.getNfcCardEmulationInterface() 1036 .getAidGroupForService(USER_ID, WALLET_PAYMENT_SERVICE, 1037 CardEmulation.CATEGORY_PAYMENT), aidGroup); 1038 1039 ExtendedMockito.verify(() -> { 1040 NfcPermissions.enforceUserPermissions(mContext); 1041 }); 1042 ExtendedMockito.verify(() -> { 1043 NfcPermissions.validateUserId(USER_ID); 1044 }); 1045 verify(mRegisteredServicesCache).initialize(); 1046 verify(mRegisteredServicesCache, times(2)) 1047 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1048 verify(mRegisteredServicesCache).getAidGroupForService(eq(USER_ID), anyInt(), 1049 eq(WALLET_PAYMENT_SERVICE), eq(CardEmulation.CATEGORY_PAYMENT)); 1050 verifyNoMoreInteractions(mRegisteredServicesCache); 1051 } 1052 1053 @Test testCardEmulationGetAidGroupForService_serviceDoesNotExists()1054 public void testCardEmulationGetAidGroupForService_serviceDoesNotExists() 1055 throws RemoteException { 1056 AidGroup aidGroup = Mockito.mock(AidGroup.class); 1057 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 1058 when(mRegisteredServicesCache.getAidGroupForService(eq(USER_ID), 1059 anyInt(), any(), eq(CardEmulation.CATEGORY_PAYMENT))).thenReturn(aidGroup); 1060 1061 Assert.assertNull(mCardEmulationManager.getNfcCardEmulationInterface() 1062 .getAidGroupForService(USER_ID, WALLET_PAYMENT_SERVICE, 1063 CardEmulation.CATEGORY_PAYMENT)); 1064 1065 ExtendedMockito.verify(() -> { 1066 NfcPermissions.enforceUserPermissions(mContext); 1067 }); 1068 ExtendedMockito.verify(() -> { 1069 NfcPermissions.validateUserId(USER_ID); 1070 }); 1071 verify(mRegisteredServicesCache).initialize(); 1072 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 1073 verify(mRegisteredServicesCache, times(2)) 1074 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1075 verifyNoMoreInteractions(mRegisteredServicesCache); 1076 } 1077 1078 @Test testCardEmulationRemoveAidGroupForService_serviceExists()1079 public void testCardEmulationRemoveAidGroupForService_serviceExists() 1080 throws RemoteException { 1081 AidGroup aidGroup = Mockito.mock(AidGroup.class); 1082 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 1083 when(mRegisteredServicesCache.removeAidGroupForService(eq(USER_ID), 1084 anyInt(), any(), eq(CardEmulation.CATEGORY_PAYMENT))).thenReturn(true); 1085 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1086 .removeAidGroupForService(USER_ID, WALLET_PAYMENT_SERVICE, 1087 CardEmulation.CATEGORY_PAYMENT)); 1088 1089 ExtendedMockito.verify(() -> { 1090 NfcPermissions.enforceUserPermissions(mContext); 1091 }); 1092 ExtendedMockito.verify(() -> { 1093 NfcPermissions.validateUserId(USER_ID); 1094 }); 1095 verify(mRegisteredServicesCache).initialize(); 1096 verify(mRegisteredServicesCache, times(2)) 1097 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1098 verify(mRegisteredServicesCache).removeAidGroupForService(eq(USER_ID), anyInt(), 1099 eq(WALLET_PAYMENT_SERVICE), eq(CardEmulation.CATEGORY_PAYMENT)); 1100 verifyNoMoreInteractions(mRegisteredServicesCache); 1101 verify(mNfcService).onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_UPDATED)); 1102 } 1103 1104 @Test testCardEmulationRemoveAidGroupForService_serviceDoesNotExists()1105 public void testCardEmulationRemoveAidGroupForService_serviceDoesNotExists() 1106 throws RemoteException { 1107 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 1108 when(mRegisteredServicesCache.removeAidGroupForService(eq(USER_ID), 1109 anyInt(), any(), eq(CardEmulation.CATEGORY_PAYMENT))).thenReturn(true); 1110 1111 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1112 .removeAidGroupForService(USER_ID, WALLET_PAYMENT_SERVICE, 1113 CardEmulation.CATEGORY_PAYMENT)); 1114 1115 ExtendedMockito.verify(() -> { 1116 NfcPermissions.enforceUserPermissions(mContext); 1117 }); 1118 ExtendedMockito.verify(() -> { 1119 NfcPermissions.validateUserId(USER_ID); 1120 }); 1121 verify(mRegisteredServicesCache).initialize(); 1122 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 1123 verify(mRegisteredServicesCache, times(2)) 1124 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1125 verifyNoMoreInteractions(mRegisteredServicesCache); 1126 } 1127 1128 @Test testCardEmulationGetServices()1129 public void testCardEmulationGetServices() 1130 throws RemoteException { 1131 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 1132 when(mRegisteredServicesCache.getServicesForCategory(eq(USER_ID), 1133 eq(CardEmulation.CATEGORY_PAYMENT))).thenReturn(UPDATED_SERVICES); 1134 1135 Assert.assertEquals(mCardEmulationManager.getNfcCardEmulationInterface() 1136 .getServices(USER_ID, CardEmulation.CATEGORY_PAYMENT), UPDATED_SERVICES); 1137 1138 ExtendedMockito.verify(() -> { 1139 NfcPermissions.validateProfileId(mContext, USER_ID); 1140 }); 1141 ExtendedMockito.verify(() -> { 1142 NfcPermissions.enforceAdminPermissions(mContext); 1143 }); 1144 verify(mRegisteredServicesCache).initialize(); 1145 verify(mRegisteredServicesCache).getServicesForCategory(eq(USER_ID), 1146 eq(CardEmulation.CATEGORY_PAYMENT)); 1147 verifyNoMoreInteractions(mRegisteredServicesCache); 1148 } 1149 1150 @Test testCardEmulationSetPreferredService_serviceExists()1151 public void testCardEmulationSetPreferredService_serviceExists() 1152 throws RemoteException { 1153 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 1154 when(mPreferredServices.registerPreferredForegroundService(eq(WALLET_PAYMENT_SERVICE), 1155 anyInt())).thenReturn(true); 1156 1157 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1158 .setPreferredService(WALLET_PAYMENT_SERVICE)); 1159 1160 ExtendedMockito.verify(() -> { 1161 NfcPermissions.enforceUserPermissions(mContext); 1162 }); 1163 verify(mRegisteredServicesCache).initialize(); 1164 verify(mRegisteredServicesCache, times(2)) 1165 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1166 verifyNoMoreInteractions(mRegisteredServicesCache); 1167 verify(mPreferredServices).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1168 eq(USER_ID)); 1169 verify(mPreferredServices).registerPreferredForegroundService(eq(WALLET_PAYMENT_SERVICE), 1170 anyInt()); 1171 verifyNoMoreInteractions(mPreferredServices); 1172 } 1173 1174 @Test testCardEmulationSetPreferredService_serviceDoesNotExists()1175 public void testCardEmulationSetPreferredService_serviceDoesNotExists() 1176 throws RemoteException { 1177 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(false); 1178 when(mPreferredServices.registerPreferredForegroundService(eq(WALLET_PAYMENT_SERVICE), 1179 anyInt())).thenReturn(false); 1180 1181 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1182 .setPreferredService(WALLET_PAYMENT_SERVICE)); 1183 1184 ExtendedMockito.verify(() -> { 1185 NfcPermissions.enforceUserPermissions(mContext); 1186 }); 1187 verify(mRegisteredServicesCache).initialize(); 1188 verify(mRegisteredServicesCache).invalidateCache(eq(USER_ID), eq(true)); 1189 verify(mRegisteredServicesCache, times(2)) 1190 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1191 verifyNoMoreInteractions(mRegisteredServicesCache); 1192 verify(mPreferredServices).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1193 eq(USER_ID)); 1194 verifyNoMoreInteractions(mPreferredServices); 1195 } 1196 1197 @Test testCardEmulationUnsetPreferredService_serviceExists()1198 public void testCardEmulationUnsetPreferredService_serviceExists() 1199 throws RemoteException { 1200 when(mRegisteredServicesCache.hasService(eq(USER_ID), any())).thenReturn(true); 1201 when(mPreferredServices.unregisteredPreferredForegroundService(anyInt())) 1202 .thenReturn(true); 1203 1204 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1205 .unsetPreferredService()); 1206 1207 ExtendedMockito.verify(() -> { 1208 NfcPermissions.enforceUserPermissions(mContext); 1209 }); 1210 verify(mPreferredServices).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1211 eq(USER_ID)); 1212 verify(mPreferredServices).unregisteredPreferredForegroundService(anyInt()); 1213 verifyNoMoreInteractions(mPreferredServices); 1214 } 1215 1216 @Test testCardEmulationUnsetPreferredService_serviceDoesNotExists()1217 public void testCardEmulationUnsetPreferredService_serviceDoesNotExists() 1218 throws RemoteException { 1219 when(mPreferredServices.unregisteredPreferredForegroundService(anyInt())) 1220 .thenReturn(false); 1221 1222 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1223 .unsetPreferredService()); 1224 1225 ExtendedMockito.verify(() -> { 1226 NfcPermissions.enforceUserPermissions(mContext); 1227 }); 1228 verify(mPreferredServices).unregisteredPreferredForegroundService(anyInt()); 1229 } 1230 1231 @Test testCardEmulationSupportsAidPrefixRegistration_doesSupport()1232 public void testCardEmulationSupportsAidPrefixRegistration_doesSupport() 1233 throws RemoteException { 1234 when(mRegisteredAidCache.supportsAidPrefixRegistration()).thenReturn(true); 1235 1236 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1237 .supportsAidPrefixRegistration()); 1238 1239 verify(mRegisteredAidCache).supportsAidPrefixRegistration(); 1240 } 1241 1242 @Test testCardEmulationSupportsAidPrefixRegistration_doesNotSupport()1243 public void testCardEmulationSupportsAidPrefixRegistration_doesNotSupport() 1244 throws RemoteException { 1245 when(mRegisteredAidCache.supportsAidPrefixRegistration()).thenReturn(false); 1246 1247 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1248 .supportsAidPrefixRegistration()); 1249 1250 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1251 eq(USER_ID)); 1252 verify(mRegisteredAidCache).supportsAidPrefixRegistration(); 1253 verifyNoMoreInteractions(mRegisteredAidCache); 1254 } 1255 1256 @Test testCardEmulationGetPreferredPaymentService()1257 public void testCardEmulationGetPreferredPaymentService() 1258 throws RemoteException { 1259 ApduServiceInfo apduServiceInfo = Mockito.mock(ApduServiceInfo.class); 1260 when(mRegisteredAidCache.getPreferredService()) 1261 .thenReturn(new Pair<>(USER_ID, WALLET_PAYMENT_SERVICE)); 1262 when(mRegisteredServicesCache.getService(eq(USER_ID), eq(WALLET_PAYMENT_SERVICE))) 1263 .thenReturn(apduServiceInfo); 1264 1265 Assert.assertEquals(mCardEmulationManager.getNfcCardEmulationInterface() 1266 .getPreferredPaymentService(USER_ID), apduServiceInfo); 1267 1268 ExtendedMockito.verify(() -> { 1269 NfcPermissions.validateUserId(USER_ID); 1270 }); 1271 ExtendedMockito.verify(() -> { 1272 NfcPermissions.enforceUserPermissions(mContext); 1273 }); 1274 ExtendedMockito.verify(() -> { 1275 NfcPermissions.enforcePreferredPaymentInfoPermissions(mContext); 1276 }); 1277 verify(mRegisteredServicesCache).initialize(); 1278 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1279 eq(USER_ID)); 1280 verify(mRegisteredAidCache).getPreferredService(); 1281 verify(mRegisteredServicesCache).getService(eq(USER_ID), eq(WALLET_PAYMENT_SERVICE)); 1282 verifyNoMoreInteractions(mRegisteredAidCache); 1283 verifyNoMoreInteractions(mRegisteredServicesCache); 1284 } 1285 1286 @Test testCardEmulationSetServiceEnabledForCategoryOther_resourceTrue()1287 public void testCardEmulationSetServiceEnabledForCategoryOther_resourceTrue() 1288 throws RemoteException { 1289 when(mResources.getBoolean(R.bool.enable_service_for_category_other)).thenReturn(true); 1290 when(mRegisteredServicesCache.registerOtherForService(anyInt(), any(), anyBoolean())) 1291 .thenReturn(true); 1292 1293 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1294 .setServiceEnabledForCategoryOther(USER_ID, WALLET_PAYMENT_SERVICE, true)); 1295 1296 ExtendedMockito.verify(() -> { 1297 NfcPermissions.enforceUserPermissions(mContext); 1298 }); 1299 verify(mRegisteredServicesCache).initialize(); 1300 verify(mRegisteredServicesCache).registerOtherForService(eq(USER_ID), 1301 eq(WALLET_PAYMENT_SERVICE), eq(true)); 1302 verifyNoMoreInteractions(mRegisteredServicesCache); 1303 } 1304 1305 @Test testCardEmulationSetServiceEnabledForCategoryOther_resourceFalse()1306 public void testCardEmulationSetServiceEnabledForCategoryOther_resourceFalse() 1307 throws RemoteException { 1308 when(mResources.getBoolean(R.bool.enable_service_for_category_other)).thenReturn(false); 1309 when(mRegisteredServicesCache.registerOtherForService(anyInt(), any(), anyBoolean())) 1310 .thenReturn(true); 1311 1312 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1313 .setServiceEnabledForCategoryOther(USER_ID, WALLET_PAYMENT_SERVICE, true)); 1314 1315 verify(mRegisteredServicesCache).initialize(); 1316 verifyNoMoreInteractions(mRegisteredServicesCache); 1317 } 1318 1319 @Test testCardEmulationIsDefaultPaymentRegistered_walletRoleEnabledWalletSet()1320 public void testCardEmulationIsDefaultPaymentRegistered_walletRoleEnabledWalletSet() 1321 throws RemoteException { 1322 when(mWalletRoleObserver.isWalletRoleFeatureEnabled()).thenReturn(true); 1323 when(mWalletRoleObserver.getDefaultWalletRoleHolder(anyInt())) 1324 .thenReturn(WALLET_HOLDER_PACKAGE_NAME); 1325 when(Binder.getCallingUserHandle()).thenReturn(USER_HANDLE); 1326 1327 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1328 .isDefaultPaymentRegistered()); 1329 1330 verify(mWalletRoleObserver, times(2)).isWalletRoleFeatureEnabled(); 1331 verify(mWalletRoleObserver, times(2)) 1332 .getDefaultWalletRoleHolder(eq(USER_ID)); 1333 } 1334 1335 @Test testCardEmulationIsDefaultPaymentRegistered_walletRoleEnabledWalletNone()1336 public void testCardEmulationIsDefaultPaymentRegistered_walletRoleEnabledWalletNone() 1337 throws RemoteException { 1338 when(mWalletRoleObserver.isWalletRoleFeatureEnabled()).thenReturn(true); 1339 when(mWalletRoleObserver.getDefaultWalletRoleHolder(anyInt())) 1340 .thenReturn(null); 1341 when(Binder.getCallingUserHandle()).thenReturn(USER_HANDLE); 1342 1343 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1344 .isDefaultPaymentRegistered()); 1345 1346 verify(mWalletRoleObserver, times(2)).isWalletRoleFeatureEnabled(); 1347 verify(mWalletRoleObserver, times(2)) 1348 .getDefaultWalletRoleHolder(eq(USER_ID)); 1349 } 1350 1351 @Test testCardEmulationOverrideRoutingTable_callerNotForeground()1352 public void testCardEmulationOverrideRoutingTable_callerNotForeground() 1353 throws RemoteException { 1354 when(mForegroundUtils.registerUidToBackgroundCallback(any(), anyInt())) 1355 .thenReturn(false); 1356 String protocol = "DH"; 1357 String technology = "DH"; 1358 1359 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1360 .overrideRoutingTable(USER_ID, protocol, technology)); 1361 1362 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1363 eq(USER_ID)); 1364 verify(mRoutingOptionManager).getOffHostRouteEse(); 1365 verify(mRoutingOptionManager).getOffHostRouteUicc(); 1366 verifyNoMoreInteractions(mRoutingOptionManager); 1367 verifyNoMoreInteractions(mRegisteredAidCache); 1368 } 1369 1370 @Test testCardEmulationOverrideRoutingTable_callerForegroundRouteNull()1371 public void testCardEmulationOverrideRoutingTable_callerForegroundRouteNull() 1372 throws RemoteException { 1373 when(mForegroundUtils.registerUidToBackgroundCallback(any(), anyInt())) 1374 .thenReturn(true); 1375 1376 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1377 .overrideRoutingTable(USER_ID, null, null)); 1378 1379 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1380 eq(USER_ID)); 1381 verify(mRoutingOptionManager).overrideDefaultIsoDepRoute(eq(-1)); 1382 verify(mRoutingOptionManager).overrideDefaultOffHostRoute(eq(-1)); 1383 verify(mRoutingOptionManager).getOffHostRouteEse(); 1384 verify(mRoutingOptionManager).getOffHostRouteUicc(); 1385 verify(mRegisteredAidCache).onRoutingOverridedOrRecovered(); 1386 verifyNoMoreInteractions(mRoutingOptionManager); 1387 verifyNoMoreInteractions(mRegisteredAidCache); 1388 } 1389 1390 @Test testCardEmulationOverrideRoutingTable_callerForegroundRouteDH()1391 public void testCardEmulationOverrideRoutingTable_callerForegroundRouteDH() 1392 throws RemoteException { 1393 when(mForegroundUtils.registerUidToBackgroundCallback(any(), anyInt())) 1394 .thenReturn(true); 1395 String protocol = "DH"; 1396 String technology = "DH"; 1397 1398 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1399 .overrideRoutingTable(USER_ID, protocol, technology)); 1400 1401 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1402 eq(USER_ID)); 1403 verify(mRoutingOptionManager).overrideDefaultIsoDepRoute(eq(0)); 1404 verify(mRoutingOptionManager).overrideDefaultOffHostRoute(eq(0)); 1405 verify(mRoutingOptionManager).getOffHostRouteEse(); 1406 verify(mRoutingOptionManager).getOffHostRouteUicc(); 1407 verify(mRegisteredAidCache).onRoutingOverridedOrRecovered(); 1408 verifyNoMoreInteractions(mRoutingOptionManager); 1409 verifyNoMoreInteractions(mRegisteredAidCache); 1410 } 1411 1412 @Test testCardEmulationOverrideRoutingTable_callerForegroundRouteeSE()1413 public void testCardEmulationOverrideRoutingTable_callerForegroundRouteeSE() 1414 throws RemoteException { 1415 when(mForegroundUtils.registerUidToBackgroundCallback(any(), anyInt())) 1416 .thenReturn(true); 1417 String protocol = "eSE1"; 1418 String technology = "eSE1"; 1419 1420 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1421 .overrideRoutingTable(USER_ID, protocol, technology)); 1422 1423 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1424 eq(USER_ID)); 1425 verify(mRoutingOptionManager).overrideDefaultIsoDepRoute(eq(TEST_DATA_1[0] & 0xFF)); 1426 verify(mRoutingOptionManager).overrideDefaultOffHostRoute(eq(TEST_DATA_1[0] & 0xFF)); 1427 verify(mRoutingOptionManager).getOffHostRouteEse(); 1428 verify(mRoutingOptionManager).getOffHostRouteUicc(); 1429 verify(mRegisteredAidCache).onRoutingOverridedOrRecovered(); 1430 verifyNoMoreInteractions(mRoutingOptionManager); 1431 verifyNoMoreInteractions(mRegisteredAidCache); 1432 } 1433 1434 @Test testCardEmulationOverrideRoutingTable_callerForegroundRouteSIM()1435 public void testCardEmulationOverrideRoutingTable_callerForegroundRouteSIM() 1436 throws RemoteException { 1437 when(mForegroundUtils.registerUidToBackgroundCallback(any(), anyInt())) 1438 .thenReturn(true); 1439 String protocol = "SIM1"; 1440 String technology = "SIM1"; 1441 1442 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1443 .overrideRoutingTable(USER_ID, protocol, technology)); 1444 1445 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1446 eq(USER_ID)); 1447 verify(mRoutingOptionManager).overrideDefaultIsoDepRoute(eq(TEST_DATA_2[0] & 0xFF)); 1448 verify(mRoutingOptionManager).overrideDefaultOffHostRoute(eq(TEST_DATA_2[0] & 0xFF)); 1449 verify(mRoutingOptionManager).getOffHostRouteEse(); 1450 verify(mRoutingOptionManager).getOffHostRouteUicc(); 1451 verify(mRegisteredAidCache).onRoutingOverridedOrRecovered(); 1452 verifyNoMoreInteractions(mRoutingOptionManager); 1453 verifyNoMoreInteractions(mRegisteredAidCache); 1454 } 1455 1456 @Test testCardEmulationRecoverRoutingTable_callerForeground()1457 public void testCardEmulationRecoverRoutingTable_callerForeground() 1458 throws RemoteException { 1459 when(mForegroundUtils.isInForeground(anyInt())) 1460 .thenReturn(true); 1461 1462 assertTrue(mCardEmulationManager.getNfcCardEmulationInterface() 1463 .recoverRoutingTable(USER_ID)); 1464 1465 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1466 eq(USER_ID)); 1467 verify(mRoutingOptionManager).recoverOverridedRoutingTable(); 1468 verify(mRoutingOptionManager).getOffHostRouteEse(); 1469 verify(mRoutingOptionManager).getOffHostRouteUicc(); 1470 verify(mRegisteredAidCache).onRoutingOverridedOrRecovered(); 1471 verifyNoMoreInteractions(mRoutingOptionManager); 1472 verifyNoMoreInteractions(mRegisteredAidCache); 1473 } 1474 1475 @Test testCardEmulationRecoverRoutingTable_callerNotForeground()1476 public void testCardEmulationRecoverRoutingTable_callerNotForeground() 1477 throws RemoteException { 1478 when(mForegroundUtils.isInForeground(anyInt())) 1479 .thenReturn(false); 1480 1481 Assert.assertFalse(mCardEmulationManager.getNfcCardEmulationInterface() 1482 .recoverRoutingTable(USER_ID)); 1483 1484 verify(mRegisteredAidCache).onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), 1485 eq(USER_ID)); 1486 verify(mRoutingOptionManager).getOffHostRouteEse(); 1487 verify(mRoutingOptionManager).getOffHostRouteUicc(); 1488 verifyNoMoreInteractions(mRoutingOptionManager); 1489 verifyNoMoreInteractions(mRegisteredAidCache); 1490 } 1491 1492 @Test testNfcFCardEmulationGetSystemCodeForService_serviceExists()1493 public void testNfcFCardEmulationGetSystemCodeForService_serviceExists() 1494 throws RemoteException { 1495 String systemCode = "systemCode"; 1496 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1497 .thenReturn(true); 1498 when(mRegisteredNfcFServicesCache.getSystemCodeForService(anyInt(), 1499 anyInt(), any())).thenReturn(systemCode); 1500 1501 Assert.assertEquals(mCardEmulationManager.getNfcFCardEmulationInterface() 1502 .getSystemCodeForService(USER_ID, WALLET_PAYMENT_SERVICE), systemCode); 1503 1504 ExtendedMockito.verify(() -> { 1505 NfcPermissions.validateUserId(USER_ID); 1506 }); 1507 ExtendedMockito.verify(() -> { 1508 NfcPermissions.enforceUserPermissions(mContext); 1509 }); 1510 verify(mRegisteredNfcFServicesCache).initialize(); 1511 verify(mRegisteredNfcFServicesCache, times(2)) 1512 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1513 verify(mRegisteredNfcFServicesCache).getSystemCodeForService(eq(USER_ID), anyInt(), 1514 eq(WALLET_PAYMENT_SERVICE)); 1515 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1516 } 1517 1518 @Test testNfcFCardEmulationGetSystemCodeForService_serviceDoesNotExists()1519 public void testNfcFCardEmulationGetSystemCodeForService_serviceDoesNotExists() 1520 throws RemoteException { 1521 String systemCode = "systemCode"; 1522 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1523 .thenReturn(false); 1524 when(mRegisteredNfcFServicesCache.getSystemCodeForService(anyInt(), 1525 anyInt(), any())).thenReturn(systemCode); 1526 1527 Assert.assertNull(mCardEmulationManager.getNfcFCardEmulationInterface() 1528 .getSystemCodeForService(USER_ID, WALLET_PAYMENT_SERVICE)); 1529 1530 ExtendedMockito.verify(() -> { 1531 NfcPermissions.validateUserId(USER_ID); 1532 }); 1533 ExtendedMockito.verify(() -> { 1534 NfcPermissions.enforceUserPermissions(mContext); 1535 }); 1536 verify(mRegisteredNfcFServicesCache).initialize(); 1537 verify(mRegisteredNfcFServicesCache).invalidateCache(eq(USER_ID)); 1538 verify(mRegisteredNfcFServicesCache, times(2)) 1539 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1540 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1541 } 1542 1543 @Test testNfcFCardEmulationRegisterSystemCodeForService_serviceExists()1544 public void testNfcFCardEmulationRegisterSystemCodeForService_serviceExists() 1545 throws RemoteException { 1546 String systemCode = "systemCode"; 1547 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1548 .thenReturn(true); 1549 when(mRegisteredNfcFServicesCache.registerSystemCodeForService(anyInt(), 1550 anyInt(), any(), anyString())).thenReturn(true); 1551 1552 assertTrue(mCardEmulationManager.getNfcFCardEmulationInterface() 1553 .registerSystemCodeForService(USER_ID, WALLET_PAYMENT_SERVICE, systemCode)); 1554 1555 ExtendedMockito.verify(() -> { 1556 NfcPermissions.validateUserId(USER_ID); 1557 }); 1558 ExtendedMockito.verify(() -> { 1559 NfcPermissions.enforceUserPermissions(mContext); 1560 }); 1561 verify(mRegisteredNfcFServicesCache).initialize(); 1562 verify(mRegisteredNfcFServicesCache, times(2)) 1563 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1564 verify(mRegisteredNfcFServicesCache).registerSystemCodeForService(eq(USER_ID), anyInt(), 1565 eq(WALLET_PAYMENT_SERVICE), eq(systemCode)); 1566 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1567 } 1568 1569 @Test testNfcFCardEmulationRegisterSystemCodeForService_serviceDoesNotExists()1570 public void testNfcFCardEmulationRegisterSystemCodeForService_serviceDoesNotExists() 1571 throws RemoteException { 1572 String systemCode = "systemCode"; 1573 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1574 .thenReturn(false); 1575 when(mRegisteredNfcFServicesCache.registerSystemCodeForService(anyInt(), 1576 anyInt(), any(), anyString())).thenReturn(true); 1577 1578 Assert.assertFalse(mCardEmulationManager.getNfcFCardEmulationInterface() 1579 .registerSystemCodeForService(USER_ID, WALLET_PAYMENT_SERVICE, systemCode)); 1580 1581 ExtendedMockito.verify(() -> { 1582 NfcPermissions.validateUserId(USER_ID); 1583 }); 1584 ExtendedMockito.verify(() -> { 1585 NfcPermissions.enforceUserPermissions(mContext); 1586 }); 1587 verify(mRegisteredNfcFServicesCache).initialize(); 1588 verify(mRegisteredNfcFServicesCache).invalidateCache(eq(USER_ID)); 1589 verify(mRegisteredNfcFServicesCache, times(2)) 1590 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1591 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1592 } 1593 1594 @Test testNfcFCardEmulationRemoveSystemCodeForService_serviceExists()1595 public void testNfcFCardEmulationRemoveSystemCodeForService_serviceExists() 1596 throws RemoteException { 1597 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1598 .thenReturn(true); 1599 when(mRegisteredNfcFServicesCache.removeSystemCodeForService(anyInt(), 1600 anyInt(), any())).thenReturn(true); 1601 1602 assertTrue(mCardEmulationManager.getNfcFCardEmulationInterface() 1603 .removeSystemCodeForService(USER_ID, WALLET_PAYMENT_SERVICE)); 1604 1605 ExtendedMockito.verify(() -> { 1606 NfcPermissions.validateUserId(USER_ID); 1607 }); 1608 ExtendedMockito.verify(() -> { 1609 NfcPermissions.enforceUserPermissions(mContext); 1610 }); 1611 verify(mRegisteredNfcFServicesCache).initialize(); 1612 verify(mRegisteredNfcFServicesCache, times(2)) 1613 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1614 verify(mRegisteredNfcFServicesCache).removeSystemCodeForService(eq(USER_ID), anyInt(), 1615 eq(WALLET_PAYMENT_SERVICE)); 1616 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1617 } 1618 1619 @Test testNfcFCardEmulationRemoveSystemCodeForService_serviceDoesNotExists()1620 public void testNfcFCardEmulationRemoveSystemCodeForService_serviceDoesNotExists() 1621 throws RemoteException { 1622 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1623 .thenReturn(false); 1624 when(mRegisteredNfcFServicesCache.removeSystemCodeForService(anyInt(), 1625 anyInt(), any())).thenReturn(true); 1626 1627 Assert.assertFalse(mCardEmulationManager.getNfcFCardEmulationInterface() 1628 .removeSystemCodeForService(USER_ID, WALLET_PAYMENT_SERVICE)); 1629 1630 ExtendedMockito.verify(() -> { 1631 NfcPermissions.validateUserId(USER_ID); 1632 }); 1633 ExtendedMockito.verify(() -> { 1634 NfcPermissions.enforceUserPermissions(mContext); 1635 }); 1636 verify(mRegisteredNfcFServicesCache).initialize(); 1637 verify(mRegisteredNfcFServicesCache).invalidateCache(eq(USER_ID)); 1638 verify(mRegisteredNfcFServicesCache, times(2)) 1639 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1640 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1641 } 1642 1643 @Test testNfcFCardEmulationGetNfcid2ForService_serviceExists()1644 public void testNfcFCardEmulationGetNfcid2ForService_serviceExists() 1645 throws RemoteException { 1646 String nfcid2 = "nfcid2"; 1647 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1648 .thenReturn(true); 1649 when(mRegisteredNfcFServicesCache.getNfcid2ForService(anyInt(), 1650 anyInt(), any())).thenReturn(nfcid2); 1651 1652 Assert.assertEquals(mCardEmulationManager.getNfcFCardEmulationInterface() 1653 .getNfcid2ForService(USER_ID, WALLET_PAYMENT_SERVICE), nfcid2); 1654 1655 ExtendedMockito.verify(() -> { 1656 NfcPermissions.validateUserId(USER_ID); 1657 }); 1658 ExtendedMockito.verify(() -> { 1659 NfcPermissions.enforceUserPermissions(mContext); 1660 }); 1661 verify(mRegisteredNfcFServicesCache).initialize(); 1662 verify(mRegisteredNfcFServicesCache, times(2)) 1663 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1664 verify(mRegisteredNfcFServicesCache).getNfcid2ForService(eq(USER_ID), anyInt(), 1665 eq(WALLET_PAYMENT_SERVICE)); 1666 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1667 } 1668 1669 @Test testNfcFCardEmulationGetNfcid2ForService_serviceDoesNotExists()1670 public void testNfcFCardEmulationGetNfcid2ForService_serviceDoesNotExists() 1671 throws RemoteException { 1672 String nfcid2 = "nfcid2"; 1673 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1674 .thenReturn(false); 1675 when(mRegisteredNfcFServicesCache.getNfcid2ForService(anyInt(), 1676 anyInt(), any())).thenReturn(nfcid2); 1677 1678 Assert.assertNull(mCardEmulationManager.getNfcFCardEmulationInterface() 1679 .getNfcid2ForService(USER_ID, WALLET_PAYMENT_SERVICE)); 1680 1681 ExtendedMockito.verify(() -> { 1682 NfcPermissions.validateUserId(USER_ID); 1683 }); 1684 ExtendedMockito.verify(() -> { 1685 NfcPermissions.enforceUserPermissions(mContext); 1686 }); 1687 verify(mRegisteredNfcFServicesCache).initialize(); 1688 verify(mRegisteredNfcFServicesCache).invalidateCache(eq(USER_ID)); 1689 verify(mRegisteredNfcFServicesCache, times(2)) 1690 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1691 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1692 } 1693 1694 @Test testNfcFCardEmulationSetNfcid2ForService_serviceExists()1695 public void testNfcFCardEmulationSetNfcid2ForService_serviceExists() 1696 throws RemoteException { 1697 String nfcid2 = "nfcid2"; 1698 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1699 .thenReturn(true); 1700 when(mRegisteredNfcFServicesCache.setNfcid2ForService(anyInt(), 1701 anyInt(), any(), anyString())).thenReturn(true); 1702 1703 assertTrue(mCardEmulationManager.getNfcFCardEmulationInterface() 1704 .setNfcid2ForService(USER_ID, WALLET_PAYMENT_SERVICE, nfcid2)); 1705 1706 ExtendedMockito.verify(() -> { 1707 NfcPermissions.validateUserId(USER_ID); 1708 }); 1709 ExtendedMockito.verify(() -> { 1710 NfcPermissions.enforceUserPermissions(mContext); 1711 }); 1712 verify(mRegisteredNfcFServicesCache).initialize(); 1713 verify(mRegisteredNfcFServicesCache, times(2)) 1714 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1715 verify(mRegisteredNfcFServicesCache).setNfcid2ForService(eq(USER_ID), anyInt(), 1716 eq(WALLET_PAYMENT_SERVICE), eq(nfcid2)); 1717 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1718 } 1719 1720 @Test testNfcFCardEmulationSetNfcid2ForService_serviceDoesNotExists()1721 public void testNfcFCardEmulationSetNfcid2ForService_serviceDoesNotExists() 1722 throws RemoteException { 1723 String nfcid2 = "nfcid2"; 1724 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1725 .thenReturn(false); 1726 when(mRegisteredNfcFServicesCache.setNfcid2ForService(anyInt(), 1727 anyInt(), any(), anyString())).thenReturn(true); 1728 1729 Assert.assertFalse(mCardEmulationManager.getNfcFCardEmulationInterface() 1730 .setNfcid2ForService(USER_ID, WALLET_PAYMENT_SERVICE, nfcid2)); 1731 1732 ExtendedMockito.verify(() -> { 1733 NfcPermissions.validateUserId(USER_ID); 1734 }); 1735 ExtendedMockito.verify(() -> { 1736 NfcPermissions.enforceUserPermissions(mContext); 1737 }); 1738 verify(mRegisteredNfcFServicesCache).initialize(); 1739 verify(mRegisteredNfcFServicesCache).invalidateCache(eq(USER_ID)); 1740 verify(mRegisteredNfcFServicesCache, times(2)) 1741 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1742 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1743 } 1744 1745 @Test testNfcFCardEmulationEnableNfcFForegroundService_serviceExists()1746 public void testNfcFCardEmulationEnableNfcFForegroundService_serviceExists() 1747 throws RemoteException { 1748 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1749 .thenReturn(true); 1750 when(mEnabledNfcFServices.registerEnabledForegroundService(any(), 1751 anyInt())).thenReturn(true); 1752 when(Binder.getCallingUserHandle()).thenReturn(USER_HANDLE); 1753 1754 assertTrue(mCardEmulationManager.getNfcFCardEmulationInterface() 1755 .enableNfcFForegroundService(WALLET_PAYMENT_SERVICE)); 1756 1757 ExtendedMockito.verify(() -> { 1758 NfcPermissions.enforceUserPermissions(mContext); 1759 }); 1760 verify(mRegisteredNfcFServicesCache).initialize(); 1761 verify(mRegisteredNfcFServicesCache, times(2)) 1762 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1763 verify(mEnabledNfcFServices).registerEnabledForegroundService(eq(WALLET_PAYMENT_SERVICE), 1764 anyInt()); 1765 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1766 } 1767 1768 @Test testNfcFCardEmulationEnableNfcFForegroundService_serviceDoesNotExists()1769 public void testNfcFCardEmulationEnableNfcFForegroundService_serviceDoesNotExists() 1770 throws RemoteException { 1771 when(mRegisteredNfcFServicesCache.hasService(eq(USER_ID), any())) 1772 .thenReturn(false); 1773 when(mEnabledNfcFServices.registerEnabledForegroundService(any(), 1774 anyInt())).thenReturn(true); 1775 1776 Assert.assertFalse(mCardEmulationManager.getNfcFCardEmulationInterface() 1777 .enableNfcFForegroundService(WALLET_PAYMENT_SERVICE)); 1778 1779 ExtendedMockito.verify(() -> { 1780 NfcPermissions.enforceUserPermissions(mContext); 1781 }); 1782 verify(mRegisteredNfcFServicesCache).initialize(); 1783 verify(mRegisteredNfcFServicesCache).invalidateCache(eq(USER_ID)); 1784 verify(mRegisteredNfcFServicesCache, times(2)) 1785 .hasService(eq(USER_ID),eq(WALLET_PAYMENT_SERVICE)); 1786 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1787 verifyNoMoreInteractions(mEnabledNfcFServices); 1788 } 1789 1790 @Test testNfcFCardEmulationDisableNfcFForegroundService_serviceDoesNotExists()1791 public void testNfcFCardEmulationDisableNfcFForegroundService_serviceDoesNotExists() 1792 throws RemoteException { 1793 when(mEnabledNfcFServices.unregisteredEnabledForegroundService(anyInt())) 1794 .thenReturn(true); 1795 1796 assertTrue(mCardEmulationManager.getNfcFCardEmulationInterface() 1797 .disableNfcFForegroundService()); 1798 1799 ExtendedMockito.verify(() -> { 1800 NfcPermissions.enforceUserPermissions(mContext); 1801 }); 1802 verify(mEnabledNfcFServices).unregisteredEnabledForegroundService(anyInt()); 1803 verifyNoMoreInteractions(mEnabledNfcFServices); 1804 } 1805 1806 @Test testNfcFCardEmulationGetServices()1807 public void testNfcFCardEmulationGetServices() 1808 throws RemoteException { 1809 when(mRegisteredNfcFServicesCache.getServices(anyInt())) 1810 .thenReturn(UPDATED_NFC_SERVICES); 1811 1812 Assert.assertEquals(mCardEmulationManager.getNfcFCardEmulationInterface() 1813 .getNfcFServices(USER_ID), UPDATED_NFC_SERVICES); 1814 1815 ExtendedMockito.verify(() -> { 1816 NfcPermissions.validateProfileId(mContext, USER_ID); 1817 }); 1818 ExtendedMockito.verify(() -> { 1819 NfcPermissions.enforceUserPermissions(mContext); 1820 }); 1821 verify(mRegisteredNfcFServicesCache).initialize(); 1822 verify(mRegisteredNfcFServicesCache).getServices(eq(USER_ID)); 1823 verifyNoMoreInteractions(mRegisteredNfcFServicesCache); 1824 } 1825 1826 @Test testNfcFCardEmulationGetMaxNumOfRegisterableSystemCodes()1827 public void testNfcFCardEmulationGetMaxNumOfRegisterableSystemCodes() 1828 throws RemoteException { 1829 when(mNfcService.getLfT3tMax()).thenReturn(3); 1830 1831 Assert.assertEquals(mCardEmulationManager.getNfcFCardEmulationInterface() 1832 .getMaxNumOfRegisterableSystemCodes(), 3); 1833 1834 ExtendedMockito.verify(() -> { 1835 NfcPermissions.enforceUserPermissions(mContext); 1836 }); 1837 verify(mNfcService).getLfT3tMax(); 1838 verifyNoMoreInteractions(mNfcService); 1839 } 1840 1841 @Test testOnPreferredPaymentServiceChanged_observeModeEnabled()1842 public void testOnPreferredPaymentServiceChanged_observeModeEnabled() { 1843 when(mRegisteredAidCache.getPreferredService()) 1844 .thenReturn(new Pair<>(-1, null)); 1845 mCardEmulationManager.onPreferredPaymentServiceChanged(0, null); 1846 1847 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), any())) 1848 .thenReturn(true); 1849 when(android.nfc.Flags.nfcObserveMode()).thenReturn(true); 1850 1851 mCardEmulationManager.onPreferredPaymentServiceChanged(USER_ID, WALLET_PAYMENT_SERVICE); 1852 when(mRegisteredAidCache.getPreferredService()) 1853 .thenReturn(new Pair<>(USER_ID, WALLET_PAYMENT_SERVICE)); 1854 1855 verify(mHostEmulationManager).onPreferredPaymentServiceChanged(eq(USER_ID), 1856 eq(WALLET_PAYMENT_SERVICE)); 1857 verify(mRegisteredAidCache).onWalletRoleHolderChanged( 1858 eq(WALLET_HOLDER_PACKAGE_NAME), eq(USER_ID)); 1859 verify(mRegisteredAidCache).onPreferredPaymentServiceChanged(eq(USER_ID), 1860 eq(WALLET_PAYMENT_SERVICE)); 1861 verify(mRegisteredServicesCache).initialize(); 1862 verify(mNfcService, times(2)) 1863 .onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_CHANGED)); 1864 } 1865 1866 @Test testOnPreferredPaymentServiceChanged_observeModeDisabled()1867 public void testOnPreferredPaymentServiceChanged_observeModeDisabled() { 1868 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), any())) 1869 .thenReturn(true); 1870 when(mRegisteredAidCache.getPreferredService()) 1871 .thenReturn(new Pair<>(USER_ID, WALLET_PAYMENT_SERVICE)); 1872 when(android.nfc.Flags.nfcObserveMode()).thenReturn(false); 1873 1874 mCardEmulationManager.onPreferredPaymentServiceChanged(USER_ID, WALLET_PAYMENT_SERVICE); 1875 1876 verify(mHostEmulationManager).onPreferredPaymentServiceChanged(eq(USER_ID), 1877 eq(WALLET_PAYMENT_SERVICE)); 1878 verify(mRegisteredAidCache).onWalletRoleHolderChanged( 1879 eq(WALLET_HOLDER_PACKAGE_NAME), eq(USER_ID)); 1880 verify(mRegisteredAidCache).onPreferredPaymentServiceChanged(eq(USER_ID), 1881 eq(WALLET_PAYMENT_SERVICE)); 1882 verify(mRegisteredServicesCache).initialize(); 1883 verify(mNfcService).onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_CHANGED)); 1884 assertUpdateForShouldDefaultToObserveMode(false); 1885 } 1886 1887 @Test testOnPreferredForegroundServiceChanged_observeModeEnabled()1888 public void testOnPreferredForegroundServiceChanged_observeModeEnabled() { 1889 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), any())) 1890 .thenReturn(true); 1891 when(mRegisteredAidCache.getPreferredService()) 1892 .thenReturn(new Pair<>(USER_ID, WALLET_PAYMENT_SERVICE)); 1893 when(android.nfc.Flags.nfcObserveMode()).thenReturn(true); 1894 1895 mCardEmulationManager.onPreferredForegroundServiceChanged(USER_ID, WALLET_PAYMENT_SERVICE); 1896 1897 verify(mRegisteredAidCache).onWalletRoleHolderChanged( 1898 eq(WALLET_HOLDER_PACKAGE_NAME), eq(USER_ID)); 1899 verify(mHostEmulationManager).onPreferredForegroundServiceChanged(eq(USER_ID), 1900 eq(WALLET_PAYMENT_SERVICE)); 1901 verify(mRegisteredAidCache).onPreferredForegroundServiceChanged(eq(USER_ID), 1902 eq(WALLET_PAYMENT_SERVICE)); 1903 verify(mRegisteredServicesCache).initialize(); 1904 verify(mNfcService).onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_CHANGED)); 1905 } 1906 1907 @Test testOnPreferredForegroundServiceChanged_observeModeDisabled()1908 public void testOnPreferredForegroundServiceChanged_observeModeDisabled() { 1909 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), any())) 1910 .thenReturn(true); 1911 when(mRegisteredAidCache.getPreferredService()) 1912 .thenReturn(new Pair<>(USER_ID, WALLET_PAYMENT_SERVICE)); 1913 when(android.nfc.Flags.nfcObserveMode()).thenReturn(false); 1914 1915 mCardEmulationManager.onPreferredForegroundServiceChanged(USER_ID, WALLET_PAYMENT_SERVICE); 1916 1917 verify(mHostEmulationManager).onPreferredForegroundServiceChanged(eq(USER_ID), 1918 eq(WALLET_PAYMENT_SERVICE)); 1919 verify(mRegisteredAidCache).onWalletRoleHolderChanged( 1920 eq(WALLET_HOLDER_PACKAGE_NAME), eq(USER_ID)); 1921 verify(mRegisteredAidCache).onPreferredForegroundServiceChanged(eq(USER_ID), 1922 eq(WALLET_PAYMENT_SERVICE)); 1923 verify(mRegisteredServicesCache).initialize(); 1924 verify(mNfcService).onPreferredPaymentChanged(eq(NfcAdapter.PREFERRED_PAYMENT_CHANGED)); 1925 assertUpdateForShouldDefaultToObserveMode(false); 1926 } 1927 1928 @Test testOnPreferredPaymentServiceChanged_toNull_dontUpdateObserveMode()1929 public void testOnPreferredPaymentServiceChanged_toNull_dontUpdateObserveMode() { 1930 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), any())) 1931 .thenReturn(true); 1932 when(mRegisteredAidCache.getPreferredService()) 1933 .thenReturn(new Pair<>(USER_ID, WALLET_PAYMENT_SERVICE)); 1934 when(android.nfc.Flags.nfcObserveMode()).thenReturn(true); 1935 1936 mCardEmulationManager.onPreferredPaymentServiceChanged(USER_ID, null); 1937 1938 verify(mRegisteredServicesCache).initialize(); 1939 assertUpdateForShouldDefaultToObserveMode(false); 1940 } 1941 1942 @Test testOnPreferredForegroundServiceChanged_toNull_dontUpdateObserveMode()1943 public void testOnPreferredForegroundServiceChanged_toNull_dontUpdateObserveMode() { 1944 when(mRegisteredServicesCache.doesServiceShouldDefaultToObserveMode(anyInt(), any())) 1945 .thenReturn(true); 1946 when(mRegisteredAidCache.getPreferredService()) 1947 .thenReturn(new Pair<>(USER_ID, WALLET_PAYMENT_SERVICE)); 1948 when(android.nfc.Flags.nfcObserveMode()).thenReturn(true); 1949 1950 mCardEmulationManager.onPreferredForegroundServiceChanged(USER_ID, null); 1951 1952 verify(mRegisteredServicesCache).initialize(); 1953 assertUpdateForShouldDefaultToObserveMode(false); 1954 } 1955 1956 @Test testOnWalletRoleHolderChanged()1957 public void testOnWalletRoleHolderChanged() { 1958 mCardEmulationManager.onWalletRoleHolderChanged(WALLET_HOLDER_PACKAGE_NAME, USER_ID); 1959 1960 verify(mPreferredServices, times(2)) 1961 .onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), eq(USER_ID)); 1962 verify(mRegisteredAidCache, times(2)) 1963 .onWalletRoleHolderChanged(eq(WALLET_HOLDER_PACKAGE_NAME), eq(USER_ID)); 1964 verifyNoMoreInteractions(mPreferredServices); 1965 verifyNoMoreInteractions(mRegisteredAidCache); 1966 } 1967 1968 @Test testOnEnabledForegroundNfcFServiceChanged()1969 public void testOnEnabledForegroundNfcFServiceChanged() { 1970 mCardEmulationManager.onEnabledForegroundNfcFServiceChanged(USER_ID, 1971 WALLET_PAYMENT_SERVICE); 1972 1973 verify(mRegisteredT3tIdentifiersCache).onEnabledForegroundNfcFServiceChanged(eq(USER_ID), 1974 eq(WALLET_PAYMENT_SERVICE)); 1975 verify(mHostNfcFEmulationManager) 1976 .onEnabledForegroundNfcFServiceChanged(eq(USER_ID), 1977 eq(WALLET_PAYMENT_SERVICE)); 1978 verifyNoMoreInteractions(mRegisteredT3tIdentifiersCache); 1979 verifyNoMoreInteractions(mHostNfcFEmulationManager); 1980 } 1981 1982 @Test testGetRegisteredAidCategory()1983 public void testGetRegisteredAidCategory() { 1984 RegisteredAidCache.AidResolveInfo aidResolveInfo = Mockito.mock( 1985 RegisteredAidCache.AidResolveInfo.class); 1986 when(aidResolveInfo.getCategory()).thenReturn(CardEmulation.CATEGORY_PAYMENT); 1987 1988 when(mRegisteredAidCache.resolveAid(anyString())).thenReturn(aidResolveInfo); 1989 1990 Assert.assertEquals(mCardEmulationManager.getRegisteredAidCategory(PAYMENT_AID_1), 1991 CardEmulation.CATEGORY_PAYMENT); 1992 1993 verify(mRegisteredAidCache).resolveAid(eq(PAYMENT_AID_1)); 1994 verify(aidResolveInfo).getCategory(); 1995 } 1996 1997 @Test testIsRequiresScreenOnServiceExist()1998 public void testIsRequiresScreenOnServiceExist() { 1999 when(mRegisteredAidCache.isRequiresScreenOnServiceExist()).thenReturn(true); 2000 2001 assertTrue(mCardEmulationManager.isRequiresScreenOnServiceExist()); 2002 } 2003 2004 assertUpdateForShouldDefaultToObserveMode(boolean flagEnabled)2005 private void assertUpdateForShouldDefaultToObserveMode(boolean flagEnabled) { 2006 if (flagEnabled) { 2007 ExtendedMockito.verify(() -> { 2008 NfcAdapter.getDefaultAdapter(mContext); 2009 }); 2010 verify(mRegisteredAidCache).getPreferredService(); 2011 verify(mRegisteredServicesCache).doesServiceShouldDefaultToObserveMode(eq(USER_ID), 2012 eq(WALLET_PAYMENT_SERVICE)); 2013 verify(mNfcAdapter).setObserveModeEnabled(eq(true)); 2014 } 2015 verifyNoMoreInteractions(mNfcAdapter); 2016 verifyNoMoreInteractions(mRegisteredServicesCache); 2017 } 2018 createInstanceWithMockParams()2019 private CardEmulationManager createInstanceWithMockParams() { 2020 when(mRoutingOptionManager.getOffHostRouteEse()).thenReturn(TEST_DATA_1); 2021 when(mRoutingOptionManager.getOffHostRouteUicc()).thenReturn(TEST_DATA_2); 2022 when(mWalletRoleObserver.isWalletRoleFeatureEnabled()).thenReturn(true); 2023 when(mWalletRoleObserver.getDefaultWalletRoleHolder(eq(USER_ID))) 2024 .thenReturn(WALLET_HOLDER_PACKAGE_NAME); 2025 2026 return new CardEmulationManager(mContext, mForegroundUtils, mWalletRoleObserver, 2027 mRegisteredAidCache, mRegisteredT3tIdentifiersCache, mHostEmulationManager, 2028 mHostNfcFEmulationManager, mRegisteredServicesCache, mRegisteredNfcFServicesCache, 2029 mPreferredServices, mEnabledNfcFServices, mRoutingOptionManager, mPowerManager); 2030 } 2031 } 2032