• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 package android.accessibilityservice.cts.utils;
18 
19 
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 
23 import java.util.Optional;
24 import java.util.function.Consumer;
25 
26 public class CtsTestUtils {
CtsTestUtils()27     private CtsTestUtils() {}
28 
29     /** The timeout in milliseconds between two events to consider the device idle. */
30     public static final long DEFAULT_IDLE_TIMEOUT_MS = 500L;
31 
32     /** The maximal global timeout in milliseconds in which to wait for an idle state. */
33     public static final long DEFAULT_GLOBAL_TIMEOUT_MS = 5000L;
34 
assertThrows(Runnable action)35     public static Throwable assertThrows(Runnable action) {
36         return assertThrows(Throwable.class, action);
37     }
38 
assertThrows(Class<E> exceptionClass, Runnable action)39     public static <E extends Throwable> E assertThrows(Class<E> exceptionClass, Runnable action) {
40         try {
41             action.run();
42             throw new AssertionError("Expected an exception");
43         } catch (Throwable e) {
44             if (exceptionClass.isInstance(e)) {
45                 return (E) e;
46             }
47             throw new RuntimeException(e);
48         }
49     }
50 
runIfNotNull(T callee, Consumer<T> action)51     public static <T> void runIfNotNull(T callee, Consumer<T> action) {
52         Optional.ofNullable(callee).ifPresent(action);
53     }
54 
55     /** Returns {@code true} if run on an Automotive device */
isAutomotive(Context context)56     public static boolean isAutomotive(Context context) {
57         PackageManager pm = context.getPackageManager();
58         return pm != null && pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
59     }
60 }
61