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