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 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.provider.DeviceConfig; 22 23 import androidx.test.filters.SmallTest; 24 import androidx.test.runner.AndroidJUnit4; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import java.util.function.Consumer; 30 31 @SmallTest 32 @RunWith(AndroidJUnit4.class) 33 public class TextClassificationConstantsTest { 34 35 @Test booleanSettings()36 public void booleanSettings() { 37 assertSettings( 38 TextClassificationConstants.LOCAL_TEXT_CLASSIFIER_ENABLED, 39 "false", 40 settings -> assertThat(settings.isLocalTextClassifierEnabled()).isFalse()); 41 } 42 43 @Test intSettings()44 public void intSettings() { 45 assertSettings( 46 TextClassificationConstants.GENERATE_LINKS_MAX_TEXT_LENGTH, 47 "128", 48 settings -> assertThat(settings.getGenerateLinksMaxTextLength()).isEqualTo(128)); 49 } 50 51 @Test stringSettings()52 public void stringSettings() { 53 assertSettings( 54 TextClassificationConstants.TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE, 55 "com.example.textclassifier", 56 settings -> assertThat( 57 settings.getTextClassifierServicePackageOverride()) 58 .isEqualTo("com.example.textclassifier")); 59 } 60 61 @Test longSettings()62 public void longSettings() { 63 assertSettings( 64 TextClassificationConstants.SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND, 65 "1", 66 settings -> assertThat( 67 settings.getSystemTextClassifierApiTimeoutInSecond()) 68 .isEqualTo(1)); 69 } 70 assertSettings( String key, String value, Consumer<TextClassificationConstants> settingsConsumer)71 private static void assertSettings( 72 String key, String value, Consumer<TextClassificationConstants> settingsConsumer) { 73 final String originalValue = 74 DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, key); 75 TextClassificationConstants settings = new TextClassificationConstants(); 76 try { 77 setDeviceConfig(key, value); 78 settingsConsumer.accept(settings); 79 } finally { 80 setDeviceConfig(key, originalValue); 81 } 82 } 83 setDeviceConfig(String key, String value)84 private static void setDeviceConfig(String key, String value) { 85 DeviceConfig.setProperty( 86 DeviceConfig.NAMESPACE_TEXTCLASSIFIER, key, value, /* makeDefault */ false); 87 } 88 } 89