Skip to content

Commit c44e0ce

Browse files
Add tests for app usage notifications
1 parent c699913 commit c44e0ce

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.d4rk.androidtutorials.java.notifications.managers;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.eq;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.when;
9+
10+
import android.app.AlarmManager;
11+
import android.app.PendingIntent;
12+
import android.content.Context;
13+
14+
import org.junit.Test;
15+
import org.mockito.ArgumentCaptor;
16+
17+
import java.util.concurrent.TimeUnit;
18+
19+
/**
20+
* Tests for {@link AppUsageNotificationsManager}.
21+
*/
22+
public class AppUsageNotificationsManagerTest {
23+
24+
@Test
25+
public void scheduleAppUsageCheck_setsRepeatingAlarmWithThreeDayInterval() {
26+
AlarmManager alarmManager = mock(AlarmManager.class);
27+
Context context = mock(Context.class);
28+
when(context.getSystemService(Context.ALARM_SERVICE)).thenReturn(alarmManager);
29+
when(context.getApplicationContext()).thenReturn(context);
30+
31+
long now = System.currentTimeMillis();
32+
AppUsageNotificationsManager manager = new AppUsageNotificationsManager(context);
33+
34+
manager.scheduleAppUsageCheck();
35+
36+
ArgumentCaptor<Long> triggerCaptor = ArgumentCaptor.forClass(Long.class);
37+
verify(alarmManager).setRepeating(
38+
eq(AlarmManager.RTC_WAKEUP),
39+
triggerCaptor.capture(),
40+
eq(TimeUnit.DAYS.toMillis(3)),
41+
any(PendingIntent.class)
42+
);
43+
44+
long expectedTrigger = now + TimeUnit.DAYS.toMillis(3);
45+
assertTrue(Math.abs(triggerCaptor.getValue() - expectedTrigger) < 1000);
46+
}
47+
}
48+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)