Tony Mak | f99ee17 | 2018-11-23 12:14:39 +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 | |
| 17 | package android.view.textclassifier; |
| 18 | |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 19 | import static android.view.textclassifier.ConversationActions.Message.PERSON_USER_OTHERS; |
| 20 | import static android.view.textclassifier.ConversationActions.Message.PERSON_USER_SELF; |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 21 | |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 24 | import android.app.PendingIntent; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 25 | import android.app.Person; |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 26 | import android.app.RemoteAction; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.Intent; |
| 29 | import android.graphics.drawable.Icon; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 30 | import android.net.Uri; |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 31 | import android.os.Bundle; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 32 | import android.view.textclassifier.intent.LabeledIntent; |
| 33 | import android.view.textclassifier.intent.TemplateIntentFactory; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 34 | |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 35 | import androidx.test.InstrumentationRegistry; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 36 | import androidx.test.filters.SmallTest; |
| 37 | import androidx.test.runner.AndroidJUnit4; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 38 | |
| 39 | import com.google.android.textclassifier.ActionsSuggestionsModel; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 40 | import com.google.android.textclassifier.RemoteActionTemplate; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 41 | |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 45 | import java.time.Instant; |
| 46 | import java.time.ZoneId; |
| 47 | import java.time.ZonedDateTime; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 48 | import java.util.Arrays; |
| 49 | import java.util.Collections; |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 50 | import java.util.List; |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 51 | import java.util.Locale; |
| 52 | import java.util.function.Function; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 53 | |
| 54 | @SmallTest |
| 55 | @RunWith(AndroidJUnit4.class) |
| 56 | public class ActionsSuggestionsHelperTest { |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 57 | private static final String LOCALE_TAG = Locale.US.toLanguageTag(); |
| 58 | private static final Function<CharSequence, String> LANGUAGE_DETECTOR = |
| 59 | charSequence -> LOCALE_TAG; |
| 60 | |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 61 | @Test |
| 62 | public void testToNativeMessages_emptyInput() { |
| 63 | ActionsSuggestionsModel.ConversationMessage[] conversationMessages = |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 64 | ActionsSuggestionsHelper.toNativeMessages( |
| 65 | Collections.emptyList(), LANGUAGE_DETECTOR); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 66 | |
| 67 | assertThat(conversationMessages).isEmpty(); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testToNativeMessages_noTextMessages() { |
| 72 | ConversationActions.Message messageWithoutText = |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 73 | new ConversationActions.Message.Builder(PERSON_USER_OTHERS).build(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 74 | |
| 75 | ActionsSuggestionsModel.ConversationMessage[] conversationMessages = |
| 76 | ActionsSuggestionsHelper.toNativeMessages( |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 77 | Collections.singletonList(messageWithoutText), LANGUAGE_DETECTOR); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 78 | |
| 79 | assertThat(conversationMessages).isEmpty(); |
| 80 | } |
| 81 | |
| 82 | @Test |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 83 | public void testToNativeMessages_userIdEncoding() { |
| 84 | Person userA = new Person.Builder().setName("userA").build(); |
| 85 | Person userB = new Person.Builder().setName("userB").build(); |
| 86 | |
| 87 | ConversationActions.Message firstMessage = |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 88 | new ConversationActions.Message.Builder(userB) |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 89 | .setText("first") |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 90 | .build(); |
| 91 | ConversationActions.Message secondMessage = |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 92 | new ConversationActions.Message.Builder(userA) |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 93 | .setText("second") |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 94 | .build(); |
| 95 | ConversationActions.Message thirdMessage = |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 96 | new ConversationActions.Message.Builder(PERSON_USER_SELF) |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 97 | .setText("third") |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 98 | .build(); |
| 99 | ConversationActions.Message fourthMessage = |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 100 | new ConversationActions.Message.Builder(userA) |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 101 | .setText("fourth") |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 102 | .build(); |
| 103 | |
| 104 | ActionsSuggestionsModel.ConversationMessage[] conversationMessages = |
| 105 | ActionsSuggestionsHelper.toNativeMessages( |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 106 | Arrays.asList(firstMessage, secondMessage, thirdMessage, fourthMessage), |
| 107 | LANGUAGE_DETECTOR); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 108 | |
| 109 | assertThat(conversationMessages).hasLength(4); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 110 | assertNativeMessage(conversationMessages[0], firstMessage.getText(), 2, 0); |
| 111 | assertNativeMessage(conversationMessages[1], secondMessage.getText(), 1, 0); |
| 112 | assertNativeMessage(conversationMessages[2], thirdMessage.getText(), 0, 0); |
| 113 | assertNativeMessage(conversationMessages[3], fourthMessage.getText(), 1, 0); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testToNativeMessages_referenceTime() { |
| 118 | ConversationActions.Message firstMessage = |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 119 | new ConversationActions.Message.Builder(PERSON_USER_OTHERS) |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 120 | .setText("first") |
| 121 | .setReferenceTime(createZonedDateTimeFromMsUtc(1000)) |
| 122 | .build(); |
| 123 | ConversationActions.Message secondMessage = |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 124 | new ConversationActions.Message.Builder(PERSON_USER_OTHERS) |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 125 | .setText("second") |
| 126 | .build(); |
| 127 | ConversationActions.Message thirdMessage = |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 128 | new ConversationActions.Message.Builder(PERSON_USER_OTHERS) |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 129 | .setText("third") |
| 130 | .setReferenceTime(createZonedDateTimeFromMsUtc(2000)) |
| 131 | .build(); |
| 132 | |
| 133 | ActionsSuggestionsModel.ConversationMessage[] conversationMessages = |
| 134 | ActionsSuggestionsHelper.toNativeMessages( |
| 135 | Arrays.asList(firstMessage, secondMessage, thirdMessage), |
| 136 | LANGUAGE_DETECTOR); |
| 137 | |
| 138 | assertThat(conversationMessages).hasLength(3); |
| 139 | assertNativeMessage(conversationMessages[0], firstMessage.getText(), 1, 1000); |
| 140 | assertNativeMessage(conversationMessages[1], secondMessage.getText(), 1, 0); |
| 141 | assertNativeMessage(conversationMessages[2], thirdMessage.getText(), 1, 2000); |
| 142 | } |
| 143 | |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 144 | @Test |
| 145 | public void testDeduplicateActions() { |
| 146 | Bundle phoneExtras = new Bundle(); |
| 147 | Intent phoneIntent = new Intent(); |
| 148 | phoneIntent.setComponent(new ComponentName("phone", "intent")); |
| 149 | ExtrasUtils.putActionIntent(phoneExtras, phoneIntent); |
| 150 | |
| 151 | Bundle anotherPhoneExtras = new Bundle(); |
| 152 | Intent anotherPhoneIntent = new Intent(); |
| 153 | anotherPhoneIntent.setComponent(new ComponentName("phone", "another.intent")); |
| 154 | ExtrasUtils.putActionIntent(anotherPhoneExtras, anotherPhoneIntent); |
| 155 | |
| 156 | Bundle urlExtras = new Bundle(); |
| 157 | Intent urlIntent = new Intent(); |
| 158 | urlIntent.setComponent(new ComponentName("url", "intent")); |
| 159 | ExtrasUtils.putActionIntent(urlExtras, urlIntent); |
| 160 | |
| 161 | PendingIntent pendingIntent = PendingIntent.getActivity( |
| 162 | InstrumentationRegistry.getTargetContext(), |
| 163 | 0, |
| 164 | phoneIntent, |
| 165 | 0); |
| 166 | Icon icon = Icon.createWithData(new byte[0], 0, 0); |
| 167 | ConversationAction action = |
| 168 | new ConversationAction.Builder(ConversationAction.TYPE_CALL_PHONE) |
| 169 | .setAction(new RemoteAction(icon, "label", "1", pendingIntent)) |
| 170 | .setExtras(phoneExtras) |
| 171 | .build(); |
| 172 | ConversationAction actionWithSameLabel = |
| 173 | new ConversationAction.Builder(ConversationAction.TYPE_CALL_PHONE) |
| 174 | .setAction(new RemoteAction( |
| 175 | icon, "label", "2", pendingIntent)) |
| 176 | .setExtras(phoneExtras) |
| 177 | .build(); |
| 178 | ConversationAction actionWithSamePackageButDifferentClass = |
| 179 | new ConversationAction.Builder(ConversationAction.TYPE_CALL_PHONE) |
| 180 | .setAction(new RemoteAction( |
| 181 | icon, "label", "3", pendingIntent)) |
| 182 | .setExtras(anotherPhoneExtras) |
| 183 | .build(); |
| 184 | ConversationAction actionWithDifferentLabel = |
| 185 | new ConversationAction.Builder(ConversationAction.TYPE_CALL_PHONE) |
| 186 | .setAction(new RemoteAction( |
| 187 | icon, "another_label", "4", pendingIntent)) |
| 188 | .setExtras(phoneExtras) |
| 189 | .build(); |
| 190 | ConversationAction actionWithDifferentPackage = |
| 191 | new ConversationAction.Builder(ConversationAction.TYPE_OPEN_URL) |
| 192 | .setAction(new RemoteAction(icon, "label", "5", pendingIntent)) |
| 193 | .setExtras(urlExtras) |
| 194 | .build(); |
| 195 | ConversationAction actionWithoutRemoteAction = |
| 196 | new ConversationAction.Builder(ConversationAction.TYPE_CREATE_REMINDER) |
| 197 | .build(); |
| 198 | |
| 199 | List<ConversationAction> conversationActions = |
| 200 | ActionsSuggestionsHelper.removeActionsWithDuplicates( |
| 201 | Arrays.asList(action, actionWithSameLabel, |
| 202 | actionWithSamePackageButDifferentClass, actionWithDifferentLabel, |
| 203 | actionWithDifferentPackage, actionWithoutRemoteAction)); |
| 204 | |
| 205 | assertThat(conversationActions).hasSize(3); |
| 206 | assertThat(conversationActions.get(0).getAction().getContentDescription()).isEqualTo("4"); |
| 207 | assertThat(conversationActions.get(1).getAction().getContentDescription()).isEqualTo("5"); |
| 208 | assertThat(conversationActions.get(2).getAction()).isNull(); |
| 209 | } |
| 210 | |
Tony Mak | 82e6002 | 2019-06-05 11:53:28 +0100 | [diff] [blame] | 211 | @Test |
| 212 | public void testDeduplicateActions_nullComponent() { |
| 213 | Bundle phoneExtras = new Bundle(); |
| 214 | Intent phoneIntent = new Intent(Intent.ACTION_DIAL); |
| 215 | ExtrasUtils.putActionIntent(phoneExtras, phoneIntent); |
| 216 | PendingIntent pendingIntent = PendingIntent.getActivity( |
| 217 | InstrumentationRegistry.getTargetContext(), |
| 218 | 0, |
| 219 | phoneIntent, |
| 220 | 0); |
| 221 | Icon icon = Icon.createWithData(new byte[0], 0, 0); |
| 222 | ConversationAction action = |
| 223 | new ConversationAction.Builder(ConversationAction.TYPE_CALL_PHONE) |
| 224 | .setAction(new RemoteAction(icon, "label", "1", pendingIntent)) |
| 225 | .setExtras(phoneExtras) |
| 226 | .build(); |
| 227 | ConversationAction actionWithSameLabel = |
| 228 | new ConversationAction.Builder(ConversationAction.TYPE_CALL_PHONE) |
| 229 | .setAction(new RemoteAction( |
| 230 | icon, "label", "2", pendingIntent)) |
| 231 | .setExtras(phoneExtras) |
| 232 | .build(); |
| 233 | |
| 234 | List<ConversationAction> conversationActions = |
| 235 | ActionsSuggestionsHelper.removeActionsWithDuplicates( |
| 236 | Arrays.asList(action, actionWithSameLabel)); |
| 237 | |
| 238 | assertThat(conversationActions).isEmpty(); |
| 239 | } |
| 240 | |
Colin Cross | 5ea7fb7 | 2019-12-18 17:16:36 -0800 | [diff] [blame] | 241 | @Test |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 242 | public void createLabeledIntentResult_null() { |
| 243 | ActionsSuggestionsModel.ActionSuggestion nativeSuggestion = |
| 244 | new ActionsSuggestionsModel.ActionSuggestion( |
| 245 | "text", |
| 246 | ConversationAction.TYPE_OPEN_URL, |
| 247 | 1.0f, |
| 248 | null, |
Tony Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 249 | null, |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 250 | null |
| 251 | ); |
| 252 | |
| 253 | LabeledIntent.Result labeledIntentResult = |
| 254 | ActionsSuggestionsHelper.createLabeledIntentResult( |
| 255 | InstrumentationRegistry.getTargetContext(), |
| 256 | new TemplateIntentFactory(), |
| 257 | nativeSuggestion); |
| 258 | |
| 259 | assertThat(labeledIntentResult).isNull(); |
| 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void createLabeledIntentResult_emptyList() { |
| 264 | ActionsSuggestionsModel.ActionSuggestion nativeSuggestion = |
| 265 | new ActionsSuggestionsModel.ActionSuggestion( |
| 266 | "text", |
| 267 | ConversationAction.TYPE_OPEN_URL, |
| 268 | 1.0f, |
| 269 | null, |
Tony Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 270 | null, |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 271 | new RemoteActionTemplate[0] |
| 272 | ); |
| 273 | |
| 274 | LabeledIntent.Result labeledIntentResult = |
| 275 | ActionsSuggestionsHelper.createLabeledIntentResult( |
| 276 | InstrumentationRegistry.getTargetContext(), |
| 277 | new TemplateIntentFactory(), |
| 278 | nativeSuggestion); |
| 279 | |
| 280 | assertThat(labeledIntentResult).isNull(); |
| 281 | } |
| 282 | |
| 283 | @Test |
| 284 | public void createLabeledIntentResult() { |
| 285 | ActionsSuggestionsModel.ActionSuggestion nativeSuggestion = |
| 286 | new ActionsSuggestionsModel.ActionSuggestion( |
| 287 | "text", |
| 288 | ConversationAction.TYPE_OPEN_URL, |
| 289 | 1.0f, |
| 290 | null, |
Tony Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 291 | null, |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 292 | new RemoteActionTemplate[]{ |
| 293 | new RemoteActionTemplate( |
| 294 | "title", |
| 295 | null, |
| 296 | "description", |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 297 | null, |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 298 | Intent.ACTION_VIEW, |
| 299 | Uri.parse("http://www.android.com").toString(), |
| 300 | null, |
| 301 | 0, |
| 302 | null, |
| 303 | null, |
| 304 | null, |
| 305 | 0)}); |
| 306 | |
| 307 | LabeledIntent.Result labeledIntentResult = |
| 308 | ActionsSuggestionsHelper.createLabeledIntentResult( |
| 309 | InstrumentationRegistry.getTargetContext(), |
| 310 | new TemplateIntentFactory(), |
| 311 | nativeSuggestion); |
| 312 | |
| 313 | assertThat(labeledIntentResult.remoteAction.getTitle()).isEqualTo("title"); |
| 314 | assertThat(labeledIntentResult.resolvedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); |
| 315 | } |
| 316 | |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 317 | private ZonedDateTime createZonedDateTimeFromMsUtc(long msUtc) { |
| 318 | return ZonedDateTime.ofInstant(Instant.ofEpochMilli(msUtc), ZoneId.of("UTC")); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | private static void assertNativeMessage( |
| 322 | ActionsSuggestionsModel.ConversationMessage nativeMessage, |
| 323 | CharSequence text, |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 324 | int userId, |
| 325 | long referenceTimeInMsUtc) { |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 326 | assertThat(nativeMessage.getText()).isEqualTo(text.toString()); |
| 327 | assertThat(nativeMessage.getUserId()).isEqualTo(userId); |
Tony Mak | 159f028 | 2019-03-01 14:03:25 +0000 | [diff] [blame] | 328 | assertThat(nativeMessage.getDetectedTextLanguageTags()).isEqualTo(LOCALE_TAG); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 329 | assertThat(nativeMessage.getReferenceTimeMsUtc()).isEqualTo(referenceTimeInMsUtc); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 330 | } |
| 331 | } |