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 17package com.android.settings.testutils.shadow; 18 19import android.os.SystemProperties; 20 21import org.robolectric.annotation.Implementation; 22import org.robolectric.annotation.Implements; 23import org.robolectric.shadows.ShadowSystemProperties; 24 25import java.util.HashMap; 26import java.util.Map; 27 28/** 29 * This class provides write capability to ShadowSystemProperties. 30 */ 31@Implements(SystemProperties.class) 32public class SettingsShadowSystemProperties extends ShadowSystemProperties { 33 34 private static final Map<String, String> sValues = new HashMap<>(); 35 36 @Implementation 37 public static synchronized boolean getBoolean(String key, boolean def) { 38 if (sValues.containsKey(key)) { 39 String val = sValues.get(key); 40 return "y".equals(val) || "yes".equals(val) || "1".equals(val) || "true".equals(val) 41 || "on".equals(val); 42 } 43 return ShadowSystemProperties.getBoolean(key, def); 44 } 45 46 @Implementation 47 public static synchronized String get(String key) { 48 if (sValues.containsKey(key)) { 49 return sValues.get(key); 50 } 51 return ShadowSystemProperties.get(key); 52 } 53 54 public static synchronized void set(String key, String val) { 55 sValues.put(key, val); 56 } 57 58 @Implementation 59 public static String get(String key, String def) { 60 String value = sValues.get(key); 61 return value == null ? def : value; 62 } 63 64 public static synchronized void clear() { 65 sValues.clear(); 66 } 67 68} 69