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