blob: e28699113a3d498753ac514732661c49e2b162d8 [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 Reynolds503ed942017-10-04 16:04:56 -040019import static android.service.notification.NotificationListenerService.Ranking
20 .USER_SENTIMENT_NEGATIVE;
21import static android.service.notification.NotificationListenerService.Ranking
22 .USER_SENTIMENT_NEUTRAL;
Julia Reynoldsc65656a2018-02-12 09:55:14 -050023import static android.service.notification.NotificationListenerService.Ranking
24 .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 Reynolds89945c52018-06-13 10:45:21 -040032import static org.junit.Assert.fail;
33import static org.mockito.ArgumentMatchers.any;
34import static org.mockito.ArgumentMatchers.anyInt;
35import static org.mockito.ArgumentMatchers.eq;
36import static org.mockito.Mockito.mock;
37import static org.mockito.Mockito.times;
38import static org.mockito.Mockito.verify;
Julia Reynolds0c299d42016-11-15 14:37:04 -050039import static org.mockito.Mockito.when;
40
41import android.app.ActivityManager;
Julia Reynolds89945c52018-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;
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 Reynolds89945c52018-06-13 10:45:21 -040055import android.os.RemoteException;
Julia Reynolds0c299d42016-11-15 14:37:04 -050056import android.os.UserHandle;
57import android.provider.Settings;
Julia Reynolds503ed942017-10-04 16:04:56 -040058import android.service.notification.Adjustment;
Julia Reynolds0c299d42016-11-15 14:37:04 -050059import android.service.notification.StatusBarNotification;
Julia Reynolds0c299d42016-11-15 14:37:04 -050060import android.support.test.runner.AndroidJUnit4;
61import android.test.suitebuilder.annotation.SmallTest;
Julia Reynolds89945c52018-06-13 10:45:21 -040062import android.util.Slog;
Julia Reynolds0c299d42016-11-15 14:37:04 -050063
Chris Wrenb3921792017-06-01 13:34:46 -040064import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk74f5e362017-12-06 08:56:33 -050065import com.android.server.UiServiceTestCase;
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
73import java.util.Objects;
74
75@SmallTest
76@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050077public class NotificationRecordTest extends UiServiceTestCase {
Julia Reynolds0c299d42016-11-15 14:37:04 -050078
Julia Reynolds89945c52018-06-13 10:45:21 -040079 private final Context mMockContext = mock(Context.class);
Julia Reynolds0c299d42016-11-15 14:37:04 -050080 @Mock PackageManager mPm;
81
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060082 private final String pkg = PKG_N_MR1;
Julia Reynolds19ad7472016-12-12 08:51:08 -050083 private final int uid = 9583;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060084 private final String pkg2 = PKG_O;
Julia Reynolds0c299d42016-11-15 14:37:04 -050085 private final int uid2 = 1111111;
86 private final int id1 = 1;
87 private final int id2 = 2;
88 private final String tag1 = "tag1";
89 private final String tag2 = "tag2";
90 private final String channelId = "channel";
91 NotificationChannel channel =
92 new NotificationChannel(channelId, "test", NotificationManager.IMPORTANCE_DEFAULT);
Chris Wrenb3921792017-06-01 13:34:46 -040093 private final String channelIdLong =
94 "give_a_developer_a_string_argument_and_who_knows_what_they_will_pass_in_there";
95 final String groupId = "group";
96 final String groupIdOverride = "other_group";
97 private final String groupIdLong =
98 "0|com.foo.bar|g:content://com.foo.bar.ui/account%3A-0000000/account/";
99 NotificationChannel channelLongId =
100 new NotificationChannel(channelIdLong, "long", NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500101 NotificationChannel defaultChannel =
102 new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "test",
103 NotificationManager.IMPORTANCE_UNSPECIFIED);
104 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500105
Julia Reynolds0c299d42016-11-15 14:37:04 -0500106 private static final long[] CUSTOM_VIBRATION = new long[] {
107 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
108 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
109 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
Julia Reynoldsf57de462016-11-23 11:31:46 -0500110 private static final long[] CUSTOM_CHANNEL_VIBRATION = new long[] {300, 400, 300, 400 };
Julia Reynolds0c299d42016-11-15 14:37:04 -0500111 private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI;
112 private static final AudioAttributes CUSTOM_ATTRIBUTES = new AudioAttributes.Builder()
113 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
114 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
115 .build();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500116 private static final NotificationRecord.Light CUSTOM_LIGHT =
117 new NotificationRecord.Light(1, 2, 3);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500118
119 @Before
120 public void setUp() {
121 MockitoAnnotations.initMocks(this);
122
Chris Wren89aa2262017-05-05 18:05:56 -0400123 when(mMockContext.getResources()).thenReturn(getContext().getResources());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500124 when(mMockContext.getPackageManager()).thenReturn(mPm);
Julia Reynolds89945c52018-06-13 10:45:21 -0400125 when(mMockContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500126 }
127
Julia Reynolds89945c52018-06-13 10:45:21 -0400128 private StatusBarNotification getNotification(String pkg, boolean noisy, boolean defaultSound,
Chris Wrenb3921792017-06-01 13:34:46 -0400129 boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights,
130 String group) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500131 final Builder builder = new Builder(mMockContext)
132 .setContentTitle("foo")
133 .setSmallIcon(android.R.drawable.sym_def_app_icon)
134 .setPriority(Notification.PRIORITY_HIGH);
135
136 int defaults = 0;
137 if (noisy) {
138 if (defaultSound) {
139 defaults |= Notification.DEFAULT_SOUND;
140 } else {
141 builder.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500142 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500143 }
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400144 } else {
145 channel.setSound(null, null);
146 builder.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500147 }
148 if (buzzy) {
149 if (defaultVibration) {
150 defaults |= Notification.DEFAULT_VIBRATE;
151 } else {
152 builder.setVibrate(CUSTOM_VIBRATION);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500153 channel.setVibrationPattern(CUSTOM_CHANNEL_VIBRATION);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500154 }
155 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500156 if (lights) {
157 if (defaultLights) {
158 defaults |= Notification.DEFAULT_LIGHTS;
159 } else {
160 builder.setLights(CUSTOM_LIGHT.color, CUSTOM_LIGHT.onMs, CUSTOM_LIGHT.offMs);
Julia Reynolds529e3322017-02-06 08:33:01 -0500161 channel.setLightColor(Color.BLUE);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500162 }
Julia Reynolds529e3322017-02-06 08:33:01 -0500163 channel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500164 } else {
Julia Reynolds529e3322017-02-06 08:33:01 -0500165 channel.enableLights(false);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500166 }
167
Julia Reynolds0c299d42016-11-15 14:37:04 -0500168 builder.setDefaults(defaults);
Julia Reynolds89945c52018-06-13 10:45:21 -0400169 builder.setChannelId(channelId);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500170
Chris Wrenb3921792017-06-01 13:34:46 -0400171 if(group != null) {
172 builder.setGroup(group);
173 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500174
175 Notification n = builder.build();
Julia Reynolds89945c52018-06-13 10:45:21 -0400176 return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n, mUser, null, uid);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500177 }
178
179 //
180 // Tests
181 //
182
183 @Test
184 public void testSound_default_preUpgradeUsesNotification() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500185 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500186 // pre upgrade, default sound.
Julia Reynolds89945c52018-06-13 10:45:21 -0400187 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500188 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400189 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500190
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000191 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500192 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500193 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500194 }
195
196 @Test
197 public void testSound_custom_preUpgradeUsesNotification() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500198 defaultChannel.setSound(null, null);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500199 // pre upgrade, custom sound.
Julia Reynolds89945c52018-06-13 10:45:21 -0400200 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500201 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400202 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500203
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000204 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500205 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500206 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500207 }
208
209 @Test
210 public void testSound_default_userLocked_preUpgrade() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500211 defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500212 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
213 // pre upgrade, default sound.
Julia Reynolds89945c52018-06-13 10:45:21 -0400214 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500215 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400216 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500217
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000218 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500219 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500220 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500221 }
222
223 @Test
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400224 public void testSound_noSound_preUpgrade() throws Exception {
225 // pre upgrade, default sound.
Julia Reynolds89945c52018-06-13 10:45:21 -0400226 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400227 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400228 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400229
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000230 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400231 assertEquals(null, record.getSound());
232 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes());
233 }
234
235 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500236 public void testSound_default_upgradeUsesChannel() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -0500237 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500238 // post upgrade, default sound.
Julia Reynolds89945c52018-06-13 10:45:21 -0400239 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500240 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400241 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500242
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000243 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500244 assertEquals(CUSTOM_SOUND, record.getSound());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500245 assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500246 }
247
248 @Test
249 public void testVibration_default_preUpgradeUsesNotification() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500250 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500251 // pre upgrade, default vibration.
Julia Reynolds89945c52018-06-13 10:45:21 -0400252 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500253 false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400254 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500255
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000256 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500257 assertNotNull(record.getVibration());
258 }
259
260 @Test
261 public void testVibration_custom_preUpgradeUsesNotification() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500262 defaultChannel.enableVibration(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500263 // pre upgrade, custom vibration.
Julia Reynolds89945c52018-06-13 10:45:21 -0400264 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500265 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400266 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500267
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000268 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500269 assertEquals(CUSTOM_VIBRATION, record.getVibration());
270 }
271
272 @Test
273 public void testVibration_custom_userLocked_preUpgrade() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500274 defaultChannel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500275 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
276 // pre upgrade, custom vibration.
Julia Reynolds89945c52018-06-13 10:45:21 -0400277 StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500278 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400279 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500280
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000281 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500282 assertTrue(!Objects.equals(CUSTOM_VIBRATION, record.getVibration()));
283 }
284
285 @Test
286 public void testVibration_custom_upgradeUsesChannel() throws Exception {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500287 channel.enableVibration(true);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500288 // post upgrade, custom vibration.
Julia Reynolds89945c52018-06-13 10:45:21 -0400289 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500290 false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400291 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500292
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000293 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500294 assertEquals(CUSTOM_CHANNEL_VIBRATION, record.getVibration());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500295 }
296
297 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500298 public void testImportance_preUpgrade() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400299 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500300 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400301 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000302 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500303 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
304 }
305
306 @Test
307 public void testImportance_locked_preUpgrade() throws Exception {
308 defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500309 defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds89945c52018-06-13 10:45:21 -0400310 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500311 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400312 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500313
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000314 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500315 assertEquals(NotificationManager.IMPORTANCE_LOW, record.getImportance());
316 }
317
318 @Test
Julia Reynoldsa917a112017-03-21 11:09:14 -0400319 public void testImportance_locked_unspecified_preUpgrade() throws Exception {
320 defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500321 defaultChannel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds89945c52018-06-13 10:45:21 -0400322 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa917a112017-03-21 11:09:14 -0400323 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400324 false /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400325
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000326 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400327 assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance());
328 }
329
330 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500331 public void testImportance_upgrade() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400332 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500333 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400334 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000335 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500336 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, record.getImportance());
337 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500338
339 @Test
340 public void testLights_preUpgrade_noLight() throws Exception {
Julia Reynolds89945c52018-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 false /* 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 assertNull(record.getLight());
346 }
347
348
349 @Test
350 public void testLights_preUpgrade() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400351 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500352 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400353 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000354 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500355 assertEquals(CUSTOM_LIGHT, record.getLight());
356 }
357
358 @Test
359 public void testLights_locked_preUpgrade() throws Exception {
Julia Reynolds529e3322017-02-06 08:33:01 -0500360 defaultChannel.enableLights(true);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500361 defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
Julia Reynolds89945c52018-06-13 10:45:21 -0400362 StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500363 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400364 true /* lights */, false /* defaultLights */, null /* group */);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500365
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000366 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500367 assertFalse(CUSTOM_LIGHT.equals(record.getLight()));
368 }
369
370 @Test
Julia Reynolds529e3322017-02-06 08:33:01 -0500371 public void testLights_upgrade_defaultLights() throws Exception {
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500372 int defaultLightColor = mMockContext.getResources().getColor(
373 com.android.internal.R.color.config_defaultNotificationColor);
374 int defaultLightOn = mMockContext.getResources().getInteger(
375 com.android.internal.R.integer.config_defaultNotificationLedOn);
376 int defaultLightOff = mMockContext.getResources().getInteger(
377 com.android.internal.R.integer.config_defaultNotificationLedOff);
378
379 NotificationRecord.Light expected = new NotificationRecord.Light(
380 defaultLightColor, defaultLightOn, defaultLightOff);
Julia Reynolds89945c52018-06-13 10:45:21 -0400381 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500382 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400383 true /* lights */, true /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000384 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynolds529e3322017-02-06 08:33:01 -0500385 assertEquals(expected, record.getLight());
386 }
387
388 @Test
389 public void testLights_upgrade() throws Exception {
390 int defaultLightOn = mMockContext.getResources().getInteger(
391 com.android.internal.R.integer.config_defaultNotificationLedOn);
392 int defaultLightOff = mMockContext.getResources().getInteger(
393 com.android.internal.R.integer.config_defaultNotificationLedOff);
394
395 NotificationRecord.Light expected = new NotificationRecord.Light(
396 Color.BLUE, defaultLightOn, defaultLightOff);
Julia Reynolds89945c52018-06-13 10:45:21 -0400397 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds529e3322017-02-06 08:33:01 -0500398 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400399 true /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000400 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500401 assertEquals(expected, record.getLight());
402 }
403
404 @Test
405 public void testLights_upgrade_noLight() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400406 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500407 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
Chris Wrenb3921792017-06-01 13:34:46 -0400408 false /* lights */, false /* defaultLights */, null /* group */);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000409 NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 assertNull(record.getLight());
411 }
Chris Wrenb3921792017-06-01 13:34:46 -0400412
413 @Test
414 public void testLogmakerShortChannel() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400415 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400416 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
417 false /* lights */, false /* defaultLights */, null /* group */);
418 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
419 final LogMaker logMaker = record.getLogMaker();
420 assertEquals(channelId,
421 (String) logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID));
422 assertEquals(channel.getImportance(),
423 logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE));
424 }
425
426 @Test
427 public void testLogmakerLongChannel() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400428 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400429 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
430 false /* lights */, false /*defaultLights */, null /* group */);
431 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channelLongId);
432 final String loggedId = (String)
433 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID);
434 assertEquals(channelIdLong.substring(0,10), loggedId.substring(0, 10));
435 }
436
437 @Test
438 public void testLogmakerNoGroup() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400439 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400440 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
441 false /* lights */, false /*defaultLights */, null /* group */);
442 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
443 assertNull(record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
444 }
445
446 @Test
447 public void testLogmakerShortGroup() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400448 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400449 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
450 false /* lights */, false /* defaultLights */, groupId /* group */);
451 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
452 assertEquals(groupId,
453 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
454 }
455
456 @Test
457 public void testLogmakerLongGroup() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400458 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400459 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
460 false /* lights */, false /* defaultLights */, groupIdLong /* group */);
461 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
462 final String loggedId = (String)
463 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID);
464 assertEquals(groupIdLong.substring(0,10), loggedId.substring(0, 10));
465 }
466
467 @Test
468 public void testLogmakerOverrideGroup() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400469 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Chris Wrenb3921792017-06-01 13:34:46 -0400470 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
471 false /* lights */, false /* defaultLights */, groupId /* group */);
472 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
473 assertEquals(groupId,
474 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
475 record.setOverrideGroupKey(groupIdOverride);
476 assertEquals(groupIdOverride,
477 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
478 record.setOverrideGroupKey(null);
479 assertEquals(groupId,
480 record.getLogMaker().getTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID));
481 }
482
Julia Reynolds503ed942017-10-04 16:04:56 -0400483 @Test
484 public void testNotificationStats() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400485 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds503ed942017-10-04 16:04:56 -0400486 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
487 false /* lights */, false /* defaultLights */, groupId /* group */);
488 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
Chris Wrenb3921792017-06-01 13:34:46 -0400489
Julia Reynolds503ed942017-10-04 16:04:56 -0400490 assertFalse(record.getStats().hasSeen());
491 assertFalse(record.isSeen());
492 assertFalse(record.getStats().hasDirectReplied());
493 assertFalse(record.getStats().hasExpanded());
494 assertFalse(record.getStats().hasInteracted());
495 assertFalse(record.getStats().hasViewedSettings());
496 assertFalse(record.getStats().hasSnoozed());
497
498 record.setSeen();
499 assertTrue(record.getStats().hasSeen());
500 assertTrue(record.isSeen());
501 assertFalse(record.getStats().hasDirectReplied());
502 assertFalse(record.getStats().hasExpanded());
503 assertFalse(record.getStats().hasInteracted());
504 assertFalse(record.getStats().hasViewedSettings());
505 assertFalse(record.getStats().hasSnoozed());
506
507 record.recordViewedSettings();
508 assertFalse(record.getStats().hasDirectReplied());
509 assertFalse(record.getStats().hasExpanded());
510 assertTrue(record.getStats().hasViewedSettings());
511 assertFalse(record.getStats().hasSnoozed());
512
513 record.recordSnoozed();
514 assertFalse(record.getStats().hasDirectReplied());
515 assertFalse(record.getStats().hasExpanded());
516 assertTrue(record.getStats().hasSnoozed());
517
518 record.recordExpanded();
519 assertFalse(record.getStats().hasDirectReplied());
520 assertTrue(record.getStats().hasExpanded());
521
522 record.recordDirectReplied();
523 assertTrue(record.getStats().hasDirectReplied());
524 }
525
526 @Test
527 public void testUserSentiment() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400528 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynolds503ed942017-10-04 16:04:56 -0400529 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
530 false /* lights */, false /* defaultLights */, groupId /* group */);
531 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
532
533 assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment());
534
535 Bundle signals = new Bundle();
536 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
537 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
538
539 record.applyAdjustments();
540
541 assertEquals(USER_SENTIMENT_NEGATIVE, record.getUserSentiment());
542 }
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500543
544 @Test
Rohan Shah590e1b22018-04-10 23:48:47 -0400545 public void testUserSentiment_appImportanceUpdatesSentiment() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400546 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400547 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
548 false /* lights */, false /* defaultLights */, groupId /* group */);
549 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
550 assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment());
551
552 record.setIsAppImportanceLocked(true);
553 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
554 }
555
556 @Test
557 public void testUserSentiment_appImportanceBlocksNegativeSentimentUpdate() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400558 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400559 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
560 false /* lights */, false /* defaultLights */, groupId /* group */);
561 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
562 record.setIsAppImportanceLocked(true);
563
564 Bundle signals = new Bundle();
565 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
566 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
567 record.applyAdjustments();
568
569 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
570 }
571
572 @Test
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500573 public void testUserSentiment_userLocked() throws Exception {
574 channel.lockFields(USER_LOCKED_IMPORTANCE);
Julia Reynolds89945c52018-06-13 10:45:21 -0400575 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500576 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
577 false /* lights */, false /* defaultLights */, groupId /* group */);
578 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
579
580 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
581
582 Bundle signals = new Bundle();
583 signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE);
584 record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId()));
585
586 record.applyAdjustments();
587
588 assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment());
589 }
Rohan Shah590e1b22018-04-10 23:48:47 -0400590
591 @Test
592 public void testAppImportance_returnsCorrectly() throws Exception {
Julia Reynolds89945c52018-06-13 10:45:21 -0400593 StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400594 true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
595 false /* lights */, false /* defaultLights */, groupId /* group */);
596 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
597
598 record.setIsAppImportanceLocked(true);
599 assertEquals(true, record.getIsAppImportanceLocked());
600
601 record.setIsAppImportanceLocked(false);
602 assertEquals(false, record.getIsAppImportanceLocked());
603 }
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400604
605 @Test
606 public void testIsInterruptive_textChanged_notSeen() {
Julia Reynolds89945c52018-06-13 10:45:21 -0400607 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400608 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
609 false /* lights */, false /* defaultLights */, null /* group */);
610 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
611
612 assertEquals(false, record.isInterruptive());
613
614 record.setTextChanged(true);
615 assertEquals(false, record.isInterruptive());
616 }
617
618 @Test
619 public void testIsInterruptive_textChanged_seen() {
Julia Reynolds89945c52018-06-13 10:45:21 -0400620 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400621 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
622 false /* lights */, false /* defaultLights */, null /* group */);
623 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
624
625 assertEquals(false, record.isInterruptive());
626
627 record.setTextChanged(true);
628 record.setSeen();
629 assertEquals(true, record.isInterruptive());
630 }
631
632 @Test
633 public void testIsInterruptive_textNotChanged_seen() {
Julia Reynolds89945c52018-06-13 10:45:21 -0400634 StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */,
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400635 false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */,
636 false /* lights */, false /* defaultLights */, null /* group */);
637 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
638
639 assertEquals(false, record.isInterruptive());
640
641 record.setTextChanged(false);
642 record.setSeen();
643 assertEquals(false, record.isInterruptive());
644 }
Julia Reynolds89945c52018-06-13 10:45:21 -0400645
646 @Test
647 public void testCalculateGrantableUris_PappProvided() throws RemoteException {
648 IActivityManager am = mock(IActivityManager.class);
649 when(am.checkGrantUriPermission(anyInt(), eq(null), any(),
650 anyInt(), anyInt())).thenThrow(new SecurityException());
651
652 Notification n = mock(Notification.class);
653 when(n.getChannelId()).thenReturn(channel.getId());
654 StatusBarNotification sbn =
655 new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
656 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
657 record.mAm = am;
658
659 try {
660 record.calculateGrantableUris();
661 fail("App provided uri for p targeting app should throw exception");
662 } catch (SecurityException e) {
663 // expected
664 }
665 }
666
667 @Test
668 public void testCalculateGrantableUris_PuserOverridden() throws RemoteException {
669 IActivityManager am = mock(IActivityManager.class);
670 when(am.checkGrantUriPermission(anyInt(), eq(null), any(),
671 anyInt(), anyInt())).thenThrow(SecurityException.class);
672
673 channel.lockFields(NotificationChannel.USER_LOCKED_SOUND);
674 Notification n = mock(Notification.class);
675 when(n.getChannelId()).thenReturn(channel.getId());
676 StatusBarNotification sbn =
677 new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid);
678 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
679 record.mAm = am;
680
681 record.calculateGrantableUris();
682 }
683
684 @Test
685 public void testCalculateGrantableUris_prePappProvided() throws RemoteException {
686 IActivityManager am = mock(IActivityManager.class);
687 when(am.checkGrantUriPermission(anyInt(), eq(null), any(),
688 anyInt(), anyInt())).thenThrow(SecurityException.class);
689
690 Notification n = mock(Notification.class);
691 when(n.getChannelId()).thenReturn(channel.getId());
692 StatusBarNotification sbn =
693 new StatusBarNotification(PKG_O, PKG_O, id1, tag1, uid, uid, n, mUser, null, uid);
694 NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel);
695 record.mAm = am;
696
697 record.calculateGrantableUris();
698 // should not throw
699 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500700}