blob: 82fa73f76207960cb8214ff79a033d31b1b7b93e [file] [log] [blame]
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +00001/*
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
Joanne Chungc44f5292019-12-02 21:18:43 +080019import static com.google.common.truth.Truth.assertThat;
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +000020import static com.google.common.truth.Truth.assertWithMessage;
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000021
Joanne Chungc44f5292019-12-02 21:18:43 +080022import android.provider.DeviceConfig;
23
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090024import androidx.test.filters.SmallTest;
25import androidx.test.runner.AndroidJUnit4;
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000026
Joanne Chungc44f5292019-12-02 21:18:43 +080027import com.google.common.primitives.Floats;
28
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000029import org.junit.Test;
30import org.junit.runner.RunWith;
31
32@SmallTest
33@RunWith(AndroidJUnit4.class)
34public class TextClassificationConstantsTest {
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +000035 private static final float EPSILON = 0.0001f;
36
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000037 @Test
Joanne Chungc44f5292019-12-02 21:18:43 +080038 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 Tokia77dba62019-01-25 19:27:11 +000042
Joanne Chungc44f5292019-12-02 21:18:43 +080043 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 Tokidb8fc312018-02-26 21:37:51 +000054 }
55
56 @Test
Joanne Chungc44f5292019-12-02 21:18:43 +080057 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 Tokia77dba62019-01-25 19:27:11 +000061
Joanne Chungc44f5292019-12-02 21:18:43 +080062 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 Tokidb8fc312018-02-26 21:37:51 +000073 }
74
75 @Test
Joanne Chungc44f5292019-12-02 21:18:43 +080076 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 Tokia77dba62019-01-25 19:27:11 +000080
Joanne Chungc44f5292019-12-02 21:18:43 +080081 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 Tokidb8fc312018-02-26 21:37:51 +0000155 }
156}