Bug 1468048 - Factor out loopUntilIdle() from GeckoSessionTestRule into UiThreadUtils r=jchen a=lizzard
authorJames Willcox <snorp@snorp.net>
Thu, 05 Jul 2018 14:45:27 -0500
changeset 477939 61a607f98731ece6c4ea3c24586a2c8bfc77a4d1
parent 477938 95d66abccf63483bcd778947778959e0cef6da95
child 477940 3cb6c770f9939b36c8904f114c5db42a5ee3d910
push id9474
push userarchaeopteryx@coole-files.de
push dateFri, 13 Jul 2018 21:13:56 +0000
treeherdermozilla-beta@8c9f4cc4061b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen, lizzard
bugs1468048
milestone62.0
Bug 1468048 - Factor out loopUntilIdle() from GeckoSessionTestRule into UiThreadUtils r=jchen a=lizzard This allows us to write other tests that need similar loop integration without having to use GeckoSessionTestRule. MozReview-Commit-ID: LfVSGhvyMoP
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoSessionTestRuleTest.kt
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/UiThreadUtils.java
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoSessionTestRuleTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoSessionTestRuleTest.kt
@@ -14,16 +14,17 @@ import org.mozilla.geckoview.test.rule.G
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.RejectedPromiseException
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.ReuseSession
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.Setting
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.TimeoutException
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.TimeoutMillis
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.WithDevToolsAPI
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.WithDisplay
 import org.mozilla.geckoview.test.util.Callbacks
+import org.mozilla.geckoview.test.util.UiThreadUtils
 
 import android.support.test.filters.MediumTest
 import android.support.test.runner.AndroidJUnit4
 
 import org.hamcrest.Matchers.*
 import org.junit.Assert.fail
 import org.junit.Assume.assumeThat
 import org.junit.Test
@@ -61,17 +62,17 @@ class GeckoSessionTestRuleTest : BaseSes
                    sessionRule.session.settings.getInt(GeckoSessionSettings.DISPLAY_MODE),
                    equalTo(GeckoSessionSettings.DISPLAY_MODE_MINIMAL_UI))
         assertThat("USE_TRACKING_PROTECTION should be set",
                    sessionRule.session.settings.getBoolean(
                            GeckoSessionSettings.USE_TRACKING_PROTECTION),
                    equalTo(true))
     }
 
