Bug 1441982 - Don't wrap RuntimeException instances if they are already a RuntimeException r=jchen draft
authorJames Willcox <snorp@snorp.net>
Tue, 27 Feb 2018 18:13:00 -0500
changeset 763876 d7ec0d83c32a6174e41ab32ae370bc2453fdf948
parent 763875 c555770cef5889111d905bcf621743801b36b424
child 763877 e646d4c037b9f3faf4c686cc881b92be6614bfb8
push id101579
push userbmo:snorp@snorp.net
push dateTue, 06 Mar 2018 20:04:07 +0000
reviewersjchen
bugs1441982
milestone60.0a1
Bug 1441982 - Don't wrap RuntimeException instances if they are already a RuntimeException r=jchen Otherwise we can't use @Test(expected = IllegalArgumentException) MozReview-Commit-ID: EWKtUu5ok8X
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.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
@@ -24,17 +24,16 @@ 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;
 import android.support.test.rule.UiThreadTestRule;
-import android.view.Display;
 import android.view.MotionEvent;
 import android.view.Surface;
 
 import java.lang.annotation.Annotation;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
@@ -547,16 +546,25 @@ public class GeckoSessionTestRule extend
                 }
             } else if (WithDisplay.class.equals(annotation.annotationType())) {
                 final WithDisplay displaySize = (WithDisplay)annotation;
                 mDisplaySize = new Point(displaySize.width(), displaySize.height());
             }
         }
     }
 
+    private static RuntimeException unwrapRuntimeException(Throwable e) {
+        final Throwable cause = e.getCause();
+        if (cause != null && cause instanceof RuntimeException) {
+            return (RuntimeException)cause;
+        }
+
+        return new RuntimeException(cause);
+    }
+
     protected void prepareSession(final Description description) throws Throwable {
         final GeckoSessionSettings settings = new GeckoSessionSettings(mDefaultSettings);
 
         applyAnnotations(Arrays.asList(description.getTestClass().getAnnotations()), settings);
         applyAnnotations(description.getAnnotations(), settings);
 
         final List<CallRecord> records = new ArrayList<>();
         final CallbackDelegates waitDelegates = new CallbackDelegates();
@@ -581,17 +589,17 @@ public class GeckoSessionTestRule extend
                     call = testDelegates.prepareMethodCall(method);
                 }
 
                 try {
                     mCurrentMethodCall = call;
                     return method.invoke((call != null) ? call.target
                                                         : Callbacks.Default.INSTANCE, args);
                 } catch (final IllegalAccessException | InvocationTargetException e) {
-                    throw new RuntimeException(e.getCause() != null ? e.getCause() : e);
+                    throw unwrapRuntimeException(e);
                 } finally {
                     mCurrentMethodCall = null;
                 }
             }
         };
 
         final Class<?>[] classes = CALLBACK_CLASSES.toArray(new Class<?>[CALLBACK_CLASSES.size()]);
         mCallbackProxy = Proxy.newProxyInstance(GeckoSession.class.getClassLoader(),
@@ -718,17 +726,17 @@ public class GeckoSessionTestRule extend
         }
 
         try {
             while (true) {
                 final Message msg;
                 try {
                     msg = (Message) getNextMessage.invoke(queue);
                 } catch (final IllegalAccessException | InvocationTargetException e) {
-                    throw new RuntimeException(e.getCause() != null ? e.getCause() : e);
+                    throw unwrapRuntimeException(e);
                 }
                 if (msg.getTarget() == handler && msg.obj == handler) {
                     // Our idle signal.
                     break;
                 } else if (msg.getTarget() == null) {
                     looper.quit();
                     break;
                 }
@@ -864,17 +872,17 @@ public class GeckoSessionTestRule extend
         // custom handlers set.
         for (final Class<?> ifce : CALLBACK_CLASSES) {
             try {
                 assertThat("Callbacks should be set through" +
                            " GeckoSessionTestRule delegate methods",
                            getCallbackGetter(ifce).invoke(mSession), sameInstance(mCallbackProxy));
             } catch (final NoSuchMethodException | IllegalAccessException |
                            InvocationTargetException e) {
-                throw new RuntimeException(e.getCause() != null ? e.getCause() : e);
+                throw unwrapRuntimeException(e);
             }
         }
 
         boolean calledAny = false;
         int index = mLastWaitStart = mLastWaitEnd;
 
         while (!calledAny || !methodCalls.isEmpty()) {
             while (index >= mCallRecords.size()) {
@@ -950,17 +958,17 @@ public class GeckoSessionTestRule extend
             methodCall.incrementCounter();
             assertOrder(methodCall, order);
             order = Math.max(methodCall.getOrder(), order);
 
             try {
                 mCurrentMethodCall = methodCall;
                 record.method.invoke(callback, record.args);
             } catch (final IllegalAccessException | InvocationTargetException e) {
-                throw new RuntimeException(e.getCause() != null ? e.getCause() : e);
+                throw unwrapRuntimeException(e);
             } finally {
                 mCurrentMethodCall = null;
             }
             calledAny = true;
         }
 
         for (final MethodCall methodCall : methodCalls) {
             assertMatchesCount(methodCall);