blob: 0ea53fa3e04fd9618720f28ae8f1c7ae2bfe2a2b [file] [log] [blame]
Julia Reynolds8a3b4592017-06-26 17:15:14 -04001/*
2 * Copyright (C) 2017 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 com.android.server.notification;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertNotNull;
21import static junit.framework.Assert.assertNull;
22
Julia Reynolds7217dc92018-03-07 12:12:09 -050023import static org.junit.Assert.assertFalse;
Julia Reynolds8a3b4592017-06-26 17:15:14 -040024import static org.junit.Assert.assertTrue;
Brett Chabot84151d92019-02-27 15:37:59 -080025import static org.mockito.Matchers.anyInt;
Julia Reynolds7217dc92018-03-07 12:12:09 -050026import static org.mockito.Mockito.mock;
Julia Reynolds8a3b4592017-06-26 17:15:14 -040027import static org.mockito.Mockito.spy;
28import static org.mockito.Mockito.when;
29
30import android.app.ActivityManager;
31import android.app.Notification;
Julia Reynolds7217dc92018-03-07 12:12:09 -050032import android.app.PendingIntent;
Brett Chabot84151d92019-02-27 15:37:59 -080033import android.app.Person;
Julia Reynolds7217dc92018-03-07 12:12:09 -050034import android.app.RemoteInput;
Julia Reynolds8a3b4592017-06-26 17:15:14 -040035import android.content.Context;
36import android.content.pm.ApplicationInfo;
Kristian Monsen30f59b22018-04-09 10:27:16 +020037import android.content.res.Resources;
Julia Reynolds7217dc92018-03-07 12:12:09 -050038import android.graphics.Bitmap;
Julia Reynolds8a3b4592017-06-26 17:15:14 -040039import android.graphics.Color;
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -040040import android.graphics.Typeface;
Julia Reynolds7217dc92018-03-07 12:12:09 -050041import android.graphics.drawable.Icon;
42import android.net.Uri;
Julia Reynolds8a3b4592017-06-26 17:15:14 -040043import android.os.Build;
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -040044import android.text.SpannableStringBuilder;
45import android.text.Spanned;
46import android.text.style.StyleSpan;
Tony Mak638430e2018-10-08 19:19:10 +010047import android.util.Pair;
Julia Reynolds7217dc92018-03-07 12:12:09 -050048import android.widget.RemoteViews;
Julia Reynolds8a3b4592017-06-26 17:15:14 -040049
Brett Chabot84151d92019-02-27 15:37:59 -080050import androidx.test.filters.SmallTest;
51import androidx.test.runner.AndroidJUnit4;
52
Jason Monk74f5e362017-12-06 08:56:33 -050053import com.android.server.UiServiceTestCase;
54
Julia Reynolds8a3b4592017-06-26 17:15:14 -040055import org.junit.Before;
56import org.junit.Test;
57import org.junit.runner.RunWith;
58import org.mockito.Mock;
59import org.mockito.MockitoAnnotations;
60
61@RunWith(AndroidJUnit4.class)
62@SmallTest
Jason Monk74f5e362017-12-06 08:56:33 -050063public class NotificationTest extends UiServiceTestCase {
Julia Reynolds8a3b4592017-06-26 17:15:14 -040064
65 @Mock
66 ActivityManager mAm;
67
Kristian Monsen30f59b22018-04-09 10:27:16 +020068 @Mock
69 Resources mResources;
70
Julia Reynolds8a3b4592017-06-26 17:15:14 -040071 @Before
72 public void setUp() {
73 MockitoAnnotations.initMocks(this);
74 }
75
76 @Test
Kristian Monsen30f59b22018-04-09 10:27:16 +020077 public void testStripsExtendersInLowRamModeNoWhitelistNoTv() {
Julia Reynolds8a3b4592017-06-26 17:15:14 -040078 Notification.Builder nb = new Notification.Builder(mContext, "channel");
79 nb.extend(new Notification.CarExtender().setColor(Color.RED));
80 nb.extend(new Notification.TvExtender().setChannelId("different channel"));
81 nb.extend(new Notification.WearableExtender().setDismissalId("dismiss"));
82 Notification before = nb.build();
83
Kristian Monsen30f59b22018-04-09 10:27:16 +020084 // No whitelist
85 Context context = spy(getContext());
86 when(context.getResources()).thenReturn(mResources);
87 when(mResources.getStringArray(anyInt())).thenReturn(new String[0]);
88
89 Notification after = Notification.Builder.maybeCloneStrippedForDelivery(before, true,
90 context);
Julia Reynolds8a3b4592017-06-26 17:15:14 -040091
92 assertEquals("different channel", new Notification.TvExtender(before).getChannelId());
93 assertNull(new Notification.TvExtender(after).getChannelId());
94
95 assertEquals(Color.RED, new Notification.CarExtender(before).getColor());
96 assertEquals(Notification.COLOR_DEFAULT, new Notification.CarExtender(after).getColor());
97
98 assertEquals("dismiss", new Notification.WearableExtender(before).getDismissalId());
99 assertNull(new Notification.WearableExtender(after).getDismissalId());
100 }
101
102 @Test
Kristian Monsen30f59b22018-04-09 10:27:16 +0200103 public void testStripsExtendersInLowRamModeHasWhitelist() {
104 Notification.Builder nb = new Notification.Builder(mContext, "channel");
105 nb.extend(new Notification.CarExtender().setColor(Color.RED));
106 nb.extend(new Notification.TvExtender().setChannelId("different channel"));
107 nb.extend(new Notification.WearableExtender().setDismissalId("dismiss"));
108 Notification before = nb.build();
109
110 // Has whitelist
111 Context context = spy(mContext);
112 when(context.getResources()).thenReturn(mResources);
113 when(mResources.getStringArray(anyInt())).thenReturn(new String[1]);
114
115 Notification after = Notification.Builder.maybeCloneStrippedForDelivery(before, true,
116 context);
117
118 assertEquals("different channel", new Notification.TvExtender(before).getChannelId());
119 assertEquals("different channel", new Notification.TvExtender(after).getChannelId());
120
121 assertEquals(Color.RED, new Notification.CarExtender(before).getColor());
122 assertEquals(Color.RED, new Notification.CarExtender(after).getColor());
123
124 assertEquals("dismiss", new Notification.WearableExtender(before).getDismissalId());
125 assertEquals("dismiss", new Notification.WearableExtender(after).getDismissalId());
126 }
127
128 @Test
Julia Reynolds8a3b4592017-06-26 17:15:14 -0400129 public void testStripsRemoteViewsInLowRamMode() {
Kristian Monsen30f59b22018-04-09 10:27:16 +0200130 Context context = spy(mContext);
Julia Reynolds8a3b4592017-06-26 17:15:14 -0400131 ApplicationInfo ai = new ApplicationInfo();
132 ai.targetSdkVersion = Build.VERSION_CODES.M;
133 when(context.getApplicationInfo()).thenReturn(ai);
134
135 final Notification.BigTextStyle style = new Notification.BigTextStyle()
136 .bigText("hello")
137 .setSummaryText("And the summary");
138 Notification before = new Notification.Builder(context, "channel")
139 .setContentText("hi")
140 .setStyle(style)
141 .build();
142
Kristian Monsen30f59b22018-04-09 10:27:16 +0200143 Notification after = Notification.Builder.maybeCloneStrippedForDelivery(before, true,
144 mContext);
Julia Reynolds8a3b4592017-06-26 17:15:14 -0400145 assertNotNull(before.contentView);
146 assertNotNull(before.bigContentView);
147 assertNotNull(before.headsUpContentView);
148 assertNull(after.contentView);
149 assertNull(after.bigContentView);
150 assertNull(after.headsUpContentView);
151 }
152
153 @Test
154 public void testDoesNotStripsExtendersInNormalRamMode() {
155 Notification.Builder nb = new Notification.Builder(mContext, "channel");
156 nb.extend(new Notification.CarExtender().setColor(Color.RED));
157 nb.extend(new Notification.TvExtender().setChannelId("different channel"));
158 nb.extend(new Notification.WearableExtender().setDismissalId("dismiss"));
159 Notification before = nb.build();
Kristian Monsen30f59b22018-04-09 10:27:16 +0200160 Notification after = Notification.Builder.maybeCloneStrippedForDelivery(before, false,
161 mContext);
Julia Reynolds8a3b4592017-06-26 17:15:14 -0400162
163 assertTrue(before == after);
164
165 assertEquals("different channel", new Notification.TvExtender(before).getChannelId());
166 assertEquals(Color.RED, new Notification.CarExtender(before).getColor());
167 assertEquals("dismiss", new Notification.WearableExtender(before).getDismissalId());
168 }
Julia Reynolds7217dc92018-03-07 12:12:09 -0500169
170 @Test
171 public void testStyleChangeVisiblyDifferent_noStyles() {
172 Notification.Builder n1 = new Notification.Builder(mContext, "test");
173 Notification.Builder n2 = new Notification.Builder(mContext, "test");
174
175 assertFalse(Notification.areStyledNotificationsVisiblyDifferent(n1, n2));
176 }
177
178 @Test
179 public void testStyleChangeVisiblyDifferent_noStyleToStyle() {
180 Notification.Builder n1 = new Notification.Builder(mContext, "test");
181 Notification.Builder n2 = new Notification.Builder(mContext, "test")
182 .setStyle(new Notification.BigTextStyle());
183
184 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(n1, n2));
185 }
186
187 @Test
188 public void testStyleChangeVisiblyDifferent_styleToNoStyle() {
189 Notification.Builder n2 = new Notification.Builder(mContext, "test");
190 Notification.Builder n1 = new Notification.Builder(mContext, "test")
191 .setStyle(new Notification.BigTextStyle());
192
193 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(n1, n2));
194 }
195
196 @Test
197 public void testStyleChangeVisiblyDifferent_changeStyle() {
198 Notification.Builder n1 = new Notification.Builder(mContext, "test")
199 .setStyle(new Notification.InboxStyle());
200 Notification.Builder n2 = new Notification.Builder(mContext, "test")
201 .setStyle(new Notification.BigTextStyle());
202
203 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(n1, n2));
204 }
205
206 @Test
207 public void testInboxTextChange() {
208 Notification.Builder nInbox1 = new Notification.Builder(mContext, "test")
209 .setStyle(new Notification.InboxStyle().addLine("a").addLine("b"));
210 Notification.Builder nInbox2 = new Notification.Builder(mContext, "test")
211 .setStyle(new Notification.InboxStyle().addLine("b").addLine("c"));
212
213 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(nInbox1, nInbox2));
214 }
215
216 @Test
217 public void testBigTextTextChange() {
218 Notification.Builder nBigText1 = new Notification.Builder(mContext, "test")
219 .setStyle(new Notification.BigTextStyle().bigText("something"));
220 Notification.Builder nBigText2 = new Notification.Builder(mContext, "test")
221 .setStyle(new Notification.BigTextStyle().bigText("else"));
222
223 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(nBigText1, nBigText2));
224 }
225
226 @Test
227 public void testBigPictureChange() {
Dan Sandler7d67bd42018-05-15 14:06:38 -0400228 Bitmap bitA = mock(Bitmap.class);
229 when(bitA.getGenerationId()).thenReturn(100);
230 Bitmap bitB = mock(Bitmap.class);
231 when(bitB.getGenerationId()).thenReturn(200);
232
Julia Reynolds7217dc92018-03-07 12:12:09 -0500233 Notification.Builder nBigPic1 = new Notification.Builder(mContext, "test")
Dan Sandler7d67bd42018-05-15 14:06:38 -0400234 .setStyle(new Notification.BigPictureStyle().bigPicture(bitA));
Julia Reynolds7217dc92018-03-07 12:12:09 -0500235 Notification.Builder nBigPic2 = new Notification.Builder(mContext, "test")
Dan Sandler7d67bd42018-05-15 14:06:38 -0400236 .setStyle(new Notification.BigPictureStyle().bigPicture(bitB));
Julia Reynolds7217dc92018-03-07 12:12:09 -0500237
238 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(nBigPic1, nBigPic2));
239 }
240
241 @Test
242 public void testMessagingChange_text() {
243 Notification.Builder nM1 = new Notification.Builder(mContext, "test")
244 .setStyle(new Notification.MessagingStyle("")
245 .addMessage(new Notification.MessagingStyle.Message(
Selim Cinek9acd6732018-03-23 16:39:02 -0700246 "a", 100, mock(Person.class))));
Julia Reynolds7217dc92018-03-07 12:12:09 -0500247 Notification.Builder nM2 = new Notification.Builder(mContext, "test")
248 .setStyle(new Notification.MessagingStyle("")
249 .addMessage(new Notification.MessagingStyle.Message(
Selim Cinek9acd6732018-03-23 16:39:02 -0700250 "a", 100, mock(Person.class)))
Julia Reynolds7217dc92018-03-07 12:12:09 -0500251 .addMessage(new Notification.MessagingStyle.Message(
Selim Cinek9acd6732018-03-23 16:39:02 -0700252 "b", 100, mock(Person.class)))
Julia Reynolds7217dc92018-03-07 12:12:09 -0500253 );
254
255 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(nM1, nM2));
256 }
257
258 @Test
259 public void testMessagingChange_data() {
260 Notification.Builder nM1 = new Notification.Builder(mContext, "test")
261 .setStyle(new Notification.MessagingStyle("")
262 .addMessage(new Notification.MessagingStyle.Message(
263 "a", 100, mock(Person.class))
264 .setData("text", mock(Uri.class))));
265 Notification.Builder nM2 = new Notification.Builder(mContext, "test")
266 .setStyle(new Notification.MessagingStyle("")
267 .addMessage(new Notification.MessagingStyle.Message(
268 "a", 100, mock(Person.class))));
269
270 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(nM1, nM2));
271 }
272
273 @Test
274 public void testMessagingChange_sender() {
275 Person a = mock(Person.class);
276 when(a.getName()).thenReturn("A");
277 Person b = mock(Person.class);
278 when(b.getName()).thenReturn("b");
279 Notification.Builder nM1 = new Notification.Builder(mContext, "test")
280 .setStyle(new Notification.MessagingStyle("")
281 .addMessage(new Notification.MessagingStyle.Message("a", 100, b)));
282 Notification.Builder nM2 = new Notification.Builder(mContext, "test")
283 .setStyle(new Notification.MessagingStyle("")
284 .addMessage(new Notification.MessagingStyle.Message("a", 100, a)));
285
286 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(nM1, nM2));
287 }
288
289 @Test
290 public void testMessagingChange_key() {
291 Person a = mock(Person.class);
292 when(a.getKey()).thenReturn("A");
293 Person b = mock(Person.class);
294 when(b.getKey()).thenReturn("b");
295 Notification.Builder nM1 = new Notification.Builder(mContext, "test")
296 .setStyle(new Notification.MessagingStyle("")
297 .addMessage(new Notification.MessagingStyle.Message("a", 100, a)));
298 Notification.Builder nM2 = new Notification.Builder(mContext, "test")
299 .setStyle(new Notification.MessagingStyle("")
300 .addMessage(new Notification.MessagingStyle.Message("a", 100, b)));
301
302 assertTrue(Notification.areStyledNotificationsVisiblyDifferent(nM1, nM2));
303 }
304
305 @Test
306 public void testMessagingChange_ignoreTimeChange() {
307 Notification.Builder nM1 = new Notification.Builder(mContext, "test")
308 .setStyle(new Notification.MessagingStyle("")
309 .addMessage(new Notification.MessagingStyle.Message(
Selim Cinek9acd6732018-03-23 16:39:02 -0700310 "a", 100, mock(Person.class))));
Julia Reynolds7217dc92018-03-07 12:12:09 -0500311 Notification.Builder nM2 = new Notification.Builder(mContext, "test")
312 .setStyle(new Notification.MessagingStyle("")
313 .addMessage(new Notification.MessagingStyle.Message(
Selim Cinek9acd6732018-03-23 16:39:02 -0700314 "a", 1000, mock(Person.class)))
Julia Reynolds7217dc92018-03-07 12:12:09 -0500315 );
316
317 assertFalse(Notification.areStyledNotificationsVisiblyDifferent(nM1, nM2));
318 }
319
320 @Test
321 public void testRemoteViews_nullChange() {
322 Notification.Builder n1 = new Notification.Builder(mContext, "test")
323 .setContent(mock(RemoteViews.class));
324 Notification.Builder n2 = new Notification.Builder(mContext, "test");
325 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
326
327 n1 = new Notification.Builder(mContext, "test");
328 n2 = new Notification.Builder(mContext, "test")
329 .setContent(mock(RemoteViews.class));
330 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
331
332 n1 = new Notification.Builder(mContext, "test")
333 .setCustomBigContentView(mock(RemoteViews.class));
334 n2 = new Notification.Builder(mContext, "test");
335 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
336
337 n1 = new Notification.Builder(mContext, "test");
338 n2 = new Notification.Builder(mContext, "test")
339 .setCustomBigContentView(mock(RemoteViews.class));
340 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
341
342 n1 = new Notification.Builder(mContext, "test");
343 n2 = new Notification.Builder(mContext, "test");
344 assertFalse(Notification.areRemoteViewsChanged(n1, n2));
345 }
346
347 @Test
Julia Reynoldse5c60452018-04-30 14:41:36 -0400348 public void testRemoteViews_layoutChange() {
349 RemoteViews a = mock(RemoteViews.class);
350 when(a.getLayoutId()).thenReturn(234);
351 RemoteViews b = mock(RemoteViews.class);
352 when(b.getLayoutId()).thenReturn(189);
353
354 Notification.Builder n1 = new Notification.Builder(mContext, "test").setContent(a);
355 Notification.Builder n2 = new Notification.Builder(mContext, "test").setContent(b);
356 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
357
358 n1 = new Notification.Builder(mContext, "test").setCustomBigContentView(a);
359 n2 = new Notification.Builder(mContext, "test").setCustomBigContentView(b);
360 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
361
362 n1 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(a);
363 n2 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(b);
364 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
365 }
366
367 @Test
368 public void testRemoteViews_layoutSame() {
369 RemoteViews a = mock(RemoteViews.class);
370 when(a.getLayoutId()).thenReturn(234);
371 RemoteViews b = mock(RemoteViews.class);
372 when(b.getLayoutId()).thenReturn(234);
373
374 Notification.Builder n1 = new Notification.Builder(mContext, "test").setContent(a);
375 Notification.Builder n2 = new Notification.Builder(mContext, "test").setContent(b);
376 assertFalse(Notification.areRemoteViewsChanged(n1, n2));
377
378 n1 = new Notification.Builder(mContext, "test").setCustomBigContentView(a);
379 n2 = new Notification.Builder(mContext, "test").setCustomBigContentView(b);
380 assertFalse(Notification.areRemoteViewsChanged(n1, n2));
381
382 n1 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(a);
383 n2 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(b);
384 assertFalse(Notification.areRemoteViewsChanged(n1, n2));
385 }
386
387 @Test
388 public void testRemoteViews_sequenceChange() {
389 RemoteViews a = mock(RemoteViews.class);
390 when(a.getLayoutId()).thenReturn(234);
391 when(a.getSequenceNumber()).thenReturn(1);
392 RemoteViews b = mock(RemoteViews.class);
393 when(b.getLayoutId()).thenReturn(234);
394 when(b.getSequenceNumber()).thenReturn(2);
395
396 Notification.Builder n1 = new Notification.Builder(mContext, "test").setContent(a);
397 Notification.Builder n2 = new Notification.Builder(mContext, "test").setContent(b);
398 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
399
400 n1 = new Notification.Builder(mContext, "test").setCustomBigContentView(a);
401 n2 = new Notification.Builder(mContext, "test").setCustomBigContentView(b);
402 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
403
404 n1 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(a);
405 n2 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(b);
406 assertTrue(Notification.areRemoteViewsChanged(n1, n2));
407 }
408
409 @Test
410 public void testRemoteViews_sequenceSame() {
411 RemoteViews a = mock(RemoteViews.class);
412 when(a.getLayoutId()).thenReturn(234);
413 when(a.getSequenceNumber()).thenReturn(1);
414 RemoteViews b = mock(RemoteViews.class);
415 when(b.getLayoutId()).thenReturn(234);
416 when(b.getSequenceNumber()).thenReturn(1);
417
418 Notification.Builder n1 = new Notification.Builder(mContext, "test").setContent(a);
419 Notification.Builder n2 = new Notification.Builder(mContext, "test").setContent(b);
420 assertFalse(Notification.areRemoteViewsChanged(n1, n2));
421
422 n1 = new Notification.Builder(mContext, "test").setCustomBigContentView(a);
423 n2 = new Notification.Builder(mContext, "test").setCustomBigContentView(b);
424 assertFalse(Notification.areRemoteViewsChanged(n1, n2));
425
426 n1 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(a);
427 n2 = new Notification.Builder(mContext, "test").setCustomHeadsUpContentView(b);
428 assertFalse(Notification.areRemoteViewsChanged(n1, n2));
429 }
430
431 @Test
Julia Reynolds7217dc92018-03-07 12:12:09 -0500432 public void testActionsDifferent_null() {
433 Notification n1 = new Notification.Builder(mContext, "test")
434 .build();
435 Notification n2 = new Notification.Builder(mContext, "test")
436 .build();
437
438 assertFalse(Notification.areActionsVisiblyDifferent(n1, n2));
439 }
440
441 @Test
442 public void testActionsDifferentSame() {
443 PendingIntent intent = mock(PendingIntent.class);
444 Icon icon = mock(Icon.class);
445
446 Notification n1 = new Notification.Builder(mContext, "test")
447 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent).build())
448 .build();
449 Notification n2 = new Notification.Builder(mContext, "test")
450 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent).build())
451 .build();
452
453 assertFalse(Notification.areActionsVisiblyDifferent(n1, n2));
454 }
455
456 @Test
457 public void testActionsDifferentText() {
458 PendingIntent intent = mock(PendingIntent.class);
459 Icon icon = mock(Icon.class);
460
461 Notification n1 = new Notification.Builder(mContext, "test")
462 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent).build())
463 .build();
464 Notification n2 = new Notification.Builder(mContext, "test")
465 .addAction(new Notification.Action.Builder(icon, "TEXT 2", intent).build())
466 .build();
467
468 assertTrue(Notification.areActionsVisiblyDifferent(n1, n2));
469 }
470
471 @Test
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -0400472 public void testActionsDifferentSpannables() {
473 PendingIntent intent = mock(PendingIntent.class);
474 Icon icon = mock(Icon.class);
475
476 Notification n1 = new Notification.Builder(mContext, "test")
477 .addAction(new Notification.Action.Builder(icon,
478 new SpannableStringBuilder().append("test1",
479 new StyleSpan(Typeface.BOLD),
480 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE),
481 intent).build())
482 .build();
483 Notification n2 = new Notification.Builder(mContext, "test")
484 .addAction(new Notification.Action.Builder(icon, "test1", intent).build())
485 .build();
486
487 assertFalse(Notification.areActionsVisiblyDifferent(n1, n2));
488 }
489
490 @Test
Julia Reynolds7217dc92018-03-07 12:12:09 -0500491 public void testActionsDifferentNumber() {
492 PendingIntent intent = mock(PendingIntent.class);
493 Icon icon = mock(Icon.class);
494
495 Notification n1 = new Notification.Builder(mContext, "test")
496 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent).build())
497 .build();
498 Notification n2 = new Notification.Builder(mContext, "test")
499 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent).build())
500 .addAction(new Notification.Action.Builder(icon, "TEXT 2", intent).build())
501 .build();
502
503 assertTrue(Notification.areActionsVisiblyDifferent(n1, n2));
504 }
505
506 @Test
507 public void testActionsDifferentIntent() {
508 PendingIntent intent1 = mock(PendingIntent.class);
509 PendingIntent intent2 = mock(PendingIntent.class);
510 Icon icon = mock(Icon.class);
511
512 Notification n1 = new Notification.Builder(mContext, "test")
513 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent1).build())
514 .build();
515 Notification n2 = new Notification.Builder(mContext, "test")
516 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent2).build())
517 .build();
518
519 assertFalse(Notification.areActionsVisiblyDifferent(n1, n2));
520 }
521
522 @Test
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -0400523 public void testActionsIgnoresRemoteInputs() {
Julia Reynolds7217dc92018-03-07 12:12:09 -0500524 PendingIntent intent = mock(PendingIntent.class);
525 Icon icon = mock(Icon.class);
526
527 Notification n1 = new Notification.Builder(mContext, "test")
528 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent)
529 .addRemoteInput(new RemoteInput.Builder("a")
530 .setChoices(new CharSequence[] {"i", "m"})
531 .build())
532 .build())
533 .build();
534 Notification n2 = new Notification.Builder(mContext, "test")
535 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent)
536 .addRemoteInput(new RemoteInput.Builder("a")
537 .setChoices(new CharSequence[] {"t", "m"})
538 .build())
539 .build())
540 .build();
541
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -0400542 assertFalse(Notification.areActionsVisiblyDifferent(n1, n2));
Julia Reynolds7217dc92018-03-07 12:12:09 -0500543 }
Tony Mak638430e2018-10-08 19:19:10 +0100544
545 @Test
546 public void testFreeformRemoteInputActionPair_noRemoteInput() {
547 PendingIntent intent = mock(PendingIntent.class);
548 Icon icon = mock(Icon.class);
549 Notification notification = new Notification.Builder(mContext, "test")
550 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent)
551 .build())
552 .build();
553 assertNull(notification.findRemoteInputActionPair(false));
554 }
555
556 @Test
557 public void testFreeformRemoteInputActionPair_hasRemoteInput() {
558 PendingIntent intent = mock(PendingIntent.class);
559 Icon icon = mock(Icon.class);
560
561 RemoteInput remoteInput = new RemoteInput.Builder("a").build();
562
563 Notification.Action actionWithRemoteInput =
564 new Notification.Action.Builder(icon, "TEXT 1", intent)
565 .addRemoteInput(remoteInput)
566 .addRemoteInput(remoteInput)
567 .build();
568
569 Notification.Action actionWithoutRemoteInput =
570 new Notification.Action.Builder(icon, "TEXT 2", intent)
571 .build();
572
573 Notification notification = new Notification.Builder(mContext, "test")
574 .addAction(actionWithoutRemoteInput)
575 .addAction(actionWithRemoteInput)
576 .build();
577
578 Pair<RemoteInput, Notification.Action> remoteInputActionPair =
579 notification.findRemoteInputActionPair(false);
580
581 assertNotNull(remoteInputActionPair);
582 assertEquals(remoteInput, remoteInputActionPair.first);
583 assertEquals(actionWithRemoteInput, remoteInputActionPair.second);
584 }
585
586 @Test
587 public void testFreeformRemoteInputActionPair_requestFreeform_noFreeformRemoteInput() {
588 PendingIntent intent = mock(PendingIntent.class);
589 Icon icon = mock(Icon.class);
590 Notification notification = new Notification.Builder(mContext, "test")
591 .addAction(new Notification.Action.Builder(icon, "TEXT 1", intent)
592 .addRemoteInput(
593 new RemoteInput.Builder("a")
594 .setAllowFreeFormInput(false).build())
595 .build())
596 .build();
597 assertNull(notification.findRemoteInputActionPair(true));
598 }
599
600 @Test
601 public void testFreeformRemoteInputActionPair_requestFreeform_hasFreeformRemoteInput() {
602 PendingIntent intent = mock(PendingIntent.class);
603 Icon icon = mock(Icon.class);
604
605 RemoteInput remoteInput =
606 new RemoteInput.Builder("a").setAllowFreeFormInput(false).build();
607 RemoteInput freeformRemoteInput =
608 new RemoteInput.Builder("b").setAllowFreeFormInput(true).build();
609
610 Notification.Action actionWithFreeformRemoteInput =
611 new Notification.Action.Builder(icon, "TEXT 1", intent)
612 .addRemoteInput(remoteInput)
613 .addRemoteInput(freeformRemoteInput)
614 .build();
615
616 Notification.Action actionWithoutFreeformRemoteInput =
617 new Notification.Action.Builder(icon, "TEXT 2", intent)
618 .addRemoteInput(remoteInput)
619 .build();
620
621 Notification notification = new Notification.Builder(mContext, "test")
622 .addAction(actionWithoutFreeformRemoteInput)
623 .addAction(actionWithFreeformRemoteInput)
624 .build();
625
626 Pair<RemoteInput, Notification.Action> remoteInputActionPair =
627 notification.findRemoteInputActionPair(true);
628
629 assertNotNull(remoteInputActionPair);
630 assertEquals(freeformRemoteInput, remoteInputActionPair.first);
631 assertEquals(actionWithFreeformRemoteInput, remoteInputActionPair.second);
632 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -0400633}
634