From 0d11c4a7efc5430d4106283cca4899cb488fb9a2 Mon Sep 17 00:00:00 2001 From: Archisha Baranwal Date: Thu, 12 Sep 2024 15:09:07 +0000 Subject: [PATCH] Adding programmatic provision of WRITE_SECURE_SETTINGS permission to Launcher3Tests. Bug: 361222449 Test: atest Launcher3Tests Flag: EXEMPT test fixes Change-Id: I4226c4c50047d71a99944fc4e5d70080d134c3d3 --- .../android/launcher3/util/LauncherModelHelper.java | 3 +++ .../src/com/android/launcher3/util/TestUtil.java | 11 +++++++++++ .../src/com/android/launcher3/model/LoaderTaskTest.kt | 3 +++ .../util/rule/ExtendedLongPressTimeoutRule.java | 5 +++++ 4 files changed, 22 insertions(+) diff --git a/tests/multivalentTests/src/com/android/launcher3/util/LauncherModelHelper.java b/tests/multivalentTests/src/com/android/launcher3/util/LauncherModelHelper.java index 2d53e29bd6..8e4bf88b88 100644 --- a/tests/multivalentTests/src/com/android/launcher3/util/LauncherModelHelper.java +++ b/tests/multivalentTests/src/com/android/launcher3/util/LauncherModelHelper.java @@ -22,6 +22,7 @@ import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentat import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; import static com.android.launcher3.util.TestUtil.runOnExecutorSync; +import static com.android.launcher3.util.TestUtil.grantWriteSecurePermission; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; @@ -185,6 +186,8 @@ public class LauncherModelHelper { */ public LauncherModelHelper setupDefaultLayoutProvider(LauncherLayoutBuilder builder) throws Exception { + grantWriteSecurePermission(); + InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(sandboxContext); if (idp.numRows == 0 && idp.numColumns == 0) { idp.numRows = idp.numColumns = idp.numDatabaseHotseatIcons = DEFAULT_GRID_SIZE; diff --git a/tests/multivalentTests/src/com/android/launcher3/util/TestUtil.java b/tests/multivalentTests/src/com/android/launcher3/util/TestUtil.java index 3646f0c4a4..64035da5a5 100644 --- a/tests/multivalentTests/src/com/android/launcher3/util/TestUtil.java +++ b/tests/multivalentTests/src/com/android/launcher3/util/TestUtil.java @@ -26,6 +26,7 @@ import static com.android.launcher3.LauncherSettings.Settings.LAYOUT_DIGEST_TAG; import static org.junit.Assert.assertTrue; +import android.Manifest; import android.app.Instrumentation; import android.app.blob.BlobHandle; import android.app.blob.BlobStoreManager; @@ -169,6 +170,8 @@ public class TestUtil { } String key = Base64.encodeToString(digest, NO_WRAP | NO_PADDING); + + grantWriteSecurePermission(); Settings.Secure.putString(context.getContentResolver(), LAYOUT_DIGEST_KEY, key); wait.await(); return () -> @@ -224,6 +227,14 @@ public class TestUtil { assertTrue(message, failed); } + /** + * Grants [WRITE_SECURE_SETTINGS] permission in runtime. + */ + public static void grantWriteSecurePermission() { + getInstrumentation().getUiAutomation() + .adoptShellPermissionIdentity(Manifest.permission.WRITE_SECURE_SETTINGS); + } + /** Interface to indicate a runnable which can throw any exception. */ public interface UncheckedRunnable { /** Method to run the task */ diff --git a/tests/src/com/android/launcher3/model/LoaderTaskTest.kt b/tests/src/com/android/launcher3/model/LoaderTaskTest.kt index d16674c6c2..0dd13a9574 100644 --- a/tests/src/com/android/launcher3/model/LoaderTaskTest.kt +++ b/tests/src/com/android/launcher3/model/LoaderTaskTest.kt @@ -22,6 +22,7 @@ import com.android.launcher3.ui.TestViewHelpers import com.android.launcher3.util.Executors.MODEL_EXECUTOR import com.android.launcher3.util.LauncherModelHelper.SandboxModelContext import com.android.launcher3.util.LooperIdleLock +import com.android.launcher3.util.TestUtil import com.android.launcher3.util.UserIconInfo import com.google.common.truth.Truth import java.util.concurrent.CountDownLatch @@ -116,6 +117,8 @@ class LoaderTaskTest { `when`(idleLock.awaitLocked(1000)).thenReturn(false) `when`(iconCache.updateHandler).thenReturn(iconCacheUpdateHandler) context.putObject(UserCache.INSTANCE, userCache) + + TestUtil.grantWriteSecurePermission() } @After diff --git a/tests/src/com/android/launcher3/util/rule/ExtendedLongPressTimeoutRule.java b/tests/src/com/android/launcher3/util/rule/ExtendedLongPressTimeoutRule.java index 702988c05b..8a9ff3ed38 100644 --- a/tests/src/com/android/launcher3/util/rule/ExtendedLongPressTimeoutRule.java +++ b/tests/src/com/android/launcher3/util/rule/ExtendedLongPressTimeoutRule.java @@ -16,6 +16,9 @@ package com.android.launcher3.util.rule; +import static com.android.launcher3.util.TestUtil.grantWriteSecurePermission; + +import android.app.Instrumentation; import android.content.ContentResolver; import android.provider.Settings; import android.util.Log; @@ -51,6 +54,7 @@ public class ExtendedLongPressTimeoutRule implements TestRule { try { Log.d(TAG, "In try-block: Setting long press timeout from " + prevLongPressTimeout + "ms to " + newLongPressTimeout + "ms"); + grantWriteSecurePermission(); Settings.Secure.putInt( contentResolver, Settings.Secure.LONG_PRESS_TIMEOUT, @@ -63,6 +67,7 @@ public class ExtendedLongPressTimeoutRule implements TestRule { } finally { Log.d(TAG, "In finally-block: resetting long press timeout to " + prevLongPressTimeout + "ms"); + grantWriteSecurePermission(); Settings.Secure.putInt( contentResolver, Settings.Secure.LONG_PRESS_TIMEOUT,