Merge "Adding programmatic provision of WRITE_SECURE_SETTINGS permission to Launcher3Tests." into main

This commit is contained in:
Archisha Baranwal
2024-09-27 08:35:45 +00:00
committed by Android (Google) Code Review
4 changed files with 22 additions and 0 deletions
@@ -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;
@@ -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 */
@@ -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
@@ -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,