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