Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [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 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 20 | import static com.google.common.truth.Truth.assertWithMessage; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 21 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 22 | import android.provider.DeviceConfig; |
| 23 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 24 | import androidx.test.filters.SmallTest; |
| 25 | import androidx.test.runner.AndroidJUnit4; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 26 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 27 | import com.google.common.primitives.Floats; |
| 28 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | |
| 32 | @SmallTest |
| 33 | @RunWith(AndroidJUnit4.class) |
| 34 | public class TextClassificationConstantsTest { |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 35 | private static final float EPSILON = 0.0001f; |
| 36 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 37 | @Test |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 38 | public void testLoadFromDeviceConfig_booleanValue() throws Exception { |
| 39 | // Saves config original value. |
| 40 | final String originalValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 41 | TextClassificationConstants.LOCAL_TEXT_CLASSIFIER_ENABLED); |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 42 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 43 | final TextClassificationConstants constants = new TextClassificationConstants(); |
| 44 | try { |
| 45 | // Sets and checks different value. |
| 46 | setDeviceConfig(TextClassificationConstants.LOCAL_TEXT_CLASSIFIER_ENABLED, "false"); |
| 47 | assertWithMessage(TextClassificationConstants.LOCAL_TEXT_CLASSIFIER_ENABLED) |
| 48 | .that(constants.isLocalTextClassifierEnabled()).isFalse(); |
| 49 | } finally { |
| 50 | // Restores config original value. |
| 51 | setDeviceConfig(TextClassificationConstants.LOCAL_TEXT_CLASSIFIER_ENABLED, |
| 52 | originalValue); |
| 53 | } |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Test |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 57 | public void testLoadFromDeviceConfig_IntValue() throws Exception { |
| 58 | // Saves config original value. |
| 59 | final String originalValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 60 | TextClassificationConstants.SUGGEST_SELECTION_MAX_RANGE_LENGTH); |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 61 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 62 | final TextClassificationConstants constants = new TextClassificationConstants(); |
| 63 | try { |
| 64 | // Sets and checks different value. |
| 65 | setDeviceConfig(TextClassificationConstants.SUGGEST_SELECTION_MAX_RANGE_LENGTH, "8"); |
| 66 | assertWithMessage(TextClassificationConstants.SUGGEST_SELECTION_MAX_RANGE_LENGTH) |
| 67 | .that(constants.getSuggestSelectionMaxRangeLength()).isEqualTo(8); |
| 68 | } finally { |
| 69 | // Restores config original value. |
| 70 | setDeviceConfig(TextClassificationConstants.SUGGEST_SELECTION_MAX_RANGE_LENGTH, |
| 71 | originalValue); |
| 72 | } |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 76 | public void testLoadFromDeviceConfig_StringValue() throws Exception { |
| 77 | // Saves config original value. |
| 78 | final String originalValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 79 | TextClassificationConstants.TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE); |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 80 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 81 | final TextClassificationConstants constants = new TextClassificationConstants(); |
| 82 | try { |
| 83 | // Sets and checks different value. |
| 84 | final String testTextClassifier = "com.example.textclassifier"; |
| 85 | setDeviceConfig(TextClassificationConstants.TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE, |
| 86 | testTextClassifier); |
| 87 | assertWithMessage(TextClassificationConstants.TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE) |
| 88 | .that(constants.getTextClassifierServicePackageOverride()).isEqualTo( |
| 89 | testTextClassifier); |
| 90 | } finally { |
| 91 | // Restores config original value. |
| 92 | setDeviceConfig(TextClassificationConstants.TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE, |
| 93 | originalValue); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testLoadFromDeviceConfig_FloatValue() throws Exception { |
| 99 | // Saves config original value. |
| 100 | final String originalValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 101 | TextClassificationConstants.LANG_ID_THRESHOLD_OVERRIDE); |
| 102 | |
| 103 | final TextClassificationConstants constants = new TextClassificationConstants(); |
| 104 | try { |
| 105 | // Sets and checks different value. |
| 106 | setDeviceConfig(TextClassificationConstants.LANG_ID_THRESHOLD_OVERRIDE, "2"); |
| 107 | assertWithMessage(TextClassificationConstants.LANG_ID_THRESHOLD_OVERRIDE) |
| 108 | .that(constants.getLangIdThresholdOverride()).isWithin(EPSILON).of(2f); |
| 109 | } finally { |
| 110 | // Restores config original value. |
| 111 | setDeviceConfig(TextClassificationConstants.LANG_ID_THRESHOLD_OVERRIDE, originalValue); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testLoadFromDeviceConfig_StringList() throws Exception { |
| 117 | // Saves config original value. |
| 118 | final String originalValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 119 | TextClassificationConstants.ENTITY_LIST_DEFAULT); |
| 120 | |
| 121 | final TextClassificationConstants constants = new TextClassificationConstants(); |
| 122 | try { |
| 123 | // Sets and checks different value. |
| 124 | setDeviceConfig(TextClassificationConstants.ENTITY_LIST_DEFAULT, "email:url"); |
| 125 | assertWithMessage(TextClassificationConstants.ENTITY_LIST_DEFAULT) |
| 126 | .that(constants.getEntityListDefault()) |
| 127 | .containsExactly("email", "url"); |
| 128 | } finally { |
| 129 | // Restores config original value. |
| 130 | setDeviceConfig(TextClassificationConstants.ENTITY_LIST_DEFAULT, originalValue); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testLoadFromDeviceConfig_FloatList() throws Exception { |
| 136 | // Saves config original value. |
| 137 | final String originalValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 138 | TextClassificationConstants.LANG_ID_CONTEXT_SETTINGS); |
| 139 | |
| 140 | final TextClassificationConstants constants = new TextClassificationConstants(); |
| 141 | try { |
| 142 | // Sets and checks different value. |
| 143 | setDeviceConfig(TextClassificationConstants.LANG_ID_CONTEXT_SETTINGS, "30:0.5:0.3"); |
| 144 | assertThat(Floats.asList(constants.getLangIdContextSettings())).containsExactly(30f, |
| 145 | 0.5f, 0.3f).inOrder(); |
| 146 | } finally { |
| 147 | // Restores config original value. |
| 148 | setDeviceConfig(TextClassificationConstants.LANG_ID_CONTEXT_SETTINGS, originalValue); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private void setDeviceConfig(String key, String value) { |
| 153 | DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, key, |
| 154 | value, /* makeDefault */ false); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 155 | } |
| 156 | } |