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 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.Matchers.anyInt; |
| 22 | import static org.mockito.Matchers.anyString; |
| 23 | import static org.mockito.Matchers.eq; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.app.Notification; |
| 27 | import android.app.NotificationChannel; |
| 28 | import android.app.NotificationManager; |
| 29 | import android.content.Context; |
| 30 | import android.content.pm.ApplicationInfo; |
| 31 | import android.content.pm.PackageManager; |
| 32 | import android.media.session.MediaSession; |
| 33 | import android.os.Build; |
| 34 | import android.os.UserHandle; |
| 35 | import android.provider.Settings; |
| 36 | import android.service.notification.StatusBarNotification; |
| 37 | import android.telecom.TelecomManager; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 38 | import android.support.test.runner.AndroidJUnit4; |
| 39 | import android.test.suitebuilder.annotation.SmallTest; |
| 40 | |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.mockito.Mock; |
| 45 | import org.mockito.MockitoAnnotations; |
| 46 | |
| 47 | import java.util.ArrayList; |
| 48 | import java.util.Collections; |
| 49 | import java.util.List; |
| 50 | |
| 51 | @SmallTest |
| 52 | @RunWith(AndroidJUnit4.class) |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 53 | public class NotificationComparatorTest extends NotificationTestCase { |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 54 | @Mock Context mContext; |
| 55 | @Mock TelecomManager mTm; |
| 56 | @Mock RankingHandler handler; |
| 57 | @Mock PackageManager mPm; |
| 58 | |
| 59 | private final String callPkg = "com.android.server.notification"; |
| 60 | private final int callUid = 10; |
| 61 | private String smsPkg; |
| 62 | private final int smsUid = 11; |
| 63 | private final String pkg2 = "pkg2"; |
| 64 | private final int uid2 = 1111111; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 65 | private static final String TEST_CHANNEL_ID = "test_channel_id"; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 66 | |
| 67 | private NotificationRecord mRecordMinCall; |
| 68 | private NotificationRecord mRecordHighCall; |
| 69 | private NotificationRecord mRecordDefaultMedia; |
| 70 | private NotificationRecord mRecordEmail; |
| 71 | private NotificationRecord mRecordInlineReply; |
| 72 | private NotificationRecord mRecordSms; |
| 73 | private NotificationRecord mRecordStarredContact; |
| 74 | private NotificationRecord mRecordContact; |
| 75 | private NotificationRecord mRecordUrgent; |
| 76 | private NotificationRecord mRecordCheater; |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 77 | private NotificationRecord mRecordCheaterColorized; |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 78 | private NotificationRecord mNoMediaSessionMedia; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 79 | |
| 80 | @Before |
| 81 | public void setUp() { |
| 82 | MockitoAnnotations.initMocks(this); |
| 83 | int userId = UserHandle.myUserId(); |
| 84 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 85 | when(mContext.getResources()).thenReturn(getContext().getResources()); |
| 86 | when(mContext.getContentResolver()).thenReturn(getContext().getContentResolver()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 87 | when(mContext.getPackageManager()).thenReturn(mPm); |
| 88 | when(mContext.getSystemService(eq(Context.TELECOM_SERVICE))).thenReturn(mTm); |
| 89 | when(mTm.getDefaultDialerPackage()).thenReturn(callPkg); |
| 90 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 91 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 92 | try { |
| 93 | when(mPm.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(legacy); |
| 94 | when(mContext.getApplicationInfo()).thenReturn(legacy); |
| 95 | } catch (PackageManager.NameNotFoundException e) { |
| 96 | // let's hope not |
| 97 | } |
| 98 | |
| 99 | smsPkg = Settings.Secure.getString(mContext.getContentResolver(), |
| 100 | Settings.Secure.SMS_DEFAULT_APPLICATION); |
| 101 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 102 | Notification n1 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 103 | .setCategory(Notification.CATEGORY_CALL) |
| 104 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 105 | .build(); |
| 106 | mRecordMinCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 107 | callPkg, 1, "minCall", callUid, callUid, n1, |
| 108 | new UserHandle(userId), "", 2000), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 109 | mRecordMinCall.setUserImportance(NotificationManager.IMPORTANCE_MIN); |
| 110 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 111 | Notification n2 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 112 | .setCategory(Notification.CATEGORY_CALL) |
| 113 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
Selim Cinek | 3f146d0 | 2017-02-06 13:32:47 -0800 | [diff] [blame] | 114 | .setColorized(true /* colorized */) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 115 | .build(); |
| 116 | mRecordHighCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 117 | callPkg, 1, "highcall", callUid, callUid, n2, |
| 118 | new UserHandle(userId), "", 1999), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 119 | mRecordHighCall.setUserImportance(NotificationManager.IMPORTANCE_HIGH); |
| 120 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 121 | Notification n3 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 122 | .setStyle(new Notification.MediaStyle() |
| 123 | .setMediaSession(new MediaSession.Token(null))) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 124 | .build(); |
| 125 | mRecordDefaultMedia = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 126 | pkg2, 1, "media", uid2, uid2, n3, new UserHandle(userId), |
| 127 | "", 1499), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 128 | mRecordDefaultMedia.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); |
| 129 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 130 | Notification n4 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 131 | .setStyle(new Notification.MessagingStyle("sender!")).build(); |
| 132 | mRecordInlineReply = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 133 | pkg2, 1, "inlinereply", uid2, uid2, n4, new UserHandle(userId), |
| 134 | "", 1599), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 135 | mRecordInlineReply.setUserImportance(NotificationManager.IMPORTANCE_HIGH); |
| 136 | mRecordInlineReply.setPackagePriority(Notification.PRIORITY_MAX); |
| 137 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 138 | Notification n5 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 139 | .setCategory(Notification.CATEGORY_MESSAGE).build(); |
| 140 | mRecordSms = new NotificationRecord(mContext, new StatusBarNotification(smsPkg, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 141 | smsPkg, 1, "sms", smsUid, smsUid, n5, new UserHandle(userId), |
| 142 | "", 1299), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 143 | mRecordSms.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); |
| 144 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 145 | Notification n6 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 146 | mRecordStarredContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 147 | pkg2, 1, "starred", uid2, uid2, n6, new UserHandle(userId), |
| 148 | "", 1259), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 149 | mRecordStarredContact.setContactAffinity(ValidateNotificationPeople.STARRED_CONTACT); |
| 150 | mRecordStarredContact.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); |
| 151 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 152 | Notification n7 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 153 | mRecordContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 154 | pkg2, 1, "contact", uid2, uid2, n7, new UserHandle(userId), |
| 155 | "", 1259), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 156 | mRecordContact.setContactAffinity(ValidateNotificationPeople.VALID_CONTACT); |
| 157 | mRecordContact.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); |
| 158 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 159 | Notification n8 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 160 | mRecordUrgent = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 161 | pkg2, 1, "urgent", uid2, uid2, n8, new UserHandle(userId), |
| 162 | "", 1258), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 163 | mRecordUrgent.setUserImportance(NotificationManager.IMPORTANCE_HIGH); |
| 164 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 165 | Notification n9 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 166 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 167 | .setFlag(Notification.FLAG_ONGOING_EVENT |
| 168 | |Notification.FLAG_FOREGROUND_SERVICE, true) |
| 169 | .build(); |
| 170 | mRecordCheater = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 171 | pkg2, 1, "cheater", uid2, uid2, n9, new UserHandle(userId), |
| 172 | "", 9258), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 173 | mRecordCheater.setUserImportance(NotificationManager.IMPORTANCE_LOW); |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 174 | mRecordCheater.setPackagePriority(Notification.PRIORITY_MAX); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 175 | |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 176 | Notification n10 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 177 | .setStyle(new Notification.InboxStyle().setSummaryText("message!")).build(); |
| 178 | mRecordEmail = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 179 | pkg2, 1, "email", uid2, uid2, n10, new UserHandle(userId), |
| 180 | "", 1599), getDefaultChannel()); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 181 | mRecordEmail.setUserImportance(NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 182 | |
| 183 | Notification n11 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 184 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 185 | .setColorized(true) |
| 186 | .build(); |
| 187 | mRecordCheaterColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2, |
| 188 | pkg2, 1, "cheater", uid2, uid2, n11, new UserHandle(userId), |
| 189 | "", 9258), getDefaultChannel()); |
| 190 | mRecordCheaterColorized.setUserImportance(NotificationManager.IMPORTANCE_LOW); |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 191 | |
| 192 | Notification n12 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 193 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 194 | .setColorized(true) |
| 195 | .setStyle(new Notification.MediaStyle()) |
| 196 | .build(); |
| 197 | mNoMediaSessionMedia = new NotificationRecord(mContext, new StatusBarNotification( |
| 198 | pkg2, pkg2, 1, "cheater", uid2, uid2, n12, new UserHandle(userId), |
| 199 | "", 9258), getDefaultChannel()); |
| 200 | mNoMediaSessionMedia.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void testOrdering() throws Exception { |
| 205 | final List<NotificationRecord> expected = new ArrayList<>(); |
| 206 | expected.add(mRecordHighCall); |
| 207 | expected.add(mRecordDefaultMedia); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 208 | expected.add(mRecordInlineReply); |
| 209 | expected.add(mRecordSms); |
Selim Cinek | 3f146d0 | 2017-02-06 13:32:47 -0800 | [diff] [blame] | 210 | expected.add(mRecordStarredContact); |
| 211 | expected.add(mRecordContact); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 212 | expected.add(mRecordEmail); |
| 213 | expected.add(mRecordUrgent); |
Selim Cinek | c6c639cb | 2017-05-05 19:49:22 -0700 | [diff] [blame] | 214 | expected.add(mNoMediaSessionMedia); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 215 | expected.add(mRecordCheater); |
Julia Reynolds | 889280c | 2017-04-12 12:27:00 -0400 | [diff] [blame] | 216 | expected.add(mRecordCheaterColorized); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 217 | expected.add(mRecordMinCall); |
| 218 | |
| 219 | List<NotificationRecord> actual = new ArrayList<>(); |
| 220 | actual.addAll(expected); |
| 221 | Collections.shuffle(actual); |
| 222 | |
| 223 | Collections.sort(actual, new NotificationComparator(mContext)); |
| 224 | |
| 225 | assertEquals(expected, actual); |
| 226 | } |
| 227 | |
| 228 | @Test |
| 229 | public void testMessaging() throws Exception { |
| 230 | NotificationComparator comp = new NotificationComparator(mContext); |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 231 | assertTrue(comp.isImportantMessaging(mRecordInlineReply)); |
| 232 | assertTrue(comp.isImportantMessaging(mRecordSms)); |
| 233 | assertFalse(comp.isImportantMessaging(mRecordEmail)); |
| 234 | assertFalse(comp.isImportantMessaging(mRecordCheater)); |
| 235 | } |
| 236 | |
Selim Cinek | 3f146d0 | 2017-02-06 13:32:47 -0800 | [diff] [blame] | 237 | @Test |
| 238 | public void testPeople() throws Exception { |
| 239 | NotificationComparator comp = new NotificationComparator(mContext); |
| 240 | assertTrue(comp.isImportantPeople(mRecordStarredContact)); |
| 241 | assertTrue(comp.isImportantPeople(mRecordContact)); |
| 242 | } |
| 243 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 244 | private NotificationChannel getDefaultChannel() { |
| 245 | return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name", |
| 246 | NotificationManager.IMPORTANCE_LOW); |
| 247 | } |
| 248 | } |