diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java index 6e51003b571f..01c09c67ce22 100644 --- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java +++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java @@ -2243,46 +2243,14 @@ public class GeckoSessionTestRule implements TestRule { * @param The type of the value held by the {@link GeckoResult} * @return The value of the completed {@link GeckoResult}. */ - public T waitForResult(@NonNull GeckoResult result) { - final ResultHolder holder = new ResultHolder<>(result); - + public T waitForResult(@NonNull GeckoResult result) throws Throwable { + beforeWait(); try { - beforeWait(); - while (!holder.isComplete) { - UiThreadUtils.loopUntilIdle(mTimeoutMillis); - } + return UiThreadUtils.waitForResult(result, mTimeoutMillis); + } catch (final Throwable e) { + throw unwrapRuntimeException(e); } finally { afterWait(mCallRecords.size()); } - - if (holder.error != null) { - throw unwrapRuntimeException(holder.error); - } - - return holder.value; - } - - private static class ResultHolder { - public T value; - public Throwable error; - public boolean isComplete; - - public ResultHolder(GeckoResult result) { - result.then(new OnValueListener() { - @Override - public GeckoResult onValue(T value) { - ResultHolder.this.value = value; - isComplete = true; - return null; - } - }, new OnExceptionListener() { - @Override - public GeckoResult onException(Throwable error) { - ResultHolder.this.error = error; - isComplete = true; - return null; - } - }); - } } } diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/UiThreadUtils.java b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/UiThreadUtils.java index a9258a6fa364..d9788324d137 100644 --- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/UiThreadUtils.java +++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/UiThreadUtils.java @@ -1,10 +1,18 @@ +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + package org.mozilla.geckoview.test.util; +import org.mozilla.geckoview.GeckoResult; + import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.MessageQueue; import android.os.SystemClock; +import android.support.annotation.NonNull; import android.util.Log; import java.lang.reflect.InvocationTargetException; @@ -72,6 +80,52 @@ public class UiThreadUtils { return new RuntimeException(cause != null ? cause : e); } + /** + * This waits for the given result and returns it's value. If + * the result failed with an exception, it is rethrown. + * + * @param result A {@link GeckoResult} instance. + * @param The type of the value held by the {@link GeckoResult} + * @return The value of the completed {@link GeckoResult}. + */ + public static T waitForResult(@NonNull GeckoResult result, long timeout) throws Throwable { + final ResultHolder holder = new ResultHolder<>(result); + + while (!holder.isComplete) { + loopUntilIdle(timeout); + } + + if (holder.error != null) { + throw holder.error; + } + + return holder.value; + } + + private static class ResultHolder { + public T value; + public Throwable error; + public boolean isComplete; + + public ResultHolder(GeckoResult result) { + result.then(new GeckoResult.OnValueListener() { + @Override + public GeckoResult onValue(T value) { + ResultHolder.this.value = value; + isComplete = true; + return null; + } + }, new GeckoResult.OnExceptionListener() { + @Override + public GeckoResult onException(Throwable error) { + ResultHolder.this.error = error; + isComplete = true; + return null; + } + }); + } + } + public static void loopUntilIdle(final long timeout) { // Adapted from GeckoThread.pumpMessageLoop. final MessageQueue queue = HANDLER.getLooper().getQueue();