1 /* 2 * Copyright (C) 2025 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 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.app.AlarmManager; 24 import android.app.PendingIntent; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.nfc.Flags; 28 29 import com.android.dx.mockito.inline.extended.ExtendedMockito; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.mockito.MockitoSession; 37 import org.mockito.internal.util.reflection.FieldSetter; 38 import org.mockito.quality.Strictness; 39 40 import java.util.concurrent.CountDownLatch; 41 42 public class NfcWatchdogTest { 43 @Mock 44 private Context mContext; 45 private NfcWatchdog mNfcWatchdog; 46 private MockitoSession mStaticMockSession; 47 48 @Before setUp()49 public void setUp() { 50 mStaticMockSession = ExtendedMockito.mockitoSession() 51 .mockStatic(Flags.class) 52 .mockStatic(PendingIntent.class) 53 .strictness(Strictness.LENIENT) 54 .startMocking(); 55 MockitoAnnotations.initMocks(this); 56 when(android.nfc.Flags.nfcWatchdog()).thenReturn(true); 57 58 mNfcWatchdog = new NfcWatchdog(mContext); 59 } 60 61 @After tearDown()62 public void tearDown() { 63 mStaticMockSession.finishMocking(); 64 } 65 66 @Test testNotifyHasReturned()67 public void testNotifyHasReturned() { 68 CountDownLatch mCountDownLatch = mock(CountDownLatch.class); 69 try { 70 FieldSetter.setField(mNfcWatchdog, 71 mNfcWatchdog.getClass().getDeclaredField("mCountDownLatch"), 72 mCountDownLatch); 73 74 } catch (NoSuchFieldException nsfe) { 75 throw new RuntimeException(nsfe); 76 } 77 78 mNfcWatchdog.notifyHasReturned(); 79 verify(mCountDownLatch).countDown(); 80 } 81 82 @Test testStopMonitoring()83 public void testStopMonitoring() { 84 AlarmManager alarmManager = mock(AlarmManager.class); 85 when(mContext.getSystemService(AlarmManager.class)).thenReturn(alarmManager); 86 87 mNfcWatchdog.stopMonitoring(); 88 verify(alarmManager).cancelAll(); 89 } 90 91 @Test testEnsureWatchdogMonitoring()92 public void testEnsureWatchdogMonitoring() { 93 AlarmManager alarmManager = mock(AlarmManager.class); 94 AlarmManager.AlarmClockInfo alarmClockInfo = mock(AlarmManager.AlarmClockInfo.class); 95 PendingIntent pendingIntent = mock(PendingIntent.class); 96 when(android.nfc.Flags.nfcWatchdog()).thenReturn(true); 97 when(mContext.getSystemService(AlarmManager.class)).thenReturn(alarmManager); 98 when(alarmManager.getNextAlarmClock()).thenReturn(alarmClockInfo); 99 when(PendingIntent.getBroadcast(mContext, 0, mock(Intent.class), 100 PendingIntent.FLAG_IMMUTABLE)).thenReturn(pendingIntent); 101 102 mNfcWatchdog.ensureWatchdogMonitoring(); 103 verify(alarmManager).getNextAlarmClock(); 104 } 105 } 106