blob: e15af3dbecc46afb278a8e5705864280161431b7 [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;
Gus Prevas2c963d22018-12-03 15:49:06 -050038import android.telecom.TelecomManager;
Julia Reynolds4a02afb2016-12-13 13:39:52 -050039import android.test.suitebuilder.annotation.SmallTest;
40
Brett Chabot84151d92019-02-27 15:37:59 -080041import androidx.test.runner.AndroidJUnit4;
42
Jason Monk74f5e362017-12-06 08:56:33 -050043import com.android.server.UiServiceTestCase;
44
Julia Reynolds4a02afb2016-12-13 13:39:52 -050045import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48import org.mockito.Mock;
49import org.mockito.MockitoAnnotations;
50
51import java.util.ArrayList;
52import java.util.Collections;
53import java.util.List;
54
55@SmallTest
56@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050057public class NotificationComparatorTest extends UiServiceTestCase {
Julia Reynolds4a02afb2016-12-13 13:39:52 -050058 @Mock Context mContext;
59 @Mock TelecomManager mTm;
60 @Mock RankingHandler handler;
61 @Mock PackageManager mPm;
62
63 private final String callPkg = "com.android.server.notification";
64 private final int callUid = 10;
65 private String smsPkg;
66 private final int smsUid = 11;
67 private final String pkg2 = "pkg2";
68 private final int uid2 = 1111111;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050069 private static final String TEST_CHANNEL_ID = "test_channel_id";
Julia Reynolds4a02afb2016-12-13 13:39:52 -050070
71 private NotificationRecord mRecordMinCall;
72 private NotificationRecord mRecordHighCall;
73 private NotificationRecord mRecordDefaultMedia;
74 private NotificationRecord mRecordEmail;
75 private NotificationRecord mRecordInlineReply;
76 private NotificationRecord mRecordSms;
77 private NotificationRecord mRecordStarredContact;
78 private NotificationRecord mRecordContact;
79 private NotificationRecord mRecordUrgent;
80 private NotificationRecord mRecordCheater;
Julia Reynolds889280c2017-04-12 12:27:00 -040081 private NotificationRecord mRecordCheaterColorized;
Selim Cinekc6c639cb2017-05-05 19:49:22 -070082 private NotificationRecord mNoMediaSessionMedia;
Julia Reynolds472f01c2018-01-18 09:25:44 -050083 private NotificationRecord mRecordColorized;
84 private NotificationRecord mRecordColorizedCall;
Julia Reynolds4a02afb2016-12-13 13:39:52 -050085
86 @Before
87 public void setUp() {
88 MockitoAnnotations.initMocks(this);
89 int userId = UserHandle.myUserId();
90
Chris Wren89aa2262017-05-05 18:05:56 -040091 when(mContext.getResources()).thenReturn(getContext().getResources());
92 when(mContext.getContentResolver()).thenReturn(getContext().getContentResolver());
Julia Reynolds4a02afb2016-12-13 13:39:52 -050093 when(mContext.getPackageManager()).thenReturn(mPm);
94 when(mContext.getSystemService(eq(Context.TELECOM_SERVICE))).thenReturn(mTm);
95 when(mTm.getDefaultDialerPackage()).thenReturn(callPkg);
96 final ApplicationInfo legacy = new ApplicationInfo();
97 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
98 try {
99 when(mPm.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(legacy);
100 when(mContext.getApplicationInfo()).thenReturn(legacy);
101 } catch (PackageManager.NameNotFoundException e) {
102 // let's hope not
103 }
104
105 smsPkg = Settings.Secure.getString(mContext.getContentResolver(),
106 Settings.Secure.SMS_DEFAULT_APPLICATION);
107
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500108 Notification n1 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500109 .setCategory(Notification.CATEGORY_CALL)
110 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
111 .build();
112 mRecordMinCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
Julia Reynolds924eed12017-01-19 09:52:07 -0500113 callPkg, 1, "minCall", callUid, callUid, n1,
114 new UserHandle(userId), "", 2000), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400115 mRecordMinCall.setSystemImportance(NotificationManager.IMPORTANCE_MIN);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500116
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500117 Notification n2 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500118 .setCategory(Notification.CATEGORY_CALL)
119 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
120 .build();
121 mRecordHighCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
Julia Reynolds924eed12017-01-19 09:52:07 -0500122 callPkg, 1, "highcall", callUid, callUid, n2,
123 new UserHandle(userId), "", 1999), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400124 mRecordHighCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500125
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500126 Notification n3 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500127 .setStyle(new Notification.MediaStyle()
128 .setMediaSession(new MediaSession.Token(null)))
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500129 .build();
130 mRecordDefaultMedia = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500131 pkg2, 1, "media", uid2, uid2, n3, new UserHandle(userId),
132 "", 1499), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400133 mRecordDefaultMedia.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500134
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500135 Notification n4 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500136 .setStyle(new Notification.MessagingStyle("sender!")).build();
137 mRecordInlineReply = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500138 pkg2, 1, "inlinereply", uid2, uid2, n4, new UserHandle(userId),
139 "", 1599), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400140 mRecordInlineReply.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500141 mRecordInlineReply.setPackagePriority(Notification.PRIORITY_MAX);
142
Kristian Monsen05f34792018-04-09 10:27:16 +0200143 if (smsPkg != null) {
144 Notification n5 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
145 .setCategory(Notification.CATEGORY_MESSAGE).build();
146 mRecordSms = new NotificationRecord(mContext, new StatusBarNotification(smsPkg,
147 smsPkg, 1, "sms", smsUid, smsUid, n5, new UserHandle(userId),
148 "", 1299), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400149 mRecordSms.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Kristian Monsen05f34792018-04-09 10:27:16 +0200150 }
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500151
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500152 Notification n6 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500153 mRecordStarredContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500154 pkg2, 1, "starred", uid2, uid2, n6, new UserHandle(userId),
155 "", 1259), getDefaultChannel());
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500156 mRecordStarredContact.setContactAffinity(ValidateNotificationPeople.STARRED_CONTACT);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400157 mRecordStarredContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500158
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500159 Notification n7 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500160 mRecordContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500161 pkg2, 1, "contact", uid2, uid2, n7, new UserHandle(userId),
162 "", 1259), getDefaultChannel());
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500163 mRecordContact.setContactAffinity(ValidateNotificationPeople.VALID_CONTACT);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400164 mRecordContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500165
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500166 Notification n8 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500167 mRecordUrgent = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500168 pkg2, 1, "urgent", uid2, uid2, n8, new UserHandle(userId),
169 "", 1258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400170 mRecordUrgent.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500171
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500172 Notification n9 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500173 .setCategory(Notification.CATEGORY_MESSAGE)
174 .setFlag(Notification.FLAG_ONGOING_EVENT
175 |Notification.FLAG_FOREGROUND_SERVICE, true)
176 .build();
177 mRecordCheater = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500178 pkg2, 1, "cheater", uid2, uid2, n9, new UserHandle(userId),
179 "", 9258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400180 mRecordCheater.setSystemImportance(NotificationManager.IMPORTANCE_LOW);
Julia Reynolds889280c2017-04-12 12:27:00 -0400181 mRecordCheater.setPackagePriority(Notification.PRIORITY_MAX);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500182
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500183 Notification n10 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500184 .setStyle(new Notification.InboxStyle().setSummaryText("message!")).build();
185 mRecordEmail = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
Julia Reynolds924eed12017-01-19 09:52:07 -0500186 pkg2, 1, "email", uid2, uid2, n10, new UserHandle(userId),
187 "", 1599), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400188 mRecordEmail.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds889280c2017-04-12 12:27:00 -0400189
190 Notification n11 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
191 .setCategory(Notification.CATEGORY_MESSAGE)
192 .setColorized(true)
193 .build();
194 mRecordCheaterColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
195 pkg2, 1, "cheater", uid2, uid2, n11, new UserHandle(userId),
196 "", 9258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400197 mRecordCheaterColorized.setSystemImportance(NotificationManager.IMPORTANCE_LOW);
Selim Cinekc6c639cb2017-05-05 19:49:22 -0700198
199 Notification n12 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
200 .setCategory(Notification.CATEGORY_MESSAGE)
201 .setColorized(true)
202 .setStyle(new Notification.MediaStyle())
203 .build();
204 mNoMediaSessionMedia = new NotificationRecord(mContext, new StatusBarNotification(
205 pkg2, pkg2, 1, "cheater", uid2, uid2, n12, new UserHandle(userId),
206 "", 9258), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400207 mNoMediaSessionMedia.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500208
209 Notification n13 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
210 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
211 .setColorized(true /* colorized */)
212 .build();
213 mRecordColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
214 pkg2, 1, "colorized", uid2, uid2, n13,
215 new UserHandle(userId), "", 1999), getDefaultChannel());
Gus Prevas2c963d22018-12-03 15:49:06 -0500216 mRecordColorized.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500217
218 Notification n14 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
219 .setCategory(Notification.CATEGORY_CALL)
220 .setColorized(true)
221 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
222 .build();
223 mRecordColorizedCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
224 callPkg, 1, "colorizedCall", callUid, callUid, n14,
225 new UserHandle(userId), "", 1999), getDefaultChannel());
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400226 mRecordColorizedCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500227 }
228
229 @Test
Gus Prevas2c963d22018-12-03 15:49:06 -0500230 public void testOrdering() {
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500231 final List<NotificationRecord> expected = new ArrayList<>();
Julia Reynolds472f01c2018-01-18 09:25:44 -0500232 expected.add(mRecordColorizedCall);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500233 expected.add(mRecordColorized);
Gus Prevas2c963d22018-12-03 15:49:06 -0500234 expected.add(mRecordDefaultMedia);
Julia Reynolds472f01c2018-01-18 09:25:44 -0500235 expected.add(mRecordHighCall);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500236 expected.add(mRecordInlineReply);
Kristian Monsen05f34792018-04-09 10:27:16 +0200237 if (mRecordSms != null) {
238 expected.add(mRecordSms);
239 }
Selim Cinek3f146d02017-02-06 13:32:47 -0800240 expected.add(mRecordStarredContact);
241 expected.add(mRecordContact);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500242 expected.add(mRecordEmail);
243 expected.add(mRecordUrgent);
Selim Cinekc6c639cb2017-05-05 19:49:22 -0700244 expected.add(mNoMediaSessionMedia);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500245 expected.add(mRecordCheater);
Julia Reynolds889280c2017-04-12 12:27:00 -0400246 expected.add(mRecordCheaterColorized);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500247 expected.add(mRecordMinCall);
248
249 List<NotificationRecord> actual = new ArrayList<>();
250 actual.addAll(expected);
251 Collections.shuffle(actual);
252
253 Collections.sort(actual, new NotificationComparator(mContext));
254
Gus Prevas2c963d22018-12-03 15:49:06 -0500255 assertThat(actual, contains(expected.toArray()));
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500256 }
257
258 @Test
Gus Prevas2c963d22018-12-03 15:49:06 -0500259 public void testMessaging() {
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500260 NotificationComparator comp = new NotificationComparator(mContext);
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500261 assertTrue(comp.isImportantMessaging(mRecordInlineReply));
Kristian Monsen05f34792018-04-09 10:27:16 +0200262 if (mRecordSms != null) {
263 assertTrue(comp.isImportantMessaging(mRecordSms));
264 }
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500265 assertFalse(comp.isImportantMessaging(mRecordEmail));
266 assertFalse(comp.isImportantMessaging(mRecordCheater));
267 }
268
Selim Cinek3f146d02017-02-06 13:32:47 -0800269 @Test
Gus Prevas2c963d22018-12-03 15:49:06 -0500270 public void testPeople() {
Selim Cinek3f146d02017-02-06 13:32:47 -0800271 NotificationComparator comp = new NotificationComparator(mContext);
272 assertTrue(comp.isImportantPeople(mRecordStarredContact));
273 assertTrue(comp.isImportantPeople(mRecordContact));
274 }
275
Julia Reynolds4a02afb2016-12-13 13:39:52 -0500276 private NotificationChannel getDefaultChannel() {
277 return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name",
278 NotificationManager.IMPORTANCE_LOW);
279 }
280}