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 
17 package android.app.cts;
18 
19 import android.app.Instrumentation;
20 import android.app.KeyguardManager;
21 import android.content.Context;
22 import android.os.PowerManager;
23 import android.util.Log;
24 
25 import com.android.compatibility.common.util.CommonTestUtils;
26 import com.android.compatibility.common.util.SystemUtil;
27 
28 public class CtsAppTestUtils {
29     private static final String TAG = CtsAppTestUtils.class.getName();
30 
executeShellCmd(Instrumentation instrumentation, String cmd)31     public static String executeShellCmd(Instrumentation instrumentation, String cmd)
32             throws Exception {
33         final String result = SystemUtil.runShellCommand(instrumentation, cmd);
34         Log.d(TAG, String.format("Output for '%s': %s", cmd, result));
35         return result;
36     }
37 
isScreenInteractive(Context context)38     public static boolean isScreenInteractive(Context context) {
39         final PowerManager powerManager =
40                 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
41         return powerManager.isInteractive();
42     }
43 
isKeyguardLocked(Context context)44     public static boolean isKeyguardLocked(Context context) {
45         final KeyguardManager keyguardManager =
46                 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
47         return keyguardManager.isKeyguardLocked();
48     }
49 
turnScreenOn(Instrumentation instrumentation, Context context)50     public static void turnScreenOn(Instrumentation instrumentation, Context context)
51             throws Exception {
52         executeShellCmd(instrumentation, "input keyevent KEYCODE_WAKEUP");
53         executeShellCmd(instrumentation, "wm dismiss-keyguard");
54         CommonTestUtils.waitUntil("Device does not wake up after 5 seconds", 5,
55                 () ->  {
56                     return isScreenInteractive(context)
57                             && !isKeyguardLocked(context);
58                 });
59     }
60 
makeUidIdle(Instrumentation instrumentation, String packageName)61     public static String makeUidIdle(Instrumentation instrumentation, String packageName)
62             throws Exception {
63         // Force app to go idle now
64         String cmd = "am make-uid-idle " + packageName;
65         return executeShellCmd(instrumentation, cmd);
66     }
67 
68     /**
69      * Find a line containing {@code label} in {@code lines}.
70      */
findLine(String[] lines, CharSequence label)71     public static String findLine(String[] lines, CharSequence label) {
72         for (String line : lines) {
73             if (line.contains(label)) {
74                 return line;
75             }
76         }
77         return null;
78     }
79 
80     /**
81      * This method returns the ambiguously nullable platform type <code>T!</code> in Kotlin.
82      * This allows Kotlin tests cases to pass <code>null</code> to a Java method parameter annotated
83      * with <code>@NonNull</code>, which can be important for validating that the Java code under
84      * test implements runtime <code>null</code> checks.
85      */
platformNull()86     public static <T> T platformNull() {
87         return null;
88     }
89 }
90