Bug 946656 - Correct regex that determines a loading page in WaitHelper. r=margaret, a=test-only
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 18 Dec 2013 09:55:32 -0800
changeset 175465 8b6b7d90f75e4290f549a3deffd0d60e82b8d3c8
parent 175464 61f553e5db49afda821d2ba468c277786f290c0e
child 175466 2f891e8ee3c4cf9fa6d56ed651e962323a6d6349
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret, test-only
bugs946656
milestone28.0a2
Bug 946656 - Correct regex that determines a loading page in WaitHelper. r=margaret, a=test-only
mobile/android/base/tests/helpers/WaitHelper.java
--- a/mobile/android/base/tests/helpers/WaitHelper.java
+++ b/mobile/android/base/tests/helpers/WaitHelper.java
@@ -10,16 +10,18 @@ import org.mozilla.gecko.Actions;
 import org.mozilla.gecko.Actions.EventExpecter;
 import org.mozilla.gecko.tests.components.ToolbarComponent;
 import org.mozilla.gecko.tests.UITestContext;
 import org.mozilla.gecko.tests.UITestContext.ComponentType;
 
 import com.jayway.android.robotium.solo.Condition;
 import com.jayway.android.robotium.solo.Solo;
 
+import java.util.regex.Pattern;
+
 /**
  * Provides functionality related to waiting on certain events to happen.
  */
 public final class WaitHelper {
     // TODO: Make public for when Solo.waitForCondition is used directly (i.e. do not want
     // assertion from waitFor)?
     private static final int DEFAULT_MAX_WAIT_MS = 5000;
     private static final int PAGE_LOAD_WAIT_MS = 10000;
@@ -98,57 +100,72 @@ public final class WaitHelper {
             // on and let the assertions fail if applicable.
             final boolean hasTimedOut = !sSolo.waitForCondition(new Condition() {
                 @Override
                 public boolean isSatisfied() {
                     return verifier.hasStateChanged();
                 }
             }, CHANGE_WAIT_MS);
 
-            if (hasTimedOut) {
-                sContext.dumpLog(verifier.getClass().getName() + " timed out.");
-            }
+            sContext.dumpLog(verifier.getLogTag() +
+                    (hasTimedOut ? "timed out." : "was satisfied."));
         }
     }
 
     /**
      * Implementations of this interface verify that the state of the test has changed from
      * the invocation of storeState to the invocation of hasStateChanged. A boolean will be
      * returned from hasStateChanged, indicating this change of status.
      */
     private static interface ChangeVerifier {
+        public String getLogTag();
+
         /**
          * Stores the initial state of the system. This system state is used to diff against
          * the end state to determine if the system has changed. Since this is just a diff
          * (with a timeout), this method could potentially store state inconsistent with
          * what is visible to the user.
          */
         public void storeState();
         public boolean hasStateChanged();
     }
 
     private static class ToolbarTitleTextChangeVerifier implements ChangeVerifier {
+        private static final String LOGTAG =
+                ToolbarTitleTextChangeVerifier.class.getSimpleName() + ": ";
+
         // A regex that matches the page title that shows up while the page is loading.
-        private static final String LOADING_REGEX = "^[A-Za-z]{3,9}://";
+        private static final Pattern LOADING_PREFIX = Pattern.compile("[A-Za-z]{3,9}://");
+
+        private CharSequence mOldTitleText;
 
-        private CharSequence oldTitleText;
+        @Override
+        public String getLogTag() {
+            return LOGTAG;
+        }
 
         @Override
         public void storeState() {
-            oldTitleText = sToolbar.getPotentiallyInconsistentTitle();
+            mOldTitleText = sToolbar.getPotentiallyInconsistentTitle();
+            sContext.dumpLog(LOGTAG + "stored title, \"" + mOldTitleText + "\".");
         }
 
         @Override
         public boolean hasStateChanged() {
             // TODO: Additionally, consider Solo.waitForText.
             // TODO: Robocop sleeps .5 sec between calls. Cache title view?
             final CharSequence title = sToolbar.getPotentiallyInconsistentTitle();
 
             // TODO: Handle the case where the URL is shown instead of page title by preference.
             // HACK: We want to wait until the title changes to the state a tester may assert
             // (e.g. the page title). However, the title is set to the URL before the title is
             // loaded from the server and set as the final page title; we ignore the
             // intermediate URL loading state here.
-            final boolean isLoading = title.toString().matches(LOADING_REGEX);
-            return !isLoading && !oldTitleText.equals(title);
+            final boolean isLoading = LOADING_PREFIX.matcher(title).lookingAt();
+            final boolean hasStateChanged = !isLoading && !mOldTitleText.equals(title);
+
+            if (hasStateChanged) {
+                sContext.dumpLog(LOGTAG + "state changed to title, \"" + title + "\".");
+            }
+            return hasStateChanged;
         }
     }
 }