Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [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 android.app; |
| 18 | |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 19 | import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast; |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 20 | |
Kodlee Yin | c72c44d | 2017-12-21 22:07:15 +0000 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
Julia Reynolds | d71c5a9 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 22 | import static org.junit.Assert.assertFalse; |
Robin Lee | ad7e72a | 2017-12-04 15:45:46 +0100 | [diff] [blame] | 23 | import static org.junit.Assert.assertNotSame; |
Felipe Leme | 90205ef | 2019-03-05 09:59:52 -0800 | [diff] [blame] | 24 | import static org.junit.Assert.assertNull; |
Robin Lee | ad7e72a | 2017-12-04 15:45:46 +0100 | [diff] [blame] | 25 | import static org.junit.Assert.assertSame; |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
Kodlee Yin | c72c44d | 2017-12-21 22:07:15 +0000 | [diff] [blame] | 28 | import android.annotation.Nullable; |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 30 | import android.content.Intent; |
Felipe Leme | 90205ef | 2019-03-05 09:59:52 -0800 | [diff] [blame] | 31 | import android.content.LocusId; |
Robin Lee | ad7e72a | 2017-12-04 15:45:46 +0100 | [diff] [blame] | 32 | import android.graphics.BitmapFactory; |
| 33 | import android.graphics.drawable.Icon; |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 34 | import android.media.session.MediaSession; |
Kodlee Yin | 1465642 | 2017-12-22 17:00:46 -0800 | [diff] [blame] | 35 | import android.os.Build; |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 36 | import android.os.Parcel; |
Robin Lee | ad7e72a | 2017-12-04 15:45:46 +0100 | [diff] [blame] | 37 | import android.os.Parcelable; |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 38 | import android.widget.RemoteViews; |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 39 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 40 | import androidx.test.InstrumentationRegistry; |
| 41 | import androidx.test.filters.SmallTest; |
| 42 | import androidx.test.runner.AndroidJUnit4; |
| 43 | |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | |
Kodlee Yin | c72c44d | 2017-12-21 22:07:15 +0000 | [diff] [blame] | 48 | import java.util.function.Consumer; |
| 49 | |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 50 | @RunWith(AndroidJUnit4.class) |
| 51 | @SmallTest |
| 52 | public class NotificationTest { |
| 53 | |
| 54 | private Context mContext; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() { |
| 58 | mContext = InstrumentationRegistry.getContext(); |
| 59 | } |
| 60 | |
| 61 | @Test |
Julia Reynolds | d71c5a9 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 62 | public void testColorizedByPermission() { |
| 63 | Notification n = new Notification.Builder(mContext, "test") |
| 64 | .setFlag(Notification.FLAG_CAN_COLORIZE, true) |
| 65 | .setColorized(true) |
| 66 | .build(); |
| 67 | assertTrue(n.isColorized()); |
| 68 | |
| 69 | n = new Notification.Builder(mContext, "test") |
| 70 | .setFlag(Notification.FLAG_CAN_COLORIZE, true) |
| 71 | .build(); |
| 72 | assertFalse(n.isColorized()); |
| 73 | |
| 74 | n = new Notification.Builder(mContext, "test") |
| 75 | .setFlag(Notification.FLAG_CAN_COLORIZE, false) |
| 76 | .setColorized(true) |
| 77 | .build(); |
| 78 | assertFalse(n.isColorized()); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testColorizedByForeground() { |
| 83 | Notification n = new Notification.Builder(mContext, "test") |
| 84 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 85 | .setColorized(true) |
| 86 | .build(); |
| 87 | assertTrue(n.isColorized()); |
| 88 | |
| 89 | n = new Notification.Builder(mContext, "test") |
| 90 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 91 | .build(); |
| 92 | assertFalse(n.isColorized()); |
| 93 | |
| 94 | n = new Notification.Builder(mContext, "test") |
| 95 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, false) |
| 96 | .setColorized(true) |
| 97 | .build(); |
| 98 | assertFalse(n.isColorized()); |
| 99 | } |
| 100 | |
| 101 | @Test |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 102 | public void testColorSatisfiedWhenBgDarkTextDarker() { |
| 103 | Notification.Builder builder = getMediaNotification(); |
Julia Reynolds | d71c5a9 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 104 | Notification n = builder.build(); |
| 105 | |
| 106 | assertTrue(n.isColorized()); |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 107 | |
| 108 | // An initial guess where the foreground color is actually darker than an already dark bg |
| 109 | int backgroundColor = 0xff585868; |
| 110 | int initialForegroundColor = 0xff505868; |
| 111 | builder.setColorPalette(backgroundColor, initialForegroundColor); |
Selim Cinek | 10bbc0e | 2018-11-21 15:14:10 -0800 | [diff] [blame] | 112 | int primaryTextColor = builder.getPrimaryTextColor(builder.mParams); |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 113 | assertTrue(satisfiesTextContrast(primaryTextColor, backgroundColor)); |
Selim Cinek | 10bbc0e | 2018-11-21 15:14:10 -0800 | [diff] [blame] | 114 | int secondaryTextColor = builder.getSecondaryTextColor(builder.mParams); |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 115 | assertTrue(satisfiesTextContrast(secondaryTextColor, backgroundColor)); |
| 116 | } |
| 117 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 118 | @Test |
| 119 | public void testHasCompletedProgress_noProgress() { |
| 120 | Notification n = new Notification.Builder(mContext).build(); |
| 121 | |
| 122 | assertFalse(n.hasCompletedProgress()); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testHasCompletedProgress_complete() { |
| 127 | Notification n = new Notification.Builder(mContext) |
| 128 | .setProgress(100, 100, true) |
| 129 | .build(); |
| 130 | Notification n2 = new Notification.Builder(mContext) |
| 131 | .setProgress(10, 10, false) |
| 132 | .build(); |
| 133 | assertTrue(n.hasCompletedProgress()); |
| 134 | assertTrue(n2.hasCompletedProgress()); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testHasCompletedProgress_notComplete() { |
| 139 | Notification n = new Notification.Builder(mContext) |
| 140 | .setProgress(100, 99, true) |
| 141 | .build(); |
| 142 | Notification n2 = new Notification.Builder(mContext) |
| 143 | .setProgress(10, 4, false) |
| 144 | .build(); |
| 145 | assertFalse(n.hasCompletedProgress()); |
| 146 | assertFalse(n2.hasCompletedProgress()); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void testHasCompletedProgress_zeroMax() { |
| 151 | Notification n = new Notification.Builder(mContext) |
| 152 | .setProgress(0, 0, true) |
| 153 | .build(); |
| 154 | assertFalse(n.hasCompletedProgress()); |
| 155 | } |
| 156 | |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 157 | @Test |
Robin Lee | ad7e72a | 2017-12-04 15:45:46 +0100 | [diff] [blame] | 158 | public void largeIconMultipleReferences_keptAfterParcelling() { |
| 159 | Icon originalIcon = Icon.createWithBitmap(BitmapFactory.decodeResource( |
| 160 | mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96)); |
| 161 | |
| 162 | Notification n = new Notification.Builder(mContext).setLargeIcon(originalIcon).build(); |
| 163 | assertSame(n.getLargeIcon(), originalIcon); |
| 164 | |
| 165 | Notification q = writeAndReadParcelable(n); |
| 166 | assertNotSame(q.getLargeIcon(), n.getLargeIcon()); |
| 167 | |
| 168 | assertTrue(q.getLargeIcon().getBitmap().sameAs(n.getLargeIcon().getBitmap())); |
| 169 | assertSame(q.getLargeIcon(), q.extras.getParcelable(Notification.EXTRA_LARGE_ICON)); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void largeIconReferenceInExtrasOnly_keptAfterParcelling() { |
| 174 | Icon originalIcon = Icon.createWithBitmap(BitmapFactory.decodeResource( |
| 175 | mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96)); |
| 176 | |
| 177 | Notification n = new Notification.Builder(mContext).build(); |
| 178 | n.extras.putParcelable(Notification.EXTRA_LARGE_ICON, originalIcon); |
| 179 | assertSame(n.getLargeIcon(), null); |
| 180 | |
| 181 | Notification q = writeAndReadParcelable(n); |
| 182 | assertSame(q.getLargeIcon(), null); |
| 183 | assertTrue(((Icon) q.extras.getParcelable(Notification.EXTRA_LARGE_ICON)).getBitmap() |
| 184 | .sameAs(originalIcon.getBitmap())); |
| 185 | } |
| 186 | |
| 187 | @Test |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 188 | public void allPendingIntents_recollectedAfterReusingBuilder() { |
| 189 | PendingIntent intent1 = PendingIntent.getActivity(mContext, 0, new Intent("test1"), 0); |
| 190 | PendingIntent intent2 = PendingIntent.getActivity(mContext, 0, new Intent("test2"), 0); |
| 191 | |
| 192 | Notification.Builder builder = new Notification.Builder(mContext, "channel"); |
| 193 | builder.setContentIntent(intent1); |
| 194 | |
| 195 | Parcel p = Parcel.obtain(); |
| 196 | |
| 197 | Notification n1 = builder.build(); |
| 198 | n1.writeToParcel(p, 0); |
| 199 | |
| 200 | builder.setContentIntent(intent2); |
| 201 | Notification n2 = builder.build(); |
| 202 | n2.writeToParcel(p, 0); |
| 203 | |
| 204 | assertTrue(n2.allPendingIntents.contains(intent2)); |
| 205 | } |
| 206 | |
| 207 | @Test |
| 208 | public void allPendingIntents_containsCustomRemoteViews() { |
| 209 | PendingIntent intent = PendingIntent.getActivity(mContext, 0, new Intent("test"), 0); |
| 210 | |
| 211 | RemoteViews contentView = new RemoteViews(mContext.getPackageName(), 0 /* layoutId */); |
| 212 | contentView.setOnClickPendingIntent(1 /* id */, intent); |
| 213 | |
| 214 | Notification.Builder builder = new Notification.Builder(mContext, "channel"); |
| 215 | builder.setCustomContentView(contentView); |
| 216 | |
| 217 | Parcel p = Parcel.obtain(); |
| 218 | |
| 219 | Notification n = builder.build(); |
| 220 | n.writeToParcel(p, 0); |
| 221 | |
| 222 | assertTrue(n.allPendingIntents.contains(intent)); |
| 223 | } |
| 224 | |
Kodlee Yin | 9ac617c | 2017-12-19 11:20:50 -0800 | [diff] [blame] | 225 | @Test |
Kodlee Yin | 1465642 | 2017-12-22 17:00:46 -0800 | [diff] [blame] | 226 | public void messagingStyle_isGroupConversation() { |
| 227 | mContext.getApplicationInfo().targetSdkVersion = Build.VERSION_CODES.P; |
Kodlee Yin | 9ac617c | 2017-12-19 11:20:50 -0800 | [diff] [blame] | 228 | Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle("self name") |
Kodlee Yin | 1465642 | 2017-12-22 17:00:46 -0800 | [diff] [blame] | 229 | .setGroupConversation(true) |
| 230 | .setConversationTitle("test conversation title"); |
Kodlee Yin | 9ac617c | 2017-12-19 11:20:50 -0800 | [diff] [blame] | 231 | Notification notification = new Notification.Builder(mContext, "test id") |
| 232 | .setSmallIcon(1) |
| 233 | .setContentTitle("test title") |
| 234 | .setStyle(messagingStyle) |
| 235 | .build(); |
| 236 | |
| 237 | assertTrue(messagingStyle.isGroupConversation()); |
| 238 | assertTrue(notification.extras.getBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION)); |
| 239 | } |
| 240 | |
Kodlee Yin | 1465642 | 2017-12-22 17:00:46 -0800 | [diff] [blame] | 241 | @Test |
| 242 | public void messagingStyle_isGroupConversation_noConversationTitle() { |
| 243 | mContext.getApplicationInfo().targetSdkVersion = Build.VERSION_CODES.P; |
| 244 | Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle("self name") |
| 245 | .setGroupConversation(true) |
| 246 | .setConversationTitle(null); |
| 247 | Notification notification = new Notification.Builder(mContext, "test id") |
| 248 | .setSmallIcon(1) |
| 249 | .setContentTitle("test title") |
| 250 | .setStyle(messagingStyle) |
| 251 | .build(); |
| 252 | |
| 253 | assertTrue(messagingStyle.isGroupConversation()); |
| 254 | assertTrue(notification.extras.getBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION)); |
| 255 | } |
| 256 | |
| 257 | @Test |
| 258 | public void messagingStyle_isGroupConversation_withConversationTitle_legacy() { |
| 259 | // In legacy (version < P), isGroupConversation is controlled by conversationTitle. |
| 260 | mContext.getApplicationInfo().targetSdkVersion = Build.VERSION_CODES.O; |
| 261 | Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle("self name") |
| 262 | .setGroupConversation(false) |
| 263 | .setConversationTitle("test conversation title"); |
| 264 | Notification notification = new Notification.Builder(mContext, "test id") |
| 265 | .setSmallIcon(1) |
| 266 | .setContentTitle("test title") |
| 267 | .setStyle(messagingStyle) |
| 268 | .build(); |
| 269 | |
| 270 | assertTrue(messagingStyle.isGroupConversation()); |
| 271 | assertFalse(notification.extras.getBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION)); |
| 272 | } |
| 273 | |
| 274 | @Test |
| 275 | public void messagingStyle_isGroupConversation_withoutConversationTitle_legacy() { |
| 276 | // In legacy (version < P), isGroupConversation is controlled by conversationTitle. |
| 277 | mContext.getApplicationInfo().targetSdkVersion = Build.VERSION_CODES.O; |
| 278 | Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle("self name") |
| 279 | .setGroupConversation(true) |
| 280 | .setConversationTitle(null); |
| 281 | Notification notification = new Notification.Builder(mContext, "test id") |
| 282 | .setSmallIcon(1) |
| 283 | .setContentTitle("test title") |
| 284 | .setStyle(messagingStyle) |
| 285 | .build(); |
| 286 | |
| 287 | assertFalse(messagingStyle.isGroupConversation()); |
| 288 | assertTrue(notification.extras.getBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION)); |
| 289 | } |
| 290 | |
Kodlee Yin | c72c44d | 2017-12-21 22:07:15 +0000 | [diff] [blame] | 291 | @Test |
| 292 | public void action_builder_hasDefault() { |
| 293 | Notification.Action action = makeNotificationAction(null); |
| 294 | assertEquals(Notification.Action.SEMANTIC_ACTION_NONE, action.getSemanticAction()); |
| 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void action_builder_setSemanticAction() { |
| 299 | Notification.Action action = makeNotificationAction( |
| 300 | builder -> builder.setSemanticAction(Notification.Action.SEMANTIC_ACTION_REPLY)); |
| 301 | assertEquals(Notification.Action.SEMANTIC_ACTION_REPLY, action.getSemanticAction()); |
| 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void action_parcel() { |
| 306 | Notification.Action action = writeAndReadParcelable( |
| 307 | makeNotificationAction(builder -> { |
| 308 | builder.setSemanticAction(Notification.Action.SEMANTIC_ACTION_ARCHIVE); |
| 309 | builder.setAllowGeneratedReplies(true); |
| 310 | })); |
| 311 | |
| 312 | assertEquals(Notification.Action.SEMANTIC_ACTION_ARCHIVE, action.getSemanticAction()); |
| 313 | assertTrue(action.getAllowGeneratedReplies()); |
| 314 | } |
| 315 | |
| 316 | @Test |
| 317 | public void action_clone() { |
| 318 | Notification.Action action = makeNotificationAction( |
| 319 | builder -> builder.setSemanticAction(Notification.Action.SEMANTIC_ACTION_DELETE)); |
| 320 | assertEquals( |
| 321 | Notification.Action.SEMANTIC_ACTION_DELETE, |
| 322 | action.clone().getSemanticAction()); |
| 323 | } |
| 324 | |
Felipe Leme | 90205ef | 2019-03-05 09:59:52 -0800 | [diff] [blame] | 325 | @Test |
| 326 | public void testBuilder_setLocusId() { |
| 327 | LocusId locusId = new LocusId("4815162342"); |
| 328 | Notification notification = new Notification.Builder(mContext, "whatever") |
| 329 | .setLocusId(locusId).build(); |
| 330 | assertEquals(locusId, notification.getLocusId()); |
| 331 | |
| 332 | Notification clone = writeAndReadParcelable(notification); |
| 333 | assertEquals(locusId, clone.getLocusId()); |
| 334 | } |
| 335 | |
| 336 | @Test |
| 337 | public void testBuilder_setLocusId_null() { |
| 338 | Notification notification = new Notification.Builder(mContext, "whatever") |
| 339 | .setLocusId(null).build(); |
| 340 | assertNull(notification.getLocusId()); |
| 341 | |
| 342 | Notification clone = writeAndReadParcelable(notification); |
| 343 | assertNull(clone.getLocusId()); |
| 344 | } |
| 345 | |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 346 | private Notification.Builder getMediaNotification() { |
| 347 | MediaSession session = new MediaSession(mContext, "test"); |
| 348 | return new Notification.Builder(mContext, "color") |
| 349 | .setSmallIcon(com.android.internal.R.drawable.emergency_icon) |
| 350 | .setContentTitle("Title") |
| 351 | .setContentText("Text") |
| 352 | .setStyle(new Notification.MediaStyle().setMediaSession(session.getSessionToken())); |
| 353 | } |
Robin Lee | ad7e72a | 2017-12-04 15:45:46 +0100 | [diff] [blame] | 354 | |
| 355 | /** |
| 356 | * Writes an arbitrary {@link Parcelable} into a {@link Parcel} using its writeToParcel |
| 357 | * method before reading it out again to check that it was sent properly. |
| 358 | */ |
| 359 | private static <T extends Parcelable> T writeAndReadParcelable(T original) { |
| 360 | Parcel p = Parcel.obtain(); |
| 361 | p.writeParcelable(original, /* flags */ 0); |
| 362 | p.setDataPosition(0); |
| 363 | return p.readParcelable(/* classLoader */ null); |
| 364 | } |
Kodlee Yin | c72c44d | 2017-12-21 22:07:15 +0000 | [diff] [blame] | 365 | |
| 366 | /** |
| 367 | * Creates a Notification.Action by mocking initial dependencies and then applying |
| 368 | * transformations if they're defined. |
| 369 | */ |
| 370 | private Notification.Action makeNotificationAction( |
| 371 | @Nullable Consumer<Notification.Action.Builder> transformation) { |
| 372 | Notification.Action.Builder actionBuilder = |
| 373 | new Notification.Action.Builder(null, "Test Title", null); |
| 374 | if (transformation != null) { |
| 375 | transformation.accept(actionBuilder); |
| 376 | } |
| 377 | return actionBuilder.build(); |
| 378 | } |
Selim Cinek | 389edcd | 2017-05-11 19:16:44 -0700 | [diff] [blame] | 379 | } |