Bug 1442243 - 2. Allow Callbacks interfaces for waitUntilCalled(Class<?>); r=me draft
authorJim Chen <nchen@mozilla.com>
Wed, 07 Mar 2018 16:12:50 -0500
changeset 764511 a6591c0f55dec3e3362a17538c8292ba5fc3b86e
parent 764510 90c573545950db44eb23723ab9df3d7d4902901b
child 764512 5d2bc69aa4eb7cc1fb8a5a202cb1114fb643fb16
push id101776
push userbmo:nchen@mozilla.com
push dateWed, 07 Mar 2018 21:13:11 +0000
reviewersme
bugs1442243
milestone60.0a1
Bug 1442243 - 2. Allow Callbacks interfaces for waitUntilCalled(Class<?>); r=me Allow calls like `waitUntilCalled(Callbacks.NavigationListener::class)` in addition to `waitUntilCalled(GeckoSession.NavigationListener::class)`. MozReview-Commit-ID: CZHYnjGlxtT
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
@@ -166,16 +166,21 @@ class GeckoSessionTestRuleTest {
     }
 
     @Test(expected = AssertionError::class)
     fun waitUntilCalled_throwOnNotGeckoSessionInterface() {
         sessionRule.session.loadTestPath(HELLO_HTML_PATH)
         sessionRule.waitUntilCalled(CharSequence::class)
     }
 
+    fun waitUntilCalled_notThrowOnCallbackInterface() {
+        sessionRule.session.loadTestPath(HELLO_HTML_PATH)
+        sessionRule.waitUntilCalled(Callbacks.ProgressDelegate::class)
+    }
+
     @Test fun waitUntilCalled_anyObjectMethod() {
         sessionRule.session.loadTestPath(HELLO_HTML_PATH)
 
         var counter = 0
 
         sessionRule.waitUntilCalled(object : Callbacks.ProgressDelegate {
             override fun onPageStart(session: GeckoSession, url: String) {
                 counter++
--- 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
@@ -840,35 +840,44 @@ public class GeckoSessionTestRule extend
      * Wait until the specified methods have been called on the specified callback
      * interface. If no methods are specified, wait until any method has been called.
      *
      * @param callback Target callback interface; must be an interface under GeckoSession.
      * @param methods List of methods to wait on; use empty or null or wait on any method.
      */
     public void waitUntilCalled(final @NonNull Class<?> callback,
                                 final @Nullable String... methods) {
-        assertThat("Class should be a GeckoSession interface",
-                   callback, isIn(CALLBACK_CLASSES));
-
         final int length = (methods != null) ? methods.length : 0;
         final Pattern[] patterns = new Pattern[length];
         for (int i = 0; i < length; i++) {
             patterns[i] = Pattern.compile(methods[i]);
         }
 
         final List<MethodCall> waitMethods = new ArrayList<>();
+        boolean isSessionCallback = false;
 
-        for (final Method method : callback.getDeclaredMethods()) {
-            for (final Pattern pattern : patterns) {
-                if (pattern.matcher(method.getName()).matches()) {
+        for (final Class<?> ifce : CALLBACK_CLASSES) {
+            if (!ifce.isAssignableFrom(callback)) {
+                continue;
+            }
+            for (final Method method : ifce.getMethods()) {
+                for (final Pattern pattern : patterns) {
+                    if (!pattern.matcher(method.getName()).matches()) {
+                        continue;
+                    }
                     waitMethods.add(new MethodCall(method, /* requirement */ null));
+                    break;
                 }
             }
+            isSessionCallback = true;
         }
 
+        assertThat("Class should be a GeckoSession interface",
+                   isSessionCallback, equalTo(true));
+
         waitUntilCalled(callback, waitMethods);
     }
 
     /**
      * Wait until the specified methods have been called on the specified object,
      * as specified by any {@link AssertCalled @AssertCalled} annotations. If no
      * {@link AssertCalled @AssertCalled} annotations are found, wait until any method
      * has been called. Only methods belonging to a GeckoSession callback are supported.