blob: 2d44e79d0f828dd73c21c31a9f24e5073a3848b0 [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;
42import android.support.test.InstrumentationRegistry;
43import android.support.test.runner.AndroidJUnit4;
44import android.testing.TestableContext;
45
46import org.junit.Before;
47import org.junit.Rule;
48import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.mockito.Mock;
51import org.mockito.MockitoAnnotations;
52
53import java.util.ArrayList;
54
55@RunWith(AndroidJUnit4.class)
56public 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 Benbernou1ee91a32019-01-28 11:26:46 -050063 @Mock
64 private SmsHelper mSmsHelper;
65
66 private static final String DEFAULT_SMS_PACKAGE_NAME = "foo";
Julia Reynolds901bf282018-08-14 10:09:36 -040067
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 Benbernou1ee91a32019-01-28 11:26:46 -050081 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 Reynolds901bf282018-08-14 10:09:36 -040090 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 Benbernou1ee91a32019-01-28 11:26:46 -0500103 when(mSmsHelper.getDefaultSmsApplication())
104 .thenReturn(new ComponentName(DEFAULT_SMS_PACKAGE_NAME, "bar"));
Julia Reynolds901bf282018-08-14 10:09:36 -0400105 }
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 Benbernou1ee91a32019-01-28 11:26:46 -0500115 NotificationEntry entry = new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400116 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 Benbernou1ee91a32019-01-28 11:26:46 -0500123 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 Reynolds901bf282018-08-14 10:09:36 -0400140 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 Benbernou1ee91a32019-01-28 11:26:46 -0500150 NotificationEntry entry =
151 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400152 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 Benbernou1ee91a32019-01-28 11:26:46 -0500162 NotificationEntry entry =
163 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400164 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 Benbernou1ee91a32019-01-28 11:26:46 -0500174 NotificationEntry entry =
175 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400176 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 Benbernou1ee91a32019-01-28 11:26:46 -0500186 mPackageManager, generateSbn(channel.getId()), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400187
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 Benbernou1ee91a32019-01-28 11:26:46 -0500195 mPackageManager, generateSbn(channel.getId()), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400196
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 Benbernou1ee91a32019-01-28 11:26:46 -0500207 NotificationEntry entry =
208 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400209
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 Benbernou1ee91a32019-01-28 11:26:46 -0500221 NotificationEntry entry =
222 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400223
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 Benbernou1ee91a32019-01-28 11:26:46 -0500234 NotificationEntry entry =
235 new NotificationEntry(mPackageManager, generateSbn(n), channel, mSmsHelper);
Julia Reynolds901bf282018-08-14 10:09:36 -0400236
237 assertFalse(entry.isOngoing());
238 }
239}