Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.notification; |
| 17 | |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 18 | import static org.hamcrest.Matchers.contains; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 19 | import static org.junit.Assert.assertFalse; |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 20 | import static org.junit.Assert.assertThat; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 21 | import static org.junit.Assert.assertTrue; |
| 22 | import static org.mockito.Matchers.anyInt; |
| 23 | import static org.mockito.Matchers.anyString; |
| 24 | import static org.mockito.Matchers.eq; |
| 25 | import static org.mockito.Mockito.when; |
Alex Mang | 730d949 | 2019-12-03 18:06:09 -0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.spy; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 27 | |
| 28 | import android.app.Notification; |
| 29 | import android.app.NotificationChannel; |
| 30 | import android.app.NotificationManager; |
| 31 | import android.content.Context; |
| 32 | import android.content.pm.ApplicationInfo; |
| 33 | import android.content.pm.PackageManager; |
| 34 | import android.media.session.MediaSession; |
| 35 | import android.os.Build; |
| 36 | import android.os.UserHandle; |
| 37 | import android.provider.Settings; |
| 38 | import android.service.notification.StatusBarNotification; |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 39 | import android.telecom.TelecomManager; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 40 | import android.test.suitebuilder.annotation.SmallTest; |
| 41 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 42 | import androidx.test.runner.AndroidJUnit4; |
| 43 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 44 | import com.android.server.UiServiceTestCase; |
| 45 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.MockitoAnnotations; |
| 51 | |
| 52 | import java.util.ArrayList; |
| 53 | import java.util.Collections; |
| 54 | import java.util.List; |
| 55 | |
| 56 | @SmallTest |
| 57 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 58 | public class NotificationComparatorTest extends UiServiceTestCase { |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 59 | @Mock Context mContext; |
| 60 | @Mock TelecomManager mTm; |
| 61 | @Mock RankingHandler handler; |
| 62 | @Mock PackageManager mPm; |
| 63 | |
| 64 | private final String callPkg = "com.android.server.notification"; |
| 65 | private final int callUid = 10; |
| 66 | private String smsPkg; |
| 67 | private final int smsUid = 11; |
| 68 | private final String pkg2 = "pkg2"; |
| 69 | private final int uid2 = 1111111; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 70 | private static final String TEST_CHANNEL_ID = "test_channel_id"; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 71 | |
Lyn Han | 405e0b7 | 2019-08-13 16:07:55 -0700 | [diff] [blame] | 72 | private NotificationRecord mRecordMinCallNonInterruptive; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 73 | private NotificationRecord mRecordMinCall; |
| 74 | private NotificationRecord mRecordHighCall; |
| 75 | private NotificationRecord mRecordDefaultMedia; |
| 76 | private NotificationRecord mRecordEmail; |
| 77 | private NotificationRecord mRecordInlineReply; |
| 78 | private NotificationRecord mRecordSms; |
| 79 | private NotificationRecord mRecordStarredContact; |
| 80 | private NotificationRecord mRecordContact; |
| 81 | private NotificationRecord mRecordUrgent; |
| 82 | private NotificationRecord mRecordCheater; |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 83 | private NotificationRecord mRecordCheaterColorized; |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 84 | private NotificationRecord mNoMediaSessionMedia; |
Julia Reynolds | 472f01c | 2018-01-18 09:25:44 -0500 | [diff] [blame] | 85 | private NotificationRecord mRecordColorized; |
| 86 | private NotificationRecord mRecordColorizedCall; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 87 | |
| 88 | @Before |
| 89 | public void setUp() { |
| 90 | MockitoAnnotations.initMocks(this); |
| 91 | int userId = UserHandle.myUserId(); |
| 92 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 93 | when(mContext.getResources()).thenReturn(getContext().getResources()); |
| 94 | when(mContext.getContentResolver()).thenReturn(getContext().getContentResolver()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 95 | when(mContext.getPackageManager()).thenReturn(mPm); |
| 96 | when(mContext.getSystemService(eq(Context.TELECOM_SERVICE))).thenReturn(mTm); |
| 97 | when(mTm.getDefaultDialerPackage()).thenReturn(callPkg); |
| 98 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 99 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 100 | try { |
| 101 | when(mPm.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(legacy); |
| 102 | when(mContext.getApplicationInfo()).thenReturn(legacy); |
| 103 | } catch (PackageManager.NameNotFoundException e) { |
| 104 | // let's hope not |
| 105 | } |
| 106 | |
| 107 | smsPkg = Settings.Secure.getString(mContext.getContentResolver(), |
| 108 | Settings.Secure.SMS_DEFAULT_APPLICATION); |
| 109 | |
Lyn Han | 405e0b7 | 2019-08-13 16:07:55 -0700 | [diff] [blame] | 110 | Notification nonInterruptiveNotif = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 111 | .setCategory(Notification.CATEGORY_CALL) |
| 112 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 113 | .build(); |
| 114 | mRecordMinCallNonInterruptive = new NotificationRecord(mContext, |
| 115 | new StatusBarNotification(callPkg, |
| 116 | callPkg, 1, "mRecordMinCallNonInterruptive", callUid, callUid, |
| 117 | nonInterruptiveNotif, |
| 118 | new UserHandle(userId), "", 2000), getDefaultChannel()); |
| 119 | mRecordMinCallNonInterruptive.setSystemImportance(NotificationManager.IMPORTANCE_MIN); |
| 120 | mRecordMinCallNonInterruptive.setInterruptive(false); |
| 121 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 122 | Notification n1 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 123 | .setCategory(Notification.CATEGORY_CALL) |
| 124 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 125 | .build(); |
| 126 | mRecordMinCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 127 | callPkg, 1, "minCall", callUid, callUid, n1, |
| 128 | new UserHandle(userId), "", 2000), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 129 | mRecordMinCall.setSystemImportance(NotificationManager.IMPORTANCE_MIN); |
Lyn Han | 405e0b7 | 2019-08-13 16:07:55 -0700 | [diff] [blame] | 130 | mRecordMinCall.setInterruptive(true); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 131 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 132 | Notification n2 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 133 | .setCategory(Notification.CATEGORY_CALL) |
| 134 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 135 | .build(); |
| 136 | mRecordHighCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 137 | callPkg, 1, "highcall", callUid, callUid, n2, |
| 138 | new UserHandle(userId), "", 1999), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 139 | mRecordHighCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 140 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 141 | Notification n3 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 142 | .setStyle(new Notification.MediaStyle() |
| 143 | .setMediaSession(new MediaSession.Token(null))) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 144 | .build(); |
| 145 | mRecordDefaultMedia = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 146 | pkg2, 1, "media", uid2, uid2, n3, new UserHandle(userId), |
| 147 | "", 1499), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 148 | mRecordDefaultMedia.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 149 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 150 | Notification n4 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 151 | .setStyle(new Notification.MessagingStyle("sender!")).build(); |
| 152 | mRecordInlineReply = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 153 | pkg2, 1, "inlinereply", uid2, uid2, n4, new UserHandle(userId), |
| 154 | "", 1599), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 155 | mRecordInlineReply.setSystemImportance(NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 156 | mRecordInlineReply.setPackagePriority(Notification.PRIORITY_MAX); |
| 157 | |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 158 | if (smsPkg != null) { |
| 159 | Notification n5 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 160 | .setCategory(Notification.CATEGORY_MESSAGE).build(); |
| 161 | mRecordSms = new NotificationRecord(mContext, new StatusBarNotification(smsPkg, |
| 162 | smsPkg, 1, "sms", smsUid, smsUid, n5, new UserHandle(userId), |
| 163 | "", 1299), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 164 | mRecordSms.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 165 | } |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 166 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 167 | Notification n6 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 168 | mRecordStarredContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 169 | pkg2, 1, "starred", uid2, uid2, n6, new UserHandle(userId), |
| 170 | "", 1259), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 171 | mRecordStarredContact.setContactAffinity(ValidateNotificationPeople.STARRED_CONTACT); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 172 | mRecordStarredContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 173 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 174 | Notification n7 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 175 | mRecordContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 176 | pkg2, 1, "contact", uid2, uid2, n7, new UserHandle(userId), |
| 177 | "", 1259), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 178 | mRecordContact.setContactAffinity(ValidateNotificationPeople.VALID_CONTACT); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 179 | mRecordContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 180 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 181 | Notification n8 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 182 | mRecordUrgent = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 183 | pkg2, 1, "urgent", uid2, uid2, n8, new UserHandle(userId), |
| 184 | "", 1258), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 185 | mRecordUrgent.setSystemImportance(NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 186 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 187 | Notification n9 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 188 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 189 | .setFlag(Notification.FLAG_ONGOING_EVENT |
| 190 | |Notification.FLAG_FOREGROUND_SERVICE, true) |
| 191 | .build(); |
| 192 | mRecordCheater = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 193 | pkg2, 1, "cheater", uid2, uid2, n9, new UserHandle(userId), |
| 194 | "", 9258), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 195 | mRecordCheater.setSystemImportance(NotificationManager.IMPORTANCE_LOW); |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 196 | mRecordCheater.setPackagePriority(Notification.PRIORITY_MAX); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 197 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 198 | Notification n10 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 199 | .setStyle(new Notification.InboxStyle().setSummaryText("message!")).build(); |
| 200 | mRecordEmail = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 201 | pkg2, 1, "email", uid2, uid2, n10, new UserHandle(userId), |
| 202 | "", 1599), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 203 | mRecordEmail.setSystemImportance(NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 204 | |
| 205 | Notification n11 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 206 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 207 | .setColorized(true) |
| 208 | .build(); |
| 209 | mRecordCheaterColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
| 210 | pkg2, 1, "cheater", uid2, uid2, n11, new UserHandle(userId), |
| 211 | "", 9258), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 212 | mRecordCheaterColorized.setSystemImportance(NotificationManager.IMPORTANCE_LOW); |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 213 | |
| 214 | Notification n12 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 215 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 216 | .setColorized(true) |
| 217 | .setStyle(new Notification.MediaStyle()) |
| 218 | .build(); |
| 219 | mNoMediaSessionMedia = new NotificationRecord(mContext, new StatusBarNotification( |
| 220 | pkg2, pkg2, 1, "cheater", uid2, uid2, n12, new UserHandle(userId), |
| 221 | "", 9258), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 222 | mNoMediaSessionMedia.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | 472f01c | 2018-01-18 09:25:44 -0500 | [diff] [blame] | 223 | |
| 224 | Notification n13 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 225 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 226 | .setColorized(true /* colorized */) |
| 227 | .build(); |
| 228 | mRecordColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
| 229 | pkg2, 1, "colorized", uid2, uid2, n13, |
| 230 | new UserHandle(userId), "", 1999), getDefaultChannel()); |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 231 | mRecordColorized.setSystemImportance(NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 472f01c | 2018-01-18 09:25:44 -0500 | [diff] [blame] | 232 | |
| 233 | Notification n14 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 234 | .setCategory(Notification.CATEGORY_CALL) |
| 235 | .setColorized(true) |
| 236 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 237 | .build(); |
| 238 | mRecordColorizedCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg, |
| 239 | callPkg, 1, "colorizedCall", callUid, callUid, n14, |
| 240 | new UserHandle(userId), "", 1999), getDefaultChannel()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 241 | mRecordColorizedCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Test |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 245 | public void testOrdering() { |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 246 | final List<NotificationRecord> expected = new ArrayList<>(); |
Julia Reynolds | 472f01c | 2018-01-18 09:25:44 -0500 | [diff] [blame] | 247 | expected.add(mRecordColorizedCall); |
Julia Reynolds | 472f01c | 2018-01-18 09:25:44 -0500 | [diff] [blame] | 248 | expected.add(mRecordColorized); |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 249 | expected.add(mRecordDefaultMedia); |
Julia Reynolds | 472f01c | 2018-01-18 09:25:44 -0500 | [diff] [blame] | 250 | expected.add(mRecordHighCall); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 251 | expected.add(mRecordInlineReply); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 252 | if (mRecordSms != null) { |
| 253 | expected.add(mRecordSms); |
| 254 | } |
Selim Cinek | 3f146d0 | 2017-02-06 13:32:47 -0800 | [diff] [blame] | 255 | expected.add(mRecordStarredContact); |
| 256 | expected.add(mRecordContact); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 257 | expected.add(mRecordEmail); |
| 258 | expected.add(mRecordUrgent); |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 259 | expected.add(mNoMediaSessionMedia); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 260 | expected.add(mRecordCheater); |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 261 | expected.add(mRecordCheaterColorized); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 262 | expected.add(mRecordMinCall); |
Lyn Han | 405e0b7 | 2019-08-13 16:07:55 -0700 | [diff] [blame] | 263 | expected.add(mRecordMinCallNonInterruptive); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 264 | |
| 265 | List<NotificationRecord> actual = new ArrayList<>(); |
| 266 | actual.addAll(expected); |
| 267 | Collections.shuffle(actual); |
| 268 | |
| 269 | Collections.sort(actual, new NotificationComparator(mContext)); |
| 270 | |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 271 | assertThat(actual, contains(expected.toArray())); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Test |
Alex Mang | 730d949 | 2019-12-03 18:06:09 -0800 | [diff] [blame] | 275 | public void testRankingScoreOverrides() { |
| 276 | NotificationComparator comp = new NotificationComparator(mContext); |
| 277 | NotificationRecord recordMinCallNonInterruptive = spy(mRecordMinCallNonInterruptive); |
| 278 | assertTrue(comp.compare(mRecordMinCall, recordMinCallNonInterruptive) < 0); |
| 279 | |
| 280 | when(recordMinCallNonInterruptive.getRankingScore()).thenReturn(1f); |
| 281 | assertTrue(comp.compare(mRecordMinCall, recordMinCallNonInterruptive) > 0); |
| 282 | assertTrue(comp.compare(mRecordCheater, recordMinCallNonInterruptive) > 0); |
| 283 | assertTrue(comp.compare(mRecordColorizedCall, recordMinCallNonInterruptive) < 0); |
| 284 | } |
| 285 | |
| 286 | @Test |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 287 | public void testMessaging() { |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 288 | NotificationComparator comp = new NotificationComparator(mContext); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 289 | assertTrue(comp.isImportantMessaging(mRecordInlineReply)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 290 | if (mRecordSms != null) { |
| 291 | assertTrue(comp.isImportantMessaging(mRecordSms)); |
| 292 | } |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 293 | assertFalse(comp.isImportantMessaging(mRecordEmail)); |
| 294 | assertFalse(comp.isImportantMessaging(mRecordCheater)); |
| 295 | } |
| 296 | |
Selim Cinek | 3f146d0 | 2017-02-06 13:32:47 -0800 | [diff] [blame] | 297 | @Test |
Gus Prevas | 2c963d2 | 2018-12-03 15:49:06 -0500 | [diff] [blame] | 298 | public void testPeople() { |
Selim Cinek | 3f146d0 | 2017-02-06 13:32:47 -0800 | [diff] [blame] | 299 | NotificationComparator comp = new NotificationComparator(mContext); |
| 300 | assertTrue(comp.isImportantPeople(mRecordStarredContact)); |
| 301 | assertTrue(comp.isImportantPeople(mRecordContact)); |
| 302 | } |
| 303 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 304 | private NotificationChannel getDefaultChannel() { |
| 305 | return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name", |
| 306 | NotificationManager.IMPORTANCE_LOW); |
| 307 | } |
| 308 | } |