summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUjjwal Kumar <ujjwal.kumar@ittiam.com>2023-06-14 16:46:40 +0000
committerNisha CT <nishact@google.com>2023-06-14 16:49:40 +0000
commit1c0fc4f662d7dbcb545c38a10e724643a54faf5d (patch)
treee44ef5342333e472bc88a0a246cfdea0f4f38bce
parent87e81ef98c6a01e14b8f7a1f142af8ffbc51be34 (diff)
downloadplatform_testing-1c0fc4f662d7dbcb545c38a10e724643a54faf5d.tar.gz
Added a common polling method for device-side tests
Bug: 268193777 Bug: 276748580 Test: Ran the new testcase on android-11.0.0_r46 with/without patch Change-Id: I6a6c0bbc22d0147d0dd6b472ecdc3db5f91c30b4
-rw-r--r--libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java81
1 files changed, 76 insertions, 5 deletions
diff --git a/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java b/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java
index c6504e067..a218f5890 100644
--- a/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java
+++ b/libraries/sts-common-util/device-side/src/com/android/sts/common/SystemUtil.java
@@ -18,27 +18,39 @@ package com.android.sts.common;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assume.assumeThat;
+import static org.junit.Assume.assumeTrue;
import android.app.Instrumentation;
+import android.os.Handler;
+import android.os.HandlerThread;
import com.android.compatibility.common.util.UserSettings;
import java.util.Optional;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.function.BooleanSupplier;
public class SystemUtil {
+ private static final String TAG = "SystemUtil";
+ public static final long DEFAULT_MAX_POLL_TIME_MS = 30_000L;
+ public static final long DEFAULT_POLL_TIME_MS = 100L;
/**
* Set the value of a device setting and set it back to old value upon closing.
*
* @param instrumentation {@link Instrumentation} instance, obtained from a test running in
- * instrumentation framework
+ * instrumentation framework
* @param namespace "system", "secure", or "global"
* @param key setting key to set
* @param value setting value to set to
* @return AutoCloseable that resets the setting back to existing value upon closing.
*/
- public static AutoCloseable withSetting(Instrumentation instrumentation, final String namespace,
- final String key, String value) {
+ public static AutoCloseable withSetting(
+ Instrumentation instrumentation,
+ final String namespace,
+ final String key,
+ String value) {
UserSettings userSettings = new UserSettings(UserSettings.Namespace.of(namespace));
String getSettingRes = userSettings.get(key);
final Optional<String> oldSetting = Optional.ofNullable(getSettingRes);
@@ -46,8 +58,10 @@ public class SystemUtil {
String getSettingCurrent = userSettings.get(key);
Optional<String> currSetting = Optional.ofNullable(getSettingCurrent);
- assumeThat(String.format("Could not set %s:%s to %s", namespace, key, value),
- currSetting.isPresent() ? currSetting.get().trim() : null, equalTo(value));
+ assumeThat(
+ String.format("Could not set %s:%s to %s", namespace, key, value),
+ currSetting.isPresent() ? currSetting.get().trim() : null,
+ equalTo(value));
return new AutoCloseable() {
@Override
@@ -69,4 +83,61 @@ public class SystemUtil {
}
};
}
+
+ /**
+ * Poll on a condition supplied by the user.
+ *
+ * @param waitCondition returns true when the polling condition is met, false otherwise.
+ * @return boolean value of {@code waitCondition}.
+ * @throws IllegalArgumentException when {@code pollingTime} is not a positive ineteger and is
+ * not less than {@code maxPollingTime}.
+ * @throws InterruptedException if the current thread is interrupted.
+ */
+ public static boolean poll(BooleanSupplier waitCondition)
+ throws IllegalArgumentException, InterruptedException {
+ return poll(waitCondition, DEFAULT_POLL_TIME_MS, DEFAULT_MAX_POLL_TIME_MS);
+ }
+
+ /**
+ * Poll on a condition supplied by the user.
+ *
+ * @param waitCondition returns true when the polling condition is met, false otherwise.
+ * @param pollingTime wait between successive calls to fetch value of {@code waitCondition} in
+ * milliseconds
+ * @param maxPollingTime maximum waiting time before return.
+ * @return boolean value of {@code waitCondition}.
+ * @throws IllegalArgumentException when {@code pollingTime} is not a positive ineteger and is
+ * not less than {@code maxPollingTime}.
+ * @throws InterruptedException if the current thread is interrupted.
+ */
+ public static boolean poll(BooleanSupplier waitCondition, long pollingTime, long maxPollingTime)
+ throws IllegalArgumentException, InterruptedException {
+ // The value of pollingTime should be a positive integer
+ if (pollingTime <= 0) {
+ throw new IllegalArgumentException("pollingTime should be a positive integer");
+ }
+
+ // The value of pollingTime should be less than maxPollingTime
+ if (pollingTime >= maxPollingTime) {
+ throw new IllegalArgumentException("pollingTime should be less than maxPollingTime");
+ }
+
+ // Use handlerThread to run task in a separate thread.
+ final HandlerThread handlerThread = new HandlerThread(TAG);
+ handlerThread.start();
+ Handler handler = new Handler(handlerThread.getLooper());
+ final Semaphore semaphore = new Semaphore(0);
+ final long startTime = System.currentTimeMillis();
+ do {
+ // Check for the status.
+ if (waitCondition.getAsBoolean()) {
+ return true;
+ }
+
+ // Wait before checking status again.
+ handler.postDelayed(() -> semaphore.release(), pollingTime);
+ assumeTrue(semaphore.tryAcquire(maxPollingTime, TimeUnit.MILLISECONDS));
+ } while (System.currentTimeMillis() - startTime <= maxPollingTime);
+ return waitCondition.getAsBoolean();
+ }
}