|
| 1 | +package com.d4rk.androidtutorials.java.notifications.managers; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import android.app.NotificationChannel; |
| 8 | +import android.app.NotificationManager; |
| 9 | +import android.app.PendingIntent; |
| 10 | +import android.content.Context; |
| 11 | +import android.content.Intent; |
| 12 | + |
| 13 | +import com.google.android.gms.tasks.OnSuccessListener; |
| 14 | +import com.google.android.gms.tasks.Task; |
| 15 | +import com.google.android.play.core.appupdate.AppUpdateInfo; |
| 16 | +import com.google.android.play.core.appupdate.AppUpdateManager; |
| 17 | +import com.google.android.play.core.appupdate.AppUpdateManagerFactory; |
| 18 | +import com.google.android.play.core.install.model.AppUpdateType; |
| 19 | +import com.google.android.play.core.install.model.UpdateAvailability; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.mockito.MockedConstruction; |
| 23 | +import org.mockito.MockedStatic; |
| 24 | + |
| 25 | +/** |
| 26 | + * Unit tests for {@link AppUpdateNotificationsManager}. |
| 27 | + */ |
| 28 | +public class AppUpdateNotificationsManagerTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void checkAndSendUpdateNotification_updateAvailableAndAllowed_sendsNotification() { |
| 32 | + Context context = mock(Context.class); |
| 33 | + NotificationManager notificationManager = mock(NotificationManager.class); |
| 34 | + when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager); |
| 35 | + when(context.getString(anyInt())).thenReturn("test"); |
| 36 | + when(context.getPackageName()).thenReturn("com.test"); |
| 37 | + |
| 38 | + AppUpdateManager appUpdateManager = mock(AppUpdateManager.class); |
| 39 | + AppUpdateInfo appUpdateInfo = mock(AppUpdateInfo.class); |
| 40 | + when(appUpdateInfo.updateAvailability()).thenReturn(UpdateAvailability.UPDATE_AVAILABLE); |
| 41 | + when(appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)).thenReturn(true); |
| 42 | + |
| 43 | + Task<AppUpdateInfo> task = mock(Task.class); |
| 44 | + doAnswer(invocation -> { |
| 45 | + OnSuccessListener<AppUpdateInfo> listener = invocation.getArgument(0); |
| 46 | + listener.onSuccess(appUpdateInfo); |
| 47 | + return task; |
| 48 | + }).when(task).addOnSuccessListener(any()); |
| 49 | + when(appUpdateManager.getAppUpdateInfo()).thenReturn(task); |
| 50 | + |
| 51 | + try (MockedStatic<AppUpdateManagerFactory> factory = mockStatic(AppUpdateManagerFactory.class); |
| 52 | + MockedStatic<PendingIntent> pendingIntent = mockStatic(PendingIntent.class); |
| 53 | + MockedConstruction<NotificationChannel> channel = mockConstruction(NotificationChannel.class)) { |
| 54 | + factory.when(() -> AppUpdateManagerFactory.create(context)).thenReturn(appUpdateManager); |
| 55 | + pendingIntent.when(() -> PendingIntent.getActivity(eq(context), anyInt(), any(Intent.class), anyInt())) |
| 56 | + .thenReturn(mock(PendingIntent.class)); |
| 57 | + |
| 58 | + AppUpdateNotificationsManager manager = new AppUpdateNotificationsManager(context); |
| 59 | + manager.checkAndSendUpdateNotification(); |
| 60 | + } |
| 61 | + |
| 62 | + verify(notificationManager).createNotificationChannel(any(NotificationChannel.class)); |
| 63 | + verify(notificationManager).notify(eq(0), any()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void checkAndSendUpdateNotification_updateNotAvailable_doesNotSendNotification() { |
| 68 | + Context context = mock(Context.class); |
| 69 | + NotificationManager notificationManager = mock(NotificationManager.class); |
| 70 | + when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager); |
| 71 | + when(context.getString(anyInt())).thenReturn("test"); |
| 72 | + when(context.getPackageName()).thenReturn("com.test"); |
| 73 | + |
| 74 | + AppUpdateManager appUpdateManager = mock(AppUpdateManager.class); |
| 75 | + AppUpdateInfo appUpdateInfo = mock(AppUpdateInfo.class); |
| 76 | + when(appUpdateInfo.updateAvailability()).thenReturn(UpdateAvailability.UPDATE_NOT_AVAILABLE); |
| 77 | + |
| 78 | + Task<AppUpdateInfo> task = mock(Task.class); |
| 79 | + doAnswer(invocation -> { |
| 80 | + OnSuccessListener<AppUpdateInfo> listener = invocation.getArgument(0); |
| 81 | + listener.onSuccess(appUpdateInfo); |
| 82 | + return task; |
| 83 | + }).when(task).addOnSuccessListener(any()); |
| 84 | + when(appUpdateManager.getAppUpdateInfo()).thenReturn(task); |
| 85 | + |
| 86 | + try (MockedStatic<AppUpdateManagerFactory> factory = mockStatic(AppUpdateManagerFactory.class); |
| 87 | + MockedStatic<PendingIntent> pendingIntent = mockStatic(PendingIntent.class); |
| 88 | + MockedConstruction<NotificationChannel> channel = mockConstruction(NotificationChannel.class)) { |
| 89 | + factory.when(() -> AppUpdateManagerFactory.create(context)).thenReturn(appUpdateManager); |
| 90 | + pendingIntent.when(() -> PendingIntent.getActivity(eq(context), anyInt(), any(Intent.class), anyInt())) |
| 91 | + .thenReturn(mock(PendingIntent.class)); |
| 92 | + |
| 93 | + AppUpdateNotificationsManager manager = new AppUpdateNotificationsManager(context); |
| 94 | + manager.checkAndSendUpdateNotification(); |
| 95 | + } |
| 96 | + |
| 97 | + verify(notificationManager, never()).createNotificationChannel(any(NotificationChannel.class)); |
| 98 | + verify(notificationManager, never()).notify(anyInt(), any()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void checkAndSendUpdateNotification_updateNotAllowed_doesNotSendNotification() { |
| 103 | + Context context = mock(Context.class); |
| 104 | + NotificationManager notificationManager = mock(NotificationManager.class); |
| 105 | + when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager); |
| 106 | + when(context.getString(anyInt())).thenReturn("test"); |
| 107 | + when(context.getPackageName()).thenReturn("com.test"); |
| 108 | + |
| 109 | + AppUpdateManager appUpdateManager = mock(AppUpdateManager.class); |
| 110 | + AppUpdateInfo appUpdateInfo = mock(AppUpdateInfo.class); |
| 111 | + when(appUpdateInfo.updateAvailability()).thenReturn(UpdateAvailability.UPDATE_AVAILABLE); |
| 112 | + when(appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)).thenReturn(false); |
| 113 | + |
| 114 | + Task<AppUpdateInfo> task = mock(Task.class); |
| 115 | + doAnswer(invocation -> { |
| 116 | + OnSuccessListener<AppUpdateInfo> listener = invocation.getArgument(0); |
| 117 | + listener.onSuccess(appUpdateInfo); |
| 118 | + return task; |
| 119 | + }).when(task).addOnSuccessListener(any()); |
| 120 | + when(appUpdateManager.getAppUpdateInfo()).thenReturn(task); |
| 121 | + |
| 122 | + try (MockedStatic<AppUpdateManagerFactory> factory = mockStatic(AppUpdateManagerFactory.class); |
| 123 | + MockedStatic<PendingIntent> pendingIntent = mockStatic(PendingIntent.class); |
| 124 | + MockedConstruction<NotificationChannel> channel = mockConstruction(NotificationChannel.class)) { |
| 125 | + factory.when(() -> AppUpdateManagerFactory.create(context)).thenReturn(appUpdateManager); |
| 126 | + pendingIntent.when(() -> PendingIntent.getActivity(eq(context), anyInt(), any(Intent.class), anyInt())) |
| 127 | + .thenReturn(mock(PendingIntent.class)); |
| 128 | + |
| 129 | + AppUpdateNotificationsManager manager = new AppUpdateNotificationsManager(context); |
| 130 | + manager.checkAndSendUpdateNotification(); |
| 131 | + } |
| 132 | + |
| 133 | + verify(notificationManager, never()).createNotificationChannel(any(NotificationChannel.class)); |
| 134 | + verify(notificationManager, never()).notify(anyInt(), any()); |
| 135 | + } |
| 136 | +} |
| 137 | + |
0 commit comments