• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.pm.PackageManager;
25 import android.nfc.INfcUnlockHandler;
26 import android.nfc.Tag;
27 import android.os.IBinder;
28 import android.os.RemoteException;
29 
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.MockitoAnnotations;
35 import org.mockito.MockitoSession;
36 import org.mockito.quality.Strictness;
37 
38 import androidx.test.ext.junit.runners.AndroidJUnit4;
39 
40 import com.android.dx.mockito.inline.extended.ExtendedMockito;
41 
42 @RunWith(AndroidJUnit4.class)
43 public class NfcUnlockManagerTest {
44     private MockitoSession mStaticMockSession;
45     private NfcUnlockManager mNfcUnlockManager;
46 
47 
48     @Before
setUp()49     public void setUp() throws PackageManager.NameNotFoundException {
50         mStaticMockSession = ExtendedMockito.mockitoSession()
51                 .strictness(Strictness.LENIENT)
52                 .startMocking();
53 
54         MockitoAnnotations.initMocks(this);
55         mNfcUnlockManager = NfcUnlockManager.getInstance();
56     }
57 
58     @After
tearDown()59     public void tearDown() {
60         mStaticMockSession.finishMocking();
61     }
62 
63     @Test
testAddUnlockHandler()64     public void testAddUnlockHandler() {
65         INfcUnlockHandler unlockHandler = mock(INfcUnlockHandler.class);
66         IBinder iBinder = mock(IBinder.class);
67         when(unlockHandler.asBinder()).thenReturn(iBinder);
68         int result = mNfcUnlockManager.addUnlockHandler(unlockHandler, 1);
69         assertThat(result).isEqualTo(1);
70     }
71 
72     @Test
testRemoveUnlockHandler()73     public void testRemoveUnlockHandler() {
74         INfcUnlockHandler unlockHandler = mock(INfcUnlockHandler.class);
75         IBinder iBinder = mock(IBinder.class);
76         when(unlockHandler.asBinder()).thenReturn(iBinder);
77         int result = mNfcUnlockManager.addUnlockHandler(unlockHandler, 1);
78         assertThat(result).isEqualTo(1);
79 
80         int pollMask = mNfcUnlockManager.getLockscreenPollMask();
81         assertThat(pollMask).isEqualTo(result);
82         result = mNfcUnlockManager.removeUnlockHandler(iBinder);
83         assertThat(pollMask).isEqualTo(1);
84     }
85 
86     @Test
testTryUnlock()87     public void testTryUnlock() throws RemoteException {
88         INfcUnlockHandler unlockHandler = mock(INfcUnlockHandler.class);
89         IBinder iBinder = mock(IBinder.class);
90         when(unlockHandler.asBinder()).thenReturn(iBinder);
91         int result = mNfcUnlockManager.addUnlockHandler(unlockHandler, 1);
92         assertThat(result).isEqualTo(1);
93 
94         Tag tag = mock(Tag.class);
95         when(unlockHandler.onUnlockAttempted(tag)).thenReturn(true);
96         boolean status = mNfcUnlockManager.tryUnlock(tag);
97         assertThat(status).isTrue();
98     }
99 
100     @Test
testIsLockScreenPollingEnabled()101     public void testIsLockScreenPollingEnabled() {
102         INfcUnlockHandler unlockHandler = mock(INfcUnlockHandler.class);
103         IBinder iBinder = mock(IBinder.class);
104         when(unlockHandler.asBinder()).thenReturn(iBinder);
105         int result = mNfcUnlockManager.addUnlockHandler(unlockHandler, 1);
106         assertThat(result).isEqualTo(1);
107 
108         boolean lockScreenPollMask = mNfcUnlockManager.isLockscreenPollingEnabled();
109         assertThat(lockScreenPollMask).isTrue();
110     }
111 
112     @Test
testGetLockScreenPollMask()113     public void testGetLockScreenPollMask() {
114         INfcUnlockHandler unlockHandler = mock(INfcUnlockHandler.class);
115         IBinder iBinder = mock(IBinder.class);
116         when(unlockHandler.asBinder()).thenReturn(iBinder);
117         int result = mNfcUnlockManager.addUnlockHandler(unlockHandler, 1);
118         assertThat(result).isEqualTo(1);
119 
120         int status = mNfcUnlockManager.getLockscreenPollMask();
121         assertThat(status).isEqualTo(result);
122     }
123 }
124