• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 com.android.textclassifier.common;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import androidx.test.ext.junit.runners.AndroidJUnit4;
22 import androidx.test.filters.SmallTest;
23 import androidx.test.platform.app.InstrumentationRegistry;
24 import com.android.textclassifier.testing.TestingDeviceConfig;
25 import com.google.common.collect.ImmutableMap;
26 import java.util.Map;
27 import java.util.function.Consumer;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4.class)
35 public class TextClassifierSettingsTest {
36   private static final String WRITE_DEVICE_CONFIG_PERMISSION =
37       "android.permission.WRITE_DEVICE_CONFIG";
38   private static final float EPSILON = 0.0001f;
39 
40   @Before
setup()41   public void setup() {
42     InstrumentationRegistry.getInstrumentation()
43         .getUiAutomation()
44         .adoptShellPermissionIdentity(WRITE_DEVICE_CONFIG_PERMISSION);
45   }
46 
47   @After
tearDown()48   public void tearDown() {
49     InstrumentationRegistry.getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
50   }
51 
52   @Test
booleanSetting()53   public void booleanSetting() {
54     assertSettings(
55         TextClassifierSettings.TEMPLATE_INTENT_FACTORY_ENABLED,
56         "false",
57         settings -> assertThat(settings.isTemplateIntentFactoryEnabled()).isFalse());
58   }
59 
60   @Test
intSetting()61   public void intSetting() {
62     assertSettings(
63         TextClassifierSettings.SUGGEST_SELECTION_MAX_RANGE_LENGTH,
64         "8",
65         settings -> assertThat(settings.getSuggestSelectionMaxRangeLength()).isEqualTo(8));
66   }
67 
68   @Test
floatSetting()69   public void floatSetting() {
70     assertSettings(
71         TextClassifierSettings.LANG_ID_THRESHOLD_OVERRIDE,
72         "3.14",
73         settings -> assertThat(settings.getLangIdThresholdOverride()).isWithin(EPSILON).of(3.14f));
74   }
75 
76   @Test
stringListSetting()77   public void stringListSetting() {
78     assertSettings(
79         TextClassifierSettings.ENTITY_LIST_DEFAULT,
80         "email:url",
81         settings ->
82             assertThat(settings.getEntityListDefault()).containsExactly("email", "url").inOrder());
83   }
84 
85   @Test
floatListSetting()86   public void floatListSetting() {
87     assertSettings(
88         TextClassifierSettings.LANG_ID_CONTEXT_SETTINGS,
89         "30:0.5:0.3",
90         settings ->
91             assertThat(settings.getLangIdContextSettings())
92                 .usingTolerance(EPSILON)
93                 .containsExactly(30f, 0.5f, 0.3f)
94                 .inOrder());
95   }
96 
97   @Test
getLanguageTagsForManifestAndUrlMap()98   public void getLanguageTagsForManifestAndUrlMap() {
99     assertSettings(
100         ImmutableMap.of(
101             "manifest_url_annotator_en", "https://annotator-en",
102             "manifest_url_annotator_en-us", "https://annotator-en-us",
103             "manifest_url_annotator_zh-hant-hk", "https://annotator-zh",
104             "manifest_url_lang_id_universal", "https://lang_id"),
105         settings ->
106             assertThat(settings.getLanguageTagAndManifestUrlMap(ModelType.ANNOTATOR))
107                 .containsExactlyEntriesIn(
108                     ImmutableMap.of(
109                         "en", "https://annotator-en",
110                         "en-us", "https://annotator-en-us",
111                         "zh-hant-hk", "https://annotator-zh")));
112 
113     assertSettings(
114         ImmutableMap.of(
115             "manifest_url_annotator_en", "https://annotator-en",
116             "manifest_url_annotator_en-us", "https://annotator-en-us",
117             "manifest_url_annotator_zh-hant-hk", "https://annotator-zh",
118             "manifest_url_lang_id_universal", "https://lang_id"),
119         settings ->
120             assertThat(settings.getLanguageTagAndManifestUrlMap(ModelType.LANG_ID))
121                 .containsExactlyEntriesIn(ImmutableMap.of("universal", "https://lang_id")));
122 
123     assertSettings(
124         ImmutableMap.of(
125             "manifest_url_annotator_en", "https://annotator-en",
126             "manifest_url_annotator_en-us", "https://annotator-en-us",
127             "manifest_url_annotator_zh-hant-hk", "https://annotator-zh",
128             "manifest_url_lang_id_universal", "https://lang_id"),
129         settings ->
130             assertThat(settings.getLanguageTagAndManifestUrlMap(ModelType.ACTIONS_SUGGESTIONS))
131                 .isEmpty());
132   }
133 
assertSettings( String key, String value, Consumer<TextClassifierSettings> settingsConsumer)134   private static void assertSettings(
135       String key, String value, Consumer<TextClassifierSettings> settingsConsumer) {
136     assertSettings(ImmutableMap.of(key, value), settingsConsumer);
137   }
138 
assertSettings( Map<String, String> keyValueMap, Consumer<TextClassifierSettings> settingsConsumer)139   private static void assertSettings(
140       Map<String, String> keyValueMap, Consumer<TextClassifierSettings> settingsConsumer) {
141     TestingDeviceConfig deviceConfig = new TestingDeviceConfig();
142     TextClassifierSettings settings = new TextClassifierSettings(deviceConfig, /* isWear= */ false);
143     for (String key : keyValueMap.keySet()) {
144       deviceConfig.setConfig(key, keyValueMap.get(key));
145     }
146     settingsConsumer.accept(settings);
147   }
148 }
149