summaryrefslogtreecommitdiff
path: root/java/tests/instrumentation/src/com/android/textclassifier/common/TextClassifierSettingsTest.java
blob: 2f936e48c9ae2274761db9a1956ac77f71a8b769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.textclassifier.common;

import static com.google.common.truth.Truth.assertThat;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import com.android.textclassifier.testing.TestingDeviceConfig;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.function.Consumer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class TextClassifierSettingsTest {
  private static final String WRITE_DEVICE_CONFIG_PERMISSION =
      "android.permission.WRITE_DEVICE_CONFIG";
  private static final float EPSILON = 0.0001f;

  @Before
  public void setup() {
    InstrumentationRegistry.getInstrumentation()
        .getUiAutomation()
        .adoptShellPermissionIdentity(WRITE_DEVICE_CONFIG_PERMISSION);
  }

  @After
  public void tearDown() {
    InstrumentationRegistry.getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
  }

  @Test
  public void booleanSetting() {
    assertSettings(
        TextClassifierSettings.TEMPLATE_INTENT_FACTORY_ENABLED,
        "false",
        settings -> assertThat(settings.isTemplateIntentFactoryEnabled()).isFalse());
  }

  @Test
  public void intSetting() {
    assertSettings(
        TextClassifierSettings.SUGGEST_SELECTION_MAX_RANGE_LENGTH,
        "8",
        settings -> assertThat(settings.getSuggestSelectionMaxRangeLength()).isEqualTo(8));
  }

  @Test
  public void floatSetting() {
    assertSettings(
        TextClassifierSettings.LANG_ID_THRESHOLD_OVERRIDE,
        "3.14",
        settings -> assertThat(settings.getLangIdThresholdOverride()).isWithin(EPSILON).of(3.14f));
  }

  @Test
  public void stringListSetting() {
    assertSettings(
        TextClassifierSettings.ENTITY_LIST_DEFAULT,
        "email:url",
        settings ->
            assertThat(settings.getEntityListDefault()).containsExactly("email", "url").inOrder());
  }

  @Test
  public void floatListSetting() {
    assertSettings(
        TextClassifierSettings.LANG_ID_CONTEXT_SETTINGS,
        "30:0.5:0.3",
        settings ->
            assertThat(settings.getLangIdContextSettings())
                .usingTolerance(EPSILON)
                .containsExactly(30f, 0.5f, 0.3f)
                .inOrder());
  }

  @Test
  public void getLanguageTagsForManifestAndUrlMap() {
    assertSettings(
        ImmutableMap.of(
            "manifest_url_annotator_en", "https://annotator-en",
            "manifest_url_annotator_en-us", "https://annotator-en-us",
            "manifest_url_annotator_zh-hant-hk", "https://annotator-zh",
            "manifest_url_lang_id_universal", "https://lang_id"),
        settings ->
            assertThat(settings.getLanguageTagAndManifestUrlMap(ModelType.ANNOTATOR))
                .containsExactlyEntriesIn(
                    ImmutableMap.of(
                        "en", "https://annotator-en",
                        "en-us", "https://annotator-en-us",
                        "zh-hant-hk", "https://annotator-zh")));

    assertSettings(
        ImmutableMap.of(
            "manifest_url_annotator_en", "https://annotator-en",
            "manifest_url_annotator_en-us", "https://annotator-en-us",
            "manifest_url_annotator_zh-hant-hk", "https://annotator-zh",
            "manifest_url_lang_id_universal", "https://lang_id"),
        settings ->
            assertThat(settings.getLanguageTagAndManifestUrlMap(ModelType.LANG_ID))
                .containsExactlyEntriesIn(ImmutableMap.of("universal", "https://lang_id")));

    assertSettings(
        ImmutableMap.of(
            "manifest_url_annotator_en", "https://annotator-en",
            "manifest_url_annotator_en-us", "https://annotator-en-us",
            "manifest_url_annotator_zh-hant-hk", "https://annotator-zh",
            "manifest_url_lang_id_universal", "https://lang_id"),
        settings ->
            assertThat(settings.getLanguageTagAndManifestUrlMap(ModelType.ACTIONS_SUGGESTIONS))
                .isEmpty());
  }

  private static void assertSettings(
      String key, String value, Consumer<TextClassifierSettings> settingsConsumer) {
    assertSettings(ImmutableMap.of(key, value), settingsConsumer);
  }

  private static void assertSettings(
      Map<String, String> keyValueMap, Consumer<TextClassifierSettings> settingsConsumer) {
    TestingDeviceConfig deviceConfig = new TestingDeviceConfig();
    TextClassifierSettings settings = new TextClassifierSettings(deviceConfig, /* isWear= */ false);
    for (String key : keyValueMap.keySet()) {
      deviceConfig.setConfig(key, keyValueMap.get(key));
    }
    settingsConsumer.accept(settings);
  }
}