blob: e98662bbcacf64165b123fa1bca8145396cea11b [file] [log] [blame]
Joshua Tsuji614b1df2019-03-26 13:57:05 -04001/*
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 Mellor99a302602019-06-14 11:39:56 -070017package com.android.systemui.bubbles;
Joshua Tsuji614b1df2019-03-26 13:57:05 -040018
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNull;
21import static org.mockito.Mockito.doReturn;
22import static org.mockito.Mockito.when;
23
24import android.app.Notification;
25import android.os.Bundle;
Mady Mellor99a302602019-06-14 11:39:56 -070026import android.os.UserHandle;
Joshua Tsuji614b1df2019-03-26 13:57:05 -040027import android.service.notification.StatusBarNotification;
28import android.testing.AndroidTestingRunner;
29import android.testing.TestableLooper;
30
31import androidx.test.filters.SmallTest;
32
33import com.android.systemui.SysuiTestCase;
Mady Mellor99a302602019-06-14 11:39:56 -070034import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Joshua Tsuji614b1df2019-03-26 13:57:05 -040035
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.MockitoAnnotations;
41
42@SmallTest
43@RunWith(AndroidTestingRunner.class)
44@TestableLooper.RunWithLooper
Mady Mellor99a302602019-06-14 11:39:56 -070045public class BubbleTest extends SysuiTestCase {
Joshua Tsuji614b1df2019-03-26 13:57:05 -040046 @Mock
47 private StatusBarNotification mStatusBarNotification;
48 @Mock
49 private Notification mNotif;
50
51 private NotificationEntry mEntry;
Mady Mellor99a302602019-06-14 11:39:56 -070052 private Bubble mBubble;
Joshua Tsuji614b1df2019-03-26 13:57:05 -040053 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 Mellor99a302602019-06-14 11:39:56 -070061 when(mStatusBarNotification.getUser()).thenReturn(new UserHandle(0));
Joshua Tsuji614b1df2019-03-26 13:57:05 -040062 mExtras = new Bundle();
63 mNotif.extras = mExtras;
Joshua Tsuji614b1df2019-03-26 13:57:05 -040064 mEntry = new NotificationEntry(mStatusBarNotification);
Mady Mellor99a302602019-06-14 11:39:56 -070065
66 mBubble = new Bubble(mContext, mEntry);
Joshua Tsuji614b1df2019-03-26 13:57:05 -040067 }
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 Mellor99a302602019-06-14 11:39:56 -070074 assertEquals(msg, mBubble.getUpdateMessage(mContext));
Joshua Tsuji614b1df2019-03-26 13:57:05 -040075 }
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 Mellor99a302602019-06-14 11:39:56 -070085 assertEquals(msg, mBubble.getUpdateMessage(mContext));
Joshua Tsuji614b1df2019-03-26 13:57:05 -040086 }
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 Mellor99a302602019-06-14 11:39:56 -070093 assertNull(mBubble.getUpdateMessage(mContext));
Joshua Tsuji614b1df2019-03-26 13:57:05 -040094 }
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 Mellor99a302602019-06-14 11:39:56 -0700108 assertEquals("Really? I prefer them that way.", mBubble.getUpdateMessage(mContext));
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400109 }
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 Mellor99a302602019-06-14 11:39:56 -0700123 assertEquals("Mady: Oh, hello!", mBubble.getUpdateMessage(mContext));
Joshua Tsuji614b1df2019-03-26 13:57:05 -0400124 }
125}