blob: 1b3c7245233916d221ba5698d352f165007db9cb [file] [log] [blame]
Tony Makfc374572019-03-05 14:46:24 +00001/*
2 * Copyright (C) 2019 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 */
16package android.view.textclassifier;
17
18import static com.google.common.truth.Truth.assertThat;
19
20import android.provider.DeviceConfig;
21import android.support.test.uiautomator.UiDevice;
22
23import androidx.test.InstrumentationRegistry;
24import androidx.test.filters.SmallTest;
25import androidx.test.runner.AndroidJUnit4;
26
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31
32import java.io.IOException;
33
34@SmallTest
35@RunWith(AndroidJUnit4.class)
36public class ConfigParserTest {
37 private static final String SETTINGS = "int=42,float=12.3,boolean=true,string=abc";
38 private static final String CLEAR_DEVICE_CONFIG_KEY_CMD =
39 "device_config delete " + DeviceConfig.NAMESPACE_TEXTCLASSIFIER;
40 private static final String[] DEVICE_CONFIG_KEYS = new String[]{
41 "boolean",
42 "string",
43 "int",
44 "float"
45 };
46
47 private ConfigParser mConfigParser;
48
49 @Before
50 public void setup() throws IOException {
51 mConfigParser = new ConfigParser(SETTINGS);
52 clearDeviceConfig();
53 }
54
55 @After
56 public void tearDown() throws IOException {
57 clearDeviceConfig();
58 }
59
60 @Test
61 public void getBoolean_deviceConfig() {
62 DeviceConfig.setProperty(
63 DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
64 "boolean",
65 "false",
66 false);
67 boolean value = mConfigParser.getBoolean("boolean", true);
68 assertThat(value).isFalse();
69 }
70
71 @Test
72 public void getBoolean_settings() {
73 boolean value = mConfigParser.getBoolean(
74 "boolean",
75 false);
76 assertThat(value).isTrue();
77 }
78
79 @Test
80 public void getInt_deviceConfig() {
81 DeviceConfig.setProperty(
82 DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
83 "int",
84 "1",
85 false);
86 int value = mConfigParser.getInt("int", 0);
87 assertThat(value).isEqualTo(1);
88 }
89
90 @Test
91 public void getInt_settings() {
92 int value = mConfigParser.getInt("int", 0);
93 assertThat(value).isEqualTo(42);
94 }
95
96 @Test
97 public void getFloat_deviceConfig() {
98 DeviceConfig.setProperty(
99 DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
100 "float",
101 "3.14",
102 false);
103 float value = mConfigParser.getFloat("float", 0);
104 assertThat(value).isWithin(0.0001f).of(3.14f);
105 }
106
107 @Test
108 public void getFloat_settings() {
109 float value = mConfigParser.getFloat("float", 0);
110 assertThat(value).isWithin(0.0001f).of(12.3f);
111 }
112
113 @Test
114 public void getString_deviceConfig() {
115 DeviceConfig.setProperty(
116 DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
117 "string",
118 "hello",
119 false);
120 String value = mConfigParser.getString("string", "");
121 assertThat(value).isEqualTo("hello");
122 }
123
124 @Test
125 public void getString_settings() {
126 String value = mConfigParser.getString("string", "");
127 assertThat(value).isEqualTo("abc");
128 }
129
130 private static void clearDeviceConfig() throws IOException {
131 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
132 for (String key : DEVICE_CONFIG_KEYS) {
133 uiDevice.executeShellCommand(CLEAR_DEVICE_CONFIG_KEY_CMD + " " + key);
134 }
135 }
136}