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