1 /* 2 * Copyright (C) 2022 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 package android.nfc.test; 17 18 import static android.content.pm.PackageManager.MATCH_ALL; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.junit.Assert.assertFalse; 23 import static org.junit.Assert.assertTrue; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.content.pm.PackageManager.PackageInfoFlags; 28 import android.content.res.Resources; 29 import android.nfc.NfcAdapter; 30 import android.os.SystemProperties; 31 import android.text.TextUtils; 32 import android.util.Log; 33 34 import androidx.test.InstrumentationRegistry; 35 import androidx.test.ext.junit.runners.AndroidJUnit4; 36 37 import com.android.internal.util.ArrayUtils; 38 39 import org.junit.After; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 45 @RunWith(AndroidJUnit4.class) 46 public final class NfcFeatureFlagTest { 47 48 private static final String TAG = NfcFeatureFlagTest.class.getSimpleName(); 49 private Context mContext; 50 private NfcAdapter mNfcAdapter; 51 private boolean mNfcSupported; 52 53 @Before setUp()54 public void setUp() { 55 mContext = InstrumentationRegistry.getTargetContext(); 56 PackageManager pm = mContext.getPackageManager(); 57 if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) { 58 mNfcSupported = false; 59 return; 60 } 61 mNfcSupported = true; 62 mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext); 63 Assert.assertNotNull(mNfcAdapter); 64 } 65 66 @After tearDown()67 public void tearDown() throws Exception { 68 } 69 getNfcApkPkgName()70 private String getNfcApkPkgName() throws Exception { 71 // Check if "com.google.android.nfc" or "com.android.nfc" is installed on the device. 72 return mContext.getPackageManager() 73 .getInstalledPackages(PackageInfoFlags.of(MATCH_ALL)) 74 .stream() 75 .filter(p -> p.packageName.equals("com.google.android.nfc")) 76 .findFirst() 77 .map(p -> p.packageName) 78 .orElse("com.android.nfc"); 79 } 80 getNfcResourceBooleanByName(String name)81 private boolean getNfcResourceBooleanByName(String name) throws Exception { 82 Resources resources = mContext.getPackageManager().getResourcesForApplication( 83 getNfcApkPkgName()); 84 int arrayId = resources.getIdentifier(name, "bool", getNfcApkPkgName()); 85 if (arrayId == 0) { 86 throw new Resources.NotFoundException("Resource array '" + name 87 + "' not found in package " + getNfcApkPkgName()); 88 } 89 return resources.getBoolean(arrayId); 90 } 91 getNfcResourceStringArrayByName(String name)92 private String[] getNfcResourceStringArrayByName(String name) throws Exception { 93 Resources resources = mContext.getPackageManager().getResourcesForApplication( 94 getNfcApkPkgName()); 95 int arrayId = resources.getIdentifier(name, "array", getNfcApkPkgName()); 96 if (arrayId == 0) { 97 throw new Resources.NotFoundException("Resource array '" + name 98 + "' not found in package " + getNfcApkPkgName()); 99 } 100 return resources.getStringArray(arrayId); 101 } 102 103 @Test testIsSecureNfcSupported()104 public void testIsSecureNfcSupported() throws Exception { 105 if (!mNfcSupported) return; 106 boolean allSupport = getNfcResourceBooleanByName("enable_secure_nfc_support"); 107 if (allSupport) { 108 assertTrue(mNfcAdapter.isSecureNfcSupported()); 109 return; 110 } 111 String[] skuList = getNfcResourceStringArrayByName("config_skuSupportsSecureNfc"); 112 String sku = SystemProperties.get("ro.boot.hardware.sku"); 113 if (TextUtils.isEmpty(sku) || !ArrayUtils.contains(skuList, sku)) { 114 assertFalse(mNfcAdapter.isSecureNfcSupported()); 115 } else { 116 assertTrue(mNfcAdapter.isSecureNfcSupported()); 117 } 118 } 119 120 @Test testIsControllerAlwaysOnSupported()121 public void testIsControllerAlwaysOnSupported() throws Exception { 122 if (!mNfcSupported) return; 123 InstrumentationRegistry.getInstrumentation().getUiAutomation() 124 .adoptShellPermissionIdentity(); 125 assertThat(getNfcResourceBooleanByName("nfcc_always_on_allowed")) 126 .isEqualTo(mNfcAdapter.isControllerAlwaysOnSupported()); 127 InstrumentationRegistry.getInstrumentation().getUiAutomation() 128 .dropShellPermissionIdentity(); 129 } 130 131 @Test testIsTagIntentAppPreferenceSupported()132 public void testIsTagIntentAppPreferenceSupported() throws Exception { 133 if (!mNfcSupported) return; 134 assertThat(getNfcResourceBooleanByName("tag_intent_app_pref_supported")) 135 .isEqualTo(mNfcAdapter.isTagIntentAppPreferenceSupported()); 136 } 137 } 138