|
| 1 | +package com.d4rk.androidtutorials.java.notifications.workers; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 6 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | +import static org.mockito.ArgumentMatchers.eq; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.mockConstruction; |
| 11 | +import static org.mockito.Mockito.never; |
| 12 | +import static org.mockito.Mockito.times; |
| 13 | +import static org.mockito.Mockito.verify; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import android.app.Notification; |
| 17 | +import android.app.NotificationChannel; |
| 18 | +import android.app.NotificationManager; |
| 19 | +import android.content.Context; |
| 20 | +import android.content.SharedPreferences; |
| 21 | + |
| 22 | +import androidx.preference.PreferenceManager; |
| 23 | +import androidx.work.WorkerParameters; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | +import org.mockito.ArgumentCaptor; |
| 27 | +import org.mockito.MockedConstruction; |
| 28 | +import org.mockito.MockedStatic; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link AppUsageNotificationWorker}. |
| 32 | + */ |
| 33 | +public class AppUsageNotificationWorkerTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void doWork_lastUsedExceedsThreshold_showsNotificationAndUpdatesTimestamp() { |
| 37 | + Context context = mock(Context.class); |
| 38 | + when(context.getApplicationContext()).thenReturn(context); |
| 39 | + when(context.getString(anyInt())).thenReturn(""); |
| 40 | + NotificationManager notificationManager = mock(NotificationManager.class); |
| 41 | + when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager); |
| 42 | + |
| 43 | + SharedPreferences sharedPreferences = mock(SharedPreferences.class); |
| 44 | + SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class); |
| 45 | + when(sharedPreferences.getLong(eq("lastUsed"), anyLong())).thenReturn(0L); |
| 46 | + when(sharedPreferences.edit()).thenReturn(editor); |
| 47 | + when(editor.putLong(anyString(), anyLong())).thenReturn(editor); |
| 48 | + |
| 49 | + try (MockedStatic<PreferenceManager> prefManager = mockStatic(PreferenceManager.class); |
| 50 | + MockedConstruction<NotificationChannel> ignoredChannel = mockConstruction(NotificationChannel.class)) { |
| 51 | + prefManager.when(() -> PreferenceManager.getDefaultSharedPreferences(context)) |
| 52 | + .thenReturn(sharedPreferences); |
| 53 | + |
| 54 | + WorkerParameters workerParameters = mock(WorkerParameters.class); |
| 55 | + AppUsageNotificationWorker worker = new AppUsageNotificationWorker(context, workerParameters); |
| 56 | + |
| 57 | + worker.doWork(); |
| 58 | + } |
| 59 | + |
| 60 | + verify(notificationManager, times(1)).notify(eq(0), any(Notification.class)); |
| 61 | + |
| 62 | + ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class); |
| 63 | + verify(editor).putLong(eq("lastUsed"), captor.capture()); |
| 64 | + verify(editor).apply(); |
| 65 | + assertTrue(captor.getValue() > 0L); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void doWork_lastUsedWithinThreshold_noNotificationButTimestampUpdated() { |
| 70 | + Context context = mock(Context.class); |
| 71 | + when(context.getApplicationContext()).thenReturn(context); |
| 72 | + when(context.getString(anyInt())).thenReturn(""); |
| 73 | + NotificationManager notificationManager = mock(NotificationManager.class); |
| 74 | + when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager); |
| 75 | + |
| 76 | + long lastUsed = System.currentTimeMillis(); |
| 77 | + SharedPreferences sharedPreferences = mock(SharedPreferences.class); |
| 78 | + SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class); |
| 79 | + when(sharedPreferences.getLong(eq("lastUsed"), anyLong())).thenReturn(lastUsed); |
| 80 | + when(sharedPreferences.edit()).thenReturn(editor); |
| 81 | + when(editor.putLong(anyString(), anyLong())).thenReturn(editor); |
| 82 | + |
| 83 | + try (MockedStatic<PreferenceManager> prefManager = mockStatic(PreferenceManager.class); |
| 84 | + MockedConstruction<NotificationChannel> ignoredChannel = mockConstruction(NotificationChannel.class)) { |
| 85 | + prefManager.when(() -> PreferenceManager.getDefaultSharedPreferences(context)) |
| 86 | + .thenReturn(sharedPreferences); |
| 87 | + |
| 88 | + WorkerParameters workerParameters = mock(WorkerParameters.class); |
| 89 | + AppUsageNotificationWorker worker = new AppUsageNotificationWorker(context, workerParameters); |
| 90 | + |
| 91 | + worker.doWork(); |
| 92 | + } |
| 93 | + |
| 94 | + verify(notificationManager, never()).notify(anyInt(), any(Notification.class)); |
| 95 | + |
| 96 | + ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class); |
| 97 | + verify(editor).putLong(eq("lastUsed"), captor.capture()); |
| 98 | + verify(editor).apply(); |
| 99 | + assertTrue(captor.getValue() >= lastUsed); |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments