blob: 5453adb6447b6068dd78cd18b4df30ef04bc4a9a [file] [log] [blame]
package android.nfc.cts;
import static org.junit.Assume.assumeTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doNothing;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.nfc.*;
import android.nfc.tech.*;
import android.os.Bundle;
import android.os.RemoteException;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.InstrumentationRegistry;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.util.reflection.FieldSetter;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.HashMap;
@RunWith(JUnit4.class)
public class NfcAdapterTest {
@Mock private INfcAdapter mService;
private Context mContext;
private boolean supportsHardware() {
final PackageManager pm = mContext.getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_NFC);
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = InstrumentationRegistry.getContext();
assumeTrue(supportsHardware());
}
@Test
public void testGetDefaultAdapter() {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
Assert.assertNotNull(adapter);
}
@Test
public void testAddNfcUnlockHandler() {
try {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
adapter.addNfcUnlockHandler(new CtsNfcUnlockHandler(), new String[]{"IsoDep"});
} catch (Exception e) {
throw new IllegalStateException("Unexpected Exception: " + e);
}
}
@Test
public void testDisableWithNoParams() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.disable(anyBoolean())).thenReturn(true);
boolean result = adapter.disable();
Assert.assertTrue(result);
}
@Test
public void testDisableWithParam() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.disable(anyBoolean())).thenReturn(true);
boolean result = adapter.disable(true);
Assert.assertTrue(result);
}
@Test
public void testDisableForegroundDispatch() {
try {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
Activity activity = createAndResumeActivity();
adapter.disableForegroundDispatch(activity);
} catch (Exception e) {
throw new IllegalStateException("Unexpected Exception: " + e);
}
}
@Test
public void testDisableReaderMode() {
try {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
Activity activity = createAndResumeActivity();
adapter.disableReaderMode(activity);
} catch (Exception e) {
throw new IllegalStateException("Unexpected Exception: " + e);
}
}
@Test
public void testEnable() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.enable()).thenReturn(true);
boolean result = adapter.enable();
Assert.assertTrue(result);
}
@Test
public void testEnableForegroundDispatch() {
try {
NfcAdapter adapter = createMockedInstance();
Activity activity = createAndResumeActivity();
Intent intent = new Intent(ApplicationProvider.getApplicationContext(),
NfcFCardEmulationActivity.class);
PendingIntent pendingIntent
= PendingIntent.getActivity(ApplicationProvider.getApplicationContext(),
0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
String[][] techLists = new String[][]{new String[]{}};
doNothing().when(mService).setForegroundDispatch(any(PendingIntent.class),
any(IntentFilter[].class), any(TechListParcel.class));
adapter.enableForegroundDispatch(activity, pendingIntent, null, techLists);
} catch (Exception e) {
throw new IllegalStateException("Unexpected Exception: " + e);
}
}
@Test
public void testEnableReaderMode() {
try {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
Activity activity = createAndResumeActivity();
adapter.enableReaderMode(activity, new CtsReaderCallback(),
NfcAdapter.FLAG_READER_NFC_A, new Bundle());
} catch (Exception e) {
throw new IllegalStateException("Unexpected Exception: " + e);
}
}
@Test
public void testEnableSecureNfc() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.setNfcSecure(anyBoolean())).thenReturn(true);
boolean result = adapter.enableSecureNfc(true);
Assert.assertTrue(result);
}
@Test
public void testGetNfcAntennaInfo() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
NfcAntennaInfo info = new NfcAntennaInfo(0, 0, false,
new ArrayList<AvailableNfcAntenna>());
when(mService.getNfcAntennaInfo()).thenReturn(info);
NfcAntennaInfo result = adapter.getNfcAntennaInfo();
Assert.assertEquals(info, result);
}
@Test
public void testIgnore() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
Tag tag = new Tag(new byte[]{0x00}, new int[]{}, new Bundle[]{}, 0, 0L, null);
when(mService.ignore(anyInt(), anyInt(), eq(null))).thenReturn(true);
boolean result = adapter.ignore(tag, 0, null, null);
Assert.assertTrue(result);
}
@Test
public void testIsControllerAlwaysOn() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.isControllerAlwaysOn()).thenReturn(true);
boolean result = adapter.isControllerAlwaysOn();
Assert.assertTrue(result);
}
@Test
public void testIsControllerAlwaysOnSupported() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.isControllerAlwaysOnSupported()).thenReturn(true);
boolean result = adapter.isControllerAlwaysOnSupported();
Assert.assertTrue(result);
}
@Test
public void testIsEnabled() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.getState()).thenReturn(NfcAdapter.STATE_ON);
boolean result = adapter.isEnabled();
Assert.assertTrue(result);
}
@Test
public void testIsSecureNfcEnabled() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.isNfcSecureEnabled()).thenReturn(true);
boolean result = adapter.isSecureNfcEnabled();
Assert.assertTrue(result);
}
@Test
public void testIsSecureNfcSupported() throws NoSuchFieldException, RemoteException {
NfcAdapter adapter = createMockedInstance();
when(mService.deviceSupportsNfcSecure()).thenReturn(true);
boolean result = adapter.isSecureNfcSupported();
Assert.assertTrue(result);
}
@Test
public void testRemoveNfcUnlockHandler() {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
boolean result = adapter.removeNfcUnlockHandler(new CtsNfcUnlockHandler());
Assert.assertTrue(result);
}
private class CtsReaderCallback implements NfcAdapter.ReaderCallback {
@Override
public void onTagDiscovered(Tag tag) {}
}
private class CtsNfcUnlockHandler implements NfcAdapter.NfcUnlockHandler {
@Override
public boolean onUnlockAttempted(Tag tag) {
return true;
}
}
private Activity createAndResumeActivity() {
Intent intent = new Intent(ApplicationProvider.getApplicationContext(),
NfcFCardEmulationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Activity activity = InstrumentationRegistry.getInstrumentation().startActivitySync(intent);
InstrumentationRegistry.getInstrumentation().callActivityOnResume(activity);
return activity;
}
private NfcAdapter createMockedInstance() throws NoSuchFieldException {
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
FieldSetter.setField(adapter, adapter.getClass().getDeclaredField("sService"), mService);
return adapter;
}
}