CriteriaHelper.java revision a02191e04bc25c4935f804f2c080ae28663d096d
1ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// Copyright 2012 The Chromium Authors. All rights reserved. 2ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// Use of this source code is governed by a BSD-style license that can be 3ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// found in the LICENSE file. 4ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 5ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownpackage org.chromium.content.browser.test.util; 6ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 7ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownimport static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; 8ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 9ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownimport android.os.SystemClock; 10ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 11ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownimport org.chromium.base.ThreadUtils; 12ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 13ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownimport java.util.concurrent.Callable; 14ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 15ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** 16ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * Helper methods for creating and managing criteria. 17ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * 18ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * <p> 19ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * If possible, use callbacks or testing delegates instead of criteria as they 20ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * do not introduce any polling delays. Should only use Criteria if no suitable 21ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * other approach exists. 22ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown */ 23ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownpublic class CriteriaHelper { 24ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 25ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /** The default maximum time to wait for a criteria to become valid. */ 26ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown public static final long DEFAULT_MAX_TIME_TO_POLL = scaleTimeout(3000); 27ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /** The default polling interval to wait between checking for a satisfied criteria. */ 28ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown public static final long DEFAULT_POLLING_INTERVAL = 50; 29ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 30ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /** 31ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * Checks whether the given Criteria is satisfied at a given interval, until either 32ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * the criteria is satisfied, or the specified maxTimeoutMs number of ms has elapsed. 33ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * @param criteria The Criteria that will be checked. 34ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * @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} 105