Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 1 | /* |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 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 | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.content.Intent; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 22 | import android.view.textclassifier.TextClassifier; |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 23 | |
| 24 | import androidx.test.InstrumentationRegistry; |
| 25 | import androidx.test.filters.SmallTest; |
| 26 | import androidx.test.runner.AndroidJUnit4; |
| 27 | |
| 28 | import com.google.android.textclassifier.AnnotatorModel; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | import java.util.List; |
| 35 | |
| 36 | @SmallTest |
| 37 | @RunWith(AndroidJUnit4.class) |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 38 | public class LegacyIntentClassificationFactoryTest { |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 39 | |
| 40 | private static final String TEXT = "text"; |
| 41 | |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 42 | private LegacyClassificationIntentFactory mLegacyIntentClassificationFactory; |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 43 | |
| 44 | @Before |
| 45 | public void setup() { |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 46 | mLegacyIntentClassificationFactory = new LegacyClassificationIntentFactory(); |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void create_typeDictionary() { |
| 51 | AnnotatorModel.ClassificationResult classificationResult = |
| 52 | new AnnotatorModel.ClassificationResult( |
| 53 | TextClassifier.TYPE_DICTIONARY, |
| 54 | 1.0f, |
| 55 | null, |
| 56 | null, |
| 57 | null, |
| 58 | null, |
| 59 | null, |
| 60 | null, |
| 61 | null, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 62 | null, |
| 63 | null, |
| 64 | null, |
Tony Mak | 159f028 | 2019-03-01 14:03:25 +0000 | [diff] [blame] | 65 | null, |
Tony Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 66 | null, |
Tony Mak | 237f37b | 2019-03-22 13:35:40 +0000 | [diff] [blame] | 67 | null, |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 68 | null, |
| 69 | 0L, |
| 70 | 0L, |
| 71 | 0d); |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 72 | |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 73 | List<LabeledIntent> intents = mLegacyIntentClassificationFactory.create( |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 74 | InstrumentationRegistry.getContext(), |
| 75 | TEXT, |
| 76 | /* foreignText */ false, |
| 77 | null, |
| 78 | classificationResult); |
| 79 | |
| 80 | assertThat(intents).hasSize(1); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 81 | LabeledIntent labeledIntent = intents.get(0); |
| 82 | Intent intent = labeledIntent.intent; |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 83 | assertThat(intent.getAction()).isEqualTo(Intent.ACTION_DEFINE); |
| 84 | assertThat(intent.getStringExtra(Intent.EXTRA_TEXT)).isEqualTo(TEXT); |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void create_translateAndDictionary() { |
| 89 | AnnotatorModel.ClassificationResult classificationResult = |
| 90 | new AnnotatorModel.ClassificationResult( |
| 91 | TextClassifier.TYPE_DICTIONARY, |
| 92 | 1.0f, |
| 93 | null, |
| 94 | null, |
| 95 | null, |
| 96 | null, |
| 97 | null, |
| 98 | null, |
| 99 | null, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 100 | null, |
| 101 | null, |
| 102 | null, |
Tony Mak | 159f028 | 2019-03-01 14:03:25 +0000 | [diff] [blame] | 103 | null, |
Tony Mak | 2a3adb7 | 2019-03-20 17:55:53 +0000 | [diff] [blame] | 104 | null, |
Tony Mak | 237f37b | 2019-03-22 13:35:40 +0000 | [diff] [blame] | 105 | null, |
Tony Mak | 221bb4f | 2019-09-12 17:17:14 +0100 | [diff] [blame] | 106 | null, |
| 107 | 0L, |
| 108 | 0L, |
| 109 | 0d); |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 110 | |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 111 | List<LabeledIntent> intents = mLegacyIntentClassificationFactory.create( |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 112 | InstrumentationRegistry.getContext(), |
| 113 | TEXT, |
| 114 | /* foreignText */ true, |
| 115 | null, |
| 116 | classificationResult); |
| 117 | |
| 118 | assertThat(intents).hasSize(2); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 119 | assertThat(intents.get(0).intent.getAction()).isEqualTo(Intent.ACTION_DEFINE); |
| 120 | assertThat(intents.get(1).intent.getAction()).isEqualTo(Intent.ACTION_TRANSLATE); |
Tony Mak | fc039c3 | 2019-01-17 19:32:08 +0000 | [diff] [blame] | 121 | } |
| 122 | } |