blob: a5fa903c4f0090e3da9d2bfc3621863d6799cb05 [file] [log] [blame]
Julia Reynolds0c299d42016-11-15 14:37:04 -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
Julia Reynolds503ed942017-10-04 16:04:56 -040018import static android.service.notification.NotificationListenerService.Ranking
19 .USER_SENTIMENT_NEGATIVE;
20import static android.service.notification.NotificationListenerService.Ranking
21 .USER_SENTIMENT_NEUTRAL;
22
Julia Reynolds0c299d42016-11-15 14:37:04 -050023import static junit.framework.Assert.assertEquals;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -050024import static junit.framework.Assert.assertFalse;
Julia Reynolds0c299d42016-11-15 14:37:04 -050025import static junit.framework.Assert.assertNotNull;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -050026import static junit.framework.Assert.assertNull;
Julia Reynolds0c299d42016-11-15 14:37:04 -050027import static junit.framework.Assert.assertTrue;
28
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000029import static org.mockito.Matchers.anyInt;
Julia Reynolds0c299d42016-11-15 14:37:04 -050030import static org.mockito.Matchers.eq;
31import static org.mockito.Mockito.when;
32
33import android.app.ActivityManager;
34import android.app.Notification;
35import android.app.Notification.Builder;
36import android.app.NotificationChannel;
37import android.app.NotificationManager;
38import android.content.Context;
39import android.content.pm.ApplicationInfo;
40import android.content.pm.PackageManager;
Julia Reynolds529e3322017-02-06 08:33:01 -050041import android.graphics.Color;
Julia Reynolds0c299d42016-11-15 14:37:04 -050042import android.media.AudioAttributes;
Chris Wrenb3921792017-06-01 13:34:46 -040043import android.metrics.LogMaker;
Julia Reynolds0c299d42016-11-15 14:37:04 -050044import android.net.Uri;
45import android.os.Build;
Julia Reynolds503ed942017-10-04 16:04:56 -040046import android.os.Bundle;
Julia Reynolds0c299d42016-11-15 14:37:04 -050047import android.os.UserHandle;
48import android.provider.Settings;
Julia Reynolds503ed942017-10-04 16:04:56 -040049import android.service.notification.Adjustment;
Julia Reynolds0c299d42016-11-15 14:37:04 -050050import android.service.notification.StatusBarNotification;
Julia Reynolds0c299d42016-11-15 14:37:04 -050051import android.support.test.runner.AndroidJUnit4;
52import android.test.suitebuilder.annotation.SmallTest;
53
54
Chris Wrenb3921792017-06-01 13:34:46 -040055import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk74f5e362017-12-06 08:56:33 -050056import com.android.server.UiServiceTestCase;
Chris Wrenb3921792017-06-01 13:34:46 -040057
Julia Reynolds0c299d42016-11-15 14:37:04 -050058import org.junit.Before;
59import org.junit.Test;
60import org.junit.runner.RunWith;
61import org.mockito.Mock;
62import org.mockito.Mockito;
63import org.mockito.MockitoAnnotations;
64
65import java.util.Objects;
66
67@SmallTest
68@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050069public class NotificationRecordTest extends UiServiceTestCase {
Julia Reynolds0c299d42016-11-15 14:37:04 -050070
71 private final Context mMockContext = Mockito.mock(Context.class);
72 @Mock PackageManager mPm;
73
74 private final String pkg = "com.android.server.notification";
Julia Reynolds19ad7472016-12-12 08:51:08 -050075 private final int uid = 9583;
Julia Reynolds0c299d42016-11-15 14:37:04 -050076 private final String pkg2 = "pkg2";
77 private final int uid2 = 1111111;
78 private final int id1 = 1;
79 private final int id2 = 2;
80 private final String tag1 = "tag1";
81 private final String tag2 = "tag2";
82 private final String channelId = "channel";
83 NotificationChannel channel =
84 new NotificationChannel(channelId, "test", NotificationManager.IMPORTANCE_DEFAULT);
Chris Wrenb3921792017-06-01 13:34:46 -040085 private final String channelIdLong =
86 "give_a_developer_a_string_argument_and_who_knows_what_they_will_pass_in_there";
87 final String groupId = "group";
88 final String groupIdOverride = "other_group";
89 private final String groupIdLong =
90 "0|com.foo.bar|g:content://com.foo.bar.ui/account%3A-0000000/account/";
91 NotificationChannel channelLongId =
92 new NotificationChannel(channelIdLong, "long", NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds0c299d42016-11-15 14:37:04 -050093 NotificationChannel defaultChannel =
94 new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "test",
95 NotificationManager.IMPORTANCE_UNSPECIFIED);
96 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000097 final ApplicationInfo legacy = new ApplicationInfo();
Julia Reynolds0c299d42016-11-15 14:37:04 -050098 final ApplicationInfo upgrade = new ApplicationInfo();
99
Julia Reynolds0c299d42016-11-15 14:37:04 -0500100 private static final long[] CUSTOM_VIBRATION = new long[] {
101 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
102 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
103 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
Julia Reynoldsf57de462016-11-23 11:31:46 -0500104 private static final long[] CUSTOM_CHANNEL_VIBRATION = new long[] {300, 400, 300, 400 };
Julia Reynolds0c299d42016-11-15 14:37:04 -0500105 private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI;
106 private static final AudioAttributes CUSTOM_ATTRIBUTES = new AudioAttributes.Builder()
107 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
108 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
109 .build();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500110 private static final NotificationRecord.Light CUSTOM_LIGHT =
111 new NotificationRecord.Light(1, 2, 3);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500112
113 @Before
114 public void setUp() {
115 MockitoAnnotations.initMocks(this);
116
Chris Wren89aa2262017-05-05 18:05:56 -0400117 when(mMockContext.getResources()).thenReturn(getContext().getResources());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500118 when(mMockContext.getPackageManager()).thenReturn(mPm);
119
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000120 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
Dan Sandlere103b782017-05-17 16:07:56 -0700121 upgrade.targetSdkVersion = Build.VERSION_CODES.O;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000122 try {
123 when(mPm.getApplicationInfoAsUser(eq(pkg), anyInt(), anyInt())).thenReturn(legacy);
124 when(mPm.getApplicationInfoAsUser(eq(pkg2), anyInt(), anyInt())).thenReturn(upgrade);
125 } catch (PackageManager.NameNotFoundException e) {}
Julia Reynolds0c299d42016-11-15 14:37:04 -0500126 }
127
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000128 private StatusBarNotification getNotification(boolean preO, boolean noisy, boolean defaultSound,
Chris Wrenb3921792017-06-01 13:34:46 -0400129 boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights,
130 String group) {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000131 when(mMockContext.getApplicationInfo()).thenReturn(preO ? legacy : upgrade);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500132 final Builder builder = new Builder(mMockContext)
133 .setContentTitle("foo")
134 .setSmallIcon(android.R.drawable.sym_def_app_icon)
135 .setPriority(Notification.PRIORITY_HIGH);
136
137 int defaults = 0;
138 if (noisy) {
139 if (defaultSound) {
140 defaults |= Notification.DEFAULT_SOUND;
141 } else {
142 builder.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500143 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500144 }
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400145 } else {
146 channel.setSound(null, null);
147 builder.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500148 }
149 if (buzzy) {
150 if (defaultVibration) {
151 defaults |= Notification.DEFAULT_VIBRATE;
152 } else {
153 builder.setVibrate(CUSTOM_VIBRATION);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500154 channel.setVibrationPattern(CUSTOM_CHANNEL_VIBRATION);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500155 }
156 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500157 if (lights) {
158 if (defaultLights) {
159 defaults |= Notification.DEFAULT_LIGHTS;
160 } else {
161 builder.setLights(CUSTOM_LIGHT.color, CUSTOM_LIGHT.onMs, CUSTOM_LIGHT.offMs);
Julia Reynolds529e3322017-02-06 08:33:01 -0500162 channel.setLightColor(Color.BLUE);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500163 }
Julia Reynolds529e3322017-02-06 08:33:01 -0500164 channel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500165 } else {
Julia Reynolds529e3322017-02-06 08:33:01 -0500166 channel.enableLights(false);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500167 }
168
Julia Reynolds0c299d42016-11-15 14:37:04 -0500169 builder.setDefaults(defaults);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000170 if (!preO) {
Julia Reynoldsbad42972017-04-25 13:52:49 -0400171 builder.setChannelId(channelId);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500172 }
173
Chris Wrenb3921792017-06-01 13:34:46 -0400174 if(group != null) {
175 builder.setGroup(group);
176 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500177
178 Notification n = builder.build();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000179 if (preO) {
Julia Reynolds924eed12017-01-19 09:52:07 -0500180 return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n,
Julia Reynolds0c299d42016-11-15 14:37:04 -0500181 mUser, null, uid);
182 } else {
Julia Reynolds924eed12017-01-19 09:52:07 -0500183 return new StatusBarNotification(pkg2, pkg2, id2, tag2, uid2, uid2, n,
Julia Reynolds0c299d42016-11-15 14:37:04 -0500184 mUser, null, uid2);
185 }
186 }
187
188 //
189 // Tests
190 //
191
192 @Test
193 public void testSound_default_preUpgradeUsesNotification() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500194 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500195 // pre upgrade, default sound.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000196 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500197 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400198 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500199
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000200 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500201 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500202 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500203 }
204
205 @Test
206 public void testSound_custom_preUpgradeUsesNotification() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500207 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500208 // pre upgrade, custom sound.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000209 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500210 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400211 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500212
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000213 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500214 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500215 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500216 }
217
218 @Test
219 public void testSound_default_userLocked_preUpgrade() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500220 defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500221 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
222 // pre upgrade, default sound.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000223 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500224 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400225 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500226
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000227 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500228 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500229 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500230 }
231
232 @Test
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400233 public void testSound_noSound_preUpgrade() throws Exception {
234 // pre upgrade, default sound.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000235 StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400236 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400237 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400238
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000239 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400240 assertEquals(null, record.getSound());
241 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
242 }
243
244 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500245 public void testSound_default_upgradeUsesChannel() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500246 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500247 // post upgrade, default sound.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000248 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500249 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400250 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500251
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000252 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500253 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500254 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500255 }
256
257 @Test
258 public void testVibration_default_preUpgradeUsesNotification() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500259 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500260 // pre upgrade, default vibration.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000261 StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500262 false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400263 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500264
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000265 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500266 assertNotNull(record.getVibration());
267 }
268
269 @Test
270 public void testVibration_custom_preUpgradeUsesNotification() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500271 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500272 // pre upgrade, custom vibration.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000273 StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500274 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400275 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500276
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000277 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500278 assertEquals(CUSTOM_VIBRATION, record.getVibration());
279 }
280
281 @Test
282 public void testVibration_custom_userLocked_preUpgrade() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500283 defaultChannel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500284 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
285 // pre upgrade, custom vibration.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000286 StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500287 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400288 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500289
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000290 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500291 assertTrue(!Objects.equals(CUSTOM_VIBRATION, record.getVibration()));
292 }
293
294 @Test
295 public void testVibration_custom_upgradeUsesChannel() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500296 channel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500297 // post upgrade, custom vibration.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000298 StatusBarNotification sbn = getNotification(false /*preO */, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500299 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400300 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500301
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000302 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500303 assertEquals(CUSTOM_CHANNEL_VIBRATION, record.getVibration());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500304 }
305
306 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500307 public void testImportance_preUpgrade() throws Exception {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000308 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500309 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400310 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000311 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500312 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
313 }
314
315 @Test
316 public void testImportance_locked_preUpgrade() throws Exception {
317 defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
318 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000319 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500320 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400321 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500322
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000323 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500324 assertEquals(NotificationManager.IMPORTANCE_LOW, record.getImportance());
325 }
326
327 @Test
Julia Reynoldsa917a112017-03-21 11:09:14 -0400328 public void testImportance_locked_unspecified_preUpgrade() throws Exception {
329 defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
330 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000331 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa917a112017-03-21 11:09:14 -0400332 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400333 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400334
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000335 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400336 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
337 }
338
339 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500340 public void testImportance_upgrade() throws Exception {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000341 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500342 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400343 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000344 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500345 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, record.getImportance());
346 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500347
348 @Test
349 public void testLights_preUpgrade_noLight() throws Exception {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000350 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500351 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400352 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000353 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500354 assertNull(record.getLight());
355 }
356
357
358 @Test
359 public void testLights_preUpgrade() throws Exception {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000360 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500361 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400362 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000363 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500364 assertEquals(CUSTOM_LIGHT, record.getLight());
365 }
366
367 @Test
368 public void testLights_locked_preUpgrade() throws Exception {
Julia Reynolds529e3322017-02-06 08:33:01 -0500369 defaultChannel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500370 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000371 StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500372 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400373 true /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500374
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000375 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500376 assertFalse(CUSTOM_LIGHT.equals(record.getLight()));
377 }
378
379 @Test
Julia Reynolds529e3322017-02-06 08:33:01 -0500380 public void testLights_upgrade_defaultLights() throws Exception {
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500381 int defaultLightColor = mMockContext.getResources().getColor(
382 com.android.internal.R.color.config_defaultNotificationColor);
383 int defaultLightOn = mMockContext.getResources().getInteger(
384 com.android.internal.R.integer.config_defaultNotificationLedOn);
385 int defaultLightOff = mMockContext.getResources().getInteger(
386 com.android.internal.R.integer.config_defaultNotificationLedOff);
387
388 NotificationRecord.Light expected = new NotificationRecord.Light(
389 defaultLightColor, defaultLightOn, defaultLightOff);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000390 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500391 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400392 true /* lights */, true /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000393 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds529e3322017-02-06 08:33:01 -0500394 assertEquals(expected, record.getLight());
395 }
396
397 @Test
398 public void testLights_upgrade() throws Exception {
399 int defaultLightOn = mMockContext.getResources().getInteger(
400 com.android.internal.R.integer.config_defaultNotificationLedOn);
401 int defaultLightOff = mMockContext.getResources().getInteger(
402 com.android.internal.R.integer.config_defaultNotificationLedOff);
403
404 NotificationRecord.Light expected = new NotificationRecord.Light(
405 Color.BLUE, defaultLightOn, defaultLightOff);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000406 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
Julia Reynolds529e3322017-02-06 08:33:01 -0500407 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400408 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000409 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 assertEquals(expected, record.getLight());
411 }
412
413 @Test
414 public void testLights_upgrade_noLight() throws Exception {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000415 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500416 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400417 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000418 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 assertNull(record.getLight());
420 }
Chris Wrenb3921792017-06-01 13:34:46 -0400421
422 @Test
423 public void testLogmakerShortChannel() throws Exception {
424 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
425 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
426 false /* lights */, false /* defaultLights */, null /* group */);
427 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
428 final LogMaker logMaker = record.getLogMaker();
429 assertEquals(channelId,
430 (String) logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
431 assertEquals(channel.getImportance(),
432 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
433 }
434
435 @Test
436 public void testLogmakerLongChannel() throws Exception {
437 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
438 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
439 false /* lights */, false /*defaultLights */, null /* group */);
440 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channelLongId);
441 final String loggedId = (String)
442 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID);
443 assertEquals(channelIdLong.substring(0,10), loggedId.substring(0, 10));
444 }
445
446 @Test
447 public void testLogmakerNoGroup() throws Exception {
448 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
449 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
450 false /* lights */, false /*defaultLights */, null /* group */);
451 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
452 assertNull(record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
453 }
454
455 @Test
456 public void testLogmakerShortGroup() throws Exception {
457 StatusBarNotification sbn = getNotification(false /*reO */, true /* noisy */,
458 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
459 false /* lights */, false /* defaultLights */, groupId /* group */);
460 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
461 assertEquals(groupId,
462 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
463 }
464
465 @Test
466 public void testLogmakerLongGroup() throws Exception {
467 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
468 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
469 false /* lights */, false /* defaultLights */, groupIdLong /* group */);
470 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
471 final String loggedId = (String)
472 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID);
473 assertEquals(groupIdLong.substring(0,10), loggedId.substring(0, 10));
474 }
475
476 @Test
477 public void testLogmakerOverrideGroup() throws Exception {
478 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
479 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
480 false /* lights */, false /* defaultLights */, groupId /* group */);
481 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
482 assertEquals(groupId,
483 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
484 record.setOverrideGroupKey(groupIdOverride);
485 assertEquals(groupIdOverride,
486 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
487 record.setOverrideGroupKey(null);
488 assertEquals(groupId,
489 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
490 }
491
Julia Reynolds503ed942017-10-04 16:04:56 -0400492 @Test
493 public void testNotificationStats() throws Exception {
494 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
495 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
496 false /* lights */, false /* defaultLights */, groupId /* group */);
497 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Chris Wrenb3921792017-06-01 13:34:46 -0400498
Julia Reynolds503ed942017-10-04 16:04:56 -0400499 assertFalse(record.getStats().hasSeen());
500 assertFalse(record.isSeen());
501 assertFalse(record.getStats().hasDirectReplied());
502 assertFalse(record.getStats().hasExpanded());
503 assertFalse(record.getStats().hasInteracted());
504 assertFalse(record.getStats().hasViewedSettings());
505 assertFalse(record.getStats().hasSnoozed());
506
507 record.setSeen();
508 assertTrue(record.getStats().hasSeen());
509 assertTrue(record.isSeen());
510 assertFalse(record.getStats().hasDirectReplied());
511 assertFalse(record.getStats().hasExpanded());
512 assertFalse(record.getStats().hasInteracted());
513 assertFalse(record.getStats().hasViewedSettings());
514 assertFalse(record.getStats().hasSnoozed());
515
516 record.recordViewedSettings();
517 assertFalse(record.getStats().hasDirectReplied());
518 assertFalse(record.getStats().hasExpanded());
519 assertTrue(record.getStats().hasViewedSettings());
520 assertFalse(record.getStats().hasSnoozed());
521
522 record.recordSnoozed();
523 assertFalse(record.getStats().hasDirectReplied());
524 assertFalse(record.getStats().hasExpanded());
525 assertTrue(record.getStats().hasSnoozed());
526
527 record.recordExpanded();
528 assertFalse(record.getStats().hasDirectReplied());
529 assertTrue(record.getStats().hasExpanded());
530
531 record.recordDirectReplied();
532 assertTrue(record.getStats().hasDirectReplied());
533 }
534
535 @Test
536 public void testUserSentiment() throws Exception {
537 StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */,
538 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
539 false /* lights */, false /* defaultLights */, groupId /* group */);
540 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
541
542 assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment());
543
544 Bundle signals = new Bundle();
545 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
546 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
547
548 record.applyAdjustments();
549
550 assertEquals(USER_SENTIMENT_NEGATIVE, record.getUserSentiment());
551 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500552}