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