/* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.customization.testutils; import android.os.SystemClock; import org.junit.Assert; /** * A utility class for waiting for a condition to be true. */ public class Wait { private static final long DEFAULT_SLEEP_MS = 200; public static void atMost(String message, Condition condition, long timeout) { atMost(message, condition, timeout, DEFAULT_SLEEP_MS); } public static void atMost(String message, Condition condition, long timeout, long sleepMillis) { long endTime = SystemClock.uptimeMillis() + timeout; while (SystemClock.uptimeMillis() < endTime) { try { if (condition.isTrue()) { return; } } catch (Throwable t) { throw new RuntimeException(t); } SystemClock.sleep(sleepMillis); } // Check once more before returning false. try { if (condition.isTrue()) { return; } } catch (Throwable t) { throw new RuntimeException(t); } Assert.fail(message); } }