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.eq; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.when; 22 23 import android.annotation.NonNull; 24 import android.annotation.Nullable; 25 import android.app.ActivityManager; 26 import android.content.BroadcastReceiver; 27 import android.content.Context; 28 import android.content.ContextWrapper; 29 import android.content.Intent; 30 import android.content.IntentFilter; 31 import android.content.pm.ActivityInfo; 32 import android.content.pm.PackageManager; 33 import android.content.pm.ResolveInfo; 34 import android.content.res.Resources; 35 import android.content.res.XmlResourceParser; 36 import android.nfc.NfcAdapter; 37 import android.os.Handler; 38 import android.os.PowerManager; 39 import android.os.UserHandle; 40 import android.util.Log; 41 42 import org.junit.Assert; 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mockito; 47 48 import com.android.nfc.flags.Flags; 49 50 import org.mockito.ArgumentMatchers; 51 import org.xmlpull.v1.XmlPullParser; 52 import org.xmlpull.v1.XmlPullParserException; 53 54 import androidx.test.ext.junit.runners.AndroidJUnit4; 55 import androidx.test.platform.app.InstrumentationRegistry; 56 57 import java.io.IOException; 58 import java.util.ArrayList; 59 import java.util.List; 60 61 @RunWith(AndroidJUnit4.class) 62 public final class RegisteredComponentCacheTest { 63 64 private RegisteredComponentCache mRegisteredComponentCache; 65 private boolean mNfcSupported; 66 private Context mockContext; 67 private static final String TAG = RegisteredComponentCacheTest.class.getSimpleName(); 68 69 @Before setUp()70 public void setUp() { 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 PowerManager mockPowerManager = mock(PowerManager.class); 81 when(mockPowerManager.isInteractive()).thenReturn(false); 82 Resources mockResources = mock(Resources.class); 83 when(mockResources.getBoolean(eq(R.bool.tag_intent_app_pref_supported))) 84 .thenReturn(false); 85 86 mockContext = new ContextWrapper(context) { 87 @Override 88 public Object getSystemService(String name) { 89 if (Context.POWER_SERVICE.equals(name)) { 90 Log.i(TAG, "[Mock] mockPowerManager"); 91 return mockPowerManager; 92 } 93 return super.getSystemService(name); 94 } 95 96 @Override 97 public Resources getResources() { 98 Log.i(TAG, "[Mock] getResources"); 99 return mockResources; 100 } 101 102 @Override 103 public Intent registerReceiverForAllUsers(@Nullable BroadcastReceiver receiver, 104 @NonNull IntentFilter filter, @Nullable String broadcastPermission, 105 @Nullable Handler scheduler) { 106 Log.i(TAG, "[Mock] getIntent"); 107 return mock(Intent.class); 108 } 109 110 @NonNull 111 public Context createPackageContextAsUser( 112 @NonNull String packageName, @CreatePackageOptions int flags, 113 @NonNull UserHandle user) { 114 Log.i(TAG, "[Mock] createPackageContextAsUser"); 115 Context mockedContext = mock(Context.class); 116 PackageManager packageManager = mock(PackageManager.class); 117 when(mockedContext.getPackageManager()).thenReturn(packageManager); 118 ResolveInfo resolveInfo = mock(ResolveInfo.class); 119 ActivityInfo mockActivityInfo = mock(ActivityInfo.class); 120 mockActivityInfo.packageName = "com.android.nfc"; 121 mockActivityInfo.name = "NfcRootActivity"; 122 XmlResourceParser parser = mock(XmlResourceParser.class); 123 try { 124 when(parser.getEventType()).thenReturn(XmlPullParser.START_TAG); 125 when(parser.next()).thenReturn(XmlPullParser.START_TAG, XmlPullParser.END_TAG, 126 XmlPullParser.END_DOCUMENT); 127 when(parser.getName()).thenReturn("tech", "tech-list"); 128 } catch (XmlPullParserException | IOException ignored) { 129 } 130 when(mockActivityInfo.loadXmlMetaData(packageManager, 131 NfcAdapter.ACTION_TECH_DISCOVERED)).thenReturn(parser); 132 resolveInfo.activityInfo = mockActivityInfo; 133 List<ResolveInfo> resolveInfoList = new ArrayList<ResolveInfo>(); 134 resolveInfoList.add(resolveInfo); 135 when(packageManager.queryIntentActivitiesAsUser(Mockito.any(Intent.class), 136 Mockito.any(PackageManager.ResolveInfoFlags.class), 137 Mockito.any(UserHandle.class))).thenReturn(resolveInfoList); 138 139 return mockedContext; 140 } 141 }; 142 143 InstrumentationRegistry.getInstrumentation().runOnMainSync( 144 () -> mRegisteredComponentCache = new RegisteredComponentCache(mockContext, 145 NfcAdapter.ACTION_TECH_DISCOVERED, NfcAdapter.ACTION_TECH_DISCOVERED)); 146 Assert.assertNotNull(mRegisteredComponentCache); 147 } 148 149 @Test testGetComponents()150 public void testGetComponents() { 151 if (!mNfcSupported) return; 152 153 ArrayList<RegisteredComponentCache.ComponentInfo> componentInfos = 154 mRegisteredComponentCache.getComponents(); 155 Assert.assertNotNull(componentInfos); 156 Assert.assertTrue(componentInfos.size() > 0); 157 } 158 }