diff --git a/core/tests/coretests/src/android/provider/DeviceConfigTest.java b/core/tests/coretests/src/android/provider/DeviceConfigTest.java index fd7753b66a466..5b3be58ae0de2 100644 --- a/core/tests/coretests/src/android/provider/DeviceConfigTest.java +++ b/core/tests/coretests/src/android/provider/DeviceConfigTest.java @@ -22,6 +22,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.testng.Assert.assertThrows; +import android.app.ActivityThread; import android.content.ContentResolver; import android.os.Bundle; import android.platform.test.annotations.Presubmit; @@ -35,6 +36,12 @@ import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + /** Tests that ensure appropriate settings are backed up. */ @Presubmit @RunWith(AndroidJUnit4.class) @@ -701,66 +708,67 @@ public class DeviceConfigTest { assertThat(result.getString(KEY4, DEFAULT_VALUE)).isEqualTo(DEFAULT_VALUE); } - // TODO(mpape): resolve b/142727848 and re-enable listener tests -// @Test -// public void onPropertiesChangedListener_setPropertyCallback() throws InterruptedException { -// final CountDownLatch countDownLatch = new CountDownLatch(1); -// -// DeviceConfig.OnPropertiesChangedListener changeListener = (properties) -> { -// assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); -// assertThat(properties.getKeyset()).contains(KEY); -// assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE); -// countDownLatch.countDown(); -// }; -// -// try { -// DeviceConfig.addOnPropertiesChangedListener(NAMESPACE, -// ActivityThread.currentApplication().getMainExecutor(), changeListener); -// DeviceConfig.setProperty(NAMESPACE, KEY, VALUE, false); -// assertThat(countDownLatch.await( -// WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); -// } catch (InterruptedException e) { -// Assert.fail(e.getMessage()); -// } finally { -// DeviceConfig.removeOnPropertiesChangedListener(changeListener); -// } -// } -// -// @Test -// public void onPropertiesChangedListener_setPropertiesCallback() throws InterruptedException { -// final CountDownLatch countDownLatch = new CountDownLatch(1); -// DeviceConfig.setProperty(NAMESPACE, KEY, VALUE, false); -// DeviceConfig.setProperty(NAMESPACE, KEY2, VALUE2, false); -// -// Map keyValues = new HashMap<>(2); -// keyValues.put(KEY, VALUE2); -// keyValues.put(KEY3, VALUE3); -// Properties setProperties = new Properties(NAMESPACE, keyValues); -// -// DeviceConfig.OnPropertiesChangedListener changeListener = (properties) -> { -// assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); -// assertThat(properties.getKeyset()).containsExactly(KEY, KEY2, KEY3); -// // KEY updated from VALUE to VALUE2 -// assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE2); -// // KEY2 deleted (returns default_value) -// assertThat(properties.getString(KEY2, "default_value")).isEqualTo("default_value"); -// //KEY3 added with VALUE3 -// assertThat(properties.getString(KEY3, "default_value")).isEqualTo(VALUE3); -// countDownLatch.countDown(); -// }; -// -// try { -// DeviceConfig.addOnPropertiesChangedListener(NAMESPACE, -// ActivityThread.currentApplication().getMainExecutor(), changeListener); -// DeviceConfig.setProperties(setProperties); -// assertThat(countDownLatch.await( -// WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); -// } catch (InterruptedException e) { -// Assert.fail(e.getMessage()); -// } finally { -// DeviceConfig.removeOnPropertiesChangedListener(changeListener); -// } -// } + @Test + public void onPropertiesChangedListener_setPropertyCallback() throws InterruptedException { + final CountDownLatch countDownLatch = new CountDownLatch(1); + + DeviceConfig.OnPropertiesChangedListener changeListener = (properties) -> { + assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); + assertThat(properties.getKeyset()).contains(KEY); + assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE); + countDownLatch.countDown(); + }; + + try { + DeviceConfig.addOnPropertiesChangedListener(NAMESPACE, + Objects.requireNonNull(ActivityThread.currentApplication()).getMainExecutor(), + changeListener); + DeviceConfig.setProperty(NAMESPACE, KEY, VALUE, false); + assertThat(countDownLatch.await( + WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); + } catch (InterruptedException e) { + Assert.fail(e.getMessage()); + } finally { + DeviceConfig.removeOnPropertiesChangedListener(changeListener); + } + } + + @Test + public void onPropertiesChangedListener_setPropertiesCallback() { + final CountDownLatch countDownLatch = new CountDownLatch(1); + DeviceConfig.setProperty(NAMESPACE, KEY, VALUE, false); + DeviceConfig.setProperty(NAMESPACE, KEY2, VALUE2, false); + + Map keyValues = new HashMap<>(2); + keyValues.put(KEY, VALUE2); + keyValues.put(KEY3, VALUE3); + Properties setProperties = new Properties(NAMESPACE, keyValues); + + DeviceConfig.OnPropertiesChangedListener changeListener = (properties) -> { + assertThat(properties.getNamespace()).isEqualTo(NAMESPACE); + assertThat(properties.getKeyset()).containsExactly(KEY, KEY2, KEY3); + // KEY updated from VALUE to VALUE2 + assertThat(properties.getString(KEY, "default_value")).isEqualTo(VALUE2); + // KEY2 deleted (returns default_value) + assertThat(properties.getString(KEY2, "default_value")).isEqualTo("default_value"); + //KEY3 added with VALUE3 + assertThat(properties.getString(KEY3, "default_value")).isEqualTo(VALUE3); + countDownLatch.countDown(); + }; + + try { + DeviceConfig.addOnPropertiesChangedListener(NAMESPACE, + Objects.requireNonNull(ActivityThread.currentApplication()).getMainExecutor(), + changeListener); + DeviceConfig.setProperties(setProperties); + assertThat(countDownLatch.await( + WAIT_FOR_PROPERTY_CHANGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue(); + } catch (InterruptedException | DeviceConfig.BadConfigException e) { + Assert.fail(e.getMessage()); + } finally { + DeviceConfig.removeOnPropertiesChangedListener(changeListener); + } + } @Test public void syncDisabling() throws Exception {