Add support for setProperties in TestableDeviceConfig
Bug: 190024878 Test: atest FrameworksMockingServicesTests:TestableDeviceConfigTest Change-Id: I27c75b7d8b6972ba641679073d21494c7e48af3d
This commit is contained in:
@@ -95,18 +95,25 @@ public final class TestableDeviceConfig implements StaticMockFixture {
|
||||
String name = invocationOnMock.getArgument(1);
|
||||
String value = invocationOnMock.getArgument(2);
|
||||
mKeyValueMap.put(getKey(namespace, name), value);
|
||||
for (DeviceConfig.OnPropertiesChangedListener listener :
|
||||
mOnPropertiesChangedListenerMap.keySet()) {
|
||||
if (namespace.equals(mOnPropertiesChangedListenerMap.get(listener).first)) {
|
||||
mOnPropertiesChangedListenerMap.get(listener).second.execute(
|
||||
() -> listener.onPropertiesChanged(
|
||||
getProperties(namespace, name, value)));
|
||||
}
|
||||
}
|
||||
invokeListeners(namespace, getProperties(namespace, name, value));
|
||||
return true;
|
||||
}
|
||||
).when(() -> DeviceConfig.setProperty(anyString(), anyString(), anyString(), anyBoolean()));
|
||||
|
||||
doAnswer((Answer<Boolean>) invocationOnMock -> {
|
||||
Properties properties = invocationOnMock.getArgument(0);
|
||||
String namespace = properties.getNamespace();
|
||||
Map<String, String> keyValues = new ArrayMap<>();
|
||||
for (String name : properties.getKeyset()) {
|
||||
String value = properties.getString(name, /* defaultValue= */ "");
|
||||
mKeyValueMap.put(getKey(namespace, name), value);
|
||||
keyValues.put(name.toLowerCase(), value);
|
||||
}
|
||||
invokeListeners(namespace, getProperties(namespace, keyValues));
|
||||
return true;
|
||||
}
|
||||
).when(() -> DeviceConfig.setProperties(any(Properties.class)));
|
||||
|
||||
doAnswer((Answer<String>) invocationOnMock -> {
|
||||
String namespace = invocationOnMock.getArgument(0);
|
||||
String name = invocationOnMock.getArgument(1);
|
||||
@@ -153,6 +160,16 @@ public final class TestableDeviceConfig implements StaticMockFixture {
|
||||
return Pair.create(values[0], values[1]);
|
||||
}
|
||||
|
||||
private void invokeListeners(String namespace, Properties properties) {
|
||||
for (DeviceConfig.OnPropertiesChangedListener listener :
|
||||
mOnPropertiesChangedListenerMap.keySet()) {
|
||||
if (namespace.equals(mOnPropertiesChangedListenerMap.get(listener).first)) {
|
||||
mOnPropertiesChangedListenerMap.get(listener).second.execute(
|
||||
() -> listener.onPropertiesChanged(properties));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Properties getProperties(String namespace, String name, String value) {
|
||||
return getProperties(namespace, Collections.singletonMap(name.toLowerCase(), value));
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
import android.app.ActivityThread;
|
||||
import android.platform.test.annotations.Presubmit;
|
||||
import android.provider.DeviceConfig;
|
||||
import android.provider.DeviceConfig.BadConfigException;
|
||||
import android.provider.DeviceConfig.Properties;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
@@ -91,6 +92,16 @@ public class TestableDeviceConfigTest {
|
||||
assertThat(result).isEqualTo(newValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setProperties() throws BadConfigException {
|
||||
String newKey = "key2";
|
||||
String newValue = "value2";
|
||||
DeviceConfig.setProperties(new Properties.Builder(sNamespace).setString(sKey,
|
||||
sValue).setString(newKey, newValue).build());
|
||||
assertThat(DeviceConfig.getProperty(sNamespace, sKey)).isEqualTo(sValue);
|
||||
assertThat(DeviceConfig.getProperty(sNamespace, newKey)).isEqualTo(newValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProperties_empty() {
|
||||
String newKey = "key2";
|
||||
@@ -131,13 +142,12 @@ public class TestableDeviceConfigTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListener() throws InterruptedException {
|
||||
public void testListener_setProperty() throws InterruptedException {
|
||||
CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||
|
||||
OnPropertiesChangedListener changeListener = (properties) -> {
|
||||
assertThat(properties.getNamespace()).isEqualTo(sNamespace);
|
||||
assertThat(properties.getKeyset().size()).isEqualTo(1);
|
||||
assertThat(properties.getKeyset()).contains(sKey);
|
||||
assertThat(properties.getKeyset()).containsExactly(sKey);
|
||||
assertThat(properties.getString(sKey, "bogus_value")).isEqualTo(sValue);
|
||||
assertThat(properties.getString("bogus_key", "bogus_value")).isEqualTo("bogus_value");
|
||||
countDownLatch.countDown();
|
||||
@@ -153,6 +163,32 @@ public class TestableDeviceConfigTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListener_setProperties() throws BadConfigException, InterruptedException {
|
||||
CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||
String newKey = "key2";
|
||||
String newValue = "value2";
|
||||
|
||||
OnPropertiesChangedListener changeListener = (properties) -> {
|
||||
assertThat(properties.getNamespace()).isEqualTo(sNamespace);
|
||||
assertThat(properties.getKeyset()).containsExactly(sKey, newKey);
|
||||
assertThat(properties.getString(sKey, "bogus_value")).isEqualTo(sValue);
|
||||
assertThat(properties.getString(newKey, "bogus_value")).isEqualTo(newValue);
|
||||
assertThat(properties.getString("bogus_key", "bogus_value")).isEqualTo("bogus_value");
|
||||
countDownLatch.countDown();
|
||||
};
|
||||
try {
|
||||
DeviceConfig.addOnPropertiesChangedListener(sNamespace,
|
||||
ActivityThread.currentApplication().getMainExecutor(), changeListener);
|
||||
DeviceConfig.setProperties(new Properties.Builder(sNamespace).setString(sKey,
|
||||
sValue).setString(newKey, newValue).build());
|
||||
assertThat(countDownLatch.await(
|
||||
WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue();
|
||||
} finally {
|
||||
DeviceConfig.removeOnPropertiesChangedListener(changeListener);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user