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