blob: 185723ffa09bf9986b1d88c0a7d32dafbc599439 [file] [log] [blame]
Kenny Guya0f6de82018-04-06 16:20:16 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.statusbar;
16
Kenny Guy8cc15d22018-05-09 09:50:55 +010017import static junit.framework.Assert.assertFalse;
18import static junit.framework.Assert.assertTrue;
Tony Mak29996702018-11-26 16:23:34 +000019
Kenny Guya0f6de82018-04-06 16:20:16 +010020import static org.mockito.ArgumentMatchers.argThat;
Kenny Guya0f6de82018-04-06 16:20:16 +010021import static org.mockito.ArgumentMatchers.isNull;
Gus Prevas772e5322018-12-21 16:22:16 -050022import static org.mockito.Mockito.mock;
Kenny Guya0f6de82018-04-06 16:20:16 +010023import static org.mockito.Mockito.verify;
Kenny Guya0f6de82018-04-06 16:20:16 +010024
Kevina5ff1fa2018-08-21 16:35:48 -070025import android.app.ActivityManager;
Kenny Guya0f6de82018-04-06 16:20:16 +010026import android.app.Notification;
Gus Prevas772e5322018-12-21 16:22:16 -050027import android.os.Handler;
28import android.os.Looper;
Kenny Guya0f6de82018-04-06 16:20:16 +010029import android.os.RemoteException;
Kevina5ff1fa2018-08-21 16:35:48 -070030import android.os.UserHandle;
Kenny Guya0f6de82018-04-06 16:20:16 +010031import android.service.notification.StatusBarNotification;
Kenny Guya0f6de82018-04-06 16:20:16 +010032import android.testing.AndroidTestingRunner;
33import android.testing.TestableLooper;
34
Brett Chabot84151d92019-02-27 15:37:59 -080035import androidx.test.filters.SmallTest;
36
Gustav Sennton13edb492019-01-28 21:40:04 +000037import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Kenny Guya0f6de82018-04-06 16:20:16 +010038import com.android.internal.statusbar.IStatusBarService;
39import com.android.systemui.R;
40import com.android.systemui.SysuiTestCase;
Selim Cinekd17b3502019-07-02 20:38:32 -070041import com.android.systemui.plugins.statusbar.StatusBarStateController;
Rohan Shah20790b82018-07-02 17:21:04 -070042import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050043import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Gus Prevas772e5322018-12-21 16:22:16 -050044import com.android.systemui.statusbar.phone.ShadeController;
Kenny Guya0f6de82018-04-06 16:20:16 +010045
46import org.junit.Before;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49import org.mockito.Mock;
50import org.mockito.MockitoAnnotations;
51
52@RunWith(AndroidTestingRunner.class)
53@TestableLooper.RunWithLooper
54@SmallTest
55public class SmartReplyControllerTest extends SysuiTestCase {
Kevina5ff1fa2018-08-21 16:35:48 -070056 private static final String TEST_PACKAGE_NAME = "test";
57 private static final int TEST_UID = 0;
Kenny Guya0f6de82018-04-06 16:20:16 +010058 private static final String TEST_CHOICE_TEXT = "A Reply";
59 private static final int TEST_CHOICE_INDEX = 2;
60 private static final int TEST_CHOICE_COUNT = 4;
Gustav Senntond25a64d2018-12-07 10:58:39 +000061 private static final int TEST_ACTION_COUNT = 3;
Kenny Guya0f6de82018-04-06 16:20:16 +010062
63 private Notification mNotification;
Ned Burnsf81c4c42019-01-07 14:10:43 -050064 private NotificationEntry mEntry;
Kevina5ff1fa2018-08-21 16:35:48 -070065 private SmartReplyController mSmartReplyController;
66 private NotificationRemoteInputManager mRemoteInputManager;
Kenny Guya0f6de82018-04-06 16:20:16 +010067
Kevina5ff1fa2018-08-21 16:35:48 -070068 @Mock private NotificationPresenter mPresenter;
69 @Mock private RemoteInputController.Delegate mDelegate;
70 @Mock private NotificationRemoteInputManager.Callback mCallback;
71 @Mock private StatusBarNotification mSbn;
72 @Mock private NotificationEntryManager mNotificationEntryManager;
73 @Mock private IStatusBarService mIStatusBarService;
Selim Cinekd17b3502019-07-02 20:38:32 -070074 @Mock private StatusBarStateController mStatusBarStateController;
Kenny Guya0f6de82018-04-06 16:20:16 +010075
76 @Before
77 public void setUp() {
78 MockitoAnnotations.initMocks(this);
Kenny Guya0f6de82018-04-06 16:20:16 +010079 mDependency.injectTestDependency(NotificationEntryManager.class,
80 mNotificationEntryManager);
Kenny Guya0f6de82018-04-06 16:20:16 +010081
Jason Monk752c68f2018-12-21 12:07:55 -050082 mSmartReplyController = new SmartReplyController(mNotificationEntryManager,
83 mIStatusBarService);
Kevina5ff1fa2018-08-21 16:35:48 -070084 mDependency.injectTestDependency(SmartReplyController.class,
85 mSmartReplyController);
86
Gus Prevas772e5322018-12-21 16:22:16 -050087 mRemoteInputManager = new NotificationRemoteInputManager(mContext,
88 mock(NotificationLockscreenUserManager.class), mSmartReplyController,
89 mNotificationEntryManager, () -> mock(ShadeController.class),
Selim Cinekd17b3502019-07-02 20:38:32 -070090 mStatusBarStateController,
Gus Prevas772e5322018-12-21 16:22:16 -050091 Handler.createAsync(Looper.myLooper()));
Gus Prevas21437b32018-12-05 10:36:13 -050092 mRemoteInputManager.setUpWithCallback(mCallback, mDelegate);
Kenny Guya0f6de82018-04-06 16:20:16 +010093 mNotification = new Notification.Builder(mContext, "")
94 .setSmallIcon(R.drawable.ic_person)
95 .setContentTitle("Title")
96 .setContentText("Text").build();
Kevina5ff1fa2018-08-21 16:35:48 -070097
98 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID,
99 0, mNotification, new UserHandle(ActivityManager.getCurrentUser()), null, 0);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500100 mEntry = new NotificationEntry(mSbn);
Kenny Guya0f6de82018-04-06 16:20:16 +0100101 }
102
103 @Test
Kenny Guy8cc15d22018-05-09 09:50:55 +0100104 public void testSendSmartReply_updatesRemoteInput() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000105 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
106 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guya0f6de82018-04-06 16:20:16 +0100107
108 // Sending smart reply should make calls to NotificationEntryManager
109 // to update the notification with reply and spinner.
Kenny Guya0f6de82018-04-06 16:20:16 +0100110 verify(mNotificationEntryManager).updateNotification(
Kevina5ff1fa2018-08-21 16:35:48 -0700111 argThat(sbn -> sbn.getKey().equals(mSbn.getKey())), isNull());
Kenny Guya0f6de82018-04-06 16:20:16 +0100112 }
113
114 @Test
115 public void testSendSmartReply_logsToStatusBar() throws RemoteException {
Milo Sredkov13d88112019-02-01 12:23:24 +0000116 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
117 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guya0f6de82018-04-06 16:20:16 +0100118
119 // Check we log the result to the status bar service.
Kevina5ff1fa2018-08-21 16:35:48 -0700120 verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000121 TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, false);
Tony Mak29996702018-11-26 16:23:34 +0000122 }
123
124
125 @Test
Milo Sredkov13d88112019-02-01 12:23:24 +0000126 public void testSendSmartReply_logsToStatusBar_modifiedBeforeSending() throws RemoteException {
127 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
128 MetricsEvent.LOCATION_UNKNOWN, true /* modifiedBeforeSending */);
Tony Mak29996702018-11-26 16:23:34 +0000129
130 // Check we log the result to the status bar service.
131 verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000132 TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, true);
Kenny Guya0f6de82018-04-06 16:20:16 +0100133 }
134
135 @Test
Gustav Senntond25a64d2018-12-07 10:58:39 +0000136 public void testShowSmartSuggestions_logsToStatusBar() throws RemoteException {
137 final boolean generatedByAsssistant = true;
Milo Sredkov13d88112019-02-01 12:23:24 +0000138 final boolean editBeforeSending = true;
Gustav Senntond25a64d2018-12-07 10:58:39 +0000139 mSmartReplyController.smartSuggestionsAdded(mEntry, TEST_CHOICE_COUNT, TEST_ACTION_COUNT,
Milo Sredkov13d88112019-02-01 12:23:24 +0000140 generatedByAsssistant, editBeforeSending);
Kenny Guya0f6de82018-04-06 16:20:16 +0100141
142 // Check we log the result to the status bar service.
Gustav Senntond25a64d2018-12-07 10:58:39 +0000143 verify(mIStatusBarService).onNotificationSmartSuggestionsAdded(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000144 TEST_CHOICE_COUNT, TEST_ACTION_COUNT, generatedByAsssistant, editBeforeSending);
Kenny Guya0f6de82018-04-06 16:20:16 +0100145 }
Kenny Guy8cc15d22018-05-09 09:50:55 +0100146
147 @Test
148 public void testSendSmartReply_reportsSending() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000149 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
150 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guy8cc15d22018-05-09 09:50:55 +0100151
Kevina5ff1fa2018-08-21 16:35:48 -0700152 assertTrue(mSmartReplyController.isSendingSmartReply(mSbn.getKey()));
Kenny Guy8cc15d22018-05-09 09:50:55 +0100153 }
154
155 @Test
156 public void testSendingSmartReply_afterRemove_shouldReturnFalse() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000157 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
158 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kevina5ff1fa2018-08-21 16:35:48 -0700159 mSmartReplyController.stopSending(mEntry);
Kenny Guy8cc15d22018-05-09 09:50:55 +0100160
Kevina5ff1fa2018-08-21 16:35:48 -0700161 assertFalse(mSmartReplyController.isSendingSmartReply(mSbn.getKey()));
Kenny Guy8cc15d22018-05-09 09:50:55 +0100162 }
Kenny Guya0f6de82018-04-06 16:20:16 +0100163}