Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view.textclassifier; |
| 18 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 19 | import static org.hamcrest.CoreMatchers.not; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 20 | import static org.junit.Assert.assertEquals; |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertNotSame; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 23 | import static org.junit.Assert.assertThat; |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 24 | import static org.junit.Assert.assertTrue; |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 25 | import static org.mockito.Matchers.argThat; |
| 26 | import static org.mockito.Mockito.any; |
| 27 | import static org.mockito.Mockito.anyInt; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 28 | import static org.mockito.Mockito.mock; |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 29 | import static org.mockito.Mockito.when; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 30 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 31 | import android.content.Context; |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 32 | import android.content.ContextWrapper; |
| 33 | import android.content.Intent; |
| 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.pm.ResolveInfo; |
| 36 | import android.net.Uri; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 37 | import android.os.LocaleList; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 38 | import android.service.textclassifier.TextClassifierService; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 39 | import android.support.test.InstrumentationRegistry; |
| 40 | import android.support.test.filters.SmallTest; |
| 41 | import android.support.test.runner.AndroidJUnit4; |
Abodunrinwa Toki | a69950c | 2018-11-29 13:51:56 +0000 | [diff] [blame] | 42 | import android.text.Spannable; |
| 43 | import android.text.SpannableString; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 44 | |
| 45 | import org.hamcrest.BaseMatcher; |
| 46 | import org.hamcrest.Description; |
| 47 | import org.hamcrest.Matcher; |
| 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 51 | import org.mockito.ArgumentMatcher; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 52 | |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 53 | import java.util.Arrays; |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 54 | import java.util.Collections; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 55 | import java.util.List; |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 56 | |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 57 | @SmallTest |
| 58 | @RunWith(AndroidJUnit4.class) |
| 59 | public class TextClassificationManagerTest { |
| 60 | |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 61 | private static final LocaleList LOCALES = LocaleList.forLanguageTags("en-US"); |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 62 | private static final String NO_TYPE = null; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 63 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 64 | private Context mContext; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 65 | private TextClassificationManager mTcm; |
| 66 | private TextClassifier mClassifier; |
| 67 | |
| 68 | @Before |
| 69 | public void setup() { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 70 | mContext = InstrumentationRegistry.getTargetContext(); |
| 71 | mTcm = mContext.getSystemService(TextClassificationManager.class); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 72 | // Test with the local textClassifier only. (We only bundle "en" model by default). |
| 73 | // It's hard to reliably test the results of the device's TextClassifierServiceImpl here. |
| 74 | mClassifier = mTcm.getTextClassifier(TextClassifier.LOCAL); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testSmartSelection() { |
| 79 | if (isTextClassifierDisabled()) return; |
| 80 | |
| 81 | String text = "Contact me at droid@android.com"; |
| 82 | String selected = "droid"; |
| 83 | String suggested = "droid@android.com"; |
| 84 | int startIndex = text.indexOf(selected); |
| 85 | int endIndex = startIndex + selected.length(); |
| 86 | int smartStartIndex = text.indexOf(suggested); |
| 87 | int smartEndIndex = smartStartIndex + suggested.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 88 | TextSelection.Request request = new TextSelection.Request.Builder( |
| 89 | text, startIndex, endIndex) |
| 90 | .setDefaultLocales(LOCALES) |
| 91 | .build(); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 92 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 93 | TextSelection selection = mClassifier.suggestSelection(request); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 94 | assertThat(selection, |
Abodunrinwa Toki | a2df6e5 | 2017-04-13 09:56:52 +0100 | [diff] [blame] | 95 | isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_EMAIL)); |
| 96 | } |
| 97 | |
| 98 | @Test |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 99 | public void testSmartSelection_url() { |
| 100 | if (isTextClassifierDisabled()) return; |
| 101 | |
| 102 | String text = "Visit http://www.android.com for more information"; |
| 103 | String selected = "http"; |
| 104 | String suggested = "http://www.android.com"; |
| 105 | int startIndex = text.indexOf(selected); |
| 106 | int endIndex = startIndex + selected.length(); |
| 107 | int smartStartIndex = text.indexOf(suggested); |
| 108 | int smartEndIndex = smartStartIndex + suggested.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 109 | TextSelection.Request request = new TextSelection.Request.Builder( |
| 110 | text, startIndex, endIndex) |
| 111 | .setDefaultLocales(LOCALES) |
| 112 | .build(); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 113 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 114 | TextSelection selection = mClassifier.suggestSelection(request); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 115 | assertThat(selection, |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 116 | isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_URL)); |
| 117 | } |
| 118 | |
| 119 | @Test |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 120 | public void testSmartSelection_withEmoji() { |
| 121 | if (isTextClassifierDisabled()) return; |
| 122 | |
| 123 | String text = "\uD83D\uDE02 Hello."; |
| 124 | String selected = "Hello"; |
| 125 | int startIndex = text.indexOf(selected); |
| 126 | int endIndex = startIndex + selected.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 127 | TextSelection.Request request = new TextSelection.Request.Builder( |
| 128 | text, startIndex, endIndex) |
| 129 | .setDefaultLocales(LOCALES) |
| 130 | .build(); |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 131 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 132 | TextSelection selection = mClassifier.suggestSelection(request); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 133 | assertThat(selection, |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 134 | isTextSelection(startIndex, endIndex, NO_TYPE)); |
| 135 | } |
| 136 | |
| 137 | @Test |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 138 | public void testClassifyText() { |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 139 | if (isTextClassifierDisabled()) return; |
| 140 | |
| 141 | String text = "Contact me at droid@android.com"; |
| 142 | String classifiedText = "droid@android.com"; |
| 143 | int startIndex = text.indexOf(classifiedText); |
| 144 | int endIndex = startIndex + classifiedText.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 145 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 146 | text, startIndex, endIndex) |
| 147 | .setDefaultLocales(LOCALES) |
| 148 | .build(); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 149 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 150 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 151 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_EMAIL)); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Test |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 155 | public void testTextClassifyText_url() { |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 156 | if (isTextClassifierDisabled()) return; |
| 157 | |
Abodunrinwa Toki | 86ef982 | 2017-05-11 20:05:50 +0100 | [diff] [blame] | 158 | String text = "Visit www.android.com for more information"; |
Abodunrinwa Toki | 70d41cd | 2017-05-02 21:43:41 +0100 | [diff] [blame] | 159 | String classifiedText = "www.android.com"; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 160 | int startIndex = text.indexOf(classifiedText); |
| 161 | int endIndex = startIndex + classifiedText.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 162 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 163 | text, startIndex, endIndex) |
| 164 | .setDefaultLocales(LOCALES) |
| 165 | .build(); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 166 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 167 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 168 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_URL)); |
Abodunrinwa Toki | 86ef982 | 2017-05-11 20:05:50 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 172 | public void testTextClassifyText_address() { |
| 173 | if (isTextClassifierDisabled()) return; |
| 174 | |
| 175 | String text = "Brandschenkestrasse 110, Zürich, Switzerland"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 176 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 177 | text, 0, text.length()) |
| 178 | .setDefaultLocales(LOCALES) |
| 179 | .build(); |
| 180 | |
| 181 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 182 | assertThat(classification, isTextClassification(text, TextClassifier.TYPE_ADDRESS)); |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Test |
Abodunrinwa Toki | 86ef982 | 2017-05-11 20:05:50 +0100 | [diff] [blame] | 186 | public void testTextClassifyText_url_inCaps() { |
| 187 | if (isTextClassifierDisabled()) return; |
| 188 | |
| 189 | String text = "Visit HTTP://ANDROID.COM for more information"; |
| 190 | String classifiedText = "HTTP://ANDROID.COM"; |
| 191 | int startIndex = text.indexOf(classifiedText); |
| 192 | int endIndex = startIndex + classifiedText.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 193 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 194 | text, startIndex, endIndex) |
| 195 | .setDefaultLocales(LOCALES) |
| 196 | .build(); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 197 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 198 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 199 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_URL)); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Test |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 203 | public void testTextClassifyText_date() { |
| 204 | if (isTextClassifierDisabled()) return; |
| 205 | |
| 206 | String text = "Let's meet on January 9, 2018."; |
| 207 | String classifiedText = "January 9, 2018"; |
| 208 | int startIndex = text.indexOf(classifiedText); |
| 209 | int endIndex = startIndex + classifiedText.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 210 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 211 | text, startIndex, endIndex) |
| 212 | .setDefaultLocales(LOCALES) |
| 213 | .build(); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 214 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 215 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 216 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_DATE)); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void testTextClassifyText_datetime() { |
| 221 | if (isTextClassifierDisabled()) return; |
| 222 | |
| 223 | String text = "Let's meet 2018/01/01 10:30:20."; |
| 224 | String classifiedText = "2018/01/01 10:30:20"; |
| 225 | int startIndex = text.indexOf(classifiedText); |
| 226 | int endIndex = startIndex + classifiedText.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 227 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 228 | text, startIndex, endIndex) |
| 229 | .setDefaultLocales(LOCALES) |
| 230 | .build(); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 231 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 232 | TextClassification classification = mClassifier.classifyText(request); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 233 | assertThat(classification, |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 234 | isTextClassification(classifiedText, TextClassifier.TYPE_DATE_TIME)); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Test |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 238 | public void testGenerateLinks_phone() { |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 239 | if (isTextClassifierDisabled()) return; |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 240 | String text = "The number is +12122537077. See you tonight!"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 241 | TextLinks.Request request = new TextLinks.Request.Builder(text).build(); |
| 242 | assertThat(mClassifier.generateLinks(request), |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 243 | isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE)); |
| 244 | } |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 245 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 246 | @Test |
| 247 | public void testGenerateLinks_exclude() { |
| 248 | if (isTextClassifierDisabled()) return; |
| 249 | String text = "The number is +12122537077. See you tonight!"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 250 | List<String> hints = Collections.EMPTY_LIST; |
| 251 | List<String> included = Collections.EMPTY_LIST; |
| 252 | List<String> excluded = Arrays.asList(TextClassifier.TYPE_PHONE); |
| 253 | TextLinks.Request request = new TextLinks.Request.Builder(text) |
| 254 | .setEntityConfig(TextClassifier.EntityConfig.create(hints, included, excluded)) |
| 255 | .setDefaultLocales(LOCALES) |
| 256 | .build(); |
| 257 | assertThat(mClassifier.generateLinks(request), |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 258 | not(isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE))); |
| 259 | } |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 260 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 261 | @Test |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 262 | public void testGenerateLinks_explicit_address() { |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 263 | if (isTextClassifierDisabled()) return; |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 264 | String text = "The address is 1600 Amphitheater Parkway, Mountain View, CA. See you!"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 265 | List<String> explicit = Arrays.asList(TextClassifier.TYPE_ADDRESS); |
| 266 | TextLinks.Request request = new TextLinks.Request.Builder(text) |
| 267 | .setEntityConfig(TextClassifier.EntityConfig.createWithExplicitEntityList(explicit)) |
| 268 | .setDefaultLocales(LOCALES) |
| 269 | .build(); |
| 270 | assertThat(mClassifier.generateLinks(request), |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 271 | isTextLinksContaining(text, "1600 Amphitheater Parkway, Mountain View, CA", |
| 272 | TextClassifier.TYPE_ADDRESS)); |
| 273 | } |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 274 | |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 275 | @Test |
| 276 | public void testGenerateLinks_exclude_override() { |
| 277 | if (isTextClassifierDisabled()) return; |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 278 | String text = "The number is +12122537077. See you tonight!"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 279 | List<String> hints = Collections.EMPTY_LIST; |
| 280 | List<String> included = Arrays.asList(TextClassifier.TYPE_PHONE); |
| 281 | List<String> excluded = Arrays.asList(TextClassifier.TYPE_PHONE); |
| 282 | TextLinks.Request request = new TextLinks.Request.Builder(text) |
| 283 | .setEntityConfig(TextClassifier.EntityConfig.create(hints, included, excluded)) |
| 284 | .setDefaultLocales(LOCALES) |
| 285 | .build(); |
| 286 | assertThat(mClassifier.generateLinks(request), |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 287 | not(isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE))); |
| 288 | } |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 289 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 290 | @Test |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 291 | public void testGenerateLinks_maxLength() { |
| 292 | if (isTextClassifierDisabled()) return; |
| 293 | char[] manySpaces = new char[mClassifier.getMaxGenerateLinksTextLength()]; |
| 294 | Arrays.fill(manySpaces, ' '); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 295 | TextLinks.Request request = new TextLinks.Request.Builder(new String(manySpaces)).build(); |
| 296 | TextLinks links = mClassifier.generateLinks(request); |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 297 | assertTrue(links.getLinks().isEmpty()); |
| 298 | } |
| 299 | |
| 300 | @Test(expected = IllegalArgumentException.class) |
| 301 | public void testGenerateLinks_tooLong() { |
| 302 | if (isTextClassifierDisabled()) { |
| 303 | throw new IllegalArgumentException("pass if disabled"); |
| 304 | } |
| 305 | char[] manySpaces = new char[mClassifier.getMaxGenerateLinksTextLength() + 1]; |
| 306 | Arrays.fill(manySpaces, ' '); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 307 | TextLinks.Request request = new TextLinks.Request.Builder(new String(manySpaces)).build(); |
| 308 | mClassifier.generateLinks(request); |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Test |
Abodunrinwa Toki | a69950c | 2018-11-29 13:51:56 +0000 | [diff] [blame] | 312 | public void testApplyLinks_unsupportedCharacter() { |
| 313 | if (isTextClassifierDisabled()) return; |
| 314 | Spannable url = new SpannableString("\u202Emoc.diordna.com"); |
| 315 | TextLinks.Request request = new TextLinks.Request.Builder(url).build(); |
| 316 | assertEquals( |
| 317 | TextLinks.STATUS_NO_LINKS_APPLIED, |
| 318 | mClassifier.generateLinks(request).apply(url, 0, null)); |
| 319 | } |
| 320 | |
| 321 | @Test |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 322 | public void testSetTextClassifier() { |
| 323 | TextClassifier classifier = mock(TextClassifier.class); |
| 324 | mTcm.setTextClassifier(classifier); |
| 325 | assertEquals(classifier, mTcm.getTextClassifier()); |
| 326 | } |
| 327 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 328 | @Test |
| 329 | public void testGetLocalTextClassifier() { |
| 330 | assertTrue(mTcm.getTextClassifier(TextClassifier.LOCAL) instanceof TextClassifierImpl); |
| 331 | } |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 332 | @Test |
| 333 | public void testGetSystemTextClassifier() { |
| 334 | assertTrue( |
| 335 | TextClassifierService.getServiceComponentName(mContext) == null |
| 336 | || mTcm.getTextClassifier(TextClassifier.SYSTEM) instanceof SystemTextClassifier); |
| 337 | } |
| 338 | |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 339 | @Test |
| 340 | public void testCannotResolveIntent() { |
| 341 | final PackageManager fakePackageMgr = mock(PackageManager.class); |
| 342 | |
| 343 | ResolveInfo validInfo = mContext.getPackageManager().resolveActivity( |
| 344 | new Intent(Intent.ACTION_DIAL).setData(Uri.parse("tel:+12122537077")), 0); |
| 345 | // Make packageManager fail when it gets the following intent: |
| 346 | ArgumentMatcher<Intent> toFailIntent = |
| 347 | intent -> intent.getAction().equals(Intent.ACTION_INSERT_OR_EDIT); |
| 348 | |
| 349 | when(fakePackageMgr.resolveActivity(any(Intent.class), anyInt())).thenReturn(validInfo); |
| 350 | when(fakePackageMgr.resolveActivity(argThat(toFailIntent), anyInt())).thenReturn(null); |
| 351 | |
| 352 | ContextWrapper fakeContext = new ContextWrapper(mContext) { |
| 353 | @Override |
| 354 | public PackageManager getPackageManager() { |
| 355 | return fakePackageMgr; |
| 356 | } |
| 357 | }; |
| 358 | |
| 359 | TextClassifier fallback = TextClassifier.NO_OP; |
| 360 | TextClassifier classifier = new TextClassifierImpl( |
| 361 | fakeContext, TextClassificationConstants.loadFromString(null), fallback); |
| 362 | |
| 363 | String text = "Contact me at +12122537077"; |
| 364 | String classifiedText = "+12122537077"; |
| 365 | int startIndex = text.indexOf(classifiedText); |
| 366 | int endIndex = startIndex + classifiedText.length(); |
| 367 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 368 | text, startIndex, endIndex) |
| 369 | .setDefaultLocales(LOCALES) |
| 370 | .build(); |
| 371 | |
| 372 | TextClassification result = classifier.classifyText(request); |
| 373 | TextClassification fallbackResult = fallback.classifyText(request); |
| 374 | |
| 375 | // classifier should not totally fail in which case it returns a fallback result. |
| 376 | // It should skip the failing intent and return a result for non-failing intents. |
| 377 | assertFalse(result.getActions().isEmpty()); |
| 378 | assertNotSame(result, fallbackResult); |
| 379 | } |
| 380 | |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 381 | private boolean isTextClassifierDisabled() { |
| 382 | return mClassifier == TextClassifier.NO_OP; |
| 383 | } |
| 384 | |
| 385 | private static Matcher<TextSelection> isTextSelection( |
| 386 | final int startIndex, final int endIndex, final String type) { |
| 387 | return new BaseMatcher<TextSelection>() { |
| 388 | @Override |
| 389 | public boolean matches(Object o) { |
| 390 | if (o instanceof TextSelection) { |
| 391 | TextSelection selection = (TextSelection) o; |
| 392 | return startIndex == selection.getSelectionStartIndex() |
| 393 | && endIndex == selection.getSelectionEndIndex() |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 394 | && typeMatches(selection, type); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 395 | } |
| 396 | return false; |
| 397 | } |
| 398 | |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 399 | private boolean typeMatches(TextSelection selection, String type) { |
| 400 | return type == null |
| 401 | || (selection.getEntityCount() > 0 |
| 402 | && type.trim().equalsIgnoreCase(selection.getEntity(0))); |
| 403 | } |
| 404 | |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 405 | @Override |
| 406 | public void describeTo(Description description) { |
| 407 | description.appendValue( |
| 408 | String.format("%d, %d, %s", startIndex, endIndex, type)); |
| 409 | } |
| 410 | }; |
| 411 | } |
| 412 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 413 | private static Matcher<TextLinks> isTextLinksContaining( |
| 414 | final String text, final String substring, final String type) { |
| 415 | return new BaseMatcher<TextLinks>() { |
| 416 | |
| 417 | @Override |
| 418 | public void describeTo(Description description) { |
| 419 | description.appendText("text=").appendValue(text) |
| 420 | .appendText(", substring=").appendValue(substring) |
| 421 | .appendText(", type=").appendValue(type); |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public boolean matches(Object o) { |
| 426 | if (o instanceof TextLinks) { |
| 427 | for (TextLinks.TextLink link : ((TextLinks) o).getLinks()) { |
| 428 | if (text.subSequence(link.getStart(), link.getEnd()).equals(substring)) { |
| 429 | return type.equals(link.getEntity(0)); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | return false; |
| 434 | } |
| 435 | }; |
| 436 | } |
| 437 | |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 438 | private static Matcher<TextClassification> isTextClassification( |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 439 | final String text, final String type) { |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 440 | return new BaseMatcher<TextClassification>() { |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 441 | @Override |
| 442 | public boolean matches(Object o) { |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 443 | if (o instanceof TextClassification) { |
| 444 | TextClassification result = (TextClassification) o; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 445 | return text.equals(result.getText()) |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 446 | && result.getEntityCount() > 0 |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 447 | && type.equals(result.getEntity(0)); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public void describeTo(Description description) { |
| 454 | description.appendText("text=").appendValue(text) |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 455 | .appendText(", type=").appendValue(type); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 456 | } |
| 457 | }; |
| 458 | } |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 459 | } |