Upstream version 7.35.139.0
[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 import org.chromium.base.ThreadUtils;
12
13 import java.util.concurrent.Callable;
14
15 /**
16  * Helper methods for creating and managing criteria.
17  *
18  * <p>
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.
22  */
23 public class CriteriaHelper {
24
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;
29
30     /**
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
35      * before timeout.
36      * @param checkIntervalMs The number of ms between checks.
37      * @return true iff checking has ended with the criteria being satisfied.
38      * @throws InterruptedException
39      */
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();
47         }
48         return isSatisfied;
49     }
50
51     /**
52      * Checks whether the given Criteria is satisfied polling at a default interval.
53      *
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)
58      */
59     public static boolean pollForCriteria(Criteria criteria) throws InterruptedException {
60         return pollForCriteria(criteria, DEFAULT_MAX_TIME_TO_POLL, DEFAULT_POLLING_INTERVAL);
61     }
62
63     /**
64      * Checks whether the given Criteria is satisfied polling at a default interval on the UI
65      * thread.
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)
70      */
71     public static boolean pollForUIThreadCriteria(final Criteria criteria)
72             throws InterruptedException {
73         final Callable<Boolean> callable = new Callable<Boolean>() {
74             @Override
75             public Boolean call() throws Exception {
76                 return criteria.isSatisfied();
77             }
78         };
79
80         return pollForCriteria(new Criteria() {
81             @Override
82             public boolean isSatisfied() {
83                 return ThreadUtils.runOnUiThreadBlockingNoException(callable);
84             }
85         });
86     }
87
88     /**
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.
92      */
93     public static boolean runUntilCriteria(Runnable runnable, Criteria criteria,
94             int maxAttempts, long maxTimeoutMs, long checkIntervalMs) throws InterruptedException {
95         int count = 0;
96         boolean success = false;
97         while (count < maxAttempts && !success) {
98             count++;
99             runnable.run();
100             success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs);
101         }
102         return success;
103     }
104 }