1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser.test.util;
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
9 import android.os.SystemClock;
11 import org.chromium.base.ThreadUtils;
13 import java.util.concurrent.Callable;
16 * Helper methods for creating and managing criteria.
19 * If possible, use callbacks or testing delegates instead of criteria as they
20 * do not introduce any polling delays. Should only use Criteria if no suitable
21 * other approach exists.
23 public class CriteriaHelper {
25 /** The default maximum time to wait for a criteria to become valid. */
26 public static final long DEFAULT_MAX_TIME_TO_POLL = scaleTimeout(3000);
27 /** The default polling interval to wait between checking for a satisfied criteria. */
28 public static final long DEFAULT_POLLING_INTERVAL = 50;
31 * Checks whether the given Criteria is satisfied at a given interval, until either
32 * the criteria is satisfied, or the specified maxTimeoutMs number of ms has elapsed.
33 * @param criteria The Criteria that will be checked.
34 * @param maxTimeoutMs The maximum number of ms that this check will be performed for
36 * @param checkIntervalMs The number of ms between checks.
37 * @return true iff checking has ended with the criteria being satisfied.
38 * @throws InterruptedException
40 public static boolean pollForCriteria(Criteria criteria, long maxTimeoutMs,
41 long checkIntervalMs) throws InterruptedException {
42 boolean isSatisfied = criteria.isSatisfied();
43 long startTime = SystemClock.uptimeMillis();
44 while (!isSatisfied && SystemClock.uptimeMillis() - startTime < maxTimeoutMs) {
45 Thread.sleep(checkIntervalMs);
46 isSatisfied = criteria.isSatisfied();
52 * Checks whether the given Criteria is satisfied polling at a default interval.
54 * @param criteria The Criteria that will be checked.
55 * @return iff checking has ended with the criteria being satisfied.
56 * @throws InterruptedException
57 * @see #pollForCriteria(Criteria, long, long)
59 public static boolean pollForCriteria(Criteria criteria) throws InterruptedException {
60 return pollForCriteria(criteria, DEFAULT_MAX_TIME_TO_POLL, DEFAULT_POLLING_INTERVAL);
64 * Checks whether the given Criteria is satisfied polling at a default interval on the UI
66 * @param criteria The Criteria that will be checked.
67 * @return iff checking has ended with the criteria being satisfied.
68 * @throws InterruptedException
69 * @see #pollForCriteria(Criteria)
71 public static boolean pollForUIThreadCriteria(final Criteria criteria)
72 throws InterruptedException {
73 final Callable<Boolean> callable = new Callable<Boolean>() {
75 public Boolean call() throws Exception {
76 return criteria.isSatisfied();
80 return pollForCriteria(new Criteria() {
82 public boolean isSatisfied() {
83 return ThreadUtils.runOnUiThreadBlockingNoException(callable);
89 * Performs the runnable action, then checks whether the given criteria are satisfied
90 * until the specified timeout, using the pollForCriteria method. If not, then the runnable
91 * action is performed again, to a maximum of maxAttempts tries.
93 public static boolean runUntilCriteria(Runnable runnable, Criteria criteria,
94 int maxAttempts, long maxTimeoutMs, long checkIntervalMs) throws InterruptedException {
96 boolean success = false;
97 while (count < maxAttempts && !success) {
100 success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs);