Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2018 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 | |
| 17 | package android.ext.services.notification; |
| 18 | |
| 19 | import static android.app.Notification.FLAG_CAN_COLORIZE; |
| 20 | import static android.app.Notification.FLAG_FOREGROUND_SERVICE; |
| 21 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
| 22 | import static android.media.AudioAttributes.USAGE_ALARM; |
| 23 | |
| 24 | import static junit.framework.Assert.assertFalse; |
| 25 | import static junit.framework.Assert.assertTrue; |
| 26 | |
| 27 | import static org.mockito.ArgumentMatchers.anyInt; |
| 28 | import static org.mockito.ArgumentMatchers.anyString; |
| 29 | import static org.mockito.Mockito.when; |
| 30 | |
| 31 | import android.app.Notification; |
| 32 | import android.app.NotificationChannel; |
| 33 | import android.app.Person; |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 34 | import android.content.ComponentName; |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 35 | import android.content.pm.ApplicationInfo; |
| 36 | import android.content.pm.IPackageManager; |
| 37 | import android.media.AudioAttributes; |
| 38 | import android.os.Build; |
| 39 | import android.os.Process; |
| 40 | import android.os.UserHandle; |
| 41 | import android.service.notification.StatusBarNotification; |
| 42 | import android.support.test.InstrumentationRegistry; |
| 43 | import android.support.test.runner.AndroidJUnit4; |
| 44 | import android.testing.TestableContext; |
| 45 | |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Rule; |
| 48 | import org.junit.Test; |
| 49 | import org.junit.runner.RunWith; |
| 50 | import org.mockito.Mock; |
| 51 | import org.mockito.MockitoAnnotations; |
| 52 | |
| 53 | import java.util.ArrayList; |
| 54 | |
| 55 | @RunWith(AndroidJUnit4.class) |
| 56 | public class NotificationEntryTest { |
| 57 | private String mPkg = "pkg"; |
| 58 | private int mUid = 2018; |
| 59 | @Mock |
| 60 | private IPackageManager mPackageManager; |
| 61 | @Mock |
| 62 | private ApplicationInfo mAppInfo; |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 63 | @Mock |
| 64 | private SmsHelper mSmsHelper; |
| 65 | |
| 66 | private static final String DEFAULT_SMS_PACKAGE_NAME = "foo"; |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 67 | |
| 68 | @Rule |
| 69 | public final TestableContext mContext = |
| 70 | new TestableContext(InstrumentationRegistry.getContext(), null); |
| 71 | |
| 72 | private StatusBarNotification generateSbn(String channelId) { |
| 73 | Notification n = new Notification.Builder(mContext, channelId) |
| 74 | .setContentTitle("foo") |
| 75 | .build(); |
| 76 | |
| 77 | return new StatusBarNotification(mPkg, mPkg, 0, "tag", mUid, mUid, n, |
| 78 | UserHandle.SYSTEM, null, 0); |
| 79 | } |
| 80 | |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 81 | private StatusBarNotification generateSbn(String channelId, String packageName) { |
| 82 | Notification n = new Notification.Builder(mContext, channelId) |
| 83 | .setContentTitle("foo") |
| 84 | .build(); |
| 85 | |
| 86 | return new StatusBarNotification(packageName, packageName, 0, "tag", mUid, mUid, n, |
| 87 | UserHandle.SYSTEM, null, 0); |
| 88 | } |
| 89 | |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 90 | private StatusBarNotification generateSbn(Notification n) { |
| 91 | return new StatusBarNotification(mPkg, mPkg, 0, "tag", mUid, mUid, n, |
| 92 | UserHandle.SYSTEM, null, 0); |
| 93 | } |
| 94 | |
| 95 | @Before |
| 96 | public void setUp() throws Exception { |
| 97 | MockitoAnnotations.initMocks(this); |
| 98 | mPkg = mContext.getPackageName(); |
| 99 | mUid = Process.myUid(); |
| 100 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
| 101 | .thenReturn(mAppInfo); |
| 102 | mAppInfo.targetSdkVersion = Build.VERSION_CODES.P; |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 103 | when(mSmsHelper.getDefaultSmsApplication()) |
| 104 | .thenReturn(new ComponentName(DEFAULT_SMS_PACKAGE_NAME, "bar")); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testHasPerson() { |
| 109 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 110 | StatusBarNotification sbn = generateSbn(channel.getId()); |
| 111 | ArrayList<Person> people = new ArrayList<>(); |
| 112 | people.add(new Person.Builder().setKey("mailto:testing@android.com").build()); |
| 113 | sbn.getNotification().extras.putParcelableArrayList(Notification.EXTRA_PEOPLE_LIST, people); |
| 114 | |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 115 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 116 | assertTrue(entry.involvesPeople()); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testNotPerson() { |
| 121 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 122 | StatusBarNotification sbn = generateSbn(channel.getId()); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 123 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
| 124 | assertFalse(entry.involvesPeople()); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testHasPerson_matchesDefaultSmsApp() { |
| 129 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 130 | StatusBarNotification sbn = generateSbn(channel.getId(), DEFAULT_SMS_PACKAGE_NAME); |
| 131 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
| 132 | assertTrue(entry.involvesPeople()); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testHasPerson_doesntMatchDefaultSmsApp() { |
| 137 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 138 | StatusBarNotification sbn = generateSbn(channel.getId(), "abc"); |
| 139 | NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 140 | assertFalse(entry.involvesPeople()); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testIsInboxStyle() { |
| 145 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 146 | |
| 147 | Notification n = new Notification.Builder(mContext, channel.getId()) |
| 148 | .setStyle(new Notification.InboxStyle()) |
| 149 | .build(); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 150 | NotificationEntry entry = |
| 151 | new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 152 | assertTrue(entry.hasStyle(Notification.InboxStyle.class)); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testIsMessagingStyle() { |
| 157 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 158 | |
| 159 | Notification n = new Notification.Builder(mContext, channel.getId()) |
| 160 | .setStyle(new Notification.MessagingStyle("")) |
| 161 | .build(); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 162 | NotificationEntry entry = |
| 163 | new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 164 | assertTrue(entry.hasStyle(Notification.MessagingStyle.class)); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void testIsNotPersonStyle() { |
| 169 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 170 | |
| 171 | Notification n = new Notification.Builder(mContext, channel.getId()) |
| 172 | .setStyle(new Notification.BigPictureStyle()) |
| 173 | .build(); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 174 | NotificationEntry entry = |
| 175 | new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 176 | assertFalse(entry.hasStyle(Notification.InboxStyle.class)); |
| 177 | assertFalse(entry.hasStyle(Notification.MessagingStyle.class)); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testIsAudioAttributes() { |
| 182 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 183 | channel.setSound(null, new AudioAttributes.Builder().setUsage(USAGE_ALARM).build()); |
| 184 | |
| 185 | NotificationEntry entry = new NotificationEntry( |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 186 | mPackageManager, generateSbn(channel.getId()), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 187 | |
| 188 | assertTrue(entry.isAudioAttributesUsage(USAGE_ALARM)); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testIsNotAudioAttributes() { |
| 193 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 194 | NotificationEntry entry = new NotificationEntry( |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 195 | mPackageManager, generateSbn(channel.getId()), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 196 | |
| 197 | assertFalse(entry.isAudioAttributesUsage(USAGE_ALARM)); |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void testIsCategory() { |
| 202 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 203 | |
| 204 | Notification n = new Notification.Builder(mContext, channel.getId()) |
| 205 | .setCategory(Notification.CATEGORY_EMAIL) |
| 206 | .build(); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 207 | NotificationEntry entry = |
| 208 | new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 209 | |
| 210 | assertTrue(entry.isCategory(Notification.CATEGORY_EMAIL)); |
| 211 | assertFalse(entry.isCategory(Notification.CATEGORY_MESSAGE)); |
| 212 | } |
| 213 | |
| 214 | @Test |
| 215 | public void testIsOngoing() { |
| 216 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 217 | |
| 218 | Notification n = new Notification.Builder(mContext, channel.getId()) |
| 219 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
| 220 | .build(); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 221 | NotificationEntry entry = |
| 222 | new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 223 | |
| 224 | assertTrue(entry.isOngoing()); |
| 225 | } |
| 226 | |
| 227 | @Test |
| 228 | public void testIsNotOngoing() { |
| 229 | NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH); |
| 230 | |
| 231 | Notification n = new Notification.Builder(mContext, channel.getId()) |
| 232 | .setFlag(FLAG_CAN_COLORIZE, true) |
| 233 | .build(); |
Nadia Benbernou | 1ee91a3 | 2019-01-28 11:26:46 -0500 | [diff] [blame] | 234 | NotificationEntry entry = |
| 235 | new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 236 | |
| 237 | assertFalse(entry.isOngoing()); |
| 238 | } |
| 239 | } |