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