blob: bcea5fea6a13c5473f5367927afda872e2ab25f8 [file] [log] [blame]
Tony Makae33c3b2019-01-31 14:29:19 +00001/*
2 * Copyright (C) 2019 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
Tony Mak8ab9b182019-03-01 16:44:17 +000017package android.view.textclassifier.intent;
Tony Makae33c3b2019-01-31 14:29:19 +000018
19import static com.google.common.truth.Truth.assertThat;
20
Tony Mak8ab9b182019-03-01 16:44:17 +000021import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.ArgumentMatchers.eq;
23import static org.mockito.ArgumentMatchers.same;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.verify;
26
27import android.content.Context;
Tony Makae33c3b2019-01-31 14:29:19 +000028import android.content.Intent;
Tony Mak8ab9b182019-03-01 16:44:17 +000029import android.view.textclassifier.TextClassifier;
Tony Makae33c3b2019-01-31 14:29:19 +000030
31import androidx.test.InstrumentationRegistry;
32import androidx.test.filters.SmallTest;
33import androidx.test.runner.AndroidJUnit4;
34
35import com.google.android.textclassifier.AnnotatorModel;
36import com.google.android.textclassifier.RemoteActionTemplate;
37
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
43
44import java.util.List;
45
46@SmallTest
47@RunWith(AndroidJUnit4.class)
48public class TemplateClassificationIntentFactoryTest {
49
50 private static final String TEXT = "text";
Tony Makac9b4d82019-02-15 13:57:38 +000051 private static final String TITLE_WITHOUT_ENTITY = "Map";
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +000052 private static final String DESCRIPTION = "Opens in Maps";
Tony Mak15b64be2019-04-01 20:02:29 +010053 private static final String DESCRIPTION_WITH_APP_NAME = "Use %1$s to open Map";
Tony Makae33c3b2019-01-31 14:29:19 +000054 private static final String ACTION = Intent.ACTION_VIEW;
55
56 @Mock
Tony Mak8ab9b182019-03-01 16:44:17 +000057 private ClassificationIntentFactory mFallback;
Tony Makae33c3b2019-01-31 14:29:19 +000058 private TemplateClassificationIntentFactory mTemplateClassificationIntentFactory;
59
60 @Before
61 public void setup() {
62 MockitoAnnotations.initMocks(this);
63 mTemplateClassificationIntentFactory = new TemplateClassificationIntentFactory(
64 new TemplateIntentFactory(),
65 mFallback);
66 }
67
68 @Test
69 public void create_foreignText() {
Tony Makae33c3b2019-01-31 14:29:19 +000070 AnnotatorModel.ClassificationResult classificationResult =
71 new AnnotatorModel.ClassificationResult(
72 TextClassifier.TYPE_ADDRESS,
73 1.0f,
74 null,
75 null,
76 null,
77 null,
78 null,
79 null,
80 null,
Tony Makac9b4d82019-02-15 13:57:38 +000081 null,
82 null,
83 null,
Tony Mak159f0282019-03-01 14:03:25 +000084 null,
Tony Mak2a3adb72019-03-20 17:55:53 +000085 null,
Tony Mak221bb4f2019-09-12 17:17:14 +010086 null,
Tony Mak237f37b2019-03-22 13:35:40 +000087 createRemoteActionTemplates(),
Tony Mak221bb4f2019-09-12 17:17:14 +010088 0L,
89 0L,
90 0d);
Tony Makae33c3b2019-01-31 14:29:19 +000091
Tony Makac9b4d82019-02-15 13:57:38 +000092 List<LabeledIntent> intents =
Tony Makae33c3b2019-01-31 14:29:19 +000093 mTemplateClassificationIntentFactory.create(
94 InstrumentationRegistry.getContext(),
95 TEXT,
96 /* foreignText */ true,
97 null,
98 classificationResult);
99
100 assertThat(intents).hasSize(2);
Tony Makac9b4d82019-02-15 13:57:38 +0000101 LabeledIntent labeledIntent = intents.get(0);
102 assertThat(labeledIntent.titleWithoutEntity).isEqualTo(TITLE_WITHOUT_ENTITY);
103 Intent intent = labeledIntent.intent;
Tony Makae33c3b2019-01-31 14:29:19 +0000104 assertThat(intent.getAction()).isEqualTo(ACTION);
Tony Makae33c3b2019-01-31 14:29:19 +0000105
106 labeledIntent = intents.get(1);
Tony Makac9b4d82019-02-15 13:57:38 +0000107 intent = labeledIntent.intent;
Tony Makae33c3b2019-01-31 14:29:19 +0000108 assertThat(intent.getAction()).isEqualTo(Intent.ACTION_TRANSLATE);
Tony Makae33c3b2019-01-31 14:29:19 +0000109 }
110
111 @Test
112 public void create_notForeignText() {
Tony Makae33c3b2019-01-31 14:29:19 +0000113 AnnotatorModel.ClassificationResult classificationResult =
114 new AnnotatorModel.ClassificationResult(
115 TextClassifier.TYPE_ADDRESS,
116 1.0f,
117 null,
118 null,
119 null,
120 null,
121 null,
122 null,
123 null,
Tony Makac9b4d82019-02-15 13:57:38 +0000124 null,
125 null,
126 null,
Tony Mak159f0282019-03-01 14:03:25 +0000127 null,
Tony Mak2a3adb72019-03-20 17:55:53 +0000128 null,
Tony Mak221bb4f2019-09-12 17:17:14 +0100129 null,
Tony Mak237f37b2019-03-22 13:35:40 +0000130 createRemoteActionTemplates(),
Tony Mak221bb4f2019-09-12 17:17:14 +0100131 0L,
132 0L,
133 0d);
Tony Makae33c3b2019-01-31 14:29:19 +0000134
Tony Makac9b4d82019-02-15 13:57:38 +0000135 List<LabeledIntent> intents =
Tony Makae33c3b2019-01-31 14:29:19 +0000136 mTemplateClassificationIntentFactory.create(
137 InstrumentationRegistry.getContext(),
138 TEXT,
139 /* foreignText */ false,
140 null,
141 classificationResult);
142
143 assertThat(intents).hasSize(1);
Tony Makac9b4d82019-02-15 13:57:38 +0000144 LabeledIntent labeledIntent = intents.get(0);
145 assertThat(labeledIntent.titleWithoutEntity).isEqualTo(TITLE_WITHOUT_ENTITY);
146 Intent intent = labeledIntent.intent;
Tony Makae33c3b2019-01-31 14:29:19 +0000147 assertThat(intent.getAction()).isEqualTo(ACTION);
Tony Makae33c3b2019-01-31 14:29:19 +0000148 }
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +0000149
Tony Mak8ab9b182019-03-01 16:44:17 +0000150 @Test
151 public void create_nullTemplate() {
152 AnnotatorModel.ClassificationResult classificationResult =
153 new AnnotatorModel.ClassificationResult(
154 TextClassifier.TYPE_ADDRESS,
155 1.0f,
156 null,
157 null,
158 null,
159 null,
160 null,
161 null,
162 null,
163 null,
164 null,
165 null,
166 null,
Tony Mak2a3adb72019-03-20 17:55:53 +0000167 null,
Tony Mak237f37b2019-03-22 13:35:40 +0000168 null,
Tony Mak221bb4f2019-09-12 17:17:14 +0100169 null,
170 0L,
171 0L,
172 0d);
Tony Mak8ab9b182019-03-01 16:44:17 +0000173
174 mTemplateClassificationIntentFactory.create(
175 InstrumentationRegistry.getContext(),
176 TEXT,
177 /* foreignText */ false,
178 null,
179 classificationResult);
180
181
182 verify(mFallback).create(
183 same(InstrumentationRegistry.getContext()), eq(TEXT), eq(false), eq(null),
184 same(classificationResult));
185 }
186
187 @Test
188 public void create_emptyResult() {
189 AnnotatorModel.ClassificationResult classificationResult =
190 new AnnotatorModel.ClassificationResult(
191 TextClassifier.TYPE_ADDRESS,
192 1.0f,
193 null,
194 null,
195 null,
196 null,
197 null,
198 null,
199 null,
200 null,
201 null,
202 null,
203 null,
Tony Mak2a3adb72019-03-20 17:55:53 +0000204 null,
Tony Mak221bb4f2019-09-12 17:17:14 +0100205 null,
Tony Mak237f37b2019-03-22 13:35:40 +0000206 new RemoteActionTemplate[0],
Tony Mak221bb4f2019-09-12 17:17:14 +0100207 0L,
208 0L,
209 0d);
Tony Mak8ab9b182019-03-01 16:44:17 +0000210
211 mTemplateClassificationIntentFactory.create(
212 InstrumentationRegistry.getContext(),
213 TEXT,
214 /* foreignText */ false,
215 null,
216 classificationResult);
217
218
219 verify(mFallback, never()).create(
220 any(Context.class), eq(TEXT), eq(false), eq(null),
221 any(AnnotatorModel.ClassificationResult.class));
222 }
223
224
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +0000225 private static RemoteActionTemplate[] createRemoteActionTemplates() {
226 return new RemoteActionTemplate[]{
227 new RemoteActionTemplate(
Tony Makac9b4d82019-02-15 13:57:38 +0000228 TITLE_WITHOUT_ENTITY,
229 null,
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +0000230 DESCRIPTION,
Tony Mak15b64be2019-04-01 20:02:29 +0100231 DESCRIPTION_WITH_APP_NAME,
Abodunrinwa Tokic33fc772019-02-06 01:17:10 +0000232 ACTION,
233 null,
234 null,
235 null,
236 null,
237 null,
238 null,
239 null
240 )
241 };
242 }
Tony Makae33c3b2019-01-31 14:29:19 +0000243}