Joshua Tsuji | 614b1df | 2019-03-26 13:57:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.systemui.statusbar.notification.collection; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | import static org.mockito.Mockito.doReturn; |
| 22 | import static org.mockito.Mockito.when; |
| 23 | |
| 24 | import android.app.Notification; |
| 25 | import android.os.Bundle; |
| 26 | import android.service.notification.StatusBarNotification; |
| 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper; |
| 29 | |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | |
| 32 | import com.android.systemui.SysuiTestCase; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | import org.mockito.Mock; |
| 38 | import org.mockito.MockitoAnnotations; |
| 39 | |
| 40 | @SmallTest |
| 41 | @RunWith(AndroidTestingRunner.class) |
| 42 | @TestableLooper.RunWithLooper |
| 43 | public class NotificationEntryTest extends SysuiTestCase { |
| 44 | @Mock |
| 45 | private StatusBarNotification mStatusBarNotification; |
| 46 | @Mock |
| 47 | private Notification mNotif; |
| 48 | |
| 49 | private NotificationEntry mEntry; |
| 50 | private Bundle mExtras; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | MockitoAnnotations.initMocks(this); |
| 55 | |
| 56 | when(mStatusBarNotification.getKey()).thenReturn("key"); |
| 57 | when(mStatusBarNotification.getNotification()).thenReturn(mNotif); |
| 58 | |
| 59 | mExtras = new Bundle(); |
| 60 | mNotif.extras = mExtras; |
| 61 | |
| 62 | mEntry = new NotificationEntry(mStatusBarNotification); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testGetUpdateMessage_default() { |
| 67 | final String msg = "Hello there!"; |
| 68 | doReturn(Notification.Style.class).when(mNotif).getNotificationStyle(); |
| 69 | mExtras.putCharSequence(Notification.EXTRA_TEXT, msg); |
| 70 | assertEquals(msg, mEntry.getUpdateMessage(mContext)); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testGetUpdateMessage_bigText() { |
| 75 | final String msg = "A big hello there!"; |
| 76 | doReturn(Notification.BigTextStyle.class).when(mNotif).getNotificationStyle(); |
| 77 | mExtras.putCharSequence(Notification.EXTRA_TEXT, "A small hello there."); |
| 78 | mExtras.putCharSequence(Notification.EXTRA_BIG_TEXT, msg); |
| 79 | |
| 80 | // Should be big text, not the small text. |
| 81 | assertEquals(msg, mEntry.getUpdateMessage(mContext)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testGetUpdateMessage_media() { |
| 86 | doReturn(Notification.MediaStyle.class).when(mNotif).getNotificationStyle(); |
| 87 | |
| 88 | // Media notifs don't get update messages. |
| 89 | assertNull(mEntry.getUpdateMessage(mContext)); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testGetUpdateMessage_inboxStyle() { |
| 94 | doReturn(Notification.InboxStyle.class).when(mNotif).getNotificationStyle(); |
| 95 | mExtras.putCharSequenceArray( |
| 96 | Notification.EXTRA_TEXT_LINES, |
| 97 | new CharSequence[]{ |
| 98 | "How do you feel about tests?", |
| 99 | "They're okay, I guess.", |
| 100 | "I hate when they're flaky.", |
| 101 | "Really? I prefer them that way."}); |
| 102 | |
| 103 | // Should be the last one only. |
| 104 | assertEquals("Really? I prefer them that way.", mEntry.getUpdateMessage(mContext)); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testGetUpdateMessage_messagingStyle() { |
| 109 | doReturn(Notification.MessagingStyle.class).when(mNotif).getNotificationStyle(); |
| 110 | mExtras.putParcelableArray( |
| 111 | Notification.EXTRA_MESSAGES, |
| 112 | new Bundle[]{ |
| 113 | new Notification.MessagingStyle.Message( |
| 114 | "Hello", 0, "Josh").toBundle(), |
| 115 | new Notification.MessagingStyle.Message( |
| 116 | "Oh, hello!", 0, "Mady").toBundle()}); |
| 117 | |
| 118 | // Should be the last one only. |
| 119 | assertEquals("Mady: Oh, hello!", mEntry.getUpdateMessage(mContext)); |
| 120 | } |
| 121 | } |