blob: 789b829456f3f8307a2e9ad7e3cafa1565b6b6f6 [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
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +000019import static com.google.common.truth.Truth.assertWithMessage;
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000020
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090021import androidx.test.filters.SmallTest;
22import androidx.test.runner.AndroidJUnit4;
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000023
Tony Mak72e17972019-03-16 10:28:42 +000024import org.junit.Assert;
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000025import org.junit.Test;
26import org.junit.runner.RunWith;
27
28@SmallTest
29@RunWith(AndroidJUnit4.class)
30public class TextClassificationConstantsTest {
31
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +000032 private static final float EPSILON = 0.0001f;
33
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000034 @Test
35 public void testLoadFromString() {
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +000036 final String s = "local_textclassifier_enabled=true,"
37 + "system_textclassifier_enabled=true,"
38 + "model_dark_launch_enabled=true,"
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +000039 + "smart_selection_enabled=true,"
40 + "smart_text_share_enabled=true,"
41 + "smart_linkify_enabled=true,"
42 + "smart_select_animation_enabled=true,"
43 + "suggest_selection_max_range_length=10,"
44 + "classify_text_max_range_length=11,"
45 + "generate_links_max_text_length=12,"
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +000046 + "generate_links_log_sample_rate=13,"
47 + "entity_list_default=phone,"
48 + "entity_list_not_editable=address:flight,"
49 + "entity_list_editable=date:datetime,"
50 + "in_app_conversation_action_types_default=text_reply,"
51 + "notification_conversation_action_types_default=send_email:call_phone,"
Tony Mak72e17972019-03-16 10:28:42 +000052 + "lang_id_threshold_override=0.3,"
Abodunrinwa Toki0634af32019-04-04 13:10:59 +010053 + "lang_id_context_settings=10:1:0.5,"
54 + "detect_language_from_text_enabled=true,"
55 + "template_intent_factory_enabled=true,"
56 + "translate_in_classification_enabled=true";
57 final TextClassificationConstants constants = new TextClassificationConstants(() -> s);
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +000058
59 assertWithMessage("local_textclassifier_enabled")
60 .that(constants.isLocalTextClassifierEnabled()).isTrue();
61 assertWithMessage("system_textclassifier_enabled")
62 .that(constants.isSystemTextClassifierEnabled()).isTrue();
63 assertWithMessage("model_dark_launch_enabled")
64 .that(constants.isModelDarkLaunchEnabled()).isTrue();
65 assertWithMessage("smart_selection_enabled")
66 .that(constants.isSmartSelectionEnabled()).isTrue();
67 assertWithMessage("smart_text_share_enabled")
68 .that(constants.isSmartTextShareEnabled()).isTrue();
69 assertWithMessage("smart_linkify_enabled")
70 .that(constants.isSmartLinkifyEnabled()).isTrue();
71 assertWithMessage("smart_select_animation_enabled")
72 .that(constants.isSmartSelectionAnimationEnabled()).isTrue();
73 assertWithMessage("suggest_selection_max_range_length")
74 .that(constants.getSuggestSelectionMaxRangeLength()).isEqualTo(10);
75 assertWithMessage("classify_text_max_range_length")
76 .that(constants.getClassifyTextMaxRangeLength()).isEqualTo(11);
77 assertWithMessage("generate_links_max_text_length")
78 .that(constants.getGenerateLinksMaxTextLength()).isEqualTo(12);
79 assertWithMessage("generate_links_log_sample_rate")
80 .that(constants.getGenerateLinksLogSampleRate()).isEqualTo(13);
81 assertWithMessage("entity_list_default")
82 .that(constants.getEntityListDefault())
83 .containsExactly("phone");
84 assertWithMessage("entity_list_not_editable")
85 .that(constants.getEntityListNotEditable())
86 .containsExactly("address", "flight");
87 assertWithMessage("entity_list_editable")
88 .that(constants.getEntityListEditable())
89 .containsExactly("date", "datetime");
90 assertWithMessage("in_app_conversation_action_types_default")
91 .that(constants.getInAppConversationActionTypes())
92 .containsExactly("text_reply");
93 assertWithMessage("notification_conversation_action_types_default")
94 .that(constants.getNotificationConversationActionTypes())
95 .containsExactly("send_email", "call_phone");
96 assertWithMessage("lang_id_threshold_override")
97 .that(constants.getLangIdThresholdOverride()).isWithin(EPSILON).of(0.3f);
Tony Mak72e17972019-03-16 10:28:42 +000098 Assert.assertArrayEquals("lang_id_context_settings",
99 constants.getLangIdContextSettings(), new float[]{10, 1, 0.5f}, EPSILON);
Abodunrinwa Toki0634af32019-04-04 13:10:59 +0100100 assertWithMessage("detect_language_from_text_enabled")
101 .that(constants.isLocalTextClassifierEnabled()).isTrue();
102 assertWithMessage("template_intent_factory_enabled")
103 .that(constants.isLocalTextClassifierEnabled()).isTrue();
104 assertWithMessage("translate_in_classification_enabled")
105 .that(constants.isLocalTextClassifierEnabled()).isTrue();
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +0000106 }
107
108 @Test
109 public void testLoadFromString_differentValues() {
Abodunrinwa Tokic7073a42018-02-28 23:02:13 +0000110 final String s = "local_textclassifier_enabled=false,"
111 + "system_textclassifier_enabled=false,"
112 + "model_dark_launch_enabled=false,"
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +0000113 + "smart_selection_enabled=false,"
114 + "smart_text_share_enabled=false,"
115 + "smart_linkify_enabled=false,"
116 + "smart_select_animation_enabled=false,"
117 + "suggest_selection_max_range_length=8,"
118 + "classify_text_max_range_length=7,"
119 + "generate_links_max_text_length=6,"
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +0000120 + "generate_links_log_sample_rate=5,"
121 + "entity_list_default=email:url,"
122 + "entity_list_not_editable=date,"
123 + "entity_list_editable=flight,"
124 + "in_app_conversation_action_types_default=view_map:track_flight,"
125 + "notification_conversation_action_types_default=share_location,"
Tony Mak72e17972019-03-16 10:28:42 +0000126 + "lang_id_threshold_override=2,"
Abodunrinwa Toki0634af32019-04-04 13:10:59 +0100127 + "lang_id_context_settings=30:0.5:0.3,"
128 + "detect_language_from_text_enabled=false,"
129 + "template_intent_factory_enabled=false,"
130 + "translate_in_classification_enabled=false";
131 final TextClassificationConstants constants = new TextClassificationConstants(() -> s);
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +0000132
133 assertWithMessage("local_textclassifier_enabled")
134 .that(constants.isLocalTextClassifierEnabled()).isFalse();
135 assertWithMessage("system_textclassifier_enabled")
136 .that(constants.isSystemTextClassifierEnabled()).isFalse();
137 assertWithMessage("model_dark_launch_enabled")
138 .that(constants.isModelDarkLaunchEnabled()).isFalse();
139 assertWithMessage("smart_selection_enabled")
140 .that(constants.isSmartSelectionEnabled()).isFalse();
141 assertWithMessage("smart_text_share_enabled")
142 .that(constants.isSmartTextShareEnabled()).isFalse();
143 assertWithMessage("smart_linkify_enabled")
144 .that(constants.isSmartLinkifyEnabled()).isFalse();
145 assertWithMessage("smart_select_animation_enabled")
146 .that(constants.isSmartSelectionAnimationEnabled()).isFalse();
147 assertWithMessage("suggest_selection_max_range_length")
148 .that(constants.getSuggestSelectionMaxRangeLength()).isEqualTo(8);
149 assertWithMessage("classify_text_max_range_length")
150 .that(constants.getClassifyTextMaxRangeLength()).isEqualTo(7);
151 assertWithMessage("generate_links_max_text_length")
152 .that(constants.getGenerateLinksMaxTextLength()).isEqualTo(6);
153 assertWithMessage("generate_links_log_sample_rate")
154 .that(constants.getGenerateLinksLogSampleRate()).isEqualTo(5);
155 assertWithMessage("entity_list_default")
156 .that(constants.getEntityListDefault())
157 .containsExactly("email", "url");
158 assertWithMessage("entity_list_not_editable")
159 .that(constants.getEntityListNotEditable())
160 .containsExactly("date");
161 assertWithMessage("entity_list_editable")
162 .that(constants.getEntityListEditable())
163 .containsExactly("flight");
164 assertWithMessage("in_app_conversation_action_types_default")
165 .that(constants.getInAppConversationActionTypes())
166 .containsExactly("view_map", "track_flight");
167 assertWithMessage("notification_conversation_action_types_default")
168 .that(constants.getNotificationConversationActionTypes())
169 .containsExactly("share_location");
170 assertWithMessage("lang_id_threshold_override")
171 .that(constants.getLangIdThresholdOverride()).isWithin(EPSILON).of(2f);
Tony Mak72e17972019-03-16 10:28:42 +0000172 Assert.assertArrayEquals("lang_id_context_settings",
173 constants.getLangIdContextSettings(), new float[]{30, 0.5f, 0.3f}, EPSILON);
Abodunrinwa Toki0634af32019-04-04 13:10:59 +0100174 assertWithMessage("detect_language_from_text_enabled")
175 .that(constants.isLocalTextClassifierEnabled()).isFalse();
176 assertWithMessage("template_intent_factory_enabled")
177 .that(constants.isLocalTextClassifierEnabled()).isFalse();
178 assertWithMessage("translate_in_classification_enabled")
179 .that(constants.isLocalTextClassifierEnabled()).isFalse();
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +0000180 }
181
182 @Test
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +0000183 public void testLoadFromString_defaultValues() {
Abodunrinwa Toki0634af32019-04-04 13:10:59 +0100184 final TextClassificationConstants constants = new TextClassificationConstants(() -> "");
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +0000185
186 assertWithMessage("local_textclassifier_enabled")
187 .that(constants.isLocalTextClassifierEnabled()).isTrue();
188 assertWithMessage("system_textclassifier_enabled")
189 .that(constants.isSystemTextClassifierEnabled()).isTrue();
190 assertWithMessage("model_dark_launch_enabled")
191 .that(constants.isModelDarkLaunchEnabled()).isFalse();
192 assertWithMessage("smart_selection_enabled")
193 .that(constants.isSmartSelectionEnabled()).isTrue();
194 assertWithMessage("smart_text_share_enabled")
195 .that(constants.isSmartTextShareEnabled()).isTrue();
196 assertWithMessage("smart_linkify_enabled")
197 .that(constants.isSmartLinkifyEnabled()).isTrue();
198 assertWithMessage("smart_select_animation_enabled")
199 .that(constants.isSmartSelectionAnimationEnabled()).isTrue();
200 assertWithMessage("suggest_selection_max_range_length")
201 .that(constants.getSuggestSelectionMaxRangeLength()).isEqualTo(10 * 1000);
202 assertWithMessage("classify_text_max_range_length")
203 .that(constants.getClassifyTextMaxRangeLength()).isEqualTo(10 * 1000);
204 assertWithMessage("generate_links_max_text_length")
205 .that(constants.getGenerateLinksMaxTextLength()).isEqualTo(100 * 1000);
206 assertWithMessage("generate_links_log_sample_rate")
207 .that(constants.getGenerateLinksLogSampleRate()).isEqualTo(100);
208 assertWithMessage("entity_list_default")
209 .that(constants.getEntityListDefault())
210 .containsExactly("address", "email", "url", "phone", "date", "datetime", "flight");
211 assertWithMessage("entity_list_not_editable")
212 .that(constants.getEntityListNotEditable())
213 .containsExactly("address", "email", "url", "phone", "date", "datetime", "flight");
214 assertWithMessage("entity_list_editable")
215 .that(constants.getEntityListEditable())
216 .containsExactly("address", "email", "url", "phone", "date", "datetime", "flight");
217 assertWithMessage("in_app_conversation_action_types_default")
218 .that(constants.getInAppConversationActionTypes())
219 .containsExactly("text_reply", "create_reminder", "call_phone", "open_url",
Tony Maka396be42019-02-07 14:37:22 +0000220 "send_email", "send_sms", "track_flight", "view_calendar", "view_map",
Tony Mak09214422019-03-01 18:25:23 +0000221 "add_contact", "copy");
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +0000222 assertWithMessage("notification_conversation_action_types_default")
223 .that(constants.getNotificationConversationActionTypes())
224 .containsExactly("text_reply", "create_reminder", "call_phone", "open_url",
Tony Maka396be42019-02-07 14:37:22 +0000225 "send_email", "send_sms", "track_flight", "view_calendar", "view_map",
Tony Mak09214422019-03-01 18:25:23 +0000226 "add_contact", "copy");
Abodunrinwa Tokia77dba62019-01-25 19:27:11 +0000227 assertWithMessage("lang_id_threshold_override")
228 .that(constants.getLangIdThresholdOverride()).isWithin(EPSILON).of(-1f);
Tony Mak72e17972019-03-16 10:28:42 +0000229 Assert.assertArrayEquals("lang_id_context_settings",
230 constants.getLangIdContextSettings(), new float[]{20, 1, 0.4f}, EPSILON);
Abodunrinwa Toki0634af32019-04-04 13:10:59 +0100231 assertWithMessage("detect_language_from_text_enabled")
232 .that(constants.isLocalTextClassifierEnabled()).isTrue();
233 assertWithMessage("template_intent_factory_enabled")
234 .that(constants.isLocalTextClassifierEnabled()).isTrue();
235 assertWithMessage("translate_in_classification_enabled")
236 .that(constants.isLocalTextClassifierEnabled()).isTrue();
Abodunrinwa Tokidb8fc312018-02-26 21:37:51 +0000237 }
238}