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 | |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertWithMessage; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 20 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 21 | import androidx.test.filters.SmallTest; |
| 22 | import androidx.test.runner.AndroidJUnit4; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 23 | |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 24 | import org.junit.Assert; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | |
| 28 | @SmallTest |
| 29 | @RunWith(AndroidJUnit4.class) |
| 30 | public class TextClassificationConstantsTest { |
| 31 | |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 32 | private static final float EPSILON = 0.0001f; |
| 33 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 34 | @Test |
| 35 | public void testLoadFromString() { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 36 | final String s = "local_textclassifier_enabled=true," |
| 37 | + "system_textclassifier_enabled=true," |
| 38 | + "model_dark_launch_enabled=true," |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 39 | + "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 Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 46 | + "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 Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 52 | + "lang_id_threshold_override=0.3," |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame^] | 53 | + "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 Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 58 | |
| 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 Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 98 | Assert.assertArrayEquals("lang_id_context_settings", |
| 99 | constants.getLangIdContextSettings(), new float[]{10, 1, 0.5f}, EPSILON); |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame^] | 100 | 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 Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testLoadFromString_differentValues() { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 110 | final String s = "local_textclassifier_enabled=false," |
| 111 | + "system_textclassifier_enabled=false," |
| 112 | + "model_dark_launch_enabled=false," |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 113 | + "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 Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 120 | + "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 Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 126 | + "lang_id_threshold_override=2," |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame^] | 127 | + "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 Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 132 | |
| 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 Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 172 | Assert.assertArrayEquals("lang_id_context_settings", |
| 173 | constants.getLangIdContextSettings(), new float[]{30, 0.5f, 0.3f}, EPSILON); |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame^] | 174 | 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 Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Test |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 183 | public void testLoadFromString_defaultValues() { |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame^] | 184 | final TextClassificationConstants constants = new TextClassificationConstants(() -> ""); |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 185 | |
| 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 Mak | a396be4 | 2019-02-07 14:37:22 +0000 | [diff] [blame] | 220 | "send_email", "send_sms", "track_flight", "view_calendar", "view_map", |
Tony Mak | 0921442 | 2019-03-01 18:25:23 +0000 | [diff] [blame] | 221 | "add_contact", "copy"); |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 222 | assertWithMessage("notification_conversation_action_types_default") |
| 223 | .that(constants.getNotificationConversationActionTypes()) |
| 224 | .containsExactly("text_reply", "create_reminder", "call_phone", "open_url", |
Tony Mak | a396be4 | 2019-02-07 14:37:22 +0000 | [diff] [blame] | 225 | "send_email", "send_sms", "track_flight", "view_calendar", "view_map", |
Tony Mak | 0921442 | 2019-03-01 18:25:23 +0000 | [diff] [blame] | 226 | "add_contact", "copy"); |
Abodunrinwa Toki | a77dba6 | 2019-01-25 19:27:11 +0000 | [diff] [blame] | 227 | assertWithMessage("lang_id_threshold_override") |
| 228 | .that(constants.getLangIdThresholdOverride()).isWithin(EPSILON).of(-1f); |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 229 | Assert.assertArrayEquals("lang_id_context_settings", |
| 230 | constants.getLangIdContextSettings(), new float[]{20, 1, 0.4f}, EPSILON); |
Abodunrinwa Toki | 0634af3 | 2019-04-04 13:10:59 +0100 | [diff] [blame^] | 231 | 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 Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 237 | } |
| 238 | } |