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.any;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.annotation.NonNull;
26 import android.annotation.Nullable;
27 import android.content.BroadcastReceiver;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.ContextWrapper;
31 import android.content.Intent;
32 import android.content.IntentFilter;
33 import android.content.pm.PackageManager;
34 import android.content.pm.ResolveInfo;
35 import android.content.pm.ServiceInfo;
36 import android.content.res.Resources;
37 import android.os.Handler;
38 import android.util.Log;
39 
40 import androidx.test.ext.junit.runners.AndroidJUnit4;
41 import androidx.test.platform.app.InstrumentationRegistry;
42 
43 import com.android.dx.mockito.inline.extended.ExtendedMockito;
44 
45 import org.junit.After;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mockito;
51 import org.mockito.MockitoSession;
52 import org.mockito.quality.Strictness;
53 
54 import java.util.ArrayList;
55 import java.util.List;
56 
57 
58 @RunWith(AndroidJUnit4.class)
59 public class DtaServiceConnectorTest {
60 
61     private static final String TAG = DtaServiceConnectorTest.class.getSimpleName();
62     private boolean mNfcSupported;
63     private MockitoSession mStaticMockSession;
64     private Context mockContext;
65     private DtaServiceConnector mDtaServiceConnector;
66     private Intent implicitIntent;
67 
68     @Before
setUp()69     public void setUp() throws Exception {
70         mStaticMockSession = ExtendedMockito.mockitoSession()
71                 .strictness(Strictness.LENIENT)
72                 .startMocking();
73 
74         Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
75         PackageManager pm = context.getPackageManager();
76         if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) {
77             mNfcSupported = false;
78             return;
79         }
80         mNfcSupported = true;
81 
82         Resources mockResources = Mockito.mock(Resources.class);
83         when(mockResources.getBoolean(eq(R.bool.tag_intent_app_pref_supported)))
84                 .thenReturn(false);
85 
86         implicitIntent = mock(Intent.class);
87 
88         mockContext = new ContextWrapper(context) {
89 
90             public PackageManager getPackageManager() {
91                 Log.i(TAG, "[Mock] getPackageManager");
92                 PackageManager packageManager = mock(PackageManager.class);
93                 ResolveInfo resolveInfo = mock(ResolveInfo.class);
94                 resolveInfo.serviceInfo = mock(ServiceInfo.class);
95                 resolveInfo.serviceInfo.packageName = "com.android.nfc";
96                 resolveInfo.serviceInfo.name = "Nfc";
97                 List<ResolveInfo> resolveInfos = new ArrayList<>();
98                 resolveInfos.add(resolveInfo);
99                 when(implicitIntent.getAction()).thenReturn("nfcaction");
100                 when(packageManager.queryIntentServices(implicitIntent, 0)).thenReturn(
101                         resolveInfos);
102                 return packageManager;
103             }
104 
105             @Override
106             public Resources getResources() {
107                 Log.i(TAG, "[Mock] getResources");
108                 return mockResources;
109             }
110 
111             @Override
112             public Intent registerReceiverForAllUsers(@Nullable BroadcastReceiver receiver,
113                     @NonNull IntentFilter filter, @Nullable String broadcastPermission,
114                     @Nullable Handler scheduler) {
115                 Log.i(TAG, "[Mock] getIntent");
116                 return Mockito.mock(Intent.class);
117             }
118         };
119 
120         InstrumentationRegistry.getInstrumentation().runOnMainSync(
121                 () -> mDtaServiceConnector = new DtaServiceConnector(mockContext));
122         Assert.assertNotNull(mDtaServiceConnector);
123     }
124 
125     @After
tearDown()126     public void tearDown() throws Exception {
127         mStaticMockSession.finishMocking();
128     }
129 
130     @Test
testCreateExplicitFromImplicitIntent()131     public void testCreateExplicitFromImplicitIntent() {
132         if (!mNfcSupported) return;
133 
134         Intent intent = DtaServiceConnector.createExplicitFromImplicitIntent(mockContext,
135                 implicitIntent);
136         Assert.assertNotNull(intent);
137         ComponentName componentName = intent.getComponent();
138         Assert.assertNotNull(componentName);
139         Assert.assertEquals("com.android.nfc", componentName.getPackageName());
140     }
141 }