1 /*
2  * Copyright (C) 2021 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 package com.android.systemui.people;
17 
18 import static android.app.Notification.CATEGORY_MISSED_CALL;
19 
20 import static com.android.systemui.people.NotificationHelper.getHighestPriorityNotification;
21 import static com.android.systemui.people.NotificationHelper.getMessagingStyleMessages;
22 import static com.android.systemui.people.NotificationHelper.getSenderIfGroupConversation;
23 import static com.android.systemui.people.NotificationHelper.isMissedCall;
24 import static com.android.systemui.people.NotificationHelper.isMissedCallOrHasContent;
25 import static com.android.systemui.people.PeopleSpaceUtils.PACKAGE_NAME;
26 
27 import static com.google.common.truth.Truth.assertThat;
28 
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertTrue;
31 
32 import android.app.Notification;
33 import android.app.Person;
34 import android.content.pm.ShortcutInfo;
35 import android.net.Uri;
36 import android.os.Bundle;
37 import android.os.UserHandle;
38 import android.service.notification.StatusBarNotification;
39 import android.testing.AndroidTestingRunner;
40 
41 import androidx.test.filters.SmallTest;
42 
43 import com.android.internal.util.ArrayUtils;
44 import com.android.systemui.SysuiTestCase;
45 import com.android.systemui.statusbar.SbnBuilder;
46 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
47 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
48 
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 
52 import java.util.List;
53 import java.util.Set;
54 
55 @RunWith(AndroidTestingRunner.class)
56 @SmallTest
57 public class NotificationHelperTest extends SysuiTestCase {
58     private static final String SHORTCUT_ID_1 = "101";
59     private static final String SHORTCUT_ID_2 = "102";
60 
61     private static final String NOTIFICATION_TEXT_1 = "notification_text_1";
62     private static final String NOTIFICATION_TEXT_2 = "notification_text_2";
63     private static final String NOTIFICATION_TEXT_3 = "notification_text_3";
64     private static final Uri URI = Uri.parse("fake_uri");
65     private static final Person PERSON = new Person.Builder()
66             .setName("name")
67             .setKey("abc")
68             .setUri(URI.toString())
69             .setBot(false)
70             .build();
71 
72     private final Notification mNotification1 = new Notification.Builder(mContext, "test")
73             .setContentTitle("TEST_TITLE")
74             .setContentText("TEST_TEXT")
75             .setShortcutId(SHORTCUT_ID_1)
76             .setStyle(new Notification.MessagingStyle(PERSON)
77                     .addMessage(new Notification.MessagingStyle.Message(
78                             NOTIFICATION_TEXT_1, 0, PERSON))
79                     .addMessage(new Notification.MessagingStyle.Message(
80                             NOTIFICATION_TEXT_2, 20, PERSON))
81                     .addMessage(new Notification.MessagingStyle.Message(
82                             NOTIFICATION_TEXT_3, 10, PERSON))
83             )
84             .build();
85 
86     private final Notification mNotification2 = new Notification.Builder(mContext, "test")
87             .setContentTitle("TEST_TITLE")
88             .setContentText("TEST_TEXT")
89             .setShortcutId(SHORTCUT_ID_1)
90             .setStyle(new Notification.MessagingStyle(PERSON)
91                     .addMessage(new Notification.MessagingStyle.Message(
92                             NOTIFICATION_TEXT_1, 0, PERSON))
93             )
94             .build();
95 
96     private final Notification mNoContentNotification = new Notification.Builder(mContext, "test")
97             .setContentTitle("TEST_TITLE")
98             .setContentText("TEST_TEXT")
99             .setShortcutId(SHORTCUT_ID_1)
100             .setStyle(new Notification.MessagingStyle(PERSON))
101             .build();
102 
103     private final Notification mMissedCallNotification = new Notification.Builder(mContext, "test")
104             .setContentTitle("TEST_TITLE")
105             .setContentText("TEST_TEXT")
106             .setShortcutId(SHORTCUT_ID_2)
107             .setCategory(CATEGORY_MISSED_CALL)
108             .setStyle(new Notification.MessagingStyle(PERSON))
109             .build();
110 
111     private final NotificationEntry mNotificationEntry1 = new NotificationEntryBuilder()
112             .setNotification(mNotification1)
113             .setShortcutInfo(new ShortcutInfo.Builder(mContext, SHORTCUT_ID_1).build())
114             .setUser(UserHandle.of(0))
115             .setPkg(PACKAGE_NAME)
116             .build();
117 
118     private final NotificationEntry mNotificationEntry2 = new NotificationEntryBuilder()
119             .setNotification(mNotification2)
120             .setShortcutInfo(new ShortcutInfo.Builder(mContext, SHORTCUT_ID_1).build())
121             .setUser(UserHandle.of(0))
122             .setPkg(PACKAGE_NAME)
123             .build();
124 
125 
126     private final NotificationEntry mMissedCallNotificationEntry = new NotificationEntryBuilder()
127             .setNotification(mMissedCallNotification)
128             .setShortcutInfo(new ShortcutInfo.Builder(mContext, SHORTCUT_ID_1).build())
129             .setUser(UserHandle.of(0))
130             .setPkg(PACKAGE_NAME)
131             .build();
132 
133     private final NotificationEntry mNoContentNotificationEntry = new NotificationEntryBuilder()
134             .setNotification(mNoContentNotification)
135             .setShortcutInfo(new ShortcutInfo.Builder(mContext, SHORTCUT_ID_1).build())
136             .setUser(UserHandle.of(0))
137             .setPkg(PACKAGE_NAME)
138             .build();
139 
140     @Test
testGetMessagingStyleMessagesNoMessage()141     public void testGetMessagingStyleMessagesNoMessage() {
142         Notification notification = new Notification.Builder(mContext, "test")
143                 .setContentTitle("TEST_TITLE")
144                 .setContentText("TEST_TEXT")
145                 .setShortcutId(SHORTCUT_ID_1)
146                 .build();
147         StatusBarNotification sbn = new SbnBuilder()
148                 .setNotification(notification)
149                 .build();
150 
151         List<Notification.MessagingStyle.Message> messages =
152                 getMessagingStyleMessages(sbn.getNotification());
153 
154         assertThat(ArrayUtils.isEmpty(messages)).isTrue();
155     }
156 
157     @Test
testGetMessagingStyleMessages()158     public void testGetMessagingStyleMessages() {
159         StatusBarNotification sbn = new SbnBuilder()
160                 .setNotification(mNotification1)
161                 .build();
162 
163         List<Notification.MessagingStyle.Message> messages =
164                 getMessagingStyleMessages(sbn.getNotification());
165 
166         assertThat(messages.size()).isEqualTo(3);
167         assertThat(messages.get(0).getText().toString()).isEqualTo(NOTIFICATION_TEXT_2);
168     }
169 
170     @Test
testIsMissedCall_notMissedCall()171     public void testIsMissedCall_notMissedCall() {
172         assertFalse(isMissedCall(mNotificationEntry1));
173     }
174 
175     @Test
testIsMissedCall_missedCall()176     public void testIsMissedCall_missedCall() {
177         assertTrue(isMissedCall(mMissedCallNotificationEntry));
178     }
179 
180     @Test
testisMissedCallOrHasContent_NoContent()181     public void testisMissedCallOrHasContent_NoContent() {
182         assertFalse(isMissedCallOrHasContent(mNoContentNotificationEntry));
183     }
184 
185     @Test
testisMissedCallOrHasContent_Hasontent()186     public void testisMissedCallOrHasContent_Hasontent() {
187         assertTrue(isMissedCallOrHasContent(mNotificationEntry1));
188     }
189 
190     @Test
testGetHighestPriorityNotification_missedCallHigherPriority()191     public void testGetHighestPriorityNotification_missedCallHigherPriority() {
192         Set<NotificationEntry> notifications = Set.of(
193                 mNotificationEntry1, mMissedCallNotificationEntry);
194 
195         assertThat(getHighestPriorityNotification(notifications))
196                 .isEqualTo(mMissedCallNotificationEntry);
197     }
198 
199     @Test
testGetHighestPriorityNotification_moreRecentLastMessage()200     public void testGetHighestPriorityNotification_moreRecentLastMessage() {
201         Set<NotificationEntry> notifications = Set.of(
202                 mNotificationEntry1, mNotificationEntry2);
203 
204         assertThat(getHighestPriorityNotification(notifications))
205                 .isEqualTo(mNotificationEntry1);
206     }
207 
208     @Test
testGetSenderIfGroupConversation_notGroup()209     public void testGetSenderIfGroupConversation_notGroup() {
210         Notification.MessagingStyle.Message message = new Notification.MessagingStyle.Message(
211                 NOTIFICATION_TEXT_3, 10, PERSON);
212         Notification notification = new Notification.Builder(mContext, "test")
213                 .setContentTitle("TEST_TITLE")
214                 .setContentText("TEST_TEXT")
215                 .setShortcutId(SHORTCUT_ID_1)
216                 .setStyle(new Notification.MessagingStyle(PERSON).addMessage(message))
217                 .build();
218         assertThat(getSenderIfGroupConversation(notification, message)).isNull();
219     }
220 
221     @Test
testGetSenderIfGroupConversation_group()222     public void testGetSenderIfGroupConversation_group() {
223         Bundle extras = new Bundle();
224         extras.putBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION, true);
225         Notification.MessagingStyle.Message message = new Notification.MessagingStyle.Message(
226                 NOTIFICATION_TEXT_3, 10, PERSON);
227 
228         Notification notification = new Notification.Builder(mContext, "test")
229                 .setContentTitle("TEST_TITLE")
230                 .setContentText("TEST_TEXT")
231                 .setShortcutId(SHORTCUT_ID_1)
232                 .setStyle(new Notification.MessagingStyle(PERSON)
233                         .setGroupConversation(true)
234                         .addMessage(message))
235                 .addExtras(extras)
236                 .build();
237         assertThat(getSenderIfGroupConversation(notification, message)).isEqualTo("name");
238     }
239 
240     @Test
testGetSenderIfGroupConversation_groupNoName()241     public void testGetSenderIfGroupConversation_groupNoName() {
242         Bundle extras = new Bundle();
243         extras.putBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION, true);
244         Notification.MessagingStyle.Message message = new Notification.MessagingStyle.Message(
245                 NOTIFICATION_TEXT_3, 10, new Person.Builder().build());
246 
247         Notification notification = new Notification.Builder(mContext, "test")
248                 .setContentTitle("TEST_TITLE")
249                 .setContentText("TEST_TEXT")
250                 .setShortcutId(SHORTCUT_ID_1)
251                 .setStyle(new Notification.MessagingStyle(PERSON).addMessage(message))
252                 .setExtras(extras)
253                 .build();
254         assertThat(getSenderIfGroupConversation(notification, message)).isNull();
255     }
256 }
257