CriteriaHelper.java revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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.
4
5package org.chromium.content.browser.test.util;
6
7import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9/**
10 * Helper methods for creating and managing criteria.
11 *
12 * <p>
13 * If possible, use callbacks or testing delegates instead of criteria as they
14 * do not introduce any polling delays.  Should only use Criteria if no suitable
15 * other approach exists.
16 */
17public class CriteriaHelper {
18
19    /** The default maximum time to wait for a criteria to become valid. */
20    public static final long DEFAULT_MAX_TIME_TO_POLL = scaleTimeout(3000);
21    /** The default polling interval to wait between checking for a satisfied criteria. */
22    public static final long DEFAULT_POLLING_INTERVAL = 50;
23
24    /**
25     * Checks whether the given Criteria is satisfied at a given interval, until either
26     * the criteria is satisfied, or the specified maxTimeoutMs number of ms has elapsed.
27     * @param criteria The Criteria that will be checked.
28     * @param maxTimeoutMs The maximum number of ms that this check will be performed for
29     * before timeout.
30     * @param checkIntervalMs The number of ms between checks.
31     * @return true iff checking has ended with the criteria being satisfied.
32     * @throws InterruptedException
33     */
34    public static boolean pollForCriteria(Criteria criteria, long maxTimeoutMs,
35            long checkIntervalMs) throws InterruptedException {
36        boolean isSatisfied = criteria.isSatisfied();
37        long startTime = System.currentTimeMillis();
38        while (!isSatisfied && System.currentTimeMillis() - startTime < maxTimeoutMs) {
39            Thread.sleep(checkIntervalMs);
40            isSatisfied = criteria.isSatisfied();
41        }
42        return isSatisfied;
43    }
44
45    /**
46     * Checks whether the given Criteria is satisfied polling at a default interval.
47     *
48     * @param criteria The Criteria that will be checked.
49     * @return iff checking has ended with the criteria being satisfied.
50     * @throws InterruptedException
51     * @see #pollForCriteria(Criteria, long, long)
52     */
53    public static boolean pollForCriteria(Criteria criteria) throws InterruptedException {
54        return pollForCriteria(criteria, DEFAULT_MAX_TIME_TO_POLL, DEFAULT_POLLING_INTERVAL);
55    }
56
57    /**
58     * Performs the runnable action, then checks whether the given criteria are satisfied
59     * until the specified timeout, using the pollForCriteria method. If not, then the runnable
60     * action is performed again, to a maximum of maxAttempts tries.
61     */
62    public static boolean runUntilCriteria(Runnable runnable, Criteria criteria,
63            int maxAttempts, long maxTimeoutMs, long checkIntervalMs) throws InterruptedException {
64        int count = 0;
65        boolean success = false;
66        while (count < maxAttempts && !success) {
67            count++;
68            runnable.run();
69            success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs);
70        }
71        return success;
72    }
73}
74