blob: 5d5754bc41beb809f47fd122b182c4c97a237ed9 [file] [log] [blame]
Matt Pape1278d1c2018-12-11 13:03:49 -08001/*
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
17package android.provider;
18
19import static android.provider.DeviceConfig.OnPropertyChangedListener;
20
Linus Tufvesson9d501752019-02-12 11:33:22 +000021import static com.google.common.truth.Truth.assertThat;
Matt Pape1278d1c2018-12-11 13:03:49 -080022
23import android.app.ActivityThread;
24import android.content.ContentResolver;
25import android.os.Bundle;
Matt Pape1278d1c2018-12-11 13:03:49 -080026import android.platform.test.annotations.Presubmit;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090027
28import androidx.test.InstrumentationRegistry;
29import androidx.test.filters.SmallTest;
30import androidx.test.runner.AndroidJUnit4;
Matt Pape1278d1c2018-12-11 13:03:49 -080031
32import org.junit.After;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
Linus Tufvesson9d501752019-02-12 11:33:22 +000036import java.util.concurrent.CountDownLatch;
37import java.util.concurrent.TimeUnit;
Matt Pape1278d1c2018-12-11 13:03:49 -080038
39/** Tests that ensure appropriate settings are backed up. */
40@Presubmit
41@RunWith(AndroidJUnit4.class)
42@SmallTest
43public 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 Pape1278d1c2018-12-11 13:03:49 -080050 @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 Tufvesson9d501752019-02-12 11:33:22 +000058 assertThat(result).isNull();
Matt Pape1278d1c2018-12-11 13:03:49 -080059 }
60
61 @Test
62 public void setAndGetProperty_sameNamespace() {
63 DeviceConfig.setProperty(sNamespace, sKey, sValue, false);
64 String result = DeviceConfig.getProperty(sNamespace, sKey);
Linus Tufvesson9d501752019-02-12 11:33:22 +000065 assertThat(result).isEqualTo(sValue);
Matt Pape1278d1c2018-12-11 13:03:49 -080066 }
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 Tufvesson9d501752019-02-12 11:33:22 +000073 assertThat(result).isNull();
Matt Pape1278d1c2018-12-11 13:03:49 -080074 }
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 Tufvesson9d501752019-02-12 11:33:22 +000083 assertThat(result).isEqualTo(sValue);
Matt Pape1278d1c2018-12-11 13:03:49 -080084 result = DeviceConfig.getProperty(newNamespace, sKey);
Linus Tufvesson9d501752019-02-12 11:33:22 +000085 assertThat(result).isEqualTo(newValue);
Matt Pape1278d1c2018-12-11 13:03:49 -080086
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 Tufvesson9d501752019-02-12 11:33:22 +000097 assertThat(result).isEqualTo(newValue);
Matt Pape1278d1c2018-12-11 13:03:49 -080098 }
99
100 @Test
Linus Tufvesson9d501752019-02-12 11:33:22 +0000101 public void testListener() throws InterruptedException {
102 CountDownLatch countDownLatch = new CountDownLatch(1);
Matt Pape1278d1c2018-12-11 13:03:49 -0800103
Linus Tufvesson9d501752019-02-12 11:33:22 +0000104 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 Pape1278d1c2018-12-11 13:03:49 -0800110
Matt Pape1278d1c2018-12-11 13:03:49 -0800111 try {
Linus Tufvesson9d501752019-02-12 11:33:22 +0000112 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 Pape1278d1c2018-12-11 13:03:49 -0800117 } finally {
118 DeviceConfig.removeOnPropertyChangedListener(changeListener);
119 }
Linus Tufvesson9d501752019-02-12 11:33:22 +0000120
Matt Pape1278d1c2018-12-11 13:03:49 -0800121 }
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 Tufvesson9d501752019-02-12 11:33:22 +0000128 assertThat(result).isNotNull();
Matt Pape1278d1c2018-12-11 13:03:49 -0800129 return compositeName.equals(result.getString(Settings.NameValueTable.VALUE));
130 }
131
132}