diff options
author | 2024-08-17 19:31:37 -0700 | |
---|---|---|
committer | 2024-08-17 19:31:37 -0700 | |
commit | 0bae36bf10e38af61e9c50438691eaed01e6d9ea (patch) | |
tree | b4c8a6aa2a3995731b5484c25f5f1656490aad2e /media/tests | |
parent | 2ff4120877f721a6d964bbf493c6e6f95257de65 (diff) |
audio: Add permission update barrier for tests
To avoid test flakiness, we need the equivalent of purgePermissionCache
for the new permission sync scheme: tests need to block until the
permission state change they trigger via shell perms is visible to the
audio system.
Migrate the permission update tasks to use an executor service, and keep
track of futures which are scheduled on it, allowing us to block until
these futures are complete. This also allows for true task conflation,
which enables dropping the delay duration lower. Move listener start to
later in the boot sequence for performance.
Test: perfetto for performance and ensuring dispatch
Test: atest CtsMediaAudioTestCases
Bug: 338089555
Flag: com.android.media.audio.audioserver_permissions
Change-Id: I56dadf11ad397896bd5f7757e6b12eb173d6e984
Diffstat (limited to 'media/tests')
-rw-r--r-- | media/tests/mediatestutils/Android.bp | 2 | ||||
-rw-r--r-- | media/tests/mediatestutils/java/com/android/media/mediatestutils/PermissionUpdateBarrierRule.java | 59 |
2 files changed, 61 insertions, 0 deletions
diff --git a/media/tests/mediatestutils/Android.bp b/media/tests/mediatestutils/Android.bp index 88938e2f71d0..8c68f210ce3d 100644 --- a/media/tests/mediatestutils/Android.bp +++ b/media/tests/mediatestutils/Android.bp @@ -27,9 +27,11 @@ java_library { name: "mediatestutils", srcs: [ "java/com/android/media/mediatestutils/TestUtils.java", + "java/com/android/media/mediatestutils/PermissionUpdateBarrierRule.java", ], static_libs: [ "androidx.concurrent_concurrent-futures", + "androidx.test.runner", "guava", "mediatestutils_host", ], diff --git a/media/tests/mediatestutils/java/com/android/media/mediatestutils/PermissionUpdateBarrierRule.java b/media/tests/mediatestutils/java/com/android/media/mediatestutils/PermissionUpdateBarrierRule.java new file mode 100644 index 000000000000..c51b5dea9546 --- /dev/null +++ b/media/tests/mediatestutils/java/com/android/media/mediatestutils/PermissionUpdateBarrierRule.java @@ -0,0 +1,59 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.media.mediatestutils; + +import android.content.Context; +import android.media.AudioManager; + +import androidx.test.platform.app.InstrumentationRegistry; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Barrier to wait for permission updates to propagate to audioserver, to avoid flakiness when using + * {@code com.android.compatability.common.util.AdoptShellPermissionsRule}. Note, this rule should + * <b> always </b> be placed after the adopt permission rule. Don't use rule when changing + * permission state in {@code @Before}, since that executes after all rules. + */ +public class PermissionUpdateBarrierRule implements TestRule { + + private final Context mContext; + + /** + * @param context the context to use + */ + public PermissionUpdateBarrierRule(Context context) { + mContext = context; + } + + public PermissionUpdateBarrierRule() { + this(InstrumentationRegistry.getInstrumentation().getContext()); + } + + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + mContext.getSystemService(AudioManager.class).permissionUpdateBarrier(); + base.evaluate(); + } + }; + } +} |