1 package com.android.launcher3.util; 2 3 import android.os.SystemClock; 4 5 import org.junit.Assert; 6 7 /** 8 * A utility class for waiting for a condition to be true. 9 */ 10 public class Wait { 11 12 private static final long DEFAULT_SLEEP_MS = 200; 13 atMost(String message, Condition condition, long timeout)14 public static void atMost(String message, Condition condition, long timeout) { 15 atMost(message, condition, timeout, DEFAULT_SLEEP_MS); 16 } 17 atMost(String message, Condition condition, long timeout, long sleepMillis)18 public static void atMost(String message, Condition condition, long timeout, long sleepMillis) { 19 long endTime = SystemClock.uptimeMillis() + timeout; 20 while (SystemClock.uptimeMillis() < endTime) { 21 try { 22 if (condition.isTrue()) { 23 return; 24 } 25 } catch (Throwable t) { 26 throw new RuntimeException(t); 27 } 28 SystemClock.sleep(sleepMillis); 29 } 30 31 // Check once more before returning false. 32 try { 33 if (condition.isTrue()) { 34 return; 35 } 36 } catch (Throwable t) { 37 throw new RuntimeException(t); 38 } 39 Assert.fail(message); 40 } 41 } 42