Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 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 android.provider; |
| 18 | |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 19 | import static android.provider.DeviceConfig.OnPropertiesChangedListener; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 20 | import static android.provider.DeviceConfig.OnPropertyChangedListener; |
| 21 | |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 22 | import static com.google.common.truth.Truth.assertThat; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 23 | |
| 24 | import android.app.ActivityThread; |
| 25 | import android.content.ContentResolver; |
| 26 | import android.os.Bundle; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 27 | import android.platform.test.annotations.Presubmit; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 28 | |
| 29 | import androidx.test.InstrumentationRegistry; |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | import androidx.test.runner.AndroidJUnit4; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 32 | |
| 33 | import org.junit.After; |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 34 | import org.junit.Assert; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 38 | import java.util.concurrent.CountDownLatch; |
| 39 | import java.util.concurrent.TimeUnit; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 40 | |
| 41 | /** Tests that ensure appropriate settings are backed up. */ |
| 42 | @Presubmit |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | @SmallTest |
| 45 | public class DeviceConfigTest { |
| 46 | // TODO(b/109919982): Migrate tests to CTS |
| 47 | private static final String sNamespace = "namespace1"; |
| 48 | private static final String sKey = "key1"; |
| 49 | private static final String sValue = "value1"; |
| 50 | private static final long WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS = 2000; // 2 sec |
| 51 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 52 | @After |
| 53 | public void cleanUp() { |
| 54 | deleteViaContentProvider(sNamespace, sKey); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void getProperty_empty() { |
| 59 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 60 | assertThat(result).isNull(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
Matt Pape | 043437c | 2019-02-20 10:30:47 -0800 | [diff] [blame] | 64 | public void getString_empty() { |
| 65 | final String default_value = "default_value"; |
| 66 | final String result = DeviceConfig.getString(sNamespace, sKey, default_value); |
| 67 | assertThat(result).isEqualTo(default_value); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void getString_nonEmpty() { |
| 72 | final String value = "new_value"; |
| 73 | final String default_value = "default"; |
| 74 | DeviceConfig.setProperty(sNamespace, sKey, value, false); |
| 75 | |
| 76 | final String result = DeviceConfig.getString(sNamespace, sKey, default_value); |
| 77 | assertThat(result).isEqualTo(value); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void getBoolean_empty() { |
| 82 | final boolean default_value = true; |
| 83 | final boolean result = DeviceConfig.getBoolean(sNamespace, sKey, default_value); |
| 84 | assertThat(result).isEqualTo(default_value); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void getBoolean_valid() { |
| 89 | final boolean value = true; |
| 90 | final boolean default_value = false; |
| 91 | DeviceConfig.setProperty(sNamespace, sKey, String.valueOf(value), false); |
| 92 | |
| 93 | final boolean result = DeviceConfig.getBoolean(sNamespace, sKey, default_value); |
| 94 | assertThat(result).isEqualTo(value); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void getBoolean_invalid() { |
| 99 | final boolean default_value = true; |
| 100 | DeviceConfig.setProperty(sNamespace, sKey, "not_a_boolean", false); |
| 101 | |
| 102 | final boolean result = DeviceConfig.getBoolean(sNamespace, sKey, default_value); |
| 103 | // Anything non-null other than case insensitive "true" parses to false. |
| 104 | assertThat(result).isFalse(); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void getInt_empty() { |
| 109 | final int default_value = 999; |
| 110 | final int result = DeviceConfig.getInt(sNamespace, sKey, default_value); |
| 111 | assertThat(result).isEqualTo(default_value); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void getInt_valid() { |
| 116 | final int value = 123; |
| 117 | final int default_value = 999; |
| 118 | DeviceConfig.setProperty(sNamespace, sKey, String.valueOf(value), false); |
| 119 | |
| 120 | final int result = DeviceConfig.getInt(sNamespace, sKey, default_value); |
| 121 | assertThat(result).isEqualTo(value); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void getInt_invalid() { |
| 126 | final int default_value = 999; |
| 127 | DeviceConfig.setProperty(sNamespace, sKey, "not_an_int", false); |
| 128 | |
| 129 | final int result = DeviceConfig.getInt(sNamespace, sKey, default_value); |
| 130 | // Failure to parse results in using the default value |
| 131 | assertThat(result).isEqualTo(default_value); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void getLong_empty() { |
| 136 | final long default_value = 123456; |
| 137 | final long result = DeviceConfig.getLong(sNamespace, sKey, default_value); |
| 138 | assertThat(result).isEqualTo(default_value); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void getLong_valid() { |
| 143 | final long value = 456789; |
| 144 | final long default_value = 123456; |
| 145 | DeviceConfig.setProperty(sNamespace, sKey, String.valueOf(value), false); |
| 146 | |
| 147 | final long result = DeviceConfig.getLong(sNamespace, sKey, default_value); |
| 148 | assertThat(result).isEqualTo(value); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void getLong_invalid() { |
| 153 | final long default_value = 123456; |
| 154 | DeviceConfig.setProperty(sNamespace, sKey, "not_a_long", false); |
| 155 | |
| 156 | final long result = DeviceConfig.getLong(sNamespace, sKey, default_value); |
| 157 | // Failure to parse results in using the default value |
| 158 | assertThat(result).isEqualTo(default_value); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void getFloat_empty() { |
| 163 | final float default_value = 123.456f; |
| 164 | final float result = DeviceConfig.getFloat(sNamespace, sKey, default_value); |
| 165 | assertThat(result).isEqualTo(default_value); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void getFloat_valid() { |
| 170 | final float value = 456.789f; |
| 171 | final float default_value = 123.456f; |
| 172 | DeviceConfig.setProperty(sNamespace, sKey, String.valueOf(value), false); |
| 173 | |
| 174 | final float result = DeviceConfig.getFloat(sNamespace, sKey, default_value); |
| 175 | assertThat(result).isEqualTo(value); |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void getFloat_invalid() { |
| 180 | final float default_value = 123.456f; |
| 181 | DeviceConfig.setProperty(sNamespace, sKey, "not_a_float", false); |
| 182 | |
| 183 | final float result = DeviceConfig.getFloat(sNamespace, sKey, default_value); |
| 184 | // Failure to parse results in using the default value |
| 185 | assertThat(result).isEqualTo(default_value); |
| 186 | } |
| 187 | |
| 188 | @Test |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 189 | public void setAndGetProperty_sameNamespace() { |
| 190 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 191 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 192 | assertThat(result).isEqualTo(sValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void setAndGetProperty_differentNamespace() { |
| 197 | String newNamespace = "namespace2"; |
| 198 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 199 | String result = DeviceConfig.getProperty(newNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 200 | assertThat(result).isNull(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void setAndGetProperty_multipleNamespaces() { |
| 205 | String newNamespace = "namespace2"; |
| 206 | String newValue = "value2"; |
| 207 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 208 | DeviceConfig.setProperty(newNamespace, sKey, newValue, false); |
| 209 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 210 | assertThat(result).isEqualTo(sValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 211 | result = DeviceConfig.getProperty(newNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 212 | assertThat(result).isEqualTo(newValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 213 | |
| 214 | // clean up |
| 215 | deleteViaContentProvider(newNamespace, sKey); |
| 216 | } |
| 217 | |
| 218 | @Test |
| 219 | public void setAndGetProperty_overrideValue() { |
| 220 | String newValue = "value2"; |
| 221 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 222 | DeviceConfig.setProperty(sNamespace, sKey, newValue, false); |
| 223 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 224 | assertThat(result).isEqualTo(newValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Test |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 228 | public void testOnPropertiesChangedListener() throws InterruptedException { |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 229 | final CountDownLatch countDownLatch = new CountDownLatch(1); |
| 230 | |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 231 | OnPropertiesChangedListener changeListener = (properties) -> { |
| 232 | assertThat(properties.getNamespace()).isEqualTo(sNamespace); |
| 233 | assertThat(properties.getKeyset()).contains(sKey); |
| 234 | assertThat(properties.getString(sKey, "default_value")).isEqualTo(sValue); |
| 235 | countDownLatch.countDown(); |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 236 | }; |
| 237 | |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 238 | try { |
| 239 | DeviceConfig.addOnPropertiesChangedListener(sNamespace, |
| 240 | ActivityThread.currentApplication().getMainExecutor(), changeListener); |
| 241 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 242 | assertThat(countDownLatch.await( |
| 243 | WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); |
| 244 | } catch (InterruptedException e) { |
| 245 | Assert.fail(e.getMessage()); |
| 246 | } finally { |
| 247 | DeviceConfig.removeOnPropertiesChangedListener(changeListener); |
| 248 | } |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Test |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 252 | public void testOnPropertyChangedListener() throws InterruptedException { |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 253 | CountDownLatch countDownLatch = new CountDownLatch(1); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 254 | |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 255 | OnPropertyChangedListener changeListener = (namespace, name, value) -> { |
| 256 | assertThat(namespace).isEqualTo(sNamespace); |
| 257 | assertThat(name).isEqualTo(sKey); |
| 258 | assertThat(value).isEqualTo(sValue); |
| 259 | countDownLatch.countDown(); |
| 260 | }; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 261 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 262 | try { |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 263 | DeviceConfig.addOnPropertyChangedListener(sNamespace, |
| 264 | ActivityThread.currentApplication().getMainExecutor(), changeListener); |
| 265 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 266 | assertThat(countDownLatch.await( |
| 267 | WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); |
Matt Pape | 1013d29 | 2019-02-27 15:20:50 -0800 | [diff] [blame] | 268 | } catch (InterruptedException e) { |
| 269 | Assert.fail(e.getMessage()); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 270 | } finally { |
| 271 | DeviceConfig.removeOnPropertyChangedListener(changeListener); |
| 272 | } |
Matt Pape | ea9881d | 2019-03-07 15:59:47 -0800 | [diff] [blame] | 273 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | private static boolean deleteViaContentProvider(String namespace, String key) { |
| 277 | ContentResolver resolver = InstrumentationRegistry.getContext().getContentResolver(); |
| 278 | String compositeName = namespace + "/" + key; |
| 279 | Bundle result = resolver.call( |
| 280 | DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, compositeName, null); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 281 | assertThat(result).isNotNull(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 282 | return compositeName.equals(result.getString(Settings.NameValueTable.VALUE)); |
| 283 | } |
| 284 | |
| 285 | } |