Index: content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelper.java |
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelper.java b/content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelper.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..434682b1b00fda9a2105b0a7770adf967bb4a1fb |
--- /dev/null |
+++ b/content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelper.java |
@@ -0,0 +1,99 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.content.browser.test; |
+ |
+import org.chromium.content.browser.ContentView; |
+ |
+import java.lang.reflect.Field; |
+ |
+ |
+/** |
+ * Helper methods for creating and managing criteria. |
+ */ |
+public class CriteriaHelper { |
+ |
+ /** The default maximum time to wait for a criteria to become valid. */ |
+ public static final long DEFAULT_MAX_TIME_TO_POLL = 3000; |
+ /** The default polling interval to wait between checking for a satisfied criteria. */ |
+ public static final long DEFAULT_POLLING_INTERVAL = 50; |
+ |
+ /** |
Yaron
2012/09/05 06:10:43
indentation looks off
Ted C
2012/09/05 18:45:35
Done.
|
+ * Checks whether the given Criteria is satisfied at a given interval, until either |
+ * the criteria is satisfied, or the specified maxTimeoutMs number of ms has elapsed. |
+ * @param criteria The Criteria that will be checked. |
+ * @param maxTimeoutMs The maximum number of ms that this check will be performed for |
+ * before timeout. |
+ * @param checkIntervalMs The number of ms between checks. |
+ * @return true iff checking has ended with the criteria being satisfied. |
+ * @throws InterruptedException |
+ */ |
+ public static boolean pollForCriteria(Criteria criteria, long maxTimeoutMs, |
+ long checkIntervalMs) throws InterruptedException { |
+ boolean isSatisfied = criteria.isSatisfied(); |
+ long startTime = System.currentTimeMillis(); |
+ while (!isSatisfied && System.currentTimeMillis() - startTime < maxTimeoutMs) { |
+ Thread.sleep(checkIntervalMs); |
+ isSatisfied = criteria.isSatisfied(); |
+ } |
+ return isSatisfied; |
+ } |
+ |
+ /** |
+ * Checks whether the given Criteria is satisfied polling at a default interval. |
+ * |
+ * @param criteria The Criteria that will be checked. |
+ * @return iff checking has ended with the criteria being satisfied. |
+ * @throws InterruptedException |
+ * @see #pollForCriteria(Criteria, long, long) |
+ */ |
+ public static boolean pollForCriteria(Criteria criteria) throws InterruptedException { |
+ return pollForCriteria(criteria, DEFAULT_MAX_TIME_TO_POLL, DEFAULT_POLLING_INTERVAL); |
+ } |
+ |
+ /** |
+ * Performs the runnable action, then checks whether the given criteria are satisfied |
+ * until the specified timeout, using the pollForCriteria method. If not, then the runnable |
+ * action is performed again, to a maximum of maxAttempts tries. |
+ */ |
+ public static boolean runUntilCriteria(Runnable runnable, Criteria criteria, |
+ int maxAttempts, long maxTimeoutMs, long checkIntervalMs) throws InterruptedException { |
+ int count = 0; |
+ boolean success = false; |
+ while (count < maxAttempts && !success) { |
+ count++; |
+ runnable.run(); |
+ success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs); |
+ } |
+ return success; |
+ } |
+ |
+ public static class NotZoomingCriteria implements Criteria { |
Yaron
2012/09/05 06:10:43
How re-used is this? Seems like it doesn't really
Ted C
2012/09/05 18:45:35
Removed as it isn't used yet.
|
+ private ContentView mContentView; |
+ private float mCurrentMagnifyScale; |
+ |
+ public NotZoomingCriteria(ContentView chromeView) { |
+ mContentView = chromeView; |
+ } |
+ |
+ @Override |
+ public boolean isSatisfied() { |
+ float lastMagnifyScale = mCurrentMagnifyScale; |
+ try { |
+ mCurrentMagnifyScale = getMagnifyScale(); |
+ } catch (Exception e) { |
+ return false; |
+ } |
+ return lastMagnifyScale != 0 && lastMagnifyScale == mCurrentMagnifyScale; |
+ } |
+ |
+ private float getMagnifyScale() throws NoSuchFieldException, |
+ IllegalArgumentException, IllegalAccessException { |
+ Field field; |
+ field = ContentView.class.getDeclaredField("mMagnifyScale"); |
+ field.setAccessible(true); |
+ return field.getFloat(mContentView); |
+ } |
+ } |
+} |