Bug 1553515 - Add waitForCondition. r=snorp

Differential Revision: https://phabricator.services.mozilla.com/D32581

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Agi Sferro 2019-07-08 20:48:33 +00:00
Родитель 7371119f56
Коммит f3c69e1f87
2 изменённых файлов: 34 добавлений и 11 удалений

Просмотреть файл

@ -397,9 +397,8 @@ public class GeckoSessionTestRule implements TestRule {
* @return Fulfilled value of the promise.
*/
public Object getValue() {
while (mPromise.isPending()) {
UiThreadUtils.loopUntilIdle(mTimeoutMillis);
}
UiThreadUtils.waitForCondition(() -> !mPromise.isPending(), mTimeoutMillis);
if (mPromise.isRejected()) {
throw new RejectedPromiseException(mPromise.getReason());
}
@ -1188,7 +1187,6 @@ public class GeckoSessionTestRule implements TestRule {
classes, recorder);
mAllDelegates = new HashSet<>(DEFAULT_DELEGATES);
final boolean useDefaultSession = !mClosedSession && mDefaultSettings.equals(settings);
mMainSession = new GeckoSession(settings);
prepareSession(mMainSession);
@ -1264,10 +1262,8 @@ public class GeckoSessionTestRule implements TestRule {
}
};
do {
UiThreadUtils.loopUntilIdle(env.getDefaultTimeoutMillis());
} while (mCallRecordHandler != null);
UiThreadUtils.waitForCondition(() -> mCallRecordHandler == null,
env.getDefaultTimeoutMillis());
} finally {
mCallRecordHandler = null;
}
@ -1986,9 +1982,7 @@ public class GeckoSessionTestRule implements TestRule {
private Object evaluateJS(final @NonNull Tab tab, final @NonNull String js) {
final Actor.Reply<Object> reply = tab.getConsole().evaluateJS(js);
while (!reply.hasResult()) {
UiThreadUtils.loopUntilIdle(mTimeoutMillis);
}
UiThreadUtils.waitForCondition(reply::hasResult, mTimeoutMillis);
final Object result = reply.get();
if (result instanceof Promise) {

Просмотреть файл

@ -126,6 +126,35 @@ public class UiThreadUtils {
}
}
public interface Condition {
boolean test();
}
public static void waitForCondition(Condition condition, final long timeout) {
// Adapted from GeckoThread.pumpMessageLoop.
final MessageQueue queue = HANDLER.getLooper().getQueue();
TIMEOUT_RUNNABLE.set(timeout);
try {
while (!condition.test()) {
final Message msg;
try {
msg = (Message) sGetNextMessage.invoke(queue);
} catch (final IllegalAccessException | InvocationTargetException e) {
throw unwrapRuntimeException(e);
}
if (msg.getTarget() == null) {
HANDLER.getLooper().quit();
return;
}
msg.getTarget().dispatchMessage(msg);
}
} finally {
TIMEOUT_RUNNABLE.cancel();
}
}
public static void loopUntilIdle(final long timeout) {
// Adapted from GeckoThread.pumpMessageLoop.
final MessageQueue queue = HANDLER.getLooper().getQueue();