2016-05-19 12:15:39 -07:00
|
|
|
package com.android.launcher3.util;
|
|
|
|
|
|
|
|
|
|
import android.os.SystemClock;
|
2019-07-31 16:41:46 -07:00
|
|
|
import android.util.Log;
|
2016-05-19 12:15:39 -07:00
|
|
|
|
2019-11-04 14:50:22 -08:00
|
|
|
import com.android.launcher3.tapl.LauncherInstrumentation;
|
|
|
|
|
|
2018-10-09 18:46:45 -07:00
|
|
|
import org.junit.Assert;
|
|
|
|
|
|
2019-12-12 14:18:19 -08:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
2016-05-19 12:15:39 -07:00
|
|
|
/**
|
|
|
|
|
* A utility class for waiting for a condition to be true.
|
|
|
|
|
*/
|
|
|
|
|
public class Wait {
|
|
|
|
|
|
|
|
|
|
private static final long DEFAULT_SLEEP_MS = 200;
|
|
|
|
|
|
2019-11-04 14:50:22 -08:00
|
|
|
public static void atMost(String message, Condition condition, long timeout,
|
|
|
|
|
LauncherInstrumentation launcher) {
|
2019-12-12 14:18:19 -08:00
|
|
|
atMost(() -> message, condition, timeout, DEFAULT_SLEEP_MS, launcher);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void atMost(Supplier<String> message, Condition condition, long timeout,
|
|
|
|
|
LauncherInstrumentation launcher) {
|
2019-11-04 14:50:22 -08:00
|
|
|
atMost(message, condition, timeout, DEFAULT_SLEEP_MS, launcher);
|
2016-05-19 12:15:39 -07:00
|
|
|
}
|
|
|
|
|
|
2019-12-12 14:18:19 -08:00
|
|
|
public static void atMost(Supplier<String> message, Condition condition, long timeout,
|
|
|
|
|
long sleepMillis,
|
2019-11-04 14:50:22 -08:00
|
|
|
LauncherInstrumentation launcher) {
|
2019-07-31 16:41:46 -07:00
|
|
|
final long startTime = SystemClock.uptimeMillis();
|
|
|
|
|
long endTime = startTime + timeout;
|
|
|
|
|
Log.d("Wait", "atMost: " + startTime + " - " + endTime);
|
2016-05-19 12:15:39 -07:00
|
|
|
while (SystemClock.uptimeMillis() < endTime) {
|
|
|
|
|
try {
|
|
|
|
|
if (condition.isTrue()) {
|
2018-10-09 18:46:45 -07:00
|
|
|
return;
|
2016-05-19 12:15:39 -07:00
|
|
|
}
|
|
|
|
|
} catch (Throwable t) {
|
2019-01-29 16:52:22 -08:00
|
|
|
throw new RuntimeException(t);
|
2016-05-19 12:15:39 -07:00
|
|
|
}
|
|
|
|
|
SystemClock.sleep(sleepMillis);
|
|
|
|
|
}
|
2016-08-27 15:33:16 -07:00
|
|
|
|
|
|
|
|
// Check once more before returning false.
|
|
|
|
|
try {
|
|
|
|
|
if (condition.isTrue()) {
|
2018-10-09 18:46:45 -07:00
|
|
|
return;
|
2016-08-27 15:33:16 -07:00
|
|
|
}
|
|
|
|
|
} catch (Throwable t) {
|
2019-01-29 16:52:22 -08:00
|
|
|
throw new RuntimeException(t);
|
2016-08-27 15:33:16 -07:00
|
|
|
}
|
2019-07-31 16:41:46 -07:00
|
|
|
Log.d("Wait", "atMost: timed out: " + SystemClock.uptimeMillis());
|
2021-09-23 19:19:53 -07:00
|
|
|
launcher.checkForAnomaly(false, false);
|
2019-12-12 14:18:19 -08:00
|
|
|
Assert.fail(message.get());
|
2016-05-19 12:15:39 -07:00
|
|
|
}
|
2020-01-28 10:44:57 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interface representing a generic condition
|
|
|
|
|
*/
|
|
|
|
|
public interface Condition {
|
|
|
|
|
|
|
|
|
|
boolean isTrue() throws Throwable;
|
|
|
|
|
}
|
2016-05-19 12:15:39 -07:00
|
|
|
}
|