blob: c8a53cc86347c266768b282d7e18f944e6ae80df [file] [log] [blame]
Abodunrinwa Toki65b25272017-04-13 17:25:59 +01001/*
2 * Copyright (C) 2017 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
Richard Ledleydb18a572017-11-30 17:33:51 +000019import static org.hamcrest.CoreMatchers.not;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010020import static org.junit.Assert.assertEquals;
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010021import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertNotSame;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010023import static org.junit.Assert.assertThat;
Jan Althaus108aad32018-01-30 15:26:55 +010024import static org.junit.Assert.assertTrue;
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010025import static org.mockito.Matchers.argThat;
26import static org.mockito.Mockito.any;
27import static org.mockito.Mockito.anyInt;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010028import static org.mockito.Mockito.mock;
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010029import static org.mockito.Mockito.when;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010030
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000031import android.content.Context;
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010032import android.content.ContextWrapper;
33import android.content.Intent;
34import android.content.pm.PackageManager;
35import android.content.pm.ResolveInfo;
36import android.net.Uri;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010037import android.os.LocaleList;
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000038import android.service.textclassifier.TextClassifierService;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010039import android.support.test.InstrumentationRegistry;
40import android.support.test.filters.SmallTest;
41import android.support.test.runner.AndroidJUnit4;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010042
43import org.hamcrest.BaseMatcher;
44import org.hamcrest.Description;
45import org.hamcrest.Matcher;
46import org.junit.Before;
47import org.junit.Test;
48import org.junit.runner.RunWith;
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010049import org.mockito.ArgumentMatcher;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010050
Jan Althaus108aad32018-01-30 15:26:55 +010051import java.util.Arrays;
Richard Ledley1fc998b2018-02-16 15:45:06 +000052import java.util.Collections;
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010053import java.util.List;
Jan Althaus108aad32018-01-30 15:26:55 +010054
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010055@SmallTest
56@RunWith(AndroidJUnit4.class)
57public class TextClassificationManagerTest {
58
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +010059 private static final LocaleList LOCALES = LocaleList.forLanguageTags("en-US");
Abodunrinwa Tokib4162972017-05-05 18:07:17 +010060 private static final String NO_TYPE = null;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010061
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000062 private Context mContext;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010063 private TextClassificationManager mTcm;
64 private TextClassifier mClassifier;
65
66 @Before
67 public void setup() {
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000068 mContext = InstrumentationRegistry.getTargetContext();
69 mTcm = mContext.getSystemService(TextClassificationManager.class);
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010070 // Test with the local textClassifier only. (We only bundle "en" model by default).
71 // It's hard to reliably test the results of the device's TextClassifierServiceImpl here.
72 mClassifier = mTcm.getTextClassifier(TextClassifier.LOCAL);
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010073 }
74
75 @Test
76 public void testSmartSelection() {
77 if (isTextClassifierDisabled()) return;
78
79 String text = "Contact me at droid@android.com";
80 String selected = "droid";
81 String suggested = "droid@android.com";
82 int startIndex = text.indexOf(selected);
83 int endIndex = startIndex + selected.length();
84 int smartStartIndex = text.indexOf(suggested);
85 int smartEndIndex = smartStartIndex + suggested.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010086 TextSelection.Request request = new TextSelection.Request.Builder(
87 text, startIndex, endIndex)
88 .setDefaultLocales(LOCALES)
89 .build();
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010090
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010091 TextSelection selection = mClassifier.suggestSelection(request);
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +000092 assertThat(selection,
Abodunrinwa Tokia2df6e52017-04-13 09:56:52 +010093 isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_EMAIL));
94 }
95
96 @Test
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010097 public void testSmartSelection_url() {
98 if (isTextClassifierDisabled()) return;
99
100 String text = "Visit http://www.android.com for more information";
101 String selected = "http";
102 String suggested = "http://www.android.com";
103 int startIndex = text.indexOf(selected);
104 int endIndex = startIndex + selected.length();
105 int smartStartIndex = text.indexOf(suggested);
106 int smartEndIndex = smartStartIndex + suggested.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100107 TextSelection.Request request = new TextSelection.Request.Builder(
108 text, startIndex, endIndex)
109 .setDefaultLocales(LOCALES)
110 .build();
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100111
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100112 TextSelection selection = mClassifier.suggestSelection(request);
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000113 assertThat(selection,
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100114 isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_URL));
115 }
116
117 @Test
Abodunrinwa Tokib4162972017-05-05 18:07:17 +0100118 public void testSmartSelection_withEmoji() {
119 if (isTextClassifierDisabled()) return;
120
121 String text = "\uD83D\uDE02 Hello.";
122 String selected = "Hello";
123 int startIndex = text.indexOf(selected);
124 int endIndex = startIndex + selected.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100125 TextSelection.Request request = new TextSelection.Request.Builder(
126 text, startIndex, endIndex)
127 .setDefaultLocales(LOCALES)
128 .build();
Abodunrinwa Tokib4162972017-05-05 18:07:17 +0100129
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100130 TextSelection selection = mClassifier.suggestSelection(request);
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000131 assertThat(selection,
Abodunrinwa Tokib4162972017-05-05 18:07:17 +0100132 isTextSelection(startIndex, endIndex, NO_TYPE));
133 }
134
135 @Test
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +0100136 public void testClassifyText() {
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100137 if (isTextClassifierDisabled()) return;
138
139 String text = "Contact me at droid@android.com";
140 String classifiedText = "droid@android.com";
141 int startIndex = text.indexOf(classifiedText);
142 int endIndex = startIndex + classifiedText.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100143 TextClassification.Request request = new TextClassification.Request.Builder(
144 text, startIndex, endIndex)
145 .setDefaultLocales(LOCALES)
146 .build();
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000147
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100148 TextClassification classification = mClassifier.classifyText(request);
Jan Althaus20d346e2018-03-23 14:03:52 +0100149 assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_EMAIL));
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100150 }
151
152 @Test
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +0100153 public void testTextClassifyText_url() {
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100154 if (isTextClassifierDisabled()) return;
155
Abodunrinwa Toki86ef9822017-05-11 20:05:50 +0100156 String text = "Visit www.android.com for more information";
Abodunrinwa Toki70d41cd2017-05-02 21:43:41 +0100157 String classifiedText = "www.android.com";
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100158 int startIndex = text.indexOf(classifiedText);
159 int endIndex = startIndex + classifiedText.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100160 TextClassification.Request request = new TextClassification.Request.Builder(
161 text, startIndex, endIndex)
162 .setDefaultLocales(LOCALES)
163 .build();
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000164
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100165 TextClassification classification = mClassifier.classifyText(request);
Jan Althaus20d346e2018-03-23 14:03:52 +0100166 assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_URL));
Abodunrinwa Toki86ef9822017-05-11 20:05:50 +0100167 }
168
169 @Test
Jan Althauseaff57e2018-02-12 12:47:27 +0100170 public void testTextClassifyText_address() {
171 if (isTextClassifierDisabled()) return;
172
173 String text = "Brandschenkestrasse 110, Zürich, Switzerland";
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100174 TextClassification.Request request = new TextClassification.Request.Builder(
175 text, 0, text.length())
176 .setDefaultLocales(LOCALES)
177 .build();
178
179 TextClassification classification = mClassifier.classifyText(request);
Jan Althaus20d346e2018-03-23 14:03:52 +0100180 assertThat(classification, isTextClassification(text, TextClassifier.TYPE_ADDRESS));
Jan Althauseaff57e2018-02-12 12:47:27 +0100181 }
182
183 @Test
Abodunrinwa Toki86ef9822017-05-11 20:05:50 +0100184 public void testTextClassifyText_url_inCaps() {
185 if (isTextClassifierDisabled()) return;
186
187 String text = "Visit HTTP://ANDROID.COM for more information";
188 String classifiedText = "HTTP://ANDROID.COM";
189 int startIndex = text.indexOf(classifiedText);
190 int endIndex = startIndex + classifiedText.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100191 TextClassification.Request request = new TextClassification.Request.Builder(
192 text, startIndex, endIndex)
193 .setDefaultLocales(LOCALES)
194 .build();
Abodunrinwa Toki4d232d62017-11-23 12:22:45 +0000195
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100196 TextClassification classification = mClassifier.classifyText(request);
Jan Althaus20d346e2018-03-23 14:03:52 +0100197 assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_URL));
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100198 }
199
200 @Test
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +0100201 public void testTextClassifyText_date() {
202 if (isTextClassifierDisabled()) return;
203
204 String text = "Let's meet on January 9, 2018.";
205 String classifiedText = "January 9, 2018";
206 int startIndex = text.indexOf(classifiedText);
207 int endIndex = startIndex + classifiedText.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100208 TextClassification.Request request = new TextClassification.Request.Builder(
209 text, startIndex, endIndex)
210 .setDefaultLocales(LOCALES)
211 .build();
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +0100212
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100213 TextClassification classification = mClassifier.classifyText(request);
Jan Althaus20d346e2018-03-23 14:03:52 +0100214 assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_DATE));
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +0100215 }
216
217 @Test
218 public void testTextClassifyText_datetime() {
219 if (isTextClassifierDisabled()) return;
220
221 String text = "Let's meet 2018/01/01 10:30:20.";
222 String classifiedText = "2018/01/01 10:30:20";
223 int startIndex = text.indexOf(classifiedText);
224 int endIndex = startIndex + classifiedText.length();
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100225 TextClassification.Request request = new TextClassification.Request.Builder(
226 text, startIndex, endIndex)
227 .setDefaultLocales(LOCALES)
228 .build();
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +0100229
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100230 TextClassification classification = mClassifier.classifyText(request);
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +0100231 assertThat(classification,
Jan Althaus20d346e2018-03-23 14:03:52 +0100232 isTextClassification(classifiedText, TextClassifier.TYPE_DATE_TIME));
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +0100233 }
234
235 @Test
Richard Ledleydb18a572017-11-30 17:33:51 +0000236 public void testGenerateLinks_phone() {
Richard Ledley68d94522017-10-05 10:52:19 +0100237 if (isTextClassifierDisabled()) return;
Richard Ledleydb18a572017-11-30 17:33:51 +0000238 String text = "The number is +12122537077. See you tonight!";
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100239 TextLinks.Request request = new TextLinks.Request.Builder(text).build();
240 assertThat(mClassifier.generateLinks(request),
Richard Ledleydb18a572017-11-30 17:33:51 +0000241 isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE));
242 }
Richard Ledley68d94522017-10-05 10:52:19 +0100243
Richard Ledleydb18a572017-11-30 17:33:51 +0000244 @Test
245 public void testGenerateLinks_exclude() {
246 if (isTextClassifierDisabled()) return;
247 String text = "The number is +12122537077. See you tonight!";
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100248 List<String> hints = Collections.EMPTY_LIST;
249 List<String> included = Collections.EMPTY_LIST;
250 List<String> excluded = Arrays.asList(TextClassifier.TYPE_PHONE);
251 TextLinks.Request request = new TextLinks.Request.Builder(text)
252 .setEntityConfig(TextClassifier.EntityConfig.create(hints, included, excluded))
253 .setDefaultLocales(LOCALES)
254 .build();
255 assertThat(mClassifier.generateLinks(request),
Richard Ledleydb18a572017-11-30 17:33:51 +0000256 not(isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE)));
257 }
Richard Ledley68d94522017-10-05 10:52:19 +0100258
Richard Ledleydb18a572017-11-30 17:33:51 +0000259 @Test
Richard Ledley1fc998b2018-02-16 15:45:06 +0000260 public void testGenerateLinks_explicit_address() {
Richard Ledleydb18a572017-11-30 17:33:51 +0000261 if (isTextClassifierDisabled()) return;
Richard Ledley1fc998b2018-02-16 15:45:06 +0000262 String text = "The address is 1600 Amphitheater Parkway, Mountain View, CA. See you!";
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100263 List<String> explicit = Arrays.asList(TextClassifier.TYPE_ADDRESS);
264 TextLinks.Request request = new TextLinks.Request.Builder(text)
265 .setEntityConfig(TextClassifier.EntityConfig.createWithExplicitEntityList(explicit))
266 .setDefaultLocales(LOCALES)
267 .build();
268 assertThat(mClassifier.generateLinks(request),
Richard Ledley1fc998b2018-02-16 15:45:06 +0000269 isTextLinksContaining(text, "1600 Amphitheater Parkway, Mountain View, CA",
270 TextClassifier.TYPE_ADDRESS));
271 }
Jan Althaus108aad32018-01-30 15:26:55 +0100272
Richard Ledley1fc998b2018-02-16 15:45:06 +0000273 @Test
274 public void testGenerateLinks_exclude_override() {
275 if (isTextClassifierDisabled()) return;
Richard Ledleydb18a572017-11-30 17:33:51 +0000276 String text = "The number is +12122537077. See you tonight!";
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100277 List<String> hints = Collections.EMPTY_LIST;
278 List<String> included = Arrays.asList(TextClassifier.TYPE_PHONE);
279 List<String> excluded = Arrays.asList(TextClassifier.TYPE_PHONE);
280 TextLinks.Request request = new TextLinks.Request.Builder(text)
281 .setEntityConfig(TextClassifier.EntityConfig.create(hints, included, excluded))
282 .setDefaultLocales(LOCALES)
283 .build();
284 assertThat(mClassifier.generateLinks(request),
Richard Ledleydb18a572017-11-30 17:33:51 +0000285 not(isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE)));
286 }
Richard Ledley68d94522017-10-05 10:52:19 +0100287
Richard Ledleydb18a572017-11-30 17:33:51 +0000288 @Test
Jan Althaus108aad32018-01-30 15:26:55 +0100289 public void testGenerateLinks_maxLength() {
290 if (isTextClassifierDisabled()) return;
291 char[] manySpaces = new char[mClassifier.getMaxGenerateLinksTextLength()];
292 Arrays.fill(manySpaces, ' ');
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100293 TextLinks.Request request = new TextLinks.Request.Builder(new String(manySpaces)).build();
294 TextLinks links = mClassifier.generateLinks(request);
Jan Althaus108aad32018-01-30 15:26:55 +0100295 assertTrue(links.getLinks().isEmpty());
296 }
297
298 @Test(expected = IllegalArgumentException.class)
299 public void testGenerateLinks_tooLong() {
300 if (isTextClassifierDisabled()) {
301 throw new IllegalArgumentException("pass if disabled");
302 }
303 char[] manySpaces = new char[mClassifier.getMaxGenerateLinksTextLength() + 1];
304 Arrays.fill(manySpaces, ' ');
Abodunrinwa Toki080c8542018-03-27 00:04:06 +0100305 TextLinks.Request request = new TextLinks.Request.Builder(new String(manySpaces)).build();
306 mClassifier.generateLinks(request);
Jan Althaus108aad32018-01-30 15:26:55 +0100307 }
308
309 @Test
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100310 public void testSetTextClassifier() {
311 TextClassifier classifier = mock(TextClassifier.class);
312 mTcm.setTextClassifier(classifier);
313 assertEquals(classifier, mTcm.getTextClassifier());
314 }
315
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +0000316 @Test
317 public void testGetLocalTextClassifier() {
318 assertTrue(mTcm.getTextClassifier(TextClassifier.LOCAL) instanceof TextClassifierImpl);
319 }
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +0000320 @Test
321 public void testGetSystemTextClassifier() {
322 assertTrue(
323 TextClassifierService.getServiceComponentName(mContext) == null
324 || mTcm.getTextClassifier(TextClassifier.SYSTEM) instanceof SystemTextClassifier);
325 }
326
Abodunrinwa Toki253827f2018-04-24 19:19:48 +0100327 @Test
328 public void testCannotResolveIntent() {
329 final PackageManager fakePackageMgr = mock(PackageManager.class);
330
331 ResolveInfo validInfo = mContext.getPackageManager().resolveActivity(
332 new Intent(Intent.ACTION_DIAL).setData(Uri.parse("tel:+12122537077")), 0);
333 // Make packageManager fail when it gets the following intent:
334 ArgumentMatcher<Intent> toFailIntent =
335 intent -> intent.getAction().equals(Intent.ACTION_INSERT_OR_EDIT);
336
337 when(fakePackageMgr.resolveActivity(any(Intent.class), anyInt())).thenReturn(validInfo);
338 when(fakePackageMgr.resolveActivity(argThat(toFailIntent), anyInt())).thenReturn(null);
339
340 ContextWrapper fakeContext = new ContextWrapper(mContext) {
341 @Override
342 public PackageManager getPackageManager() {
343 return fakePackageMgr;
344 }
345 };
346
347 TextClassifier fallback = TextClassifier.NO_OP;
348 TextClassifier classifier = new TextClassifierImpl(
349 fakeContext, TextClassificationConstants.loadFromString(null), fallback);
350
351 String text = "Contact me at +12122537077";
352 String classifiedText = "+12122537077";
353 int startIndex = text.indexOf(classifiedText);
354 int endIndex = startIndex + classifiedText.length();
355 TextClassification.Request request = new TextClassification.Request.Builder(
356 text, startIndex, endIndex)
357 .setDefaultLocales(LOCALES)
358 .build();
359
360 TextClassification result = classifier.classifyText(request);
361 TextClassification fallbackResult = fallback.classifyText(request);
362
363 // classifier should not totally fail in which case it returns a fallback result.
364 // It should skip the failing intent and return a result for non-failing intents.
365 assertFalse(result.getActions().isEmpty());
366 assertNotSame(result, fallbackResult);
367 }
368
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100369 private boolean isTextClassifierDisabled() {
370 return mClassifier == TextClassifier.NO_OP;
371 }
372
373 private static Matcher<TextSelection> isTextSelection(
374 final int startIndex, final int endIndex, final String type) {
375 return new BaseMatcher<TextSelection>() {
376 @Override
377 public boolean matches(Object o) {
378 if (o instanceof TextSelection) {
379 TextSelection selection = (TextSelection) o;
380 return startIndex == selection.getSelectionStartIndex()
381 && endIndex == selection.getSelectionEndIndex()
Abodunrinwa Tokib4162972017-05-05 18:07:17 +0100382 && typeMatches(selection, type);
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100383 }
384 return false;
385 }
386
Abodunrinwa Tokib4162972017-05-05 18:07:17 +0100387 private boolean typeMatches(TextSelection selection, String type) {
388 return type == null
389 || (selection.getEntityCount() > 0
390 && type.trim().equalsIgnoreCase(selection.getEntity(0)));
391 }
392
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100393 @Override
394 public void describeTo(Description description) {
395 description.appendValue(
396 String.format("%d, %d, %s", startIndex, endIndex, type));
397 }
398 };
399 }
400
Richard Ledleydb18a572017-11-30 17:33:51 +0000401 private static Matcher<TextLinks> isTextLinksContaining(
402 final String text, final String substring, final String type) {
403 return new BaseMatcher<TextLinks>() {
404
405 @Override
406 public void describeTo(Description description) {
407 description.appendText("text=").appendValue(text)
408 .appendText(", substring=").appendValue(substring)
409 .appendText(", type=").appendValue(type);
410 }
411
412 @Override
413 public boolean matches(Object o) {
414 if (o instanceof TextLinks) {
415 for (TextLinks.TextLink link : ((TextLinks) o).getLinks()) {
416 if (text.subSequence(link.getStart(), link.getEnd()).equals(substring)) {
417 return type.equals(link.getEntity(0));
418 }
419 }
420 }
421 return false;
422 }
423 };
424 }
425
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +0100426 private static Matcher<TextClassification> isTextClassification(
Jan Althaus20d346e2018-03-23 14:03:52 +0100427 final String text, final String type) {
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +0100428 return new BaseMatcher<TextClassification>() {
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100429 @Override
430 public boolean matches(Object o) {
Abodunrinwa Tokie0b57892017-04-28 19:59:57 +0100431 if (o instanceof TextClassification) {
432 TextClassification result = (TextClassification) o;
Jan Althaus20d346e2018-03-23 14:03:52 +0100433 return text.equals(result.getText())
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100434 && result.getEntityCount() > 0
Jan Althaus20d346e2018-03-23 14:03:52 +0100435 && type.equals(result.getEntity(0));
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100436 }
437 return false;
438 }
439
440 @Override
441 public void describeTo(Description description) {
442 description.appendText("text=").appendValue(text)
Jan Althaus20d346e2018-03-23 14:03:52 +0100443 .appendText(", type=").appendValue(type);
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100444 }
445 };
446 }
Abodunrinwa Toki65b25272017-04-13 17:25:59 +0100447}