diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2020-01-09 20:27:11 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2020-01-09 20:27:11 +0000 |
commit | 561816be24be412e0611fe1641c6b7deb9b94526 (patch) | |
tree | 3dc56ab5e6fd1099da8f34aed88eed4e33961b3f | |
parent | b9dd758acf48240c37f0f0bc1599769b64347087 (diff) | |
parent | acb30a0fac549108e64c4c14b40be90bad3069ab (diff) | |
download | cts-561816be24be412e0611fe1641c6b7deb9b94526.tar.gz |
Merge "Fix a race condition in ZenModeTest" into pie-cts-dev
3 files changed, 43 insertions, 2 deletions
diff --git a/common/device-side/util-axt/src/com/android/compatibility/common/util/BroadcastTestBase.java b/common/device-side/util-axt/src/com/android/compatibility/common/util/BroadcastTestBase.java index bf5dc391fa0..2dd54e6babd 100644 --- a/common/device-side/util-axt/src/com/android/compatibility/common/util/BroadcastTestBase.java +++ b/common/device-side/util-axt/src/com/android/compatibility/common/util/BroadcastTestBase.java @@ -18,11 +18,15 @@ package com.android.compatibility.common.util; import android.content.BroadcastReceiver; import android.content.ComponentName; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; +import android.database.ContentObserver; +import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.test.ActivityInstrumentationTestCase2; import android.util.Log; @@ -120,4 +124,36 @@ public class BroadcastTestBase extends ActivityInstrumentationTestCase2< } } } + + protected CountDownLatch registerForChanges(Uri uri) throws Exception { + final CountDownLatch latch = new CountDownLatch(1); + final ContentResolver resolver = mActivity.getContentResolver(); + mActivity.runOnUiThread(() -> { + resolver.registerContentObserver(uri, true, + new ContentObserver(new Handler()) { + @Override + public void onChange(boolean selfChange) { + latch.countDown(); + resolver.unregisterContentObserver(this); + } + }); + }); + return latch; + } + + protected boolean startTestAndWaitForChange(BroadcastUtils.TestcaseType testCaseType, Uri uri, + String pkg, String cls) + throws Exception { + Log.i(TAG, "Begin Testing: " + testCaseType); + registerBroadcastReceiver(testCaseType); + CountDownLatch latch = registerForChanges(uri); + mActivity.startTest(testCaseType.toString(), pkg, cls); + if (!mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS) + || !latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) { + fail("Failed to change in " + TIMEOUT_MS + "msec"); + return false; + } + return true; + } + } diff --git a/tests/tests/voicesettings/src/android/voicesettings/cts/AirplaneModeTest.java b/tests/tests/voicesettings/src/android/voicesettings/cts/AirplaneModeTest.java index db6bbb90f44..3c508a9908a 100644 --- a/tests/tests/voicesettings/src/android/voicesettings/cts/AirplaneModeTest.java +++ b/tests/tests/voicesettings/src/android/voicesettings/cts/AirplaneModeTest.java @@ -87,7 +87,10 @@ public class AirplaneModeTest extends BroadcastTestBase { } private boolean runTest(BroadcastUtils.TestcaseType test, int expectedMode) throws Exception { - if (!startTestAndWaitForBroadcast(test, VOICE_SETTINGS_PACKAGE, VOICE_INTERACTION_CLASS)) { + if (!startTestAndWaitForChange(test, + Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), + VOICE_SETTINGS_PACKAGE, + VOICE_INTERACTION_CLASS)) { return false; } diff --git a/tests/tests/voicesettings/src/android/voicesettings/cts/ZenModeTest.java b/tests/tests/voicesettings/src/android/voicesettings/cts/ZenModeTest.java index e01f3b9a587..e157ad00d41 100644 --- a/tests/tests/voicesettings/src/android/voicesettings/cts/ZenModeTest.java +++ b/tests/tests/voicesettings/src/android/voicesettings/cts/ZenModeTest.java @@ -92,7 +92,9 @@ public class ZenModeTest extends BroadcastTestBase { } private boolean runTest(BroadcastUtils.TestcaseType test, int expectedMode) throws Exception { - if (!startTestAndWaitForBroadcast(test, VOICE_SETTINGS_PACKAGE, VOICE_INTERACTION_CLASS)) { + if (!startTestAndWaitForChange(test, + Settings.Global.getUriFor(ZEN_MODE), VOICE_SETTINGS_PACKAGE, + VOICE_INTERACTION_CLASS)) { return false; } |