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; 18 19 import static org.mockito.ArgumentMatchers.isA; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.when; 22 23 import android.app.ActivityManager; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.ContextWrapper; 27 import android.content.pm.PackageManager; 28 import android.nfc.cardemulation.NfcFServiceInfo; 29 import android.os.UserHandle; 30 import android.util.Log; 31 32 import androidx.test.ext.junit.runners.AndroidJUnit4; 33 import androidx.test.platform.app.InstrumentationRegistry; 34 35 import com.android.dx.mockito.inline.extended.ExtendedMockito; 36 import com.android.nfc.cardemulation.AidRoutingManager; 37 import com.android.nfc.cardemulation.EnabledNfcFServices; 38 import com.android.nfc.cardemulation.RegisteredNfcFServicesCache; 39 import com.android.nfc.cardemulation.RegisteredT3tIdentifiersCache; 40 import com.android.nfc.cardemulation.RoutingOptionManager; 41 42 import org.junit.After; 43 import org.junit.Assert; 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.MockitoSession; 48 import org.mockito.quality.Strictness; 49 50 @RunWith(AndroidJUnit4.class) 51 public class EnableNfcFServiceTest { 52 53 private static final String TAG = EnableNfcFServiceTest.class.getSimpleName(); 54 private boolean mNfcSupported; 55 private MockitoSession mStaticMockSession; 56 private ComponentName mComponentName; 57 private NfcFServiceInfo mNfcFServiceInfo; 58 private EnabledNfcFServices mEnabledNfcFServices; 59 private ForegroundUtils mForegroundUtils; 60 61 @Before setUp()62 public void setUp() throws Exception { 63 mStaticMockSession = ExtendedMockito.mockitoSession() 64 .mockStatic(RoutingOptionManager.class) 65 .mockStatic(NfcService.class) 66 .mockStatic(NfcStatsLog.class) 67 .mockStatic(UserHandle.class) 68 .mockStatic(ForegroundUtils.class) 69 .strictness(Strictness.LENIENT) 70 .startMocking(); 71 72 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 73 PackageManager pm = context.getPackageManager(); 74 if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) { 75 mNfcSupported = false; 76 return; 77 } 78 mNfcSupported = true; 79 80 Context mockContext = new ContextWrapper(context) { 81 82 }; 83 84 mForegroundUtils = mock(ForegroundUtils.class); 85 when(ForegroundUtils.getInstance( 86 mockContext.getSystemService(ActivityManager.class))).thenReturn(mForegroundUtils); 87 RegisteredNfcFServicesCache registeredNfcFServicesCache = mock( 88 RegisteredNfcFServicesCache.class); 89 mComponentName = mock(ComponentName.class); 90 mNfcFServiceInfo = mock(NfcFServiceInfo.class); 91 when(registeredNfcFServicesCache.getService(1, mComponentName)).thenReturn( 92 mNfcFServiceInfo); 93 RegisteredT3tIdentifiersCache registeredT3tIdentifiersCache = mock( 94 RegisteredT3tIdentifiersCache.class); 95 96 RoutingOptionManager routingOptionManager = mock(RoutingOptionManager.class); 97 when(RoutingOptionManager.getInstance()).thenReturn(routingOptionManager); 98 InstrumentationRegistry.getInstrumentation().runOnMainSync( 99 () -> mEnabledNfcFServices = new EnabledNfcFServices(mockContext, 100 registeredNfcFServicesCache, registeredT3tIdentifiersCache, 101 (userId, service) -> { 102 Log.d(TAG, "CallBack is Received for userid " + userId); 103 104 })); 105 Assert.assertNotNull(mEnabledNfcFServices); 106 } 107 108 @After tearDown()109 public void tearDown() throws Exception { 110 mStaticMockSession.finishMocking(); 111 } 112 113 @Test testOnHostEmulationActivated()114 public void testOnHostEmulationActivated() { 115 if (!mNfcSupported) return; 116 117 boolean isActivated = mEnabledNfcFServices.isActivated(); 118 Assert.assertFalse(isActivated); 119 mEnabledNfcFServices.onHostEmulationActivated(); 120 isActivated = mEnabledNfcFServices.isActivated(); 121 Assert.assertTrue(isActivated); 122 } 123 124 @Test testOnHostEmulationDeactivated()125 public void testOnHostEmulationDeactivated() { 126 if (!mNfcSupported) return; 127 128 mEnabledNfcFServices.onHostEmulationActivated(); 129 boolean isActivated = mEnabledNfcFServices.isActivated(); 130 Assert.assertTrue(isActivated); 131 mEnabledNfcFServices.onHostEmulationDeactivated(); 132 isActivated = mEnabledNfcFServices.isActivated(); 133 Assert.assertFalse(isActivated); 134 } 135 136 @Test testRegisterEnabledForegroundService()137 public void testRegisterEnabledForegroundService() { 138 if (!mNfcSupported) return; 139 140 UserHandle userHandle = mock(UserHandle.class); 141 when(userHandle.getIdentifier()).thenReturn(1); 142 when(UserHandle.getUserHandleForUid(1)).thenReturn(userHandle); 143 when(mNfcFServiceInfo.getSystemCode()).thenReturn("Nfc"); 144 when(mNfcFServiceInfo.getNfcid2()).thenReturn("NfcId"); 145 when(mNfcFServiceInfo.getT3tPmm()).thenReturn("T3"); 146 when(mForegroundUtils.registerUidToBackgroundCallback(mEnabledNfcFServices, 1)).thenReturn( 147 true); 148 boolean isRegistered = mEnabledNfcFServices.registerEnabledForegroundService(mComponentName, 149 1); 150 Assert.assertTrue(isRegistered); 151 } 152 153 154 @Test testOnNfcDisabled()155 public void testOnNfcDisabled() { 156 if (!mNfcSupported) return; 157 158 mEnabledNfcFServices.onNfcDisabled(); 159 boolean isNfcDisabled = mEnabledNfcFServices.isNfcDisabled(); 160 Assert.assertTrue(isNfcDisabled); 161 } 162 163 @Test testOnUserSwitched()164 public void testOnUserSwitched() { 165 if (!mNfcSupported) return; 166 167 mEnabledNfcFServices.onUserSwitched(0); 168 boolean isUserSwitched = mEnabledNfcFServices.isUserSwitched(); 169 Assert.assertTrue(isUserSwitched); 170 171 } 172 } 173