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-06-27 15:58:18 +00:00
Родитель fdf7807894
Коммит a00eeafc7b
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();