Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 17 | package android.view.textclassifier.intent; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.eq; |
| 23 | import static org.mockito.ArgumentMatchers.same; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | |
| 27 | import android.content.Context; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 28 | import android.content.Intent; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 29 | import android.view.textclassifier.TextClassifier; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 30 | |
| 31 | import androidx.test.InstrumentationRegistry; |
| 32 | import androidx.test.filters.SmallTest; |
| 33 | import androidx.test.runner.AndroidJUnit4; |
| 34 | |
| 35 | import com.google.android.textclassifier.AnnotatorModel; |
| 36 | import com.google.android.textclassifier.RemoteActionTemplate; |
| 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.MockitoAnnotations; |
| 43 | |
| 44 | import java.util.List; |
| 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidJUnit4.class) |
| 48 | public class TemplateClassificationIntentFactoryTest { |
| 49 | |
| 50 | private static final String TEXT = "text"; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 51 | private static final String TITLE_WITHOUT_ENTITY = "Map"; |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 52 | private static final String DESCRIPTION = "Opens in Maps"; |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 53 | private static final String DESCRIPTION_WITH_APP_NAME = "Use %1$s to open Map"; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 54 | private static final String ACTION = Intent.ACTION_VIEW; |
| 55 | |
| 56 | @Mock |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 57 | private ClassificationIntentFactory mFallback; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 58 | 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 Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 70 | 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 Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 81 | null, |
| 82 | null, |
| 83 | null, |
Tony Mak | 159f028 | 2019-03-01 14:03:25 +0000 | [diff] [blame] | 84 | null, |
Tony Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 85 | null, |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 86 | null, |
Tony Mak | 237f37b | 2019-03-22 13:35:40 +0000 | [diff] [blame] | 87 | createRemoteActionTemplates(), |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 88 | 0L, |
| 89 | 0L, |
| 90 | 0d); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 91 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 92 | List<LabeledIntent> intents = |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 93 | mTemplateClassificationIntentFactory.create( |
| 94 | InstrumentationRegistry.getContext(), |
| 95 | TEXT, |
| 96 | /* foreignText */ true, |
| 97 | null, |
| 98 | classificationResult); |
| 99 | |
| 100 | assertThat(intents).hasSize(2); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 101 | LabeledIntent labeledIntent = intents.get(0); |
| 102 | assertThat(labeledIntent.titleWithoutEntity).isEqualTo(TITLE_WITHOUT_ENTITY); |
| 103 | Intent intent = labeledIntent.intent; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 104 | assertThat(intent.getAction()).isEqualTo(ACTION); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 105 | |
| 106 | labeledIntent = intents.get(1); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 107 | intent = labeledIntent.intent; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 108 | assertThat(intent.getAction()).isEqualTo(Intent.ACTION_TRANSLATE); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void create_notForeignText() { |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 113 | 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 Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 124 | null, |
| 125 | null, |
| 126 | null, |
Tony Mak | 159f028 | 2019-03-01 14:03:25 +0000 | [diff] [blame] | 127 | null, |
Tony Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 128 | null, |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 129 | null, |
Tony Mak | 237f37b | 2019-03-22 13:35:40 +0000 | [diff] [blame] | 130 | createRemoteActionTemplates(), |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 131 | 0L, |
| 132 | 0L, |
| 133 | 0d); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 134 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 135 | List<LabeledIntent> intents = |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 136 | mTemplateClassificationIntentFactory.create( |
| 137 | InstrumentationRegistry.getContext(), |
| 138 | TEXT, |
| 139 | /* foreignText */ false, |
| 140 | null, |
| 141 | classificationResult); |
| 142 | |
| 143 | assertThat(intents).hasSize(1); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 144 | LabeledIntent labeledIntent = intents.get(0); |
| 145 | assertThat(labeledIntent.titleWithoutEntity).isEqualTo(TITLE_WITHOUT_ENTITY); |
| 146 | Intent intent = labeledIntent.intent; |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 147 | assertThat(intent.getAction()).isEqualTo(ACTION); |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 148 | } |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 149 | |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 150 | @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 Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 167 | null, |
Tony Mak | 237f37b | 2019-03-22 13:35:40 +0000 | [diff] [blame] | 168 | null, |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 169 | null, |
| 170 | 0L, |
| 171 | 0L, |
| 172 | 0d); |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 173 | |
| 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 Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 204 | null, |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 205 | null, |
Tony Mak | 237f37b | 2019-03-22 13:35:40 +0000 | [diff] [blame] | 206 | new RemoteActionTemplate[0], |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 207 | 0L, |
| 208 | 0L, |
| 209 | 0d); |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 210 | |
| 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 Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 225 | private static RemoteActionTemplate[] createRemoteActionTemplates() { |
| 226 | return new RemoteActionTemplate[]{ |
| 227 | new RemoteActionTemplate( |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 228 | TITLE_WITHOUT_ENTITY, |
| 229 | null, |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 230 | DESCRIPTION, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 231 | DESCRIPTION_WITH_APP_NAME, |
Abodunrinwa Toki | c33fc77 | 2019-02-06 01:17:10 +0000 | [diff] [blame] | 232 | ACTION, |
| 233 | null, |
| 234 | null, |
| 235 | null, |
| 236 | null, |
| 237 | null, |
| 238 | null, |
| 239 | null |
| 240 | ) |
| 241 | }; |
| 242 | } |
Tony Mak | ae33c3b | 2019-01-31 14:29:19 +0000 | [diff] [blame] | 243 | } |