Bug 1405081 - Move GeckoRuntime creation out of GeckoSesionTestRule r=jchen
authorJames Willcox <snorp@snorp.net>
Thu, 04 Oct 2018 17:47:14 -0500
changeset 500408 017c51559d4afdbe3478b38d2585ec5566787965
parent 500407 0dd3fef17f153feec51df058785579dcdeb91d83
child 500409 5fda5eb5fd01b8c4d77ed10279d931665c4e86f7
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen
bugs1405081
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1405081 - Move GeckoRuntime creation out of GeckoSesionTestRule r=jchen This allows us to easily share a GeckoRuntime with other tests that don't use GeckoSessionTestRule. Differential Revision: https://phabricator.services.mozilla.com/D7796
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/RuntimeCreator.java
--- 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
@@ -1,27 +1,24 @@
 /* -*- 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.rule;
 
 import org.mozilla.geckoview.GeckoDisplay;
-import org.mozilla.geckoview.BuildConfig;
 import org.mozilla.geckoview.GeckoResult;
-import org.mozilla.geckoview.GeckoResult.OnExceptionListener;
 import org.mozilla.geckoview.GeckoResult.OnValueListener;
 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.RuntimeCreator;
 import org.mozilla.geckoview.test.util.Environment;
-import org.mozilla.geckoview.test.TestCrashHandler;
 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.*;
@@ -36,21 +33,17 @@ import org.junit.rules.ErrorCollector;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 
 import android.app.Instrumentation;
 import android.graphics.Point;
 import android.graphics.SurfaceTexture;
 import android.net.LocalSocketAddress;
-import android.os.Build;
-import android.os.Bundle;
-import android.os.Debug;
 import android.os.Looper;
-import android.os.Process;
 import android.os.SystemClock;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.test.InstrumentationRegistry;
 import android.util.Pair;
 import android.view.MotionEvent;
 import android.view.Surface;
 
@@ -808,17 +801,16 @@ public class GeckoSessionTestRule implem
             addCallbackClasses(list, ifce);
         }
 
         return new HashSet<>(list);
     }
 
     private static final Set<Class<?>> DEFAULT_DELEGATES = getDefaultDelegates();
 
-    private static GeckoRuntime sRuntime;
     private static RDPConnection sRDPConnection;
     protected static GeckoSession sCachedSession;
     protected static Tab sCachedRDPTab;
 
     public final Environment env = new Environment();
 
     protected final Instrumentation mInstrumentation =
             InstrumentationRegistry.getInstrumentation();
@@ -939,17 +931,17 @@ public class GeckoSessionTestRule implem
     }
 
     /**
      * Get the runtime set up for the current test.
      *
      * @return GeckoRuntime object.
      */
     public @NonNull GeckoRuntime getRuntime() {
-        return sRuntime;
+        return RuntimeCreator.getRuntime();
     }
 
     protected static Object setDelegate(final @NonNull Class<?> cls,
                                         final @NonNull GeckoSession session,
                                         final @Nullable Object delegate)
             throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
         if (cls == GeckoSession.TextInputDelegate.class) {
             return SessionTextInput.class.getMethod("setDelegate",
@@ -1115,17 +1107,17 @@ public class GeckoSessionTestRule implem
                                    args, arrayWithSize(greaterThan(0)));
                         assertThat("Callback first argument must be session object",
                                    args[0], instanceOf(GeckoSession.class));
                         session = (GeckoSession) args[0];
                     }
 
                     if (sOnCrash.equals(method) && !mIgnoreCrash && isUsingSession(session)) {
                         if (env.shouldShutdownOnCrash()) {
-                            sRuntime.shutdown();
+                            getRuntime().shutdown();
                         }
 
                         throw new ChildCrashedException("Child process crashed");
                     }
 
                     records.add(new CallRecord(session, method, args));
 
                     call = waitDelegates.prepareMethodCall(session, method);
@@ -1187,40 +1179,16 @@ public class GeckoSessionTestRule implem
         };
 
         final Class<?>[] classes = DEFAULT_DELEGATES.toArray(
                 new Class<?>[DEFAULT_DELEGATES.size()]);
         mCallbackProxy = Proxy.newProxyInstance(GeckoSession.class.getClassLoader(),
                                                 classes, recorder);
         mAllDelegates = new HashSet<>(DEFAULT_DELEGATES);
 
