blob: 1ca46491b363bea5cd8f759fca47f1f1ba464b1a [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
19import static org.junit.Assert.assertEquals;
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010020import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotSame;
Jan Althaus108aad32018-01-30 15:26:55 +010022import static org.junit.Assert.assertTrue;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010023import static org.mockito.Mockito.mock;
24
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000025import android.content.Context;
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010026import android.content.Intent;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010027import android.os.LocaleList;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090028
29import androidx.test.InstrumentationRegistry;
30import androidx.test.filters.SmallTest;
31import androidx.test.runner.AndroidJUnit4;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010032
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010033import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010037@SmallTest
38@RunWith(AndroidJUnit4.class)
39public class TextClassificationManagerTest {
40
Lukas Zilkaf8c36bf2018-02-07 20:30:08 +010041 private static final LocaleList LOCALES = LocaleList.forLanguageTags("en-US");
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010042
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000043 private Context mContext;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010044 private TextClassificationManager mTcm;
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010045
46 @Before
47 public void setup() {
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000048 mContext = InstrumentationRegistry.getTargetContext();
49 mTcm = mContext.getSystemService(TextClassificationManager.class);
Tony Makadbebcc2018-10-30 18:59:06 +000050 }
51
52 @Test
Abodunrinwa Toki65b25272017-04-13 17:25:59 +010053 public void testSetTextClassifier() {
54 TextClassifier classifier = mock(TextClassifier.class);
55 mTcm.setTextClassifier(classifier);
56 assertEquals(classifier, mTcm.getTextClassifier());
57 }
58
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000059 @Test
60 public void testGetLocalTextClassifier() {
61 assertTrue(mTcm.getTextClassifier(TextClassifier.LOCAL) instanceof TextClassifierImpl);
62 }
Joanne Chungb50ab4b2019-10-14 16:40:57 +080063
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000064 @Test
65 public void testGetSystemTextClassifier() {
Tony Makc5a74322020-02-04 17:18:15 +000066 assertTrue(mTcm.getTextClassifier(TextClassifier.SYSTEM) instanceof SystemTextClassifier);
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000067 }
68
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010069 @Test
70 public void testCannotResolveIntent() {
Abodunrinwa Toki6a211992018-11-26 17:37:59 +000071 Context fakeContext = new FakeContextBuilder()
72 .setAllIntentComponent(FakeContextBuilder.DEFAULT_COMPONENT)
73 .setIntentComponent(Intent.ACTION_INSERT_OR_EDIT, null)
74 .build();
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010075
76 TextClassifier fallback = TextClassifier.NO_OP;
77 TextClassifier classifier = new TextClassifierImpl(
Joanne Chungc44f5292019-12-02 21:18:43 +080078 fakeContext, new TextClassificationConstants(), fallback);
Abodunrinwa Toki253827f2018-04-24 19:19:48 +010079
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 Toki65b25272017-04-13 17:25:59 +010097}