Bug 1442243 - 1. Add GeckoSessionTestRule.env; r?snorp draft
authorJim Chen <nchen@mozilla.com>
Wed, 07 Mar 2018 16:12:50 -0500
changeset 764510 90c573545950db44eb23723ab9df3d7d4902901b
parent 764272 493e45400842b6ccfffb63b58b40b33a0b8154ab
child 764511 a6591c0f55dec3e3362a17538c8292ba5fc3b86e
push id101776
push userbmo:nchen@mozilla.com
push dateWed, 07 Mar 2018 21:13:11 +0000
reviewerssnorp
bugs1442243
milestone60.0a1
Bug 1442243 - 1. Add GeckoSessionTestRule.env; r?snorp Add an env object to GeckoSessionTestRule to easily check if we're in automation, in e10s mode, etc. MozReview-Commit-ID: 3LIOJoHWPR4
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
--- 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
@@ -17,17 +17,17 @@ import android.support.test.runner.Andro
 
 import org.hamcrest.Matcher
 import org.hamcrest.Matchers.*
 import org.junit.Rule
 import org.junit.Test
 import org.junit.rules.ErrorCollector
 import org.junit.runner.RunWith
 
-inline fun GeckoSession.loadTestPath(path: String) =
+fun GeckoSession.loadTestPath(path: String) =
         this.loadUri(GeckoSessionTestRule.APK_URI_PREFIX + path.removePrefix("/"))
 
 /**
  * Test for the GeckoSessionTestRule class, to ensure it properly sets up a session for
  * each test, and to ensure it can properly wait for and assert delegate
  * callbacks.
  */
 @RunWith(AndroidJUnit4::class)
--- 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
@@ -15,16 +15,18 @@ import org.hamcrest.Matcher;
 
 import org.junit.rules.ErrorCollector;
 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.os.Bundle;
+import android.os.Debug;
 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.support.annotation.Nullable;
 import android.support.test.InstrumentationRegistry;
@@ -59,16 +61,17 @@ import kotlin.reflect.KClass;
  * TestRule that, for each test, sets up a GeckoSession, runs the test on the UI thread,
  * and tears down the GeckoSession at the end of the test. The rule also provides methods
  * for waiting on particular callbacks to be called, and methods for asserting that
  * callbacks are called in the proper order.
  */
 public class GeckoSessionTestRule extends UiThreadTestRule {
 
     private static final long DEFAULT_TIMEOUT_MILLIS = 10000;
+    private static final long DEFAULT_DEBUG_TIMEOUT_MILLIS = 86400000;
     public static final String APK_URI_PREFIX = "resource://android/";
 
     /**
      * Specify the timeout for any of the wait methods, in milliseconds. Can be used
      * on classes or methods.
      */
     @Target({ElementType.METHOD, ElementType.TYPE})
     @Retention(RetentionPolicy.RUNTIME)
@@ -323,16 +326,61 @@ public class GeckoSessionTestRule extend
             return (m1.getDeclaringClass().isAssignableFrom(m2.getDeclaringClass()) ||
                     m2.getDeclaringClass().isAssignableFrom(m1.getDeclaringClass())) &&
                     m1.getName().equals(m2.getName()) &&
                     m1.getReturnType().equals(m2.getReturnType()) &&
                     Arrays.equals(m1.getParameterTypes(), m2.getParameterTypes());
         }
     }
 
