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 | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 19 | import static android.service.notification.NotificationListenerService.Ranking |
| 20 | .USER_SENTIMENT_NEGATIVE; |
| 21 | import static android.service.notification.NotificationListenerService.Ranking |
| 22 | .USER_SENTIMENT_NEUTRAL; |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 23 | import static android.service.notification.NotificationListenerService.Ranking |
| 24 | .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 | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 32 | import static org.junit.Assert.fail; |
| 33 | import static org.mockito.ArgumentMatchers.any; |
| 34 | import static org.mockito.ArgumentMatchers.anyInt; |
| 35 | import static org.mockito.ArgumentMatchers.eq; |
| 36 | import static org.mockito.Mockito.mock; |
| 37 | import static org.mockito.Mockito.times; |
| 38 | import static org.mockito.Mockito.verify; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 39 | import static org.mockito.Mockito.when; |
| 40 | |
| 41 | import android.app.ActivityManager; |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 42 | import android.app.IActivityManager; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 43 | import android.app.Notification; |
| 44 | import android.app.Notification.Builder; |
| 45 | import android.app.NotificationChannel; |
| 46 | import android.app.NotificationManager; |
| 47 | import android.content.Context; |
| 48 | import android.content.pm.ApplicationInfo; |
| 49 | import android.content.pm.PackageManager; |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 50 | import android.graphics.Color; |
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 | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 55 | import android.os.RemoteException; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 56 | import android.os.UserHandle; |
| 57 | import android.provider.Settings; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 58 | import android.service.notification.Adjustment; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 59 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 60 | import android.support.test.runner.AndroidJUnit4; |
| 61 | import android.test.suitebuilder.annotation.SmallTest; |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 62 | import android.util.Slog; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 63 | |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 64 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 65 | import com.android.server.UiServiceTestCase; |
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 | |
| 73 | import java.util.Objects; |
| 74 | |
| 75 | @SmallTest |
| 76 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 77 | public class NotificationRecordTest extends UiServiceTestCase { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 78 | |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 79 | private final Context mMockContext = mock(Context.class); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 80 | @Mock PackageManager mPm; |
| 81 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 82 | private final String pkg = PKG_N_MR1; |
Julia Reynolds | 19ad747 | 2016-12-12 08:51:08 -0500 | [diff] [blame] | 83 | private final int uid = 9583; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 84 | private final String pkg2 = PKG_O; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 85 | 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 Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 93 | 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 Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 101 | 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 Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 105 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 106 | 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 Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 110 | 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] | 111 | 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 Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 116 | private static final NotificationRecord.Light CUSTOM_LIGHT = |
| 117 | new NotificationRecord.Light(1, 2, 3); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 118 | |
| 119 | @Before |
| 120 | public void setUp() { |
| 121 | MockitoAnnotations.initMocks(this); |
| 122 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 123 | when(mMockContext.getResources()).thenReturn(getContext().getResources()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 124 | when(mMockContext.getPackageManager()).thenReturn(mPm); |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 125 | when(mMockContext.getApplicationInfo()).thenReturn(new ApplicationInfo()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 128 | private StatusBarNotification getNotification(String pkg, boolean noisy, boolean defaultSound, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 129 | boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights, |
| 130 | String group) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 131 | 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 Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 142 | channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 143 | } |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 144 | } else { |
| 145 | channel.setSound(null, null); |
| 146 | builder.setSound(null, null); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 147 | } |
| 148 | if (buzzy) { |
| 149 | if (defaultVibration) { |
| 150 | defaults |= Notification.DEFAULT_VIBRATE; |
| 151 | } else { |
| 152 | builder.setVibrate(CUSTOM_VIBRATION); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 153 | channel.setVibrationPattern(CUSTOM_CHANNEL_VIBRATION); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 154 | } |
| 155 | } |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 156 | 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 Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 161 | channel.setLightColor(Color.BLUE); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 162 | } |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 163 | channel.enableLights(true); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 164 | } else { |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 165 | channel.enableLights(false); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 168 | builder.setDefaults(defaults); |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 169 | builder.setChannelId(channelId); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 170 | |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 171 | if(group != null) { |
| 172 | builder.setGroup(group); |
| 173 | } |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 174 | |
| 175 | Notification n = builder.build(); |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 176 | 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] | 177 | } |
| 178 | |
| 179 | // |
| 180 | // Tests |
| 181 | // |
| 182 | |
| 183 | @Test |
| 184 | public void testSound_default_preUpgradeUsesNotification() throws Exception { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 185 | defaultChannel.setSound(null, null); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 186 | // pre upgrade, default sound. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 187 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 188 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 189 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 190 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 191 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 192 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 193 | assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void testSound_custom_preUpgradeUsesNotification() throws Exception { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 198 | defaultChannel.setSound(null, null); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 199 | // pre upgrade, custom sound. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 200 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 201 | false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 202 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 203 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 204 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 205 | assertEquals(CUSTOM_SOUND, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 206 | assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testSound_default_userLocked_preUpgrade() throws Exception { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 211 | defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 212 | defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND); |
| 213 | // pre upgrade, default sound. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 214 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 215 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 216 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 217 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 218 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 219 | assertEquals(CUSTOM_SOUND, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 220 | assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Test |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 224 | public void testSound_noSound_preUpgrade() throws Exception { |
| 225 | // pre upgrade, default sound. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 226 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 227 | false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 228 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 229 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 230 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | b9e712e | 2017-04-17 10:31:03 -0400 | [diff] [blame] | 231 | assertEquals(null, record.getSound()); |
| 232 | assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes()); |
| 233 | } |
| 234 | |
| 235 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 236 | public void testSound_default_upgradeUsesChannel() throws Exception { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 237 | channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 238 | // post upgrade, default sound. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 239 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 240 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 241 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 242 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 243 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 244 | assertEquals(CUSTOM_SOUND, record.getSound()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 245 | assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Test |
| 249 | public void testVibration_default_preUpgradeUsesNotification() throws Exception { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 250 | defaultChannel.enableVibration(false); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 251 | // pre upgrade, default vibration. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 252 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 253 | false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 254 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 255 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 256 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 257 | assertNotNull(record.getVibration()); |
| 258 | } |
| 259 | |
| 260 | @Test |
| 261 | public void testVibration_custom_preUpgradeUsesNotification() throws Exception { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 262 | defaultChannel.enableVibration(false); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 263 | // pre upgrade, custom vibration. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 264 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 265 | false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 266 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 267 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 268 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 269 | assertEquals(CUSTOM_VIBRATION, record.getVibration()); |
| 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testVibration_custom_userLocked_preUpgrade() throws Exception { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 274 | defaultChannel.enableVibration(true); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 275 | defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION); |
| 276 | // pre upgrade, custom vibration. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 277 | StatusBarNotification sbn = getNotification(PKG_N_MR1, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 278 | false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 279 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 280 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 281 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 282 | assertTrue(!Objects.equals(CUSTOM_VIBRATION, record.getVibration())); |
| 283 | } |
| 284 | |
| 285 | @Test |
| 286 | public void testVibration_custom_upgradeUsesChannel() throws Exception { |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 287 | channel.enableVibration(true); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 288 | // post upgrade, custom vibration. |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 289 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 290 | false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 291 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 292 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 293 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 294 | assertEquals(CUSTOM_CHANNEL_VIBRATION, record.getVibration()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 298 | public void testImportance_preUpgrade() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 299 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 300 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 301 | false /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 302 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 303 | 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 Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 309 | defaultChannel.lockFields(USER_LOCKED_IMPORTANCE); |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 310 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 311 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 312 | false /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 313 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 314 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 315 | assertEquals(NotificationManager.IMPORTANCE_LOW, record.getImportance()); |
| 316 | } |
| 317 | |
| 318 | @Test |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 319 | public void testImportance_locked_unspecified_preUpgrade() throws Exception { |
| 320 | defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 321 | defaultChannel.lockFields(USER_LOCKED_IMPORTANCE); |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 322 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [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 */); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 325 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 326 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 327 | assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance()); |
| 328 | } |
| 329 | |
| 330 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 331 | public void testImportance_upgrade() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 332 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 333 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 334 | false /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 335 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 336 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, record.getImportance()); |
| 337 | } |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 338 | |
| 339 | @Test |
| 340 | public void testLights_preUpgrade_noLight() throws Exception { |
Julia Reynolds | 89945c5 | 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 | false /* 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 | assertNull(record.getLight()); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | @Test |
| 350 | public void testLights_preUpgrade() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 351 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 352 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 353 | true /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 354 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 355 | assertEquals(CUSTOM_LIGHT, record.getLight()); |
| 356 | } |
| 357 | |
| 358 | @Test |
| 359 | public void testLights_locked_preUpgrade() throws Exception { |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 360 | defaultChannel.enableLights(true); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 361 | defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS); |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 362 | StatusBarNotification sbn = getNotification(PKG_N_MR1, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 363 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 364 | true /* lights */, false /* defaultLights */, null /* group */); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 365 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 366 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 367 | assertFalse(CUSTOM_LIGHT.equals(record.getLight())); |
| 368 | } |
| 369 | |
| 370 | @Test |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 371 | public void testLights_upgrade_defaultLights() throws Exception { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 372 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 381 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 382 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 383 | true /* lights */, true /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 384 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 385 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 397 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 398 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 399 | true /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 400 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 401 | assertEquals(expected, record.getLight()); |
| 402 | } |
| 403 | |
| 404 | @Test |
| 405 | public void testLights_upgrade_noLight() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 406 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 407 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 408 | false /* lights */, false /* defaultLights */, null /* group */); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 409 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 410 | assertNull(record.getLight()); |
| 411 | } |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 412 | |
| 413 | @Test |
| 414 | public void testLogmakerShortChannel() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 415 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 416 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 428 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 429 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 439 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 440 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 448 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 449 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 458 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 459 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 469 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 470 | 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 Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 483 | @Test |
| 484 | public void testNotificationStats() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 485 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 486 | true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, |
| 487 | false /* lights */, false /* defaultLights */, groupId /* group */); |
| 488 | NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 489 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 490 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 528 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 529 | 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 Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 543 | |
| 544 | @Test |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 545 | public void testUserSentiment_appImportanceUpdatesSentiment() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 546 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 547 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 558 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 559 | 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 Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 573 | public void testUserSentiment_userLocked() throws Exception { |
| 574 | channel.lockFields(USER_LOCKED_IMPORTANCE); |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 575 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 576 | 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 Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 590 | |
| 591 | @Test |
| 592 | public void testAppImportance_returnsCorrectly() throws Exception { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 593 | StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 594 | 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 Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 604 | |
| 605 | @Test |
| 606 | public void testIsInterruptive_textChanged_notSeen() { |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 607 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 608 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 620 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 621 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 634 | StatusBarNotification sbn = getNotification(PKG_O, false /* noisy */, |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 635 | 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 Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 645 | |
| 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 Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 700 | } |