1 /* 2 * Copyright (C) 2018 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.helpers; 17 18 import android.app.KeyguardManager; 19 import android.content.Context; 20 import android.hardware.display.DisplayManager; 21 import android.os.RemoteException; 22 import android.os.SystemClock; 23 import android.view.Display; 24 25 import androidx.test.InstrumentationRegistry; 26 import androidx.test.uiautomator.UiDevice; 27 28 import java.io.IOException; 29 30 public class HelperTestUtility { 31 32 // Clear the cache. 33 private static final String CLEAR_CACHE_CMD = "echo 3 > /proc/sys/vm/drop_caches"; 34 // Dismiss the lockscreen. 35 private static final String UNLOCK_CMD = "wm dismiss-keyguard"; 36 // Input a keycode. 37 private static final String KEYEVENT_CMD_TEMPLATE = "input keyevent %s"; 38 // Launch an app. 39 private static final String LAUNCH_APP_CMD_TEMPLATE = 40 "monkey -p %s -c android.intent.category.LAUNCHER 1"; 41 // Keycode(s) for convenience functions. 42 private static final String KEYCODE_WAKEUP = "KEYCODE_WAKEUP"; 43 // Delay between actions happening in the device. 44 public static final int ACTION_DELAY = 4000; 45 46 private static UiDevice mDevice; 47 48 /** 49 * Returns the active {@link UiDevice} to interact with. 50 */ getUiDevice()51 public static UiDevice getUiDevice() { 52 if (mDevice == null) { 53 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 54 } 55 return mDevice; 56 } 57 58 /** 59 * Runs a shell command, {@code cmd}, and returns the output. 60 */ executeShellCommand(String cmd)61 public static String executeShellCommand(String cmd) { 62 try { 63 return getUiDevice().executeShellCommand(cmd); 64 } catch (IOException e) { 65 throw new RuntimeException(e); 66 } 67 } 68 69 /** 70 * Clears the cache. 71 */ clearCache()72 public static void clearCache() { 73 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 74 executeShellCommand(CLEAR_CACHE_CMD); 75 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 76 } 77 78 /** 79 * Close the test app and clear the cache. 80 * TODO: Replace it with the rules. 81 */ clearApp(String killCmd)82 public static void clearApp(String killCmd) { 83 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 84 executeShellCommand(killCmd); 85 clearCache(); 86 } 87 88 /** 89 * Send a keycode via adb. 90 */ sendKeyCode(String keyCode)91 public static void sendKeyCode(String keyCode) { 92 executeShellCommand(String.format(KEYEVENT_CMD_TEMPLATE, keyCode)); 93 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 94 } 95 96 /** 97 * Launch an app via adb. 98 */ launchPackageViaAdb(String pkgName)99 public static void launchPackageViaAdb(String pkgName) { 100 executeShellCommand(String.format(LAUNCH_APP_CMD_TEMPLATE, pkgName)); 101 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 102 } 103 104 /** 105 * Wake up and unlock the device if the device is not in the unlocked state already. 106 */ wakeUpAndUnlock()107 public static void wakeUpAndUnlock() { 108 // Check whether display is on using DisplayManager. 109 Context context = InstrumentationRegistry.getContext(); 110 DisplayManager displayManager = 111 (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); 112 boolean displayIsOn = (displayManager.getDisplays()[0].getState() == Display.STATE_ON); 113 // Check whether lockscreen is dismissed using KeyguardManager. 114 KeyguardManager keyguardManager = 115 (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); 116 boolean displayIsUnlocked = !keyguardManager.isKeyguardLocked(); 117 if (!(displayIsOn && displayIsUnlocked)) { 118 sendKeyCode(KEYCODE_WAKEUP); 119 executeShellCommand(UNLOCK_CMD); 120 SystemClock.sleep(HelperTestUtility.ACTION_DELAY); 121 } 122 } 123 124 /** 125 * This method will lock orientation. 126 */ setOrientationNatural()127 public static void setOrientationNatural() { 128 try { 129 getUiDevice().setOrientationNatural(); 130 } catch (RemoteException e) { 131 throw new RuntimeException(e); 132 } 133 } 134 135 /** 136 * This method will unlock orientation. 137 */ unfreezeRotation()138 public static void unfreezeRotation() { 139 try { 140 mDevice.unfreezeRotation(); 141 } catch (RemoteException e) { 142 throw new RuntimeException(e); 143 } 144 } 145 } 146 147