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.Mockito.mock;
20 
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.ContextWrapper;
24 import android.content.pm.PackageManager;
25 
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoSession;
33 import org.mockito.quality.Strictness;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 import androidx.test.platform.app.InstrumentationRegistry;
37 
38 import com.android.dx.mockito.inline.extended.ExtendedMockito;
39 import com.android.nfc.cardemulation.AidRoutingManager;
40 import com.android.nfc.cardemulation.RegisteredAidCache;
41 import com.android.nfc.cardemulation.WalletRoleObserver;
42 
43 @RunWith(AndroidJUnit4.class)
44 public class RegisteredAidCacheTest {
45 
46     private static final String TAG = RegisteredAidCacheTest.class.getSimpleName();
47     private boolean mNfcSupported;
48     private MockitoSession mStaticMockSession;
49     private RegisteredAidCache mRegisteredAidCache;
50     private Context mockContext;
51     @Mock
52     private WalletRoleObserver mWalletRoleObserver;
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         mStaticMockSession = ExtendedMockito.mockitoSession()
57                 .strictness(Strictness.LENIENT)
58                 .startMocking();
59 
60         Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
61         PackageManager pm = context.getPackageManager();
62         if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) {
63             mNfcSupported = false;
64             return;
65         }
66         mNfcSupported = true;
67 
68         mockContext = new ContextWrapper(context) {
69 
70         };
71 
72         AidRoutingManager routingManager = mock(AidRoutingManager.class);
73         InstrumentationRegistry.getInstrumentation().runOnMainSync(
74                 () -> mRegisteredAidCache = new RegisteredAidCache(
75                         mockContext, mWalletRoleObserver, routingManager));
76         Assert.assertNotNull(mRegisteredAidCache);
77     }
78 
79     @After
tearDown()80     public void tearDown() throws Exception {
81         mStaticMockSession.finishMocking();
82     }
83 
84 
85     @Test
testOnPreferredForegroundServiceChanged()86     public void testOnPreferredForegroundServiceChanged() {
87         if (!mNfcSupported) return;
88 
89         ComponentName componentName = mRegisteredAidCache.getPreferredService().second;
90         Assert.assertNull(componentName);
91 
92         componentName = new ComponentName("com.android.nfc",
93                 RegisteredAidCacheTest.class.getName());
94         mRegisteredAidCache.onPreferredForegroundServiceChanged(0, componentName);
95         ComponentName preferredService = mRegisteredAidCache.getPreferredService().second;
96 
97         Assert.assertNotNull(preferredService);
98         Assert.assertEquals(componentName.getClassName(), preferredService.getClassName());
99     }
100 
101 }