|
| 1 | +package com.d4rk.androidtutorials.java.notifications.managers; |
| 2 | + |
| 3 | +import android.app.AlarmManager; |
| 4 | +import android.app.PendingIntent; |
| 5 | +import android.content.ComponentName; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.Intent; |
| 8 | + |
| 9 | +import com.d4rk.androidtutorials.java.notifications.receivers.AppUsageNotificationReceiver; |
| 10 | + |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.mockito.ArgumentCaptor; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.MockedStatic; |
| 17 | +import org.mockito.MockitoAnnotations; |
| 18 | + |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.ArgumentMatchers.argThat; |
| 24 | +import static org.mockito.ArgumentMatchers.eq; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.mockStatic; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +public class AppUsageNotificationsManagerTest { |
| 31 | + |
| 32 | + private static final String PACKAGE_NAME = "com.d4rk.androidtutorials.java"; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private Context context; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private AlarmManager alarmManager; |
| 39 | + |
| 40 | + private AutoCloseable closeable; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() { |
| 44 | + closeable = MockitoAnnotations.openMocks(this); |
| 45 | + when(context.getSystemService(Context.ALARM_SERVICE)).thenReturn(alarmManager); |
| 46 | + when(context.getPackageName()).thenReturn(PACKAGE_NAME); |
| 47 | + } |
| 48 | + |
| 49 | + @After |
| 50 | + public void tearDown() throws Exception { |
| 51 | + closeable.close(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void scheduleAppUsageCheck_setsRepeatingAlarmEveryThreeDaysWithProperPendingIntent() { |
| 56 | + PendingIntent pendingIntent = mock(PendingIntent.class); |
| 57 | + |
| 58 | + try (MockedStatic<PendingIntent> pendingIntentStatic = mockStatic(PendingIntent.class)) { |
| 59 | + pendingIntentStatic.when(() -> PendingIntent.getBroadcast( |
| 60 | + eq(context), |
| 61 | + eq(0), |
| 62 | + any(Intent.class), |
| 63 | + eq(PendingIntent.FLAG_IMMUTABLE) |
| 64 | + )).thenReturn(pendingIntent); |
| 65 | + |
| 66 | + AppUsageNotificationsManager manager = new AppUsageNotificationsManager(context); |
| 67 | + |
| 68 | + long beforeCall = System.currentTimeMillis(); |
| 69 | + manager.scheduleAppUsageCheck(); |
| 70 | + long afterCall = System.currentTimeMillis(); |
| 71 | + |
| 72 | + long expectedInterval = TimeUnit.DAYS.toMillis(3); |
| 73 | + |
| 74 | + ArgumentCaptor<Long> triggerTimeCaptor = ArgumentCaptor.forClass(Long.class); |
| 75 | + |
| 76 | + verify(alarmManager).setRepeating( |
| 77 | + eq(AlarmManager.RTC_WAKEUP), |
| 78 | + triggerTimeCaptor.capture(), |
| 79 | + eq(expectedInterval), |
| 80 | + eq(pendingIntent) |
| 81 | + ); |
| 82 | + |
| 83 | + long triggerTime = triggerTimeCaptor.getValue(); |
| 84 | + long scheduledBaseTime = triggerTime - expectedInterval; |
| 85 | + |
| 86 | + assertTrue("Trigger time should be scheduled three days from the invocation time", scheduledBaseTime >= beforeCall); |
| 87 | + assertTrue("Trigger time should be scheduled three days from the invocation time", scheduledBaseTime <= afterCall); |
| 88 | + |
| 89 | + pendingIntentStatic.verify(() -> PendingIntent.getBroadcast( |
| 90 | + eq(context), |
| 91 | + eq(0), |
| 92 | + argThat(this::isAppUsageReceiverIntent), |
| 93 | + eq(PendingIntent.FLAG_IMMUTABLE) |
| 94 | + )); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private boolean isAppUsageReceiverIntent(Intent intent) { |
| 99 | + if (intent == null) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + ComponentName componentName = intent.getComponent(); |
| 103 | + return componentName != null |
| 104 | + && componentName.getClassName().equals(AppUsageNotificationReceiver.class.getName()); |
| 105 | + } |
| 106 | +} |
0 commit comments