-        if (sRuntime == null) {
-            final GeckoRuntimeSettings.Builder runtimeSettingsBuilder =
-                new GeckoRuntimeSettings.Builder();
-            runtimeSettingsBuilder.arguments(new String[] { "-purgecaches" })
-                    .extras(InstrumentationRegistry.getArguments())
-                    .remoteDebuggingEnabled(true)
-                    .consoleOutput(true);
-
-            if (env.isAutomation()) {
-                runtimeSettingsBuilder.crashHandler(TestCrashHandler.class);
-            }
-
-            sRuntime = GeckoRuntime.create(
-                InstrumentationRegistry.getTargetContext(),
-                runtimeSettingsBuilder.build());
-
-            sRuntime.setDelegate(new GeckoRuntime.Delegate() {
-                @Override
-                public void onShutdown() {
-                    Process.killProcess(Process.myPid());
-                }
-            });
-        }
-
         if (sCachedSession != null && !sCachedSession.isOpen()) {
             sCachedSession = null;
         }
 
         final boolean useDefaultSession = !mClosedSession && mDefaultSettings.equals(settings);
         if (useDefaultSession && mReuseSession && sCachedSession != null) {
             mMainSession = sCachedSession;
         } else {
@@ -1262,17 +1230,17 @@ public class GeckoSessionTestRule implem
 
     /**
      * Call open() on a session, and ensure it's ready for use by the test. In particular,
      * remove any extra calls recorded as part of opening the session.
      *
      * @param session Session to open.
      */
     public void openSession(final GeckoSession session) {
-        session.open(sRuntime);
+        session.open(getRuntime());
         waitForOpenSession(session);
     }
 
     /* package */ void waitForOpenSession(final GeckoSession session) {
         waitForInitialLoad(session);
 
         if (mWithDevTools) {
             if (sRDPConnection == null) {
@@ -1366,18 +1334,18 @@ public class GeckoSessionTestRule implem
             session.close();
         }
     }
 
     protected boolean isUsingSession(final GeckoSession session) {
         return session.equals(mMainSession) || mSubSessions.contains(session);
     }
 
-    protected static void deleteCrashDumps() {
-        File dumpDir = new File(sRuntime.getProfileDir(), "minidumps");
+    protected void deleteCrashDumps() {
+        File dumpDir = new File(getRuntime().getProfileDir(), "minidumps");
         for (final File dump : dumpDir.listFiles()) {
             dump.delete();
         }
     }
 
     protected void cleanupStatement() throws Throwable {
         mWaitScopeDelegates.clear();
         mTestScopeDelegates.clear();
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/RuntimeCreator.java
@@ -0,0 +1,42 @@
+package org.mozilla.geckoview.test.util;
+
+import org.mozilla.geckoview.GeckoRuntime;
+import org.mozilla.geckoview.GeckoRuntimeSettings;
+import org.mozilla.geckoview.test.TestCrashHandler;
+
+import android.os.Process;
+import android.support.test.InstrumentationRegistry;
+
+public class RuntimeCreator {
+    private static GeckoRuntime sRuntime;
+
+    public static GeckoRuntime getRuntime() {
+        if (sRuntime != null) {
+            return sRuntime;
+        }
+
+        final GeckoRuntimeSettings.Builder runtimeSettingsBuilder =
+                new GeckoRuntimeSettings.Builder();
+        runtimeSettingsBuilder.arguments(new String[]{"-purgecaches"})
+                .extras(InstrumentationRegistry.getArguments())
+                .remoteDebuggingEnabled(true)
+                .consoleOutput(true);
+
+        if (new Environment().isAutomation()) {
+            runtimeSettingsBuilder.crashHandler(TestCrashHandler.class);
+        }
+
+        sRuntime = GeckoRuntime.create(
+                InstrumentationRegistry.getTargetContext(),
+                runtimeSettingsBuilder.build());
+
+        sRuntime.setDelegate(new GeckoRuntime.Delegate() {
+            @Override
+            public void onShutdown() {
+                Process.killProcess(Process.myPid());
+            }
+        });
+
+        return sRuntime;
+    }
+}