blob: ec7e83f1f24399997401533391d7c971d946dce9 [file] [log] [blame]
Tony Makf99ee172018-11-23 12:14:39 +00001/*
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
17package android.view.textclassifier;
18
Tony Mak91daa152019-01-24 16:00:28 +000019import static android.view.textclassifier.ConversationActions.Message.PERSON_USER_OTHERS;
20import static android.view.textclassifier.ConversationActions.Message.PERSON_USER_SELF;
Tony Mak82fa8d92018-12-07 17:37:43 +000021
Tony Makf99ee172018-11-23 12:14:39 +000022import static com.google.common.truth.Truth.assertThat;
23
Tony Makc12035e2019-02-26 17:45:34 +000024import android.app.PendingIntent;
Tony Makf99ee172018-11-23 12:14:39 +000025import android.app.Person;
Tony Makc12035e2019-02-26 17:45:34 +000026import android.app.RemoteAction;
27import android.content.ComponentName;
28import android.content.Intent;
29import android.graphics.drawable.Icon;
Tony Mak8ab9b182019-03-01 16:44:17 +000030import android.net.Uri;
Tony Makc12035e2019-02-26 17:45:34 +000031import android.os.Bundle;
Tony Mak8ab9b182019-03-01 16:44:17 +000032import android.view.textclassifier.intent.LabeledIntent;
33import android.view.textclassifier.intent.TemplateIntentFactory;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090034
Tony Makc12035e2019-02-26 17:45:34 +000035import androidx.test.InstrumentationRegistry;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090036import androidx.test.filters.SmallTest;
37import androidx.test.runner.AndroidJUnit4;
Tony Makf99ee172018-11-23 12:14:39 +000038
39import com.google.android.textclassifier.ActionsSuggestionsModel;
Tony Mak8ab9b182019-03-01 16:44:17 +000040import com.google.android.textclassifier.RemoteActionTemplate;
Tony Makf99ee172018-11-23 12:14:39 +000041
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
Tony Mak82fa8d92018-12-07 17:37:43 +000045import java.time.Instant;
46import java.time.ZoneId;
47import java.time.ZonedDateTime;
Tony Makf99ee172018-11-23 12:14:39 +000048import java.util.Arrays;
49import java.util.Collections;
Tony Makc12035e2019-02-26 17:45:34 +000050import java.util.List;
Tony Mak82fa8d92018-12-07 17:37:43 +000051import java.util.Locale;
52import java.util.function.Function;
Tony Makf99ee172018-11-23 12:14:39 +000053
54@SmallTest
55@RunWith(AndroidJUnit4.class)
56public class ActionsSuggestionsHelperTest {
Tony Mak82fa8d92018-12-07 17:37:43 +000057 private static final String LOCALE_TAG = Locale.US.toLanguageTag();
58 private static final Function<CharSequence, String> LANGUAGE_DETECTOR =
59 charSequence -> LOCALE_TAG;
60
Tony Makf99ee172018-11-23 12:14:39 +000061 @Test
62 public void testToNativeMessages_emptyInput() {
63 ActionsSuggestionsModel.ConversationMessage[] conversationMessages =
Tony Mak82fa8d92018-12-07 17:37:43 +000064 ActionsSuggestionsHelper.toNativeMessages(
65 Collections.emptyList(), LANGUAGE_DETECTOR);
Tony Makf99ee172018-11-23 12:14:39 +000066
67 assertThat(conversationMessages).isEmpty();
68 }
69
70 @Test
71 public void testToNativeMessages_noTextMessages() {
72 ConversationActions.Message messageWithoutText =
Tony Mak91daa152019-01-24 16:00:28 +000073 new ConversationActions.Message.Builder(PERSON_USER_OTHERS).build();
Tony Makf99ee172018-11-23 12:14:39 +000074
75 ActionsSuggestionsModel.ConversationMessage[] conversationMessages =
76 ActionsSuggestionsHelper.toNativeMessages(
Tony Mak82fa8d92018-12-07 17:37:43 +000077 Collections.singletonList(messageWithoutText), LANGUAGE_DETECTOR);
Tony Makf99ee172018-11-23 12:14:39 +000078
79 assertThat(conversationMessages).isEmpty();
80 }
81
82 @Test
Tony Makf99ee172018-11-23 12:14:39 +000083 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 Mak82fa8d92018-12-07 17:37:43 +000088 new ConversationActions.Message.Builder(userB)
Tony Makf99ee172018-11-23 12:14:39 +000089 .setText("first")
Tony Makf99ee172018-11-23 12:14:39 +000090 .build();
91 ConversationActions.Message secondMessage =
Tony Mak82fa8d92018-12-07 17:37:43 +000092 new ConversationActions.Message.Builder(userA)
Tony Makf99ee172018-11-23 12:14:39 +000093 .setText("second")
Tony Makf99ee172018-11-23 12:14:39 +000094 .build();
95 ConversationActions.Message thirdMessage =
Tony Mak91daa152019-01-24 16:00:28 +000096 new ConversationActions.Message.Builder(PERSON_USER_SELF)
Tony Makf99ee172018-11-23 12:14:39 +000097 .setText("third")
Tony Makf99ee172018-11-23 12:14:39 +000098 .build();
99 ConversationActions.Message fourthMessage =
Tony Mak82fa8d92018-12-07 17:37:43 +0000100 new ConversationActions.Message.Builder(userA)
Tony Makf99ee172018-11-23 12:14:39 +0000101 .setText("fourth")
Tony Makf99ee172018-11-23 12:14:39 +0000102 .build();
103
104 ActionsSuggestionsModel.ConversationMessage[] conversationMessages =
105 ActionsSuggestionsHelper.toNativeMessages(
Tony Mak82fa8d92018-12-07 17:37:43 +0000106 Arrays.asList(firstMessage, secondMessage, thirdMessage, fourthMessage),
107 LANGUAGE_DETECTOR);
Tony Makf99ee172018-11-23 12:14:39 +0000108
109 assertThat(conversationMessages).hasLength(4);
Tony Mak82fa8d92018-12-07 17:37:43 +0000110 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 Mak91daa152019-01-24 16:00:28 +0000119 new ConversationActions.Message.Builder(PERSON_USER_OTHERS)
Tony Mak82fa8d92018-12-07 17:37:43 +0000120 .setText("first")
121 .setReferenceTime(createZonedDateTimeFromMsUtc(1000))
122 .build();
123 ConversationActions.Message secondMessage =
Tony Mak91daa152019-01-24 16:00:28 +0000124 new ConversationActions.Message.Builder(PERSON_USER_OTHERS)
Tony Mak82fa8d92018-12-07 17:37:43 +0000125 .setText("second")
126 .build();
127 ConversationActions.Message thirdMessage =
Tony Mak91daa152019-01-24 16:00:28 +0000128 new ConversationActions.Message.Builder(PERSON_USER_OTHERS)
Tony Mak82fa8d92018-12-07 17:37:43 +0000129 .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 Makc12035e2019-02-26 17:45:34 +0000144 @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 Mak82e60022019-06-05 11:53:28 +0100211 @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 Cross5ea7fb72019-12-18 17:16:36 -0800241 @Test
Tony Mak8ab9b182019-03-01 16:44:17 +0000242 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 Mak2a3adb72019-03-20 17:55:53 +0000249 null,
Tony Mak8ab9b182019-03-01 16:44:17 +0000250 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 Mak2a3adb72019-03-20 17:55:53 +0000270 null,
Tony Mak8ab9b182019-03-01 16:44:17 +0000271 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 Mak2a3adb72019-03-20 17:55:53 +0000291 null,
Tony Mak8ab9b182019-03-01 16:44:17 +0000292 new RemoteActionTemplate[]{
293 new RemoteActionTemplate(
294 "title",
295 null,
296 "description",
Tony Mak15b64be2019-04-01 20:02:29 +0100297 null,
Tony Mak8ab9b182019-03-01 16:44:17 +0000298 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 Mak82fa8d92018-12-07 17:37:43 +0000317 private ZonedDateTime createZonedDateTimeFromMsUtc(long msUtc) {
318 return ZonedDateTime.ofInstant(Instant.ofEpochMilli(msUtc), ZoneId.of("UTC"));
Tony Makf99ee172018-11-23 12:14:39 +0000319 }
320
321 private static void assertNativeMessage(
322 ActionsSuggestionsModel.ConversationMessage nativeMessage,
323 CharSequence text,
Tony Mak82fa8d92018-12-07 17:37:43 +0000324 int userId,
325 long referenceTimeInMsUtc) {
Tony Makf99ee172018-11-23 12:14:39 +0000326 assertThat(nativeMessage.getText()).isEqualTo(text.toString());
327 assertThat(nativeMessage.getUserId()).isEqualTo(userId);
Tony Mak159f0282019-03-01 14:03:25 +0000328 assertThat(nativeMessage.getDetectedTextLanguageTags()).isEqualTo(LOCALE_TAG);
Tony Mak82fa8d92018-12-07 17:37:43 +0000329 assertThat(nativeMessage.getReferenceTimeMsUtc()).isEqualTo(referenceTimeInMsUtc);
Tony Makf99ee172018-11-23 12:14:39 +0000330 }
331}