+    protected static class CallRecord {
+        public final Method method;
+        public final MethodCall methodCall;
+        public final Object[] args;
+
+        public CallRecord(final Method method, final Object[] args) {
+            this.method = method;
+            this.methodCall = new MethodCall(method, /* requirement */ null);
+            this.args = args;
+        }
+    }
+
+    public class Environment {
+        /* package */ Environment() {
+        }
+
+        private String getEnvVar(final String name) {
+            final int nameLen = name.length();
+            final Bundle args = InstrumentationRegistry.getArguments();
+            String env = args.getString("env0", null);
+            for (int i = 1; env != null; i++) {
+                if (env.length() >= nameLen + 1 &&
+                        env.startsWith(name) &&
+                        env.charAt(nameLen) == '=') {
+                    return env.substring(nameLen + 1);
+                }
+                env = args.getString("env" + i, null);
+            }
+            return "";
+        }
+
+        public boolean isAutomation() {
+            return !getEnvVar("MOZ_IN_AUTOMATION").isEmpty();
+        }
+
+        public boolean isE10s() {
+            return mSession.getSettings().getBoolean(
+                    GeckoSessionSettings.USE_MULTIPROCESS);
+        }
+
+        public boolean isDebugging() {
+            return Debug.isDebuggerConnected();
+        }
+    }
+
     protected class CallbackDelegates {
         private final Map<Method, MethodCall> mDelegates = new HashMap<>();
         private int mOrder;
 
         public void delegate(final Object callback) {
             for (final Class<?> ifce : CALLBACK_CLASSES) {
                 if (!ifce.isInstance(callback)) {
                     continue;
@@ -373,28 +421,16 @@ public class GeckoSessionTestRule extend
             assertAllowMoreCalls(call);
             call.incrementCounter();
             assertOrder(call, mOrder);
             mOrder = Math.max(call.getOrder(), mOrder);
             return call;
         }
     }
 
-    protected static class CallRecord {
-        public final Method method;
-        public final MethodCall methodCall;
-        public final Object[] args;
-
-        public CallRecord(final Method method, final Object[] args) {
-            this.method = method;
-            this.methodCall = new MethodCall(method, /* requirement */ null);
-            this.args = args;
-        }
-    }
-
     /* package */ static AssertCalled getAssertCalled(final Method method, final Object callback) {
         final AssertCalled annotation = method.getAnnotation(AssertCalled.class);
         if (annotation != null) {
             return annotation;
         }
 
         // Some Kotlin lambdas have an invoke method that carries the annotation,
         // instead of the interface method carrying the annotation.
@@ -426,31 +462,33 @@ public class GeckoSessionTestRule extend
         }
 
         final HashSet<Class<?>> set = new HashSet<>(list);
         return set.toArray(new Class<?>[set.size()]);
     }
 
     private static final List<Class<?>> CALLBACK_CLASSES = Arrays.asList(getCallbackClasses());
 
+    public final Environment env = new Environment();
+
     protected final Instrumentation mInstrumentation =
             InstrumentationRegistry.getInstrumentation();
     protected final GeckoSessionSettings mDefaultSettings;
 
     protected ErrorCollector mErrorCollector;
     protected GeckoSession mSession;
     protected Point mDisplaySize;
     protected Object mCallbackProxy;
     protected List<CallRecord> mCallRecords;
     protected CallbackDelegates mWaitScopeDelegates;
     protected CallbackDelegates mTestScopeDelegates;
     protected int mLastWaitStart;
     protected int mLastWaitEnd;
     protected MethodCall mCurrentMethodCall;
-    protected long mTimeoutMillis = DEFAULT_TIMEOUT_MILLIS;
+    protected long mTimeoutMillis;
     protected SurfaceTexture mDisplayTexture;
     protected Surface mDisplaySurface;
     protected GeckoDisplay mDisplay;
 
     public GeckoSessionTestRule() {
         mDefaultSettings = new GeckoSessionSettings();
     }
 
@@ -557,16 +595,18 @@ public class GeckoSessionTestRule extend
             return (RuntimeException)cause;
         }
 
         return new RuntimeException(cause);
     }
 
     protected void prepareSession(final Description description) throws Throwable {
         final GeckoSessionSettings settings = new GeckoSessionSettings(mDefaultSettings);
+        mTimeoutMillis = !env.isDebugging() ? DEFAULT_TIMEOUT_MILLIS
+                                            : DEFAULT_DEBUG_TIMEOUT_MILLIS;
 
         applyAnnotations(Arrays.asList(description.getTestClass().getAnnotations()), settings);
         applyAnnotations(description.getAnnotations(), settings);
 
         final List<CallRecord> records = new ArrayList<>();
         final CallbackDelegates waitDelegates = new CallbackDelegates();
         final CallbackDelegates testDelegates = new CallbackDelegates();
         mCallRecords = records;
@@ -653,17 +693,17 @@ public class GeckoSessionTestRule extend
 
         mSession = null;
         mCallbackProxy = null;
         mCallRecords = null;
         mWaitScopeDelegates = null;
         mTestScopeDelegates = null;
         mLastWaitStart = 0;
         mLastWaitEnd = 0;
-        mTimeoutMillis = DEFAULT_TIMEOUT_MILLIS;
+        mTimeoutMillis = 0;
     }
 
     @Override
     public Statement apply(final Statement base, final Description description) {
         return super.apply(new Statement() {
             @Override
             public void evaluate() throws Throwable {
                 try {