blob: 068129569d14cd2416d2c24f0b14db6781485bcc [file] [log] [blame]
Julia Reynolds4a02afb2016-12-13 13:39:52 -05001/*
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 */
16package com.android.server.notification;
17
Gus Prevas2c963d22018-12-03 15:49:06 -050018import static org.hamcrest.Matchers.contains;
Julia Reynolds4a02afb2016-12-13 13:39:52 -050019import static org.junit.Assert.assertFalse;
Gus Prevas2c963d22018-12-03 15:49:06 -050020import static org.junit.Assert.assertThat;
Julia Reynolds4a02afb2016-12-13 13:39:52 -050021import static org.junit.Assert.assertTrue;
22import static org.mockito.Matchers.anyInt;
23import static org.mockito.Matchers.anyString;
24import static org.mockito.Matchers.eq;
25import static org.mockito.Mockito.when;
26
27import android.app.Notification;
28import android.app.NotificationChannel;
29import android.app.NotificationManager;
30import android.content.Context;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.PackageManager;
33import android.media.session.MediaSession;
34import android.os.Build;
35import android.os.UserHandle;
36import android.provider.Settings;
37import android.service.notification.StatusBarNotification;
Julia Reynolds4a02afb2016-12-13 13:39:52 -050038import android.support.test.runner.AndroidJUnit4;
Gus Prevas2c963d22018-12-03 15:49:06 -050039import android.telecom.TelecomManager;
Julia Reynolds4a02afb2016-12-13 13:39:52 -050040import android.test.suitebuilder.annotation.SmallTest;
41
Jason Monk74f5e362017-12-06 08:56:33 -050042import com.android.server.UiServiceTestCase;
43
Julia Reynolds4a02afb2016-12-13 13:39:52 -050044import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.Mock;
48import org.mockito.MockitoAnnotations;
49
50import java.util.ArrayList;
51import java.util.Collections;
52import java.util.List;
53
54@SmallTest
55@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050056public class NotificationComparatorTest extends UiServiceTestCase {
Julia Reynolds4a02afb2016-12-13 13:39:52 -050057 @Mock Context mContext;
58 @Mock TelecomManager mTm;
59 @Mock RankingHandler handler;
60 @Mock PackageManager mPm;
61
62 private final String callPkg = "com.android.server.notification";
63 private final int callUid = 10;
64 private String smsPkg;
65 private final int smsUid = 11;
66 private final String pkg2 = "pkg2";
67 private final int uid2 = 1111111;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050068 private static final String TEST_CHANNEL_ID = "test_channel_id";
Julia Reynolds4a02afb2016-12-13 13:39:52 -050069
70 private NotificationRecord mRecordMinCall;
71 private NotificationRecord mRecordHighCall;
72 private NotificationRecord mRecordDefaultMedia;
73 private NotificationRecord mRecordEmail;
74 private NotificationRecord mRecordInlineReply;
75 private NotificationRecord mRecordSms;
76 private NotificationRecord mRecordStarredContact;
77 private NotificationRecord mRecordContact;
78 private NotificationRecord mRecordUrgent;
79 private NotificationRecord mRecordCheater;
Julia Reynolds889280c2017-04-12 12:27:00 -040080 private NotificationRecord mRecordCheaterColorized;
Selim Cinekc6c639cb2017-05-05 19:49:22 -070081 private NotificationRecord mNoMediaSessionMedia;
Julia Reynolds472f01c2018-01-18 09:25:44 -050082 private NotificationRecord mRecordColorized;
83 private NotificationRecord mRecordColorizedCall;
Julia Reynolds4a02afb2016-12-13 13:39:52 -050084
85 @Before
86 public void setUp() {
87 MockitoAnnotations.initMocks(this);
88 int userId = UserHandle.myUserId();
89
Chris Wren89aa2262017-05-05 18:05:56 -040090 when(mContext.getResources()).thenReturn(getContext().getResources());
91 when(mContext.getContentResolver()).thenReturn(getContext().getContentResolver());
Julia Reynolds4a02afb2016-12-13 13:39:52 -050092 when(mContext.getPackageManager()).thenReturn(mPm);
93 when(mContext.getSystemService(eq(Context.TELECOM_SERVICE))).thenReturn(mTm);
94 when(mTm.getDefaultDialerPackage()).thenReturn(callPkg);
95 final ApplicationInfo legacy = new ApplicationInfo();
96 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
97 try {
98 when(mPm.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(legacy);
99 when(mContext.getApplicationInfo()).thenReturn(legacy);
100 } catch (PackageManager.NameNotFoundException e) {
101 // let's hope not
102 }
103
104 smsPkg = Settings.Secure.getString(mContext.getContentResolver(),
105 Settings.Secure.SMS_DEFAULT_APPLICATION);
106
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500107 Notification n1 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500108 .setCategory(Notification.CATEGORY_CALL)
109 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
110 .build();
111 mRecordMinCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
Julia Reynolds924eed12017-01-19 09:52:07 -0500112 callPkg, 1, "minCall", callUid, callUid, n1,
113 new UserHandle(userId), "", 2000), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400114 mRecordMinCall.setSystemImportance(NotificationManager.IMPORTANCE_MIN);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500115
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500116 Notification n2 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500117 .setCategory(Notification.CATEGORY_CALL)
118 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
119 .build();
120 mRecordHighCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
Julia Reynolds924eed12017-01-19 09:52:07 -0500121 callPkg, 1, "highcall", callUid, callUid, n2,
122 new UserHandle(userId), "", 1999), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400123 mRecordHighCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500124
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500125 Notification n3 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500126 .setStyle(new Notification.MediaStyle()
127 .setMediaSession(new MediaSession.Token(null)))
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500128 .build();
129 mRecordDefaultMedia = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500130 pkg2, 1, "media", uid2, uid2, n3, new UserHandle(userId),
131 "", 1499), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400132 mRecordDefaultMedia.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500133
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500134 Notification n4 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500135 .setStyle(new Notification.MessagingStyle("sender!")).build();
136 mRecordInlineReply = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500137 pkg2, 1, "inlinereply", uid2, uid2, n4, new UserHandle(userId),
138 "", 1599), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400139 mRecordInlineReply.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500140 mRecordInlineReply.setPackagePriority(Notification.PRIORITY_MAX);
141
Kristian Monsen05f34792018-04-09 10:27:16 +0200142 if (smsPkg != null) {
143 Notification n5 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
144 .setCategory(Notification.CATEGORY_MESSAGE).build();
145 mRecordSms = new NotificationRecord(mContext, new StatusBarNotification(smsPkg,
146 smsPkg, 1, "sms", smsUid, smsUid, n5, new UserHandle(userId),
147 "", 1299), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400148 mRecordSms.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Kristian Monsen05f34792018-04-09 10:27:16 +0200149 }
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500150
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500151 Notification n6 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500152 mRecordStarredContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500153 pkg2, 1, "starred", uid2, uid2, n6, new UserHandle(userId),
154 "", 1259), getDefaultChannel());
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500155 mRecordStarredContact.setContactAffinity(ValidateNotificationPeople.STARRED_CONTACT);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400156 mRecordStarredContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500157
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500158 Notification n7 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500159 mRecordContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500160 pkg2, 1, "contact", uid2, uid2, n7, new UserHandle(userId),
161 "", 1259), getDefaultChannel());
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500162 mRecordContact.setContactAffinity(ValidateNotificationPeople.VALID_CONTACT);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400163 mRecordContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500164
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500165 Notification n8 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500166 mRecordUrgent = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500167 pkg2, 1, "urgent", uid2, uid2, n8, new UserHandle(userId),
168 "", 1258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400169 mRecordUrgent.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500170
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500171 Notification n9 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500172 .setCategory(Notification.CATEGORY_MESSAGE)
173 .setFlag(Notification.FLAG_ONGOING_EVENT
174 |Notification.FLAG_FOREGROUND_SERVICE, true)
175 .build();
176 mRecordCheater = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500177 pkg2, 1, "cheater", uid2, uid2, n9, new UserHandle(userId),
178 "", 9258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400179 mRecordCheater.setSystemImportance(NotificationManager.IMPORTANCE_LOW);
Julia Reynolds889280c2017-04-12 12:27:00 -0400180 mRecordCheater.setPackagePriority(Notification.PRIORITY_MAX);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500181
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500182 Notification n10 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500183 .setStyle(new Notification.InboxStyle().setSummaryText("message!")).build();
184 mRecordEmail = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500185 pkg2, 1, "email", uid2, uid2, n10, new UserHandle(userId),
186 "", 1599), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400187 mRecordEmail.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds889280c2017-04-12 12:27:00 -0400188
189 Notification n11 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
190 .setCategory(Notification.CATEGORY_MESSAGE)
191 .setColorized(true)
192 .build();
193 mRecordCheaterColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
194 pkg2, 1, "cheater", uid2, uid2, n11, new UserHandle(userId),
195 "", 9258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400196 mRecordCheaterColorized.setSystemImportance(NotificationManager.IMPORTANCE_LOW);
Selim Cinekc6c639cb2017-05-05 19:49:22 -0700197
198 Notification n12 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
199 .setCategory(Notification.CATEGORY_MESSAGE)
200 .setColorized(true)
201 .setStyle(new Notification.MediaStyle())
202 .build();
203 mNoMediaSessionMedia = new NotificationRecord(mContext, new StatusBarNotification(
204 pkg2, pkg2, 1, "cheater", uid2, uid2, n12, new UserHandle(userId),
205 "", 9258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400206 mNoMediaSessionMedia.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500207
208 Notification n13 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
209 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
210 .setColorized(true /* colorized */)
211 .build();
212 mRecordColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
213 pkg2, 1, "colorized", uid2, uid2, n13,
214 new UserHandle(userId), "", 1999), getDefaultChannel());
Gus Prevas2c963d22018-12-03 15:49:06 -0500215 mRecordColorized.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500216
217 Notification n14 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
218 .setCategory(Notification.CATEGORY_CALL)
219 .setColorized(true)
220 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
221 .build();
222 mRecordColorizedCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
223 callPkg, 1, "colorizedCall", callUid, callUid, n14,
224 new UserHandle(userId), "", 1999), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400225 mRecordColorizedCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500226 }
227
228 @Test
Gus Prevas2c963d22018-12-03 15:49:06 -0500229 public void testOrdering() {
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500230 final List<NotificationRecord> expected = new ArrayList<>();
Julia Reynolds472f01c2018-01-18 09:25:44 -0500231 expected.add(mRecordColorizedCall);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500232 expected.add(mRecordColorized);
Gus Prevas2c963d22018-12-03 15:49:06 -0500233 expected.add(mRecordDefaultMedia);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500234 expected.add(mRecordHighCall);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500235 expected.add(mRecordInlineReply);
Kristian Monsen05f34792018-04-09 10:27:16 +0200236 if (mRecordSms != null) {
237 expected.add(mRecordSms);
238 }
Selim Cinek3f146d02017-02-06 13:32:47 -0800239 expected.add(mRecordStarredContact);
240 expected.add(mRecordContact);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500241 expected.add(mRecordEmail);
242 expected.add(mRecordUrgent);
Selim Cinekc6c639cb2017-05-05 19:49:22 -0700243 expected.add(mNoMediaSessionMedia);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500244 expected.add(mRecordCheater);
Julia Reynolds889280c2017-04-12 12:27:00 -0400245 expected.add(mRecordCheaterColorized);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500246 expected.add(mRecordMinCall);
247
248 List<NotificationRecord> actual = new ArrayList<>();
249 actual.addAll(expected);
250 Collections.shuffle(actual);
251
252 Collections.sort(actual, new NotificationComparator(mContext));
253
Gus Prevas2c963d22018-12-03 15:49:06 -0500254 assertThat(actual, contains(expected.toArray()));
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500255 }
256
257 @Test
Gus Prevas2c963d22018-12-03 15:49:06 -0500258 public void testMessaging() {
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500259 NotificationComparator comp = new NotificationComparator(mContext);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500260 assertTrue(comp.isImportantMessaging(mRecordInlineReply));
Kristian Monsen05f34792018-04-09 10:27:16 +0200261 if (mRecordSms != null) {
262 assertTrue(comp.isImportantMessaging(mRecordSms));
263 }
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500264 assertFalse(comp.isImportantMessaging(mRecordEmail));
265 assertFalse(comp.isImportantMessaging(mRecordCheater));
266 }
267
Selim Cinek3f146d02017-02-06 13:32:47 -0800268 @Test
Gus Prevas2c963d22018-12-03 15:49:06 -0500269 public void testPeople() {
Selim Cinek3f146d02017-02-06 13:32:47 -0800270 NotificationComparator comp = new NotificationComparator(mContext);
271 assertTrue(comp.isImportantPeople(mRecordStarredContact));
272 assertTrue(comp.isImportantPeople(mRecordContact));
273 }
274
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500275 private NotificationChannel getDefaultChannel() {
276 return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name",
277 NotificationManager.IMPORTANCE_LOW);
278 }
279}