blob: 31cd28019ea9c9dfe0ada4841e5eda900214e2c9 [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;
32import android.support.test.filters.SmallTest;
33import android.testing.AndroidTestingRunner;
34import android.testing.TestableLooper;
35
Gustav Sennton13edb492019-01-28 21:40:04 +000036import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Kenny Guya0f6de82018-04-06 16:20:16 +010037import com.android.internal.statusbar.IStatusBarService;
38import com.android.systemui.R;
39import com.android.systemui.SysuiTestCase;
Rohan Shah20790b82018-07-02 17:21:04 -070040import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050041import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Gus Prevas772e5322018-12-21 16:22:16 -050042import com.android.systemui.statusbar.phone.ShadeController;
Kenny Guya0f6de82018-04-06 16:20:16 +010043
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.Mock;
48import org.mockito.MockitoAnnotations;
49
50@RunWith(AndroidTestingRunner.class)
51@TestableLooper.RunWithLooper
52@SmallTest
53public class SmartReplyControllerTest extends SysuiTestCase {
Kevina5ff1fa2018-08-21 16:35:48 -070054 private static final String TEST_PACKAGE_NAME = "test";
55 private static final int TEST_UID = 0;
Kenny Guya0f6de82018-04-06 16:20:16 +010056 private static final String TEST_CHOICE_TEXT = "A Reply";
57 private static final int TEST_CHOICE_INDEX = 2;
58 private static final int TEST_CHOICE_COUNT = 4;
Gustav Senntond25a64d2018-12-07 10:58:39 +000059 private static final int TEST_ACTION_COUNT = 3;
Kenny Guya0f6de82018-04-06 16:20:16 +010060
61 private Notification mNotification;
Ned Burnsf81c4c42019-01-07 14:10:43 -050062 private NotificationEntry mEntry;
Kevina5ff1fa2018-08-21 16:35:48 -070063 private SmartReplyController mSmartReplyController;
64 private NotificationRemoteInputManager mRemoteInputManager;
Kenny Guya0f6de82018-04-06 16:20:16 +010065
Kevina5ff1fa2018-08-21 16:35:48 -070066 @Mock private NotificationPresenter mPresenter;
67 @Mock private RemoteInputController.Delegate mDelegate;
68 @Mock private NotificationRemoteInputManager.Callback mCallback;
69 @Mock private StatusBarNotification mSbn;
70 @Mock private NotificationEntryManager mNotificationEntryManager;
71 @Mock private IStatusBarService mIStatusBarService;
Kenny Guya0f6de82018-04-06 16:20:16 +010072
73 @Before
74 public void setUp() {
75 MockitoAnnotations.initMocks(this);
Kenny Guya0f6de82018-04-06 16:20:16 +010076 mDependency.injectTestDependency(NotificationEntryManager.class,
77 mNotificationEntryManager);
Kenny Guya0f6de82018-04-06 16:20:16 +010078
Jason Monk752c68f2018-12-21 12:07:55 -050079 mSmartReplyController = new SmartReplyController(mNotificationEntryManager,
80 mIStatusBarService);
Kevina5ff1fa2018-08-21 16:35:48 -070081 mDependency.injectTestDependency(SmartReplyController.class,
82 mSmartReplyController);
83
Gus Prevas772e5322018-12-21 16:22:16 -050084 mRemoteInputManager = new NotificationRemoteInputManager(mContext,
85 mock(NotificationLockscreenUserManager.class), mSmartReplyController,
86 mNotificationEntryManager, () -> mock(ShadeController.class),
87 Handler.createAsync(Looper.myLooper()));
Gus Prevas21437b32018-12-05 10:36:13 -050088 mRemoteInputManager.setUpWithCallback(mCallback, mDelegate);
Kenny Guya0f6de82018-04-06 16:20:16 +010089 mNotification = new Notification.Builder(mContext, "")
90 .setSmallIcon(R.drawable.ic_person)
91 .setContentTitle("Title")
92 .setContentText("Text").build();
Kevina5ff1fa2018-08-21 16:35:48 -070093
94 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID,
95 0, mNotification, new UserHandle(ActivityManager.getCurrentUser()), null, 0);
Ned Burnsf81c4c42019-01-07 14:10:43 -050096 mEntry = new NotificationEntry(mSbn);
Kenny Guya0f6de82018-04-06 16:20:16 +010097 }
98
99 @Test
Kenny Guy8cc15d22018-05-09 09:50:55 +0100100 public void testSendSmartReply_updatesRemoteInput() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000101 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
102 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guya0f6de82018-04-06 16:20:16 +0100103
104 // Sending smart reply should make calls to NotificationEntryManager
105 // to update the notification with reply and spinner.
Kenny Guya0f6de82018-04-06 16:20:16 +0100106 verify(mNotificationEntryManager).updateNotification(
Kevina5ff1fa2018-08-21 16:35:48 -0700107 argThat(sbn -> sbn.getKey().equals(mSbn.getKey())), isNull());
Kenny Guya0f6de82018-04-06 16:20:16 +0100108 }
109
110 @Test
111 public void testSendSmartReply_logsToStatusBar() throws RemoteException {
Milo Sredkov13d88112019-02-01 12:23:24 +0000112 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
113 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guya0f6de82018-04-06 16:20:16 +0100114
115 // Check we log the result to the status bar service.
Kevina5ff1fa2018-08-21 16:35:48 -0700116 verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000117 TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, false);
Tony Mak29996702018-11-26 16:23:34 +0000118 }
119
120
121 @Test
Milo Sredkov13d88112019-02-01 12:23:24 +0000122 public void testSendSmartReply_logsToStatusBar_modifiedBeforeSending() throws RemoteException {
123 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
124 MetricsEvent.LOCATION_UNKNOWN, true /* modifiedBeforeSending */);
Tony Mak29996702018-11-26 16:23:34 +0000125
126 // Check we log the result to the status bar service.
127 verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000128 TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, true);
Kenny Guya0f6de82018-04-06 16:20:16 +0100129 }
130
131 @Test
Gustav Senntond25a64d2018-12-07 10:58:39 +0000132 public void testShowSmartSuggestions_logsToStatusBar() throws RemoteException {
133 final boolean generatedByAsssistant = true;
Milo Sredkov13d88112019-02-01 12:23:24 +0000134 final boolean editBeforeSending = true;
Gustav Senntond25a64d2018-12-07 10:58:39 +0000135 mSmartReplyController.smartSuggestionsAdded(mEntry, TEST_CHOICE_COUNT, TEST_ACTION_COUNT,
Milo Sredkov13d88112019-02-01 12:23:24 +0000136 generatedByAsssistant, editBeforeSending);
Kenny Guya0f6de82018-04-06 16:20:16 +0100137
138 // Check we log the result to the status bar service.
Gustav Senntond25a64d2018-12-07 10:58:39 +0000139 verify(mIStatusBarService).onNotificationSmartSuggestionsAdded(mSbn.getKey(),
Milo Sredkov13d88112019-02-01 12:23:24 +0000140 TEST_CHOICE_COUNT, TEST_ACTION_COUNT, generatedByAsssistant, editBeforeSending);
Kenny Guya0f6de82018-04-06 16:20:16 +0100141 }
Kenny Guy8cc15d22018-05-09 09:50:55 +0100142
143 @Test
144 public void testSendSmartReply_reportsSending() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000145 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
146 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kenny Guy8cc15d22018-05-09 09:50:55 +0100147
Kevina5ff1fa2018-08-21 16:35:48 -0700148 assertTrue(mSmartReplyController.isSendingSmartReply(mSbn.getKey()));
Kenny Guy8cc15d22018-05-09 09:50:55 +0100149 }
150
151 @Test
152 public void testSendingSmartReply_afterRemove_shouldReturnFalse() {
Milo Sredkov13d88112019-02-01 12:23:24 +0000153 mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT,
154 MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */);
Kevina5ff1fa2018-08-21 16:35:48 -0700155 mSmartReplyController.stopSending(mEntry);
Kenny Guy8cc15d22018-05-09 09:50:55 +0100156
Kevina5ff1fa2018-08-21 16:35:48 -0700157 assertFalse(mSmartReplyController.isSendingSmartReply(mSbn.getKey()));
Kenny Guy8cc15d22018-05-09 09:50:55 +0100158 }
Kenny Guya0f6de82018-04-06 16:20:16 +0100159}