Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2018 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 android.ext.services.notification; |
| 18 | |
| 19 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
| 20 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
| 21 | |
| 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | import static org.mockito.ArgumentMatchers.anyLong; |
| 25 | import static org.mockito.ArgumentMatchers.anyString; |
| 26 | import static org.mockito.Mockito.mock; |
| 27 | import static org.mockito.Mockito.never; |
| 28 | import static org.mockito.Mockito.times; |
| 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | |
| 32 | import android.app.AlarmManager; |
| 33 | import android.app.Notification; |
| 34 | import android.app.NotificationChannel; |
| 35 | import android.app.PendingIntent; |
| 36 | import android.content.pm.ApplicationInfo; |
| 37 | import android.content.pm.IPackageManager; |
| 38 | import android.os.Build; |
| 39 | import android.os.Process; |
| 40 | import android.os.UserHandle; |
| 41 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 42 | import android.testing.TestableContext; |
| 43 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 44 | import androidx.test.InstrumentationRegistry; |
| 45 | import androidx.test.runner.AndroidJUnit4; |
| 46 | |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 47 | import org.junit.Before; |
| 48 | import org.junit.Rule; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
| 53 | |
| 54 | @RunWith(AndroidJUnit4.class) |
| 55 | public class AgingHelperTest { |
| 56 | private String mPkg = "pkg"; |
| 57 | private int mUid = 2018; |
| 58 | |
| 59 | @Rule |
| 60 | public final TestableContext mContext = |
| 61 | new TestableContext(InstrumentationRegistry.getTargetContext(), null); |
| 62 | |
| 63 | @Mock |
| 64 | private NotificationCategorizer mCategorizer; |
| 65 | @Mock |
| 66 | private AlarmManager mAlarmManager; |
| 67 | @Mock |
| 68 | private IPackageManager mPackageManager; |
| 69 | @Mock |
| 70 | private AgingHelper.Callback mCallback; |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 71 | @Mock |
| 72 | private SmsHelper mSmsHelper; |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 73 | |
| 74 | private AgingHelper mAgingHelper; |
| 75 | |
| 76 | private StatusBarNotification generateSbn(String channelId) { |
| 77 | Notification n = new Notification.Builder(mContext, channelId) |
| 78 | .setContentTitle("foo") |
| 79 | .build(); |
| 80 | |
| 81 | return new StatusBarNotification(mPkg, mPkg, 0, "tag", mUid, mUid, n, |
| 82 | UserHandle.SYSTEM, null, 0); |
| 83 | } |
| 84 | |
| 85 | @Before |
| 86 | public void setUp() throws Exception { |
| 87 | MockitoAnnotations.initMocks(this); |
| 88 | mPkg = mContext.getPackageName(); |
| 89 | mUid = Process.myUid(); |
| 90 | |
| 91 | ApplicationInfo info = mock(ApplicationInfo.class); |
| 92 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
| 93 | .thenReturn(info); |
| 94 | info.targetSdkVersion = Build.VERSION_CODES.P; |
| 95 | |
| 96 | mContext.addMockSystemService(AlarmManager.class, mAlarmManager); |
| 97 | |
| 98 | mAgingHelper = new AgingHelper(mContext, mCategorizer, mCallback); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testNoSnoozingOnPost() { |
| 103 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 104 | StatusBarNotification sbn = generateSbn(channel.getId()); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 105 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 106 | |
| 107 | |
| 108 | mAgingHelper.onNotificationPosted(entry); |
| 109 | verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any()); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testPostResetsSnooze() { |
| 114 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 115 | StatusBarNotification sbn = generateSbn(channel.getId()); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 116 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 117 | |
| 118 | |
| 119 | mAgingHelper.onNotificationPosted(entry); |
| 120 | verify(mAlarmManager, times(1)).cancel(any(PendingIntent.class)); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testSnoozingOnSeen() { |
| 125 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 126 | StatusBarNotification sbn = generateSbn(channel.getId()); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 127 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 128 | entry.setSeen(); |
| 129 | when(mCategorizer.getCategory(entry)).thenReturn(NotificationCategorizer.CATEGORY_PEOPLE); |
| 130 | |
| 131 | mAgingHelper.onNotificationSeen(entry); |
| 132 | verify(mAlarmManager, times(1)).setExactAndAllowWhileIdle(anyInt(), anyLong(), any()); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testNoSnoozingOnSeenUserLocked() { |
| 137 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 138 | channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 139 | StatusBarNotification sbn = generateSbn(channel.getId()); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 140 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 141 | when(mCategorizer.getCategory(entry)).thenReturn(NotificationCategorizer.CATEGORY_PEOPLE); |
| 142 | |
| 143 | mAgingHelper.onNotificationSeen(entry); |
| 144 | verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any()); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testNoSnoozingOnSeenAlreadyLow() { |
| 149 | NotificationEntry entry = mock(NotificationEntry.class); |
| 150 | when(entry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_HIGH)); |
| 151 | when(entry.getImportance()).thenReturn(IMPORTANCE_MIN); |
| 152 | |
| 153 | mAgingHelper.onNotificationSeen(entry); |
| 154 | verify(mAlarmManager, never()).setExactAndAllowWhileIdle(anyInt(), anyLong(), any()); |
| 155 | } |
| 156 | } |