Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.ext.services.notification; |
| 17 | |
| 18 | import static com.google.common.truth.Truth.assertAbout; |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.any; |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.argThat; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 23 | import static org.mockito.Mockito.never; |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 24 | import static org.mockito.Mockito.times; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.annotation.NonNull; |
| 29 | import android.app.Notification; |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 30 | import android.app.NotificationChannel; |
| 31 | import android.app.NotificationManager; |
| 32 | import android.app.PendingIntent; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 33 | import android.app.Person; |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 34 | import android.app.RemoteInput; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 35 | import android.content.Context; |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 36 | import android.content.Intent; |
| 37 | import android.content.pm.IPackageManager; |
| 38 | import android.graphics.drawable.Icon; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 39 | import android.os.Process; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 40 | import android.service.notification.NotificationAssistantService; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 41 | import android.service.notification.StatusBarNotification; |
| 42 | import android.support.test.InstrumentationRegistry; |
| 43 | import android.support.test.runner.AndroidJUnit4; |
Tony Mak | ae85aae | 2019-01-09 15:59:56 +0000 | [diff] [blame] | 44 | import android.view.textclassifier.ConversationAction; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 45 | import android.view.textclassifier.ConversationActions; |
| 46 | import android.view.textclassifier.TextClassificationManager; |
| 47 | import android.view.textclassifier.TextClassifier; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 48 | import android.view.textclassifier.TextClassifierEvent; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 49 | |
| 50 | import com.google.common.truth.FailureStrategy; |
| 51 | import com.google.common.truth.Subject; |
| 52 | import com.google.common.truth.SubjectFactory; |
| 53 | |
| 54 | import org.junit.Before; |
| 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
| 57 | import org.mockito.ArgumentCaptor; |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 58 | import org.mockito.ArgumentMatcher; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 59 | import org.mockito.Mock; |
| 60 | import org.mockito.MockitoAnnotations; |
| 61 | |
| 62 | import java.time.Instant; |
| 63 | import java.time.ZoneOffset; |
| 64 | import java.time.ZonedDateTime; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 65 | import java.util.Arrays; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 66 | import java.util.Collections; |
| 67 | import java.util.List; |
| 68 | import java.util.Objects; |
| 69 | |
| 70 | import javax.annotation.Nullable; |
| 71 | |
| 72 | @RunWith(AndroidJUnit4.class) |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 73 | public class SmartActionsHelperTest { |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 74 | private static final String NOTIFICATION_KEY = "key"; |
| 75 | private static final String RESULT_ID = "id"; |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 76 | private static final float SCORE = 0.7f; |
| 77 | private static final CharSequence SMART_REPLY = "Home"; |
Tony Mak | ae85aae | 2019-01-09 15:59:56 +0000 | [diff] [blame] | 78 | private static final ConversationAction REPLY_ACTION = |
| 79 | new ConversationAction.Builder(ConversationAction.TYPE_TEXT_REPLY) |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 80 | .setTextReply(SMART_REPLY) |
| 81 | .setConfidenceScore(SCORE) |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 82 | .build(); |
| 83 | private static final String MESSAGE = "Where are you?"; |
| 84 | |
| 85 | @Mock |
| 86 | IPackageManager mIPackageManager; |
| 87 | @Mock |
| 88 | private TextClassifier mTextClassifier; |
| 89 | @Mock |
| 90 | private StatusBarNotification mStatusBarNotification; |
| 91 | @Mock |
| 92 | private SmsHelper mSmsHelper; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 93 | |
| 94 | private SmartActionsHelper mSmartActionsHelper; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 95 | private Context mContext; |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 96 | private Notification.Builder mNotificationBuilder; |
| 97 | private AssistantSettings mSettings; |
| 98 | |
| 99 | @Before |
| 100 | public void setup() { |
| 101 | MockitoAnnotations.initMocks(this); |
| 102 | mContext = InstrumentationRegistry.getTargetContext(); |
| 103 | |
| 104 | mContext.getSystemService(TextClassificationManager.class) |
| 105 | .setTextClassifier(mTextClassifier); |
| 106 | when(mTextClassifier.suggestConversationActions(any(ConversationActions.Request.class))) |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 107 | .thenReturn(new ConversationActions(Arrays.asList(REPLY_ACTION), RESULT_ID)); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 108 | |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 109 | when(mStatusBarNotification.getPackageName()).thenReturn("random.app"); |
| 110 | when(mStatusBarNotification.getUser()).thenReturn(Process.myUserHandle()); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 111 | when(mStatusBarNotification.getKey()).thenReturn(NOTIFICATION_KEY); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 112 | mNotificationBuilder = new Notification.Builder(mContext, "channel"); |
| 113 | mSettings = AssistantSettings.createForTesting( |
| 114 | null, null, Process.myUserHandle().getIdentifier(), null); |
| 115 | mSettings.mGenerateActions = true; |
| 116 | mSettings.mGenerateReplies = true; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 117 | mSmartActionsHelper = new SmartActionsHelper(mContext, mSettings); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 121 | public void testSuggest_notMessageNotification() { |
| 122 | Notification notification = mNotificationBuilder.setContentText(MESSAGE).build(); |
| 123 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
| 124 | |
| 125 | mSmartActionsHelper.suggest(createNotificationEntry()); |
| 126 | |
| 127 | verify(mTextClassifier, never()) |
| 128 | .suggestConversationActions(any(ConversationActions.Request.class)); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Test |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 132 | public void testSuggest_noInlineReply() { |
| 133 | Notification notification = |
| 134 | mNotificationBuilder |
| 135 | .setContentText(MESSAGE) |
| 136 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 137 | .build(); |
| 138 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
| 139 | |
| 140 | ConversationActions.Request request = runSuggestAndCaptureRequest(); |
| 141 | |
| 142 | // actions are enabled, but replies are not. |
| 143 | assertThat( |
| 144 | request.getTypeConfig().resolveEntityListModifications( |
| 145 | Arrays.asList(ConversationAction.TYPE_TEXT_REPLY, |
| 146 | ConversationAction.TYPE_OPEN_URL))) |
| 147 | .containsExactly(ConversationAction.TYPE_OPEN_URL); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Test |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 151 | public void testSuggest_settingsOff() { |
| 152 | mSettings.mGenerateActions = false; |
| 153 | mSettings.mGenerateReplies = false; |
| 154 | Notification notification = createMessageNotification(); |
| 155 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 156 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 157 | mSmartActionsHelper.suggest(createNotificationEntry()); |
| 158 | |
| 159 | verify(mTextClassifier, never()) |
| 160 | .suggestConversationActions(any(ConversationActions.Request.class)); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testSuggest_settings_repliesOnActionsOff() { |
| 165 | mSettings.mGenerateReplies = true; |
| 166 | mSettings.mGenerateActions = false; |
| 167 | Notification notification = createMessageNotification(); |
| 168 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
| 169 | |
| 170 | ConversationActions.Request request = runSuggestAndCaptureRequest(); |
| 171 | |
| 172 | // replies are enabled, but actions are not. |
| 173 | assertThat( |
| 174 | request.getTypeConfig().resolveEntityListModifications( |
| 175 | Arrays.asList(ConversationAction.TYPE_TEXT_REPLY, |
| 176 | ConversationAction.TYPE_OPEN_URL))) |
| 177 | .containsExactly(ConversationAction.TYPE_TEXT_REPLY); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testSuggest_settings_repliesOffActionsOn() { |
| 182 | mSettings.mGenerateReplies = false; |
| 183 | mSettings.mGenerateActions = true; |
| 184 | Notification notification = createMessageNotification(); |
| 185 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
| 186 | |
| 187 | ConversationActions.Request request = runSuggestAndCaptureRequest(); |
| 188 | |
| 189 | // actions are enabled, but replies are not. |
| 190 | assertThat( |
| 191 | request.getTypeConfig().resolveEntityListModifications( |
| 192 | Arrays.asList(ConversationAction.TYPE_TEXT_REPLY, |
| 193 | ConversationAction.TYPE_OPEN_URL))) |
| 194 | .containsExactly(ConversationAction.TYPE_OPEN_URL); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | @Test |
| 199 | public void testSuggest_nonMessageStyleMessageNotification() { |
| 200 | Notification notification = createMessageNotification(); |
| 201 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
| 202 | |
| 203 | List<ConversationActions.Message> messages = |
| 204 | runSuggestAndCaptureRequest().getConversation(); |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 205 | |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 206 | assertThat(messages).hasSize(1); |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 207 | MessageSubject.assertThat(messages.get(0)).hasText(MESSAGE); |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 208 | ArgumentCaptor<TextClassifierEvent> argumentCaptor = |
| 209 | ArgumentCaptor.forClass(TextClassifierEvent.class); |
| 210 | verify(mTextClassifier).onTextClassifierEvent(argumentCaptor.capture()); |
| 211 | TextClassifierEvent textClassifierEvent = argumentCaptor.getValue(); |
| 212 | assertTextClassifierEvent(textClassifierEvent, TextClassifierEvent.TYPE_ACTIONS_GENERATED); |
| 213 | assertThat(textClassifierEvent.getEntityTypes()).asList() |
| 214 | .containsExactly(ConversationAction.TYPE_TEXT_REPLY); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Test |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 218 | public void testSuggest_messageStyle() { |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 219 | Person me = new Person.Builder().setName("Me").build(); |
| 220 | Person userA = new Person.Builder().setName("A").build(); |
| 221 | Person userB = new Person.Builder().setName("B").build(); |
| 222 | Notification.MessagingStyle style = |
| 223 | new Notification.MessagingStyle(me) |
| 224 | .addMessage("firstMessage", 1000, (Person) null) |
| 225 | .addMessage("secondMessage", 2000, me) |
| 226 | .addMessage("thirdMessage", 3000, userA) |
| 227 | .addMessage("fourthMessage", 4000, userB); |
| 228 | Notification notification = |
| 229 | mNotificationBuilder |
| 230 | .setContentText("You have three new messages") |
| 231 | .setStyle(style) |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 232 | .setActions(createReplyAction()) |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 233 | .build(); |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 234 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 235 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 236 | List<ConversationActions.Message> messages = |
| 237 | runSuggestAndCaptureRequest().getConversation(); |
Tony Mak | 7ab8308 | 2019-02-04 17:43:53 +0000 | [diff] [blame] | 238 | assertThat(messages).hasSize(4); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 239 | |
Tony Mak | 7ab8308 | 2019-02-04 17:43:53 +0000 | [diff] [blame] | 240 | ConversationActions.Message firstMessage = messages.get(0); |
| 241 | MessageSubject.assertThat(firstMessage).hasText("firstMessage"); |
| 242 | MessageSubject.assertThat(firstMessage) |
| 243 | .hasPerson(ConversationActions.Message.PERSON_USER_SELF); |
| 244 | MessageSubject.assertThat(firstMessage) |
| 245 | .hasReferenceTime(createZonedDateTimeFromMsUtc(1000)); |
| 246 | |
| 247 | ConversationActions.Message secondMessage = messages.get(1); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 248 | MessageSubject.assertThat(secondMessage).hasText("secondMessage"); |
| 249 | MessageSubject.assertThat(secondMessage) |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 250 | .hasPerson(ConversationActions.Message.PERSON_USER_SELF); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 251 | MessageSubject.assertThat(secondMessage) |
| 252 | .hasReferenceTime(createZonedDateTimeFromMsUtc(2000)); |
| 253 | |
Tony Mak | 7ab8308 | 2019-02-04 17:43:53 +0000 | [diff] [blame] | 254 | ConversationActions.Message thirdMessage = messages.get(2); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 255 | MessageSubject.assertThat(thirdMessage).hasText("thirdMessage"); |
| 256 | MessageSubject.assertThat(thirdMessage).hasPerson(userA); |
| 257 | MessageSubject.assertThat(thirdMessage) |
| 258 | .hasReferenceTime(createZonedDateTimeFromMsUtc(3000)); |
| 259 | |
Tony Mak | 7ab8308 | 2019-02-04 17:43:53 +0000 | [diff] [blame] | 260 | ConversationActions.Message fourthMessage = messages.get(3); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 261 | MessageSubject.assertThat(fourthMessage).hasText("fourthMessage"); |
| 262 | MessageSubject.assertThat(fourthMessage).hasPerson(userB); |
| 263 | MessageSubject.assertThat(fourthMessage) |
| 264 | .hasReferenceTime(createZonedDateTimeFromMsUtc(4000)); |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 265 | |
| 266 | ArgumentCaptor<TextClassifierEvent> argumentCaptor = |
| 267 | ArgumentCaptor.forClass(TextClassifierEvent.class); |
| 268 | verify(mTextClassifier).onTextClassifierEvent(argumentCaptor.capture()); |
| 269 | TextClassifierEvent textClassifierEvent = argumentCaptor.getValue(); |
| 270 | assertTextClassifierEvent(textClassifierEvent, TextClassifierEvent.TYPE_ACTIONS_GENERATED); |
| 271 | assertThat(textClassifierEvent.getEntityTypes()).asList() |
| 272 | .containsExactly(ConversationAction.TYPE_TEXT_REPLY); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | @Test |
Tony Mak | 7ab8308 | 2019-02-04 17:43:53 +0000 | [diff] [blame] | 276 | public void testSuggest_lastMessageLocalUser() { |
| 277 | Person me = new Person.Builder().setName("Me").build(); |
| 278 | Person userA = new Person.Builder().setName("A").build(); |
| 279 | Notification.MessagingStyle style = |
| 280 | new Notification.MessagingStyle(me) |
| 281 | .addMessage("firstMessage", 1000, userA) |
| 282 | .addMessage("secondMessage", 2000, me); |
| 283 | Notification notification = |
| 284 | mNotificationBuilder |
| 285 | .setContentText("You have two new messages") |
| 286 | .setStyle(style) |
| 287 | .setActions(createReplyAction()) |
| 288 | .build(); |
| 289 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
| 290 | |
| 291 | mSmartActionsHelper.suggest(createNotificationEntry()); |
| 292 | |
| 293 | verify(mTextClassifier, never()) |
| 294 | .suggestConversationActions(any(ConversationActions.Request.class)); |
| 295 | } |
| 296 | |
| 297 | @Test |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 298 | public void testSuggest_messageStyle_noPerson() { |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 299 | Person me = new Person.Builder().setName("Me").build(); |
| 300 | Notification.MessagingStyle style = |
| 301 | new Notification.MessagingStyle(me).addMessage("message", 1000, (Person) null); |
| 302 | Notification notification = |
| 303 | mNotificationBuilder |
| 304 | .setContentText("You have one new message") |
| 305 | .setStyle(style) |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 306 | .setActions(createReplyAction()) |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 307 | .build(); |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 308 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 309 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 310 | mSmartActionsHelper.suggest(createNotificationEntry()); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 311 | |
| 312 | verify(mTextClassifier, never()) |
| 313 | .suggestConversationActions(any(ConversationActions.Request.class)); |
| 314 | } |
| 315 | |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 316 | @Test |
| 317 | public void testOnSuggestedReplySent() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 318 | Notification notification = createMessageNotification(); |
| 319 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 320 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 321 | mSmartActionsHelper.suggest(createNotificationEntry()); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 322 | mSmartActionsHelper.onSuggestedReplySent( |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 323 | NOTIFICATION_KEY, SMART_REPLY, NotificationAssistantService.SOURCE_FROM_ASSISTANT); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 324 | |
| 325 | ArgumentCaptor<TextClassifierEvent> argumentCaptor = |
| 326 | ArgumentCaptor.forClass(TextClassifierEvent.class); |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 327 | verify(mTextClassifier, times(2)).onTextClassifierEvent(argumentCaptor.capture()); |
| 328 | List<TextClassifierEvent> events = argumentCaptor.getAllValues(); |
| 329 | assertTextClassifierEvent(events.get(0), TextClassifierEvent.TYPE_ACTIONS_GENERATED); |
| 330 | assertTextClassifierEvent(events.get(1), TextClassifierEvent.TYPE_SMART_ACTION); |
| 331 | assertThat(events.get(1).getScore()).isEqualTo(SCORE); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Test |
| 335 | public void testOnSuggestedReplySent_anotherNotification() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 336 | Notification notification = createMessageNotification(); |
| 337 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 338 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 339 | mSmartActionsHelper.suggest(createNotificationEntry()); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 340 | mSmartActionsHelper.onSuggestedReplySent( |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 341 | "something_else", MESSAGE, NotificationAssistantService.SOURCE_FROM_ASSISTANT); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 342 | |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 343 | verify(mTextClassifier, never()).onTextClassifierEvent( |
| 344 | argThat(new TextClassifierEventMatcher(TextClassifierEvent.TYPE_SMART_ACTION))); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @Test |
| 348 | public void testOnSuggestedReplySent_missingResultId() { |
| 349 | when(mTextClassifier.suggestConversationActions(any(ConversationActions.Request.class))) |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 350 | .thenReturn(new ConversationActions(Collections.singletonList(REPLY_ACTION), null)); |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 351 | Notification notification = createMessageNotification(); |
| 352 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 353 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 354 | mSmartActionsHelper.suggest(createNotificationEntry()); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 355 | mSmartActionsHelper.onSuggestedReplySent( |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 356 | NOTIFICATION_KEY, SMART_REPLY, NotificationAssistantService.SOURCE_FROM_ASSISTANT); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 357 | |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 358 | verify(mTextClassifier, never()).onTextClassifierEvent(any(TextClassifierEvent.class)); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Test |
| 362 | public void testOnNotificationDirectReply() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 363 | Notification notification = createMessageNotification(); |
| 364 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 365 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 366 | mSmartActionsHelper.suggest(createNotificationEntry()); |
Tony Mak | 836147e | 2019-01-08 14:48:31 +0000 | [diff] [blame] | 367 | mSmartActionsHelper.onNotificationDirectReplied(NOTIFICATION_KEY); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 368 | |
| 369 | ArgumentCaptor<TextClassifierEvent> argumentCaptor = |
| 370 | ArgumentCaptor.forClass(TextClassifierEvent.class); |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 371 | verify(mTextClassifier, times(2)).onTextClassifierEvent(argumentCaptor.capture()); |
| 372 | List<TextClassifierEvent> events = argumentCaptor.getAllValues(); |
| 373 | assertTextClassifierEvent(events.get(0), TextClassifierEvent.TYPE_ACTIONS_GENERATED); |
| 374 | assertTextClassifierEvent(events.get(1), TextClassifierEvent.TYPE_MANUAL_REPLY); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Test |
| 378 | public void testOnNotificationExpansionChanged() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 379 | Notification notification = createMessageNotification(); |
| 380 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 381 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 382 | mSmartActionsHelper.suggest(createNotificationEntry()); |
| 383 | mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), true, true); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 384 | |
| 385 | ArgumentCaptor<TextClassifierEvent> argumentCaptor = |
| 386 | ArgumentCaptor.forClass(TextClassifierEvent.class); |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 387 | verify(mTextClassifier, times(2)).onTextClassifierEvent(argumentCaptor.capture()); |
| 388 | List<TextClassifierEvent> events = argumentCaptor.getAllValues(); |
| 389 | assertTextClassifierEvent(events.get(0), TextClassifierEvent.TYPE_ACTIONS_GENERATED); |
| 390 | assertTextClassifierEvent(events.get(1), TextClassifierEvent.TYPE_ACTIONS_SHOWN); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | @Test |
| 394 | public void testOnNotificationsSeen_notExpanded() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 395 | Notification notification = createMessageNotification(); |
| 396 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 397 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 398 | mSmartActionsHelper.suggest(createNotificationEntry()); |
| 399 | mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), false, false); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 400 | |
| 401 | verify(mTextClassifier, never()).onTextClassifierEvent( |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 402 | argThat(new TextClassifierEventMatcher(TextClassifierEvent.TYPE_ACTIONS_SHOWN))); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Test |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 406 | public void testOnNotifications_expanded() { |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 407 | Notification notification = createMessageNotification(); |
| 408 | when(mStatusBarNotification.getNotification()).thenReturn(notification); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 409 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 410 | mSmartActionsHelper.suggest(createNotificationEntry()); |
| 411 | mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), false, true); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 412 | |
| 413 | ArgumentCaptor<TextClassifierEvent> argumentCaptor = |
| 414 | ArgumentCaptor.forClass(TextClassifierEvent.class); |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 415 | verify(mTextClassifier, times(2)).onTextClassifierEvent(argumentCaptor.capture()); |
| 416 | List<TextClassifierEvent> events = argumentCaptor.getAllValues(); |
| 417 | assertTextClassifierEvent(events.get(0), TextClassifierEvent.TYPE_ACTIONS_GENERATED); |
| 418 | assertTextClassifierEvent(events.get(1), TextClassifierEvent.TYPE_ACTIONS_SHOWN); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 419 | } |
| 420 | |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 421 | private ZonedDateTime createZonedDateTimeFromMsUtc(long msUtc) { |
| 422 | return ZonedDateTime.ofInstant(Instant.ofEpochMilli(msUtc), ZoneOffset.systemDefault()); |
| 423 | } |
| 424 | |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 425 | private ConversationActions.Request runSuggestAndCaptureRequest() { |
| 426 | mSmartActionsHelper.suggest(createNotificationEntry()); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 427 | |
| 428 | ArgumentCaptor<ConversationActions.Request> argumentCaptor = |
| 429 | ArgumentCaptor.forClass(ConversationActions.Request.class); |
| 430 | verify(mTextClassifier).suggestConversationActions(argumentCaptor.capture()); |
Tony Mak | e1a27ac | 2019-01-31 16:32:19 +0000 | [diff] [blame] | 431 | return argumentCaptor.getValue(); |
| 432 | } |
| 433 | |
| 434 | private Notification.Action createReplyAction() { |
| 435 | PendingIntent pendingIntent = |
| 436 | PendingIntent.getActivity(mContext, 0, new Intent(mContext, this.getClass()), 0); |
| 437 | RemoteInput remoteInput = new RemoteInput.Builder("result") |
| 438 | .setAllowFreeFormInput(true) |
| 439 | .build(); |
| 440 | return new Notification.Action.Builder( |
| 441 | Icon.createWithResource(mContext.getResources(), |
| 442 | android.R.drawable.stat_sys_warning), |
| 443 | "Reply", pendingIntent) |
| 444 | .addRemoteInput(remoteInput) |
| 445 | .build(); |
| 446 | } |
| 447 | |
| 448 | private NotificationEntry createNotificationEntry() { |
| 449 | NotificationChannel channel = |
| 450 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 451 | return new NotificationEntry(mIPackageManager, mStatusBarNotification, channel, mSmsHelper); |
| 452 | } |
| 453 | |
| 454 | private Notification createMessageNotification() { |
| 455 | return mNotificationBuilder |
| 456 | .setContentText(MESSAGE) |
| 457 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 458 | .setActions(createReplyAction()) |
| 459 | .build(); |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 462 | private void assertTextClassifierEvent( |
| 463 | TextClassifierEvent textClassifierEvent, int expectedEventType) { |
| 464 | assertThat(textClassifierEvent.getEventCategory()) |
| 465 | .isEqualTo(TextClassifierEvent.CATEGORY_CONVERSATION_ACTIONS); |
| 466 | assertThat(textClassifierEvent.getEventContext().getPackageName()) |
| 467 | .isEqualTo(InstrumentationRegistry.getTargetContext().getPackageName()); |
| 468 | assertThat(textClassifierEvent.getEventContext().getWidgetType()) |
| 469 | .isEqualTo(TextClassifier.WIDGET_TYPE_NOTIFICATION); |
| 470 | assertThat(textClassifierEvent.getEventType()).isEqualTo(expectedEventType); |
| 471 | } |
| 472 | |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 473 | private static final class MessageSubject |
| 474 | extends Subject<MessageSubject, ConversationActions.Message> { |
| 475 | |
| 476 | private static final SubjectFactory<MessageSubject, ConversationActions.Message> FACTORY = |
| 477 | new SubjectFactory<MessageSubject, ConversationActions.Message>() { |
| 478 | @Override |
| 479 | public MessageSubject getSubject( |
| 480 | @NonNull FailureStrategy failureStrategy, |
| 481 | @NonNull ConversationActions.Message subject) { |
| 482 | return new MessageSubject(failureStrategy, subject); |
| 483 | } |
| 484 | }; |
| 485 | |
| 486 | private MessageSubject( |
| 487 | FailureStrategy failureStrategy, @Nullable ConversationActions.Message subject) { |
| 488 | super(failureStrategy, subject); |
| 489 | } |
| 490 | |
| 491 | private void hasText(String text) { |
| 492 | if (!Objects.equals(text, getSubject().getText().toString())) { |
| 493 | failWithBadResults("has text", text, "has", getSubject().getText()); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | private void hasPerson(Person person) { |
| 498 | if (!Objects.equals(person, getSubject().getAuthor())) { |
| 499 | failWithBadResults("has author", person, "has", getSubject().getAuthor()); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | private void hasReferenceTime(ZonedDateTime referenceTime) { |
| 504 | if (!Objects.equals(referenceTime, getSubject().getReferenceTime())) { |
| 505 | failWithBadResults( |
| 506 | "has reference time", |
| 507 | referenceTime, |
| 508 | "has", |
| 509 | getSubject().getReferenceTime()); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | private static MessageSubject assertThat(ConversationActions.Message message) { |
| 514 | return assertAbout(FACTORY).that(message); |
| 515 | } |
| 516 | } |
Tony Mak | 40d5ce0 | 2019-02-11 20:38:26 +0000 | [diff] [blame] | 517 | |
| 518 | private final class TextClassifierEventMatcher implements ArgumentMatcher<TextClassifierEvent> { |
| 519 | |
| 520 | private int mType; |
| 521 | |
| 522 | private TextClassifierEventMatcher(int type) { |
| 523 | mType = type; |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public boolean matches(TextClassifierEvent textClassifierEvent) { |
| 528 | if (textClassifierEvent == null) { |
| 529 | return false; |
| 530 | } |
| 531 | return mType == textClassifierEvent.getEventType(); |
| 532 | } |
| 533 | } |
Tony Mak | 43a899f | 2018-12-07 18:30:51 +0000 | [diff] [blame] | 534 | } |