Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 18 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
Julia Reynolds | 03fa85d | 2017-03-06 15:14:50 -0500 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 20 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 3e50bf6 | 2017-05-24 16:33:32 -0400 | [diff] [blame] | 21 | import static android.app.NotificationManager.IMPORTANCE_MAX; |
Julia Reynolds | d8c0ac4 | 2017-04-23 13:11:40 -0400 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | fa04e14 | 2017-04-23 13:32:01 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 24 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 25 | import static junit.framework.Assert.assertNull; |
Julia Reynolds | 32c97ef | 2016-11-28 10:47:18 -0500 | [diff] [blame] | 26 | import static junit.framework.Assert.fail; |
| 27 | |
Julia Reynolds | 3d5b3c7 | 2017-11-07 09:04:58 -0500 | [diff] [blame] | 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.junit.Assert.assertFalse; |
| 30 | import static org.junit.Assert.assertNotNull; |
| 31 | import static org.junit.Assert.assertTrue; |
| 32 | import static org.mockito.ArgumentMatchers.any; |
| 33 | import static org.mockito.Matchers.anyInt; |
| 34 | import static org.mockito.Matchers.anyString; |
| 35 | import static org.mockito.Matchers.eq; |
| 36 | import static org.mockito.Mockito.mock; |
| 37 | import static org.mockito.Mockito.never; |
| 38 | import static org.mockito.Mockito.times; |
| 39 | import static org.mockito.Mockito.verify; |
| 40 | import static org.mockito.Mockito.when; |
| 41 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 42 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 43 | import android.app.NotificationChannel; |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 44 | import android.app.NotificationChannelGroup; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 45 | import android.app.NotificationManager; |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 46 | import android.content.ContentProvider; |
| 47 | import android.content.Context; |
| 48 | import android.content.IContentProvider; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 49 | import android.content.pm.ApplicationInfo; |
| 50 | import android.content.pm.PackageManager; |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 51 | import android.content.res.Resources; |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 52 | import android.graphics.Color; |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 53 | import android.media.AudioAttributes; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 54 | import android.net.Uri; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 55 | import android.os.Build; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 56 | import android.os.UserHandle; |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 57 | import android.provider.Settings; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 58 | import android.provider.Settings.Secure; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 59 | import android.service.notification.StatusBarNotification; |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 60 | import android.support.test.InstrumentationRegistry; |
| 61 | import android.support.test.runner.AndroidJUnit4; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 62 | import android.test.suitebuilder.annotation.SmallTest; |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 63 | import android.testing.TestableContentResolver; |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 64 | import android.util.ArrayMap; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 65 | import android.util.Xml; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 66 | |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 67 | import com.android.internal.util.FastXmlSerializer; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 68 | import com.android.server.UiServiceTestCase; |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 69 | |
| 70 | import org.json.JSONArray; |
| 71 | import org.json.JSONObject; |
| 72 | import org.junit.Before; |
| 73 | import org.junit.Test; |
| 74 | import org.junit.runner.RunWith; |
| 75 | import org.mockito.Mock; |
| 76 | import org.mockito.MockitoAnnotations; |
| 77 | import org.xmlpull.v1.XmlPullParser; |
| 78 | import org.xmlpull.v1.XmlSerializer; |
| 79 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 80 | import java.io.BufferedInputStream; |
| 81 | import java.io.BufferedOutputStream; |
| 82 | import java.io.ByteArrayInputStream; |
| 83 | import java.io.ByteArrayOutputStream; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 84 | import java.util.ArrayList; |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 85 | import java.util.Arrays; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 86 | import java.util.HashMap; |
| 87 | import java.util.List; |
| 88 | import java.util.Map; |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 89 | import java.util.Objects; |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 90 | import java.util.concurrent.ThreadLocalRandom; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 91 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 92 | @SmallTest |
| 93 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 94 | public class RankingHelperTest extends UiServiceTestCase { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 95 | private static final String PKG = "com.android.server.notification"; |
| 96 | private static final int UID = 0; |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 97 | private static final UserHandle USER = UserHandle.of(0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 98 | private static final String UPDATED_PKG = "updatedPkg"; |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 99 | private static final int UID2 = 1111; |
| 100 | private static final UserHandle USER2 = UserHandle.of(10); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 101 | private static final String TEST_CHANNEL_ID = "test_channel_id"; |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 102 | private static final String TEST_AUTHORITY = "test"; |
| 103 | private static final Uri SOUND_URI = |
| 104 | Uri.parse("content://" + TEST_AUTHORITY + "/internal/audio/media/10"); |
| 105 | private static final Uri CANONICAL_SOUND_URI = |
| 106 | Uri.parse("content://" + TEST_AUTHORITY |
| 107 | + "/internal/audio/media/10?title=Test&canonical=1"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 108 | |
| 109 | @Mock NotificationUsageStats mUsageStats; |
| 110 | @Mock RankingHandler mHandler; |
| 111 | @Mock PackageManager mPm; |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 112 | @Mock IContentProvider mTestIContentProvider; |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 113 | @Mock Context mContext; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 114 | |
| 115 | private Notification mNotiGroupGSortA; |
| 116 | private Notification mNotiGroupGSortB; |
| 117 | private Notification mNotiNoGroup; |
| 118 | private Notification mNotiNoGroup2; |
| 119 | private Notification mNotiNoGroupSortA; |
| 120 | private NotificationRecord mRecordGroupGSortA; |
| 121 | private NotificationRecord mRecordGroupGSortB; |
| 122 | private NotificationRecord mRecordNoGroup; |
| 123 | private NotificationRecord mRecordNoGroup2; |
| 124 | private NotificationRecord mRecordNoGroupSortA; |
| 125 | private RankingHelper mHelper; |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 126 | private AudioAttributes mAudioAttributes; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 127 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 128 | @Before |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 129 | public void setUp() throws Exception { |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 130 | MockitoAnnotations.initMocks(this); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 131 | UserHandle user = UserHandle.ALL; |
| 132 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 133 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 134 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
Julia Reynolds | 1864ff5 | 2016-11-02 09:54:47 -0400 | [diff] [blame] | 135 | final ApplicationInfo upgrade = new ApplicationInfo(); |
Dan Sandler | e103b78 | 2017-05-17 16:07:56 -0700 | [diff] [blame] | 136 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 137 | when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(legacy); |
| 138 | when(mPm.getApplicationInfoAsUser(eq(UPDATED_PKG), anyInt(), anyInt())).thenReturn(upgrade); |
| 139 | when(mPm.getPackageUidAsUser(eq(PKG), anyInt())).thenReturn(UID); |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 140 | when(mPm.getPackageUidAsUser(eq(UPDATED_PKG), anyInt())).thenReturn(UID2); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 141 | when(mContext.getResources()).thenReturn( |
| 142 | InstrumentationRegistry.getContext().getResources()); |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 143 | when(mContext.getContentResolver()).thenReturn( |
| 144 | InstrumentationRegistry.getContext().getContentResolver()); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 145 | when(mContext.getPackageManager()).thenReturn(mPm); |
| 146 | when(mContext.getApplicationInfo()).thenReturn(legacy); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 147 | // most tests assume badging is enabled |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 148 | TestableContentResolver contentResolver = getContext().getContentResolver(); |
| 149 | contentResolver.setFallbackToExisting(false); |
| 150 | Secure.putIntForUser(contentResolver, |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 151 | Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID)); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 152 | |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 153 | ContentProvider testContentProvider = mock(ContentProvider.class); |
| 154 | when(testContentProvider.getIContentProvider()).thenReturn(mTestIContentProvider); |
| 155 | contentResolver.addProvider(TEST_AUTHORITY, testContentProvider); |
| 156 | |
| 157 | when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))) |
| 158 | .thenReturn(CANONICAL_SOUND_URI); |
| 159 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 160 | .thenReturn(CANONICAL_SOUND_URI); |
| 161 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 162 | .thenReturn(SOUND_URI); |
| 163 | |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 164 | mHelper = new RankingHelper(getContext(), mPm, mHandler, mUsageStats, |
| 165 | new String[] {ImportanceExtractor.class.getName()}); |
| 166 | |
| 167 | mNotiGroupGSortA = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 168 | .setContentTitle("A") |
| 169 | .setGroup("G") |
| 170 | .setSortKey("A") |
| 171 | .setWhen(1205) |
| 172 | .build(); |
| 173 | mRecordGroupGSortA = new NotificationRecord(mContext, new StatusBarNotification( |
| 174 | PKG, PKG, 1, null, 0, 0, mNotiGroupGSortA, user, |
| 175 | null, System.currentTimeMillis()), getDefaultChannel()); |
| 176 | |
| 177 | mNotiGroupGSortB = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 178 | .setContentTitle("B") |
| 179 | .setGroup("G") |
| 180 | .setSortKey("B") |
| 181 | .setWhen(1200) |
| 182 | .build(); |
| 183 | mRecordGroupGSortB = new NotificationRecord(mContext, new StatusBarNotification( |
| 184 | PKG, PKG, 1, null, 0, 0, mNotiGroupGSortB, user, |
| 185 | null, System.currentTimeMillis()), getDefaultChannel()); |
| 186 | |
| 187 | mNotiNoGroup = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 188 | .setContentTitle("C") |
| 189 | .setWhen(1201) |
| 190 | .build(); |
| 191 | mRecordNoGroup = new NotificationRecord(mContext, new StatusBarNotification( |
| 192 | PKG, PKG, 1, null, 0, 0, mNotiNoGroup, user, |
| 193 | null, System.currentTimeMillis()), getDefaultChannel()); |
| 194 | |
| 195 | mNotiNoGroup2 = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 196 | .setContentTitle("D") |
| 197 | .setWhen(1202) |
| 198 | .build(); |
| 199 | mRecordNoGroup2 = new NotificationRecord(mContext, new StatusBarNotification( |
| 200 | PKG, PKG, 1, null, 0, 0, mNotiNoGroup2, user, |
| 201 | null, System.currentTimeMillis()), getDefaultChannel()); |
| 202 | |
| 203 | mNotiNoGroupSortA = new Notification.Builder(mContext, TEST_CHANNEL_ID) |
| 204 | .setContentTitle("E") |
| 205 | .setWhen(1201) |
| 206 | .setSortKey("A") |
| 207 | .build(); |
| 208 | mRecordNoGroupSortA = new NotificationRecord(mContext, new StatusBarNotification( |
| 209 | PKG, PKG, 1, null, 0, 0, mNotiNoGroupSortA, user, |
| 210 | null, System.currentTimeMillis()), getDefaultChannel()); |
| 211 | |
| 212 | mAudioAttributes = new AudioAttributes.Builder() |
| 213 | .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) |
| 214 | .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) |
| 215 | .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) |
| 216 | .build(); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 219 | private NotificationChannel getDefaultChannel() { |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 220 | return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name", |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 221 | IMPORTANCE_LOW); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 224 | private ByteArrayOutputStream writeXmlAndPurge(String pkg, int uid, boolean forBackup, |
| 225 | String... channelIds) |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 226 | throws Exception { |
| 227 | XmlSerializer serializer = new FastXmlSerializer(); |
| 228 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 229 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 230 | serializer.startDocument(null, true); |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 231 | mHelper.writeXml(serializer, forBackup); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 232 | serializer.endDocument(); |
| 233 | serializer.flush(); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 234 | for (String channelId : channelIds) { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 235 | mHelper.permanentlyDeleteNotificationChannel(pkg, uid, channelId); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 236 | } |
| 237 | return baos; |
| 238 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 239 | |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 240 | private void loadStreamXml(ByteArrayOutputStream stream, boolean forRestore) throws Exception { |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 241 | loadByteArrayXml(stream.toByteArray(), forRestore); |
| 242 | } |
| 243 | |
| 244 | private void loadByteArrayXml(byte[] byteArray, boolean forRestore) throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 245 | XmlPullParser parser = Xml.newPullParser(); |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 246 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(byteArray)), null); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 247 | parser.nextTag(); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 248 | mHelper.readXml(parser, forRestore); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 251 | private void compareChannels(NotificationChannel expected, NotificationChannel actual) { |
| 252 | assertEquals(expected.getId(), actual.getId()); |
| 253 | assertEquals(expected.getName(), actual.getName()); |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 254 | assertEquals(expected.getDescription(), actual.getDescription()); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 255 | assertEquals(expected.shouldVibrate(), actual.shouldVibrate()); |
| 256 | assertEquals(expected.shouldShowLights(), actual.shouldShowLights()); |
| 257 | assertEquals(expected.getImportance(), actual.getImportance()); |
| 258 | assertEquals(expected.getLockscreenVisibility(), actual.getLockscreenVisibility()); |
| 259 | assertEquals(expected.getSound(), actual.getSound()); |
| 260 | assertEquals(expected.canBypassDnd(), actual.canBypassDnd()); |
| 261 | assertTrue(Arrays.equals(expected.getVibrationPattern(), actual.getVibrationPattern())); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 262 | assertEquals(expected.getGroup(), actual.getGroup()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 263 | assertEquals(expected.getAudioAttributes(), actual.getAudioAttributes()); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 264 | assertEquals(expected.getLightColor(), actual.getLightColor()); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Julia Reynolds | d528684 | 2017-03-02 15:37:10 -0500 | [diff] [blame] | 267 | private void compareGroups(NotificationChannelGroup expected, NotificationChannelGroup actual) { |
| 268 | assertEquals(expected.getId(), actual.getId()); |
| 269 | assertEquals(expected.getName(), actual.getName()); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 270 | assertEquals(expected.getDescription(), actual.getDescription()); |
| 271 | assertEquals(expected.isBlocked(), actual.isBlocked()); |
Julia Reynolds | d528684 | 2017-03-02 15:37:10 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 274 | private NotificationChannel getChannel() { |
| 275 | return new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 276 | } |
| 277 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 278 | private NotificationChannel findChannel(List<NotificationChannel> channels, String id) { |
| 279 | for (NotificationChannel channel : channels) { |
| 280 | if (channel.getId().equals(id)) { |
| 281 | return channel; |
| 282 | } |
| 283 | } |
| 284 | return null; |
| 285 | } |
| 286 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 287 | @Test |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 288 | public void testFindAfterRankingWithASplitGroup() throws Exception { |
| 289 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(3); |
| 290 | notificationList.add(mRecordGroupGSortA); |
| 291 | notificationList.add(mRecordGroupGSortB); |
| 292 | notificationList.add(mRecordNoGroup); |
| 293 | notificationList.add(mRecordNoGroupSortA); |
| 294 | mHelper.sort(notificationList); |
| 295 | assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortA) >= 0); |
| 296 | assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortB) >= 0); |
| 297 | assertTrue(mHelper.indexOf(notificationList, mRecordNoGroup) >= 0); |
| 298 | assertTrue(mHelper.indexOf(notificationList, mRecordNoGroupSortA) >= 0); |
| 299 | } |
| 300 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 301 | @Test |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 302 | public void testSortShouldNotThrowWithPlainNotifications() throws Exception { |
| 303 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2); |
| 304 | notificationList.add(mRecordNoGroup); |
| 305 | notificationList.add(mRecordNoGroup2); |
| 306 | mHelper.sort(notificationList); |
| 307 | } |
| 308 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 309 | @Test |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 310 | public void testSortShouldNotThrowOneSorted() throws Exception { |
| 311 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2); |
| 312 | notificationList.add(mRecordNoGroup); |
| 313 | notificationList.add(mRecordNoGroupSortA); |
| 314 | mHelper.sort(notificationList); |
| 315 | } |
| 316 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 317 | @Test |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 318 | public void testSortShouldNotThrowOneNotification() throws Exception { |
| 319 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1); |
| 320 | notificationList.add(mRecordNoGroup); |
| 321 | mHelper.sort(notificationList); |
| 322 | } |
| 323 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 324 | @Test |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 325 | public void testSortShouldNotThrowOneSortKey() throws Exception { |
| 326 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1); |
| 327 | notificationList.add(mRecordGroupGSortB); |
| 328 | mHelper.sort(notificationList); |
| 329 | } |
| 330 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 331 | @Test |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 332 | public void testSortShouldNotThrowOnEmptyList() throws Exception { |
| 333 | ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(); |
| 334 | mHelper.sort(notificationList); |
| 335 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 336 | |
| 337 | @Test |
| 338 | public void testChannelXml() throws Exception { |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 339 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 340 | ncg.setBlocked(true); |
| 341 | ncg.setDescription("group desc"); |
Julia Reynolds | d528684 | 2017-03-02 15:37:10 -0500 | [diff] [blame] | 342 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 343 | NotificationChannel channel1 = |
| 344 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 345 | NotificationChannel channel2 = |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 346 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 347 | channel2.setDescription("descriptions for all"); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 348 | channel2.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 349 | channel2.enableLights(true); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 350 | channel2.setBypassDnd(true); |
| 351 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 352 | channel2.enableVibration(true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 353 | channel2.setGroup(ncg.getId()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 354 | channel2.setVibrationPattern(new long[]{100, 67, 145, 156}); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 355 | channel2.setLightColor(Color.BLUE); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 356 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 357 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
| 358 | mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); |
| 359 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
| 360 | mHelper.createNotificationChannel(PKG, UID, channel2, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 361 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 362 | mHelper.setShowBadge(PKG, UID, true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 363 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 364 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, channel1.getId(), |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 365 | channel2.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 366 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG}, new int[]{UID}); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 367 | |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 368 | loadStreamXml(baos, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 369 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 370 | assertTrue(mHelper.canShowBadge(PKG, UID)); |
| 371 | assertEquals(channel1, mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false)); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 372 | compareChannels(channel2, |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 373 | mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 374 | |
| 375 | List<NotificationChannelGroup> actualGroups = |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 376 | mHelper.getNotificationChannelGroups(PKG, UID, false, true).getList(); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 377 | boolean foundNcg = false; |
| 378 | for (NotificationChannelGroup actual : actualGroups) { |
| 379 | if (ncg.getId().equals(actual.getId())) { |
| 380 | foundNcg = true; |
Julia Reynolds | d528684 | 2017-03-02 15:37:10 -0500 | [diff] [blame] | 381 | compareGroups(ncg, actual); |
| 382 | } else if (ncg2.getId().equals(actual.getId())) { |
| 383 | compareGroups(ncg2, actual); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | assertTrue(foundNcg); |
| 387 | |
| 388 | boolean foundChannel2Group = false; |
| 389 | for (NotificationChannelGroup actual : actualGroups) { |
| 390 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 391 | foundChannel2Group = true; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | assertTrue(foundChannel2Group); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 396 | } |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 397 | |
| 398 | @Test |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 399 | public void testChannelXmlForBackup() throws Exception { |
| 400 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 401 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 402 | NotificationChannel channel1 = |
| 403 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 404 | NotificationChannel channel2 = |
| 405 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 406 | channel2.setDescription("descriptions for all"); |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 407 | channel2.setSound(SOUND_URI, mAudioAttributes); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 408 | channel2.enableLights(true); |
| 409 | channel2.setBypassDnd(true); |
| 410 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 411 | channel2.enableVibration(false); |
| 412 | channel2.setGroup(ncg.getId()); |
| 413 | channel2.setLightColor(Color.BLUE); |
Julia Reynolds | 745c154 | 2017-05-26 14:43:47 -0400 | [diff] [blame] | 414 | NotificationChannel channel3 = new NotificationChannel("id3", "NAM3", IMPORTANCE_HIGH); |
| 415 | channel3.enableVibration(true); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 416 | |
| 417 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
| 418 | mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); |
| 419 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
| 420 | mHelper.createNotificationChannel(PKG, UID, channel2, false); |
Julia Reynolds | 745c154 | 2017-05-26 14:43:47 -0400 | [diff] [blame] | 421 | mHelper.createNotificationChannel(PKG, UID, channel3, false); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 422 | mHelper.createNotificationChannel(UPDATED_PKG, UID2, getChannel(), true); |
| 423 | |
| 424 | mHelper.setShowBadge(PKG, UID, true); |
| 425 | |
| 426 | mHelper.setImportance(UPDATED_PKG, UID2, IMPORTANCE_NONE); |
| 427 | |
| 428 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel1.getId(), |
Julia Reynolds | 745c154 | 2017-05-26 14:43:47 -0400 | [diff] [blame] | 429 | channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); |
| 430 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG, UPDATED_PKG}, |
| 431 | new int[]{UID, UID2}); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 432 | |
| 433 | mHelper.setShowBadge(UPDATED_PKG, UID2, true); |
| 434 | |
| 435 | loadStreamXml(baos, true); |
| 436 | |
| 437 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(UPDATED_PKG, UID2)); |
| 438 | assertTrue(mHelper.canShowBadge(PKG, UID)); |
| 439 | assertEquals(channel1, mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false)); |
| 440 | compareChannels(channel2, |
| 441 | mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); |
Julia Reynolds | 745c154 | 2017-05-26 14:43:47 -0400 | [diff] [blame] | 442 | compareChannels(channel3, |
| 443 | mHelper.getNotificationChannel(PKG, UID, channel3.getId(), false)); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 444 | |
| 445 | List<NotificationChannelGroup> actualGroups = |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 446 | mHelper.getNotificationChannelGroups(PKG, UID, false, true).getList(); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 447 | boolean foundNcg = false; |
| 448 | for (NotificationChannelGroup actual : actualGroups) { |
| 449 | if (ncg.getId().equals(actual.getId())) { |
| 450 | foundNcg = true; |
| 451 | compareGroups(ncg, actual); |
| 452 | } else if (ncg2.getId().equals(actual.getId())) { |
| 453 | compareGroups(ncg2, actual); |
| 454 | } |
| 455 | } |
| 456 | assertTrue(foundNcg); |
| 457 | |
| 458 | boolean foundChannel2Group = false; |
| 459 | for (NotificationChannelGroup actual : actualGroups) { |
| 460 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 461 | foundChannel2Group = true; |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | assertTrue(foundChannel2Group); |
| 466 | } |
| 467 | |
| 468 | @Test |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 469 | public void testBackupXml_backupCanonicalizedSoundUri() throws Exception { |
| 470 | NotificationChannel channel = |
| 471 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 472 | channel.setSound(SOUND_URI, mAudioAttributes); |
| 473 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 474 | |
| 475 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId()); |
| 476 | |
| 477 | // Testing that in restore we are given the canonical version |
| 478 | loadStreamXml(baos, true); |
| 479 | verify(mTestIContentProvider).uncanonicalize(any(), eq(CANONICAL_SOUND_URI)); |
| 480 | } |
| 481 | |
| 482 | @Test |
| 483 | public void testRestoreXml_withExistentCanonicalizedSoundUri() throws Exception { |
| 484 | Uri localUri = Uri.parse("content://" + TEST_AUTHORITY + "/local/url"); |
| 485 | Uri canonicalBasedOnLocal = localUri.buildUpon() |
| 486 | .appendQueryParameter("title", "Test") |
| 487 | .appendQueryParameter("canonical", "1") |
| 488 | .build(); |
| 489 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 490 | .thenReturn(canonicalBasedOnLocal); |
| 491 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 492 | .thenReturn(localUri); |
| 493 | when(mTestIContentProvider.uncanonicalize(any(), eq(canonicalBasedOnLocal))) |
| 494 | .thenReturn(localUri); |
| 495 | |
| 496 | NotificationChannel channel = |
| 497 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 498 | channel.setSound(SOUND_URI, mAudioAttributes); |
| 499 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 500 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId()); |
| 501 | |
| 502 | loadStreamXml(baos, true); |
| 503 | |
| 504 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
| 505 | PKG, UID, channel.getId(), false); |
| 506 | assertEquals(localUri, actualChannel.getSound()); |
| 507 | } |
| 508 | |
| 509 | @Test |
| 510 | public void testRestoreXml_withNonExistentCanonicalizedSoundUri() throws Exception { |
| 511 | Thread.sleep(3000); |
| 512 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 513 | .thenReturn(null); |
| 514 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 515 | .thenReturn(null); |
| 516 | |
| 517 | NotificationChannel channel = |
| 518 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 519 | channel.setSound(SOUND_URI, mAudioAttributes); |
| 520 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 521 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId()); |
| 522 | |
| 523 | loadStreamXml(baos, true); |
| 524 | |
| 525 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
| 526 | PKG, UID, channel.getId(), false); |
| 527 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /** |
| 532 | * Although we don't make backups with uncanonicalized uris anymore, we used to, so we have to |
| 533 | * handle its restore properly. |
| 534 | */ |
| 535 | @Test |
| 536 | public void testRestoreXml_withUncanonicalizedNonLocalSoundUri() throws Exception { |
| 537 | // Not a local uncanonicalized uri, simulating that it fails to exist locally |
| 538 | when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))).thenReturn(null); |
| 539 | String id = "id"; |
| 540 | String backupWithUncanonicalizedSoundUri = "<ranking version=\"1\">\n" |
| 541 | + "<package name=\"com.android.server.notification\" show_badge=\"true\">\n" |
| 542 | + "<channel id=\"" + id + "\" name=\"name\" importance=\"2\" " |
| 543 | + "sound=\"" + SOUND_URI + "\" " |
| 544 | + "usage=\"6\" content_type=\"0\" flags=\"1\" show_badge=\"true\" />\n" |
| 545 | + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" " |
| 546 | + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n" |
| 547 | + "</package>\n" |
| 548 | + "</ranking>\n"; |
| 549 | |
| 550 | loadByteArrayXml(backupWithUncanonicalizedSoundUri.getBytes(), true); |
| 551 | |
| 552 | NotificationChannel actualChannel = mHelper.getNotificationChannel(PKG, UID, id, false); |
| 553 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 554 | } |
| 555 | |
| 556 | @Test |
| 557 | public void testBackupRestoreXml_withNullSoundUri() throws Exception { |
| 558 | NotificationChannel channel = |
| 559 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 560 | channel.setSound(null, mAudioAttributes); |
| 561 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 562 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId()); |
| 563 | |
| 564 | loadStreamXml(baos, true); |
| 565 | |
| 566 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
| 567 | PKG, UID, channel.getId(), false); |
| 568 | assertEquals(null, actualChannel.getSound()); |
| 569 | } |
| 570 | |
| 571 | @Test |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 572 | public void testChannelXml_backup() throws Exception { |
| 573 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 574 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 575 | NotificationChannel channel1 = |
| 576 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 577 | NotificationChannel channel2 = |
| 578 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 579 | NotificationChannel channel3 = |
| 580 | new NotificationChannel("id3", "name3", IMPORTANCE_LOW); |
| 581 | channel3.setGroup(ncg.getId()); |
| 582 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 583 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
| 584 | mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); |
| 585 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
| 586 | mHelper.createNotificationChannel(PKG, UID, channel2, false); |
| 587 | mHelper.createNotificationChannel(PKG, UID, channel3, true); |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 588 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 589 | mHelper.deleteNotificationChannel(PKG, UID, channel1.getId()); |
| 590 | mHelper.deleteNotificationChannelGroup(PKG, UID, ncg.getId()); |
| 591 | assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 592 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 593 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel1.getId(), |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 594 | channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 595 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG}, new int[]{UID}); |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 596 | |
| 597 | XmlPullParser parser = Xml.newPullParser(); |
| 598 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), |
| 599 | null); |
| 600 | parser.nextTag(); |
| 601 | mHelper.readXml(parser, true); |
| 602 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 603 | assertNull(mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false)); |
| 604 | assertNull(mHelper.getNotificationChannel(PKG, UID, channel3.getId(), false)); |
| 605 | assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG, UID)); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 606 | //assertEquals(ncg2, mHelper.getNotificationChannelGroup(ncg2.getId(), PKG, UID)); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 607 | assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | @Test |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 611 | public void testChannelXml_defaultChannelLegacyApp_noUserSettings() throws Exception { |
Geoffrey Pitsch | 5644ccd | 2017-04-17 11:33:24 -0400 | [diff] [blame] | 612 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 613 | NotificationChannel.DEFAULT_CHANNEL_ID); |
| 614 | |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 615 | loadStreamXml(baos, false); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 616 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 617 | final NotificationChannel updated = mHelper.getNotificationChannel(PKG, UID, |
| 618 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 619 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance()); |
| 620 | assertFalse(updated.canBypassDnd()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 621 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility()); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 622 | assertEquals(0, updated.getUserLockedFields()); |
| 623 | } |
| 624 | |
| 625 | @Test |
Julia Reynolds | b3a04f0 | 2016-11-04 09:08:52 -0400 | [diff] [blame] | 626 | public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 627 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG, UID, |
| 628 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 629 | defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 630 | mHelper.updateNotificationChannel(PKG, UID, defaultChannel, true); |
Julia Reynolds | b3a04f0 | 2016-11-04 09:08:52 -0400 | [diff] [blame] | 631 | |
Geoffrey Pitsch | 5644ccd | 2017-04-17 11:33:24 -0400 | [diff] [blame] | 632 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 633 | NotificationChannel.DEFAULT_CHANNEL_ID); |
| 634 | |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 635 | loadStreamXml(baos, false); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 636 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 637 | assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel( |
| 638 | PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance()); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 639 | } |
| 640 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 641 | @Test |
| 642 | public void testChannelXml_upgradeCreateDefaultChannel() throws Exception { |
Julia Reynolds | 1864ff5 | 2016-11-02 09:54:47 -0400 | [diff] [blame] | 643 | final String preupgradeXml = "<ranking version=\"1\">\n" |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 644 | + "<package name=\"" + PKG |
| 645 | + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 646 | + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\"" |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 647 | + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID + "\" />\n" |
| 648 | + "<package name=\"" + UPDATED_PKG + "\" uid=\"" + UID2 + "\" visibility=\"" |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 649 | + Notification.VISIBILITY_PRIVATE + "\" />\n" |
| 650 | + "</ranking>"; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 651 | XmlPullParser parser = Xml.newPullParser(); |
Julia Reynolds | 1864ff5 | 2016-11-02 09:54:47 -0400 | [diff] [blame] | 652 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 653 | null); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 654 | parser.nextTag(); |
| 655 | mHelper.readXml(parser, false); |
| 656 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 657 | final NotificationChannel updated1 = |
| 658 | mHelper.getNotificationChannel(PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Julia Reynolds | 1864ff5 | 2016-11-02 09:54:47 -0400 | [diff] [blame] | 659 | assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance()); |
| 660 | assertTrue(updated1.canBypassDnd()); |
| 661 | assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility()); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 662 | assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 663 | | NotificationChannel.USER_LOCKED_PRIORITY |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 664 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
| 665 | updated1.getUserLockedFields()); |
Julia Reynolds | 1864ff5 | 2016-11-02 09:54:47 -0400 | [diff] [blame] | 666 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 667 | // No Default Channel created for updated packages |
Geoffrey Pitsch | 5644ccd | 2017-04-17 11:33:24 -0400 | [diff] [blame] | 668 | assertEquals(null, mHelper.getNotificationChannel(UPDATED_PKG, UID2, |
| 669 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | @Test |
| 673 | public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception { |
| 674 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
| 675 | PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 676 | assertTrue(defaultChannel != null); |
| 677 | ByteArrayOutputStream baos = |
| 678 | writeXmlAndPurge(PKG, UID, false, NotificationChannel.DEFAULT_CHANNEL_ID); |
| 679 | // Load package at higher sdk. |
| 680 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 681 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
| 682 | when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(upgraded); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 683 | loadStreamXml(baos, false); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 684 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 685 | // Default Channel should be gone. |
Geoffrey Pitsch | 5644ccd | 2017-04-17 11:33:24 -0400 | [diff] [blame] | 686 | assertEquals(null, mHelper.getNotificationChannel(PKG, UID, |
| 687 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | @Test |
| 691 | public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception { |
| 692 | mHelper.createNotificationChannel(PKG, UID, |
| 693 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true); |
| 694 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, |
| 695 | NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
| 696 | |
| 697 | // Load package at higher sdk. |
| 698 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 699 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
| 700 | when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(upgraded); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 701 | loadStreamXml(baos, false); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 702 | |
| 703 | // Default Channel should be gone. |
| 704 | assertEquals(null, mHelper.getNotificationChannel(PKG, UID, |
| 705 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 708 | @Test |
Geoffrey Pitsch | 6eccf00 | 2017-04-05 12:33:59 -0400 | [diff] [blame] | 709 | public void testLoadingOldChannelsDoesNotDeleteNewlyCreatedChannels() throws Exception { |
| 710 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, |
| 711 | NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
| 712 | mHelper.createNotificationChannel(PKG, UID, |
| 713 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true); |
| 714 | |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 715 | loadStreamXml(baos, false); |
Geoffrey Pitsch | 6eccf00 | 2017-04-05 12:33:59 -0400 | [diff] [blame] | 716 | |
| 717 | // Should still have the newly created channel that wasn't in the xml. |
| 718 | assertTrue(mHelper.getNotificationChannel(PKG, UID, "bananas", false) != null); |
| 719 | } |
| 720 | |
| 721 | @Test |
Julia Reynolds | 32c97ef | 2016-11-28 10:47:18 -0500 | [diff] [blame] | 722 | public void testCreateChannel_blocked() throws Exception { |
Julia Reynolds | d8c0ac4 | 2017-04-23 13:11:40 -0400 | [diff] [blame] | 723 | mHelper.setImportance(PKG, UID, IMPORTANCE_NONE); |
Julia Reynolds | 32c97ef | 2016-11-28 10:47:18 -0500 | [diff] [blame] | 724 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 725 | mHelper.createNotificationChannel(PKG, UID, |
| 726 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true); |
Julia Reynolds | 32c97ef | 2016-11-28 10:47:18 -0500 | [diff] [blame] | 727 | } |
| 728 | |
Julia Reynolds | d8c0ac4 | 2017-04-23 13:11:40 -0400 | [diff] [blame] | 729 | @Test |
Julia Reynolds | 3e50bf6 | 2017-05-24 16:33:32 -0400 | [diff] [blame] | 730 | public void testCreateChannel_badImportance() throws Exception { |
Julia Reynolds | d8c0ac4 | 2017-04-23 13:11:40 -0400 | [diff] [blame] | 731 | try { |
| 732 | mHelper.createNotificationChannel(PKG, UID, |
Julia Reynolds | 3e50bf6 | 2017-05-24 16:33:32 -0400 | [diff] [blame] | 733 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1), true); |
| 734 | fail("Was allowed to create a channel with invalid importance"); |
Julia Reynolds | d8c0ac4 | 2017-04-23 13:11:40 -0400 | [diff] [blame] | 735 | } catch (IllegalArgumentException e) { |
| 736 | // yay |
| 737 | } |
Julia Reynolds | 3e50bf6 | 2017-05-24 16:33:32 -0400 | [diff] [blame] | 738 | try { |
| 739 | mHelper.createNotificationChannel(PKG, UID, |
| 740 | new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED), true); |
| 741 | fail("Was allowed to create a channel with invalid importance"); |
| 742 | } catch (IllegalArgumentException e) { |
| 743 | // yay |
| 744 | } |
| 745 | try { |
| 746 | mHelper.createNotificationChannel(PKG, UID, |
| 747 | new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1), true); |
| 748 | fail("Was allowed to create a channel with invalid importance"); |
| 749 | } catch (IllegalArgumentException e) { |
| 750 | // yay |
| 751 | } |
| 752 | mHelper.createNotificationChannel(PKG, UID, |
| 753 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE), true); |
| 754 | mHelper.createNotificationChannel(PKG, UID, |
| 755 | new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true); |
Julia Reynolds | d8c0ac4 | 2017-04-23 13:11:40 -0400 | [diff] [blame] | 756 | } |
| 757 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 758 | |
| 759 | @Test |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 760 | public void testUpdate() throws Exception { |
| 761 | // no fields locked by user |
| 762 | final NotificationChannel channel = |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 763 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 764 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 765 | channel.enableLights(true); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 766 | channel.setBypassDnd(true); |
| 767 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 768 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 769 | mHelper.createNotificationChannel(PKG, UID, channel, false); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 770 | |
| 771 | // same id, try to update all fields |
| 772 | final NotificationChannel channel2 = |
| 773 | new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 774 | channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 775 | channel2.enableLights(false); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 776 | channel2.setBypassDnd(false); |
| 777 | channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); |
| 778 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 779 | mHelper.updateNotificationChannel(PKG, UID, channel2, true); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 780 | |
| 781 | // all fields should be changed |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 782 | assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 783 | |
| 784 | verify(mHandler, times(1)).requestSort(); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | @Test |
Julia Reynolds | fa04e14 | 2017-04-23 13:32:01 -0400 | [diff] [blame] | 788 | public void testUpdate_preUpgrade_updatesAppFields() throws Exception { |
| 789 | mHelper.setImportance(PKG, UID, IMPORTANCE_UNSPECIFIED); |
| 790 | assertTrue(mHelper.canShowBadge(PKG, UID)); |
| 791 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID)); |
| 792 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 793 | mHelper.getPackageVisibility(PKG, UID)); |
| 794 | |
| 795 | NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
| 796 | PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 797 | |
| 798 | defaultChannel.setShowBadge(false); |
| 799 | defaultChannel.setImportance(IMPORTANCE_NONE); |
| 800 | defaultChannel.setBypassDnd(true); |
| 801 | defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 802 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 803 | mHelper.updateNotificationChannel(PKG, UID, defaultChannel, true); |
Julia Reynolds | fa04e14 | 2017-04-23 13:32:01 -0400 | [diff] [blame] | 804 | |
| 805 | // ensure app level fields are changed |
| 806 | assertFalse(mHelper.canShowBadge(PKG, UID)); |
| 807 | assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG, UID)); |
| 808 | assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG, UID)); |
| 809 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG, UID)); |
| 810 | } |
| 811 | |
| 812 | @Test |
| 813 | public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception { |
| 814 | final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 815 | |
| 816 | mHelper.createNotificationChannel(PKG, UID, channel, false); |
| 817 | assertTrue(mHelper.canShowBadge(PKG, UID)); |
| 818 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID)); |
| 819 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 820 | mHelper.getPackageVisibility(PKG, UID)); |
| 821 | |
| 822 | channel.setShowBadge(false); |
| 823 | channel.setImportance(IMPORTANCE_NONE); |
| 824 | channel.setBypassDnd(true); |
| 825 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 826 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 827 | mHelper.updateNotificationChannel(PKG, UID, channel, true); |
Julia Reynolds | fa04e14 | 2017-04-23 13:32:01 -0400 | [diff] [blame] | 828 | |
| 829 | // ensure app level fields are not changed |
| 830 | assertTrue(mHelper.canShowBadge(PKG, UID)); |
| 831 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID)); |
| 832 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 833 | mHelper.getPackageVisibility(PKG, UID)); |
| 834 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID)); |
| 835 | } |
| 836 | |
| 837 | @Test |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 838 | public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception { |
| 839 | assertEquals(null, mHelper.getNotificationChannel(PKG, UID, "garbage", false)); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 840 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 841 | |
| 842 | @Test |
| 843 | public void testCreateChannel_CannotChangeHiddenFields() throws Exception { |
| 844 | final NotificationChannel channel = |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 845 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 846 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 847 | channel.enableLights(true); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 848 | channel.setBypassDnd(true); |
| 849 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 850 | channel.setShowBadge(true); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 851 | int lockMask = 0; |
| 852 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 853 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 854 | } |
| 855 | channel.lockFields(lockMask); |
| 856 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 857 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 858 | |
| 859 | NotificationChannel savedChannel = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 860 | mHelper.getNotificationChannel(PKG, UID, channel.getId(), false); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 861 | |
| 862 | assertEquals(channel.getName(), savedChannel.getName()); |
| 863 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 864 | assertFalse(savedChannel.canBypassDnd()); |
| 865 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
Julia Reynolds | f35e397 | 2017-01-05 15:41:04 -0500 | [diff] [blame] | 866 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 867 | |
| 868 | verify(mHandler, never()).requestSort(); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | @Test |
| 872 | public void testCreateChannel_CannotChangeHiddenFieldsAssistant() throws Exception { |
| 873 | final NotificationChannel channel = |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 874 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 875 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 876 | channel.enableLights(true); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 877 | channel.setBypassDnd(true); |
| 878 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 879 | channel.setShowBadge(true); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 880 | int lockMask = 0; |
| 881 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 882 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 883 | } |
| 884 | channel.lockFields(lockMask); |
| 885 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 886 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 887 | |
| 888 | NotificationChannel savedChannel = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 889 | mHelper.getNotificationChannel(PKG, UID, channel.getId(), false); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 890 | |
| 891 | assertEquals(channel.getName(), savedChannel.getName()); |
| 892 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 893 | assertFalse(savedChannel.canBypassDnd()); |
| 894 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
Julia Reynolds | f35e397 | 2017-01-05 15:41:04 -0500 | [diff] [blame] | 895 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 896 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 897 | |
| 898 | @Test |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 899 | public void testClearLockedFields() throws Exception { |
| 900 | final NotificationChannel channel = getChannel(); |
| 901 | mHelper.clearLockedFields(channel); |
| 902 | assertEquals(0, channel.getUserLockedFields()); |
| 903 | |
| 904 | channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY |
| 905 | | NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 906 | mHelper.clearLockedFields(channel); |
| 907 | assertEquals(0, channel.getUserLockedFields()); |
| 908 | } |
| 909 | |
| 910 | @Test |
| 911 | public void testLockFields_soundAndVibration() throws Exception { |
| 912 | mHelper.createNotificationChannel(PKG, UID, getChannel(), true); |
| 913 | |
| 914 | final NotificationChannel update1 = getChannel(); |
| 915 | update1.setSound(new Uri.Builder().scheme("test").build(), |
| 916 | new AudioAttributes.Builder().build()); |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 917 | update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 918 | mHelper.updateNotificationChannel(PKG, UID, update1, true); |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 919 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 920 | | NotificationChannel.USER_LOCKED_SOUND, |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 921 | mHelper.getNotificationChannel(PKG, UID, update1.getId(), false) |
| 922 | .getUserLockedFields()); |
| 923 | |
| 924 | NotificationChannel update2 = getChannel(); |
| 925 | update2.enableVibration(true); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 926 | mHelper.updateNotificationChannel(PKG, UID, update2, true); |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 927 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 928 | | NotificationChannel.USER_LOCKED_SOUND |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 929 | | NotificationChannel.USER_LOCKED_VIBRATION, |
| 930 | mHelper.getNotificationChannel(PKG, UID, update2.getId(), false) |
| 931 | .getUserLockedFields()); |
| 932 | } |
| 933 | |
| 934 | @Test |
| 935 | public void testLockFields_vibrationAndLights() throws Exception { |
| 936 | mHelper.createNotificationChannel(PKG, UID, getChannel(), true); |
| 937 | |
| 938 | final NotificationChannel update1 = getChannel(); |
| 939 | update1.setVibrationPattern(new long[]{7945, 46 ,246}); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 940 | mHelper.updateNotificationChannel(PKG, UID, update1, true); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 941 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION, |
| 942 | mHelper.getNotificationChannel(PKG, UID, update1.getId(), false) |
| 943 | .getUserLockedFields()); |
| 944 | |
| 945 | final NotificationChannel update2 = getChannel(); |
| 946 | update2.enableLights(true); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 947 | mHelper.updateNotificationChannel(PKG, UID, update2, true); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 948 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION |
| 949 | | NotificationChannel.USER_LOCKED_LIGHTS, |
| 950 | mHelper.getNotificationChannel(PKG, UID, update2.getId(), false) |
| 951 | .getUserLockedFields()); |
| 952 | } |
| 953 | |
| 954 | @Test |
| 955 | public void testLockFields_lightsAndImportance() throws Exception { |
| 956 | mHelper.createNotificationChannel(PKG, UID, getChannel(), true); |
| 957 | |
| 958 | final NotificationChannel update1 = getChannel(); |
| 959 | update1.setLightColor(Color.GREEN); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 960 | mHelper.updateNotificationChannel(PKG, UID, update1, true); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 961 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS, |
| 962 | mHelper.getNotificationChannel(PKG, UID, update1.getId(), false) |
| 963 | .getUserLockedFields()); |
| 964 | |
| 965 | final NotificationChannel update2 = getChannel(); |
| 966 | update2.setImportance(IMPORTANCE_DEFAULT); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 967 | mHelper.updateNotificationChannel(PKG, UID, update2, true); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 968 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS |
| 969 | | NotificationChannel.USER_LOCKED_IMPORTANCE, |
| 970 | mHelper.getNotificationChannel(PKG, UID, update2.getId(), false) |
| 971 | .getUserLockedFields()); |
| 972 | } |
| 973 | |
| 974 | @Test |
| 975 | public void testLockFields_visibilityAndDndAndBadge() throws Exception { |
| 976 | mHelper.createNotificationChannel(PKG, UID, getChannel(), true); |
| 977 | assertEquals(0, |
| 978 | mHelper.getNotificationChannel(PKG, UID, getChannel().getId(), false) |
| 979 | .getUserLockedFields()); |
| 980 | |
| 981 | final NotificationChannel update1 = getChannel(); |
| 982 | update1.setBypassDnd(true); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 983 | mHelper.updateNotificationChannel(PKG, UID, update1, true); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 984 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY, |
| 985 | mHelper.getNotificationChannel(PKG, UID, update1.getId(), false) |
| 986 | .getUserLockedFields()); |
| 987 | |
| 988 | final NotificationChannel update2 = getChannel(); |
| 989 | update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 990 | mHelper.updateNotificationChannel(PKG, UID, update2, true); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 991 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 992 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
| 993 | mHelper.getNotificationChannel(PKG, UID, update2.getId(), false) |
| 994 | .getUserLockedFields()); |
| 995 | |
| 996 | final NotificationChannel update3 = getChannel(); |
| 997 | update3.setShowBadge(false); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 998 | mHelper.updateNotificationChannel(PKG, UID, update3, true); |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 999 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 1000 | | NotificationChannel.USER_LOCKED_VISIBILITY |
| 1001 | | NotificationChannel.USER_LOCKED_SHOW_BADGE, |
| 1002 | mHelper.getNotificationChannel(PKG, UID, update3.getId(), false) |
| 1003 | .getUserLockedFields()); |
| 1004 | } |
| 1005 | |
| 1006 | @Test |
Julia Reynolds | 8e0eb37 | 2017-03-21 15:04:50 -0400 | [diff] [blame] | 1007 | public void testDeleteNonExistentChannel() throws Exception { |
| 1008 | mHelper.deleteNotificationChannelGroup(PKG, UID, "does not exist"); |
| 1009 | } |
| 1010 | |
| 1011 | @Test |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1012 | public void testGetDeletedChannel() throws Exception { |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 1013 | NotificationChannel channel = getChannel(); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1014 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 1015 | channel.enableLights(true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1016 | channel.setBypassDnd(true); |
| 1017 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 1018 | channel.enableVibration(true); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1019 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1020 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1021 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 1022 | mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1023 | |
| 1024 | // Does not return deleted channel |
| 1025 | NotificationChannel response = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1026 | mHelper.getNotificationChannel(PKG, UID, channel.getId(), false); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1027 | assertNull(response); |
| 1028 | |
| 1029 | // Returns deleted channel |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1030 | response = mHelper.getNotificationChannel(PKG, UID, channel.getId(), true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1031 | compareChannels(channel, response); |
| 1032 | assertTrue(response.isDeleted()); |
| 1033 | } |
| 1034 | |
| 1035 | @Test |
| 1036 | public void testGetDeletedChannels() throws Exception { |
| 1037 | Map<String, NotificationChannel> channelMap = new HashMap<>(); |
| 1038 | NotificationChannel channel = |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1039 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1040 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 1041 | channel.enableLights(true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1042 | channel.setBypassDnd(true); |
| 1043 | channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
| 1044 | channel.enableVibration(true); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1045 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1046 | channelMap.put(channel.getId(), channel); |
| 1047 | NotificationChannel channel2 = |
| 1048 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1049 | channelMap.put(channel2.getId(), channel2); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1050 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 1051 | mHelper.createNotificationChannel(PKG, UID, channel2, true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1052 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1053 | mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1054 | |
| 1055 | // Returns only non-deleted channels |
| 1056 | List<NotificationChannel> channels = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1057 | mHelper.getNotificationChannels(PKG, UID, false).getList(); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1058 | assertEquals(2, channels.size()); // Default channel + non-deleted channel |
| 1059 | for (NotificationChannel nc : channels) { |
| 1060 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1061 | compareChannels(channel2, nc); |
| 1062 | } |
| 1063 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1064 | |
| 1065 | // Returns deleted channels too |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1066 | channels = mHelper.getNotificationChannels(PKG, UID, true).getList(); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1067 | assertEquals(3, channels.size()); // Includes default channel |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1068 | for (NotificationChannel nc : channels) { |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1069 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1070 | compareChannels(channelMap.get(nc.getId()), nc); |
| 1071 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | @Test |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 1076 | public void testGetDeletedChannelCount() throws Exception { |
| 1077 | NotificationChannel channel = |
| 1078 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1079 | NotificationChannel channel2 = |
| 1080 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1081 | NotificationChannel channel3 = |
| 1082 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1083 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 1084 | mHelper.createNotificationChannel(PKG, UID, channel2, true); |
| 1085 | mHelper.createNotificationChannel(PKG, UID, channel3, true); |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 1086 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1087 | mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); |
| 1088 | mHelper.deleteNotificationChannel(PKG, UID, channel3.getId()); |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 1089 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1090 | assertEquals(2, mHelper.getDeletedChannelCount(PKG, UID)); |
| 1091 | assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID2)); |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | @Test |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1095 | public void testCreateDeletedChannel() throws Exception { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1096 | long[] vibration = new long[]{100, 67, 145, 156}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1097 | NotificationChannel channel = |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1098 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1099 | channel.setVibrationPattern(vibration); |
| 1100 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1101 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
| 1102 | mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1103 | |
| 1104 | NotificationChannel newChannel = new NotificationChannel( |
| 1105 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1106 | newChannel.setVibrationPattern(new long[]{100}); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1107 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1108 | mHelper.createNotificationChannel(PKG, UID, newChannel, true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1109 | |
| 1110 | // No long deleted, using old settings |
| 1111 | compareChannels(channel, |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1112 | mHelper.getNotificationChannel(PKG, UID, newChannel.getId(), false)); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | @Test |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 1116 | public void testOnlyHasDefaultChannel() throws Exception { |
| 1117 | assertTrue(mHelper.onlyHasDefaultChannel(PKG, UID)); |
| 1118 | assertFalse(mHelper.onlyHasDefaultChannel(UPDATED_PKG, UID2)); |
| 1119 | |
| 1120 | mHelper.createNotificationChannel(PKG, UID, getChannel(), true); |
| 1121 | assertFalse(mHelper.onlyHasDefaultChannel(PKG, UID)); |
| 1122 | } |
| 1123 | |
| 1124 | @Test |
Julia Reynolds | 03fa85d | 2017-03-06 15:14:50 -0500 | [diff] [blame] | 1125 | public void testCreateChannel_defaultChannelId() throws Exception { |
| 1126 | try { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1127 | mHelper.createNotificationChannel(PKG, UID, new NotificationChannel( |
Julia Reynolds | 03fa85d | 2017-03-06 15:14:50 -0500 | [diff] [blame] | 1128 | NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true); |
| 1129 | fail("Allowed to create default channel"); |
| 1130 | } catch (IllegalArgumentException e) { |
| 1131 | // pass |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | @Test |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1136 | public void testCreateChannel_alreadyExists() throws Exception { |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1137 | long[] vibration = new long[]{100, 67, 145, 156}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1138 | NotificationChannel channel = |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1139 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1140 | channel.setVibrationPattern(vibration); |
| 1141 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1142 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1143 | |
| 1144 | NotificationChannel newChannel = new NotificationChannel( |
| 1145 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1146 | newChannel.setVibrationPattern(new long[]{100}); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1147 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1148 | mHelper.createNotificationChannel(PKG, UID, newChannel, true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1149 | |
| 1150 | // Old settings not overridden |
| 1151 | compareChannels(channel, |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1152 | mHelper.getNotificationChannel(PKG, UID, newChannel.getId(), false)); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | @Test |
Julia Reynolds | 31b2baa | 2017-02-22 12:18:45 -0500 | [diff] [blame] | 1156 | public void testCreateChannel_noOverrideSound() throws Exception { |
| 1157 | Uri sound = new Uri.Builder().scheme("test").build(); |
| 1158 | final NotificationChannel channel = new NotificationChannel("id2", "name2", |
| 1159 | NotificationManager.IMPORTANCE_DEFAULT); |
| 1160 | channel.setSound(sound, mAudioAttributes); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1161 | mHelper.createNotificationChannel(PKG, UID, channel, true); |
Julia Reynolds | 31b2baa | 2017-02-22 12:18:45 -0500 | [diff] [blame] | 1162 | assertEquals(sound, mHelper.getNotificationChannel( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1163 | PKG, UID, channel.getId(), false).getSound()); |
Julia Reynolds | 31b2baa | 2017-02-22 12:18:45 -0500 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | @Test |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1167 | public void testPermanentlyDeleteChannels() throws Exception { |
| 1168 | NotificationChannel channel1 = |
| 1169 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1170 | NotificationChannel channel2 = |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1171 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1172 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1173 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
| 1174 | mHelper.createNotificationChannel(PKG, UID, channel2, false); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1175 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1176 | mHelper.permanentlyDeleteNotificationChannels(PKG, UID); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1177 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1178 | // Only default channel remains |
| 1179 | assertEquals(1, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | @Test |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1183 | public void testDeleteGroup() throws Exception { |
| 1184 | NotificationChannelGroup notDeleted = new NotificationChannelGroup("not", "deleted"); |
| 1185 | NotificationChannelGroup deleted = new NotificationChannelGroup("totally", "deleted"); |
| 1186 | NotificationChannel nonGroupedNonDeletedChannel = |
| 1187 | new NotificationChannel("no group", "so not deleted", IMPORTANCE_HIGH); |
| 1188 | NotificationChannel groupedButNotDeleted = |
| 1189 | new NotificationChannel("not deleted", "belongs to notDeleted", IMPORTANCE_DEFAULT); |
| 1190 | groupedButNotDeleted.setGroup("not"); |
| 1191 | NotificationChannel groupedAndDeleted = |
| 1192 | new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT); |
| 1193 | groupedAndDeleted.setGroup("totally"); |
| 1194 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1195 | mHelper.createNotificationChannelGroup(PKG, UID, notDeleted, true); |
| 1196 | mHelper.createNotificationChannelGroup(PKG, UID, deleted, true); |
| 1197 | mHelper.createNotificationChannel(PKG, UID, nonGroupedNonDeletedChannel, true); |
| 1198 | mHelper.createNotificationChannel(PKG, UID, groupedAndDeleted, true); |
| 1199 | mHelper.createNotificationChannel(PKG, UID, groupedButNotDeleted, true); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1200 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1201 | mHelper.deleteNotificationChannelGroup(PKG, UID, deleted.getId()); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1202 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1203 | assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG, UID)); |
| 1204 | assertNotNull(mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG, UID)); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1205 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1206 | assertNull(mHelper.getNotificationChannel(PKG, UID, groupedAndDeleted.getId(), false)); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1207 | compareChannels(groupedAndDeleted, |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1208 | mHelper.getNotificationChannel(PKG, UID, groupedAndDeleted.getId(), true)); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1209 | |
| 1210 | compareChannels(groupedButNotDeleted, |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1211 | mHelper.getNotificationChannel(PKG, UID, groupedButNotDeleted.getId(), false)); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1212 | compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1213 | PKG, UID, nonGroupedNonDeletedChannel.getId(), false)); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1214 | |
| 1215 | // notDeleted |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1216 | assertEquals(1, mHelper.getNotificationChannelGroups(PKG, UID).size()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1217 | |
| 1218 | verify(mHandler, never()).requestSort(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | @Test |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1222 | public void testOnUserRemoved() throws Exception { |
| 1223 | int[] user0Uids = {98, 235, 16, 3782}; |
| 1224 | int[] user1Uids = new int[user0Uids.length]; |
| 1225 | for (int i = 0; i < user0Uids.length; i++) { |
| 1226 | user1Uids[i] = UserHandle.PER_USER_RANGE + user0Uids[i]; |
| 1227 | |
| 1228 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1229 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 1230 | when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(legacy); |
| 1231 | |
| 1232 | // create records with the default channel for all user 0 and user 1 uids |
| 1233 | mHelper.getImportance(PKG, user0Uids[i]); |
| 1234 | mHelper.getImportance(PKG, user1Uids[i]); |
| 1235 | } |
| 1236 | |
| 1237 | mHelper.onUserRemoved(1); |
| 1238 | |
| 1239 | // user 0 records remain |
| 1240 | for (int i = 0; i < user0Uids.length; i++) { |
| 1241 | assertEquals(1, |
| 1242 | mHelper.getNotificationChannels(PKG, user0Uids[i], false).getList().size()); |
| 1243 | } |
| 1244 | // user 1 records are gone |
| 1245 | for (int i = 0; i < user1Uids.length; i++) { |
| 1246 | assertEquals(0, |
| 1247 | mHelper.getNotificationChannels(PKG, user1Uids[i], false).getList().size()); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | @Test |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1252 | public void testOnPackageChanged_packageRemoval() throws Exception { |
| 1253 | // Deleted |
| 1254 | NotificationChannel channel1 = |
| 1255 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1256 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1257 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1258 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1259 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1260 | assertEquals(0, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1261 | |
| 1262 | // Not deleted |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1263 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1264 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1265 | mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1266 | assertEquals(2, mHelper.getNotificationChannels(PKG, UID, false).getList().size()); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1267 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1268 | |
| 1269 | @Test |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1270 | public void testOnPackageChanged_packageRemoval_importance() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1271 | mHelper.setImportance(PKG, UID, NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1272 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1273 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1274 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1275 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID)); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | @Test |
| 1279 | public void testOnPackageChanged_packageRemoval_groups() throws Exception { |
| 1280 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1281 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1282 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1283 | mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1284 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1285 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1286 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1287 | assertEquals(0, |
| 1288 | mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList().size()); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | @Test |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 1292 | public void testOnPackageChange_downgradeTargetSdk() throws Exception { |
| 1293 | // create channel as api 26 |
| 1294 | mHelper.createNotificationChannel(UPDATED_PKG, UID2, getChannel(), true); |
| 1295 | |
| 1296 | // install new app version targeting 25 |
| 1297 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1298 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 1299 | when(mPm.getApplicationInfoAsUser(eq(UPDATED_PKG), anyInt(), anyInt())).thenReturn(legacy); |
| 1300 | mHelper.onPackagesChanged( |
| 1301 | false, UserHandle.USER_SYSTEM, new String[]{UPDATED_PKG}, new int[]{UID2}); |
| 1302 | |
| 1303 | // make sure the default channel was readded |
| 1304 | //assertEquals(2, mHelper.getNotificationChannels(UPDATED_PKG, UID2, false).getList().size()); |
| 1305 | assertNotNull(mHelper.getNotificationChannel( |
| 1306 | UPDATED_PKG, UID2, NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 1307 | } |
| 1308 | |
| 1309 | @Test |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1310 | public void testRecordDefaults() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1311 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID)); |
| 1312 | assertEquals(true, mHelper.canShowBadge(PKG, UID)); |
| 1313 | assertEquals(1, mHelper.getNotificationChannels(PKG, UID, false).getList().size()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1314 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1315 | |
| 1316 | @Test |
| 1317 | public void testCreateGroup() throws Exception { |
| 1318 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1319 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
| 1320 | assertEquals(ncg, mHelper.getNotificationChannelGroups(PKG, UID).iterator().next()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1321 | verify(mHandler, never()).requestSort(); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | @Test |
| 1325 | public void testCannotCreateChannel_badGroup() throws Exception { |
| 1326 | NotificationChannel channel1 = |
| 1327 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1328 | channel1.setGroup("garbage"); |
| 1329 | try { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1330 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1331 | fail("Created a channel with a bad group"); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1332 | } catch (IllegalArgumentException e) { |
| 1333 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | @Test |
| 1337 | public void testCannotCreateChannel_goodGroup() throws Exception { |
| 1338 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1339 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1340 | NotificationChannel channel1 = |
| 1341 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1342 | channel1.setGroup(ncg.getId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1343 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1344 | |
| 1345 | assertEquals(ncg.getId(), |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1346 | mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false).getGroup()); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | @Test |
| 1350 | public void testGetChannelGroups() throws Exception { |
Julia Reynolds | f02562a | 2017-01-26 13:33:56 -0500 | [diff] [blame] | 1351 | NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1352 | mHelper.createNotificationChannelGroup(PKG, UID, unused, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1353 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1354 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1355 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1356 | mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1357 | |
| 1358 | NotificationChannel channel1 = |
| 1359 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1360 | channel1.setGroup(ncg.getId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1361 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1362 | NotificationChannel channel1a = |
| 1363 | new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1364 | channel1a.setGroup(ncg.getId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1365 | mHelper.createNotificationChannel(PKG, UID, channel1a, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1366 | |
| 1367 | NotificationChannel channel2 = |
| 1368 | new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1369 | channel2.setGroup(ncg2.getId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1370 | mHelper.createNotificationChannel(PKG, UID, channel2, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1371 | |
| 1372 | NotificationChannel channel3 = |
| 1373 | new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1374 | mHelper.createNotificationChannel(PKG, UID, channel3, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1375 | |
| 1376 | List<NotificationChannelGroup> actual = |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1377 | mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList(); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1378 | assertEquals(3, actual.size()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 1379 | for (NotificationChannelGroup group : actual) { |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1380 | if (group.getId() == null) { |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1381 | assertEquals(2, group.getChannels().size()); // misc channel too |
| 1382 | assertTrue(channel3.getId().equals(group.getChannels().get(0).getId()) |
| 1383 | || channel3.getId().equals(group.getChannels().get(1).getId())); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1384 | } else if (group.getId().equals(ncg.getId())) { |
| 1385 | assertEquals(2, group.getChannels().size()); |
| 1386 | if (group.getChannels().get(0).getId().equals(channel1.getId())) { |
| 1387 | assertTrue(group.getChannels().get(1).getId().equals(channel1a.getId())); |
| 1388 | } else if (group.getChannels().get(0).getId().equals(channel1a.getId())) { |
| 1389 | assertTrue(group.getChannels().get(1).getId().equals(channel1.getId())); |
| 1390 | } else { |
| 1391 | fail("expected channel not found"); |
| 1392 | } |
| 1393 | } else if (group.getId().equals(ncg2.getId())) { |
| 1394 | assertEquals(1, group.getChannels().size()); |
| 1395 | assertEquals(channel2.getId(), group.getChannels().get(0).getId()); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1399 | |
| 1400 | @Test |
| 1401 | public void testGetChannelGroups_noSideEffects() throws Exception { |
| 1402 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1403 | mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1404 | |
| 1405 | NotificationChannel channel1 = |
| 1406 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1407 | channel1.setGroup(ncg.getId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1408 | mHelper.createNotificationChannel(PKG, UID, channel1, true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1409 | mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList(); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1410 | |
| 1411 | channel1.setImportance(IMPORTANCE_LOW); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1412 | mHelper.updateNotificationChannel(PKG, UID, channel1, true); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1413 | |
| 1414 | List<NotificationChannelGroup> actual = |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1415 | mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList(); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1416 | |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 1417 | assertEquals(2, actual.size()); |
| 1418 | for (NotificationChannelGroup group : actual) { |
| 1419 | if (Objects.equals(group.getId(), ncg.getId())) { |
| 1420 | assertEquals(1, group.getChannels().size()); |
| 1421 | } |
| 1422 | } |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 1423 | } |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1424 | |
| 1425 | @Test |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 1426 | public void testCreateChannel_updateName() throws Exception { |
| 1427 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1428 | mHelper.createNotificationChannel(PKG, UID, nc, true); |
| 1429 | NotificationChannel actual = mHelper.getNotificationChannel(PKG, UID, "id", false); |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 1430 | assertEquals("hello", actual.getName()); |
| 1431 | |
| 1432 | nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1433 | mHelper.createNotificationChannel(PKG, UID, nc, true); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1434 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1435 | actual = mHelper.getNotificationChannel(PKG, UID, "id", false); |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 1436 | assertEquals("goodbye", actual.getName()); |
| 1437 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1438 | |
| 1439 | verify(mHandler, times(1)).requestSort(); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | @Test |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1443 | public void testCreateChannel_addToGroup() throws Exception { |
| 1444 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
| 1445 | mHelper.createNotificationChannelGroup(PKG, UID, group, true); |
| 1446 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
| 1447 | mHelper.createNotificationChannel(PKG, UID, nc, true); |
| 1448 | NotificationChannel actual = mHelper.getNotificationChannel(PKG, UID, "id", false); |
| 1449 | assertNull(actual.getGroup()); |
| 1450 | |
| 1451 | nc = new NotificationChannel("id", "hello", IMPORTANCE_HIGH); |
| 1452 | nc.setGroup(group.getId()); |
| 1453 | mHelper.createNotificationChannel(PKG, UID, nc, true); |
| 1454 | |
| 1455 | actual = mHelper.getNotificationChannel(PKG, UID, "id", false); |
| 1456 | assertNotNull(actual.getGroup()); |
| 1457 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
| 1458 | |
| 1459 | verify(mHandler, times(1)).requestSort(); |
| 1460 | } |
| 1461 | |
| 1462 | @Test |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1463 | public void testDumpChannelsJson() throws Exception { |
| 1464 | final ApplicationInfo upgrade = new ApplicationInfo(); |
| 1465 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
| 1466 | try { |
| 1467 | when(mPm.getApplicationInfoAsUser( |
| 1468 | anyString(), anyInt(), anyInt())).thenReturn(upgrade); |
| 1469 | } catch (PackageManager.NameNotFoundException e) { |
| 1470 | } |
| 1471 | ArrayMap<String, Integer> expectedChannels = new ArrayMap<>(); |
| 1472 | int numPackages = ThreadLocalRandom.current().nextInt(1, 5); |
| 1473 | for (int i = 0; i < numPackages; i++) { |
| 1474 | String pkgName = "pkg" + i; |
| 1475 | int numChannels = ThreadLocalRandom.current().nextInt(1, 10); |
| 1476 | for (int j = 0; j < numChannels; j++) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1477 | mHelper.createNotificationChannel(pkgName, UID, |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1478 | new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true); |
| 1479 | } |
| 1480 | expectedChannels.put(pkgName, numChannels); |
| 1481 | } |
| 1482 | |
| 1483 | // delete the first channel of the first package |
| 1484 | String pkg = expectedChannels.keyAt(0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1485 | mHelper.deleteNotificationChannel("pkg" + 0, UID, "0"); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1486 | // dump should not include deleted channels |
| 1487 | int count = expectedChannels.get(pkg); |
| 1488 | expectedChannels.put(pkg, count - 1); |
| 1489 | |
| 1490 | JSONArray actual = mHelper.dumpChannelsJson(new NotificationManagerService.DumpFilter()); |
| 1491 | assertEquals(numPackages, actual.length()); |
| 1492 | for (int i = 0; i < numPackages; i++) { |
| 1493 | JSONObject object = actual.getJSONObject(i); |
| 1494 | assertTrue(expectedChannels.containsKey(object.get("packageName"))); |
Geoffrey Pitsch | 5644ccd | 2017-04-17 11:33:24 -0400 | [diff] [blame] | 1495 | assertEquals(expectedChannels.get(object.get("packageName")).intValue(), |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1496 | object.getInt("channelCount")); |
| 1497 | } |
| 1498 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1499 | |
| 1500 | @Test |
| 1501 | public void testBadgingOverrideTrue() throws Exception { |
| 1502 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1503 | Secure.NOTIFICATION_BADGING, 1, |
| 1504 | USER.getIdentifier()); |
| 1505 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1506 | assertTrue(mHelper.badgingEnabled(USER)); |
| 1507 | } |
| 1508 | |
| 1509 | @Test |
| 1510 | public void testBadgingOverrideFalse() throws Exception { |
| 1511 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1512 | Secure.NOTIFICATION_BADGING, 0, |
| 1513 | USER.getIdentifier()); |
| 1514 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1515 | assertFalse(mHelper.badgingEnabled(USER)); |
| 1516 | } |
| 1517 | |
| 1518 | @Test |
Chris Wren | 13f157f | 2017-05-12 15:02:06 -0400 | [diff] [blame] | 1519 | public void testBadgingForUserAll() throws Exception { |
| 1520 | try { |
| 1521 | mHelper.badgingEnabled(UserHandle.ALL); |
| 1522 | } catch (Exception e) { |
| 1523 | fail("just don't throw"); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | @Test |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1528 | public void testBadgingOverrideUserIsolation() throws Exception { |
| 1529 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1530 | Secure.NOTIFICATION_BADGING, 0, |
| 1531 | USER.getIdentifier()); |
| 1532 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1533 | Secure.NOTIFICATION_BADGING, 1, |
| 1534 | USER2.getIdentifier()); |
| 1535 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1536 | assertFalse(mHelper.badgingEnabled(USER)); |
| 1537 | assertTrue(mHelper.badgingEnabled(USER2)); |
| 1538 | } |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 1539 | |
| 1540 | @Test |
| 1541 | public void testOnLocaleChanged_updatesDefaultChannels() throws Exception { |
| 1542 | String newLabel = "bananas!"; |
| 1543 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG, UID, |
| 1544 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 1545 | assertFalse(newLabel.equals(defaultChannel.getName())); |
| 1546 | |
| 1547 | Resources res = mock(Resources.class); |
| 1548 | when(mContext.getResources()).thenReturn(res); |
| 1549 | when(res.getString(com.android.internal.R.string.default_notification_channel_label)) |
| 1550 | .thenReturn(newLabel); |
| 1551 | |
| 1552 | mHelper.onLocaleChanged(mContext, USER.getIdentifier()); |
| 1553 | |
| 1554 | assertEquals(newLabel, mHelper.getNotificationChannel(PKG, UID, |
| 1555 | NotificationChannel.DEFAULT_CHANNEL_ID, false).getName()); |
| 1556 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1557 | |
| 1558 | @Test |
| 1559 | public void testIsGroupBlocked_noGroup() throws Exception { |
| 1560 | assertFalse(mHelper.isGroupBlocked(PKG, UID, null)); |
| 1561 | |
| 1562 | assertFalse(mHelper.isGroupBlocked(PKG, UID, "non existent group")); |
| 1563 | } |
| 1564 | |
| 1565 | @Test |
| 1566 | public void testIsGroupBlocked_notBlocked() throws Exception { |
| 1567 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
| 1568 | mHelper.createNotificationChannelGroup(PKG, UID, group, true); |
| 1569 | |
| 1570 | assertFalse(mHelper.isGroupBlocked(PKG, UID, group.getId())); |
| 1571 | } |
| 1572 | |
| 1573 | @Test |
| 1574 | public void testIsGroupBlocked_blocked() throws Exception { |
| 1575 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
| 1576 | mHelper.createNotificationChannelGroup(PKG, UID, group, true); |
| 1577 | group.setBlocked(true); |
| 1578 | mHelper.createNotificationChannelGroup(PKG, UID, group, false); |
| 1579 | |
| 1580 | assertTrue(mHelper.isGroupBlocked(PKG, UID, group.getId())); |
| 1581 | } |
| 1582 | |
| 1583 | @Test |
| 1584 | public void testIsGroup_appCannotResetBlock() throws Exception { |
| 1585 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
| 1586 | mHelper.createNotificationChannelGroup(PKG, UID, group, true); |
| 1587 | NotificationChannelGroup group2 = group.clone(); |
| 1588 | group2.setBlocked(true); |
| 1589 | mHelper.createNotificationChannelGroup(PKG, UID, group2, false); |
| 1590 | assertTrue(mHelper.isGroupBlocked(PKG, UID, group.getId())); |
| 1591 | |
| 1592 | NotificationChannelGroup group3 = group.clone(); |
| 1593 | group3.setBlocked(false); |
| 1594 | mHelper.createNotificationChannelGroup(PKG, UID, group3, true); |
| 1595 | assertTrue(mHelper.isGroupBlocked(PKG, UID, group.getId())); |
| 1596 | } |
| 1597 | |
| 1598 | @Test |
| 1599 | public void testGetNotificationChannelGroupWithChannels() throws Exception { |
| 1600 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
| 1601 | NotificationChannelGroup other = new NotificationChannelGroup("something else", ""); |
| 1602 | mHelper.createNotificationChannelGroup(PKG, UID, group, true); |
| 1603 | mHelper.createNotificationChannelGroup(PKG, UID, other, true); |
| 1604 | |
| 1605 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 1606 | a.setGroup(group.getId()); |
| 1607 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT); |
| 1608 | b.setGroup(other.getId()); |
| 1609 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 1610 | c.setGroup(group.getId()); |
| 1611 | NotificationChannel d = new NotificationChannel("d", "d", IMPORTANCE_DEFAULT); |
| 1612 | |
| 1613 | mHelper.createNotificationChannel(PKG, UID, a, true); |
| 1614 | mHelper.createNotificationChannel(PKG, UID, b, true); |
| 1615 | mHelper.createNotificationChannel(PKG, UID, c, true); |
| 1616 | mHelper.createNotificationChannel(PKG, UID, d, true); |
| 1617 | mHelper.deleteNotificationChannel(PKG, UID, c.getId()); |
| 1618 | |
| 1619 | NotificationChannelGroup retrieved = mHelper.getNotificationChannelGroupWithChannels( |
| 1620 | PKG, UID, group.getId(), true); |
| 1621 | assertEquals(2, retrieved.getChannels().size()); |
| 1622 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 1623 | compareChannels(c, findChannel(retrieved.getChannels(), c.getId())); |
| 1624 | |
| 1625 | retrieved = mHelper.getNotificationChannelGroupWithChannels( |
| 1626 | PKG, UID, group.getId(), false); |
| 1627 | assertEquals(1, retrieved.getChannels().size()); |
| 1628 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 1629 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 1630 | } |