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.testing; 18 19 import android.provider.DeviceConfig.Properties; 20 import androidx.annotation.NonNull; 21 import com.android.textclassifier.common.TextClassifierSettings; 22 import java.util.HashMap; 23 import javax.annotation.Nullable; 24 25 /** A fake DeviceConfig implementation for testing purpose. */ 26 public final class TestingDeviceConfig implements TextClassifierSettings.IDeviceConfig { 27 28 private final HashMap<String, String> configs; 29 TestingDeviceConfig()30 public TestingDeviceConfig() { 31 this.configs = new HashMap<>(); 32 } 33 setConfig(String key, String value)34 public void setConfig(String key, String value) { 35 configs.put(key, value); 36 } 37 setConfig(String key, boolean value)38 public void setConfig(String key, boolean value) { 39 configs.put(key, Boolean.toString(value)); 40 } 41 setConfig(String key, int value)42 public void setConfig(String key, int value) { 43 configs.put(key, Integer.toString(value)); 44 } 45 setConfig(String key, float value)46 public void setConfig(String key, float value) { 47 configs.put(key, Float.toString(value)); 48 } 49 setConfig(String key, long value)50 public void setConfig(String key, long value) { 51 configs.put(key, Long.toString(value)); 52 } 53 54 @Override getProperties(@onNull String namespace, @NonNull String... names)55 public Properties getProperties(@NonNull String namespace, @NonNull String... names) { 56 Properties.Builder builder = new Properties.Builder(namespace); 57 for (String key : configs.keySet()) { 58 builder.setString(key, configs.get(key)); 59 } 60 return builder.build(); 61 } 62 63 @Override getBoolean(@onNull String namespace, @NonNull String name, boolean defaultValue)64 public boolean getBoolean(@NonNull String namespace, @NonNull String name, boolean defaultValue) { 65 return configs.containsKey(name) ? Boolean.parseBoolean(configs.get(name)) : defaultValue; 66 } 67 68 @Override getString( @onNull String namespace, @NonNull String name, @Nullable String defaultValue)69 public String getString( 70 @NonNull String namespace, @NonNull String name, @Nullable String defaultValue) { 71 return configs.containsKey(name) ? configs.get(name) : defaultValue; 72 } 73 74 @Override getInt(@onNull String namespace, @NonNull String name, int defaultValue)75 public int getInt(@NonNull String namespace, @NonNull String name, int defaultValue) { 76 return configs.containsKey(name) ? Integer.parseInt(configs.get(name)) : defaultValue; 77 } 78 79 @Override getFloat(@onNull String namespace, @NonNull String name, float defaultValue)80 public float getFloat(@NonNull String namespace, @NonNull String name, float defaultValue) { 81 return configs.containsKey(name) ? Float.parseFloat(configs.get(name)) : defaultValue; 82 } 83 84 @Override getLong(@onNull String namespace, @NonNull String name, long defaultValue)85 public long getLong(@NonNull String namespace, @NonNull String name, long defaultValue) { 86 return configs.containsKey(name) ? Long.parseLong(configs.get(name)) : defaultValue; 87 } 88 } 89