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 
17 package com.android.systemui.statusbar;
18 
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertFalse;
21 import static junit.framework.Assert.assertTrue;
22 
23 import static org.mockito.Mockito.mock;
24 
25 import android.app.Notification;
26 import android.app.RemoteInputHistoryItem;
27 import android.net.Uri;
28 import android.os.UserHandle;
29 import android.service.notification.StatusBarNotification;
30 import android.testing.AndroidTestingRunner;
31 import android.testing.TestableLooper;
32 
33 import androidx.test.filters.SmallTest;
34 
35 import com.android.systemui.SysuiTestCase;
36 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
37 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
38 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 
46 @SmallTest
47 @RunWith(AndroidTestingRunner.class)
48 @TestableLooper.RunWithLooper
49 public class RemoteInputNotificationRebuilderTest extends SysuiTestCase {
50     private static final String TEST_PACKAGE_NAME = "test";
51     private static final int TEST_UID = 0;
52     @Mock
53     private ExpandableNotificationRow mRow;
54 
55     private RemoteInputNotificationRebuilder mRebuilder;
56     private NotificationEntry mEntry;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61 
62         mRebuilder = new RemoteInputNotificationRebuilder(mContext);
63         mEntry = new NotificationEntryBuilder()
64                 .setPkg(TEST_PACKAGE_NAME)
65                 .setOpPkg(TEST_PACKAGE_NAME)
66                 .setUid(TEST_UID)
67                 .setNotification(new Notification())
68                 .setUser(UserHandle.CURRENT)
69                 .build();
70         mEntry.setRow(mRow);
71     }
72 
73     @Test
testRebuildWithRemoteInput_noExistingInput_image()74     public void testRebuildWithRemoteInput_noExistingInput_image() {
75         Uri uri = mock(Uri.class);
76         String mimeType = "image/jpeg";
77         String text = "image inserted";
78         StatusBarNotification newSbn =
79                 mRebuilder.rebuildWithRemoteInputInserted(
80                         mEntry, text, false, mimeType, uri);
81         RemoteInputHistoryItem[] messages = (RemoteInputHistoryItem[]) newSbn.getNotification()
82                 .extras.getParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS);
83         assertEquals(1, messages.length);
84         assertEquals(text, messages[0].getText());
85         assertEquals(mimeType, messages[0].getMimeType());
86         assertEquals(uri, messages[0].getUri());
87     }
88 
89     @Test
testRebuildWithRemoteInput_noExistingInputNoSpinner()90     public void testRebuildWithRemoteInput_noExistingInputNoSpinner() {
91         StatusBarNotification newSbn =
92                 mRebuilder.rebuildWithRemoteInputInserted(
93                         mEntry, "A Reply", false, null, null);
94         RemoteInputHistoryItem[] messages = (RemoteInputHistoryItem[]) newSbn.getNotification()
95                 .extras.getParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS);
96         assertEquals(1, messages.length);
97         assertEquals("A Reply", messages[0].getText());
98         assertFalse(newSbn.getNotification().extras
99                 .getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false));
100         assertTrue(newSbn.getNotification().extras
101                 .getBoolean(Notification.EXTRA_HIDE_SMART_REPLIES, false));
102     }
103 
104     @Test
testRebuildWithRemoteInput_noExistingInputWithSpinner()105     public void testRebuildWithRemoteInput_noExistingInputWithSpinner() {
106         StatusBarNotification newSbn =
107                 mRebuilder.rebuildWithRemoteInputInserted(
108                         mEntry, "A Reply", true, null, null);
109         RemoteInputHistoryItem[] messages = (RemoteInputHistoryItem[]) newSbn.getNotification()
110                 .extras.getParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS);
111         assertEquals(1, messages.length);
112         assertEquals("A Reply", messages[0].getText());
113         assertTrue(newSbn.getNotification().extras
114                 .getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false));
115         assertTrue(newSbn.getNotification().extras
116                 .getBoolean(Notification.EXTRA_HIDE_SMART_REPLIES, false));
117     }
118 
119     @Test
testRebuildWithRemoteInput_withExistingInput()120     public void testRebuildWithRemoteInput_withExistingInput() {
121         // Setup a notification entry with 1 remote input.
122         StatusBarNotification newSbn =
123                 mRebuilder.rebuildWithRemoteInputInserted(
124                         mEntry, "A Reply", false, null, null);
125         NotificationEntry entry = new NotificationEntryBuilder()
126                 .setSbn(newSbn)
127                 .build();
128 
129         // Try rebuilding to add another reply.
130         newSbn = mRebuilder.rebuildWithRemoteInputInserted(
131                 entry, "Reply 2", true, null, null);
132         RemoteInputHistoryItem[] messages = (RemoteInputHistoryItem[]) newSbn.getNotification()
133                 .extras.getParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS);
134         assertEquals(2, messages.length);
135         assertEquals("Reply 2", messages[0].getText());
136         assertEquals("A Reply", messages[1].getText());
137     }
138 
139     @Test
testRebuildWithRemoteInput_withExistingInput_image()140     public void testRebuildWithRemoteInput_withExistingInput_image() {
141         // Setup a notification entry with 1 remote input.
142         Uri uri = mock(Uri.class);
143         String mimeType = "image/jpeg";
144         String text = "image inserted";
145         StatusBarNotification newSbn =
146                 mRebuilder.rebuildWithRemoteInputInserted(
147                         mEntry, text, false, mimeType, uri);
148         NotificationEntry entry = new NotificationEntryBuilder()
149                 .setSbn(newSbn)
150                 .build();
151 
152         // Try rebuilding to add another reply.
153         newSbn = mRebuilder.rebuildWithRemoteInputInserted(
154                 entry, "Reply 2", true, null, null);
155         RemoteInputHistoryItem[] messages = (RemoteInputHistoryItem[]) newSbn.getNotification()
156                 .extras.getParcelableArray(Notification.EXTRA_REMOTE_INPUT_HISTORY_ITEMS);
157         assertEquals(2, messages.length);
158         assertEquals("Reply 2", messages[0].getText());
159         assertEquals(text, messages[1].getText());
160         assertEquals(mimeType, messages[1].getMimeType());
161         assertEquals(uri, messages[1].getUri());
162     }
163 
164     @Test
testRebuildNotificationForCanceledSmartReplies()165     public void testRebuildNotificationForCanceledSmartReplies() {
166         // Try rebuilding to remove spinner and hide buttons.
167         StatusBarNotification newSbn =
168                 mRebuilder.rebuildForCanceledSmartReplies(mEntry);
169         assertFalse(newSbn.getNotification().extras
170                 .getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false));
171         assertTrue(newSbn.getNotification().extras
172                 .getBoolean(Notification.EXTRA_HIDE_SMART_REPLIES, false));
173     }
174 }
175