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