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 | |
| 19 | import static org.junit.Assert.assertEquals; |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 20 | import static org.junit.Assert.assertFalse; |
| 21 | import static org.junit.Assert.assertNotSame; |
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 | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 26 | import android.content.Intent; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 27 | import android.os.LocaleList; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 28 | |
| 29 | import androidx.test.InstrumentationRegistry; |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | import androidx.test.runner.AndroidJUnit4; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 32 | |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 37 | @SmallTest |
| 38 | @RunWith(AndroidJUnit4.class) |
| 39 | public class TextClassificationManagerTest { |
| 40 | |
Lukas Zilka | f8c36bf | 2018-02-07 20:30:08 +0100 | [diff] [blame] | 41 | private static final LocaleList LOCALES = LocaleList.forLanguageTags("en-US"); |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 42 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 43 | private Context mContext; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 44 | private TextClassificationManager mTcm; |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 45 | |
| 46 | @Before |
| 47 | public void setup() { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 48 | mContext = InstrumentationRegistry.getTargetContext(); |
| 49 | mTcm = mContext.getSystemService(TextClassificationManager.class); |
Tony Mak | adbebcc | 2018-10-30 18:59:06 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 53 | public void testSetTextClassifier() { |
| 54 | TextClassifier classifier = mock(TextClassifier.class); |
| 55 | mTcm.setTextClassifier(classifier); |
| 56 | assertEquals(classifier, mTcm.getTextClassifier()); |
| 57 | } |
| 58 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 59 | @Test |
| 60 | public void testGetLocalTextClassifier() { |
| 61 | assertTrue(mTcm.getTextClassifier(TextClassifier.LOCAL) instanceof TextClassifierImpl); |
| 62 | } |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 63 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 64 | @Test |
| 65 | public void testGetSystemTextClassifier() { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 66 | assertTrue(mTcm.getTextClassifier(TextClassifier.SYSTEM) instanceof SystemTextClassifier); |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 69 | @Test |
| 70 | public void testCannotResolveIntent() { |
Abodunrinwa Toki | 6a21199 | 2018-11-26 17:37:59 +0000 | [diff] [blame] | 71 | Context fakeContext = new FakeContextBuilder() |
| 72 | .setAllIntentComponent(FakeContextBuilder.DEFAULT_COMPONENT) |
| 73 | .setIntentComponent(Intent.ACTION_INSERT_OR_EDIT, null) |
| 74 | .build(); |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 75 | |
| 76 | TextClassifier fallback = TextClassifier.NO_OP; |
| 77 | TextClassifier classifier = new TextClassifierImpl( |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 78 | fakeContext, new TextClassificationConstants(), fallback); |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 79 | |
| 80 | String text = "Contact me at +12122537077"; |
| 81 | String classifiedText = "+12122537077"; |
| 82 | int startIndex = text.indexOf(classifiedText); |
| 83 | int endIndex = startIndex + classifiedText.length(); |
| 84 | TextClassification.Request request = new TextClassification.Request.Builder( |
| 85 | text, startIndex, endIndex) |
| 86 | .setDefaultLocales(LOCALES) |
| 87 | .build(); |
| 88 | |
| 89 | TextClassification result = classifier.classifyText(request); |
| 90 | TextClassification fallbackResult = fallback.classifyText(request); |
| 91 | |
| 92 | // classifier should not totally fail in which case it returns a fallback result. |
| 93 | // It should skip the failing intent and return a result for non-failing intents. |
| 94 | assertFalse(result.getActions().isEmpty()); |
| 95 | assertNotSame(result, fallbackResult); |
| 96 | } |
Abodunrinwa Toki | 65b2527 | 2017-04-13 17:25:59 +0100 | [diff] [blame] | 97 | } |