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