• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 package com.android.compatibility.common.util;
17 
18 import static org.junit.Assert.fail;
19 
20 // TODO(b/131736394): Remove duplication with HostSideTestUtils.
21 /** Utility class for tests. */
22 public class CommonTestUtils {
23     @FunctionalInterface
24     public interface BooleanSupplierWithThrow<E extends Throwable> {
getAsBoolean()25         boolean getAsBoolean() throws E;
26     }
27 
28     /** Wait until {@code predicate} is satisfied, or fail, with a given timeout. */
waitUntil( String message, long timeoutSeconds, BooleanSupplierWithThrow<E> predicate)29     public static <E extends Throwable> void waitUntil(
30             String message, long timeoutSeconds, BooleanSupplierWithThrow<E> predicate)
31             throws E, InterruptedException {
32         int sleep = 125;
33         final long timeout = System.currentTimeMillis() + timeoutSeconds * 1000;
34         while (System.currentTimeMillis() < timeout) {
35             if (predicate.getAsBoolean()) {
36                 return;
37             }
38             Thread.sleep(sleep);
39         }
40         fail(message);
41     }
42 }
43