blob: fab6b7fd0d77edfeceb5e2769cfb31d4aba406cd [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;
Will Brockman934b8e32019-03-08 11:14:45 -050022import static android.service.notification.Adjustment.KEY_IMPORTANCE;
Gus Prevas7306b902018-12-11 10:57:06 -050023import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE;
24import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL;
25import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_POSITIVE;
Julia Reynolds503ed942017-10-04 16:04:56 -040026
Julia Reynolds0c299d42016-11-15 14:37:04 -050027import static junit.framework.Assert.assertEquals;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -050028import static junit.framework.Assert.assertFalse;
Julia Reynolds0c299d42016-11-15 14:37:04 -050029import static junit.framework.Assert.assertNotNull;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -050030import static junit.framework.Assert.assertNull;
Julia Reynolds0c299d42016-11-15 14:37:04 -050031import static junit.framework.Assert.assertTrue;
32
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040033import static org.junit.Assert.assertNotEquals;
Julia Reynolds218871e2018-06-13 10:45:21 -040034import static org.junit.Assert.fail;
35import static org.mockito.ArgumentMatchers.any;
36import static org.mockito.ArgumentMatchers.anyInt;
37import static org.mockito.ArgumentMatchers.eq;
38import static org.mockito.Mockito.mock;
Julia Reynolds0c299d42016-11-15 14:37:04 -050039import static org.mockito.Mockito.when;
40
41import android.app.ActivityManager;
Julia Reynolds218871e2018-06-13 10:45:21 -040042import android.app.IActivityManager;
Julia Reynolds0c299d42016-11-15 14:37:04 -050043import android.app.Notification;
44import android.app.Notification.Builder;
45import android.app.NotificationChannel;
46import android.app.NotificationManager;
47import android.content.Context;
48import android.content.pm.ApplicationInfo;
49import android.content.pm.PackageManager;
Julia Reynolds529e3322017-02-06 08:33:01 -050050import android.graphics.Color;
Tony Mak628cb932018-06-19 18:30:41 +010051import android.graphics.drawable.Icon;
Julia Reynolds0c299d42016-11-15 14:37:04 -050052import android.media.AudioAttributes;
Chris Wrenb3921792017-06-01 13:34:46 -040053import android.metrics.LogMaker;
Julia Reynolds0c299d42016-11-15 14:37:04 -050054import android.net.Uri;
Chris Wren6cbf57b2019-12-12 15:33:21 -050055import android.os.Build;
Julia Reynolds503ed942017-10-04 16:04:56 -040056import android.os.Bundle;
Julia Reynolds0c299d42016-11-15 14:37:04 -050057import android.os.UserHandle;
58import android.provider.Settings;
Julia Reynolds503ed942017-10-04 16:04:56 -040059import android.service.notification.Adjustment;
Julia Reynolds0c299d42016-11-15 14:37:04 -050060import android.service.notification.StatusBarNotification;
Chris Wren6cbf57b2019-12-12 15:33:21 -050061import android.widget.RemoteViews;
Brett Chabot84151d92019-02-27 15:37:59 -080062
63import androidx.test.filters.SmallTest;
64import androidx.test.runner.AndroidJUnit4;
Julia Reynolds0c299d42016-11-15 14:37:04 -050065
Tony Mak628cb932018-06-19 18:30:41 +010066import com.android.internal.R;
Chris Wrenb3921792017-06-01 13:34:46 -040067import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk74f5e362017-12-06 08:56:33 -050068import com.android.server.UiServiceTestCase;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070069import com.android.server.uri.UriGrantsManagerInternal;
Chris Wrenb3921792017-06-01 13:34:46 -040070
Julia Reynolds0c299d42016-11-15 14:37:04 -050071import org.junit.Before;
72import org.junit.Test;
73import org.junit.runner.RunWith;
74import org.mockito.Mock;
Julia Reynolds0c299d42016-11-15 14:37:04 -050075import org.mockito.MockitoAnnotations;
76
Tony Mak628cb932018-06-19 18:30:41 +010077import java.util.ArrayList;
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040078import java.util.Arrays;
Julia Reynolds0c299d42016-11-15 14:37:04 -050079
80@SmallTest
81@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050082public class NotificationRecordTest extends UiServiceTestCase {
Julia Reynolds0c299d42016-11-15 14:37:04 -050083
Julia Reynolds218871e2018-06-13 10:45:21 -040084 private final Context mMockContext = mock(Context.class);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040085 @Mock private PackageManager mPm;
Julia Reynolds0c299d42016-11-15 14:37:04 -050086
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060087 private final String pkg = PKG_N_MR1;
Julia Reynolds19ad7472016-12-12 08:51:08 -050088 private final int uid = 9583;
Julia Reynolds0c299d42016-11-15 14:37:04 -050089 private final int id1 = 1;
Julia Reynolds0c299d42016-11-15 14:37:04 -050090 private final String tag1 = "tag1";
Julia Reynolds0c299d42016-11-15 14:37:04 -050091 private final String channelId = "channel";
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040092 private NotificationChannel channel =
Julia Reynolds0c299d42016-11-15 14:37:04 -050093 new NotificationChannel(channelId, "test", NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040094 private final String groupId = "group";
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -040095 private NotificationChannel defaultChannel =
Julia Reynolds0c299d42016-11-15 14:37:04 -050096 new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "test",
97 NotificationManager.IMPORTANCE_UNSPECIFIED);
98 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
Julia Reynolds0c299d42016-11-15 14:37:04 -050099
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);
Chris Wren6cbf57b2019-12-12 15:33:21 -0500119 ApplicationInfo appInfo = new ApplicationInfo();
120 appInfo.targetSdkVersion = Build.VERSION_CODES.O;
121 when(mMockContext.getApplicationInfo()).thenReturn(appInfo);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500122 }
123
Julia Reynolds218871e2018-06-13 10:45:21 -0400124 private StatusBarNotification getNotification(String pkg, boolean noisy, boolean defaultSound,
Chris Wrenb3921792017-06-01 13:34:46 -0400125 boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights,
126 String group) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500127 final Builder builder = new Builder(mMockContext)
128 .setContentTitle("foo")
129 .setSmallIcon(android.R.drawable.sym_def_app_icon)
130 .setPriority(Notification.PRIORITY_HIGH);
131
132 int defaults = 0;
133 if (noisy) {
134 if (defaultSound) {
135 defaults |= Notification.DEFAULT_SOUND;
136 } else {
137 builder.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500138 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500139 }
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400140 } else {
141 channel.setSound(null, null);
142 builder.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500143 }
144 if (buzzy) {
145 if (defaultVibration) {
146 defaults |= Notification.DEFAULT_VIBRATE;
147 } else {
148 builder.setVibrate(CUSTOM_VIBRATION);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500149 channel.setVibrationPattern(CUSTOM_CHANNEL_VIBRATION);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500150 }
151 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500152 if (lights) {
153 if (defaultLights) {
154 defaults |= Notification.DEFAULT_LIGHTS;
155 } else {
156 builder.setLights(CUSTOM_LIGHT.color, CUSTOM_LIGHT.onMs, CUSTOM_LIGHT.offMs);
Julia Reynolds529e3322017-02-06 08:33:01 -0500157 channel.setLightColor(Color.BLUE);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500158 }
Julia Reynolds529e3322017-02-06 08:33:01 -0500159 channel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500160 } else {
Julia Reynolds529e3322017-02-06 08:33:01 -0500161 channel.enableLights(false);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500162 }
163
Julia Reynolds0c299d42016-11-15 14:37:04 -0500164 builder.setDefaults(defaults);
Julia Reynolds218871e2018-06-13 10:45:21 -0400165 builder.setChannelId(channelId);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500166
Chris Wrenb3921792017-06-01 13:34:46 -0400167 if(group != null) {
168 builder.setGroup(group);
169 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500170
171 Notification n = builder.build();
Julia Reynolds218871e2018-06-13 10:45:21 -0400172 return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n, mUser, null, uid);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500173 }
174
Chris Wren6cbf57b2019-12-12 15:33:21 -0500175 private StatusBarNotification getStyledNotification(boolean customContent, boolean customBig,
176 boolean customHeadsUp, Notification.Style style) {
177 final Builder builder = new Builder(mMockContext)
178 .setContentTitle("foo")
179 .setSmallIcon(android.R.drawable.sym_def_app_icon);
180 if (style != null) {
181 builder.setStyle(style);
182 }
183 if (customContent) {
184 builder.setCustomContentView(mock(RemoteViews.class));
185 }
186 if (customBig) {
187 builder.setCustomBigContentView(mock(RemoteViews.class));
188 }
189 if (customHeadsUp) {
190 builder.setCustomHeadsUpContentView(mock(RemoteViews.class));
191 }
192
193 Notification n = builder.build();
194 return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n, mUser, null, uid);
195 }
196
Julia Reynolds0c299d42016-11-15 14:37:04 -0500197 //
198 // Tests
199 //
200
201 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400202 public void testSound_default_preUpgradeUsesNotification() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500203 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500204 // pre upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400205 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500206 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400207 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500208
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000209 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500210 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500211 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500212 }
213
214 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400215 public void testSound_custom_preUpgradeUsesNotification() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500216 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500217 // pre upgrade, custom sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400218 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500219 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400220 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500221
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000222 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500223 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500224 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500225 }
226
227 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400228 public void testSound_default_userLocked_preUpgrade() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500229 defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500230 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
231 // pre upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400232 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500233 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400234 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500235
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000236 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500237 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500238 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500239 }
240
241 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400242 public void testSound_noSound_preUpgrade() {
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400243 // pre upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400244 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400245 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400246 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400247
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000248 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400249 assertNull(record.getSound());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400250 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
251 }
252
253 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400254 public void testSound_default_upgradeUsesChannel() {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500255 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500256 // post upgrade, default sound.
Julia Reynolds218871e2018-06-13 10:45:21 -0400257 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500258 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400259 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500260
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000261 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500262 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500263 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500264 }
265
266 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400267 public void testVibration_default_preUpgradeUsesNotification() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500268 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500269 // pre upgrade, default vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400270 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500271 false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400272 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500273
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000274 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500275 assertNotNull(record.getVibration());
276 }
277
278 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400279 public void testVibration_custom_preUpgradeUsesNotification() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500280 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500281 // pre upgrade, custom vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400282 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500283 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400284 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500285
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000286 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500287 assertEquals(CUSTOM_VIBRATION, record.getVibration());
288 }
289
290 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400291 public void testVibration_custom_userLocked_preUpgrade() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500292 defaultChannel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500293 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
294 // pre upgrade, custom vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400295 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500296 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400297 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500298
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000299 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400300 assertTrue(!Arrays.equals(CUSTOM_VIBRATION, record.getVibration()));
Julia Reynolds0c299d42016-11-15 14:37:04 -0500301 }
302
303 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400304 public void testVibration_custom_upgradeUsesChannel() {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500305 channel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500306 // post upgrade, custom vibration.
Julia Reynolds218871e2018-06-13 10:45:21 -0400307 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500308 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400309 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500310
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000311 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500312 assertEquals(CUSTOM_CHANNEL_VIBRATION, record.getVibration());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500313 }
314
315 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400316 public void testImportance_preUpgrade() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400317 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500318 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400319 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000320 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500321 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
322 }
323
324 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400325 public void testImportance_locked_preUpgrade() {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400326 defaultChannel.setImportance(IMPORTANCE_LOW);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500327 defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds218871e2018-06-13 10:45:21 -0400328 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500329 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400330 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500331
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000332 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400333 assertEquals(IMPORTANCE_LOW, record.getImportance());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500334 }
335
336 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400337 public void testImportance_locked_unspecified_preUpgrade() {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400338 defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500339 defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds218871e2018-06-13 10:45:21 -0400340 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa917a112017-03-21 11:09:14 -0400341 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400342 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400343
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000344 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400345 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
346 }
347
348 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400349 public void testImportance_upgrade() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400350 StatusBarNotification sbn = getNotification(PKG_O, 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, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500354 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, record.getImportance());
355 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500356
357 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400358 public void testLights_preUpgrade_noLight() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400359 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500360 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400361 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000362 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500363 assertNull(record.getLight());
364 }
365
366
367 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400368 public void testLights_preUpgrade() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400369 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500370 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400371 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000372 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500373 assertEquals(CUSTOM_LIGHT, record.getLight());
374 }
375
376 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400377 public void testLights_locked_preUpgrade() {
Julia Reynolds529e3322017-02-06 08:33:01 -0500378 defaultChannel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500379 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
Julia Reynolds218871e2018-06-13 10:45:21 -0400380 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500381 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400382 true /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500383
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000384 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400385 assertNotEquals(CUSTOM_LIGHT, record.getLight());
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500386 }
387
388 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400389 public void testLights_upgrade_defaultLights() {
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500390 int defaultLightColor = mMockContext.getResources().getColor(
391 com.android.internal.R.color.config_defaultNotificationColor);
392 int defaultLightOn = mMockContext.getResources().getInteger(
393 com.android.internal.R.integer.config_defaultNotificationLedOn);
394 int defaultLightOff = mMockContext.getResources().getInteger(
395 com.android.internal.R.integer.config_defaultNotificationLedOff);
396
397 NotificationRecord.Light expected = new NotificationRecord.Light(
398 defaultLightColor, defaultLightOn, defaultLightOff);
Julia Reynolds218871e2018-06-13 10:45:21 -0400399 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500400 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400401 true /* lights */, true /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000402 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds529e3322017-02-06 08:33:01 -0500403 assertEquals(expected, record.getLight());
404 }
405
406 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400407 public void testLights_upgrade() {
Julia Reynolds529e3322017-02-06 08:33:01 -0500408 int defaultLightOn = mMockContext.getResources().getInteger(
409 com.android.internal.R.integer.config_defaultNotificationLedOn);
410 int defaultLightOff = mMockContext.getResources().getInteger(
411 com.android.internal.R.integer.config_defaultNotificationLedOff);
412
413 NotificationRecord.Light expected = new NotificationRecord.Light(
414 Color.BLUE, defaultLightOn, defaultLightOff);
Julia Reynolds218871e2018-06-13 10:45:21 -0400415 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds529e3322017-02-06 08:33:01 -0500416 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400417 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000418 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 assertEquals(expected, record.getLight());
420 }
421
422 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400423 public void testLights_upgrade_noLight() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400424 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500425 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400426 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000427 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500428 assertNull(record.getLight());
429 }
Chris Wrenb3921792017-06-01 13:34:46 -0400430
431 @Test
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200432 public void testLogMaker() {
433 long timestamp = 1000L;
Julia Reynolds218871e2018-06-13 10:45:21 -0400434 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400435 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
436 false /* lights */, false /* defaultLights */, null /* group */);
437 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200438 final LogMaker logMaker = record.getLogMaker(timestamp);
439
440 assertNull(logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX));
Chris Wrenb3921792017-06-01 13:34:46 -0400441 assertEquals(channelId,
442 (String) logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
443 assertEquals(channel.getImportance(),
444 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200445 assertEquals(record.getLifespanMs(timestamp),
446 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS));
447 assertEquals(record.getFreshnessMs(timestamp),
448 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS));
449 assertEquals(record.getExposureMs(timestamp),
450 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS));
451 assertEquals(record.getInterruptionMs(timestamp),
452 (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS));
Will Brockman934b8e32019-03-08 11:14:45 -0500453 // If no importance calculation has been run, no explanation is available.
454 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_EXPLANATION));
455 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL));
456 assertNull(logMaker.getTaggedData(
457 MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL_EXPLANATION));
458 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_ASST));
459 }
460
461 @Test
462 public void testLogMakerImportanceApp() {
463 long timestamp = 1000L;
464 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
465 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
466 false /* lights */, false /* defaultLights */, null /* group */);
467 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
468
469 record.calculateImportance(); // This importance calculation will yield 'app'
470 final LogMaker logMaker = record.getLogMaker(timestamp);
471 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_APP,
472 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_EXPLANATION));
473 assertEquals(channel.getImportance(),
474 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
475 // The additional information is only populated if the initial importance is overridden.
476 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL));
477 assertNull(logMaker.getTaggedData(
478 MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL_EXPLANATION));
479 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_ASST));
480 }
481
482 @Test
483 public void testLogMakerImportanceAsst() {
484 long timestamp = 1000L;
485 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
486 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
487 false /* lights */, false /* defaultLights */, null /* group */);
488 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
489 Bundle signals = new Bundle();
490 signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW);
491 record.addAdjustment(new Adjustment(PKG_O, KEY_IMPORTANCE, signals, "", uid));
492 record.applyAdjustments();
493 record.calculateImportance(); // This importance calculation will yield 'asst'
494 final LogMaker logMaker = record.getLogMaker(timestamp);
495 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_ASST,
496 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_EXPLANATION));
497 // Therefore this is the assistant-set importance
498 assertEquals(IMPORTANCE_LOW,
499 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
500 // Initial importance is populated so we know what it was, since it didn't get used.
501 assertEquals(channel.getImportance(),
502 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL));
503 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_APP,
504 logMaker.getTaggedData(
505 MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL_EXPLANATION));
Will Brockman605d61b2019-03-29 13:08:49 -0400506 // This field is populated whenever mImportanceExplanationCode is.
507 assertEquals(IMPORTANCE_LOW,
508 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_ASST));
Will Brockman934b8e32019-03-08 11:14:45 -0500509 }
510
511 @Test
512 public void testLogMakerImportanceSystem() {
513 long timestamp = 1000L;
514 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
515 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
516 false /* lights */, false /* defaultLights */, null /* group */);
517 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
518 record.setSystemImportance(IMPORTANCE_HIGH);
519 record.calculateImportance(); // This importance calculation will yield 'system'
520 final LogMaker logMaker = record.getLogMaker(timestamp);
521 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_SYSTEM,
522 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_EXPLANATION));
523 // Therefore this is the system-set importance
524 assertEquals(IMPORTANCE_HIGH,
525 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
526 // Initial importance is populated so we know what it was, since it didn't get used.
527 assertEquals(channel.getImportance(),
528 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL));
529 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_APP,
530 logMaker.getTaggedData(
531 MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL_EXPLANATION));
532 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_ASST));
533 }
534
535 @Test
536 public void testLogMakerImportanceUser() {
537 long timestamp = 1000L;
538 channel.lockFields(channel.USER_LOCKED_IMPORTANCE);
539 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
540 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
541 false /* lights */, false /* defaultLights */, null /* group */);
542 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
543 record.calculateImportance(); // This importance calculation will yield 'user'
544 final LogMaker logMaker = record.getLogMaker(timestamp);
545 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_USER,
546 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_EXPLANATION));
547 // Therefore this is the user-set importance
548 assertEquals(channel.getImportance(),
549 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
550 // The additional information is only populated if the initial importance is overridden.
551 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL));
552 assertNull(logMaker.getTaggedData(
553 MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL_EXPLANATION));
554 assertNull(logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_ASST));
555 }
556
557 @Test
558 public void testLogMakerImportanceMulti() {
559 long timestamp = 1000L;
560 channel.lockFields(channel.USER_LOCKED_IMPORTANCE);
561 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
562 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
563 false /* lights */, false /* defaultLights */, null /* group */);
564 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
565 // Add all 3 ways of overriding the app-set importance of the notification
566 Bundle signals = new Bundle();
567 signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW);
568 record.addAdjustment(new Adjustment(PKG_O, KEY_IMPORTANCE, signals, "", uid));
569 record.applyAdjustments();
570 record.setSystemImportance(IMPORTANCE_HIGH);
571 record.calculateImportance(); // This importance calculation will yield 'system'
572 final LogMaker logMaker = record.getLogMaker(timestamp);
573 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_SYSTEM,
574 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_EXPLANATION));
575 // Therefore this is the system-set importance
576 assertEquals(IMPORTANCE_HIGH,
577 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
578 // Initial importance is populated so we know what it was, since it didn't get used.
579 assertEquals(channel.getImportance(),
580 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL));
581 assertEquals(MetricsEvent.IMPORTANCE_EXPLANATION_USER, logMaker.getTaggedData(
582 MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL_EXPLANATION));
583 // Assistant importance is populated so we know what it was, since it didn't get used.
584 assertEquals(IMPORTANCE_LOW, logMaker.getTaggedData(
585 MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_ASST));
Chris Wrenb3921792017-06-01 13:34:46 -0400586 }
587
Julia Reynolds503ed942017-10-04 16:04:56 -0400588 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400589 public void testNotificationStats() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400590 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds503ed942017-10-04 16:04:56 -0400591 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
592 false /* lights */, false /* defaultLights */, groupId /* group */);
593 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Chris Wrenb3921792017-06-01 13:34:46 -0400594
Julia Reynolds503ed942017-10-04 16:04:56 -0400595 assertFalse(record.getStats().hasSeen());
596 assertFalse(record.isSeen());
597 assertFalse(record.getStats().hasDirectReplied());
598 assertFalse(record.getStats().hasExpanded());
599 assertFalse(record.getStats().hasInteracted());
600 assertFalse(record.getStats().hasViewedSettings());
601 assertFalse(record.getStats().hasSnoozed());
602
603 record.setSeen();
604 assertTrue(record.getStats().hasSeen());
605 assertTrue(record.isSeen());
606 assertFalse(record.getStats().hasDirectReplied());
607 assertFalse(record.getStats().hasExpanded());
608 assertFalse(record.getStats().hasInteracted());
609 assertFalse(record.getStats().hasViewedSettings());
610 assertFalse(record.getStats().hasSnoozed());
611
612 record.recordViewedSettings();
613 assertFalse(record.getStats().hasDirectReplied());
614 assertFalse(record.getStats().hasExpanded());
615 assertTrue(record.getStats().hasViewedSettings());
616 assertFalse(record.getStats().hasSnoozed());
617
618 record.recordSnoozed();
619 assertFalse(record.getStats().hasDirectReplied());
620 assertFalse(record.getStats().hasExpanded());
621 assertTrue(record.getStats().hasSnoozed());
622
623 record.recordExpanded();
624 assertFalse(record.getStats().hasDirectReplied());
625 assertTrue(record.getStats().hasExpanded());
626
627 record.recordDirectReplied();
628 assertTrue(record.getStats().hasDirectReplied());
629 }
630
631 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400632 public void testUserSentiment() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400633 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds503ed942017-10-04 16:04:56 -0400634 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
635 false /* lights */, false /* defaultLights */, groupId /* group */);
636 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
637
638 assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment());
639
640 Bundle signals = new Bundle();
641 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
642 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
643
644 record.applyAdjustments();
645
646 assertEquals(USER_SENTIMENT_NEGATIVE, record.getUserSentiment());
647 }
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500648
649 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400650 public void testUserSentiment_appImportanceUpdatesSentiment() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400651 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400652 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
653 false /* lights */, false /* defaultLights */, groupId /* group */);
654 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
655 assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment());
656
657 record.setIsAppImportanceLocked(true);
658 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
659 }
660
661 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400662 public void testUserSentiment_appImportanceBlocksNegativeSentimentUpdate() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400663 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400664 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
665 false /* lights */, false /* defaultLights */, groupId /* group */);
666 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
667 record.setIsAppImportanceLocked(true);
668
669 Bundle signals = new Bundle();
670 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
671 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
672 record.applyAdjustments();
673
674 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
675 }
676
677 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400678 public void testUserSentiment_userLocked() {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500679 channel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds218871e2018-06-13 10:45:21 -0400680 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500681 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
682 false /* lights */, false /* defaultLights */, groupId /* group */);
683 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
684
685 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
686
687 Bundle signals = new Bundle();
688 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
689 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
690
691 record.applyAdjustments();
692
693 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
694 }
Rohan Shah590e1b22018-04-10 23:48:47 -0400695
696 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400697 public void testAppImportance_returnsCorrectly() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400698 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400699 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
700 false /* lights */, false /* defaultLights */, groupId /* group */);
701 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
702
703 record.setIsAppImportanceLocked(true);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400704 assertTrue(record.getIsAppImportanceLocked());
Rohan Shah590e1b22018-04-10 23:48:47 -0400705
706 record.setIsAppImportanceLocked(false);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400707 assertFalse(record.getIsAppImportanceLocked());
Rohan Shah590e1b22018-04-10 23:48:47 -0400708 }
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400709
710 @Test
711 public void testIsInterruptive_textChanged_notSeen() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400712 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400713 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
714 false /* lights */, false /* defaultLights */, null /* group */);
715 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
716
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400717 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400718
719 record.setTextChanged(true);
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400720 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400721 }
722
723 @Test
724 public void testIsInterruptive_textChanged_seen() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400725 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400726 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
727 false /* lights */, false /* defaultLights */, null /* group */);
728 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
729
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400730 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400731
732 record.setTextChanged(true);
733 record.setSeen();
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400734 assertTrue(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400735 }
736
737 @Test
738 public void testIsInterruptive_textNotChanged_seen() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400739 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400740 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
741 false /* lights */, false /* defaultLights */, null /* group */);
742 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
743
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400744 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400745
746 record.setTextChanged(false);
747 record.setSeen();
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400748 assertFalse(record.isInterruptive());
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400749 }
Julia Reynolds218871e2018-06-13 10:45:21 -0400750
751 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400752 public void testCalculateGrantableUris_PappProvided() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400753 IActivityManager am = mock(IActivityManager.class);
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700754 UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class);
755 when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class),
Julia Reynolds218871e2018-06-13 10:45:21 -0400756 anyInt(), anyInt())).thenThrow(new SecurityException());
757
758 Notification n = mock(Notification.class);
759 when(n.getChannelId()).thenReturn(channel.getId());
760 StatusBarNotification sbn =
761 new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
762 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
763 record.mAm = am;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700764 record.mUgmInternal = ugm;
Julia Reynolds218871e2018-06-13 10:45:21 -0400765
766 try {
767 record.calculateGrantableUris();
768 fail("App provided uri for p targeting app should throw exception");
769 } catch (SecurityException e) {
770 // expected
771 }
772 }
773
774 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400775 public void testCalculateGrantableUris_PuserOverridden() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400776 IActivityManager am = mock(IActivityManager.class);
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700777 UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class);
778 when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class),
779 anyInt(), anyInt())).thenThrow(new SecurityException());
Julia Reynolds218871e2018-06-13 10:45:21 -0400780
781 channel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
782 Notification n = mock(Notification.class);
783 when(n.getChannelId()).thenReturn(channel.getId());
784 StatusBarNotification sbn =
785 new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
786 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
787 record.mAm = am;
788
789 record.calculateGrantableUris();
790 }
791
792 @Test
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400793 public void testCalculateGrantableUris_prePappProvided() {
Julia Reynolds218871e2018-06-13 10:45:21 -0400794 IActivityManager am = mock(IActivityManager.class);
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700795 UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class);
796 when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class),
797 anyInt(), anyInt())).thenThrow(new SecurityException());
Julia Reynolds218871e2018-06-13 10:45:21 -0400798
799 Notification n = mock(Notification.class);
800 when(n.getChannelId()).thenReturn(channel.getId());
801 StatusBarNotification sbn =
802 new StatusBarNotification(PKG_O, PKG_O, id1, tag1, uid, uid, n, mUser, null, uid);
803 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
804 record.mAm = am;
805
806 record.calculateGrantableUris();
807 // should not throw
808 }
Tony Mak628cb932018-06-19 18:30:41 +0100809
810 @Test
811 public void testSmartActions() {
812 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
813 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
814 false /* lights */, false /* defaultLights */, groupId /* group */);
815 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Gustav Sennton1463d832018-11-06 16:12:48 +0000816 assertNull(record.getSystemGeneratedSmartActions());
Tony Mak628cb932018-06-19 18:30:41 +0100817
818 ArrayList<Notification.Action> smartActions = new ArrayList<>();
819 smartActions.add(new Notification.Action.Builder(
820 Icon.createWithResource(getContext(), R.drawable.btn_default),
821 "text", null).build());
Gustav Sennton1463d832018-11-06 16:12:48 +0000822 record.setSystemGeneratedSmartActions(smartActions);
823 assertEquals(smartActions, record.getSystemGeneratedSmartActions());
Tony Mak628cb932018-06-19 18:30:41 +0100824 }
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400825
826 @Test
827 public void testUpdateNotificationChannel() {
828 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
829 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
830 false /* lights */, false /* defaultLights */, groupId /* group */);
831 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
832
833 assertEquals(channel.getImportance(), record.getImportance());
834
835 record.updateNotificationChannel(
836 new NotificationChannel(channelId, "", channel.getImportance() - 1));
837
838 assertEquals(channel.getImportance() - 1, record.getImportance());
839 }
840
841 @Test
842 public void testCalculateImportance_systemImportance() {
843 channel.setImportance(IMPORTANCE_HIGH);
844 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
845 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
846 false /* lights */, false /* defaultLights */, groupId /* group */);
847 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
848
849 record.setSystemImportance(IMPORTANCE_LOW);
850 assertEquals(IMPORTANCE_LOW, record.getImportance());
851
852 record = new NotificationRecord(mMockContext, sbn, channel);
853 channel.lockFields(USER_LOCKED_IMPORTANCE);
854
855 record.setSystemImportance(IMPORTANCE_LOW);
856 assertEquals(IMPORTANCE_LOW, record.getImportance());
857 }
858
859 @Test
860 public void testCalculateImportance_asstImportance() {
861 channel.setImportance(IMPORTANCE_HIGH);
862 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
863 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
864 false /* lights */, false /* defaultLights */, groupId /* group */);
865 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
866
867 record.setAssistantImportance(IMPORTANCE_LOW);
Julia Reynolds27c0a962018-12-10 12:37:28 -0500868 record.calculateImportance();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400869 assertEquals(IMPORTANCE_LOW, record.getImportance());
870
871 // assistant ignored if user expressed preference
872 record = new NotificationRecord(mMockContext, sbn, channel);
873 channel.lockFields(USER_LOCKED_IMPORTANCE);
874
875 record.setAssistantImportance(IMPORTANCE_LOW);
Julia Reynolds27c0a962018-12-10 12:37:28 -0500876 record.calculateImportance();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400877 assertEquals(channel.getImportance(), record.getImportance());
878 }
879
880 @Test
881 public void testCalculateImportance_asstImportanceChannelUpdate() {
882 channel.setImportance(IMPORTANCE_HIGH);
883 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
884 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
885 false /* lights */, false /* defaultLights */, groupId /* group */);
886 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
887
888 record.setAssistantImportance(IMPORTANCE_LOW);
Julia Reynolds27c0a962018-12-10 12:37:28 -0500889 record.calculateImportance();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400890 assertEquals(IMPORTANCE_LOW, record.getImportance());
891
892 record.updateNotificationChannel(
893 new NotificationChannel(channelId, "", IMPORTANCE_DEFAULT));
894
895 assertEquals(IMPORTANCE_LOW, record.getImportance());
896 }
Julia Reynolds8a7e4ac2018-10-08 09:23:35 -0400897
898 @Test
899 public void testSetContactAffinity() {
900 channel.setImportance(IMPORTANCE_LOW);
901 channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
902 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
903 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
904 false /* lights */, false /* defaultLights */, groupId /* group */);
905 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
906
907 record.setContactAffinity(1.0f);
908
909 assertEquals(1.0f, record.getContactAffinity());
910 assertEquals(IMPORTANCE_LOW, record.getImportance());
911 }
Gus Prevasa3226492018-10-23 11:10:09 -0400912
913 @Test
Gus Prevas7306b902018-12-11 10:57:06 -0500914 public void testSetDidNotAudiblyAlert() {
915 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
916 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
917 false /* lights */, false /* defaultLights */, groupId /* group */);
918 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
919
920 record.setAudiblyAlerted(false);
921
922 assertEquals(-1, record.getLastAudiblyAlertedMs());
923 }
924
925 @Test
Gus Prevasa3226492018-10-23 11:10:09 -0400926 public void testSetAudiblyAlerted() {
927 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
928 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
929 false /* lights */, false /* defaultLights */, groupId /* group */);
930 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
931
932 record.setAudiblyAlerted(true);
933
Gus Prevas7306b902018-12-11 10:57:06 -0500934 assertNotEquals(-1, record.getLastAudiblyAlertedMs());
Gus Prevasa3226492018-10-23 11:10:09 -0400935 }
Julia Reynoldsd6730072019-01-04 12:52:52 -0500936
937 @Test
938 public void testIsNewEnoughForAlerting_new() {
939 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
940 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
941 false /* lights */, false /* defaultLights */, groupId /* group */);
942 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
943
944 assertTrue(record.isNewEnoughForAlerting(record.mUpdateTimeMs));
945 }
946
947 @Test
948 public void testIsNewEnoughForAlerting_old() {
949 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
950 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
951 false /* lights */, false /* defaultLights */, groupId /* group */);
952 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
953
954 assertFalse(record.isNewEnoughForAlerting(record.mUpdateTimeMs + (1000 * 60 * 60)));
955 }
Julia Reynolds413ba842019-01-11 10:38:08 -0500956
957 @Test
958 public void testIgnoreImportanceAdjustmentsForOemLockedChannels() {
959 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
960 channel.setImportanceLockedByOEM(true);
961
962 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
963 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
964 false /* lights */, false /* defaultLights */, groupId /* group */);
965 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
966
967 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
968
969 Bundle bundle = new Bundle();
Will Brockman934b8e32019-03-08 11:14:45 -0500970 bundle.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW);
Julia Reynolds413ba842019-01-11 10:38:08 -0500971 Adjustment adjustment = new Adjustment(
972 PKG_O, record.getKey(), bundle, "", record.getUserId());
973
974 record.addAdjustment(adjustment);
975 record.applyAdjustments();
976 record.calculateImportance();
977
978 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
979 }
980
981 @Test
Julia Reynolds0c245002019-03-27 16:10:11 -0400982 public void testIgnoreImportanceAdjustmentsForDefaultAppLockedChannels() {
983 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
984 channel.setImportanceLockedByCriticalDeviceFunction(true);
985
986 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
987 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
988 false /* lights */, false /* defaultLights */, groupId /* group */);
989 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
990
991 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
992
993 Bundle bundle = new Bundle();
994 bundle.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW);
995 Adjustment adjustment = new Adjustment(
996 PKG_O, record.getKey(), bundle, "", record.getUserId());
997
998 record.addAdjustment(adjustment);
999 record.applyAdjustments();
1000 record.calculateImportance();
1001
1002 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
1003 }
1004
1005 @Test
1006 public void testApplyImportanceAdjustmentsForNonOemDefaultAppLockedChannels() {
Julia Reynolds413ba842019-01-11 10:38:08 -05001007 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
1008 channel.setImportanceLockedByOEM(false);
1009
1010 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
1011 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
1012 false /* lights */, false /* defaultLights */, groupId /* group */);
1013 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1014
1015 assertEquals(IMPORTANCE_DEFAULT, record.getImportance());
1016
1017 Bundle bundle = new Bundle();
Will Brockman934b8e32019-03-08 11:14:45 -05001018 bundle.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW);
Julia Reynolds413ba842019-01-11 10:38:08 -05001019 Adjustment adjustment = new Adjustment(
1020 PKG_O, record.getKey(), bundle, "", record.getUserId());
1021
1022 record.addAdjustment(adjustment);
1023 record.applyAdjustments();
1024 record.calculateImportance();
1025
1026 assertEquals(IMPORTANCE_LOW, record.getImportance());
1027 }
Chris Wren6cbf57b2019-12-12 15:33:21 -05001028
1029 @Test
1030 public void testHasUndecoratedRemoteViews_NoRemoteViews() {
1031 StatusBarNotification sbn = getStyledNotification(false, false, false, null);
1032 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1033
1034 assertFalse("false positive detection", record.hasUndecoratedRemoteView());
1035 }
1036
1037 @Test
1038 public void testHasUndecoratedRemoteViews_NoRemoteViewsWithStyle() {
1039 StatusBarNotification sbn = getStyledNotification(false, false, false,
1040 new Notification.BigPictureStyle());
1041 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1042
1043 assertFalse("false positive detection", record.hasUndecoratedRemoteView());
1044 }
1045
1046 @Test
1047 public void testHasUndecoratedRemoteViews_UndecoratedContent() {
1048 StatusBarNotification sbn = getStyledNotification(true, false, false, null);
1049 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1050
1051 assertTrue("false negative detection", record.hasUndecoratedRemoteView());
1052 }
1053
1054
1055 @Test
1056 public void testHasUndecoratedRemoteViews_UndecoratedBig() {
1057 StatusBarNotification sbn = getStyledNotification(false, true, false, null);
1058 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1059
1060 assertTrue("false negative detection", record.hasUndecoratedRemoteView());
1061 }
1062
1063
1064 @Test
1065 public void testHasUndecoratedRemoteViews_UndecoratedHeadsup() {
1066 StatusBarNotification sbn = getStyledNotification(false, false, true, null);
1067 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1068
1069 assertTrue("false negative detection", record.hasUndecoratedRemoteView());
1070 }
1071
1072 @Test
1073 public void testHasUndecoratedRemoteViews_DecoratedRemoteViews() {
1074 StatusBarNotification sbn = getStyledNotification(true, true, true,
1075 new Notification.DecoratedCustomViewStyle());
1076 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1077
1078 assertFalse("false positive detection", record.hasUndecoratedRemoteView());
1079 }
1080
1081 @Test
1082 public void testHasUndecoratedRemoteViews_DecoratedMediaRemoteViews() {
1083 StatusBarNotification sbn = getStyledNotification(true, true, true,
1084 new Notification.DecoratedMediaCustomViewStyle());
1085 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1086
1087 assertFalse("false positive detection", record.hasUndecoratedRemoteView());
1088 }
1089
1090 @Test
1091 public void testHasUndecoratedRemoteViews_UndecoratedWrongStyle() {
1092 StatusBarNotification sbn = getStyledNotification(true, true, true,
1093 new Notification.BigPictureStyle());
1094 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
1095
1096 assertTrue("false negative detection", record.hasUndecoratedRemoteView());
1097 }
Julia Reynolds0c299d42016-11-15 14:37:04 -05001098}