1 /*
2  * Copyright (C) 2022 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.server.wm;
18 
19 import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE_IDENTIFIER;
20 import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE_IDENTIFIER;
21 
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 
25 import android.hardware.devicestate.DeviceState;
26 
27 import androidx.annotation.NonNull;
28 
29 import com.android.compatibility.common.util.SystemUtil;
30 import com.android.compatibility.common.util.ThrowingRunnable;
31 
32 /** Utility methods for CTS tests requiring the use of {@DeviceStateManager}. */
33 public final class DeviceStateUtils {
34     /**
35      * Runs the supplied {@code runnable} with the
36      * {@link android.Manifest.permission.CONTROL_DEVICE_STATE} permission held.
37      *
38      * @throws Throwable if the runnable throws an exception during execution.
39      */
runWithControlDeviceStatePermission(@onNull ThrowingRunnable runnable)40     public static void runWithControlDeviceStatePermission(@NonNull ThrowingRunnable runnable)
41             throws Throwable {
42         try {
43             SystemUtil.runWithShellPermissionIdentity(runnable,
44                     android.Manifest.permission.CONTROL_DEVICE_STATE);
45         } catch (RuntimeException e) {
46             // runWithShellPermissionIdentity() wraps exceptions thrown by the underlying runnable
47             // in runtime exceptions.
48             Throwable t = e.getCause();
49             if (t != null) {
50                 throw t;
51             }
52             throw e;
53         }
54     }
55 
56     /**
57      * Asserts that the provided {@code state} is in the range
58      * [{@link MINIMUM_DEVICE_STATE}, {@link MAXIMUM_DEVICE_STATE}].
59      */
assertValidState(int state)60     public static void assertValidState(int state) {
61         assertTrue("Device state identifier is smaller than the minimum state identifier value",
62                 state >= MINIMUM_DEVICE_STATE_IDENTIFIER);
63         assertTrue("Device state identifier is larger than the maximum state identifier value",
64                 state <= MAXIMUM_DEVICE_STATE_IDENTIFIER);
65     }
66 
67     /**
68      * asserts that the provided {@code state} has an identifier in the range
69      * [{@link MINIMUM_DEVICE_STATE}, {@link MAXIMUM_DEVICE_STATE}] and a non-null {@code name}.
70      */
assertValidDeviceState(DeviceState state)71     public static void assertValidDeviceState(DeviceState state) {
72         assertTrue("Device state identifier is smaller than the minimum state identifier value",
73                 state.getIdentifier() >= MINIMUM_DEVICE_STATE_IDENTIFIER);
74         assertTrue("Device state identifier is larger than the maximum state identifier value",
75                 state.getIdentifier() <= MAXIMUM_DEVICE_STATE_IDENTIFIER);
76         assertNotNull("Device state name was null", state.getName());
77     }
78 
DeviceStateUtils()79     private DeviceStateUtils() {}
80 }
81