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 | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 42 | |
| 43 | import org.hamcrest.BaseMatcher; |
| 44 | import org.hamcrest.Description; |
| 45 | import org.hamcrest.Matcher; |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 49 | import org.mockito.ArgumentMatcher; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 50 | |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 51 | import java.util.Arrays; |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 52 | import java.util.Collections; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 53 | import java.util.List; |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 54 | |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 55 | @SmallTest |
| 56 | @RunWith(AndroidJUnit4.class) |
| 57 | public class TextClassificationManagerTest { |
| 58 | |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 59 | private static final LocaleList LOCALES = LocaleList.forLanguageTags("en-US"); |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 60 | private static final String NO_TYPE = null; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 61 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 62 | private Context mContext; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 63 | private TextClassificationManager mTcm; |
| 64 | private TextClassifier mClassifier; |
| 65 | |
| 66 | @Before |
| 67 | public void setup() { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 68 | mContext = InstrumentationRegistry.getTargetContext(); |
| 69 | mTcm = mContext.getSystemService(TextClassificationManager.class); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 70 | // 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 Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 73 | } |
| 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 86 | TextSelection.Request request = new TextSelection.Request.Builder( |
| 87 | text, startIndex, endIndex) |
| 88 | .setDefaultLocales(LOCALES) |
| 89 | .build(); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 90 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 91 | TextSelection selection = mClassifier.suggestSelection(request); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 92 | assertThat(selection, |
Abodunrinwa Toki | a2df6e5 | 2017-04-13 09:56:52 +0100 | [diff] [blame] | 93 | isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_EMAIL)); |
| 94 | } |
| 95 | |
| 96 | @Test |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 97 | 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 107 | TextSelection.Request request = new TextSelection.Request.Builder( |
| 108 | text, startIndex, endIndex) |
| 109 | .setDefaultLocales(LOCALES) |
| 110 | .build(); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 111 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 112 | TextSelection selection = mClassifier.suggestSelection(request); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 113 | assertThat(selection, |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 114 | isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_URL)); |
| 115 | } |
| 116 | |
| 117 | @Test |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 118 | 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 125 | TextSelection.Request request = new TextSelection.Request.Builder( |
| 126 | text, startIndex, endIndex) |
| 127 | .setDefaultLocales(LOCALES) |
| 128 | .build(); |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 129 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 130 | TextSelection selection = mClassifier.suggestSelection(request); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 131 | assertThat(selection, |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 132 | isTextSelection(startIndex, endIndex, NO_TYPE)); |
| 133 | } |
| 134 | |
| 135 | @Test |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 136 | public void testClassifyText() { |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 137 | 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 143 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 144 | text, startIndex, endIndex) |
| 145 | .setDefaultLocales(LOCALES) |
| 146 | .build(); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 147 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 148 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 149 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_EMAIL)); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Test |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 153 | public void testTextClassifyText_url() { |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 154 | if (isTextClassifierDisabled()) return; |
| 155 | |
Abodunrinwa Toki | 86ef982 | 2017-05-11 20:05:50 +0100 | [diff] [blame] | 156 | String text = "Visit www.android.com for more information"; |
Abodunrinwa Toki | 70d41cd | 2017-05-02 21:43:41 +0100 | [diff] [blame] | 157 | String classifiedText = "www.android.com"; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 158 | int startIndex = text.indexOf(classifiedText); |
| 159 | int endIndex = startIndex + classifiedText.length(); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 160 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 161 | text, startIndex, endIndex) |
| 162 | .setDefaultLocales(LOCALES) |
| 163 | .build(); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 164 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 165 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 166 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_URL)); |
Abodunrinwa Toki | 86ef982 | 2017-05-11 20:05:50 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 170 | public void testTextClassifyText_address() { |
| 171 | if (isTextClassifierDisabled()) return; |
| 172 | |
| 173 | String text = "Brandschenkestrasse 110, Zürich, Switzerland"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 174 | 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 Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 180 | assertThat(classification, isTextClassification(text, TextClassifier.TYPE_ADDRESS)); |
Jan Althaus | eaff57e | 2018-02-12 12:47:27 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Test |
Abodunrinwa Toki | 86ef982 | 2017-05-11 20:05:50 +0100 | [diff] [blame] | 184 | 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 191 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 192 | text, startIndex, endIndex) |
| 193 | .setDefaultLocales(LOCALES) |
| 194 | .build(); |
Abodunrinwa Toki | 4d232d6 | 2017-11-23 12:22:45 +0000 | [diff] [blame] | 195 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 196 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 197 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_URL)); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Test |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 201 | 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 208 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 209 | text, startIndex, endIndex) |
| 210 | .setDefaultLocales(LOCALES) |
| 211 | .build(); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 212 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 213 | TextClassification classification = mClassifier.classifyText(request); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 214 | assertThat(classification, isTextClassification(classifiedText, TextClassifier.TYPE_DATE)); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 215 | } |
| 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 225 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 226 | text, startIndex, endIndex) |
| 227 | .setDefaultLocales(LOCALES) |
| 228 | .build(); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 229 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 230 | TextClassification classification = mClassifier.classifyText(request); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 231 | assertThat(classification, |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 232 | isTextClassification(classifiedText, TextClassifier.TYPE_DATE_TIME)); |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Test |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 236 | public void testGenerateLinks_phone() { |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 237 | if (isTextClassifierDisabled()) return; |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 238 | String text = "The number is +12122537077. See you tonight!"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 239 | TextLinks.Request request = new TextLinks.Request.Builder(text).build(); |
| 240 | assertThat(mClassifier.generateLinks(request), |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 241 | isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE)); |
| 242 | } |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 243 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 244 | @Test |
| 245 | public void testGenerateLinks_exclude() { |
| 246 | if (isTextClassifierDisabled()) return; |
| 247 | String text = "The number is +12122537077. See you tonight!"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 248 | 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 Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 256 | not(isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE))); |
| 257 | } |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 258 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 259 | @Test |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 260 | public void testGenerateLinks_explicit_address() { |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 261 | if (isTextClassifierDisabled()) return; |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 262 | 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] | 263 | 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 Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 269 | isTextLinksContaining(text, "1600 Amphitheater Parkway, Mountain View, CA", |
| 270 | TextClassifier.TYPE_ADDRESS)); |
| 271 | } |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 272 | |
Richard Ledley | 1fc998b | 2018-02-16 15:45:06 +0000 | [diff] [blame] | 273 | @Test |
| 274 | public void testGenerateLinks_exclude_override() { |
| 275 | if (isTextClassifierDisabled()) return; |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 276 | String text = "The number is +12122537077. See you tonight!"; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 277 | 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 Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 285 | not(isTextLinksContaining(text, "+12122537077", TextClassifier.TYPE_PHONE))); |
| 286 | } |
Richard Ledley | 68d9452 | 2017-10-05 10:52:19 +0100 | [diff] [blame] | 287 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 288 | @Test |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 289 | public void testGenerateLinks_maxLength() { |
| 290 | if (isTextClassifierDisabled()) return; |
| 291 | char[] manySpaces = new char[mClassifier.getMaxGenerateLinksTextLength()]; |
| 292 | Arrays.fill(manySpaces, ' '); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 293 | TextLinks.Request request = new TextLinks.Request.Builder(new String(manySpaces)).build(); |
| 294 | TextLinks links = mClassifier.generateLinks(request); |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 295 | 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 305 | TextLinks.Request request = new TextLinks.Request.Builder(new String(manySpaces)).build(); |
| 306 | mClassifier.generateLinks(request); |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Test |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 310 | public void testSetTextClassifier() { |
| 311 | TextClassifier classifier = mock(TextClassifier.class); |
| 312 | mTcm.setTextClassifier(classifier); |
| 313 | assertEquals(classifier, mTcm.getTextClassifier()); |
| 314 | } |
| 315 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 316 | @Test |
| 317 | public void testGetLocalTextClassifier() { |
| 318 | assertTrue(mTcm.getTextClassifier(TextClassifier.LOCAL) instanceof TextClassifierImpl); |
| 319 | } |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 320 | @Test |
| 321 | public void testGetSystemTextClassifier() { |
| 322 | assertTrue( |
| 323 | TextClassifierService.getServiceComponentName(mContext) == null |
| 324 | || mTcm.getTextClassifier(TextClassifier.SYSTEM) instanceof SystemTextClassifier); |
| 325 | } |
| 326 | |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 327 | @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 Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 369 | 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 Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 382 | && typeMatches(selection, type); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
Abodunrinwa Toki | b416297 | 2017-05-05 18:07:17 +0100 | [diff] [blame] | 387 | 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 Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 393 | @Override |
| 394 | public void describeTo(Description description) { |
| 395 | description.appendValue( |
| 396 | String.format("%d, %d, %s", startIndex, endIndex, type)); |
| 397 | } |
| 398 | }; |
| 399 | } |
| 400 | |
Richard Ledley | db18a57 | 2017-11-30 17:33:51 +0000 | [diff] [blame] | 401 | 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 Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 426 | private static Matcher<TextClassification> isTextClassification( |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 427 | final String text, final String type) { |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 428 | return new BaseMatcher<TextClassification>() { |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 429 | @Override |
| 430 | public boolean matches(Object o) { |
Abodunrinwa Toki | e0b5789 | 2017-04-28 19:59:57 +0100 | [diff] [blame] | 431 | if (o instanceof TextClassification) { |
| 432 | TextClassification result = (TextClassification) o; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 433 | return text.equals(result.getText()) |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 434 | && result.getEntityCount() > 0 |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 435 | && type.equals(result.getEntity(0)); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void describeTo(Description description) { |
| 442 | description.appendText("text=").appendValue(text) |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 443 | .appendText(", type=").appendValue(type); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 444 | } |
| 445 | }; |
| 446 | } |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 447 | } |