2016-05-19 12:15:39 -07:00
|
|
|
package com.android.launcher3.util;
|
|
|
|
|
|
|
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
2018-10-09 18:46:45 -07:00
|
|
|
import org.junit.Assert;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2018-10-09 18:46:45 -07:00
|
|
|
public static void atMost(String message, Condition condition, long timeout) {
|
|
|
|
|
atMost(message, condition, timeout, DEFAULT_SLEEP_MS);
|
2016-05-19 12:15:39 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-09 18:46:45 -07:00
|
|
|
public static void atMost(String message, Condition condition, long timeout, long sleepMillis) {
|
2016-05-19 12:15:39 -07:00
|
|
|
long endTime = SystemClock.uptimeMillis() + timeout;
|
|
|
|
|
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) {
|
|
|
|
|
// Ignore
|
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
// Ignore
|
|
|
|
|
}
|
2018-10-09 18:46:45 -07:00
|
|
|
Assert.fail(message);
|
2016-05-19 12:15:39 -07:00
|
|
|
}
|
|
|
|
|
}
|