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 android.Manifest.permission.BIND_NFC_SERVICE; 20 import static android.Manifest.permission.NFC; 21 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.PackageInfo; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ServiceInfo; 27 import android.net.Uri; 28 import android.os.UserHandle; 29 import android.text.TextUtils; 30 import android.util.Log; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 37 import java.util.Arrays; 38 39 import static org.junit.Assert.assertFalse; 40 import static org.junit.Assert.assertTrue; 41 import static org.mockito.ArgumentMatchers.any; 42 import static org.mockito.ArgumentMatchers.anyInt; 43 import static org.mockito.ArgumentMatchers.anyString; 44 import static org.mockito.Mockito.doReturn; 45 import static org.mockito.Mockito.mock; 46 47 public class UtilsTest { 48 @Mock 49 Context context; 50 51 @Mock 52 PackageManager packageManager; 53 54 @Mock 55 PackageInfo packageInfo; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 } 61 62 @Test testGetPackageNameFromIntent()63 public void testGetPackageNameFromIntent() { 64 // Prepare data for the Intent 65 Intent intent = mock(Intent.class); 66 Uri uri = mock(Uri.class); 67 doReturn("package").when(uri).getScheme(); 68 doReturn("com.example.app").when(uri).getSchemeSpecificPart(); 69 doReturn(uri).when(intent).getData(); 70 71 // Call the method and check the result 72 String packageName = Utils.getPackageNameFromIntent(intent); 73 assertTrue(packageName.equals("com.example.app")); 74 } 75 76 @Test testHasCeServicesWithValidPermissions()77 public void testHasCeServicesWithValidPermissions() throws PackageManager.NameNotFoundException { 78 // Prepare data for the test 79 Intent intent = mock(Intent.class); 80 Uri uri = mock(Uri.class); 81 doReturn("package").when(uri).getScheme(); 82 doReturn("com.example.app").when(uri).getSchemeSpecificPart(); 83 doReturn(uri).when(intent).getData(); 84 85 // Mock PackageManager behavior 86 doReturn(context).when(context).createPackageContextAsUser( 87 anyString(), anyInt(), any(UserHandle.class)); 88 doReturn(packageManager).when(context).getPackageManager(); 89 doReturn(PackageManager.PERMISSION_GRANTED).when(packageManager) 90 .checkPermission(NFC, "com.example.app"); 91 doReturn(packageInfo).when(packageManager).getPackageInfo("com.example.app", 92 PackageManager.GET_PERMISSIONS 93 | PackageManager.GET_SERVICES 94 | PackageManager.MATCH_DISABLED_COMPONENTS); 95 ServiceInfo serviceInfo = new ServiceInfo(); 96 serviceInfo.permission = BIND_NFC_SERVICE; 97 packageInfo.services = new ServiceInfo[]{serviceInfo}; 98 99 // Call the method and check the result 100 boolean result = Utils.hasCeServicesWithValidPermissions(context, intent, 123); 101 assertTrue(result); 102 } 103 } 104 105