SmartReplyControllerTest.java revision a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19
1a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy/*
2a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy * Copyright (C) 2018 The Android Open Source Project
3a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy *
4a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy * except in compliance with the License. You may obtain a copy of the License at
6a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy *
7a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy *      http://www.apache.org/licenses/LICENSE-2.0
8a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy *
9a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy * Unless required by applicable law or agreed to in writing, software distributed under the
10a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy * KIND, either express or implied. See the License for the specific language governing
12a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy * permissions and limitations under the License.
13a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy */
14a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
15a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guypackage com.android.systemui.statusbar;
16a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
17a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport static org.mockito.ArgumentMatchers.argThat;
18a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport static org.mockito.ArgumentMatchers.eq;
19a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport static org.mockito.ArgumentMatchers.isNull;
20a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport static org.mockito.Mockito.mock;
21a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport static org.mockito.Mockito.verify;
22a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport static org.mockito.Mockito.when;
23a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
24a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport android.app.Notification;
25a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport android.os.RemoteException;
26a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport android.service.notification.StatusBarNotification;
27a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport android.support.test.filters.SmallTest;
28a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport android.testing.AndroidTestingRunner;
29a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport android.testing.TestableLooper;
30a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
31a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport com.android.internal.statusbar.IStatusBarService;
32a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport com.android.systemui.R;
33a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport com.android.systemui.SysuiTestCase;
34a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
35a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport org.junit.Before;
36a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport org.junit.Test;
37a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport org.junit.runner.RunWith;
38a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport org.mockito.Mock;
39a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guyimport org.mockito.MockitoAnnotations;
40a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
41a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy@RunWith(AndroidTestingRunner.class)
42a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy@TestableLooper.RunWithLooper
43a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy@SmallTest
44a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guypublic class SmartReplyControllerTest extends SysuiTestCase {
45a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private static final String TEST_NOTIFICATION_KEY = "akey";
46a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private static final String TEST_CHOICE_TEXT = "A Reply";
47a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private static final int TEST_CHOICE_INDEX = 2;
48a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private static final int TEST_CHOICE_COUNT = 4;
49a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
50a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private Notification mNotification;
51a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private NotificationData.Entry mEntry;
52a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
53a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    @Mock
54a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private NotificationEntryManager mNotificationEntryManager;
55a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    @Mock
56a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    private IStatusBarService mIStatusBarService;
57a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
58a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    @Before
59a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    public void setUp() {
60a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        MockitoAnnotations.initMocks(this);
61a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
62a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        mDependency.injectTestDependency(NotificationEntryManager.class,
63a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                mNotificationEntryManager);
64a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        mDependency.injectTestDependency(IStatusBarService.class, mIStatusBarService);
65a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
66a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        mNotification = new Notification.Builder(mContext, "")
67a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                .setSmallIcon(R.drawable.ic_person)
68a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                .setContentTitle("Title")
69a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                .setContentText("Text").build();
70a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        StatusBarNotification sbn = mock(StatusBarNotification.class);
71a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        when(sbn.getNotification()).thenReturn(mNotification);
72a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        when(sbn.getKey()).thenReturn(TEST_NOTIFICATION_KEY);
73a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        mEntry = new NotificationData.Entry(sbn);
74a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    }
75a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
76a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    @Test
77a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    public void testSendSmartReply_updatesRemoteInput() throws RemoteException {
78a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        StatusBarNotification sbn = mock(StatusBarNotification.class);
79a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        when(sbn.getKey()).thenReturn(TEST_NOTIFICATION_KEY);
80a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        when(mNotificationEntryManager.rebuildNotificationWithRemoteInput(
81a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                argThat(entry -> entry.notification.getKey().equals(TEST_NOTIFICATION_KEY)),
82a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                eq(TEST_CHOICE_TEXT), eq(true))).thenReturn(sbn);
83a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
84a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        SmartReplyController controller = new SmartReplyController();
85a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        controller.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT);
86a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
87a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        // Sending smart reply should make calls to NotificationEntryManager
88a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        // to update the notification with reply and spinner.
89a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        verify(mNotificationEntryManager).rebuildNotificationWithRemoteInput(
90a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                argThat(entry -> entry.notification.getKey().equals(TEST_NOTIFICATION_KEY)),
91a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                eq(TEST_CHOICE_TEXT), eq(true));
92a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        verify(mNotificationEntryManager).updateNotification(
93a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                argThat(sbn2 -> sbn2.getKey().equals(TEST_NOTIFICATION_KEY)), isNull());
94a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    }
95a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
96a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    @Test
97a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    public void testSendSmartReply_logsToStatusBar() throws RemoteException {
98a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        StatusBarNotification sbn = mock(StatusBarNotification.class);
99a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        when(sbn.getKey()).thenReturn(TEST_NOTIFICATION_KEY);
100a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        when(mNotificationEntryManager.rebuildNotificationWithRemoteInput(
101a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                argThat(entry -> entry.notification.getKey().equals(TEST_NOTIFICATION_KEY)),
102a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                eq(TEST_CHOICE_TEXT), eq(true))).thenReturn(sbn);
103a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
104a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        SmartReplyController controller = new SmartReplyController();
105a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        controller.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT);
106a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
107a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        // Check we log the result to the status bar service.
108a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        verify(mIStatusBarService).onNotificationSmartReplySent(TEST_NOTIFICATION_KEY,
109a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                TEST_CHOICE_INDEX);
110a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    }
111a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
112a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    @Test
113a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    public void testShowSmartReply_logsToStatusBar() throws RemoteException {
114a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        SmartReplyController controller = new SmartReplyController();
115a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        controller.smartRepliesAdded(mEntry, TEST_CHOICE_COUNT);
116a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy
117a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        // Check we log the result to the status bar service.
118a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy        verify(mIStatusBarService).onNotificationSmartRepliesAdded(TEST_NOTIFICATION_KEY,
119a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy                TEST_CHOICE_COUNT);
120a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy    }
121a0f6de8ab2d78445fe0259ef0f5f00e25a3f7d19Kenny Guy}
122