Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 18 | import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE; |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
| 20 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
| 21 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 22 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE; |
| 23 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL; |
| 24 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_POSITIVE; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 25 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 26 | import static junit.framework.Assert.assertEquals; |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 27 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 28 | import static junit.framework.Assert.assertNotNull; |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 29 | import static junit.framework.Assert.assertNull; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 30 | import static junit.framework.Assert.assertTrue; |
| 31 | |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 32 | import static org.junit.Assert.assertNotEquals; |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 33 | import static org.junit.Assert.fail; |
| 34 | import static org.mockito.ArgumentMatchers.any; |
| 35 | import static org.mockito.ArgumentMatchers.anyInt; |
| 36 | import static org.mockito.ArgumentMatchers.eq; |
| 37 | import static org.mockito.Mockito.mock; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 38 | import static org.mockito.Mockito.when; |
| 39 | |
| 40 | import android.app.ActivityManager; |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 41 | import android.app.IActivityManager; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 42 | import android.app.Notification; |
| 43 | import android.app.Notification.Builder; |
| 44 | import android.app.NotificationChannel; |
| 45 | import android.app.NotificationManager; |
| 46 | import android.content.Context; |
| 47 | import android.content.pm.ApplicationInfo; |
| 48 | import android.content.pm.PackageManager; |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 49 | import android.graphics.Color; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 50 | import android.graphics.drawable.Icon; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 51 | import android.media.AudioAttributes; |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 52 | import android.metrics.LogMaker; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 53 | import android.net.Uri; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 54 | import android.os.Bundle; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 55 | import android.os.UserHandle; |
| 56 | import android.provider.Settings; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 57 | import android.service.notification.Adjustment; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 58 | import android.service.notification.StatusBarNotification; |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 59 | import android.support.test.filters.SmallTest; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 60 | import android.support.test.runner.AndroidJUnit4; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 61 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 62 | import com.android.internal.R; |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 63 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 64 | import com.android.server.UiServiceTestCase; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 65 | import com.android.server.uri.UriGrantsManagerInternal; |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 66 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 67 | import org.junit.Before; |
| 68 | import org.junit.Test; |
| 69 | import org.junit.runner.RunWith; |
| 70 | import org.mockito.Mock; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 71 | import org.mockito.MockitoAnnotations; |
| 72 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 73 | import java.util.ArrayList; |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 74 | import java.util.Arrays; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 75 | |
| 76 | @SmallTest |
| 77 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 78 | public class NotificationRecordTest extends UiServiceTestCase { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 79 | |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 80 | private final Context mMockContext = mock(Context.class); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 81 | @Mock private PackageManager mPm; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 82 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 83 | private final String pkg = PKG_N_MR1; |
Julia Reynolds | 19ad747 | 2016-12-12 08:51:08 -0500 | [diff] [blame] | 84 | private final int uid = 9583; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 85 | private final int id1 = 1; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 86 | private final String tag1 = "tag1"; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 87 | private final String channelId = "channel"; |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 88 | private NotificationChannel channel = |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 89 | new NotificationChannel(channelId, "test", NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 90 | private final String groupId = "group"; |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 91 | private NotificationChannel defaultChannel = |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 92 | new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "test", |
| 93 | NotificationManager.IMPORTANCE_UNSPECIFIED); |
| 94 | private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 95 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 96 | private static final long[] CUSTOM_VIBRATION = new long[] { |
| 97 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, |
| 98 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, |
| 99 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 }; |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 100 | private static final long[] CUSTOM_CHANNEL_VIBRATION = new long[] {300, 400, 300, 400 }; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 101 | private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI; |
| 102 | private static final AudioAttributes CUSTOM_ATTRIBUTES = new AudioAttributes.Builder() |
| 103 | .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) |
| 104 | .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) |
| 105 | .build(); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 106 | private static final NotificationRecord.Light CUSTOM_LIGHT = |
| 107 | new NotificationRecord.Light(1, 2, 3); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 108 | |
| 109 | @Before |
| 110 | public void setUp() { |
| 111 | MockitoAnnotations.initMocks(this); |
| 112 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 113 | when(mMockContext.getResources()).thenReturn(getContext().getResources()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 114 | when(mMockContext.getPackageManager()).thenReturn(mPm); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 115 | when(mMockContext.getApplicationInfo()).thenReturn(new ApplicationInfo()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 118 | private StatusBarNotification getNotification(String pkg, boolean noisy, boolean defaultSound, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 119 | boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights, |
| 120 | String group) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 121 | final Builder builder = new Builder(mMockContext) |
| 122 | .setContentTitle("foo") |
| 123 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 124 | .setPriority(Notification.PRIORITY_HIGH); |
| 125 | |
| 126 | int defaults = 0; |
| 127 | if (noisy) { |
| 128 | if (defaultSound) { |
| 129 | defaults |= Notification.DEFAULT_SOUND; |
| 130 | } else { |
| 131 | builder.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 132 | channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 133 | } |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 134 | } else { |
| 135 | channel.setSound(null, null); |
| 136 | builder.setSound(null, null); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 137 | } |
| 138 | if (buzzy) { |
| 139 | if (defaultVibration) { |
| 140 | defaults |= Notification.DEFAULT_VIBRATE; |
| 141 | } else { |
| 142 | builder.setVibrate(CUSTOM_VIBRATION); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 143 | channel.setVibrationPattern(CUSTOM_CHANNEL_VIBRATION); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 144 | } |
| 145 | } |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 146 | if (lights) { |
| 147 | if (defaultLights) { |
| 148 | defaults |= Notification.DEFAULT_LIGHTS; |
| 149 | } else { |
| 150 | builder.setLights(CUSTOM_LIGHT.color, CUSTOM_LIGHT.onMs, CUSTOM_LIGHT.offMs); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 151 | channel.setLightColor(Color.BLUE); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 152 | } |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 153 | channel.enableLights(true); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 154 | } else { |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 155 | channel.enableLights(false); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 156 | } |
| 157 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 158 | builder.setDefaults(defaults); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 159 | builder.setChannelId(channelId); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 160 | |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 161 | if(group != null) { |
| 162 | builder.setGroup(group); |
| 163 | } |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 164 | |
| 165 | Notification n = builder.build(); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 166 | return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n, mUser, null, uid); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // |
| 170 | // Tests |
| 171 | // |
| 172 | |
| 173 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 174 | public void testSound_default_preUpgradeUsesNotification() { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 175 | defaultChannel.setSound(null, null); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 176 | // pre upgrade, default sound. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 177 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 178 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 179 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 180 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 181 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 182 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 183 | assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 187 | public void testSound_custom_preUpgradeUsesNotification() { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 188 | defaultChannel.setSound(null, null); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 189 | // pre upgrade, custom sound. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 190 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 191 | false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 192 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 193 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 194 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 195 | assertEquals(CUSTOM_SOUND, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 196 | assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 200 | public void testSound_default_userLocked_preUpgrade() { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 201 | defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 202 | defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND); |
| 203 | // pre upgrade, default sound. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 204 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 205 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 206 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 207 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 208 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 209 | assertEquals(CUSTOM_SOUND, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 210 | assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 214 | public void testSound_noSound_preUpgrade() { |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 215 | // pre upgrade, default sound. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 216 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 217 | false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 218 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 219 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 220 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 221 | assertNull(record.getSound()); |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 222 | assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes()); |
| 223 | } |
| 224 | |
| 225 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 226 | public void testSound_default_upgradeUsesChannel() { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 227 | channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 228 | // post upgrade, default sound. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 229 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 230 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 231 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 232 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 233 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 234 | assertEquals(CUSTOM_SOUND, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 235 | assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 239 | public void testVibration_default_preUpgradeUsesNotification() { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 240 | defaultChannel.enableVibration(false); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 241 | // pre upgrade, default vibration. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 242 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 243 | false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 244 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 245 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 246 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 247 | assertNotNull(record.getVibration()); |
| 248 | } |
| 249 | |
| 250 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 251 | public void testVibration_custom_preUpgradeUsesNotification() { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 252 | defaultChannel.enableVibration(false); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 253 | // pre upgrade, custom vibration. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 254 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 255 | false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 256 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 257 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 258 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 259 | assertEquals(CUSTOM_VIBRATION, record.getVibration()); |
| 260 | } |
| 261 | |
| 262 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 263 | public void testVibration_custom_userLocked_preUpgrade() { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 264 | defaultChannel.enableVibration(true); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 265 | defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION); |
| 266 | // pre upgrade, custom vibration. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 267 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 268 | false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 269 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 270 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 271 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 272 | assertTrue(!Arrays.equals(CUSTOM_VIBRATION, record.getVibration())); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 276 | public void testVibration_custom_upgradeUsesChannel() { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 277 | channel.enableVibration(true); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 278 | // post upgrade, custom vibration. |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 279 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 280 | false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 281 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 282 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 283 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 284 | assertEquals(CUSTOM_CHANNEL_VIBRATION, record.getVibration()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 288 | public void testImportance_preUpgrade() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 289 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 290 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 291 | false /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 292 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 293 | assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance()); |
| 294 | } |
| 295 | |
| 296 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 297 | public void testImportance_locked_preUpgrade() { |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 298 | defaultChannel.setImportance(IMPORTANCE_LOW); |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 299 | defaultChannel.lockFields(USER_LOCKED_IMPORTANCE); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 300 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 301 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 302 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 303 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 304 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 305 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 309 | public void testImportance_locked_unspecified_preUpgrade() { |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 310 | defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 311 | defaultChannel.lockFields(USER_LOCKED_IMPORTANCE); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 312 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 313 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 314 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 315 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 316 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 317 | assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance()); |
| 318 | } |
| 319 | |
| 320 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 321 | public void testImportance_upgrade() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 322 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 323 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 324 | false /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 325 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 326 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, record.getImportance()); |
| 327 | } |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 328 | |
| 329 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 330 | public void testLights_preUpgrade_noLight() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 331 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 332 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 333 | false /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 334 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 335 | assertNull(record.getLight()); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 340 | public void testLights_preUpgrade() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 341 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 342 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 343 | true /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 344 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 345 | assertEquals(CUSTOM_LIGHT, record.getLight()); |
| 346 | } |
| 347 | |
| 348 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 349 | public void testLights_locked_preUpgrade() { |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 350 | defaultChannel.enableLights(true); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 351 | defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 352 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 353 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 354 | true /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 355 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 356 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 357 | assertNotEquals(CUSTOM_LIGHT, record.getLight()); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 361 | public void testLights_upgrade_defaultLights() { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 362 | int defaultLightColor = mMockContext.getResources().getColor( |
| 363 | com.android.internal.R.color.config_defaultNotificationColor); |
| 364 | int defaultLightOn = mMockContext.getResources().getInteger( |
| 365 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 366 | int defaultLightOff = mMockContext.getResources().getInteger( |
| 367 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 368 | |
| 369 | NotificationRecord.Light expected = new NotificationRecord.Light( |
| 370 | defaultLightColor, defaultLightOn, defaultLightOff); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 371 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 372 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 373 | true /* lights */, true /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 374 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 375 | assertEquals(expected, record.getLight()); |
| 376 | } |
| 377 | |
| 378 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 379 | public void testLights_upgrade() { |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 380 | int defaultLightOn = mMockContext.getResources().getInteger( |
| 381 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 382 | int defaultLightOff = mMockContext.getResources().getInteger( |
| 383 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 384 | |
| 385 | NotificationRecord.Light expected = new NotificationRecord.Light( |
| 386 | Color.BLUE, defaultLightOn, defaultLightOff); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 387 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 388 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 389 | true /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 390 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 391 | assertEquals(expected, record.getLight()); |
| 392 | } |
| 393 | |
| 394 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 395 | public void testLights_upgrade_noLight() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 396 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 397 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 398 | false /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 399 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 400 | assertNull(record.getLight()); |
| 401 | } |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 402 | |
| 403 | @Test |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 404 | public void testLogMaker() { |
| 405 | long timestamp = 1000L; |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 406 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 407 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 408 | false /* lights */, false /* defaultLights */, null /* group */); |
| 409 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 410 | final LogMaker logMaker = record.getLogMaker(timestamp); |
| 411 | |
| 412 | assertNull(logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX)); |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 413 | assertEquals(channelId, |
| 414 | (String) logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID)); |
| 415 | assertEquals(channel.getImportance(), |
| 416 | logMaker.getTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE)); |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 417 | assertEquals(record.getLifespanMs(timestamp), |
| 418 | (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS)); |
| 419 | assertEquals(record.getFreshnessMs(timestamp), |
| 420 | (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS)); |
| 421 | assertEquals(record.getExposureMs(timestamp), |
| 422 | (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS)); |
| 423 | assertEquals(record.getInterruptionMs(timestamp), |
| 424 | (int) logMaker.getTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS)); |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 427 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 428 | public void testNotificationStats() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 429 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 430 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 431 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 432 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 433 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 434 | assertFalse(record.getStats().hasSeen()); |
| 435 | assertFalse(record.isSeen()); |
| 436 | assertFalse(record.getStats().hasDirectReplied()); |
| 437 | assertFalse(record.getStats().hasExpanded()); |
| 438 | assertFalse(record.getStats().hasInteracted()); |
| 439 | assertFalse(record.getStats().hasViewedSettings()); |
| 440 | assertFalse(record.getStats().hasSnoozed()); |
| 441 | |
| 442 | record.setSeen(); |
| 443 | assertTrue(record.getStats().hasSeen()); |
| 444 | assertTrue(record.isSeen()); |
| 445 | assertFalse(record.getStats().hasDirectReplied()); |
| 446 | assertFalse(record.getStats().hasExpanded()); |
| 447 | assertFalse(record.getStats().hasInteracted()); |
| 448 | assertFalse(record.getStats().hasViewedSettings()); |
| 449 | assertFalse(record.getStats().hasSnoozed()); |
| 450 | |
| 451 | record.recordViewedSettings(); |
| 452 | assertFalse(record.getStats().hasDirectReplied()); |
| 453 | assertFalse(record.getStats().hasExpanded()); |
| 454 | assertTrue(record.getStats().hasViewedSettings()); |
| 455 | assertFalse(record.getStats().hasSnoozed()); |
| 456 | |
| 457 | record.recordSnoozed(); |
| 458 | assertFalse(record.getStats().hasDirectReplied()); |
| 459 | assertFalse(record.getStats().hasExpanded()); |
| 460 | assertTrue(record.getStats().hasSnoozed()); |
| 461 | |
| 462 | record.recordExpanded(); |
| 463 | assertFalse(record.getStats().hasDirectReplied()); |
| 464 | assertTrue(record.getStats().hasExpanded()); |
| 465 | |
| 466 | record.recordDirectReplied(); |
| 467 | assertTrue(record.getStats().hasDirectReplied()); |
| 468 | } |
| 469 | |
| 470 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 471 | public void testUserSentiment() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 472 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 473 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 474 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 475 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 476 | |
| 477 | assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment()); |
| 478 | |
| 479 | Bundle signals = new Bundle(); |
| 480 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE); |
| 481 | record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId())); |
| 482 | |
| 483 | record.applyAdjustments(); |
| 484 | |
| 485 | assertEquals(USER_SENTIMENT_NEGATIVE, record.getUserSentiment()); |
| 486 | } |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 487 | |
| 488 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 489 | public void testUserSentiment_appImportanceUpdatesSentiment() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 490 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 491 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 492 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 493 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 494 | assertEquals(USER_SENTIMENT_NEUTRAL, record.getUserSentiment()); |
| 495 | |
| 496 | record.setIsAppImportanceLocked(true); |
| 497 | assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment()); |
| 498 | } |
| 499 | |
| 500 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 501 | public void testUserSentiment_appImportanceBlocksNegativeSentimentUpdate() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 502 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 503 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 504 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 505 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 506 | record.setIsAppImportanceLocked(true); |
| 507 | |
| 508 | Bundle signals = new Bundle(); |
| 509 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE); |
| 510 | record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId())); |
| 511 | record.applyAdjustments(); |
| 512 | |
| 513 | assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment()); |
| 514 | } |
| 515 | |
| 516 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 517 | public void testUserSentiment_userLocked() { |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 518 | channel.lockFields(USER_LOCKED_IMPORTANCE); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 519 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 520 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 521 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 522 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 523 | |
| 524 | assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment()); |
| 525 | |
| 526 | Bundle signals = new Bundle(); |
| 527 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE); |
| 528 | record.addAdjustment(new Adjustment(pkg, record.getKey(), signals, null, sbn.getUserId())); |
| 529 | |
| 530 | record.applyAdjustments(); |
| 531 | |
| 532 | assertEquals(USER_SENTIMENT_POSITIVE, record.getUserSentiment()); |
| 533 | } |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 534 | |
| 535 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 536 | public void testAppImportance_returnsCorrectly() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 537 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 538 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 539 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 540 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 541 | |
| 542 | record.setIsAppImportanceLocked(true); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 543 | assertTrue(record.getIsAppImportanceLocked()); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 544 | |
| 545 | record.setIsAppImportanceLocked(false); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 546 | assertFalse(record.getIsAppImportanceLocked()); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 547 | } |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 548 | |
| 549 | @Test |
| 550 | public void testIsInterruptive_textChanged_notSeen() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 551 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 552 | false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 553 | false /* lights */, false /* defaultLights */, null /* group */); |
| 554 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 555 | |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 556 | assertFalse(record.isInterruptive()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 557 | |
| 558 | record.setTextChanged(true); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 559 | assertFalse(record.isInterruptive()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | @Test |
| 563 | public void testIsInterruptive_textChanged_seen() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 564 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 565 | false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 566 | false /* lights */, false /* defaultLights */, null /* group */); |
| 567 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 568 | |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 569 | assertFalse(record.isInterruptive()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 570 | |
| 571 | record.setTextChanged(true); |
| 572 | record.setSeen(); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 573 | assertTrue(record.isInterruptive()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | @Test |
| 577 | public void testIsInterruptive_textNotChanged_seen() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 578 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 579 | false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 580 | false /* lights */, false /* defaultLights */, null /* group */); |
| 581 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 582 | |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 583 | assertFalse(record.isInterruptive()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 584 | |
| 585 | record.setTextChanged(false); |
| 586 | record.setSeen(); |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 587 | assertFalse(record.isInterruptive()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 588 | } |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 589 | |
| 590 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 591 | public void testCalculateGrantableUris_PappProvided() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 592 | IActivityManager am = mock(IActivityManager.class); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 593 | UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class); |
| 594 | when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class), |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 595 | anyInt(), anyInt())).thenThrow(new SecurityException()); |
| 596 | |
| 597 | Notification n = mock(Notification.class); |
| 598 | when(n.getChannelId()).thenReturn(channel.getId()); |
| 599 | StatusBarNotification sbn = |
| 600 | new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid); |
| 601 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 602 | record.mAm = am; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 603 | record.mUgmInternal = ugm; |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 604 | |
| 605 | try { |
| 606 | record.calculateGrantableUris(); |
| 607 | fail("App provided uri for p targeting app should throw exception"); |
| 608 | } catch (SecurityException e) { |
| 609 | // expected |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 614 | public void testCalculateGrantableUris_PuserOverridden() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 615 | IActivityManager am = mock(IActivityManager.class); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 616 | UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class); |
| 617 | when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class), |
| 618 | anyInt(), anyInt())).thenThrow(new SecurityException()); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 619 | |
| 620 | channel.lockFields(NotificationChannel.USER_LOCKED_SOUND); |
| 621 | Notification n = mock(Notification.class); |
| 622 | when(n.getChannelId()).thenReturn(channel.getId()); |
| 623 | StatusBarNotification sbn = |
| 624 | new StatusBarNotification(PKG_P, PKG_P, id1, tag1, uid, uid, n, mUser, null, uid); |
| 625 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 626 | record.mAm = am; |
| 627 | |
| 628 | record.calculateGrantableUris(); |
| 629 | } |
| 630 | |
| 631 | @Test |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 632 | public void testCalculateGrantableUris_prePappProvided() { |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 633 | IActivityManager am = mock(IActivityManager.class); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 634 | UriGrantsManagerInternal ugm = mock(UriGrantsManagerInternal.class); |
| 635 | when(ugm.checkGrantUriPermission(anyInt(), eq(null), any(Uri.class), |
| 636 | anyInt(), anyInt())).thenThrow(new SecurityException()); |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 637 | |
| 638 | Notification n = mock(Notification.class); |
| 639 | when(n.getChannelId()).thenReturn(channel.getId()); |
| 640 | StatusBarNotification sbn = |
| 641 | new StatusBarNotification(PKG_O, PKG_O, id1, tag1, uid, uid, n, mUser, null, uid); |
| 642 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 643 | record.mAm = am; |
| 644 | |
| 645 | record.calculateGrantableUris(); |
| 646 | // should not throw |
| 647 | } |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 648 | |
| 649 | @Test |
| 650 | public void testSmartActions() { |
| 651 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 652 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 653 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 654 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 655 | assertNull(record.getSystemGeneratedSmartActions()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 656 | |
| 657 | ArrayList<Notification.Action> smartActions = new ArrayList<>(); |
| 658 | smartActions.add(new Notification.Action.Builder( |
| 659 | Icon.createWithResource(getContext(), R.drawable.btn_default), |
| 660 | "text", null).build()); |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 661 | record.setSystemGeneratedSmartActions(smartActions); |
| 662 | assertEquals(smartActions, record.getSystemGeneratedSmartActions()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 663 | } |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 664 | |
| 665 | @Test |
| 666 | public void testUpdateNotificationChannel() { |
| 667 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 668 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 669 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 670 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 671 | |
| 672 | assertEquals(channel.getImportance(), record.getImportance()); |
| 673 | |
| 674 | record.updateNotificationChannel( |
| 675 | new NotificationChannel(channelId, "", channel.getImportance() - 1)); |
| 676 | |
| 677 | assertEquals(channel.getImportance() - 1, record.getImportance()); |
| 678 | } |
| 679 | |
| 680 | @Test |
| 681 | public void testCalculateImportance_systemImportance() { |
| 682 | channel.setImportance(IMPORTANCE_HIGH); |
| 683 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 684 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 685 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 686 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 687 | |
| 688 | record.setSystemImportance(IMPORTANCE_LOW); |
| 689 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
| 690 | |
| 691 | record = new NotificationRecord(mMockContext, sbn, channel); |
| 692 | channel.lockFields(USER_LOCKED_IMPORTANCE); |
| 693 | |
| 694 | record.setSystemImportance(IMPORTANCE_LOW); |
| 695 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
| 696 | } |
| 697 | |
| 698 | @Test |
| 699 | public void testCalculateImportance_asstImportance() { |
| 700 | channel.setImportance(IMPORTANCE_HIGH); |
| 701 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 702 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 703 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 704 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 705 | |
| 706 | record.setAssistantImportance(IMPORTANCE_LOW); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 707 | record.calculateImportance(); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 708 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
| 709 | |
| 710 | // assistant ignored if user expressed preference |
| 711 | record = new NotificationRecord(mMockContext, sbn, channel); |
| 712 | channel.lockFields(USER_LOCKED_IMPORTANCE); |
| 713 | |
| 714 | record.setAssistantImportance(IMPORTANCE_LOW); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 715 | record.calculateImportance(); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 716 | assertEquals(channel.getImportance(), record.getImportance()); |
| 717 | } |
| 718 | |
| 719 | @Test |
| 720 | public void testCalculateImportance_asstImportanceChannelUpdate() { |
| 721 | channel.setImportance(IMPORTANCE_HIGH); |
| 722 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 723 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 724 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 725 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 726 | |
| 727 | record.setAssistantImportance(IMPORTANCE_LOW); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 728 | record.calculateImportance(); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 729 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
| 730 | |
| 731 | record.updateNotificationChannel( |
| 732 | new NotificationChannel(channelId, "", IMPORTANCE_DEFAULT)); |
| 733 | |
| 734 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
| 735 | } |
Julia Reynolds | 8a7e4ac | 2018-10-08 09:23:35 -0400 | [diff] [blame] | 736 | |
| 737 | @Test |
| 738 | public void testSetContactAffinity() { |
| 739 | channel.setImportance(IMPORTANCE_LOW); |
| 740 | channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 741 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 742 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 743 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 744 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 745 | |
| 746 | record.setContactAffinity(1.0f); |
| 747 | |
| 748 | assertEquals(1.0f, record.getContactAffinity()); |
| 749 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
| 750 | } |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 751 | |
| 752 | @Test |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 753 | public void testSetDidNotAudiblyAlert() { |
| 754 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 755 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 756 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 757 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 758 | |
| 759 | record.setAudiblyAlerted(false); |
| 760 | |
| 761 | assertEquals(-1, record.getLastAudiblyAlertedMs()); |
| 762 | } |
| 763 | |
| 764 | @Test |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 765 | public void testSetAudiblyAlerted() { |
| 766 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 767 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 768 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 769 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 770 | |
| 771 | record.setAudiblyAlerted(true); |
| 772 | |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 773 | assertNotEquals(-1, record.getLastAudiblyAlertedMs()); |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 774 | } |
Julia Reynolds | d673007 | 2019-01-04 12:52:52 -0500 | [diff] [blame] | 775 | |
| 776 | @Test |
| 777 | public void testIsNewEnoughForAlerting_new() { |
| 778 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 779 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 780 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 781 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 782 | |
| 783 | assertTrue(record.isNewEnoughForAlerting(record.mUpdateTimeMs)); |
| 784 | } |
| 785 | |
| 786 | @Test |
| 787 | public void testIsNewEnoughForAlerting_old() { |
| 788 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 789 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 790 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 791 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 792 | |
| 793 | assertFalse(record.isNewEnoughForAlerting(record.mUpdateTimeMs + (1000 * 60 * 60))); |
| 794 | } |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 795 | |
| 796 | @Test |
| 797 | public void testIgnoreImportanceAdjustmentsForOemLockedChannels() { |
| 798 | NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 799 | channel.setImportanceLockedByOEM(true); |
| 800 | |
| 801 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 802 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 803 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 804 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 805 | |
| 806 | assertEquals(IMPORTANCE_DEFAULT, record.getImportance()); |
| 807 | |
| 808 | Bundle bundle = new Bundle(); |
| 809 | bundle.putInt(Adjustment.KEY_IMPORTANCE, IMPORTANCE_LOW); |
| 810 | Adjustment adjustment = new Adjustment( |
| 811 | PKG_O, record.getKey(), bundle, "", record.getUserId()); |
| 812 | |
| 813 | record.addAdjustment(adjustment); |
| 814 | record.applyAdjustments(); |
| 815 | record.calculateImportance(); |
| 816 | |
| 817 | assertEquals(IMPORTANCE_DEFAULT, record.getImportance()); |
| 818 | } |
| 819 | |
| 820 | @Test |
| 821 | public void testApplyImportanceAdjustmentsForNonOemLockedChannels() { |
| 822 | NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 823 | channel.setImportanceLockedByOEM(false); |
| 824 | |
| 825 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
| 826 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 827 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 828 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
| 829 | |
| 830 | assertEquals(IMPORTANCE_DEFAULT, record.getImportance()); |
| 831 | |
| 832 | Bundle bundle = new Bundle(); |
| 833 | bundle.putInt(Adjustment.KEY_IMPORTANCE, IMPORTANCE_LOW); |
| 834 | Adjustment adjustment = new Adjustment( |
| 835 | PKG_O, record.getKey(), bundle, "", record.getUserId()); |
| 836 | |
| 837 | record.addAdjustment(adjustment); |
| 838 | record.applyAdjustments(); |
| 839 | record.calculateImportance(); |
| 840 | |
| 841 | assertEquals(IMPORTANCE_LOW, record.getImportance()); |
| 842 | } |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 843 | } |