blob: f51e911cbe97f5f2244218c465475f523c14f1f5 [file] [log] [blame]
Julia Reynolds901bf282018-08-14 10:09:36 -04001/**
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
17package android.ext.services.notification;
18
19import static android.app.Notification.FLAG_CAN_COLORIZE;
20import static android.app.Notification.FLAG_FOREGROUND_SERVICE;
21import static android.app.NotificationManager.IMPORTANCE_HIGH;
22import static android.media.AudioAttributes.USAGE_ALARM;
23
24import static junit.framework.Assert.assertFalse;
25import static junit.framework.Assert.assertTrue;
26
27import static org.mockito.ArgumentMatchers.anyInt;
28import static org.mockito.ArgumentMatchers.anyString;
29import static org.mockito.Mockito.when;
30
31import android.app.Notification;
32import android.app.NotificationChannel;
33import android.app.Person;
Nadia Benbernou1ee91a32019-01-28 11:26:46 -050034import android.content.ComponentName;
Julia Reynolds901bf282018-08-14 10:09:36 -040035import android.content.pm.ApplicationInfo;
36import android.content.pm.IPackageManager;
37import android.media.AudioAttributes;
38import android.os.Build;
39import android.os.Process;
40import android.os.UserHandle;
41import android.service.notification.StatusBarNotification;
Julia Reynolds901bf282018-08-14 10:09:36 -040042import android.testing.TestableContext;
43
Brett Chabot84151d92019-02-27 15:37:59 -080044import androidx.test.InstrumentationRegistry;
45import androidx.test.runner.AndroidJUnit4;
46
Julia Reynolds901bf282018-08-14 10:09:36 -040047import org.junit.Before;
48import org.junit.Rule;
49import org.junit.Test;
50import org.junit.runner.RunWith;
51import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
53
54import java.util.ArrayList;
55
56@RunWith(AndroidJUnit4.class)
57public 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 Benbernou1ee91a32019-01-28 11:26:46 -050064 @Mock
65 private SmsHelper mSmsHelper;
66
67 private static final String DEFAULT_SMS_PACKAGE_NAME = "foo";
Julia Reynolds901bf282018-08-14 10:09:36 -040068
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 Benbernou1ee91a32019-01-28 11:26:46 -050082 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 Reynolds901bf282018-08-14 10:09:36 -040091 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 Benbernou1ee91a32019-01-28 11:26:46 -0500104 when(mSmsHelper.getDefaultSmsApplication())
105 .thenReturn(new ComponentName(DEFAULT_SMS_PACKAGE_NAME, "bar"));
Julia Reynolds901bf282018-08-14 10:09:36 -0400106 }
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 Benbernou1ee91a32019-01-28 11:26:46 -0500116 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400117 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 Benbernou1ee91a32019-01-28 11:26:46 -0500124 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 Reynolds901bf282018-08-14 10:09:36 -0400141 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 Benbernou1ee91a32019-01-28 11:26:46 -0500151 NotificationEntry entry =
152 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400153 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 Benbernou1ee91a32019-01-28 11:26:46 -0500163 NotificationEntry entry =
164 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400165 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 Benbernou1ee91a32019-01-28 11:26:46 -0500175 NotificationEntry entry =
176 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400177 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 Benbernou1ee91a32019-01-28 11:26:46 -0500187 mPackageManager, generateSbn(channel.getId()), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400188
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 Benbernou1ee91a32019-01-28 11:26:46 -0500196 mPackageManager, generateSbn(channel.getId()), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400197
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 Benbernou1ee91a32019-01-28 11:26:46 -0500208 NotificationEntry entry =
209 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400210
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 Benbernou1ee91a32019-01-28 11:26:46 -0500222 NotificationEntry entry =
223 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400224
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 Benbernou1ee91a32019-01-28 11:26:46 -0500235 NotificationEntry entry =
236 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400237
238 assertFalse(entry.isOngoing());
239 }
240}