Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.systemui.statusbar; |
| 16 | |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 17 | import static junit.framework.Assert.assertFalse; |
| 18 | import static junit.framework.Assert.assertTrue; |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 19 | |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.argThat; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.isNull; |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 23 | import static org.mockito.Mockito.verify; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 24 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 26 | import android.app.Notification; |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 27 | import android.os.Handler; |
| 28 | import android.os.Looper; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 29 | import android.os.RemoteException; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 31 | import android.service.notification.StatusBarNotification; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 32 | import android.testing.AndroidTestingRunner; |
| 33 | import android.testing.TestableLooper; |
| 34 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 35 | import androidx.test.filters.SmallTest; |
| 36 | |
Gustav Sennton | 13edb49 | 2019-01-28 21:40:04 +0000 | [diff] [blame] | 37 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 38 | import com.android.internal.statusbar.IStatusBarService; |
| 39 | import com.android.systemui.R; |
| 40 | import com.android.systemui.SysuiTestCase; |
Selim Cinek | d17b350 | 2019-07-02 20:38:32 -0700 | [diff] [blame] | 41 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 42 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 43 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.ShadeController; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 45 | |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.MockitoAnnotations; |
| 51 | |
| 52 | @RunWith(AndroidTestingRunner.class) |
| 53 | @TestableLooper.RunWithLooper |
| 54 | @SmallTest |
| 55 | public class SmartReplyControllerTest extends SysuiTestCase { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 56 | private static final String TEST_PACKAGE_NAME = "test"; |
| 57 | private static final int TEST_UID = 0; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 58 | 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 Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 61 | private static final int TEST_ACTION_COUNT = 3; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 62 | |
| 63 | private Notification mNotification; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 64 | private NotificationEntry mEntry; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 65 | private SmartReplyController mSmartReplyController; |
| 66 | private NotificationRemoteInputManager mRemoteInputManager; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 67 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 68 | @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 Cinek | d17b350 | 2019-07-02 20:38:32 -0700 | [diff] [blame] | 74 | @Mock private StatusBarStateController mStatusBarStateController; |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 75 | |
| 76 | @Before |
| 77 | public void setUp() { |
| 78 | MockitoAnnotations.initMocks(this); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 79 | mDependency.injectTestDependency(NotificationEntryManager.class, |
| 80 | mNotificationEntryManager); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 81 | |
Jason Monk | 752c68f | 2018-12-21 12:07:55 -0500 | [diff] [blame] | 82 | mSmartReplyController = new SmartReplyController(mNotificationEntryManager, |
| 83 | mIStatusBarService); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 84 | mDependency.injectTestDependency(SmartReplyController.class, |
| 85 | mSmartReplyController); |
| 86 | |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 87 | mRemoteInputManager = new NotificationRemoteInputManager(mContext, |
| 88 | mock(NotificationLockscreenUserManager.class), mSmartReplyController, |
| 89 | mNotificationEntryManager, () -> mock(ShadeController.class), |
Selim Cinek | d17b350 | 2019-07-02 20:38:32 -0700 | [diff] [blame] | 90 | mStatusBarStateController, |
Gus Prevas | 772e532 | 2018-12-21 16:22:16 -0500 | [diff] [blame] | 91 | Handler.createAsync(Looper.myLooper())); |
Gus Prevas | 21437b3 | 2018-12-05 10:36:13 -0500 | [diff] [blame] | 92 | mRemoteInputManager.setUpWithCallback(mCallback, mDelegate); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 93 | mNotification = new Notification.Builder(mContext, "") |
| 94 | .setSmallIcon(R.drawable.ic_person) |
| 95 | .setContentTitle("Title") |
| 96 | .setContentText("Text").build(); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 97 | |
| 98 | mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, |
| 99 | 0, mNotification, new UserHandle(ActivityManager.getCurrentUser()), null, 0); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 100 | mEntry = new NotificationEntry(mSbn); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 104 | public void testSendSmartReply_updatesRemoteInput() { |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 105 | mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, |
| 106 | MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 107 | |
| 108 | // Sending smart reply should make calls to NotificationEntryManager |
| 109 | // to update the notification with reply and spinner. |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 110 | verify(mNotificationEntryManager).updateNotification( |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 111 | argThat(sbn -> sbn.getKey().equals(mSbn.getKey())), isNull()); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testSendSmartReply_logsToStatusBar() throws RemoteException { |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 116 | mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, |
| 117 | MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 118 | |
| 119 | // Check we log the result to the status bar service. |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 120 | verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(), |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 121 | TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, false); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | @Test |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 126 | public void testSendSmartReply_logsToStatusBar_modifiedBeforeSending() throws RemoteException { |
| 127 | mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, |
| 128 | MetricsEvent.LOCATION_UNKNOWN, true /* modifiedBeforeSending */); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 129 | |
| 130 | // Check we log the result to the status bar service. |
| 131 | verify(mIStatusBarService).onNotificationSmartReplySent(mSbn.getKey(), |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 132 | TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, MetricsEvent.LOCATION_UNKNOWN, true); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Test |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 136 | public void testShowSmartSuggestions_logsToStatusBar() throws RemoteException { |
| 137 | final boolean generatedByAsssistant = true; |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 138 | final boolean editBeforeSending = true; |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 139 | mSmartReplyController.smartSuggestionsAdded(mEntry, TEST_CHOICE_COUNT, TEST_ACTION_COUNT, |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 140 | generatedByAsssistant, editBeforeSending); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 141 | |
| 142 | // Check we log the result to the status bar service. |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 143 | verify(mIStatusBarService).onNotificationSmartSuggestionsAdded(mSbn.getKey(), |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 144 | TEST_CHOICE_COUNT, TEST_ACTION_COUNT, generatedByAsssistant, editBeforeSending); |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 145 | } |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 146 | |
| 147 | @Test |
| 148 | public void testSendSmartReply_reportsSending() { |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 149 | mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, |
| 150 | MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */); |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 151 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 152 | assertTrue(mSmartReplyController.isSendingSmartReply(mSbn.getKey())); |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testSendingSmartReply_afterRemove_shouldReturnFalse() { |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 157 | mSmartReplyController.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT, |
| 158 | MetricsEvent.LOCATION_UNKNOWN, false /* modifiedBeforeSending */); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 159 | mSmartReplyController.stopSending(mEntry); |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 160 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 161 | assertFalse(mSmartReplyController.isSendingSmartReply(mSbn.getKey())); |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 162 | } |
Kenny Guy | a0f6de8 | 2018-04-06 16:20:16 +0100 | [diff] [blame] | 163 | } |