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