blob: b9ae7d5e3f9377f89b4dfdb8daa388bf85322462 [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 Reynoldsc65656a2018-02-12 09:55:14 -050018import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
Julia Reynoldsefcdff42018-08-09 09:42:56 -040019import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
20import static android.app.NotificationManager.IMPORTANCE_HIGH;
21import static android.app.NotificationManager.IMPORTANCE_LOW;
Gus Prevas7306b902018-12-11 10:57:06 -050022import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE;
23import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL;
24import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_POSITIVE;
Julia Reynolds503ed942017-10-04 16:04:56 -040025
Julia Reynolds0c299d42016-11-15 14:37:04 -050026import static junit.framework.Assert.assertEquals;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -050027import static junit.framework.Assert.assertFalse;
Julia Reynolds0c299d42016-11-15 14:37:04 -050028import static junit.framework.Assert.assertNotNull;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -050029import static junit.framework.Assert.assertNull;
Julia Reynolds0c299d42016-11-15 14:37:04 -050030import static junit.framework.Assert.assertTrue;
31
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040032import static org.junit.Assert.assertNotEquals;
Julia Reynolds218871e2018-06-13 10:45:21 -040033import static org.junit.Assert.fail;
34import static org.mockito.ArgumentMatchers.any;
35import static org.mockito.ArgumentMatchers.anyInt;
36import static org.mockito.ArgumentMatchers.eq;
37import static org.mockito.Mockito.mock;
Julia Reynolds0c299d42016-11-15 14:37:04 -050038import static org.mockito.Mockito.when;
39
40import android.app.ActivityManager;
Julia Reynolds218871e2018-06-13 10:45:21 -040041import android.app.IActivityManager;
Julia Reynolds0c299d42016-11-15 14:37:04 -050042import android.app.Notification;
43import android.app.Notification.Builder;
44import android.app.NotificationChannel;
45import android.app.NotificationManager;
46import android.content.Context;
47import android.content.pm.ApplicationInfo;
48import android.content.pm.PackageManager;
Julia Reynolds529e3322017-02-06 08:33:01 -050049import android.graphics.Color;
Tony Mak628cb932018-06-19 18:30:41 +010050import android.graphics.drawable.Icon;
Julia Reynolds0c299d42016-11-15 14:37:04 -050051import android.media.AudioAttributes;
Chris Wrenb3921792017-06-01 13:34:46 -040052import android.metrics.LogMaker;
Julia Reynolds0c299d42016-11-15 14:37:04 -050053import android.net.Uri;
Julia Reynolds503ed942017-10-04 16:04:56 -040054import android.os.Bundle;
Julia Reynolds0c299d42016-11-15 14:37:04 -050055import android.os.UserHandle;
56import android.provider.Settings;
Julia Reynolds503ed942017-10-04 16:04:56 -040057import android.service.notification.Adjustment;
Julia Reynolds0c299d42016-11-15 14:37:04 -050058import android.service.notification.StatusBarNotification;
Eyal Posenera9cf9c72018-12-18 16:23:54 +020059import android.support.test.filters.SmallTest;
Julia Reynolds0c299d42016-11-15 14:37:04 -050060import android.support.test.runner.AndroidJUnit4;
Julia Reynolds0c299d42016-11-15 14:37:04 -050061
Tony Mak628cb932018-06-19 18:30:41 +010062import com.android.internal.R;
Chris Wrenb3921792017-06-01 13:34:46 -040063import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk74f5e362017-12-06 08:56:33 -050064import com.android.server.UiServiceTestCase;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070065import com.android.server.uri.UriGrantsManagerInternal;
Chris Wrenb3921792017-06-01 13:34:46 -040066
Julia Reynolds0c299d42016-11-15 14:37:04 -050067import org.junit.Before;
68import org.junit.Test;
69import org.junit.runner.RunWith;
70import org.mockito.Mock;
Julia Reynolds0c299d42016-11-15 14:37:04 -050071import org.mockito.MockitoAnnotations;
72
Tony Mak628cb932018-06-19 18:30:41 +010073import java.util.ArrayList;
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040074import java.util.Arrays;
Julia Reynolds0c299d42016-11-15 14:37:04 -050075
76@SmallTest
77@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050078public class NotificationRecordTest extends UiServiceTestCase {
Julia Reynolds0c299d42016-11-15 14:37:04 -050079
Julia Reynolds218871e2018-06-13 10:45:21 -040080 private final Context mMockContext = mock(Context.class);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040081 @Mock private PackageManager mPm;
Julia Reynolds0c299d42016-11-15 14:37:04 -050082
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060083 private final String pkg = PKG_N_MR1;
Julia Reynolds19ad7472016-12-12 08:51:08 -050084 private final int uid = 9583;
Julia Reynolds0c299d42016-11-15 14:37:04 -050085 private final int id1 = 1;
Julia Reynolds0c299d42016-11-15 14:37:04 -050086 private final String tag1 = "tag1";
Julia Reynolds0c299d42016-11-15 14:37:04 -050087 private final String channelId = "channel";
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040088 private NotificationChannel channel =
Julia Reynolds0c299d42016-11-15 14:37:04 -050089 new NotificationChannel(channelId, "test", NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040090 private final String groupId = "group";
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040091 private NotificationChannel defaultChannel =
Julia Reynolds0c299d42016-11-15 14:37:04 -050092 new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "test",
93 NotificationManager.IMPORTANCE_UNSPECIFIED);
94 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
Julia Reynolds0c299d42016-11-15 14:37:04 -050095
Julia Reynolds0c299d42016-11-15 14:37:04 -050096 private static final long[] CUSTOM_VIBRATION = new long[] {
97 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
98 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
99 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
Julia Reynoldsf57de462016-11-23 11:31:46 -0500100 private static final long[] CUSTOM_CHANNEL_VIBRATION = new long[] {300, 400, 300, 400 };
Julia Reynolds0c299d42016-11-15 14:37:04 -0500101 private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI;
102 private static final AudioAttributes CUSTOM_ATTRIBUTES = new AudioAttributes.Builder()
103 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
104 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
105 .build();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500106 private static final NotificationRecord.Light CUSTOM_LIGHT =
107 new NotificationRecord.Light(1, 2, 3);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500108
109 @Before
110 public void setUp() {
111 MockitoAnnotations.initMocks(this);
112
Chris Wren89aa2262017-05-05 18:05:56 -0400113 when(mMockContext.getResources()).thenReturn(getContext().getResources());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500114 when(mMockContext.getPackageManager()).thenReturn(mPm);
Julia Reynolds218871e2018-06-13 10:45:21 -0400115 when(mMockContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500116 }
117
Julia Reynolds218871e2018-06-13 10:45:21 -0400118 private StatusBarNotification getNotification(String pkg, boolean noisy, boolean defaultSound,
Chris Wrenb3921792017-06-01 13:34:46 -0400119 boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights,
120 String group) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500121 final Builder builder = new Builder(mMockContext)
122 .setContentTitle("foo")
123 .setSmallIcon(android.R.drawable.sym_def_app_icon)
124 .setPriority(Notification.PRIORITY_HIGH);
125
126 int defaults = 0;
127 if (noisy) {
128 if (defaultSound) {
129 defaults |= Notification.DEFAULT_SOUND;
130 } else {
131 builder.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500132 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500133 }
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400134 } else {
135 channel.setSound(null, null);
136 builder.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500137 }
138 if (buzzy) {
139 if (defaultVibration) {
140 defaults |= Notification.DEFAULT_VIBRATE;
141 } else {
142 builder.setVibrate(CUSTOM_VIBRATION);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500143 channel.setVibrationPattern(CUSTOM_CHANNEL_VIBRATION);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500144 }
145 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500146 if (lights) {
147 if (defaultLights) {
148 defaults |= Notification.DEFAULT_LIGHTS;
149 } else {
150 builder.setLights(CUSTOM_LIGHT.color, CUSTOM_LIGHT.onMs, CUSTOM_LIGHT.offMs);
Julia Reynolds529e3322017-02-06 08:33:01 -0500151 channel.setLightColor(Color.BLUE);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500152 }
Julia Reynolds529e3322017-02-06 08:33:01 -0500153 channel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500154 } else {
Julia Reynolds529e3322017-02-06 08:33:01 -0500155 channel.enableLights(false);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500156 }
157
Julia Reynolds0c299d42016-11-15 14:37:04 -0500158 builder.setDefaults(defaults);
Julia Reynolds218871e2018-06-13 10:45:21 -0400159 builder.setChannelId(channelId);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500160
Chris Wrenb3921792017-06-01 13:34:46 -0400161 if(group != null) {
162 builder.setGroup(group);
163 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500164
165 Notification n = builder.build();
Julia Reynolds218871e2018-06-13 10:45:21 -0400166 return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n, mUser, null, uid);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500167 }
168
169 //
170 // Tests
171 //
172
173 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400174 public void testSound_default_preUpgradeUsesNotification() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500175 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500176 // pre upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400177 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500178 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400179 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500180
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000181 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500182 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500183 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500184 }
185
186 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400187 public void testSound_custom_preUpgradeUsesNotification() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500188 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500189 // pre upgrade, custom sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400190 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500191 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400192 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500193
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000194 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500195 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500196 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500197 }
198
199 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400200 public void testSound_default_userLocked_preUpgrade() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500201 defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500202 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
203 // pre upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400204 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500205 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400206 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500207
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000208 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500209 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500210 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500211 }
212
213 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400214 public void testSound_noSound_preUpgrade() {
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400215 // pre upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400216 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400217 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400218 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400219
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000220 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400221 assertNull(record.getSound());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400222 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
223 }
224
225 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400226 public void testSound_default_upgradeUsesChannel() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500227 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500228 // post upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400229 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500230 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400231 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500232
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000233 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500234 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500235 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500236 }
237
238 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400239 public void testVibration_default_preUpgradeUsesNotification() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500240 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500241 // pre upgrade, default vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400242 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500243 false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400244 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500245
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000246 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500247 assertNotNull(record.getVibration());
248 }
249
250 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400251 public void testVibration_custom_preUpgradeUsesNotification() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500252 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500253 // pre upgrade, custom vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400254 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500255 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400256 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500257
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000258 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500259 assertEquals(CUSTOM_VIBRATION, record.getVibration());
260 }
261
262 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400263 public void testVibration_custom_userLocked_preUpgrade() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500264 defaultChannel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500265 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
266 // pre upgrade, custom vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400267 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500268 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400269 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500270
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000271 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400272 assertTrue(!Arrays.equals(CUSTOM_VIBRATION, record.getVibration()));
Julia Reynolds0c299d42016-11-15 14:37:04 -0500273 }
274
275 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400276 public void testVibration_custom_upgradeUsesChannel() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500277 channel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500278 // post upgrade, custom vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400279 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500280 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400281 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500282
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000283 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500284 assertEquals(CUSTOM_CHANNEL_VIBRATION, record.getVibration());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500285 }
286
287 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400288 public void testImportance_preUpgrade() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400289 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500290 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400291 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000292 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500293 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
294 }
295
296 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400297 public void testImportance_locked_preUpgrade() {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400298 defaultChannel.setImportance(IMPORTANCE_LOW);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500299 defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds218871e2018-06-13 10:45:21 -0400300 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500301 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400302 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500303
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000304 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400305 assertEquals(IMPORTANCE_LOW, record.getImportance());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500306 }
307
308 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400309 public void testImportance_locked_unspecified_preUpgrade() {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400310 defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500311 defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds218871e2018-06-13 10:45:21 -0400312 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa917a112017-03-21 11:09:14 -0400313 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400314 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400315
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000316 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400317 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
318 }
319
320 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400321 public void testImportance_upgrade() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400322 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500323 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400324 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000325 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500326 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, record.getImportance());
327 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500328
329 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400330 public void testLights_preUpgrade_noLight() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400331 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500332 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400333 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000334 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500335 assertNull(record.getLight());
336 }
337
338
339 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400340 public void testLights_preUpgrade() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400341 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500342 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400343 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000344 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500345 assertEquals(CUSTOM_LIGHT, record.getLight());
346 }
347
348 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400349 public void testLights_locked_preUpgrade() {
Julia Reynolds529e3322017-02-06 08:33:01 -0500350 defaultChannel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500351 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
Julia Reynolds218871e2018-06-13 10:45:21 -0400352 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500353 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400354 true /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500355
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000356 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400357 assertNotEquals(CUSTOM_LIGHT, record.getLight());
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500358 }
359
360 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400361 public void testLights_upgrade_defaultLights() {
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500362 int defaultLightColor = mMockContext.getResources().getColor(
363 com.android.internal.R.color.config_defaultNotificationColor);
364 int defaultLightOn = mMockContext.getResources().getInteger(
365 com.android.internal.R.integer.config_defaultNotificationLedOn);
366 int defaultLightOff = mMockContext.getResources().getInteger(
367 com.android.internal.R.integer.config_defaultNotificationLedOff);
368
369 NotificationRecord.Light expected = new NotificationRecord.Light(
370 defaultLightColor, defaultLightOn, defaultLightOff);
Julia Reynolds218871e2018-06-13 10:45:21 -0400371 StatusBarNotification sbn = getNotification(PKG_O, 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 */, true /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000374 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds529e3322017-02-06 08:33:01 -0500375 assertEquals(expected, record.getLight());
376 }
377
378 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400379 public void testLights_upgrade() {
Julia Reynolds529e3322017-02-06 08:33:01 -0500380 int defaultLightOn = mMockContext.getResources().getInteger(
381 com.android.internal.R.integer.config_defaultNotificationLedOn);
382 int defaultLightOff = mMockContext.getResources().getInteger(
383 com.android.internal.R.integer.config_defaultNotificationLedOff);
384
385 NotificationRecord.Light expected = new NotificationRecord.Light(
386 Color.BLUE, defaultLightOn, defaultLightOff);
Julia Reynolds218871e2018-06-13 10:45:21 -0400387 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds529e3322017-02-06 08:33:01 -0500388 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400389 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000390 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500391 assertEquals(expected, record.getLight());
392 }
393
394 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400395 public void testLights_upgrade_noLight() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400396 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500397 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400398 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000399 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500400 assertNull(record.getLight());
401 }
Chris Wrenb3921792017-06-01 13:34:46 -0400402
403 @Test
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200404 public void testLogMaker() {
405 long timestamp = 1000L;
Julia Reynolds218871e2018-06-13 10:45:21 -0400406 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400407 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
408 false /* lights */, false /* defaultLights */, null /* group */);
409 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200410 final LogMaker logMaker = record.getLogMaker(timestamp);
411
412 assertNull(logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX));
Chris Wrenb3921792017-06-01 13:34:46 -0400413 assertEquals(channelId,
414 (String) logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
415 assertEquals(channel.getImportance(),
416 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200417 assertEquals(record.getLifespanMs(timestamp),
418 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS));
419 assertEquals(record.getFreshnessMs(timestamp),
420 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS));
421 assertEquals(record.getExposureMs(timestamp),
422 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS));
423 assertEquals(record.getInterruptionMs(timestamp),
424 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS));
Chris Wrenb3921792017-06-01 13:34:46 -0400425 }
426
Julia Reynolds503ed942017-10-04 16:04:56 -0400427 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400428 public void testNotificationStats() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400429 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds503ed942017-10-04 16:04:56 -0400430 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
431 false /* lights */, false /* defaultLights */, groupId /* group */);
432 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Chris Wrenb3921792017-06-01 13:34:46 -0400433
Julia Reynolds503ed942017-10-04 16:04:56 -0400434 assertFalse(record.getStats().hasSeen());
435 assertFalse(record.isSeen());
436 assertFalse(record.getStats().hasDirectReplied());
437 assertFalse(record.getStats().hasExpanded());
438 assertFalse(record.getStats().hasInteracted());
439 assertFalse(record.getStats().hasViewedSettings());
440 assertFalse(record.getStats().hasSnoozed());
441
442 record.setSeen();
443 assertTrue(record.getStats().hasSeen());
444 assertTrue(record.isSeen());
445 assertFalse(record.getStats().hasDirectReplied());
446 assertFalse(record.getStats().hasExpanded());
447 assertFalse(record.getStats().hasInteracted());
448 assertFalse(record.getStats().hasViewedSettings());
449 assertFalse(record.getStats().hasSnoozed());
450
451 record.recordViewedSettings();
452 assertFalse(record.getStats().hasDirectReplied());
453 assertFalse(record.getStats().hasExpanded());
454 assertTrue(record.getStats().hasViewedSettings());
455 assertFalse(record.getStats().hasSnoozed());
456
457 record.recordSnoozed();
458 assertFalse(record.getStats().hasDirectReplied());
459 assertFalse(record.getStats().hasExpanded());
460 assertTrue(record.getStats().hasSnoozed());
461
462 record.recordExpanded();
463 assertFalse(record.getStats().hasDirectReplied());
464 assertTrue(record.getStats().hasExpanded());
465
466 record.recordDirectReplied();
467 assertTrue(record.getStats().hasDirectReplied());
468 }
469
470 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400471 public void testUserSentiment() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400472 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds503ed942017-10-04 16:04:56 -0400473 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
474 false /* lights */, false /* defaultLights */, groupId /* group */);
475 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
476
477 assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment());
478
479 Bundle signals = new Bundle();
480 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
481 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
482
483 record.applyAdjustments();
484
485 assertEquals(USER_SENTIMENT_NEGATIVE, record.getUserSentiment());
486 }
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500487
488 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400489 public void testUserSentiment_appImportanceUpdatesSentiment() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400490 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400491 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
492 false /* lights */, false /* defaultLights */, groupId /* group */);
493 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
494 assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment());
495
496 record.setIsAppImportanceLocked(true);
497 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
498 }
499
500 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400501 public void testUserSentiment_appImportanceBlocksNegativeSentimentUpdate() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400502 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400503 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
504 false /* lights */, false /* defaultLights */, groupId /* group */);
505 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
506 record.setIsAppImportanceLocked(true);
507
508 Bundle signals = new Bundle();
509 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
510 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
511 record.applyAdjustments();
512
513 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
514 }
515
516 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400517 public void testUserSentiment_userLocked() {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500518 channel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds218871e2018-06-13 10:45:21 -0400519 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500520 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
521 false /* lights */, false /* defaultLights */, groupId /* group */);
522 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
523
524 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
525
526 Bundle signals = new Bundle();
527 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
528 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
529
530 record.applyAdjustments();
531
532 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
533 }
Rohan Shah590e1b22018-04-10 23:48:47 -0400534
535 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400536 public void testAppImportance_returnsCorrectly() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400537 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400538 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
539 false /* lights */, false /* defaultLights */, groupId /* group */);
540 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
541
542 record.setIsAppImportanceLocked(true);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400543 assertTrue(record.getIsAppImportanceLocked());
Rohan Shah590e1b22018-04-10 23:48:47 -0400544
545 record.setIsAppImportanceLocked(false);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400546 assertFalse(record.getIsAppImportanceLocked());
Rohan Shah590e1b22018-04-10 23:48:47 -0400547 }
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400548
549 @Test
550 public void testIsInterruptive_textChanged_notSeen() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400551 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400552 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
553 false /* lights */, false /* defaultLights */, null /* group */);
554 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
555
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400556 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400557
558 record.setTextChanged(true);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400559 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400560 }
561
562 @Test
563 public void testIsInterruptive_textChanged_seen() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400564 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400565 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
566 false /* lights */, false /* defaultLights */, null /* group */);
567 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
568
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400569 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400570
571 record.setTextChanged(true);
572 record.setSeen();
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400573 assertTrue(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400574 }
575
576 @Test
577 public void testIsInterruptive_textNotChanged_seen() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400578 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400579 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
580 false /* lights */, false /* defaultLights */, null /* group */);
581 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
582
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400583 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400584
585 record.setTextChanged(false);
586 record.setSeen();
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400587 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400588 }
Julia Reynolds218871e2018-06-13 10:45:21 -0400589
590 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400591 public void testCalculateGrantableUris_PappProvided() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400592 IActivityManager am = mock(IActivityManager.class);
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700593 UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class);
594 when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class),
Julia Reynolds218871e2018-06-13 10:45:21 -0400595 anyInt(), anyInt())).thenThrow(new SecurityException());
596
597 Notification n = mock(Notification.class);
598 when(n.getChannelId()).thenReturn(channel.getId());
599 StatusBarNotification sbn =
600 new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
601 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
602 record.mAm = am;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700603 record.mUgmInternal = ugm;
Julia Reynolds218871e2018-06-13 10:45:21 -0400604
605 try {
606 record.calculateGrantableUris();
607 fail("App provided uri for p targeting app should throw exception");
608 } catch (SecurityException e) {
609 // expected
610 }
611 }
612
613 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400614 public void testCalculateGrantableUris_PuserOverridden() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400615 IActivityManager am = mock(IActivityManager.class);
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700616 UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class);
617 when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class),
618 anyInt(), anyInt())).thenThrow(new SecurityException());
Julia Reynolds218871e2018-06-13 10:45:21 -0400619
620 channel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
621 Notification n = mock(Notification.class);
622 when(n.getChannelId()).thenReturn(channel.getId());
623 StatusBarNotification sbn =
624 new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
625 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
626 record.mAm = am;
627
628 record.calculateGrantableUris();
629 }
630
631 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400632 public void testCalculateGrantableUris_prePappProvided() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400633 IActivityManager am = mock(IActivityManager.class);
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700634 UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class);
635 when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class),
636 anyInt(), anyInt())).thenThrow(new SecurityException());
Julia Reynolds218871e2018-06-13 10:45:21 -0400637
638 Notification n = mock(Notification.class);
639 when(n.getChannelId()).thenReturn(channel.getId());
640 StatusBarNotification sbn =
641 new StatusBarNotification(PKG_O, PKG_O, id1, tag1, uid, uid, n, mUser, null, uid);
642 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
643 record.mAm = am;
644
645 record.calculateGrantableUris();
646 // should not throw
647 }
Tony Mak628cb932018-06-19 18:30:41 +0100648
649 @Test
650 public void testSmartActions() {
651 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
652 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
653 false /* lights */, false /* defaultLights */, groupId /* group */);
654 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Gustav Sennton1463d832018-11-06 16:12:48 +0000655 assertNull(record.getSystemGeneratedSmartActions());
Tony Mak628cb932018-06-19 18:30:41 +0100656
657 ArrayList<Notification.Action> smartActions = new ArrayList<>();
658 smartActions.add(new Notification.Action.Builder(
659 Icon.createWithResource(getContext(), R.drawable.btn_default),
660 "text", null).build());
Gustav Sennton1463d832018-11-06 16:12:48 +0000661 record.setSystemGeneratedSmartActions(smartActions);
662 assertEquals(smartActions, record.getSystemGeneratedSmartActions());
Tony Mak628cb932018-06-19 18:30:41 +0100663 }
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400664
665 @Test
666 public void testUpdateNotificationChannel() {
667 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
668 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
669 false /* lights */, false /* defaultLights */, groupId /* group */);
670 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
671
672 assertEquals(channel.getImportance(), record.getImportance());
673
674 record.updateNotificationChannel(
675 new NotificationChannel(channelId, "", channel.getImportance() - 1));
676
677 assertEquals(channel.getImportance() - 1, record.getImportance());
678 }
679
680 @Test
681 public void testCalculateImportance_systemImportance() {
682 channel.setImportance(IMPORTANCE_HIGH);
683 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
684 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
685 false /* lights */, false /* defaultLights */, groupId /* group */);
686 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
687
688 record.setSystemImportance(IMPORTANCE_LOW);
689 assertEquals(IMPORTANCE_LOW, record.getImportance());
690
691 record = new NotificationRecord(mMockContext, sbn, channel);
692 channel.lockFields(USER_LOCKED_IMPORTANCE);
693
694 record.setSystemImportance(IMPORTANCE_LOW);
695 assertEquals(IMPORTANCE_LOW, record.getImportance());
696 }
697
698 @Test
699 public void testCalculateImportance_asstImportance() {
700 channel.setImportance(IMPORTANCE_HIGH);
701 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
702 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
703 false /* lights */, false /* defaultLights */, groupId /* group */);
704 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
705
706 record.setAssistantImportance(IMPORTANCE_LOW);
Julia Reynolds27c0a962018-12-10 12:37:28 -0500707 record.calculateImportance();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400708 assertEquals(IMPORTANCE_LOW, record.getImportance());
709
710 // assistant ignored if user expressed preference
711 record = new NotificationRecord(mMockContext, sbn, channel);
712 channel.lockFields(USER_LOCKED_IMPORTANCE);
713
714 record.setAssistantImportance(IMPORTANCE_LOW);
Julia Reynolds27c0a962018-12-10 12:37:28 -0500715 record.calculateImportance();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400716 assertEquals(channel.getImportance(), record.getImportance());
717 }
718
719 @Test
720 public void testCalculateImportance_asstImportanceChannelUpdate() {
721 channel.setImportance(IMPORTANCE_HIGH);
722 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
723 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
724 false /* lights */, false /* defaultLights */, groupId /* group */);
725 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
726
727 record.setAssistantImportance(IMPORTANCE_LOW);
Julia Reynolds27c0a962018-12-10 12:37:28 -0500728 record.calculateImportance();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400729 assertEquals(IMPORTANCE_LOW, record.getImportance());
730
731 record.updateNotificationChannel(
732 new NotificationChannel(channelId, "", IMPORTANCE_DEFAULT));
733
734 assertEquals(IMPORTANCE_LOW, record.getImportance());
735 }
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400736
737 @Test
738 public void testSetContactAffinity() {
739 channel.setImportance(IMPORTANCE_LOW);
740 channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
741 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
742 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
743 false /* lights */, false /* defaultLights */, groupId /* group */);
744 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
745
746 record.setContactAffinity(1.0f);
747
748 assertEquals(1.0f, record.getContactAffinity());
749 assertEquals(IMPORTANCE_LOW, record.getImportance());
750 }
Gus Prevasa3226492018-10-23 11:10:09 -0400751
752 @Test
Gus Prevas7306b902018-12-11 10:57:06 -0500753 public void testSetDidNotAudiblyAlert() {
754 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
755 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
756 false /* lights */, false /* defaultLights */, groupId /* group */);
757 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
758
759 record.setAudiblyAlerted(false);
760
761 assertEquals(-1, record.getLastAudiblyAlertedMs());
762 }
763
764 @Test
Gus Prevasa3226492018-10-23 11:10:09 -0400765 public void testSetAudiblyAlerted() {
766 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
767 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
768 false /* lights */, false /* defaultLights */, groupId /* group */);
769 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
770
771 record.setAudiblyAlerted(true);
772
Gus Prevas7306b902018-12-11 10:57:06 -0500773 assertNotEquals(-1, record.getLastAudiblyAlertedMs());
Gus Prevasa3226492018-10-23 11:10:09 -0400774 }
Julia Reynoldsd6730072019-01-04 12:52:52 -0500775
776 @Test
777 public void testIsNewEnoughForAlerting_new() {
778 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
779 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
780 false /* lights */, false /* defaultLights */, groupId /* group */);
781 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
782
783 assertTrue(record.isNewEnoughForAlerting(record.mUpdateTimeMs));
784 }
785
786 @Test
787 public void testIsNewEnoughForAlerting_old() {
788 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
789 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
790 false /* lights */, false /* defaultLights */, groupId /* group */);
791 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
792
793 assertFalse(record.isNewEnoughForAlerting(record.mUpdateTimeMs + (1000 * 60 * 60)));
794 }
Julia Reynolds413ba842019-01-11 10:38:08 -0500795
796 @Test
797 public void testIgnoreImportanceAdjustmentsForOemLockedChannels() {
798 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
799 channel.setImportanceLockedByOEM(true);
800
801 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
802 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
803 false /* lights */, false /* defaultLights */, groupId /* group */);
804 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
805
806 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
807
808 Bundle bundle = new Bundle();
809 bundle.putInt(Adjustment.KEY_IMPORTANCE, IMPORTANCE_LOW);
810 Adjustment adjustment = new Adjustment(
811 PKG_O, record.getKey(), bundle, "", record.getUserId());
812
813 record.addAdjustment(adjustment);
814 record.applyAdjustments();
815 record.calculateImportance();
816
817 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
818 }
819
820 @Test
821 public void testApplyImportanceAdjustmentsForNonOemLockedChannels() {
822 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
823 channel.setImportanceLockedByOEM(false);
824
825 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
826 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
827 false /* lights */, false /* defaultLights */, groupId /* group */);
828 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
829
830 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
831
832 Bundle bundle = new Bundle();
833 bundle.putInt(Adjustment.KEY_IMPORTANCE, IMPORTANCE_LOW);
834 Adjustment adjustment = new Adjustment(
835 PKG_O, record.getKey(), bundle, "", record.getUserId());
836
837 record.addAdjustment(adjustment);
838 record.applyAdjustments();
839 record.calculateImportance();
840
841 assertEquals(IMPORTANCE_LOW, record.getImportance());
842 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500843}