-    @Test(expected = TimeoutException::class)
+    @Test(expected = UiThreadUtils.TimeoutException::class)
     @TimeoutMillis(1000)
     fun noPendingCallbacks() {
         // Make sure we don't have unexpected pending callbacks at the start of a test.
         sessionRule.waitUntilCalled(object : Callbacks.All {})
     }
 
     @Test fun includesAllCallbacks() {
         for (ifce in GeckoSession::class.java.classes) {
@@ -928,17 +929,17 @@ class GeckoSessionTestRuleTest : BaseSes
     @Test fun createClosedSession_withSettings() {
         val settings = GeckoSessionSettings(sessionRule.session.settings)
         settings.setBoolean(GeckoSessionSettings.USE_PRIVATE_MODE, true)
 
         val newSession = sessionRule.createClosedSession(settings)
         assertThat("New session has same settings", newSession.settings, equalTo(settings))
     }
 
-    @Test(expected = TimeoutException::class)
+    @Test(expected = UiThreadUtils.TimeoutException::class)
     @TimeoutMillis(1000)
     @ClosedSessionAtStart
     fun noPendingCallbacks_withSpecificSession() {
         sessionRule.createOpenSession()
         // Make sure we don't have unexpected pending callbacks after opening a session.
         sessionRule.waitUntilCalled(object : Callbacks.All {})
     }
 
@@ -1323,17 +1324,17 @@ class GeckoSessionTestRuleTest : BaseSes
         // by calling alert(), which blocks until prompt delegate is called.
         assertThat("JS blocking result should be correct",
                    sessionRule.session.evaluateJS("alert(); 'foo'") as String,
                    equalTo("foo"))
     }
 
     @WithDevToolsAPI
     @TimeoutMillis(1000)
-    @Test(expected = TimeoutException::class)
+    @Test(expected = UiThreadUtils.TimeoutException::class)
     fun evaluateJS_canTimeout() {
         sessionRule.session.delegateUntilTestEnd(object : Callbacks.PromptDelegate {
             override fun onAlert(session: GeckoSession, title: String, msg: String, callback: GeckoSession.PromptDelegate.AlertCallback) {
                 // Do nothing for the alert, so it hangs forever.
             }
         })
         sessionRule.session.evaluateJS("alert()")
     }
--- 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
@@ -8,16 +8,17 @@ package org.mozilla.geckoview.test.rule;
 import org.mozilla.gecko.gfx.GeckoDisplay;
 import org.mozilla.geckoview.BuildConfig;
 import org.mozilla.geckoview.GeckoResponse;
 import org.mozilla.geckoview.GeckoRuntime;
 import org.mozilla.geckoview.GeckoRuntimeSettings;
 import org.mozilla.geckoview.GeckoSession;
 import org.mozilla.geckoview.GeckoSessionSettings;
 import org.mozilla.geckoview.SessionTextInput;
+import org.mozilla.geckoview.test.util.UiThreadUtils;
 import org.mozilla.geckoview.test.rdp.Actor;
 import org.mozilla.geckoview.test.rdp.Promise;
 import org.mozilla.geckoview.test.rdp.RDPConnection;
 import org.mozilla.geckoview.test.rdp.Tab;
 import org.mozilla.geckoview.test.util.Callbacks;
 
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.assertThat;
@@ -91,26 +92,23 @@ public class GeckoSessionTestRule extend
     private static final long DEFAULT_ARM_DEVICE_TIMEOUT_MILLIS = 30000;
     private static final long DEFAULT_ARM_EMULATOR_TIMEOUT_MILLIS = 120000;
     private static final long DEFAULT_X86_DEVICE_TIMEOUT_MILLIS = 30000;
     private static final long DEFAULT_X86_EMULATOR_TIMEOUT_MILLIS = 5000;
     private static final long DEFAULT_IDE_DEBUG_TIMEOUT_MILLIS = 86400000;
 
     public static final String APK_URI_PREFIX = "resource://android/";
 
-    private static final Method sGetNextMessage;
     private static final Method sOnPageStart;
     private static final Method sOnPageStop;
     private static final Method sOnNewSession;
     private static final Method sOnCrash;
 
     static {
         try {
-            sGetNextMessage = MessageQueue.class.getDeclaredMethod("next");
-            sGetNextMessage.setAccessible(true);
             sOnPageStart = GeckoSession.ProgressDelegate.class.getMethod(
                     "onPageStart", GeckoSession.class, String.class);
             sOnPageStop = GeckoSession.ProgressDelegate.class.getMethod(
                     "onPageStop", GeckoSession.class, boolean.class);
             sOnNewSession = GeckoSession.NavigationDelegate.class.getMethod(
                     "onNewSession", GeckoSession.class, String.class, GeckoResponse.class);
             sOnCrash = GeckoSession.ContentDelegate.class.getMethod(
                     "onCrash", GeckoSession.class);
@@ -332,22 +330,16 @@ public class GeckoSessionTestRule extend
     @Retention(RetentionPolicy.RUNTIME)
     public @interface IgnoreCrash {
         /**
          * @return True if content crashes should be ignored, false otherwise. Default is true.
          */
         boolean value() default true;
     }
 
-    public static class TimeoutException extends RuntimeException {
-        public TimeoutException(final String detailMessage) {
-            super(detailMessage);
-        }
-    }
-
     public static class ChildCrashedException extends RuntimeException {
         public ChildCrashedException(final String detailMessage) {
             super(detailMessage);
         }
     }
 
     public static class RejectedPromiseException extends RuntimeException {
         private final Object mReason;
@@ -398,17 +390,17 @@ public class GeckoSessionTestRule extend
         /**
          * Wait for this promise to settle. If the promise is fulfilled, return its value.
          * If the promise is rejected, throw an exception containing the reason.
          *
          * @return Fulfilled value of the promise.
          */
         public Object getValue() {
             while (mPromise.isPending()) {
-                loopUntilIdle(mTimeoutMillis);
+                UiThreadUtils.loopUntilIdle(mTimeoutMillis);
             }
             if (mPromise.isRejected()) {
                 throw new RejectedPromiseException(mPromise.getReason());
             }
             return mPromise.getValue();
         }
     }
 
@@ -865,50 +857,18 @@ public class GeckoSessionTestRule extend
             addCallbackClasses(list, ifce);
         }
 
         return new HashSet<>(list);
     }
 
     private static final Set<Class<?>> DEFAULT_DELEGATES = getDefaultDelegates();
 
-    private static final class TimeoutRunnable implements Runnable {
-        private long timeout;
-
-        public void set(final long timeout) {
-            this.timeout = timeout;
-            cancel();
-            HANDLER.postDelayed(this, timeout);
-        }
-
-        public void cancel() {
-            HANDLER.removeCallbacks(this);
-        }
-
-        @Override
-        public void run() {
-            throw new TimeoutException("Timed out after " + timeout + "ms");
-        }
-    }
-
-    /* package */ static final Handler HANDLER = new Handler(Looper.getMainLooper());
-    private static final TimeoutRunnable TIMEOUT_RUNNABLE = new TimeoutRunnable();
-    private static final MessageQueue.IdleHandler IDLE_HANDLER = new MessageQueue.IdleHandler() {
-        @Override
-        public boolean queueIdle() {
-            final Message msg = Message.obtain(HANDLER);
-            msg.obj = HANDLER;
-            HANDLER.sendMessageAtFrontOfQueue(msg);
-            return false; // Remove this idle handler.
-        }
-    };
-
     private static GeckoRuntime sRuntime;
     private static RDPConnection sRDPConnection;
-    private static long sLongestWait;
     protected static GeckoSession sCachedSession;
     protected static Tab sCachedRDPTab;
 
     public final Environment env = new Environment();
 
     protected final Instrumentation mInstrumentation =
             InstrumentationRegistry.getInstrumentation();
     protected final GeckoSessionSettings mDefaultSettings;
@@ -1384,32 +1344,32 @@ public class GeckoSessionTestRule extend
                     } else if (matching && mIsAboutBlank && sOnPageStop.equals(method)) {
                         mCallRecordHandler = null;
                     }
                     return matching;
                 }
             };
 
             do {
-                loopUntilIdle(getDefaultTimeoutMillis());
+                UiThreadUtils.loopUntilIdle(getDefaultTimeoutMillis());
             } while (mCallRecordHandler != null);
 
         } finally {
             mCallRecordHandler = null;
         }
     }
 
     /**
      * Internal method to perform callback checks at the end of a test.
      */
     public void performTestEndCheck() {
         if (sCachedSession != null && mIgnoreCrash) {
             // Make sure the cached session has been closed by crashes.
             while (sCachedSession.isOpen()) {
-                loopUntilIdle(mTimeoutMillis);
+                UiThreadUtils.loopUntilIdle(mTimeoutMillis);
             }
         }
 
         mWaitScopeDelegates.clearAndAssert();
         mTestScopeDelegates.clearAndAssert();
 
         if (sCachedSession != null && mReuseSession) {
             assertThat("Cached session should be open",
@@ -1503,69 +1463,16 @@ public class GeckoSessionTestRule extend
     }
 
     @Override
     protected boolean shouldRunOnUiThread(final Description description) {
         return true;
     }
 
     /**
-     * Loop the current thread until the message queue is idle. If loop is already idle and
-     * timeout is not specified, return immediately. If loop is already idle and timeout is
-     * specified, wait for a message to arrive first; an exception is thrown if timeout
-     * expires during the wait.
-     *
-     * @param timeout Wait timeout in milliseconds or 0 to not wait.
-     */
-    protected static void loopUntilIdle(final long timeout) {
-        // Adapted from GeckoThread.pumpMessageLoop.
-        final MessageQueue queue = HANDLER.getLooper().getQueue();
-        if (timeout > 0) {
-            TIMEOUT_RUNNABLE.set(timeout);
-        } else {
-            queue.addIdleHandler(IDLE_HANDLER);
-        }
-
-        final long startTime = SystemClock.uptimeMillis();
-        try {
-            while (true) {
-                final Message msg;
-                try {
-                    msg = (Message) sGetNextMessage.invoke(queue);
-                } catch (final IllegalAccessException | InvocationTargetException e) {
-                    throw unwrapRuntimeException(e);
-                }
-                if (msg.getTarget() == HANDLER && msg.obj == HANDLER) {
-                    // Our idle signal.
-                    break;
-                } else if (msg.getTarget() == null) {
-                    HANDLER.getLooper().quit();
-                    return;
-                }
-                msg.getTarget().dispatchMessage(msg);
-
-                if (timeout > 0) {
-                    TIMEOUT_RUNNABLE.cancel();
-                    queue.addIdleHandler(IDLE_HANDLER);
-                }
-            }
-
-            final long waitDuration = SystemClock.uptimeMillis() - startTime;
-            if (waitDuration > sLongestWait) {
-                sLongestWait = waitDuration;
-                Log.i(LOGTAG, "New longest wait: " + waitDuration + "ms");
-            }
-        } finally {
-            if (timeout > 0) {
-                TIMEOUT_RUNNABLE.cancel();
-            }
-        }
-    }
-
-    /**
      * Wait until a page load has finished on any session. A session must have started a
      * page load since the last wait, or this method will wait indefinitely.
      */
     public void waitForPageStop() {
         waitForPageStop(/* session */ null);
     }
 
     /**
@@ -1790,17 +1697,17 @@ public class GeckoSessionTestRule extend
         }
 
         boolean calledAny = false;
         int index = mLastWaitEnd;
         beforeWait();
 
         while (!calledAny || !methodCalls.isEmpty()) {
             while (index >= mCallRecords.size()) {
-                loopUntilIdle(mTimeoutMillis);
+                UiThreadUtils.loopUntilIdle(mTimeoutMillis);
             }
 
             final MethodCall recorded = mCallRecords.get(index).methodCall;
             calledAny |= recorded.method.getDeclaringClass().isAssignableFrom(delegate);
             index++;
 
             final int i = methodCalls.indexOf(recorded);
             if (i < 0) {
@@ -2152,17 +2059,17 @@ public class GeckoSessionTestRule extend
             assertThat("Should have chrome process object",
                        mRDPChromeProcess, notNullValue());
         }
     }
 
     private Object evaluateJS(final @NonNull Tab tab, final @NonNull String js) {
         final Actor.Reply<Object> reply = tab.getConsole().evaluateJS(js);
         while (!reply.hasResult()) {
-            loopUntilIdle(mTimeoutMillis);
+            UiThreadUtils.loopUntilIdle(mTimeoutMillis);
         }
 
         final Object result = reply.get();
         if (result instanceof Promise) {
             // Map the static Promise into a live Promise. In order to perform the mapping, we set
             // a tag on the static Promise, fetch a list of live Promises, and see which live
             // Promise has the same tag on it.
             final String tag = String.valueOf(result.hashCode());
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/UiThreadUtils.java
@@ -0,0 +1,119 @@
+package org.mozilla.geckoview.test.util;
+
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.MessageQueue;
+import android.os.SystemClock;
+import android.util.Log;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class UiThreadUtils {
+    private static final String LOGTAG = "UiThreadUtils";
+    private static long sLongestWait;
+
+    private static Method sGetNextMessage = null;
+    static {
+        try {
+            sGetNextMessage = MessageQueue.class.getDeclaredMethod("next");
+            sGetNextMessage.setAccessible(true);
+        } catch (NoSuchMethodException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    public static class TimeoutException extends RuntimeException {
+        public TimeoutException(final String detailMessage) {
+            super(detailMessage);
+        }
+    }
+
+    private static final class TimeoutRunnable implements Runnable {
+        private long timeout;
+
+        public void set(final long timeout) {
+            this.timeout = timeout;
+            cancel();
+            HANDLER.postDelayed(this, timeout);
+        }
+
+        public void cancel() {
+            HANDLER.removeCallbacks(this);
+        }
+
+        @Override
+        public void run() {
+            throw new TimeoutException("Timed out after " + timeout + "ms");
+        }
+    }
+
+    public static final Handler HANDLER = new Handler(Looper.getMainLooper());
+    private static final TimeoutRunnable TIMEOUT_RUNNABLE = new TimeoutRunnable();
+    private static final MessageQueue.IdleHandler IDLE_HANDLER = new MessageQueue.IdleHandler() {
+        @Override
+        public boolean queueIdle() {
+            final Message msg = Message.obtain(HANDLER);
+            msg.obj = HANDLER;
+            HANDLER.sendMessageAtFrontOfQueue(msg);
+            return false; // Remove this idle handler.
+        }
+    };
+
+    private static RuntimeException unwrapRuntimeException(final Throwable e) {
+        final Throwable cause = e.getCause();
+        if (cause != null && cause instanceof RuntimeException) {
+            return (RuntimeException) cause;
+        } else if (e instanceof RuntimeException) {
+            return (RuntimeException) e;
+        }
+
+        return new RuntimeException(cause != null ? cause : e);
+    }
+
+    public static void loopUntilIdle(final long timeout) {
+        // Adapted from GeckoThread.pumpMessageLoop.
+        final MessageQueue queue = HANDLER.getLooper().getQueue();
+        if (timeout > 0) {
+            TIMEOUT_RUNNABLE.set(timeout);
+        } else {
+            queue.addIdleHandler(IDLE_HANDLER);
+        }
+
+        final long startTime = SystemClock.uptimeMillis();
+        try {
+            while (true) {
+                final Message msg;
+                try {
+                    msg = (Message) sGetNextMessage.invoke(queue);
+                } catch (final IllegalAccessException | InvocationTargetException e) {
+                    throw unwrapRuntimeException(e);
+                }
+                if (msg.getTarget() == HANDLER && msg.obj == HANDLER) {
+                    // Our idle signal.
+                    break;
+                } else if (msg.getTarget() == null) {
+                    HANDLER.getLooper().quit();
+                    return;
+                }
+                msg.getTarget().dispatchMessage(msg);
+
+                if (timeout > 0) {
+                    TIMEOUT_RUNNABLE.cancel();
+                    queue.addIdleHandler(IDLE_HANDLER);
+                }
+            }
+
+            final long waitDuration = SystemClock.uptimeMillis() - startTime;
+            if (waitDuration > sLongestWait) {
+                sLongestWait = waitDuration;
+                Log.i(LOGTAG, "New longest wait: " + waitDuration + "ms");
+            }
+        } finally {
+            if (timeout > 0) {
+                TIMEOUT_RUNNABLE.cancel();
+            }
+        }
+    }
+}