blob: 81e373a8be2721652fbaf2a5d827516b7136c393 [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;
Rohan Shah20790b82018-07-02 17:21:04 -070041import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050042import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Gus Prevas772e5322018-12-21 16:22:16 -050043import com.android.systemui.statusbar.phone.ShadeController;
Kenny Guya0f6de82018-04-06 16:20:16 +010044
45import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48import org.mockito.Mock;
49import org.mockito.MockitoAnnotations;
50
51@RunWith(AndroidTestingRunner.class)
52@TestableLooper.RunWithLooper
53@SmallTest
54public class SmartReplyControllerTest extends SysuiTestCase {
Kevina5ff1fa2018-08-21 16:35:48 -070055 private static final String TEST_PACKAGE_NAME = "test";
56 private static final int TEST_UID = 0;
Kenny Guya0f6de82018-04-06 16:20:16 +010057 private static final String TEST_CHOICE_TEXT = "A Reply";
58 private static final int TEST_CHOICE_INDEX = 2;
59 private static final int TEST_CHOICE_COUNT = 4;
Gustav Senntond25a64d2018-12-07 10:58:39 +000060 private static final int TEST_ACTION_COUNT = 3;
Kenny Guya0f6de82018-04-06 16:20:16 +010061
62 private Notification mNotification;
Ned Burnsf81c4c42019-01-07 14:10:43 -050063 private NotificationEntry mEntry;
Kevina5ff1fa2018-08-21 16:35:48 -070064 private SmartReplyController mSmartReplyController;
65 private NotificationRemoteInputManager mRemoteInputManager;
Kenny Guya0f6de82018-04-06 16:20:16 +010066
Kevina5ff1fa2018-08-21 16:35:48 -070067 @Mock private NotificationPresenter mPresenter;
68 @Mock private RemoteInputController.Delegate mDelegate;
69 @Mock private NotificationRemoteInputManager.Callback mCallback;
70 @Mock private StatusBarNotification mSbn;
71 @Mock private NotificationEntryManager mNotificationEntryManager;
72 @Mock private IStatusBarService mIStatusBarService;
Kenny Guya0f6de82018-04-06 16:20:16 +010073
74 @Before
75 public void setUp() {
76 MockitoAnnotations.initMocks(this);
Kenny Guya0f6de82018-04-06 16:20:16 +010077 mDependency.injectTestDependency(NotificationEntryManager.class,
78 mNotificationEntryManager);
Kenny Guya0f6de82018-04-06 16:20:16 +010079
Jason Monk752c68f2018-12-21 12:07:55 -050080 mSmartReplyController = new SmartReplyController(mNotificationEntryManager,
81 mIStatusBarService);
Kevina5ff1fa2018-08-21 16:35:48 -070082 mDependency.injectTestDependency(SmartReplyController.class,
83 mSmartReplyController);
84
Gus Prevas772e5322018-12-21 16:22:16 -050085 mRemoteInputManager = new NotificationRemoteInputManager(mContext,
86 mock(NotificationLockscreenUserManager.class), mSmartReplyController,
87 mNotificationEntryManager, () -> mock(ShadeController.class),
88 Handler.createAsync(Looper.myLooper()));
Gus Prevas21437b32018-12-05 10:36:13 -050089 mRemoteInputManager.setUpWithCallback(mCallback, mDelegate);
Kenny Guya0f6de82018-04-06 16:20:16 +010090 mNotification = new Notification.Builder(mContext, "")
91 .setSmallIcon(R.drawable.ic_person)
92 .setContentTitle("Title")
93 .setContentText("Text").build();
Kevina5ff1fa2018-08-21 16:35:48 -070094
95 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID,
96 0, mNotification, new UserHandle(ActivityManager.getCurrentUser()), null, 0);
Ned Burnsf81c4c42019-01-07 14:10:43 -050097 mEntry = new NotificationEntry(mSbn);
Kenny Guya0f6de82018-04-06 16:20:16 +010098 }
99
100 @Test
Kenny Guy8cc15d22018-05-09 09:50:55 +0100101 public void testSendSmartReply_updatesRemoteInput() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000102 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
103 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guya0f6de82018-04-06 16:20:16 +0100104
105 // Sending smart reply should make calls to NotificationEntryManager
106 // to update the notification with reply and spinner.
Kenny Guya0f6de82018-04-06 16:20:16 +0100107 verify(mNotificationEntryManager).updateNotification(
Kevina5ff1fa2018-08-21 16:35:48 -0700108 argThat(sbn -> sbn.getKey().equals(mSbn.getKey())), isNull());
Kenny Guya0f6de82018-04-06 16:20:16 +0100109 }
110
111 @Test
112 public void testSendSmartReply_logsToStatusBar() throws RemoteException {
Milo Sredkov13d88112019-02-01 12:23:24 +0000113 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
114 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guya0f6de82018-04-06 16:20:16 +0100115
116 // Check we log the result to the status bar service.
Kevina5ff1fa2018-08-21 16:35:48 -0700117 verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000118 TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, false);
Tony Mak29996702018-11-26 16:23:34 +0000119 }
120
121
122 @Test
Milo Sredkov13d88112019-02-01 12:23:24 +0000123 public void testSendSmartReply_logsToStatusBar_modifiedBeforeSending() throws RemoteException {
124 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
125 MetricsEvent.LOCATION_UNKNOWN, true /* modifiedBeforeSending */);
Tony Mak29996702018-11-26 16:23:34 +0000126
127 // Check we log the result to the status bar service.
128 verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000129 TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, true);
Kenny Guya0f6de82018-04-06 16:20:16 +0100130 }
131
132 @Test
Gustav Senntond25a64d2018-12-07 10:58:39 +0000133 public void testShowSmartSuggestions_logsToStatusBar() throws RemoteException {
134 final boolean generatedByAsssistant = true;
Milo Sredkov13d88112019-02-01 12:23:24 +0000135 final boolean editBeforeSending = true;
Gustav Senntond25a64d2018-12-07 10:58:39 +0000136 mSmartReplyController.smartSuggestionsAdded(mEntry, TEST_CHOICE_COUNT, TEST_ACTION_COUNT,
Milo Sredkov13d88112019-02-01 12:23:24 +0000137 generatedByAsssistant, editBeforeSending);
Kenny Guya0f6de82018-04-06 16:20:16 +0100138
139 // Check we log the result to the status bar service.
Gustav Senntond25a64d2018-12-07 10:58:39 +0000140 verify(mIStatusBarService).onNotificationSmartSuggestionsAdded(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000141 TEST_CHOICE_COUNT, TEST_ACTION_COUNT, generatedByAsssistant, editBeforeSending);
Kenny Guya0f6de82018-04-06 16:20:16 +0100142 }
Kenny Guy8cc15d22018-05-09 09:50:55 +0100143
144 @Test
145 public void testSendSmartReply_reportsSending() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000146 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
147 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guy8cc15d22018-05-09 09:50:55 +0100148
Kevina5ff1fa2018-08-21 16:35:48 -0700149 assertTrue(mSmartReplyController.isSendingSmartReply(mSbn.getKey()));
Kenny Guy8cc15d22018-05-09 09:50:55 +0100150 }
151
152 @Test
153 public void testSendingSmartReply_afterRemove_shouldReturnFalse() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000154 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
155 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kevina5ff1fa2018-08-21 16:35:48 -0700156 mSmartReplyController.stopSending(mEntry);
Kenny Guy8cc15d22018-05-09 09:50:55 +0100157
Kevina5ff1fa2018-08-21 16:35:48 -0700158 assertFalse(mSmartReplyController.isSendingSmartReply(mSbn.getKey()));
Kenny Guy8cc15d22018-05-09 09:50:55 +0100159 }
Kenny Guya0f6de82018-04-06 16:20:16 +0100160}