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 | |
| 19 | import static android.provider.DeviceConfig.OnPropertyChangedListener; |
| 20 | |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 21 | import static com.google.common.truth.Truth.assertThat; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 22 | |
| 23 | import android.app.ActivityThread; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.os.Bundle; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 26 | import android.platform.test.annotations.Presubmit; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 27 | |
| 28 | import androidx.test.InstrumentationRegistry; |
| 29 | import androidx.test.filters.SmallTest; |
| 30 | import androidx.test.runner.AndroidJUnit4; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 31 | |
| 32 | import org.junit.After; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 36 | import java.util.concurrent.CountDownLatch; |
| 37 | import java.util.concurrent.TimeUnit; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 38 | |
| 39 | /** Tests that ensure appropriate settings are backed up. */ |
| 40 | @Presubmit |
| 41 | @RunWith(AndroidJUnit4.class) |
| 42 | @SmallTest |
| 43 | public class DeviceConfigTest { |
| 44 | // TODO(b/109919982): Migrate tests to CTS |
| 45 | private static final String sNamespace = "namespace1"; |
| 46 | private static final String sKey = "key1"; |
| 47 | private static final String sValue = "value1"; |
| 48 | private static final long WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS = 2000; // 2 sec |
| 49 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 50 | @After |
| 51 | public void cleanUp() { |
| 52 | deleteViaContentProvider(sNamespace, sKey); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void getProperty_empty() { |
| 57 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 58 | assertThat(result).isNull(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void setAndGetProperty_sameNamespace() { |
| 63 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 64 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 65 | assertThat(result).isEqualTo(sValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void setAndGetProperty_differentNamespace() { |
| 70 | String newNamespace = "namespace2"; |
| 71 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 72 | String result = DeviceConfig.getProperty(newNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 73 | assertThat(result).isNull(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void setAndGetProperty_multipleNamespaces() { |
| 78 | String newNamespace = "namespace2"; |
| 79 | String newValue = "value2"; |
| 80 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 81 | DeviceConfig.setProperty(newNamespace, sKey, newValue, false); |
| 82 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 83 | assertThat(result).isEqualTo(sValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 84 | result = DeviceConfig.getProperty(newNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 85 | assertThat(result).isEqualTo(newValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 86 | |
| 87 | // clean up |
| 88 | deleteViaContentProvider(newNamespace, sKey); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void setAndGetProperty_overrideValue() { |
| 93 | String newValue = "value2"; |
| 94 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 95 | DeviceConfig.setProperty(sNamespace, sKey, newValue, false); |
| 96 | String result = DeviceConfig.getProperty(sNamespace, sKey); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 97 | assertThat(result).isEqualTo(newValue); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 101 | public void testListener() throws InterruptedException { |
| 102 | CountDownLatch countDownLatch = new CountDownLatch(1); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 103 | |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 104 | OnPropertyChangedListener changeListener = (namespace, name, value) -> { |
| 105 | assertThat(namespace).isEqualTo(sNamespace); |
| 106 | assertThat(name).isEqualTo(sKey); |
| 107 | assertThat(value).isEqualTo(sValue); |
| 108 | countDownLatch.countDown(); |
| 109 | }; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 110 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 111 | try { |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 112 | DeviceConfig.addOnPropertyChangedListener(sNamespace, |
| 113 | ActivityThread.currentApplication().getMainExecutor(), changeListener); |
| 114 | DeviceConfig.setProperty(sNamespace, sKey, sValue, false); |
| 115 | assertThat(countDownLatch.await( |
| 116 | WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 117 | } finally { |
| 118 | DeviceConfig.removeOnPropertyChangedListener(changeListener); |
| 119 | } |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 120 | |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | private static boolean deleteViaContentProvider(String namespace, String key) { |
| 124 | ContentResolver resolver = InstrumentationRegistry.getContext().getContentResolver(); |
| 125 | String compositeName = namespace + "/" + key; |
| 126 | Bundle result = resolver.call( |
| 127 | DeviceConfig.CONTENT_URI, Settings.CALL_METHOD_DELETE_CONFIG, compositeName, null); |
Linus Tufvesson | 9d50175 | 2019-02-12 11:33:22 +0000 | [diff] [blame] | 128 | assertThat(result).isNotNull(); |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 129 | return compositeName.equals(result.getString(Settings.NameValueTable.VALUE)); |
| 130 | } |
| 131 | |
| 132 | } |