1 /* 2 * Copyright (C) 2021 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 com.android.nfc; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.nfc.NfcAdapter; 26 import android.os.SystemProperties; 27 import android.text.TextUtils; 28 29 import androidx.test.InstrumentationRegistry; 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 32 import com.android.internal.util.ArrayUtils; 33 34 import org.junit.After; 35 import org.junit.Assert; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 @RunWith(AndroidJUnit4.class) 41 public final class NfcFeatureFlagTest { 42 43 private static final String TAG = NfcFeatureFlagTest.class.getSimpleName(); 44 private Context mContext; 45 private NfcAdapter mNfcAdapter; 46 private boolean mNfcSupported; 47 48 @Before setUp()49 public void setUp() { 50 mContext = InstrumentationRegistry.getTargetContext(); 51 PackageManager pm = mContext.getPackageManager(); 52 if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) { 53 mNfcSupported = false; 54 return; 55 } 56 mNfcSupported = true; 57 mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext); 58 Assert.assertNotNull(mNfcAdapter); 59 } 60 61 @After tearDown()62 public void tearDown() throws Exception { 63 } 64 65 @Test testIsSecureNfcSupported()66 public void testIsSecureNfcSupported() { 67 if (!mNfcSupported) return; 68 String[] skuList = mContext.getResources().getStringArray( 69 R.array.config_skuSupportsSecureNfc); 70 String sku = SystemProperties.get("ro.boot.hardware.sku"); 71 if (TextUtils.isEmpty(sku) || !ArrayUtils.contains(skuList, sku)) { 72 assertFalse(mNfcAdapter.isSecureNfcSupported()); 73 } else { 74 assertTrue(mNfcAdapter.isSecureNfcSupported()); 75 } 76 } 77 78 @Test testIsControllerAlwaysOnSupported()79 public void testIsControllerAlwaysOnSupported() { 80 if (!mNfcSupported) return; 81 assertThat(mContext.getResources() 82 .getBoolean(R.bool.nfcc_always_on_allowed)) 83 .isEqualTo(mNfcAdapter.isControllerAlwaysOnSupported()); 84 } 85 } 86